template Iterator for StableVector

This commit is contained in:
Metehan Tuncbilek
2024-10-10 12:01:08 +03:00
parent 8e527074e3
commit c572094f77

View File

@@ -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<typename U>
class Iterator
{
friend class StableVector<T>;
@@ -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<const T>;
using RegIterator = Iterator<T>;
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<T> list)
: StableVector(list.size), m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0)
StableVector(std::initializer_list<T> list) : StableVector(list.size)
{
for (const T& value: list)
{
@@ -212,8 +213,7 @@ namespace OpenVulkano
}
}
StableVector(const StableVector<T>& copy)
: StableVector(copy.m_size), m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0)
StableVector(const StableVector<T>& copy) : StableVector(copy.m_size)
{
for (size_t i = 0; i < copy.Size(); i++)
{
@@ -221,8 +221,7 @@ namespace OpenVulkano
}
}
StableVector(StableVector<T>&& move) noexcept
: m_firstChunk(nullptr), m_lastChunk(nullptr), m_size(0), m_capacity(0)
StableVector(StableVector<T>&& 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