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 Integer 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.
59 template<typename _Key, typename _Val, typename _KeyInt, int _Shift = 0, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
63 * @brief Hash table constructor
64 * @param initialcapacity Sets the initial capacity of the hash table.
66 * @param factor Sets the percentage full before the hashtable is
67 * resized. Default ratio 0.5.
69 Hashtable(unsigned int initialcapacity = 1024, double factor = 0.5) {
70 // Allocate space for the hash table
71 table = (struct Hashlistnode<_Key, _Val> *)ourcalloc(initialcapacity, sizeof(struct Hashlistnode<_Key, _Val>));
74 capacity = initialcapacity;
75 capacitymask = initialcapacity - 1;
77 threshold = (unsigned int)(initialcapacity * loadfactor);
78 size = 0; // Initial number of elements in the hash
81 /** @brief Hash table destructor */
88 /** Override: new operator */
89 void *operator new(size_t size) {
90 return ourmalloc(size);
93 /** Override: delete operator */
94 void operator delete(void *p, size_t size) {
98 /** Override: new[] operator */
99 void *operator new[](size_t size) {
100 return ourmalloc(size);
103 /** Override: delete[] operator */
104 void operator delete[](void *p, size_t size) {
108 /** @brief Reset the table to its initial state. */
110 memset(table, 0, capacity * sizeof(struct Hashlistnode<_Key, _Val>));
118 /** Doesn't work with zero value */
119 _Val getRandomValue() {
121 unsigned int index = random() & capacitymask;
122 struct Hashlistnode<_Key, _Val> *bin = &table[index];
123 if (bin->key != NULL && bin->val != NULL) {
129 void resetAndDeleteKeys() {
130 for (unsigned int i = 0; i < capacity; i++) {
131 struct Hashlistnode<_Key, _Val> *bin = &table[i];
132 if (bin->key != NULL) {
135 if (bin->val != NULL) {
147 void resetAndDeleteVals() {
148 for (unsigned int i = 0; i < capacity; i++) {
149 struct Hashlistnode<_Key, _Val> *bin = &table[i];
150 if (bin->key != NULL) {
152 if (bin->val != NULL) {
159 if (zero->val != NULL)
167 void resetAndFreeVals() {
168 for (unsigned int i = 0; i < capacity; i++) {
169 struct Hashlistnode<_Key, _Val> *bin = &table[i];
170 if (bin->key != NULL) {
172 if (bin->val != NULL) {
179 if (zero->val != NULL)
188 * @brief Put a key/value pair into the table
189 * @param key The key for the new value; must not be 0 or NULL
190 * @param val The value to store in the table
192 void put(_Key key, _Val val) {
193 /* Hashtable cannot handle 0 as a key */
196 zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
204 if (size > threshold)
205 resize(capacity << 1);
207 struct Hashlistnode<_Key, _Val> *search;
209 unsigned int index = hash_function(key);
211 index &= capacitymask;
212 search = &table[index];
214 //key is null, probably done
217 if (equals(search->key, key)) {
230 * @brief Lookup the corresponding value for the given key
231 * @param key The key for finding the value; must not be 0 or NULL
232 * @return The value in the table, if the key is found; otherwise 0
234 _Val get(_Key key) const {
235 struct Hashlistnode<_Key, _Val> *search;
237 /* Hashtable cannot handle 0 as a key */
245 unsigned int oindex = hash_function(key) & capacitymask;
246 unsigned int index = oindex;
248 search = &table[index];
253 if (equals(search->key, key))
256 index &= capacitymask;
264 * @brief Remove the given key and return the corresponding value
265 * @param key The key for finding the value; must not be 0 or NULL
266 * @return The value in the table, if the key is found; otherwise 0
268 _Val remove(_Key key) {
269 struct Hashlistnode<_Key, _Val> *search;
271 /* Hashtable cannot handle 0 as a key */
285 unsigned int index = hash_function(key);
287 index &= capacitymask;
288 search = &table[index];
293 if (equals(search->key, key)) {
294 _Val v = search->val;
296 search->val = (_Val) 1;
306 unsigned int getSize() const {
312 * @brief Check whether the table contains a value for the given key
313 * @param key The key for finding the value; must not be 0 or NULL
314 * @return True, if the key is found; false otherwise
316 bool contains(_Key key) const {
317 struct Hashlistnode<_Key, _Val> *search;
319 /* Hashtable cannot handle 0 as a key */
324 unsigned int index = hash_function(key);
326 index &= capacitymask;
327 search = &table[index];
332 if (equals(search->key, key))
340 * @brief Resize the table
341 * @param newsize The new size of the table
343 void resize(unsigned int newsize) {
344 struct Hashlistnode<_Key, _Val> *oldtable = table;
345 struct Hashlistnode<_Key, _Val> *newtable;
346 unsigned int oldcapacity = capacity;
348 if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) {
349 model_print("calloc error %s %d\n", __FILE__, __LINE__);
353 table = newtable; // Update the global hashtable upon resize()
355 capacitymask = newsize - 1;
357 threshold = (unsigned int)(newsize * loadfactor);
359 struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
360 struct Hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
361 for (; bin < lastbin; bin++) {
364 struct Hashlistnode<_Key, _Val> *search;
368 unsigned int index = hash_function(key);
370 index &= capacitymask;
371 search = &table[index];
373 } while (search->key);
376 search->val = bin->val;
379 ourfree(oldtable); // Free the memory of the old hash table
381 double getLoadFactor() {return loadfactor;}
382 unsigned int getCapacity() {return capacity;}
383 struct Hashlistnode<_Key, _Val> *table;
384 struct Hashlistnode<_Key, _Val> *zero;
385 unsigned int capacity;
388 unsigned int capacitymask;
389 unsigned int threshold;
393 #endif/* __HASHTABLE_H__ */