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 void put(_Key key, _Val val) {
194 /* Hashtable cannot handle 0 as a key */
197 zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
205 if (size > threshold)
206 resize(capacity << 1);
208 struct Hashlistnode<_Key, _Val> *search;
210 unsigned int hashcode = hash_function(key);
211 unsigned int index = hashcode;
213 index &= capacitymask;
214 search = &table[index];
216 //key is null, probably done
219 if (search->hashcode == hashcode)
220 if (equals(search->key, key)) {
229 search->hashcode = hashcode;
234 * @brief Lookup the corresponding value for the given key
235 * @param key The key for finding the value; must not be 0 or NULL
236 * @return The value in the table, if the key is found; otherwise 0
238 _Val get(_Key key) const {
239 struct Hashlistnode<_Key, _Val> *search;
241 /* Hashtable cannot handle 0 as a key */
249 unsigned int hashcode = hash_function(key);
250 unsigned int oindex = hashcode & capacitymask;
251 unsigned int index = oindex;
253 search = &table[index];
258 if (hashcode == search->hashcode)
259 if (equals(search->key, key))
262 index &= capacitymask;
270 * @brief Remove the given key and return the corresponding value
271 * @param key The key for finding the value; must not be 0 or NULL
272 * @return The value in the table, if the key is found; otherwise 0
274 _Val remove(_Key key) {
275 struct Hashlistnode<_Key, _Val> *search;
277 /* Hashtable cannot handle 0 as a key */
291 unsigned int hashcode = hash_function(key);
292 unsigned int index = hashcode;
294 index &= capacitymask;
295 search = &table[index];
300 if (hashcode == search->hashcode)
301 if (equals(search->key, key)) {
302 _Val v = search->val;
304 search->val = (_Val) 1;
314 unsigned int getSize() const {
320 * @brief Check whether the table contains a value for the given key
321 * @param key The key for finding the value; must not be 0 or NULL
322 * @return True, if the key is found; false otherwise
324 bool contains(_Key key) const {
325 struct Hashlistnode<_Key, _Val> *search;
327 /* Hashtable cannot handle 0 as a key */
332 unsigned int index = hash_function(key);
333 unsigned int hashcode = index;
335 index &= capacitymask;
336 search = &table[index];
341 if (hashcode == search->hashcode)
342 if (equals(search->key, key))
350 * @brief Resize the table
351 * @param newsize The new size of the table
353 void resize(unsigned int newsize) {
354 struct Hashlistnode<_Key, _Val> *oldtable = table;
355 struct Hashlistnode<_Key, _Val> *newtable;
356 unsigned int oldcapacity = capacity;
358 if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) {
359 model_print("calloc error %s %d\n", __FILE__, __LINE__);
363 table = newtable; // Update the global hashtable upon resize()
365 capacitymask = newsize - 1;
367 threshold = (unsigned int)(newsize * loadfactor);
369 struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
370 struct Hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
371 for (; bin < lastbin; bin++) {
374 struct Hashlistnode<_Key, _Val> *search;
378 unsigned int hashcode = bin->hashcode;
379 unsigned int index = hashcode;
381 index &= capacitymask;
382 search = &table[index];
384 } while (search->key);
386 search->hashcode = hashcode;
388 search->val = bin->val;
391 ourfree(oldtable); // Free the memory of the old hash table
393 double getLoadFactor() {return loadfactor;}
394 unsigned int getCapacity() {return capacity;}
395 struct Hashlistnode<_Key, _Val> *table;
396 struct Hashlistnode<_Key, _Val> *zero;
397 unsigned int capacity;
400 unsigned int capacitymask;
401 unsigned int threshold;
405 #endif/* __HASHTABLE_H__ */