1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
11 * @brief Hashtable. Standard chained bucket variety.
24 * @brief Hashtable node
26 * @tparam _Key Type name for the key
27 * @tparam _Val Type name for the values to be stored
29 template<typename _Key, typename _Val>
34 struct Hashlistnode<_Key, _Val> *next;
35 struct Hashlistnode<_Key, _Val> *prev;
38 template<typename _Key, int _Shift, typename _KeyInt>
39 inline unsigned int defaultHashFunction(_Key hash) {
40 return (unsigned int)(((_KeyInt)hash) >> _Shift);
43 template<typename _Key>
44 inline bool defaultEquals(_Key key1, _Key key2) {
49 * @brief A simple, custom hash table
51 * By default it is snapshotting, but you can pass in your own allocation
52 * functions. Note that this table does not support the value 0 (NULL) used as
53 * a key and is designed primarily with pointer-based keys in mind. Other
54 * primitive key types are supported only for non-zero values.
56 * @tparam _Key Type name for the key
57 * @tparam _Val Type name for the values to be stored
58 * @tparam _KeyInt int32_t type that is at least as large as _Key. Used for key
59 * manipulation and storage.
60 * @tparam _Shift Logical shift to apply to all keys. Default 0.
63 template<typename _Key, typename _Val, typename _KeyInt = uintptr_t, int _Shift = 0, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
67 * @brief Hash table constructor
68 * @param initialcapacity Sets the initial capacity of the hash table.
70 * @param factor Sets the percentage full before the hashtable is
71 * resized. Default ratio 0.5.
73 Hashtable(unsigned int initialcapacity = 1024, double factor = 0.5) {
74 // Allocate space for the hash table
75 table = (struct Hashlistnode<_Key, _Val> *)ourcalloc(initialcapacity, sizeof(struct Hashlistnode<_Key, _Val>));
78 capacity = initialcapacity;
79 capacitymask = initialcapacity - 1;
81 threshold = (unsigned int)(initialcapacity * loadfactor);
82 Size = 0; // Initial number of elements in the hash
86 Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> *clone() {
87 Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> *ctable = new Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> (capacity, loadfactor);
88 struct Hashlistnode<_Key, _Val> *ptr = list;
90 ctable->put(ptr->key, ptr->val);
97 /** @brief Hash table destructor */
104 /** Override: new operator */
105 void *operator new(size_t size) {
106 return ourmalloc(size);
109 /** Override: delete operator */
110 void operator delete(void *p, size_t size) {
114 /** Override: new[] operator */
115 void *operator new[](size_t size) {
116 return ourmalloc(size);
119 /** Override: delete[] operator */
120 void operator delete[](void *p, size_t size) {
124 /** @brief Reset the table to its initial state. */
126 memset(table, 0, capacity * sizeof(struct Hashlistnode<_Key, _Val>));
135 /** Doesn't work with zero value */
136 _Val getRandomValue() {
138 unsigned int index = random() & capacitymask;
139 struct Hashlistnode<_Key, _Val> *bin = &table[index];
140 if (bin->key != NULL && bin->val != NULL) {
146 void resetAndDeleteKeys() {
147 for (unsigned int i = 0; i < capacity; i++) {
148 struct Hashlistnode<_Key, _Val> *bin = &table[i];
149 if (bin->key != NULL) {
152 if (bin->val != NULL) {
165 void resetAndDeleteVals() {
166 for (unsigned int i = 0; i < capacity; i++) {
167 struct Hashlistnode<_Key, _Val> *bin = &table[i];
168 if (bin->key != NULL) {
170 if (bin->val != NULL) {
177 if (zero->val != NULL)
186 void resetAndFreeVals() {
187 for (unsigned int i = 0; i < capacity; i++) {
188 struct Hashlistnode<_Key, _Val> *bin = &table[i];
189 if (bin->key != NULL) {
191 if (bin->val != NULL) {
198 if (zero->val != NULL)
208 * @brief Put a key/value pair into the table
209 * @param key The key for the new value; must not be 0 or NULL
210 * @param val The value to store in the table
212 _Val put(_Key key, _Val val) {
213 /* Hashtable cannot handle 0 as a key */
217 zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
233 if (Size > threshold)
234 resize(capacity << 1);
236 struct Hashlistnode<_Key, _Val> *search;
238 unsigned int hashcode = hash_function(key);
239 unsigned int index = hashcode;
241 index &= capacitymask;
242 search = &table[index];
244 //key is null, probably done
247 if (search->hashcode == hashcode)
248 if (equals(search->key, key)) {
249 _Val oldval = search->val;
258 search->hashcode = hashcode;
269 * @brief Lookup the corresponding value for the given key
270 * @param key The key for finding the value; must not be 0 or NULL
271 * @return The value in the table, if the key is found; otherwise 0
273 _Val get(_Key key) const {
274 struct Hashlistnode<_Key, _Val> *search;
276 /* Hashtable cannot handle 0 as a key */
284 unsigned int hashcode = hash_function(key);
285 unsigned int oindex = hashcode & capacitymask;
286 unsigned int index = oindex;
288 search = &table[index];
293 if (hashcode == search->hashcode)
294 if (equals(search->key, key))
297 index &= capacitymask;
305 * @brief Remove the given key and return the corresponding value
306 * @param key The key for finding the value; must not be 0 or NULL
307 * @return The value in the table, if the key is found; otherwise 0
309 _Val remove(_Key key) {
310 struct Hashlistnode<_Key, _Val> *search;
317 if (zero->next != NULL)
318 zero->next->prev = zero->prev;
322 if (zero->prev != NULL)
323 zero->prev->next = zero->next;
335 unsigned int hashcode = hash_function(key);
336 unsigned int index = hashcode;
338 index &= capacitymask;
339 search = &table[index];
344 if (hashcode == search->hashcode)
345 if (equals(search->key, key)) {
346 _Val v = search->val;
348 search->val = (_Val) 1;
351 if (search->next != NULL)
352 search->next->prev = search->prev;
356 if (search->prev != NULL)
357 search->prev->next = search->next;
369 unsigned int size() const {
375 * @brief Check whether the table contains a value for the given key
376 * @param key The key for finding the value; must not be 0 or NULL
377 * @return True, if the key is found; false otherwise
379 bool contains(_Key key) const {
380 struct Hashlistnode<_Key, _Val> *search;
382 /* Hashtable cannot handle 0 as a key */
387 unsigned int index = hash_function(key);
388 unsigned int hashcode = index;
390 index &= capacitymask;
391 search = &table[index];
396 if (hashcode == search->hashcode)
397 if (equals(search->key, key))
405 * @brief Resize the table
406 * @param newsize The new size of the table
408 void resize(unsigned int newsize) {
409 struct Hashlistnode<_Key, _Val> *oldtable = table;
410 struct Hashlistnode<_Key, _Val> *newtable;
411 unsigned int oldcapacity = capacity;
413 if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) {
414 model_print("calloc error %s %d\n", __FILE__, __LINE__);
418 table = newtable; // Update the global hashtable upon resize()
420 capacitymask = newsize - 1;
422 threshold = (unsigned int)(newsize * loadfactor);
424 struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
425 struct Hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
426 for (; bin < lastbin; bin++) {
429 struct Hashlistnode<_Key, _Val> *search;
433 unsigned int hashcode = bin->hashcode;
434 unsigned int index = hashcode;
436 index &= capacitymask;
437 search = &table[index];
439 } while (search->key);
447 search->hashcode = hashcode;
449 search->val = bin->val;
452 ourfree(oldtable); // Free the memory of the old hash table
454 double getLoadFactor() {return loadfactor;}
455 unsigned int getCapacity() {return capacity;}
456 struct Hashlistnode<_Key, _Val> *table;
457 struct Hashlistnode<_Key, _Val> *zero;
458 struct Hashlistnode<_Key, _Val> *list;
459 struct Hashlistnode<_Key, _Val> *tail;
460 unsigned int capacity;
463 unsigned int capacitymask;
464 unsigned int threshold;
468 #endif/* __HASHTABLE_H__ */