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 resetanddelete() {
130 for (unsigned int i = 0; i < capacity; i++) {
131 struct Hashlistnode<_Key, _Val> *bin = &table[i];
132 if (bin->key != NULL) {
134 if (bin->val != NULL) {
141 if (zero->val != NULL)
149 void resetandfree() {
150 for (unsigned int i = 0; i < capacity; i++) {
151 struct Hashlistnode<_Key, _Val> *bin = &table[i];
152 if (bin->key != NULL) {
154 if (bin->val != NULL) {
161 if (zero->val != NULL)
170 * @brief Put a key/value pair into the table
171 * @param key The key for the new value; must not be 0 or NULL
172 * @param val The value to store in the table
174 void put(_Key key, _Val val) {
175 /* Hashtable cannot handle 0 as a key */
178 zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
186 if (size > threshold)
187 resize(capacity << 1);
189 struct Hashlistnode<_Key, _Val> *search;
191 unsigned int index = hash_function(key);
193 index &= capacitymask;
194 search = &table[index];
196 //key is null, probably done
199 if (equals(search->key, key)) {
212 * @brief Lookup the corresponding value for the given key
213 * @param key The key for finding the value; must not be 0 or NULL
214 * @return The value in the table, if the key is found; otherwise 0
216 _Val get(_Key key) const {
217 struct Hashlistnode<_Key, _Val> *search;
219 /* Hashtable cannot handle 0 as a key */
227 unsigned int oindex = hash_function(key) & capacitymask;
228 unsigned int index = oindex;
230 search = &table[index];
235 if (equals(search->key, key))
238 index &= capacitymask;
246 * @brief Remove the given key and return the corresponding value
247 * @param key The key for finding the value; must not be 0 or NULL
248 * @return The value in the table, if the key is found; otherwise 0
250 _Val remove(_Key key) {
251 struct Hashlistnode<_Key, _Val> *search;
253 /* Hashtable cannot handle 0 as a key */
267 unsigned int index = hash_function(key);
269 index &= capacitymask;
270 search = &table[index];
275 if (equals(search->key, key)) {
276 _Val v = search->val;
278 search->val = (_Val) 1;
288 unsigned int getSize() const {
294 * @brief Check whether the table contains a value for the given key
295 * @param key The key for finding the value; must not be 0 or NULL
296 * @return True, if the key is found; false otherwise
298 bool contains(_Key key) const {
299 struct Hashlistnode<_Key, _Val> *search;
301 /* Hashtable cannot handle 0 as a key */
306 unsigned int index = hash_function(key);
308 index &= capacitymask;
309 search = &table[index];
314 if (equals(search->key, key))
322 * @brief Resize the table
323 * @param newsize The new size of the table
325 void resize(unsigned int newsize) {
326 struct Hashlistnode<_Key, _Val> *oldtable = table;
327 struct Hashlistnode<_Key, _Val> *newtable;
328 unsigned int oldcapacity = capacity;
330 if ((newtable = (struct Hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct Hashlistnode<_Key, _Val>))) == NULL) {
331 model_print("calloc error %s %d\n", __FILE__, __LINE__);
335 table = newtable; // Update the global hashtable upon resize()
337 capacitymask = newsize - 1;
339 threshold = (unsigned int)(newsize * loadfactor);
341 struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
342 struct Hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
343 for (; bin < lastbin; bin++) {
346 struct Hashlistnode<_Key, _Val> *search;
350 unsigned int index = hash_function(key);
352 index &= capacitymask;
353 search = &table[index];
355 } while (search->key);
358 search->val = bin->val;
361 ourfree(oldtable); // Free the memory of the old hash table
363 double getLoadFactor() {return loadfactor;}
364 unsigned int getCapacity() {return capacity;}
365 struct Hashlistnode<_Key, _Val> *table;
366 struct Hashlistnode<_Key, _Val> *zero;
367 unsigned int capacity;
370 unsigned int capacitymask;
371 unsigned int threshold;
375 #endif/* __HASHTABLE_H__ */