Collections de/increment operations fix

This one is more stylistic but is consistent with practice of using pre- operations for iterators.
This commit is contained in:
Mateusz Karbowiak
2024-10-31 00:45:00 +01:00
parent 9c448f75d8
commit fbb840dff3
5 changed files with 51 additions and 47 deletions

View File

@@ -482,7 +482,7 @@ public:
{ {
EnsureCapacity(_count + 1); EnsureCapacity(_count + 1);
Memory::ConstructItems(_allocation.Get() + _count, &item, 1); Memory::ConstructItems(_allocation.Get() + _count, &item, 1);
_count++; ++_count;
} }
/// <summary> /// <summary>
@@ -493,7 +493,7 @@ public:
{ {
EnsureCapacity(_count + 1); EnsureCapacity(_count + 1);
Memory::MoveItems(_allocation.Get() + _count, &item, 1); Memory::MoveItems(_allocation.Get() + _count, &item, 1);
_count++; ++_count;
} }
/// <summary> /// <summary>
@@ -557,7 +557,7 @@ public:
{ {
EnsureCapacity(_count + 1); EnsureCapacity(_count + 1);
Memory::ConstructItems(_allocation.Get() + _count, 1); Memory::ConstructItems(_allocation.Get() + _count, 1);
_count++; ++_count;
return _allocation.Get()[_count - 1]; return _allocation.Get()[_count - 1];
} }
@@ -605,7 +605,7 @@ public:
Memory::ConstructItems(data + _count, 1); Memory::ConstructItems(data + _count, 1);
for (int32 i = _count - 1; i >= index; i--) for (int32 i = _count - 1; i >= index; i--)
data[i + 1] = MoveTemp(data[i]); data[i + 1] = MoveTemp(data[i]);
_count++; ++_count;
data[index] = MoveTemp(item); data[index] = MoveTemp(item);
} }
@@ -621,7 +621,7 @@ public:
Memory::ConstructItems(data + _count, 1); Memory::ConstructItems(data + _count, 1);
for (int32 i = _count - 1; i >= index; i--) for (int32 i = _count - 1; i >= index; i--)
data[i + 1] = data[i]; data[i + 1] = data[i];
_count++; ++_count;
} }
/// <summary> /// <summary>
@@ -661,7 +661,7 @@ public:
/// <param name="item">The item to remove.</param> /// <param name="item">The item to remove.</param>
void RemoveAllKeepOrder(const T& item) void RemoveAllKeepOrder(const T& item)
{ {
for (int32 i = Count() - 1; i >= 0; i--) for (int32 i = Count() - 1; i >= 0; --i)
{ {
if (_allocation.Get()[i] == item) if (_allocation.Get()[i] == item)
{ {
@@ -679,14 +679,14 @@ public:
void RemoveAtKeepOrder(const int32 index) void RemoveAtKeepOrder(const int32 index)
{ {
ASSERT(index < _count && index >= 0); ASSERT(index < _count && index >= 0);
_count--; --_count;
T* data = _allocation.Get(); T* data = _allocation.Get();
if (index < _count) if (index < _count)
{ {
T* dst = data + index; T* dst = data + index;
T* src = data + (index + 1); T* src = data + (index + 1);
const int32 count = _count - index; const int32 count = _count - index;
for (int32 i = 0; i < count; i++) for (int32 i = 0; i < count; ++i)
dst[i] = MoveTemp(src[i]); dst[i] = MoveTemp(src[i]);
} }
Memory::DestructItems(data + _count, 1); Memory::DestructItems(data + _count, 1);
@@ -712,7 +712,7 @@ public:
/// <param name="item">The item to remove.</param> /// <param name="item">The item to remove.</param>
void RemoveAll(const T& item) void RemoveAll(const T& item)
{ {
for (int32 i = Count() - 1; i >= 0; i--) for (int32 i = Count() - 1; i >= 0; --i)
{ {
if (_allocation.Get()[i] == item) if (_allocation.Get()[i] == item)
{ {
@@ -730,7 +730,7 @@ public:
void RemoveAt(const int32 index) void RemoveAt(const int32 index)
{ {
ASSERT(index < _count && index >= 0); ASSERT(index < _count && index >= 0);
_count--; --_count;
T* data = _allocation.Get(); T* data = _allocation.Get();
if (_count) if (_count)
data[index] = data[_count]; data[index] = data[_count];
@@ -743,7 +743,7 @@ public:
void RemoveLast() void RemoveLast()
{ {
ASSERT(_count > 0); ASSERT(_count > 0);
_count--; --_count;
Memory::DestructItems(_allocation.Get() + _count, 1); Memory::DestructItems(_allocation.Get() + _count, 1);
} }
@@ -772,7 +772,7 @@ public:
{ {
T* data = _allocation.Get(); T* data = _allocation.Get();
const int32 count = _count / 2; const int32 count = _count / 2;
for (int32 i = 0; i < count; i++) for (int32 i = 0; i < count; ++i)
::Swap(data[i], data[_count - i - 1]); ::Swap(data[i], data[_count - i - 1]);
} }
@@ -1052,7 +1052,7 @@ public:
Iterator& operator--() Iterator& operator--()
{ {
if (_index > 0) if (_index > 0)
_index--; --_index;
return *this; return *this;
} }
@@ -1060,7 +1060,7 @@ public:
{ {
Iterator temp = *this; Iterator temp = *this;
if (_index > 0) if (_index > 0)
_index--; --_index;
return temp; return temp;
} }
}; };

View File

@@ -304,7 +304,7 @@ public:
void Add(const bool item) void Add(const bool item)
{ {
EnsureCapacity(_count + 1); EnsureCapacity(_count + 1);
_count++; ++_count;
Set(_count - 1, item); Set(_count - 1, item);
} }
@@ -316,7 +316,7 @@ public:
void Add(const bool* items, const int32 count) void Add(const bool* items, const int32 count)
{ {
EnsureCapacity(_count + count); EnsureCapacity(_count + count);
for (int32 i = 0; i < count; i++) for (int32 i = 0; i < count; ++i)
Add(items[i]); Add(items[i]);
} }
@@ -327,7 +327,7 @@ public:
void Add(const BitArray& other) void Add(const BitArray& other)
{ {
EnsureCapacity(_count, other.Count()); EnsureCapacity(_count, other.Count());
for (int32 i = 0; i < other.Count(); i++) for (int32 i = 0; i < other.Count(); ++i)
Add(other[i]); Add(other[i]);
} }

View File

@@ -363,8 +363,9 @@ public:
const Bucket* data = _collection->_allocation.Get(); const Bucket* data = _collection->_allocation.Get();
do do
{ {
_index++; ++_index;
} while (_index != capacity && data[_index].IsNotOccupied()); }
while (_index != capacity && data[_index].IsNotOccupied());
} }
return *this; return *this;
} }
@@ -383,8 +384,9 @@ public:
const Bucket* data = _collection->_allocation.Get(); const Bucket* data = _collection->_allocation.Get();
do do
{ {
_index--; --_index;
} while (_index > 0 && data[_index].IsNotOccupied()); }
while (_index > 0 && data[_index].IsNotOccupied());
} }
return *this; return *this;
} }
@@ -423,7 +425,7 @@ public:
// Insert // Insert
ASSERT(pos.FreeSlotIndex != -1); ASSERT(pos.FreeSlotIndex != -1);
_elementsCount++; ++_elementsCount;
Bucket& bucket = _allocation.Get()[pos.FreeSlotIndex]; Bucket& bucket = _allocation.Get()[pos.FreeSlotIndex];
bucket.Occupy(key); bucket.Occupy(key);
return bucket.Value; return bucket.Value;
@@ -582,7 +584,7 @@ public:
Memory::MoveItems(&bucket->Key, &oldBucket.Key, 1); Memory::MoveItems(&bucket->Key, &oldBucket.Key, 1);
Memory::MoveItems(&bucket->Value, &oldBucket.Value, 1); Memory::MoveItems(&bucket->Value, &oldBucket.Value, 1);
bucket->_state = Bucket::Occupied; bucket->_state = Bucket::Occupied;
_elementsCount++; ++_elementsCount;
} }
} }
} }
@@ -682,8 +684,8 @@ public:
if (pos.ObjectIndex != -1) if (pos.ObjectIndex != -1)
{ {
_allocation.Get()[pos.ObjectIndex].Delete(); _allocation.Get()[pos.ObjectIndex].Delete();
_elementsCount--; --_elementsCount;
_deletedCount++; ++_deletedCount;
return true; return true;
} }
return false; return false;
@@ -701,8 +703,8 @@ public:
{ {
ASSERT(_allocation.Get()[i._index].IsOccupied()); ASSERT(_allocation.Get()[i._index].IsOccupied());
_allocation.Get()[i._index].Delete(); _allocation.Get()[i._index].Delete();
_elementsCount--; --_elementsCount;
_deletedCount++; ++_deletedCount;
return true; return true;
} }
return false; return false;
@@ -721,7 +723,7 @@ public:
if (i->Value == value) if (i->Value == value)
{ {
Remove(i); Remove(i);
result++; ++result;
} }
} }
return result; return result;
@@ -768,7 +770,7 @@ public:
if (HasItems()) if (HasItems())
{ {
const Bucket* data = _allocation.Get(); const Bucket* data = _allocation.Get();
for (int32 i = 0; i < _size; i++) for (int32 i = 0; i < _size; ++i)
{ {
if (data[i].IsOccupied() && data[i].Value == value) if (data[i].IsOccupied() && data[i].Value == value)
return true; return true;
@@ -788,7 +790,7 @@ public:
if (HasItems()) if (HasItems())
{ {
const Bucket* data = _allocation.Get(); const Bucket* data = _allocation.Get();
for (int32 i = 0; i < _size; i++) for (int32 i = 0; i < _size; ++i)
{ {
if (data[i].IsOccupied() && data[i].Value == value) if (data[i].IsOccupied() && data[i].Value == value)
{ {
@@ -928,7 +930,7 @@ private:
result.ObjectIndex = bucketIndex; result.ObjectIndex = bucketIndex;
return; return;
} }
checksCount++; ++checksCount;
bucketIndex = (bucketIndex + DICTIONARY_PROB_FUNC(_size, checksCount)) & tableSizeMinusOne; bucketIndex = (bucketIndex + DICTIONARY_PROB_FUNC(_size, checksCount)) & tableSizeMinusOne;
} }
result.ObjectIndex = -1; result.ObjectIndex = -1;

View File

@@ -102,7 +102,7 @@ private:
to.Allocate(fromSize); to.Allocate(fromSize);
Bucket* toData = to.Get(); Bucket* toData = to.Get();
Bucket* fromData = from.Get(); Bucket* fromData = from.Get();
for (int32 i = 0; i < fromSize; i++) for (int32 i = 0; i < fromSize; ++i)
{ {
Bucket& fromBucket = fromData[i]; Bucket& fromBucket = fromData[i];
if (fromBucket.IsOccupied()) if (fromBucket.IsOccupied())
@@ -344,8 +344,9 @@ public:
const Bucket* data = _collection->_allocation.Get(); const Bucket* data = _collection->_allocation.Get();
do do
{ {
_index++; ++_index;
} while (_index != capacity && data[_index].IsNotOccupied()); }
while (_index != capacity && data[_index].IsNotOccupied());
} }
return *this; return *this;
} }
@@ -364,8 +365,9 @@ public:
const Bucket* data = _collection->_allocation.Get(); const Bucket* data = _collection->_allocation.Get();
do do
{ {
_index--; --_index;
} while (_index > 0 && data[_index].IsNotOccupied()); }
while (_index > 0 && data[_index].IsNotOccupied());
} }
return *this; return *this;
} }
@@ -559,8 +561,8 @@ public:
if (pos.ObjectIndex != -1) if (pos.ObjectIndex != -1)
{ {
_allocation.Get()[pos.ObjectIndex].Delete(); _allocation.Get()[pos.ObjectIndex].Delete();
_elementsCount--; --_elementsCount;
_deletedCount++; ++_deletedCount;
return true; return true;
} }
return false; return false;
@@ -578,8 +580,8 @@ public:
{ {
ASSERT(_allocation.Get()[i._index].IsOccupied()); ASSERT(_allocation.Get()[i._index].IsOccupied());
_allocation.Get()[i._index].Delete(); _allocation.Get()[i._index].Delete();
_elementsCount--; --_elementsCount;
_deletedCount++; ++_deletedCount;
return true; return true;
} }
return false; return false;
@@ -750,7 +752,7 @@ private:
// Insert // Insert
ASSERT(pos.FreeSlotIndex != -1); ASSERT(pos.FreeSlotIndex != -1);
_elementsCount++; ++_elementsCount;
return &_allocation.Get()[pos.FreeSlotIndex]; return &_allocation.Get()[pos.FreeSlotIndex];
} }
@@ -760,7 +762,7 @@ private:
{ {
// Fast path if it's empty // Fast path if it's empty
Bucket* data = _allocation.Get(); Bucket* data = _allocation.Get();
for (int32 i = 0; i < _size; i++) for (int32 i = 0; i < _size; ++i)
data[i]._state = Bucket::Empty; data[i]._state = Bucket::Empty;
} }
else else
@@ -770,11 +772,11 @@ private:
MoveToEmpty(oldAllocation, _allocation, _size); MoveToEmpty(oldAllocation, _allocation, _size);
_allocation.Allocate(_size); _allocation.Allocate(_size);
Bucket* data = _allocation.Get(); Bucket* data = _allocation.Get();
for (int32 i = 0; i < _size; i++) for (int32 i = 0; i < _size; ++i)
data[i]._state = Bucket::Empty; data[i]._state = Bucket::Empty;
Bucket* oldData = oldAllocation.Get(); Bucket* oldData = oldAllocation.Get();
FindPositionResult pos; FindPositionResult pos;
for (int32 i = 0; i < _size; i++) for (int32 i = 0; i < _size; ++i)
{ {
Bucket& oldBucket = oldData[i]; Bucket& oldBucket = oldData[i];
if (oldBucket.IsOccupied()) if (oldBucket.IsOccupied())
@@ -786,7 +788,7 @@ private:
bucket->_state = Bucket::Occupied; bucket->_state = Bucket::Occupied;
} }
} }
for (int32 i = 0; i < _size; i++) for (int32 i = 0; i < _size; ++i)
oldData[i].Free(); oldData[i].Free();
} }
_deletedCount = 0; _deletedCount = 0;

View File

@@ -62,7 +62,7 @@ public:
} }
Memory::ConstructItems(_allocation.Get() + _back, &data, 1); Memory::ConstructItems(_allocation.Get() + _back, &data, 1);
_back = (_back + 1) % _capacity; _back = (_back + 1) % _capacity;
_count++; ++_count;
} }
FORCE_INLINE T& PeekFront() FORCE_INLINE T& PeekFront()
@@ -91,7 +91,7 @@ public:
{ {
Memory::DestructItems(_allocation.Get() + _front, 1); Memory::DestructItems(_allocation.Get() + _front, 1);
_front = (_front + 1) % _capacity; _front = (_front + 1) % _capacity;
_count--; --_count;
} }
void Clear() void Clear()