diff --git a/openVulkanoCpp/Data/Containers/StableVector.hpp b/openVulkanoCpp/Data/Containers/StableVector.hpp index 27e37d8..38f8f9b 100644 --- a/openVulkanoCpp/Data/Containers/StableVector.hpp +++ b/openVulkanoCpp/Data/Containers/StableVector.hpp @@ -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); } diff --git a/tests/Data/Containers/StableVectorTest.cpp b/tests/Data/Containers/StableVectorTest.cpp index d0efb35..edef5a8 100644 --- a/tests/Data/Containers/StableVectorTest.cpp +++ b/tests/Data/Containers/StableVectorTest.cpp @@ -101,6 +101,12 @@ TEST_CASE("ChunkVector") REQUIRE(vec.Capacity() == tempVal); } + SECTION("Iterators with empty vector") + { + StableVector vec; + REQUIRE(vec.begin() == vec.end()); + } + SECTION("Clear") { StableVector vec;