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>
36 template<typename _Key, int _Shift, typename _KeyInt>
37 inline unsigned int defaultHashFunction(_Key hash) {
38 return (unsigned int)(((_KeyInt)hash) >> _Shift);
41 template<typename _Key>
42 inline bool defaultEquals(_Key key1, _Key key2) {
47 * @brief A simple, custom hash table
49 * By default it is snapshotting, but you can pass in your own allocation
50 * functions. Note that this table does not support the value 0 (NULL) used as
51 * a key and is designed primarily with pointer-based keys in mind. Other
52 * primitive key types are supported only for non-zero values.
54 * @tparam _Key Type name for the key
55 * @tparam _Val Type name for the values to be stored
56 * @tparam _KeyInt int32_t type that is at least as large as _Key. Used for key
57 * manipulation and storage.
58 * @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 = 1024, 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 /** @brief Hash table destructor */
89 /** Override: new operator */
90 void *operator new(size_t size) {
91 return ourmalloc(size);
94 /** Override: delete operator */
95 void operator delete(void *p, size_t size) {
99 /** Override: new[] operator */
100 void *operator new[](size_t size) {
101 return ourmalloc(size);
104 /** Override: delete[] operator */
105 void operator delete[](void *p, size_t size) {
109 /** @brief Reset the table to its initial state. */
111 memset(table, 0, capacity * sizeof(struct Hashlistnode<_Key, _Val>));
119 /** Doesn't work with zero value */
120 _Val getRandomValue() {
122 unsigned int index = random() & capacitymask;
123 struct Hashlistnode<_Key, _Val> *bin = &table[index];
124 if (bin->key != NULL && bin->val != NULL) {
130 void resetAndDeleteKeys() {
131 for (unsigned int i = 0; i < capacity; i++) {
132 struct Hashlistnode<_Key, _Val> *bin = &table[i];
133 if (bin->key != NULL) {
136 if (bin->val != NULL) {
148 void resetAndDeleteVals() {
149 for (unsigned int i = 0; i < capacity; i++) {
150 struct Hashlistnode<_Key, _Val> *bin = &table[i];
151 if (bin->key != NULL) {
153 if (bin->val != NULL) {
160 if (zero->val != NULL)
168 void resetAndFreeVals() {
169 for (unsigned int i = 0; i < capacity; i++) {
170 struct Hashlistnode<_Key, _Val> *bin = &table[i];
171 if (bin->key != NULL) {
173 if (bin->val != NULL) {
180 if (zero->val != NULL)
189 * @brief Put a key/value pair into the table
190 * @param key The key for the new value; must not be 0 or NULL
191 * @param val The value to store in the table
193 _Val put(_Key key, _Val val) {
194 /* Hashtable cannot handle 0 as a key */
198 zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
208 if (Size > threshold)
209 resize(capacity << 1);
211 struct Hashlistnode<_Key, _Val> *search;
213 unsigned int hashcode = hash_function(key);
214 unsigned int index = hashcode;
216 index &= capacitymask;
217 search = &table[index];
219 //key is null, probably done
222 if (search->hashcode == hashcode)
223 if (equals(search->key, key)) {
224 _Val oldval = search->val;
233 search->hashcode = hashcode;
239 * @brief Lookup the corresponding value for the given key
240 * @param key The key for finding the value; must not be 0 or NULL
241 * @return The value in the table, if the key is found; otherwise 0
243 _Val get(_Key key) const {
244 struct Hashlistnode<_Key, _Val> *search;
246 /* Hashtable cannot handle 0 as a key */
254 unsigned int hashcode = hash_function(key);
255 unsigned int oindex = hashcode & capacitymask;
256 unsigned int index = oindex;
258 search = &table[index];
263 if (hashcode == search->hashcode)
264 if (equals(search->key, key))
267 index &= capacitymask;
275 * @brief Remove the given key and return the corresponding value
276 * @param key The key for finding the value; must not be 0 or NULL
277 * @return The value in the table, if the key is found; otherwise 0
279 _Val remove(_Key key) {
280 struct Hashlistnode<_Key, _Val> *search;
282 /* Hashtable cannot handle 0 as a key */
296 unsigned int hashcode = hash_function(key);
297 unsigned int index = hashcode;
299 index &= capacitymask;
300 search = &table[index];
305 if (hashcode == search->hashcode)
306 if (equals(search->key, key)) {
307 _Val v = search->val;
309 search->val = (_Val) 1;
319 unsigned int size() const {
325 * @brief Check whether the table contains a value for the given key
326 * @param key The key for finding the value; must not be 0 or NULL
327 * @return True, if the key is found; false otherwise
329 bool contains(_Key key) const {
330 struct Hashlistnode<_Key, _Val> *search;
332 /* Hashtable cannot handle 0 as a key */
337 unsigned int index = hash_function(key);
338 unsigned int hashcode = index;
340 index &= capacitymask;
341 search = &table[index];
346 if (hashcode == search->hashcode)
347 if (equals(search->key, key))
355 * @brief Resize the table
356 * @param newsize The new size of the table
358 void resize(unsigned int newsize) {
359 struct Hashlistnode<_Key, _Val> *oldtable = table;
360 struct Hashlistnode<_Key, _Val> *newtable;
361 unsigned int oldcapacity = capacity;
363 if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) {
364 model_print("calloc error %s %d\n", __FILE__, __LINE__);
368 table = newtable; // Update the global hashtable upon resize()
370 capacitymask = newsize - 1;
372 threshold = (unsigned int)(newsize * loadfactor);
374 struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
375 struct Hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
376 for (; bin < lastbin; bin++) {
379 struct Hashlistnode<_Key, _Val> *search;
383 unsigned int hashcode = bin->hashcode;
384 unsigned int index = hashcode;
386 index &= capacitymask;
387 search = &table[index];
389 } while (search->key);
391 search->hashcode = hashcode;
393 search->val = bin->val;
396 ourfree(oldtable); // Free the memory of the old hash table
398 double getLoadFactor() {return loadfactor;}
399 unsigned int getCapacity() {return capacity;}
400 struct Hashlistnode<_Key, _Val> *table;
401 struct Hashlistnode<_Key, _Val> *zero;
402 unsigned int capacity;
405 unsigned int capacitymask;
406 unsigned int threshold;
410 #endif/* __HASHTABLE_H__ */