55 template <
typename K,
typename V>
99 ItemNode*
Find(
const K&
key,
size_t* pHashIndex=NULL, ItemNode** ppPrev=NULL)
102 ItemNode* pPrev = NULL;
103 ItemNode* pProbe = _lookuptable[hashindex];
106 if (_nodes[pProbe->index].key == key)
111 pProbe = pProbe->pNext;
116 *pHashIndex = hashindex;
132 if ((_indexlist == NULL) || (_size == 0))
137 for (
size_t t = 0; t <
_tsize; t++)
139 ItemNode* pNode = _lookuptable[t];
142 _indexlist[index] = pNode->index;
144 pNode = pNode->pNext;
158 if (_fIndexValid && (_size < _fsize) && _indexlist)
162 _indexlist[pos] = pNode->index;
174 (_indexlist == NULL) ||
175 ((_size > 1) && (_fIndexValid==
false)))
189 if (pNode->index == _indexlist[_indexStart])
191 _indexStart = (_indexStart + 1) % _fsize;
195 size_t indexlast = (_indexStart + (_size-1)) %
_fsize;
196 if (pNode->index == _indexlist[indexlast])
203 _fIndexValid =
false;
212 Init(0, 0, NULL, NULL, NULL, NULL);
216 FastHashBase(
size_t fsize,
size_t tsize, Item* nodelist, ItemNode* itemnodelist, ItemNode** table,
int* indexlist)
218 Init(fsize, tsize, nodelist, itemnodelist, table, indexlist);
221 void Init(
size_t fsize,
size_t tsize, Item* nodelist, ItemNode* itemnodelist, ItemNode** table,
int* indexlist)
228 _itemnodes = itemnodelist;
230 _lookuptable = table;
231 _indexlist = indexlist;
232 _fIndexValid = (_indexlist != NULL);
241 if (_lookuptable != NULL)
243 memset(_lookuptable,
'\0',
sizeof(ItemNodePtr)*_tsize);
246 if ((_fsize > 0) && (_itemnodes != NULL))
248 for (
size_t x = 0; x <
_fsize; x++)
250 _itemnodes[x].pNext = &_itemnodes[x+1];
251 _itemnodes[x].index = x;
254 _itemnodes[_fsize-1].pNext = NULL;
260 _fIndexValid = (_indexlist != NULL);
266 return ((_tsize > 0) && (_fsize > 0) && (_itemnodes != NULL) && (_lookuptable != NULL) && (_nodes != NULL));
287 ItemNode* pInsert = NULL;
288 ItemNode* pHead = _lookuptable[hashindex];
291 if (_freelist == NULL)
297 _freelist = _freelist->
pNext;
299 pItem = &_nodes[pInsert->index];
302 pItem->value =
value;
304 pInsert->pNext = pHead;
306 _lookuptable[hashindex]= pInsert;
320 ItemNode* pPrev = NULL;
321 ItemNode* pNode =
Find(key, &hashindex, &pPrev);
322 ItemNode* pNext = NULL;
329 pNext = pNode->pNext;
333 _lookuptable[hashindex] = pNext;
338 pPrev->pNext = pNext;
354 ItemNode* pNode =
Find(key);
357 Item* pItem = &(_nodes[pNode->index]);
358 pValue = &(pItem->value);
365 return (
Find(key) != NULL);
373 if ((index >= _size) || (_indexlist == NULL))
378 if (_fIndexValid ==
false)
381 if (_fIndexValid ==
false)
387 indexadjusted = (_indexStart + index) % _fsize;
388 itemindex = _indexlist[indexadjusted];
389 return &(_nodes[itemindex]);
395 return pItem ? &pItem->value : NULL;
401 template <
typename K,
typename V,
size_t FSIZE=100,
size_t TSIZE=37>
416 Item _nodesarray[FSIZE];
417 ItemNodePtr _lookuptablearray[TSIZE];
418 ItemNode _itemnodesarray[FSIZE];
419 int _indexarray[FSIZE];
423 FastHashBase<K,V>(FSIZE, TSIZE, _nodesarray, _itemnodesarray, _lookuptablearray, _indexarray)
431 template <
class K,
class V>
451 _itemnodesarray(NULL),
452 _lookuptablearray(NULL),
459 _itemnodesarray(NULL),
460 _lookuptablearray(NULL),
463 InitTable(fsize, tsize);
486 _nodesarray =
new Item[fsize];
487 _itemnodesarray =
new ItemNode[fsize];
488 _lookuptablearray =
new ItemNodePtr[tsize];
489 _indexarray =
new int[fsize];
491 if ((_nodesarray == NULL) || (_itemnodesarray == NULL) || (_lookuptablearray == NULL) || (_indexarray==NULL))
497 this->
Init(fsize, tsize, _nodesarray, _itemnodesarray, _lookuptablearray, _indexarray);
503 delete [] _nodesarray;
506 delete [] _itemnodesarray;
507 _itemnodesarray = NULL;
509 delete [] _lookuptablearray;
510 _lookuptablearray = NULL;
512 delete [] _indexarray;
515 this->
Init(0,0, NULL, NULL, NULL, NULL);
FastHashBase & operator=(const FastHashBase &)
FastHashBase< K, V >::ItemNode ItemNode
bool Exists(const K &key)
void UpdateIndexWithAdd(ItemNode *pNode)
int Insert(const K &key, V &value)
FastHashBase< K, V >::ItemNode ItemNode
size_t FastHash_Hash(void *ptr)
FastHashDynamic(size_t fsize, size_t tsize)
V * LookupValueByIndex(size_t index)
FastHashBase(size_t fsize, size_t tsize, Item *nodelist, ItemNode *itemnodelist, ItemNode **table, int *indexlist)
void UpdateIndexWithRemove(ItemNode *pNode)
Item * LookupByIndex(size_t index)
void Init(size_t fsize, size_t tsize, Item *nodelist, ItemNode *itemnodelist, ItemNode **table, int *indexlist)
ItemNode * Find(const K &key, size_t *pHashIndex=NULL, ItemNode **ppPrev=NULL)
FastHash & operator=(const FastHash &)
bool operator==(const FastHash &)
FastHashBase< K, V >::Item Item
#define COMPILE_TIME_ASSERT(x)
bool operator==(const FastHashBase &)
FastHashBase(const FastHashBase &)
FastHashBase< K, V >::Item Item
int InitTable(size_t fsize, size_t tsize)
ItemNode * _itemnodesarray
FastHash(const FastHash &)
FastHashBase< K, V >::ItemNodePtr ItemNodePtr
ItemNodePtr * _lookuptablearray
size_t FastHash_GetHashTableWidth(unsigned int maxItems)
ItemNodePtr * _lookuptable
FastHashBase< K, V >::ItemNodePtr ItemNodePtr