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>
35 template<typename _Key, int _Shift, typename _KeyInt>
36 inline unsigned int defaultHashFunction(_Key hash) {
37 return (unsigned int)(((_KeyInt)hash) >> _Shift);
40 template<typename _Key>
41 inline bool defaultEquals(_Key key1, _Key key2) {
46 * @brief A simple, custom hash table
48 * By default it is snapshotting, but you can pass in your own allocation
49 * functions. Note that this table does not support the value 0 (NULL) used as
50 * a key and is designed primarily with pointer-based keys in mind. Other
51 * primitive key types are supported only for non-zero values.
53 * @tparam _Key Type name for the key
54 * @tparam _Val Type name for the values to be stored
55 * @tparam _KeyInt int32_t type that is at least as large as _Key. Used for key
56 * manipulation and storage.
57 * @tparam _Shift Logical shift to apply to all keys. Default 0.
60 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> >
64 * @brief Hash table constructor
65 * @param initialcapacity Sets the initial capacity of the hash table.
67 * @param factor Sets the percentage full before the hashtable is
68 * resized. Default ratio 0.5.
70 Hashtable(unsigned int initialcapacity = 16, double factor = 0.5) {
71 // Allocate space for the hash table
72 table = (struct Hashlistnode<_Key, _Val> *)ourcalloc(initialcapacity, sizeof(struct Hashlistnode<_Key, _Val>));
75 capacity = initialcapacity;
76 capacitymask = initialcapacity - 1;
78 threshold = (unsigned int)(initialcapacity * loadfactor);
79 Size = 0; // Initial number of elements in the hash
82 Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> *clone() {
83 Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> *ctable = new Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> (capacity, loadfactor);
85 ctable->put(zero->key, zero->val);
87 for(unsigned int i=0; i<capacity; i++) {
88 struct Hashlistnode<_Key, _Val> ptr = table[i];
89 if (ptr.key && ptr.val)
90 ctable->put(ptr.key, ptr.val);
96 /** @brief Hash table destructor */
103 /** Override: new operator */
104 void *operator new(size_t size) {
105 return ourmalloc(size);
108 /** Override: delete operator */
109 void operator delete(void *p, size_t size) {
113 /** Override: new[] operator */
114 void *operator new[](size_t size) {
115 return ourmalloc(size);
118 /** Override: delete[] operator */
119 void operator delete[](void *p, size_t size) {
123 /** @brief Reset the table to its initial state. */
125 memset(table, 0, capacity * sizeof(struct Hashlistnode<_Key, _Val>));
133 /** Doesn't work with zero value */
134 _Val getRandomValue() {
136 unsigned int index = random() & capacitymask;
137 struct Hashlistnode<_Key, _Val> *bin = &table[index];
138 if (bin->key != NULL && bin->val != NULL) {
144 void resetAndDeleteKeys() {
145 for (unsigned int i = 0; i < capacity; i++) {
146 struct Hashlistnode<_Key, _Val> *bin = &table[i];
147 if (bin->key != NULL) {
150 if (bin->val != NULL) {
162 void resetAndDeleteVals() {
163 for (unsigned int i = 0; i < capacity; i++) {
164 struct Hashlistnode<_Key, _Val> *bin = &table[i];
165 if (bin->key != NULL) {
167 if (bin->val != NULL) {
174 if (zero->val != NULL)
182 void resetAndFreeVals() {
183 for (unsigned int i = 0; i < capacity; i++) {
184 struct Hashlistnode<_Key, _Val> *bin = &table[i];
185 if (bin->key != NULL) {
187 if (bin->val != NULL) {
194 if (zero->val != NULL)
203 * @brief Put a key/value pair into the table
204 * @param key The key for the new value; must not be 0 or NULL
205 * @param val The value to store in the table
207 _Val put(_Key key, _Val val) {
212 zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
222 if (Size > threshold)
223 resize(capacity << 1);
225 struct Hashlistnode<_Key, _Val> *search;
227 unsigned int hashcode = hash_function(key);
228 unsigned int index = hashcode;
230 index &= capacitymask;
231 search = &table[index];
233 //key is null, probably done
236 unsigned int searchhashcode = hash_function(search->key);
237 if (searchhashcode == hashcode)
238 if (equals(search->key, key)) {
239 _Val oldval = search->val;
253 * @brief Lookup the corresponding value for the given key
254 * @param key The key for finding the value; must not be 0 or NULL
255 * @return The value in the table, if the key is found; otherwise 0
257 _Val get(_Key key) const {
258 struct Hashlistnode<_Key, _Val> *search;
260 /* Hashtable cannot handle 0 as a key */
268 unsigned int hashcode = hash_function(key);
269 unsigned int oindex = hashcode & capacitymask;
270 unsigned int index = oindex;
272 search = &table[index];
277 unsigned int searchhashcode = hash_function(search->key);
278 if (hashcode == searchhashcode)
279 if (equals(search->key, key))
283 index &= capacitymask;
291 * @brief Remove the given key and return the corresponding value
292 * @param key The key for finding the value; must not be 0 or NULL
293 * @return The value in the table, if the key is found; otherwise 0
295 _Val remove(_Key key) {
296 struct Hashlistnode<_Key, _Val> *search;
312 unsigned int hashcode = hash_function(key);
313 unsigned int index = hashcode;
315 index &= capacitymask;
316 search = &table[index];
321 unsigned int searchhashcode = hash_function(search->key);
322 if (hashcode == searchhashcode)
323 if (equals(search->key, key)) {
324 _Val v = search->val;
326 search->val = (_Val) 1;
337 unsigned int size() const {
343 * @brief Check whether the table contains a value for the given key
344 * @param key The key for finding the value; must not be 0 or NULL
345 * @return True, if the key is found; false otherwise
347 bool contains(_Key key) const {
348 struct Hashlistnode<_Key, _Val> *search;
350 /* Hashtable cannot handle 0 as a key */
355 unsigned int index = hash_function(key);
356 unsigned int hashcode = index;
358 index &= capacitymask;
359 search = &table[index];
364 unsigned int searchhashcode = hash_function(search->key);
365 if (hashcode == searchhashcode)
366 if (equals(search->key, key))
375 * @brief Resize the table
376 * @param newsize The new size of the table
378 void resize(unsigned int newsize) {
379 struct Hashlistnode<_Key, _Val> *oldtable = table;
380 struct Hashlistnode<_Key, _Val> *newtable;
381 unsigned int oldcapacity = capacity;
383 if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) {
384 model_print("calloc error %s %d\n", __FILE__, __LINE__);
388 table = newtable; // Update the global hashtable upon resize()
390 capacitymask = newsize - 1;
391 threshold = (unsigned int)(newsize * loadfactor);
393 struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
394 struct Hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
395 for (; bin < lastbin; bin++) {
398 struct Hashlistnode<_Key, _Val> *search;
402 unsigned int hashcode = hash_function(bin->key);
403 unsigned int index = hashcode;
405 index &= capacitymask;
406 search = &table[index];
408 } while (search->key);
411 search->val = bin->val;
414 ourfree(oldtable); // Free the memory of the old hash table
416 double getLoadFactor() {return loadfactor;}
417 unsigned int getCapacity() {return capacity;}
418 struct Hashlistnode<_Key, _Val> *table;
419 struct Hashlistnode<_Key, _Val> *zero;
420 unsigned int capacity;
423 unsigned int capacitymask;
424 unsigned int threshold;
428 #endif/* __HASHTABLE_H__ */