From ca361f10adbda5f02bbf34e7d696dbfdd3e31a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 26 Oct 2016 16:23:09 -0700 Subject: [PATCH] Fixed hash map. --- include/bx/handlealloc.h | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/include/bx/handlealloc.h b/include/bx/handlealloc.h index 0f63ee5..856ae2d 100644 --- a/include/bx/handlealloc.h +++ b/include/bx/handlealloc.h @@ -476,20 +476,25 @@ namespace bx return false; } - void removeByKey(KeyT _key) + bool removeByKey(KeyT _key) { uint32_t idx = findIndex(_key); if (UINT32_MAX != idx) { m_handle[idx] = invalid; --m_numElements; + update(); + return true; } + + return false; } - void removeByHandle(uint16_t _handle) + bool removeByHandle(uint16_t _handle) { if (invalid != _handle) { + const uint32_t numElements = m_numElements; for (uint32_t idx = 0; idx < MaxCapacityT; ++idx) { if (m_handle[idx] == _handle) @@ -498,7 +503,15 @@ namespace bx --m_numElements; } } + + if (numElements != m_numElements) + { + update(); + return true; + } } + + return false; } uint16_t find(KeyT _key) const @@ -614,6 +627,21 @@ namespace bx return result; } + void update() + { + for (uint32_t idx = 0; idx < MaxCapacityT; ++idx) + { + if (m_handle[idx] != invalid) + { + const KeyT key = m_key[idx]; + const uint16_t handle = m_handle[idx]; + m_handle[idx] = invalid; + --m_numElements; + insert(key, handle); + } + } + } + uint32_t m_maxCapacity; uint32_t m_numElements;