From c572094f77acbcc9b520b7020c916e90dc434ca0 Mon Sep 17 00:00:00 2001 From: Metehan Tuncbilek Date: Thu, 10 Oct 2024 12:01:08 +0300 Subject: [PATCH] template Iterator for StableVector --- .../Data/Containers/StableVector.hpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/openVulkanoCpp/Data/Containers/StableVector.hpp b/openVulkanoCpp/Data/Containers/StableVector.hpp index 5533b12..0685f32 100644 --- a/openVulkanoCpp/Data/Containers/StableVector.hpp +++ b/openVulkanoCpp/Data/Containers/StableVector.hpp @@ -56,7 +56,6 @@ namespace OpenVulkano { if (m_occupiedIndices[i]) { - m_occupiedIndices[i] = false; m_data[i].~T(); } } @@ -93,6 +92,7 @@ namespace OpenVulkano }; public: + template class Iterator { friend class StableVector; @@ -100,13 +100,13 @@ namespace OpenVulkano public: Iterator(VectorChunk* chunk, size_t index) : m_chunk(chunk), m_index(index) {} - T& operator*() + U& operator*() { auto realIndex = m_chunk->GetRealIndex(m_index); return m_chunk->m_data[realIndex]; } - T* operator->() { return &operator*(); } + U* operator->() { return &operator*(); } Iterator& operator++() { @@ -186,6 +186,9 @@ namespace OpenVulkano }; public: + using ConstIterator = Iterator; + using RegIterator = Iterator; + StableVector(size_t size = DEFAULT_CHUNK_SIZE) : m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0) { @@ -194,8 +197,7 @@ namespace OpenVulkano m_capacity = size; } - StableVector(size_t size, const T& value) - : StableVector(size), m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0) + StableVector(size_t size, const T& value) : StableVector(size) { for (size_t i = 0; i < size; i++) { @@ -203,8 +205,7 @@ namespace OpenVulkano } } - StableVector(std::initializer_list list) - : StableVector(list.size), m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0) + StableVector(std::initializer_list list) : StableVector(list.size) { for (const T& value: list) { @@ -212,8 +213,7 @@ namespace OpenVulkano } } - StableVector(const StableVector& copy) - : StableVector(copy.m_size), m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0) + StableVector(const StableVector& copy) : StableVector(copy.m_size) { for (size_t i = 0; i < copy.Size(); i++) { @@ -221,8 +221,7 @@ namespace OpenVulkano } } - StableVector(StableVector&& move) noexcept - : m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0) + StableVector(StableVector&& move) noexcept : m_firstChunk(nullptr) { m_firstChunk = move.m_firstChunk; m_lastChunk = move.m_lastChunk; @@ -320,7 +319,7 @@ namespace OpenVulkano Delete(realIndex, handle.first); } - void Remove(const Iterator& it) + void Remove(const RegIterator& it) { if (it.m_chunk == nullptr) { @@ -347,7 +346,7 @@ namespace OpenVulkano return operator[](index); } - void Clear() + void Clear() { // Get the first chunk's cap size_t cap = m_firstChunk->m_capacity; @@ -369,6 +368,7 @@ namespace OpenVulkano m_firstChunk->m_nextIndex = 0; m_firstChunk->m_gapCount = 0; m_firstChunk->~VectorChunk(); + new (m_firstChunk) VectorChunk(cap); m_size = 0; m_capacity = cap; } @@ -377,11 +377,11 @@ namespace OpenVulkano size_t Capacity() const noexcept { return m_capacity; } bool Empty() const noexcept { return m_size == 0; } - Iterator begin() { return Iterator(m_firstChunk, 0); } - Iterator end() { return Iterator(m_lastChunk, m_lastChunk->m_nextIndex - 1); } + RegIterator begin() { return RegIterator(m_firstChunk, 0); } + RegIterator end() { return RegIterator(m_lastChunk, m_lastChunk->m_nextIndex - 1); } - const Iterator& cbegin() { return Iterator(m_firstChunk, 0); } - const Iterator& cend() { return Iterator(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); } //region std aliases void push_back(const T& value) { PushBack(value); } @@ -392,7 +392,7 @@ namespace OpenVulkano T& front() { return Front(); } bool empty() const noexcept { return Empty(); } void clear() { Clear(); } - void erase(const Iterator& it) { Remove(it); } + void erase(const RegIterator& it) { Remove(it); } size_t size() const noexcept { return Size(); } size_t capacity() const noexcept { return Capacity(); } //endregion