KSquare Utilities
HashTable.h
Go to the documentation of this file.
1 #ifndef _HASHTABLE_
2 #define _HASHTABLE_
3 
4 #include "KKBaseTypes.h"
5 #include "KKStr.h"
6 
7 
8 
9 #ifndef _KKSTR_
10 Error, You must include "KKStr.h" before "HashTable.h"
11 #endif
12 
13 namespace KKB
14 {
15  class HashEntry;
16  typedef HashEntry* HashEntryPtr;
17 
18  template <class Entry>
19  class HashTable
20  {
21  public:
22  typedef Entry* EntryPtr;
23 
24  private:
25  class HashEntry;
26  typedef HashEntry* HashEntryPtr;
27 
28  class HashEntry
29  {
30  public:
31  HashEntry (EntryPtr _entry,
32  HashEntryPtr _next
33  ):
34  entry (_entry),
35  next (_next)
36  {}
37 
38  EntryPtr entry;
39  HashEntryPtr next;
40  private:
41  };
42 
43  HashEntryPtr* table;
44 
45  bool owner;
46  kkuint32 tableSize;
47  kkuint32 numOfEntries;
48  kkuint32 keyLen;
49 
50  kkint32* hashFactors;
51 
52  public:
53  HashTable (bool _owner,
54  kkuint32 _tableSize,
55  kkuint32 _keyLen
56  );
57 
58  ~HashTable ();
59 
60  void AddEntry (EntryPtr entry);
61 
62  kkuint32 HashValue (const KKStr& str);
63 
64  bool DeleteEntry (EntryPtr _entry);
65 
66  EntryPtr LocateEntry (const KKStr& str);
67 
68  void Save (const KKStr& fileName);
69 
71 
72  HashEntryPtr* Table () {return table;}
73 
75  }; /* HashTable */
76 
77 
78 
79  /**
80  *@brief Hash Table management template; developed to support BitReduction algorithm.
81  *@details This template was originally written by Tong Luo to support his experiments in Bit reduction.
82  * It was later heavily modified by Kurt Kramer to conform to the PicesLibrary structure.
83  */
84  template <class Entry>
85  HashTable<Entry>::HashTable (bool _owner,
86  kkuint32 _tableSize,
87  kkuint32 _keyLen
88  ):
89  owner (_owner),
90  numOfEntries (0),
92  keyLen (_keyLen)
93  {
95  kkuint32 x;
96  for (x = 0; x < tableSize; x++)
97  table[x] = NULL;
98 
99  hashFactors = new kkint32[keyLen];
100 
101  kkint32 fact = 53;
102  kkint32 hashVal = 1;
103 
105 
106  for (x = 0; x < keyLen; x++)
107  {
108  if (hashVal > maxHashVal)
109  hashVal = 1;
110 
111  hashFactors[x] = hashVal;
112  hashVal = hashVal * fact;
113  }
114  };
115 
116 
117  template <class Entry>
118  HashTable<Entry>::~HashTable ()
119  {
121  kkuint32 idx;
123 
124  for (idx = 0; idx < tableSize; idx++)
125  {
126  next = table[idx];
127  while (next)
128  {
129  cur = next;
130  next = next->next;
131  if (owner)
132  {
133  delete cur->entry;
134  }
135  delete cur;
136  }
137  }
138 
139  delete table;
140  delete hashFactors;
141  }
142 
143 
144  template <class Entry>
145  typename HashTable<Entry>::EntryPtr HashTable<Entry>::LocateEntry (const KKStr& key)
146  {
148 
149  // kkuint32 idx = CalcIndex (hashValue);
151 
152  EntryPtr entry = NULL;
153 
155 
156  while ((!entry) && next)
157  {
158  if (next->entry->Key () == key)
159  {
160  entry = next->entry;
161  }
162  else
163  {
164  next = next->next;
165  }
166  }
167 
168  return entry;
169  } /* LocateEntry */
170 
171 
172  template <class Entry>
173  void HashTable<Entry>::AddEntry (EntryPtr entry)
174  {
176 
178 
179  if (next)
180  {
181  //cout << "Duplicate Hash " << hashValue << endl;
182  }
183 
184  table[hashValue] = new HashEntry (entry, next);
185 
186  numOfEntries++;
187  } /* AddEntry */
188 
189 
190  template <class Entry>
191  bool HashTable<Entry>::DeleteEntry (EntryPtr entry)
192  {
194 
196  HashEntryPtr last = NULL;
197 
198  bool deleted = false;
199 
200  while (next)
201  {
202  if (next == entry)
203  {
204  if (last)
205  {
206  last->next = next->next;
207  }
208  else
209  {
210  table[hashValue] = next->next;
211  }
212 
213  delete next;
214  next = NULL;
215  numOfEntries--;
216  deleted = true;
217  }
218  else
219  {
220  last = next;
221  next = next->next;
222  }
223  }
224 
225  return deleted;
226  } /* DeleteEntry */
227 
228 
229  template <class Entry>
230  kkuint32 HashTable<Entry>::HashValue (const KKStr& str)
231  {
232  const char* s = str.Str ();
233  kkint32 len = strlen (s);
234  kkint32 x = len;
235  kkuint32 y = 0;
236  long hashVal = 0;
237 
238  while ((x > 0) && (y < keyLen))
239  {
240  x--;
241  hashVal = hashVal + s[x] * hashFactors[y];
242  y++;
243  }
244 
246  return hashVal;
247  } /* HashValue */
248 
249 
250  template <class Entry>
251  void HashTable<Entry>::Save (const KKStr& fileName)
252  {
253  ofstream f (fileName.Str ());
254 
255  if (!f)
256  {
257  cerr << endl;
258  cerr << "*** ERROR ***, Error Opening File[" << fileName << "]." << endl;
259  cerr << endl;
260  return;
261  }
262 
263  HashEntryPtr next = NULL;
264  kkuint32 idx = 0;
265 
266  while (idx < TableSize ())
267  {
268  next = (Table ())[idx];
269 
270  while (next)
271  {
272  f << next->entry->ToString () << endl;
273  next = next->next;
274  }
275 
276  idx++;
277  }
278 
279  f.close ();
280  } /* Save */
281 } /* KKB */
282 
283 
284 
285 #endif
Entry * EntryPtr
Definition: HashTable.h:22
void Save(const KKStr &fileName)
Definition: HashTable.h:251
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKTHread * KKTHreadPtr
HashEntry * HashEntryPtr
Definition: HashTable.h:15
kkuint32 NumOfEntries()
Definition: HashTable.h:70
HashEntryPtr * Table()
Definition: HashTable.h:72
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
EntryPtr LocateEntry(const KKStr &str)
Definition: HashTable.h:145
kkuint32 HashValue(const KKStr &str)
Definition: HashTable.h:230
bool DeleteEntry(EntryPtr _entry)
Definition: HashTable.h:191
HashTable(bool _owner, kkuint32 _tableSize, kkuint32 _keyLen)
Hash Table management template; developed to support BitReduction algorithm.
Definition: HashTable.h:85
kkuint32 TableSize()
Definition: HashTable.h:74
void AddEntry(EntryPtr entry)
Definition: HashTable.h:173