42#ifndef HASH_TEMPLATE_H
43#define HASH_TEMPLATE_H
59 unsigned int hval = *((
const unsigned int*)(&k));
60 hval = hval + (hval>>5) + (hval>>10) + (hval >> 20);
68 unsigned int z = *((
const unsigned int*)(&s));
69 return ((z>>22)+(z>>12)+(z));
73template <
class KeyType,
class ValueType>
87 void Insert(
const KeyType& s,
const ValueType& d);
88 void Set_Value(
const KeyType& s,
const ValueType& d);
90 void Remove(
const KeyType& s,
const ValueType& d);
91 ValueType
Get(
const KeyType& s)
const;
92 bool Get(
const KeyType& s, ValueType& d)
const;
94 bool Exists(
const KeyType& s,
const ValueType& d)
const;
104 static unsigned int Get_Hash_Val(
const KeyType& s,
const unsigned int hash_array_size);
107 int Alloc_Entry(
void);
122template <
class KeyType,
class ValueType>
132 HashTable(hash_table)
140 int size=HashTable.Get_Size();
141 for (HashIndex=0;HashIndex<size;++HashIndex) {
142 Handle = HashTable.Get_Hash()[HashIndex];
149 Handle=HashTable.Get_Table()[Handle].Next;
151 int size=HashTable.Get_Size();
152 for (++HashIndex;HashIndex<size;++HashIndex) {
153 Handle = HashTable.Get_Hash()[HashIndex];
161 return HashIndex==int(HashTable.Get_Size());
166 return HashTable.Get_Table()[Handle].Value;
171 return HashTable.Get_Table()[Handle].Key;
184 unsigned int hval = Get_Hash_Val(s,Size);
187 Table[h].Next = Hash[hval];
198 for (
unsigned int i = 0; i < Size; i++)
204 while (Table[h].Next !=
NIL)
206 Table[h].Next = First;
216 unsigned int hval = Get_Hash_Val(s,Size);
222 if (Table[h].Key == s)
225 Table[prev].Next = Table[h].Next;
227 Hash[hval] = Table[h].Next;
228 Table[h].Next = First;
240 unsigned int hval = Get_Hash_Val(s,Size);
246 if (Table[h].Key == s && Table[h].Value == d)
249 Table[prev].Next = Table[h].Next;
251 Hash[hval] = Table[h].Next;
252 Table[h].Next = First;
265 int h = Hash[Get_Hash_Val(s,Size)];
267 if (Table[h].Key == s) {
280 int h = Hash[Get_Hash_Val(s,Size)];
283 if (Table[h].Key == s)
284 return Table[h].Value;
294 int h = Hash[Get_Hash_Val(s,Size)];
297 if (Table[h].Key == s)
311 int h = Hash[Get_Hash_Val(s,Size)];
314 if (Table[h].Key == s)
325 int h = Hash[Get_Hash_Val(s,Size)];
328 if (Table[h].Key == s && Table[h].Value == d)
336template <
class KeyType,
class ValueType>
inline unsigned int HashTemplateClass<KeyType,ValueType>::Get_Hash_Val(
const KeyType& s,
const unsigned int hash_array_size)
341template <
class KeyType,
class ValueType>
inline void HashTemplateClass<KeyType,ValueType>::Re_Hash()
343 unsigned int new_size = Size*2;
353 for (i = 0; i < (int)new_size; i++)
355 new_table[i].Next = NIL;
361 for (i = 0; i < (int)Size; i++)
366 unsigned int hVal = Get_Hash_Val(Table[h].Key, new_size);
367 new_table[cnt].Key = Table[h].Key;
368 new_table[cnt].Value = Table[h].Value;
369 new_table[cnt].Next = new_hash[hVal];
370 new_hash[hVal] = cnt;
379 for (i = cnt; i < (int)new_size; i++)
380 new_table[i].Next = i+1;
381 new_table[new_size-1].Next = NIL;
389template <
class KeyType,
class ValueType>
inline int HashTemplateClass<KeyType,ValueType>::Alloc_Entry()
394 First = Table[First].Next;
418 unsigned char* buffer=(
unsigned char*)s.
Peek_Buffer();
421 for (
unsigned int a=0;a<len;++a) {
422 hval+=37*hval+buffer[a];
426 unsigned int hval = *((
const unsigned int*)(buffer+len-8));
427 hval = hval + (hval>>5) + (hval>>10) + (hval >> 20);
void Remove(const KeyType &s, const ValueType &d)
bool Exists(const KeyType &s, const ValueType &d) const
bool Exists(const KeyType &s) const
bool Get(const KeyType &s, ValueType &d) const
void Set_Value(const KeyType &s, const ValueType &d)
unsigned int Get_Size(void) const
ValueType Get(const KeyType &s) const
void Remove(const KeyType &s)
void Insert(const KeyType &s, const ValueType &d)
HashTemplateIterator(HashTemplateClass< KeyType, ValueType > &hash_table)
const KeyType & Peek_Key()
static unsigned int Get_Hash_Value(const Key &k)
int Get_Length(void) const
TCHAR * Peek_Buffer(void)