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