Stable Vector fully works as both Standalone and with BinSearchArrayMap

DONE
This commit is contained in:
Metehan Tuncbilek
2024-10-09 18:20:41 +03:00
parent 6f981682f8
commit 0e34b90f67
3 changed files with 70 additions and 96 deletions

View File

@@ -56,28 +56,12 @@ namespace OpenVulkano
Pair<K, V>& FindPair(const K key) Pair<K, V>& FindPair(const K key)
{ {
size_t low = 0; std::streamsize index = FindIndexInVector(key);
size_t high = m_data.size(); if (index < 0)
while (low <= high)
{ {
size_t mid = low + (high - low) / 2; std::runtime_error("Key not found");
if (m_data[mid].first == key)
{
return m_data[mid];
} }
else if (m_data[mid].first < key) return m_data[index];
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
throw std::runtime_error("Key not found.");
} }
std::streamsize FindIndexInVector(const K key) std::streamsize FindIndexInVector(const K key)
@@ -106,31 +90,7 @@ namespace OpenVulkano
return -1; return -1;
} }
bool Contains(const K key) noexcept bool Contains(const K key) noexcept { return FindIndexInVector(key) >= 0; }
{
size_t low = 0;
size_t high = m_data.size();
while (low <= high)
{
size_t mid = low + (high - low) / 2;
if (m_data[mid].first == key)
{
return true;
}
else if (m_data[mid].first < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return false;
}
void Clear() { m_data.clear(); } void Clear() { m_data.clear(); }

View File

@@ -120,14 +120,10 @@ namespace OpenVulkano
T& operator*() T& operator*()
{ {
auto realIndex = m_chunk->GetRealIndex(m_index); auto realIndex = m_chunk->GetRealIndex(m_index);
return m_chunk->GetAlignedData(m_index); return m_chunk->GetAlignedData(realIndex);
} }
T* operator->() T* operator->() { return &operator*(); }
{
auto realIndex = m_chunk->GetRealIndex(m_index);
return &m_chunk->GetAlignedData(m_index);
}
Iterator& operator++() Iterator& operator++()
{ {
@@ -139,8 +135,7 @@ namespace OpenVulkano
Iterator operator++(int) Iterator operator++(int)
{ {
Iterator temp = *this; Iterator temp = *this;
++m_index; ++temp;
MoveToNextChunk();
return temp; return temp;
} }
@@ -154,8 +149,7 @@ namespace OpenVulkano
Iterator operator--(int) Iterator operator--(int)
{ {
Iterator temp = *this; Iterator temp = *this;
--m_index; --temp;
MoveToPrevChunk();
return temp; return temp;
} }
@@ -185,37 +179,31 @@ namespace OpenVulkano
return m_chunk != other.m_chunk || m_index != other.m_index; return m_chunk != other.m_chunk || m_index != other.m_index;
} }
size_t GetIndex() const { return m_index; } std::streamsize GetIndex() const { return m_chunk->GetRealIndex(m_index); }
size_t GetChunk() const { return m_chunk; } VectorChunk* GetChunk() const { return m_chunk; }
protected: protected:
void MoveToNextChunk() void MoveToNextChunk()
{ {
while (m_chunk && !m_chunk->m_occupiedIndices[m_index]) while (m_chunk && m_index >= m_chunk->m_size)
{
if (m_index >= m_chunk->m_size)
{ {
m_index -= m_chunk->m_size;
m_chunk = m_chunk->m_next; m_chunk = m_chunk->m_next;
m_index = 0;
}
else
{
++m_index;
} }
} }
if (m_chunk && m_index > m_chunk->m_size) void MoveToPrevChunk()
{ {
m_chunk = m_chunk->m_next; while (m_chunk && m_index < 0)
m_index = 0; {
m_index += m_chunk->m_size;
m_chunk = m_chunk->m_prev;
} }
} }
void MoveToPrevChunk() {}
private: private:
VectorChunk* m_chunk; VectorChunk* m_chunk;
size_t m_index; std::streamsize m_index;
}; };
public: public:
@@ -354,6 +342,8 @@ namespace OpenVulkano
} }
} }
} }
currentChunk = currentChunk->m_next;
} }
PushBack(value); PushBack(value);
@@ -497,7 +487,14 @@ namespace OpenVulkano
m_size--; m_size--;
} }
void Remove(const Iterator& it) { Remove(it.GetIndex()); } void Remove(const Iterator& it)
{
it.GetChunk()->m_data[it.GetIndex()].~T();
it.GetChunk()->m_occupiedIndices[it.GetIndex()] = false;
it.GetChunk()->m_size--;
it.GetChunk()->m_gapCount++;
m_size--;
}
T& operator[](size_t index) noexcept T& operator[](size_t index) noexcept
{ {
@@ -531,8 +528,6 @@ namespace OpenVulkano
m_size = 0; m_size = 0;
} }
/// std version of clear
size_t Size() const noexcept { return m_size; } size_t Size() const noexcept { return m_size; }
size_t Capacity() const noexcept { return m_capacity; } size_t Capacity() const noexcept { return m_capacity; }
bool Empty() const noexcept { return m_size == 0; } bool Empty() const noexcept { return m_size == 0; }
@@ -552,7 +547,7 @@ namespace OpenVulkano
T& front() { return Front(); } T& front() { return Front(); }
bool empty() const noexcept { return Empty(); } bool empty() const noexcept { return Empty(); }
void clear() { Clear(); } void clear() { Clear(); }
void erase(const Iterator& it) { Remove(it.GetIndex()); } void erase(const Iterator& it) { Remove(it); }
size_t size() const noexcept { return Size(); } size_t size() const noexcept { return Size(); }
size_t capacity() const noexcept { return Capacity(); } size_t capacity() const noexcept { return Capacity(); }
//endregion //endregion

View File

@@ -47,6 +47,18 @@ TEST_CASE("BinSearchArrayMap With Default")
map.Remove(48); map.Remove(48);
REQUIRE(map.Size() == 47); REQUIRE(map.Size() == 47);
for (int i = 0; i < 50; i++)
{
if (i == 16 || i == 23 || i == 48)
{
REQUIRE(!map.Contains(i));
}
else
{
REQUIRE(map.Get(i) == std::to_string(i));
}
}
} }
SECTION("Emplace") SECTION("Emplace")
@@ -81,22 +93,27 @@ TEST_CASE("BinSearchArrayMap With Default")
TEST_CASE("BinSearchArrayMap With StableVector") TEST_CASE("BinSearchArrayMap With StableVector")
{ {
//SECTION("Insert") SECTION("Insert")
//{ {
// BinSearchArrayMap<int, std::string, std::pair, StableVector> map; BinSearchArrayMap<int, std::string, std::pair, StableVector> map;
// for (int i = 0; i < 50; i++) for (int i = 0; i < 50; i++)
// { {
// map.Insert(i, std::to_string(i)); map.Insert(i, std::to_string(i));
// } }
// REQUIRE(map.Size() == 50); REQUIRE(map.Size() == 50);
// REQUIRE(map.Get(16) == "16"); REQUIRE(map.Get(16) == "16");
// REQUIRE(map.Get(23) == "23"); REQUIRE(map.Get(23) == "23");
// REQUIRE(map.Get(48) == "48"); REQUIRE(map.Get(48) == "48");
//}
/*SECTION("Remove") for (int i = 0; i < 50; i++)
{
REQUIRE(map.Get(i) == std::to_string(i));
}
}
SECTION("Remove")
{ {
BinSearchArrayMap<int, std::string, std::pair, StableVector> map; BinSearchArrayMap<int, std::string, std::pair, StableVector> map;
@@ -109,14 +126,16 @@ TEST_CASE("BinSearchArrayMap With StableVector")
map.Remove(23); map.Remove(23);
map.Remove(48); map.Remove(48);
for (int i = 0; i < map.Size(); i++) for (int i = 0; i < 50; i++)
{ {
if (i == 16 || i == 23 || i == 48) if (i == 16 || i == 23 || i == 48)
{ {
continue; REQUIRE(!map.Contains(i));
}
else
{
REQUIRE(map.Get(i) == std::to_string(i));
}
} }
printf("i: %d, value: %s\n", i, map[i].c_str());
} }
}*/
} }