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

View File

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

View File

@@ -47,6 +47,18 @@ TEST_CASE("BinSearchArrayMap With Default")
map.Remove(48);
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")
@@ -81,22 +93,27 @@ TEST_CASE("BinSearchArrayMap With Default")
TEST_CASE("BinSearchArrayMap With StableVector")
{
//SECTION("Insert")
//{
// BinSearchArrayMap<int, std::string, std::pair, StableVector> map;
SECTION("Insert")
{
BinSearchArrayMap<int, std::string, std::pair, StableVector> map;
// for (int i = 0; i < 50; i++)
// {
// map.Insert(i, std::to_string(i));
// }
for (int i = 0; i < 50; i++)
{
map.Insert(i, std::to_string(i));
}
// REQUIRE(map.Size() == 50);
// REQUIRE(map.Get(16) == "16");
// REQUIRE(map.Get(23) == "23");
// REQUIRE(map.Get(48) == "48");
//}
REQUIRE(map.Size() == 50);
REQUIRE(map.Get(16) == "16");
REQUIRE(map.Get(23) == "23");
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;
@@ -109,14 +126,16 @@ TEST_CASE("BinSearchArrayMap With StableVector")
map.Remove(23);
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)
{
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());
}
}*/
}
}