Implement missing code in ChunkedArray

This commit is contained in:
Wojtek Figat
2021-07-01 23:07:46 +02:00
parent 2deac50c53
commit ba725f5456

View File

@@ -20,22 +20,15 @@ private:
// TODO: don't use Array but small struct and don't InlinedArray or Chunk* but Chunk (less dynamic allocations) // TODO: don't use Array but small struct and don't InlinedArray or Chunk* but Chunk (less dynamic allocations)
typedef Array<T> Chunk; typedef Array<T> Chunk;
int32 _count; int32 _count = 0;
Array<Chunk*, InlinedAllocation<32>> _chunks; Array<Chunk*, InlinedAllocation<32>> _chunks;
public: public:
/// <summary>
/// Default constructor
/// </summary>
ChunkedArray() ChunkedArray()
: _count(0)
{ {
} }
/// <summary>
/// Destructor
/// </summary>
~ChunkedArray() ~ChunkedArray()
{ {
_chunks.ClearDelete(); _chunks.ClearDelete();
@@ -426,13 +419,21 @@ public:
/// <param name="newSize">The new size.</param> /// <param name="newSize">The new size.</param>
void Resize(int32 newSize) 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); EnsureCapacity(newSize);
// Add more items until reach the new size // Add more items until reach the new size
@@ -445,9 +446,9 @@ public:
// Insert some items to this chunk if can // Insert some items to this chunk if can
const int32 spaceLeft = chunk->Capacity() - chunk->Count(); const int32 spaceLeft = chunk->Capacity() - chunk->Count();
int32 itemsToInsert = Math::Min(itemsReaming, spaceLeft); int32 itemsToAdd = Math::Min(itemsReaming, spaceLeft);
chunk->Resize(chunk->Count() + itemsToInsert); chunk->Resize(chunk->Count() + itemsToAdd);
_count += itemsToInsert; _count += itemsToAdd;
// Update counter // Update counter
itemsReaming = newSize - Count(); itemsReaming = newSize - Count();
@@ -481,29 +482,16 @@ public:
public: public:
/// <summary>
/// Gets iterator for beginning of the collection.
/// </summary>
/// <returns>Iterator for beginning of the collection.</returns>
Iterator Begin() const Iterator Begin() const
{ {
return Iterator(this, 0); return Iterator(this, 0);
} }
/// <summary>
/// Gets iterator for ending of the collection.
/// </summary>
/// <returns>Iterator for ending of the collection.</returns>
Iterator End() const Iterator End() const
{ {
return Iterator(this, Count()); return Iterator(this, Count());
} }
/// <summary>
/// Gets iterator for the specified index.
/// </summary>
/// <param name="index">The index.</param>
/// <returns>Iterator for the specified index.</returns>
Iterator IteratorAt(int32 index) const Iterator IteratorAt(int32 index) const
{ {
return Iterator(this, index); return Iterator(this, index);