libluna: Wrap around when iterating through a HashTable's buckets array
All checks were successful
continuous-integration/drone/push Build is passing

Why am I so dumb?
This commit is contained in:
apio 2023-07-25 18:19:45 +02:00
parent 28dd8194af
commit 58fa297068
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -21,6 +21,10 @@ template <typename T> class HashTable
u64 index = hash(value, m_salt) % m_capacity;
check(m_size < m_capacity);
// NOTE: This should not end in an infinite loop if the table is full, since the table CANNOT be full
// (should_grow() returns true before it does). The check above verifies this.
while (true)
{
auto& bucket = m_buckets[index];
@ -28,6 +32,9 @@ template <typename T> class HashTable
{
if (*bucket == value) return false;
index++;
if (index == m_capacity)
index = 0; // Wrap around to avoid overflowing, seems like I assumed it would do that
// automatically for some reason... (facepalm).
continue;
}
bucket = { move(value) };
@ -53,6 +60,7 @@ template <typename T> class HashTable
{
if (*bucket == value) return bucket.value_ptr();
i++;
if (i == m_capacity) i = 0;
}
else
return nullptr;
@ -82,6 +90,7 @@ template <typename T> class HashTable
return true;
}
i++;
if (i == m_capacity) i = 0;
}
else
return false;