From f0b31fb4f3c8450c66927e57175f91b9d00d4e3a Mon Sep 17 00:00:00 2001 From: Adam Heinermann Date: Thu, 16 Oct 2025 15:39:14 -0700 Subject: [PATCH] feat(hash): finish TSHashTable implementation --- storm/hash/TSHashTable.hpp | 70 +++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/storm/hash/TSHashTable.hpp b/storm/hash/TSHashTable.hpp index e94058f..2086c8e 100644 --- a/storm/hash/TSHashTable.hpp +++ b/storm/hash/TSHashTable.hpp @@ -44,6 +44,7 @@ class TSHashTable { // Member functions uint32_t ComputeSlot(uint32_t hashval); + void GrowListArray(uint32_t newarraysize); void Initialize(); bool Initialized(); void InternalClear(int32_t warn); @@ -86,6 +87,31 @@ int32_t TSHashTable::GetLinkOffset() { return offsetof(T, m_linktoslot); } +template +void TSHashTable::GrowListArray(uint32_t newarraysize) { + uint32_t slotmask = this->m_slotmask + 1; + int32_t linkOffset = this->GetLinkOffset(); + + TSExplicitList templist; + templist.ChangeLinkOffset(linkOffset); + for (uint32_t i = 0; i < slotmask; i++) { + for (T* ptr = this->m_slotlistarray[i].Head(); ptr; ptr = this->m_slotlistarray[i].Head()) { + templist.LinkNode(ptr, 2, 0); + } + } + + this->m_slotlistarray.SetCount(newarraysize); + for (uint32_t i = 0; i < newarraysize; i++) { + this->m_slotlistarray[i].ChangeLinkOffset(linkOffset); + } + + this->m_slotmask = newarraysize - 1; + for (T* ptr = templist.Head(); ptr; ptr = templist.Head()) { + auto& slot = this->m_slotlistarray[this->ComputeSlot(ptr->m_hashval)]; + slot.LinkNode(ptr, 2, 0); + } +} + template T* TSHashTable::Head() { return this->m_fulllist.Head(); @@ -183,8 +209,26 @@ T* TSHashTable::InternalNewNode(uint32_t hashval, size_t extrabytes, ui template int32_t TSHashTable::MonitorFullness(uint32_t slot) { - // TODO + if (this->m_slotmask >= 0x1FFF) { + return 0; + } + if (this->m_fullnessIndicator > 3) { + this->m_fullnessIndicator -= 3; + } + else { + this->m_fullnessIndicator = 0; + } + + for (T* ptr = this->m_slotlistarray[slot].Head(); reinterpret_cast(ptr) > 0; ptr = this->m_slotlistarray[slot].RawNext(ptr)) { + this->m_fullnessIndicator++; + if (this->m_fullnessIndicator > 13) { + uint32_t grow_amt = 2 * this->m_slotmask + 2; + this->m_fullnessIndicator = 0; + this->GrowListArray(grow_amt); + return 1; + } + } return 0; } @@ -230,29 +274,21 @@ T* TSHashTable::Ptr(const char* str) { uint32_t slot = this->ComputeSlot(hashval); auto slotlist = &this->m_slotlistarray[slot]; - T* v7; - - v7 = slotlist->Head(); - - if (!v7) { + T* ptr = slotlist->Head(); + if (!ptr) { return nullptr; } - while (v7->m_hashval != hashval) { - uint32_t v8 = this->ComputeSlot(hashval); - auto v9 = &this->m_slotlistarray[v8]; - v7 = v9->RawNext(v7); + while (ptr->m_hashval != hashval || !(ptr->m_key == str)) { + uint32_t slotIdx = this->ComputeSlot(hashval); + auto v9 = &this->m_slotlistarray[slotIdx]; + ptr = v9->RawNext(ptr); - if (reinterpret_cast(v7) <= 0) { + if (reinterpret_cast(ptr) <= 0) { return nullptr; } } - - if (!(v7->m_key == str)) { - // TODO Handle collisions - } - - return v7; + return ptr; } template