libluna: Wrap around when iterating through a HashTable's buckets array
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Why am I so dumb?
This commit is contained in:
parent
28dd8194af
commit
58fa297068
@ -21,6 +21,10 @@ template <typename T> class HashTable
|
|||||||
|
|
||||||
u64 index = hash(value, m_salt) % m_capacity;
|
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)
|
while (true)
|
||||||
{
|
{
|
||||||
auto& bucket = m_buckets[index];
|
auto& bucket = m_buckets[index];
|
||||||
@ -28,6 +32,9 @@ template <typename T> class HashTable
|
|||||||
{
|
{
|
||||||
if (*bucket == value) return false;
|
if (*bucket == value) return false;
|
||||||
index++;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
bucket = { move(value) };
|
bucket = { move(value) };
|
||||||
@ -53,6 +60,7 @@ template <typename T> class HashTable
|
|||||||
{
|
{
|
||||||
if (*bucket == value) return bucket.value_ptr();
|
if (*bucket == value) return bucket.value_ptr();
|
||||||
i++;
|
i++;
|
||||||
|
if (i == m_capacity) i = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -82,6 +90,7 @@ template <typename T> class HashTable
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
|
if (i == m_capacity) i = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user