isle/LEGO1/omni/include/mxhashtable.h

231 lines
4.7 KiB
C
Raw Normal View History

#ifndef MXHASHTABLE_H
#define MXHASHTABLE_H
#include "mxcollection.h"
#include "mxcore.h"
2023-10-24 19:38:27 -04:00
#include "mxtypes.h"
2023-10-24 19:38:27 -04:00
#define HASH_TABLE_INIT_SIZE 128
2023-10-24 19:24:29 -04:00
template <class T>
class MxHashTableCursor;
template <class T>
2023-10-24 19:38:27 -04:00
class MxHashTableNode {
public:
2024-04-17 05:31:48 -04:00
MxHashTableNode<T>(T p_obj, MxU32 p_hash, MxHashTableNode* p_prev, MxHashTableNode* p_next)
2023-10-24 19:38:27 -04:00
{
m_obj = p_obj;
m_hash = p_hash;
2024-04-17 05:31:48 -04:00
m_prev = p_prev;
m_next = p_next;
2023-10-24 19:38:27 -04:00
}
// DECOMP: Should use getter and setter methods here per the style guide.
// However, LEGO1D (with no functions inlined) does not use them.
T m_obj;
2023-10-24 19:38:27 -04:00
MxU32 m_hash;
MxHashTableNode* m_prev;
MxHashTableNode* m_next;
};
template <class T>
class MxHashTable : protected MxCollection<T> {
public:
enum Option {
e_noExpand = 0,
e_expandAll,
e_expandMultiply,
};
2023-10-24 19:38:27 -04:00
MxHashTable()
{
m_numSlots = HASH_TABLE_INIT_SIZE;
2024-04-17 05:31:48 -04:00
MxU32 unused = 0;
m_slots = new MxHashTableNode<T>*[m_numSlots];
2023-10-24 19:38:27 -04:00
memset(m_slots, 0, sizeof(MxHashTableNode<T>*) * m_numSlots);
m_resizeOption = e_noExpand;
2023-10-24 19:38:27 -04:00
}
~MxHashTable() override;
2023-10-24 19:38:27 -04:00
void Resize();
void Add(T);
void DeleteAll();
virtual MxU32 Hash(T) { return 0; }
2023-10-24 19:38:27 -04:00
friend class MxHashTableCursor<T>;
protected:
void NodeInsert(MxHashTableNode<T>*);
2023-10-24 19:38:27 -04:00
MxHashTableNode<T>** m_slots; // 0x10
MxU32 m_numSlots; // 0x14
MxU32 m_autoResizeRatio; // 0x18
Option m_resizeOption; // 0x1c
2023-10-24 19:38:27 -04:00
// FIXME: or FIXME? This qword is used as an integer or double depending
// on the value of m_resizeOption. Hard to say whether this is how the devs
// did it, but a simple cast in either direction doesn't match.
union {
MxU32 m_increaseAmount; // 0x20
double m_increaseFactor; // 0x20
2023-10-24 19:38:27 -04:00
};
};
template <class T>
2023-10-24 19:38:27 -04:00
class MxHashTableCursor : public MxCore {
public:
MxHashTableCursor(MxHashTable<T>* p_table)
2023-10-24 19:38:27 -04:00
{
m_table = p_table;
2023-10-24 19:38:27 -04:00
m_match = NULL;
}
MxBool Find(T p_obj);
MxBool Current(T& p_obj);
void DeleteMatch();
2023-10-24 19:38:27 -04:00
private:
MxHashTable<T>* m_table;
MxHashTableNode<T>* m_match;
};
2023-10-24 19:38:27 -04:00
template <class T>
MxBool MxHashTableCursor<T>::Find(T p_obj)
{
MxU32 hash = m_table->Hash(p_obj);
2023-10-24 19:38:27 -04:00
2024-04-17 05:31:48 -04:00
for (MxHashTableNode<T>* t = m_table->m_slots[hash % m_table->m_numSlots]; t; t = t->m_next) {
if (t->m_hash == hash && !m_table->Compare(t->m_obj, p_obj)) {
m_match = t;
}
2023-10-24 19:38:27 -04:00
}
return m_match != NULL;
}
2023-10-24 19:38:27 -04:00
template <class T>
MxBool MxHashTableCursor<T>::Current(T& p_obj)
{
if (m_match) {
p_obj = m_match->m_obj;
}
2023-10-24 19:38:27 -04:00
return m_match != NULL;
}
2023-10-24 19:38:27 -04:00
template <class T>
void MxHashTableCursor<T>::DeleteMatch()
{
// Cut the matching node out of the linked list
// by updating pointer references.
2024-04-17 05:31:48 -04:00
if (m_match) {
if (m_match->m_prev) {
m_match->m_prev->m_next = m_match->m_next;
}
else {
// No "prev" node, so move "next" to the head of the list.
m_table->m_slots[m_match->m_hash % m_table->m_numSlots] = m_match->m_next;
}
2024-04-17 05:31:48 -04:00
if (m_match->m_next) {
m_match->m_next->m_prev = m_match->m_prev;
}
2024-04-17 05:31:48 -04:00
m_table->m_customDestructor(m_match->m_obj);
delete m_match;
m_table->m_count--;
}
}
template <class T>
MxHashTable<T>::~MxHashTable()
{
DeleteAll();
2024-04-17 05:31:48 -04:00
delete[] m_slots;
}
template <class T>
void MxHashTable<T>::DeleteAll()
{
for (MxS32 i = 0; i < m_numSlots; i++) {
2024-04-17 05:31:48 -04:00
MxHashTableNode<T>* next;
for (MxHashTableNode<T>* t = m_slots[i]; t != NULL; t = next) {
next = t->m_next;
this->m_customDestructor(t->m_obj);
2023-10-24 19:38:27 -04:00
delete t;
}
}
this->m_count = 0;
2023-10-24 19:38:27 -04:00
memset(m_slots, 0, sizeof(MxHashTableNode<T>*) * m_numSlots);
}
template <class T>
inline void MxHashTable<T>::Resize()
{
2023-10-24 19:38:27 -04:00
// Save a reference to the current table
// so we can walk nodes and re-insert
MxU32 oldSize = m_numSlots;
MxHashTableNode<T>** oldTable = m_slots;
2023-10-24 19:38:27 -04:00
switch (m_resizeOption) {
case e_expandAll:
m_numSlots += m_increaseAmount;
2023-10-24 19:38:27 -04:00
break;
case e_expandMultiply:
m_numSlots *= m_increaseFactor;
2023-10-24 19:38:27 -04:00
break;
}
2024-04-17 05:31:48 -04:00
MxU32 unused = 0;
m_slots = new MxHashTableNode<T>*[m_numSlots];
2023-10-24 19:38:27 -04:00
memset(m_slots, 0, sizeof(MxHashTableNode<T>*) * m_numSlots);
this->m_count = 0;
2023-10-24 19:38:27 -04:00
2024-04-17 05:31:48 -04:00
for (MxS32 i = 0; i < oldSize; i++) {
MxHashTableNode<T>* next;
for (MxHashTableNode<T>* t = oldTable[i]; t != NULL; t = next) {
next = t->m_next;
NodeInsert(t);
2023-10-24 19:38:27 -04:00
}
}
delete[] oldTable;
}
template <class T>
inline void MxHashTable<T>::NodeInsert(MxHashTableNode<T>* p_node)
{
MxS32 bucket = p_node->m_hash % m_numSlots;
2023-10-24 19:38:27 -04:00
p_node->m_next = m_slots[bucket];
if (m_slots[bucket]) {
2023-10-24 19:38:27 -04:00
m_slots[bucket]->m_prev = p_node;
}
2023-10-24 19:38:27 -04:00
m_slots[bucket] = p_node;
this->m_count++;
}
template <class T>
inline void MxHashTable<T>::Add(T p_newobj)
{
if (m_resizeOption && ((this->m_count + 1) / m_numSlots) > m_autoResizeRatio) {
2023-10-24 19:38:27 -04:00
MxHashTable<T>::Resize();
}
2023-10-24 19:38:27 -04:00
MxU32 hash = Hash(p_newobj);
2024-04-17 05:31:48 -04:00
MxU32 unused = 0;
MxHashTableNode<T>* node = new MxHashTableNode<T>(p_newobj, hash, NULL, NULL);
MxHashTable<T>::NodeInsert(node);
}
#undef HASH_TABLE_INIT_SIZE
2023-10-24 19:24:29 -04:00
#endif // MXHASHTABLE_H