Cleanup Iterator in ChunkedArray

This commit is contained in:
Wojtek Figat
2023-10-08 11:09:12 +02:00
parent b93cbbd194
commit b8f094e007

View File

@@ -136,13 +136,11 @@ public:
public: public:
bool IsEnd() const bool IsEnd() const
{ {
ASSERT(_collection);
return Index() == _collection->Count(); return Index() == _collection->Count();
} }
bool IsNotEnd() const bool IsNotEnd() const
{ {
ASSERT(_collection);
return Index() != _collection->Count(); return Index() != _collection->Count();
} }
@@ -171,8 +169,6 @@ public:
public: public:
Iterator& operator++() Iterator& operator++()
{ {
ASSERT(_collection);
// Check if it is not at end // Check if it is not at end
const int32 end = _collection->Count(); const int32 end = _collection->Count();
if (Index() != end) if (Index() != end)
@@ -188,38 +184,18 @@ public:
_index = 0; _index = 0;
} }
} }
return *this; return *this;
} }
Iterator operator++(int) Iterator operator++(int)
{ {
ASSERT(_collection);
Iterator temp = *this; Iterator temp = *this;
++temp;
// Check if it is not at end
const int32 end = _collection->Count();
if (Index() != end)
{
// Move forward within chunk
_index++;
// Check if need to change chunk
if (_index == ChunkSize && _chunkIndex < _collection->_chunks.Count() - 1)
{
// Move to next chunk
_chunkIndex++;
_index = 0;
}
}
return temp; return temp;
} }
Iterator& operator--() Iterator& operator--()
{ {
ASSERT(_collection);
// Check if it's not at beginning // Check if it's not at beginning
if (_index != 0 || _chunkIndex != 0) if (_index != 0 || _chunkIndex != 0)
{ {
@@ -236,32 +212,13 @@ public:
_index--; _index--;
} }
} }
return *this; return *this;
} }
Iterator operator--(int) Iterator operator--(int)
{ {
ASSERT(_collection);
Iterator temp = *this; Iterator temp = *this;
--temp;
// Check if it's not at beginning
if (_index != 0 || _chunkIndex != 0)
{
// Check if need to change chunk
if (_index == 0)
{
// Move to previous chunk
_chunkIndex--;
_index = ChunkSize - 1;
}
else
{
// Move backward within chunk
_index--;
}
}
return temp; return temp;
} }
}; };