This commit is contained in:
David Anderson 2006-01-30 21:40:10 +00:00
parent 44db80bc75
commit cb291dbdd5

View File

@ -17,20 +17,20 @@
//namespace SourceHook //namespace SourceHook
//{ //{
template <class K> template <class K>
int HashFunction(const K & k); int HashFunction(const K & k);
template <class K> template <class K>
int Compare(const K & k1, const K & k2); int Compare(const K & k1, const K & k2);
/** /**
* This is a tiny, growable hash class. * This is a tiny, growable hash class.
* Meant for quick and dirty dictionaries only! * Meant for quick and dirty dictionaries only!
*/ */
template <class K, class V> template <class K, class V>
class THash class THash
{ {
public: public:
struct THashNode struct THashNode
{ {
THashNode(const K & k, const V & v) : THashNode(const K & k, const V & v) :
@ -46,14 +46,14 @@ public:
V val; V val;
}; };
typedef List<THashNode *> * NodePtr; typedef List<THashNode *> * NodePtr;
public: public:
class const_iterator; class const_iterator;
THash() : m_Buckets(NULL), m_numBuckets(0), m_percentUsed(0.0f), m_NumItems(0) THash() : m_Buckets(NULL), m_numBuckets(0), m_percentUsed(0.0f), m_items(0)
{ {
_Refactor(); _Refactor();
} }
THash(const THash &other) : m_Buckets(new NodePtr[other.m_numBuckets]), THash(const THash &other) : m_Buckets(new NodePtr[other.m_numBuckets]),
m_numBuckets(other.m_numBuckets), m_percentUsed(other.m_percentUsed) m_numBuckets(other.m_numBuckets), m_percentUsed(other.m_percentUsed), m_items(0)
{ {
for (size_t i=0; i<m_numBuckets; i++) for (size_t i=0; i<m_numBuckets; i++)
m_Buckets[i] = NULL; m_Buckets[i] = NULL;
@ -76,6 +76,10 @@ public:
_Clear(); _Clear();
_Refactor(); _Refactor();
} }
size_t size()
{
return m_items;
}
size_t GetBuckets() size_t GetBuckets()
{ {
return m_numBuckets; return m_numBuckets;
@ -84,16 +88,12 @@ public:
{ {
return m_percentUsed; return m_percentUsed;
} }
size_t size()
{
return m_NumItems;
}
V & operator [](const K & key) V & operator [](const K & key)
{ {
THashNode *pNode = _FindOrInsert(key); THashNode *pNode = _FindOrInsert(key);
return pNode->val; return pNode->val;
} }
private: private:
void _Clear() void _Clear()
{ {
typename List<THashNode *>::iterator iter, end; typename List<THashNode *>::iterator iter, end;
@ -116,7 +116,7 @@ private:
delete [] m_Buckets; delete [] m_Buckets;
m_Buckets = NULL; m_Buckets = NULL;
m_numBuckets = 0; m_numBuckets = 0;
m_NumItems = 0; m_items = 0;
} }
THashNode *_FindOrInsert(const K & key) THashNode *_FindOrInsert(const K & key)
{ {
@ -128,7 +128,7 @@ private:
pNode = new THashNode(key, V()); pNode = new THashNode(key, V());
m_Buckets[place]->push_back(pNode); m_Buckets[place]->push_back(pNode);
m_percentUsed += (1.0f / (float)m_numBuckets); m_percentUsed += (1.0f / (float)m_numBuckets);
m_NumItems++; m_items++;
} else { } else {
typename List<THashNode *>::iterator iter; typename List<THashNode *>::iterator iter;
for (iter=m_Buckets[place]->begin(); iter!=m_Buckets[place]->end(); iter++) for (iter=m_Buckets[place]->begin(); iter!=m_Buckets[place]->end(); iter++)
@ -139,7 +139,7 @@ private:
//node does not exist //node does not exist
pNode = new THashNode(key, V()); pNode = new THashNode(key, V());
m_Buckets[place]->push_back(pNode); m_Buckets[place]->push_back(pNode);
m_NumItems++; m_items++;
} }
if (PercentUsed() > 0.75f) if (PercentUsed() > 0.75f)
_Refactor(); _Refactor();
@ -193,7 +193,7 @@ private:
m_Buckets = temp; m_Buckets = temp;
} }
} }
public: public:
friend class iterator; friend class iterator;
friend class const_iterator; friend class const_iterator;
class iterator class iterator
@ -267,9 +267,10 @@ public:
delete (*iter); delete (*iter);
hash->m_Buckets[curbucket]->erase(iter); hash->m_Buckets[curbucket]->erase(iter);
*this = tmp; *this = tmp;
m_NumItems--;
// :TODO: Maybe refactor to a lower size if required // :TODO: Maybe refactor to a lower size if required
m_items--;
} }
private: private:
void _Inc() void _Inc()
@ -418,7 +419,7 @@ public:
const THash *hash; const THash *hash;
bool end; bool end;
}; };
public: public:
iterator begin() iterator begin()
{ {
return iterator(this); return iterator(this);
@ -479,12 +480,12 @@ public:
return; return;
iter.erase(); iter.erase();
} }
private: private:
NodePtr *m_Buckets; NodePtr *m_Buckets;
size_t m_numBuckets; size_t m_numBuckets;
float m_percentUsed; float m_percentUsed;
size_t m_NumItems; size_t m_items;
}; };
//}; //};
#endif //_INCLUDE_SH_TINYHASH_H_ #endif //_INCLUDE_SH_TINYHASH_H_