From ba725f54566e8557f371266997ea249b38ee8564 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 1 Jul 2021 23:07:46 +0200 Subject: [PATCH] Implement missing code in ChunkedArray --- Source/Engine/Core/Collections/ChunkedArray.h | 44 +++++++------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/Source/Engine/Core/Collections/ChunkedArray.h b/Source/Engine/Core/Collections/ChunkedArray.h index 0d24e980d..f2c5b4264 100644 --- a/Source/Engine/Core/Collections/ChunkedArray.h +++ b/Source/Engine/Core/Collections/ChunkedArray.h @@ -20,22 +20,15 @@ private: // TODO: don't use Array but small struct and don't InlinedArray or Chunk* but Chunk (less dynamic allocations) typedef Array Chunk; - int32 _count; + int32 _count = 0; Array> _chunks; public: - /// - /// Default constructor - /// ChunkedArray() - : _count(0) { } - /// - /// Destructor - /// ~ChunkedArray() { _chunks.ClearDelete(); @@ -426,13 +419,21 @@ public: /// The new size. void Resize(int32 newSize) { - if (newSize < Count()) + while (newSize < Count()) { - MISSING_CODE("shrinking ChunkedArray on Resize"); + auto& chunk = _chunks.Last(); + int32 itemsLeft = Count() - newSize; + int32 itemsToRemove = Math::Min(chunk->Count(), itemsLeft); + chunk->Resize(chunk->Count() - itemsToRemove); + _count -= itemsToRemove; + if (chunk->Count() == 0) + { + Delete(chunk); + _chunks.RemoveLast(); + } } - else + if (newSize > Count()) { - // Require enough space at once EnsureCapacity(newSize); // Add more items until reach the new size @@ -445,9 +446,9 @@ public: // Insert some items to this chunk if can const int32 spaceLeft = chunk->Capacity() - chunk->Count(); - int32 itemsToInsert = Math::Min(itemsReaming, spaceLeft); - chunk->Resize(chunk->Count() + itemsToInsert); - _count += itemsToInsert; + int32 itemsToAdd = Math::Min(itemsReaming, spaceLeft); + chunk->Resize(chunk->Count() + itemsToAdd); + _count += itemsToAdd; // Update counter itemsReaming = newSize - Count(); @@ -481,29 +482,16 @@ public: public: - /// - /// Gets iterator for beginning of the collection. - /// - /// Iterator for beginning of the collection. Iterator Begin() const { return Iterator(this, 0); } - /// - /// Gets iterator for ending of the collection. - /// - /// Iterator for ending of the collection. Iterator End() const { return Iterator(this, Count()); } - /// - /// Gets iterator for the specified index. - /// - /// The index. - /// Iterator for the specified index. Iterator IteratorAt(int32 index) const { return Iterator(this, index);