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.
62 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> >
66 * @brief Hash table constructor
67 * @param initialcapacity Sets the initial capacity of the hash table.
69 * @param factor Sets the percentage full before the hashtable is
70 * resized. Default ratio 0.5.
72 Hashtable(unsigned int initialcapacity = 1024, double factor = 0.5) {
73 // Allocate space for the hash table
74 table = (struct Hashlistnode<_Key, _Val> *)ourcalloc(initialcapacity, sizeof(struct Hashlistnode<_Key, _Val>));
77 capacity = initialcapacity;
78 capacitymask = initialcapacity - 1;
80 threshold = (unsigned int)(initialcapacity * loadfactor);
81 Size = 0; // Initial number of elements in the hash
85 /** @brief Hash table destructor */
92 /** Override: new operator */
93 void *operator new(size_t size) {
94 return ourmalloc(size);
97 /** Override: delete operator */
98 void operator delete(void *p, size_t size) {
102 /** Override: new[] operator */
103 void *operator new[](size_t size) {
104 return ourmalloc(size);
107 /** Override: delete[] operator */
108 void operator delete[](void *p, size_t size) {
112 /** @brief Reset the table to its initial state. */
114 memset(table, 0, capacity * sizeof(struct Hashlistnode<_Key, _Val>));
123 /** Doesn't work with zero value */
124 _Val getRandomValue() {
126 unsigned int index = random() & capacitymask;
127 struct Hashlistnode<_Key, _Val> *bin = &table[index];
128 if (bin->key != NULL && bin->val != NULL) {
134 void resetAndDeleteKeys() {
135 for (unsigned int i = 0; i < capacity; i++) {
136 struct Hashlistnode<_Key, _Val> *bin = &table[i];
137 if (bin->key != NULL) {
140 if (bin->val != NULL) {
153 void resetAndDeleteVals() {
154 for (unsigned int i = 0; i < capacity; i++) {
155 struct Hashlistnode<_Key, _Val> *bin = &table[i];
156 if (bin->key != NULL) {
158 if (bin->val != NULL) {
165 if (zero->val != NULL)
174 void resetAndFreeVals() {
175 for (unsigned int i = 0; i < capacity; i++) {
176 struct Hashlistnode<_Key, _Val> *bin = &table[i];
177 if (bin->key != NULL) {
179 if (bin->val != NULL) {
186 if (zero->val != NULL)
196 * @brief Put a key/value pair into the table
197 * @param key The key for the new value; must not be 0 or NULL
198 * @param val The value to store in the table
200 _Val put(_Key key, _Val val) {
201 /* Hashtable cannot handle 0 as a key */
205 zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
221 if (Size > threshold)
222 resize(capacity << 1);
224 struct Hashlistnode<_Key, _Val> *search;
226 unsigned int hashcode = hash_function(key);
227 unsigned int index = hashcode;
229 index &= capacitymask;
230 search = &table[index];
232 //key is null, probably done
235 if (search->hashcode == hashcode)
236 if (equals(search->key, key)) {
237 _Val oldval = search->val;
246 search->hashcode = hashcode;
257 * @brief Lookup the corresponding value for the given key
258 * @param key The key for finding the value; must not be 0 or NULL
259 * @return The value in the table, if the key is found; otherwise 0
261 _Val get(_Key key) const {
262 struct Hashlistnode<_Key, _Val> *search;
264 /* Hashtable cannot handle 0 as a key */
272 unsigned int hashcode = hash_function(key);
273 unsigned int oindex = hashcode & capacitymask;
274 unsigned int index = oindex;
276 search = &table[index];
281 if (hashcode == search->hashcode)
282 if (equals(search->key, key))
285 index &= capacitymask;
293 * @brief Remove the given key and return the corresponding value
294 * @param key The key for finding the value; must not be 0 or NULL
295 * @return The value in the table, if the key is found; otherwise 0
297 _Val remove(_Key key) {
298 struct Hashlistnode<_Key, _Val> *search;
305 if (zero -> next != NULL)
306 zero -> next -> prev = zero ->prev;
310 if (zero -> prev != NULL)
311 zero -> prev -> next = zero -> next;
323 unsigned int hashcode = hash_function(key);
324 unsigned int index = hashcode;
326 index &= capacitymask;
327 search = &table[index];
332 if (hashcode == search->hashcode)
333 if (equals(search->key, key)) {
334 _Val v = search->val;
336 search->val = (_Val) 1;
339 if (search -> next != NULL)
340 search -> next -> prev = search ->prev;
342 tail = search -> prev;
344 if (search -> prev != NULL)
345 search -> prev -> next = search -> next;
357 unsigned int size() const {
363 * @brief Check whether the table contains a value for the given key
364 * @param key The key for finding the value; must not be 0 or NULL
365 * @return True, if the key is found; false otherwise
367 bool contains(_Key key) const {
368 struct Hashlistnode<_Key, _Val> *search;
370 /* Hashtable cannot handle 0 as a key */
375 unsigned int index = hash_function(key);
376 unsigned int hashcode = index;
378 index &= capacitymask;
379 search = &table[index];
384 if (hashcode == search->hashcode)
385 if (equals(search->key, key))
393 * @brief Resize the table
394 * @param newsize The new size of the table
396 void resize(unsigned int newsize) {
397 struct Hashlistnode<_Key, _Val> *oldtable = table;
398 struct Hashlistnode<_Key, _Val> *newtable;
399 unsigned int oldcapacity = capacity;
401 if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) {
402 model_print("calloc error %s %d\n", __FILE__, __LINE__);
406 table = newtable; // Update the global hashtable upon resize()
408 capacitymask = newsize - 1;
410 threshold = (unsigned int)(newsize * loadfactor);
412 struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
413 struct Hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
414 for (; bin < lastbin; bin++) {
417 struct Hashlistnode<_Key, _Val> *search;
421 unsigned int hashcode = bin->hashcode;
422 unsigned int index = hashcode;
424 index &= capacitymask;
425 search = &table[index];
427 } while (search->key);
431 search -> next = list;
433 list -> prev = search;
435 search->hashcode = hashcode;
437 search->val = bin->val;
440 ourfree(oldtable); // Free the memory of the old hash table
442 double getLoadFactor() {return loadfactor;}
443 unsigned int getCapacity() {return capacity;}
444 struct Hashlistnode<_Key, _Val> *table;
445 struct Hashlistnode<_Key, _Val> *zero;
446 struct Hashlistnode<_Key, _Val> * list;
447 struct Hashlistnode<_Key, _Val> * tail;
448 unsigned int capacity;
451 unsigned int capacitymask;
452 unsigned int threshold;
456 #endif/* __HASHTABLE_H__ */