rollback types and fix corner case with iterator

This commit is contained in:
ohyzha
2025-03-05 14:58:08 +02:00
parent b95b8b0426
commit d342f93f45
2 changed files with 25 additions and 14 deletions

View File

@@ -39,7 +39,7 @@ namespace OpenVulkano
size_t m_size; // Size of the chunk
size_t m_capacity; // Capacity of the chunk
std::ptrdiff_t m_nextIndex; // Next index to insert
size_t m_nextIndex; // Next index to insert
size_t m_gapCount; // Count of emptied gaps in the chunk
bool* m_occupiedIndices; // filled gaps array
T m_data[0]; // data array
@@ -178,11 +178,11 @@ namespace OpenVulkano
}
}
std::ptrdiff_t GetIteratorIndex() const { return m_chunk->GetRealIndex(m_index); }
size_t GetIteratorIndex() const { return m_chunk->GetRealIndex(m_index); }
private:
VectorChunk* m_chunk;
std::ptrdiff_t m_index;
size_t m_index;
};
public:
@@ -266,15 +266,6 @@ namespace OpenVulkano
return; // return? or make
}
if (m_lastChunk->m_nextIndex == -1)
{
VectorChunk* temp = m_lastChunk;
m_lastChunk = m_lastChunk->m_prev;
m_lastChunk->m_next = nullptr;
temp->~VectorChunk();
::operator delete(temp);
}
m_lastChunk->m_data[m_lastChunk->m_nextIndex - 1].~T();
m_lastChunk->m_occupiedIndices[m_lastChunk->m_nextIndex - 1] = false;
m_lastChunk->m_size--;
@@ -378,10 +369,24 @@ namespace OpenVulkano
bool Empty() const noexcept { return m_size == 0; }
RegIterator begin() { return RegIterator(m_firstChunk, 0); }
RegIterator end() { return RegIterator(m_lastChunk, m_lastChunk->m_nextIndex - 1); }
RegIterator end()
{
if (Empty()) [[unlikely]]
{
return begin();
}
return RegIterator(m_lastChunk, m_lastChunk->m_nextIndex - 1);
}
ConstIterator cbegin() const { return ConstIterator(m_firstChunk, 0); }
ConstIterator cend() const { return ConstIterator(m_lastChunk, m_lastChunk->m_nextIndex - 1); }
ConstIterator cend() const
{
if (Empty()) [[unlikely]]
{
return cbegin();
}
return ConstIterator(m_lastChunk, m_lastChunk->m_nextIndex - 1);
}
//region std aliases
void push_back(const T& value) { PushBack(value); }

View File

@@ -101,6 +101,12 @@ TEST_CASE("ChunkVector")
REQUIRE(vec.Capacity() == tempVal);
}
SECTION("Iterators with empty vector")
{
StableVector<uint32_t> vec;
REQUIRE(vec.begin() == vec.end());
}
SECTION("Clear")
{
StableVector<uint32_t> vec;