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.
14 #ifndef __HASHTABLE_H__
15 #define __HASHTABLE_H__
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 default_hash_function(_Key hash) {
37 return (unsigned int)(((_KeyInt)hash) >> _Shift);
40 template<typename _Key>
41 inline bool default_equals(_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.
58 * @tparam _malloc Provide your own 'malloc' for the table, or default to
60 * @tparam _calloc Provide your own 'calloc' for the table, or default to
62 * @tparam _free Provide your own 'free' for the table, or default to
65 template<typename _Key, typename _Val, typename _KeyInt, int _Shift = 0, void * (*_malloc)(size_t) = snapshot_malloc, void * (*_calloc)(size_t, size_t) = snapshot_calloc, void (*_free)(void *) = snapshot_free, unsigned int (*hash_function)(_Key) = default_hash_function<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = default_equals<_Key> >
69 * @brief Hash table constructor
70 * @param initialcapacity Sets the initial capacity of the hash table.
72 * @param factor Sets the percentage full before the hashtable is
73 * resized. Default ratio 0.5.
75 HashTable(unsigned int initialcapacity = 1024, double factor = 0.5) {
76 // Allocate space for the hash table
77 table = (struct hashlistnode<_Key, _Val> *)_calloc(initialcapacity, sizeof(struct hashlistnode<_Key, _Val>));
80 capacity = initialcapacity;
81 capacitymask = initialcapacity - 1;
83 threshold = (unsigned int)(initialcapacity * loadfactor);
84 size = 0; // Initial number of elements in the hash
87 /** @brief Hash table destructor */
94 /** Override: new operator */
95 void * operator new(size_t size) {
99 /** Override: delete operator */
100 void operator delete(void *p, size_t size) {
104 /** Override: new[] operator */
105 void * operator new[](size_t size) {
106 return _malloc(size);
109 /** Override: delete[] operator */
110 void operator delete[](void *p, size_t size) {
114 /** @brief Reset the table to its initial state. */
116 memset(table, 0, capacity * sizeof(struct hashlistnode<_Key, _Val>));
124 void resetanddelete() {
125 for(unsigned int i=0;i<capacity;i++) {
126 struct hashlistnode<_Key, _Val> *bin = &table[i];
127 if (bin->key != NULL) {
129 if (bin->val != NULL) {
136 if (zero->val != NULL)
144 void resetandfree() {
145 for(unsigned int i=0;i<capacity;i++) {
146 struct hashlistnode<_Key, _Val> *bin = &table[i];
147 if (bin->key != NULL) {
149 if (bin->val != NULL) {
156 if (zero->val != NULL)
165 * @brief Put a key/value pair into the table
166 * @param key The key for the new value; must not be 0 or NULL
167 * @param val The value to store in the table
169 void put(_Key key, _Val val) {
170 /* HashTable cannot handle 0 as a key */
173 zero=(struct hashlistnode<_Key, _Val> *)_malloc(sizeof(struct hashlistnode<_Key, _Val>));
181 if (size > threshold)
182 resize(capacity << 1);
184 struct hashlistnode<_Key, _Val> *search;
185 struct hashlistnode<_Key, _Val> *first = NULL;
187 unsigned int index = hash_function(key) & capacitymask;
188 unsigned int oindex = index;
190 search = &table[index];
192 //key is null, probably done
198 if (equals(search->key, key)) {
202 index = (index + 1) & capacitymask;
203 if (index == oindex) {
221 * @brief Lookup the corresponding value for the given key
222 * @param key The key for finding the value; must not be 0 or NULL
223 * @return The value in the table, if the key is found; otherwise 0
225 _Val get(_Key key) const {
226 struct hashlistnode<_Key, _Val> *search;
228 /* HashTable cannot handle 0 as a key */
236 unsigned int oindex = hash_function(key) & capacitymask;
237 unsigned int index = oindex;
239 search = &table[index];
244 if (equals(search->key, key))
247 index &= capacitymask;
255 * @brief Remove the given key and return the corresponding value
256 * @param key The key for finding the value; must not be 0 or NULL
257 * @return The value in the table, if the key is found; otherwise 0
259 _Val remove(_Key key) {
260 struct hashlistnode<_Key, _Val> *search;
261 struct hashlistnode<_Key, _Val> *replace;
263 /* HashTable cannot handle 0 as a key */
277 unsigned int index = hash_function(key);
279 index &= capacitymask;
280 search = &table[index];
285 // The case where an item is found
286 if (equals(search->key, key)) {
287 unsigned int j = index;
288 _Val v = search->val;
291 // Idea: keep bins contiguous
297 j = (j + 1) & capacitymask;
300 if (!replace->key && !replace->val) {
304 unsigned int hash = hash_function(replace->key) & capacitymask;
305 if (index <= j && index < hash && hash <= j)
307 else if (index > j && (index < hash || hash <= j) )
313 table[index] = table[j];
315 search = &table[index];
324 unsigned int getSize() const {
333 * @brief Check whether the table contains a value for the given key
334 * @param key The key for finding the value; must not be 0 or NULL
335 * @return True, if the key is found; false otherwise
337 bool contains(_Key key) const {
338 struct hashlistnode<_Key, _Val> *search;
340 /* HashTable cannot handle 0 as a key */
345 unsigned int index = hash_function(key);
347 index &= capacitymask;
348 search = &table[index];
353 if (equals(search->key, key))
361 * @brief Resize the table
362 * @param newsize The new size of the table
364 void resize(unsigned int newsize) {
365 struct hashlistnode<_Key, _Val> *oldtable = table;
366 struct hashlistnode<_Key, _Val> *newtable;
367 unsigned int oldcapacity = capacity;
369 if ((newtable = (struct hashlistnode<_Key, _Val> *)_calloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
370 model_print("calloc error %s %d\n", __FILE__, __LINE__);
374 table = newtable; // Update the global hashtable upon resize()
376 capacitymask = newsize - 1;
378 threshold = (unsigned int)(newsize * loadfactor);
380 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
381 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
382 for (;bin < lastbin;bin++) {
385 struct hashlistnode<_Key, _Val> *search;
389 unsigned int index = hash_function(key);
391 index &= capacitymask;
392 search = &table[index];
394 } while (search->key);
397 search->val = bin->val;
400 _free(oldtable); // Free the memory of the old hash table
402 double getLoadFactor() {return loadfactor;}
403 unsigned int getCapacity() {return capacity;}
404 struct hashlistnode<_Key, _Val> *table;
405 struct hashlistnode<_Key, _Val> *zero;
406 unsigned int capacity;
409 unsigned int capacitymask;
410 unsigned int threshold;
414 #endif /* __HASHTABLE_H__ */