KSquare Utilities
KKB::HashTable< Entry > Class Template Reference

#include <HashTable.h>

Public Types

typedef Entry * EntryPtr
 

Public Member Functions

 HashTable (bool _owner, kkuint32 _tableSize, kkuint32 _keyLen)
 Hash Table management template; developed to support BitReduction algorithm. More...
 
 ~HashTable ()
 
void AddEntry (EntryPtr entry)
 
bool DeleteEntry (EntryPtr _entry)
 
kkuint32 HashValue (const KKStr &str)
 
EntryPtr LocateEntry (const KKStr &str)
 
kkuint32 NumOfEntries ()
 
void Save (const KKStr &fileName)
 
HashEntryPtr * Table ()
 
kkuint32 TableSize ()
 

Detailed Description

template<class Entry>
class KKB::HashTable< Entry >

Definition at line 19 of file HashTable.h.

Member Typedef Documentation

template<class Entry >
typedef Entry* KKB::HashTable< Entry >::EntryPtr

Definition at line 22 of file HashTable.h.

Constructor & Destructor Documentation

template<class Entry >
KKB::HashTable< Entry >::HashTable ( bool  _owner,
kkuint32  _tableSize,
kkuint32  _keyLen 
)

Hash Table management template; developed to support BitReduction algorithm.

This template was originally written by Tong Luo to support his experiments in Bit reduction. It was later heavily modified by Kurt Kramer to conform to the PicesLibrary structure.

Definition at line 85 of file HashTable.h.

88  :
89  owner (_owner),
90  numOfEntries (0),
91  tableSize (_tableSize),
92  keyLen (_keyLen)
93  {
94  table = new HashEntryPtr[tableSize];
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 
104  kkint32 maxHashVal = _tableSize * 10;
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  };
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
HashEntry * HashEntryPtr
Definition: HashTable.h:15
template<class Entry >
KKB::HashTable< Entry >::~HashTable ( )

Definition at line 118 of file HashTable.h.

119  {
120  HashEntryPtr cur;
121  kkuint32 idx;
122  HashEntryPtr next;
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  }
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
HashEntry * HashEntryPtr
Definition: HashTable.h:15

Member Function Documentation

template<class Entry >
void KKB::HashTable< Entry >::AddEntry ( EntryPtr  entry)

Definition at line 173 of file HashTable.h.

174  {
175  kkuint32 hashValue = HashValue (entry->Key ());
176 
177  HashEntryPtr next = table[hashValue];
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 */
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
HashEntry * HashEntryPtr
Definition: HashTable.h:15
kkuint32 HashValue(const KKStr &str)
Definition: HashTable.h:230
template<class Entry >
bool KKB::HashTable< Entry >::DeleteEntry ( EntryPtr  _entry)

Definition at line 191 of file HashTable.h.

192  {
193  kkuint32 hashValue = HashValue (entry->Key ());
194 
195  HashEntryPtr next = table[hashValue];
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 */
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
HashEntry * HashEntryPtr
Definition: HashTable.h:15
kkuint32 HashValue(const KKStr &str)
Definition: HashTable.h:230
template<class Entry >
kkuint32 KKB::HashTable< Entry >::HashValue ( const KKStr str)

Definition at line 230 of file HashTable.h.

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 
245  hashVal = hashVal % tableSize;
246  return hashVal;
247  } /* HashValue */
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
template<class Entry >
HashTable< Entry >::EntryPtr KKB::HashTable< Entry >::LocateEntry ( const KKStr str)

Definition at line 145 of file HashTable.h.

146  {
147  kkuint32 hashValue = HashValue (key);
148 
149  // kkuint32 idx = CalcIndex (hashValue);
150  kkuint32 idx = hashValue;
151 
152  EntryPtr entry = NULL;
153 
154  HashEntryPtr next = table[idx];
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 */
Entry * EntryPtr
Definition: HashTable.h:22
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
HashEntry * HashEntryPtr
Definition: HashTable.h:15
kkuint32 HashValue(const KKStr &str)
Definition: HashTable.h:230
template<class Entry >
kkuint32 KKB::HashTable< Entry >::NumOfEntries ( )
inline

Definition at line 70 of file HashTable.h.

70 {return numOfEntries;}
template<class Entry >
void KKB::HashTable< Entry >::Save ( const KKStr fileName)

Definition at line 251 of file HashTable.h.

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 */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
HashEntry * HashEntryPtr
Definition: HashTable.h:15
HashEntryPtr * Table()
Definition: HashTable.h:72
kkuint32 TableSize()
Definition: HashTable.h:74
template<class Entry >
HashEntryPtr* KKB::HashTable< Entry >::Table ( )
inline

Definition at line 72 of file HashTable.h.

72 {return table;}
template<class Entry >
kkuint32 KKB::HashTable< Entry >::TableSize ( )
inline

Definition at line 74 of file HashTable.h.

74 {return tableSize;}

The documentation for this class was generated from the following file: