- made some minor changes to TArray after finding out that "new int()" is not the same as "new int".

With parentheses this initializes to 0 which created needless initialization code in a few places.
This commit is contained in:
Christoph Oelckers 2018-11-10 10:43:35 +01:00
parent cfe51f0c30
commit 602ea8f723
1 changed files with 24 additions and 20 deletions

View File

@ -164,7 +164,7 @@ public:
Array = (T *)M_Malloc (sizeof(T)*max); Array = (T *)M_Malloc (sizeof(T)*max);
if (reserve) if (reserve)
{ {
for (unsigned i = 0; i < Count; i++) ::new(&Array[i]) T(); ConstructEmpty(0, Count - 1);
} }
} }
TArray (const TArray<T,TT> &other) TArray (const TArray<T,TT> &other)
@ -362,17 +362,6 @@ public:
} }
} }
// Reserves a range of entries in the middle of the array, shifting elements as needed
void ReserveAt(unsigned int index, unsigned int amount)
{
Grow(amount);
memmove(&Array[index + amount], &Array[index], sizeof(T)*(Count - index - amount));
for (unsigned i = 0; i < amount; i++)
{
::new ((void *)&Array[index + i]) T();
}
}
void ShrinkToFit () void ShrinkToFit ()
{ {
if (Most > Count) if (Most > Count)
@ -411,10 +400,7 @@ public:
{ {
// Adding new entries // Adding new entries
Grow (amount - Count); Grow (amount - Count);
for (unsigned int i = Count; i < amount; ++i) ConstructEmpty(Count, amount - 1);
{
::new((void *)&Array[i]) T;
}
} }
else if (Count != amount) else if (Count != amount)
{ {
@ -423,6 +409,18 @@ public:
} }
Count = amount; Count = amount;
} }
// Ensures that the array has at most amount entries.
// Useful in cases where the initial allocation may be larger than the final result.
// Resize would create a lot of unneeded code in those cases.
void Clamp(unsigned int amount)
{
if (Count > amount)
{
// Deleting old entries
DoDelete(amount, Count - 1);
Count = amount;
}
}
void Alloc(unsigned int amount) void Alloc(unsigned int amount)
{ {
// first destroys all content and then rebuilds the array. // first destroys all content and then rebuilds the array.
@ -438,10 +436,7 @@ public:
Grow (amount); Grow (amount);
unsigned int place = Count; unsigned int place = Count;
Count += amount; Count += amount;
for (unsigned int i = place; i < Count; ++i) ConstructEmpty(place, Count - 1);
{
::new((void *)&Array[i]) T;
}
return place; return place;
} }
unsigned int Size () const unsigned int Size () const
@ -501,6 +496,15 @@ private:
Array[i].~T(); Array[i].~T();
} }
} }
void ConstructEmpty(unsigned int first, unsigned int last)
{
assert(last != ~0u);
for (unsigned int i = first; i <= last; ++i)
{
::new(&Array[i]) T;
}
}
}; };
// TDeletingArray ----------------------------------------------------------- // TDeletingArray -----------------------------------------------------------