Array cleanup.

This commit is contained in:
Leonardo Zide 2021-01-05 12:27:45 -08:00
parent cb710206b7
commit 176b238faa
2 changed files with 42 additions and 55 deletions

View file

@ -1,6 +1,5 @@
#include "lc_global.h" #include "lc_global.h"
#include "object.h" #include "object.h"
#include "lc_file.h"
lcObject::lcObject(lcObjectType ObjectType) lcObject::lcObject(lcObjectType ObjectType)
: mObjectType(ObjectType) : mObjectType(ObjectType)
@ -68,45 +67,30 @@ const T& lcObjectKeyArray<T>::CalculateKey(lcStep Step) const
template<typename T> template<typename T>
void lcObjectKeyArray<T>::ChangeKey(const T& Value, lcStep Step, bool AddKey) void lcObjectKeyArray<T>::ChangeKey(const T& Value, lcStep Step, bool AddKey)
{ {
for (int KeyIdx = 0; KeyIdx < mKeys.GetSize(); KeyIdx++) for (std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++)
{ {
lcObjectKey<T>* Key = &mKeys[KeyIdx]; if (KeyIt->Step < Step)
continue;
if (Key->Step == Step) if (KeyIt->Step == Step)
{ KeyIt->Value = Value;
Key->Value = Value; else if (AddKey)
mKeys.insert(KeyIt, lcObjectKey<T>{ Step, Value });
return; else if (KeyIt == mKeys.begin())
} 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;
}
}
if (AddKey || mKeys.GetSize() == 0)
{
lcObjectKey<T>* Key = &mKeys.Add();
Key->Step = Step;
Key->Value = Value;
}
else else
{ {
lcObjectKey<T>* Key = &mKeys[mKeys.GetSize() - 1]; KeyIt = KeyIt - 1;
Key->Value = Value; KeyIt->Value = Value;
} }
return;
}
if (AddKey || mKeys.empty())
mKeys.emplace_back(lcObjectKey<T>{ Step, Value });
else
mKeys.back().Value = Value;
} }
template<typename T> template<typename T>
@ -114,47 +98,50 @@ void lcObjectKeyArray<T>::InsertTime(lcStep Start, lcStep Time)
{ {
bool EndKey = false; bool EndKey = false;
for (int KeyIdx = 0; KeyIdx < mKeys.GetSize(); KeyIdx++) for (std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
{ {
lcObjectKey<T>& Key = mKeys[KeyIdx]; if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
{
if ((Key.Step < Start) || (Key.Step == 1)) KeyIt++;
continue; continue;
}
if (EndKey) if (EndKey)
{ {
mKeys.RemoveIndex(KeyIdx); KeyIt = mKeys.erase(KeyIt);
KeyIdx--;
continue; 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; EndKey = true;
} }
else else
Key.Step += Time; KeyIt->Step += Time;
KeyIt++;
} }
} }
template<typename T> template<typename T>
void lcObjectKeyArray<T>::RemoveTime(lcStep Start, lcStep Time) void lcObjectKeyArray<T>::RemoveTime(lcStep Start, lcStep Time)
{ {
for (int KeyIdx = 0; KeyIdx < mKeys.GetSize(); KeyIdx++) for (std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
{ {
lcObjectKey<T>& Key = mKeys[KeyIdx]; if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
if ((Key.Step < Start) || (Key.Step == 1))
continue;
if (Key.Step < Start + Time)
{ {
mKeys.RemoveIndex(KeyIdx); KeyIt++;
KeyIdx--;
continue; continue;
} }
Key.Step -= Time; if (KeyIt->Step < Start + Time)
{
KeyIt = mKeys.erase(KeyIt);
continue;
}
KeyIt->Step -= Time;
KeyIt++;
} }
} }

View file

@ -26,17 +26,17 @@ class lcObjectKeyArray
public: public:
int GetSize() const int GetSize() const
{ {
return mKeys.GetSize(); return static_cast<int>(mKeys.size());
} }
bool IsEmpty() const bool IsEmpty() const
{ {
return mKeys.IsEmpty(); return mKeys.empty();
} }
void RemoveAll() void RemoveAll()
{ {
mKeys.RemoveAll(); mKeys.clear();
} }
void SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; void SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const;
@ -47,7 +47,7 @@ public:
void RemoveTime(lcStep Start, lcStep Time); void RemoveTime(lcStep Start, lcStep Time);
protected: protected:
lcArray<lcObjectKey<T>> mKeys; std::vector<lcObjectKey<T>> mKeys;
}; };
struct lcObjectSection struct lcObjectSection