Skip to content

Commit bad6b0f

Browse files
committed
code review
1 parent aedda5b commit bad6b0f

4 files changed

Lines changed: 97 additions & 527 deletions

File tree

ZEngine/ZEngine/Core/Containers/HashMap.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ namespace ZEngine::Core::Containers
9090
return std::addressof(**this);
9191
}
9292

93+
// Returns a const reference to the key at the current position.
94+
const K& key() const
95+
{
96+
return m_entries[m_index].key;
97+
}
98+
9399
private:
94100
EntryPointer m_entries;
95101
std::size_t m_index;
@@ -198,6 +204,14 @@ namespace ZEngine::Core::Containers
198204
return (index != size_type(-1)) ? &m_entries[index].value : nullptr;
199205
}
200206

207+
// Returns a pointer to the stored key if found, nullptr otherwise.
208+
// Useful for callers that need a stable pointer into the map's key storage.
209+
const K* find_key(const K& key) const
210+
{
211+
size_type index = probe_for_key(key);
212+
return (index != size_type(-1)) ? &m_entries[index].key : nullptr;
213+
}
214+
201215
// Checks if a key exists in the hash map.
202216
// @param key The key to check.
203217
// @return True if the key exists, false otherwise.
@@ -240,9 +254,10 @@ namespace ZEngine::Core::Containers
240254
return;
241255
}
242256

243-
// Collect occupied slot indices into a scratch array.
257+
auto scratch = ZGetScratch(m_allocator);
258+
244259
Array<size_type> indices;
245-
indices.init(m_allocator, m_size);
260+
indices.init(scratch.Arena, m_size);
246261

247262
size_type cur = m_head;
248263
while (cur != size_type(-1))
@@ -263,6 +278,8 @@ namespace ZEngine::Core::Containers
263278
m_entries[indices[i]].prev = (i > 0) ? indices[i - 1] : size_type(-1);
264279
m_entries[indices[i]].next = (i + 1 < indices.size()) ? indices[i + 1] : size_type(-1);
265280
}
281+
282+
ZReleaseScratch(scratch);
266283
}
267284

268285
// Checks if the hash map is empty.
@@ -531,16 +548,6 @@ namespace ZEngine::Core::Containers
531548
}
532549
}
533550

534-
// Computes a secondary hash for double hashing to determine probe step size.
535-
// @param key The key to hash.
536-
// @return A non-zero step size for probing.
537-
size_type double_hash(const K& key) const
538-
{
539-
size_type h = hash(key);
540-
// Ensure step is non-zero and relatively prime to capacity
541-
return (h % m_entries.size()) | 1; // Odd step size
542-
}
543-
544551
Memory::ArenaAllocator* m_allocator = nullptr;
545552
Array<Entry> m_entries;
546553
size_type m_size = 0;

0 commit comments

Comments
 (0)