node insert inline

This commit is contained in:
disinvite 2023-07-11 14:23:12 -04:00
parent eea1f4b4ec
commit 9c77f88256
2 changed files with 31 additions and 37 deletions

View File

@ -31,12 +31,8 @@ void MxVariableTable::SetVariable(const char *p_key, const char *p_value)
cursor.GetMatch(&var); cursor.GetMatch(&var);
var->SetValue(p_value); var->SetValue(p_value);
} else { } else {
if (m_option && m_numKeys / m_numSlots > m_autoResizeRatio)
Resize();
Add(var); Add(var);
} }
// TODO
} }
// OFFSET: LEGO1 0x100b7740 // OFFSET: LEGO1 0x100b7740
@ -48,9 +44,6 @@ void MxVariableTable::SetVariable(MxVariable *var)
if (found) if (found)
cursor.DeleteMatch(); cursor.DeleteMatch();
if (m_option && m_numKeys / m_numSlots > m_autoResizeRatio)
Resize();
Add(var); Add(var);
} }

View File

@ -41,6 +41,23 @@ class MxHashTable : public MxCore
delete[] m_slots; delete[] m_slots;
} }
// for convenience
inline int GetBucket(int hash) {
return hash % m_numSlots;
}
inline void _NodeInsert(MxHashTableNode<T> *p_node) {
int bucket = GetBucket(p_node->m_hash);
p_node->m_next = m_slots[bucket];
if (m_slots[bucket])
m_slots[bucket]->m_prev = p_node;
m_slots[bucket] = p_node;
m_numKeys++;
}
// OFFSET: LEGO1 0x100b7ab0 // OFFSET: LEGO1 0x100b7ab0
void Resize() void Resize()
{ {
@ -51,37 +68,26 @@ class MxHashTable : public MxCore
switch (m_option) { switch (m_option) {
case 1: // flat increase by x case 1: // flat increase by x
m_numSlots = old_size + (int)m_increaseAmount; m_numSlots = old_size + m_increaseAmount;
break; break;
case 2: // increase by a factor of x case 2: // increase by a factor of x
m_numSlots = (int)(old_size * m_increaseFactor); m_numSlots = old_size * m_increaseFactor;
break; break;
} }
MxHashTableNode<T> **new_table = new MxHashTableNode<T>*[m_numSlots]; MxHashTableNode<T> **new_table = new MxHashTableNode<T>*[m_numSlots];
// FIXME: order? m_numKeys set after `rep stosd` // FIXME: order? m_numKeys set after `rep stosd`
m_slots = new_table; m_slots = new_table;
m_numKeys = 0;
memset(m_slots, 0, sizeof(MxHashTableNode<T> *) * m_numSlots); memset(m_slots, 0, sizeof(MxHashTableNode<T> *) * m_numSlots);
m_numKeys = 0;
for (int i = 0; i != old_size; i++) {
for (MxU32 i = 0; i < old_size; i++) {
MxHashTableNode<T> *t = old_table[i]; MxHashTableNode<T> *t = old_table[i];
while (t) { while (t) {
MxHashTableNode<T> *next = t->m_next; MxHashTableNode<T> *next = t->m_next;
int new_bucket = t->m_hash % m_numSlots; _NodeInsert(t);
t->m_next = m_slots[new_bucket];
// If the new bucket is not empty, make the reshuffled node
// the new head of the bucket.
if (m_slots[new_bucket])
m_slots[new_bucket]->m_prev = t;
m_slots[new_bucket] = t;
t = next; t = next;
m_numKeys++;
} }
} }
@ -90,29 +96,24 @@ class MxHashTable : public MxCore
void Add(T* p_newobj) void Add(T* p_newobj)
{ {
if (m_option && ((m_numKeys + 1) / m_numSlots) > m_autoResizeRatio)
Resize();
MxU32 hash = Hash(p_newobj); MxU32 hash = Hash(p_newobj);
MxHashTableNode<T> *node = new MxHashTableNode<T>(p_newobj, hash); MxHashTableNode<T> *node = new MxHashTableNode<T>(p_newobj, hash);
int bucket = node->m_hash % m_numSlots; _NodeInsert(node);
node->m_next = m_slots[bucket];
if (m_slots[bucket])
m_slots[bucket]->m_prev = node;
m_slots[bucket] = node;
m_numKeys++;
} }
virtual MxS8 Compare(T*, T*); virtual MxS8 Compare(T*, T*);
virtual MxU32 Hash(T*); virtual MxU32 Hash(T*);
//private: //private:
int m_numKeys; // +0x8 MxU32 m_numKeys; // +0x8
void (*m_customDestructor)(T*); // +0xc void (*m_customDestructor)(T*); // +0xc
MxHashTableNode<T> **m_slots; // +0x10 MxHashTableNode<T> **m_slots; // +0x10
MxU32 m_numSlots; // +0x14 MxU32 m_numSlots; // +0x14
int m_autoResizeRatio; MxU32 m_autoResizeRatio;
int m_option; // +0x1c int m_option; // +0x1c
// FIXME: or FIXME? This qword is used as an integer or double depending // FIXME: or FIXME? This qword is used as an integer or double depending
// on the value of m_option. Hard to say whether this is how the devs // on the value of m_option. Hard to say whether this is how the devs
@ -136,7 +137,7 @@ class MxHashTableCursor : public MxCore
MxBool Find(T *p_obj) MxBool Find(T *p_obj)
{ {
MxU32 hash = m_table->Hash(p_obj); MxU32 hash = m_table->Hash(p_obj);
int bucket = hash % m_table->m_numSlots; int bucket = m_table->GetBucket(hash);
MxHashTableNode<T> *t = m_table->m_slots[bucket]; MxHashTableNode<T> *t = m_table->m_slots[bucket];
@ -165,7 +166,7 @@ class MxHashTableCursor : public MxCore
m_match->m_prev->m_next = m_match->m_next; m_match->m_prev->m_next = m_match->m_next;
} else { } else {
// No "prev" node, so move "next" to the head of the list. // No "prev" node, so move "next" to the head of the list.
int bucket = m_match->m_hash % m_table->m_numSlots; int bucket = m_table->GetBucket(m_match->m_hash);
m_table->m_slots[bucket] = m_match->m_next; m_table->m_slots[bucket] = m_match->m_next;
} }