From 176b238faa60641d234a9928981f11b4072e29ef Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Tue, 5 Jan 2021 12:27:45 -0800 Subject: [PATCH] Array cleanup. --- common/object.cpp | 89 ++++++++++++++++++++--------------------------- common/object.h | 8 ++--- 2 files changed, 42 insertions(+), 55 deletions(-) diff --git a/common/object.cpp b/common/object.cpp index 0a93dc27..33eda7e1 100644 --- a/common/object.cpp +++ b/common/object.cpp @@ -1,6 +1,5 @@ #include "lc_global.h" #include "object.h" -#include "lc_file.h" lcObject::lcObject(lcObjectType ObjectType) : mObjectType(ObjectType) @@ -68,45 +67,30 @@ const T& lcObjectKeyArray::CalculateKey(lcStep Step) const template void lcObjectKeyArray::ChangeKey(const T& Value, lcStep Step, bool AddKey) { - for (int KeyIdx = 0; KeyIdx < mKeys.GetSize(); KeyIdx++) + for (std::vector>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++) { - lcObjectKey* Key = &mKeys[KeyIdx]; + if (KeyIt->Step < Step) + continue; - if (Key->Step == Step) + if (KeyIt->Step == Step) + KeyIt->Value = Value; + else if (AddKey) + mKeys.insert(KeyIt, lcObjectKey{ Step, Value }); + else if (KeyIt == mKeys.begin()) + KeyIt->Value = Value; + else { - Key->Value = Value; - - return; + KeyIt = KeyIt - 1; + KeyIt->Value = Value; } - if (Key->Step > Step) - { - if (AddKey) - { - Key = &mKeys.InsertAt(KeyIdx); - Key->Step = Step; - } - else if (KeyIdx) - Key = &mKeys[KeyIdx - 1]; - - Key->Value = Value; - - return; - } + return; } - if (AddKey || mKeys.GetSize() == 0) - { - lcObjectKey* Key = &mKeys.Add(); - - Key->Step = Step; - Key->Value = Value; - } + if (AddKey || mKeys.empty()) + mKeys.emplace_back(lcObjectKey{ Step, Value }); else - { - lcObjectKey* Key = &mKeys[mKeys.GetSize() - 1]; - Key->Value = Value; - } + mKeys.back().Value = Value; } template @@ -114,47 +98,50 @@ void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time) { bool EndKey = false; - for (int KeyIdx = 0; KeyIdx < mKeys.GetSize(); KeyIdx++) + for (std::vector>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();) { - lcObjectKey& Key = mKeys[KeyIdx]; - - if ((Key.Step < Start) || (Key.Step == 1)) + if ((KeyIt->Step < Start) || (KeyIt->Step == 1)) + { + KeyIt++; continue; + } if (EndKey) { - mKeys.RemoveIndex(KeyIdx); - KeyIdx--; + KeyIt = mKeys.erase(KeyIt); continue; } - if (Key.Step >= LC_STEP_MAX - Time) + if (KeyIt->Step >= LC_STEP_MAX - Time) { - Key.Step = LC_STEP_MAX; + KeyIt->Step = LC_STEP_MAX; EndKey = true; } else - Key.Step += Time; + KeyIt->Step += Time; + + KeyIt++; } } template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time) { - for (int KeyIdx = 0; KeyIdx < mKeys.GetSize(); KeyIdx++) + for (std::vector>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();) { - lcObjectKey& Key = mKeys[KeyIdx]; - - if ((Key.Step < Start) || (Key.Step == 1)) - continue; - - if (Key.Step < Start + Time) + if ((KeyIt->Step < Start) || (KeyIt->Step == 1)) { - mKeys.RemoveIndex(KeyIdx); - KeyIdx--; + KeyIt++; continue; } - Key.Step -= Time; + if (KeyIt->Step < Start + Time) + { + KeyIt = mKeys.erase(KeyIt); + continue; + } + + KeyIt->Step -= Time; + KeyIt++; } } diff --git a/common/object.h b/common/object.h index 64b60d62..85a2115e 100644 --- a/common/object.h +++ b/common/object.h @@ -26,17 +26,17 @@ class lcObjectKeyArray public: int GetSize() const { - return mKeys.GetSize(); + return static_cast(mKeys.size()); } bool IsEmpty() const { - return mKeys.IsEmpty(); + return mKeys.empty(); } void RemoveAll() { - mKeys.RemoveAll(); + mKeys.clear(); } void SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; @@ -47,7 +47,7 @@ public: void RemoveTime(lcStep Start, lcStep Time); protected: - lcArray> mKeys; + std::vector> mKeys; }; struct lcObjectSection