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;
186 unsigned int index = hash_function(key);
188 index &= capacitymask;
189 search = &table[index];
191 //key is null, probably done
194 if (equals(search->key, key)) {
207 * @brief Lookup the corresponding value for the given key
208 * @param key The key for finding the value; must not be 0 or NULL
209 * @return The value in the table, if the key is found; otherwise 0
211 _Val get(_Key key) const {
212 struct hashlistnode<_Key, _Val> *search;
214 /* HashTable cannot handle 0 as a key */
222 unsigned int oindex = hash_function(key) & capacitymask;
223 unsigned int index=oindex;
225 search = &table[index];
230 if (equals(search->key, key))
233 index &= capacitymask;
241 * @brief Remove the given key and return the corresponding value
242 * @param key The key for finding the value; must not be 0 or NULL
243 * @return The value in the table, if the key is found; otherwise 0
245 _Val remove(_Key key) {
246 struct hashlistnode<_Key, _Val> *search;
248 /* HashTable cannot handle 0 as a key */
262 unsigned int index = hash_function(key);
264 index &= capacitymask;
265 search = &table[index];
270 if (equals(search->key, key)) {
273 search->val=(_Val) 1;
283 unsigned int getSize() const {
292 * @brief Check whether the table contains a value for the given key
293 * @param key The key for finding the value; must not be 0 or NULL
294 * @return True, if the key is found; false otherwise
296 bool contains(_Key key) const {
297 struct hashlistnode<_Key, _Val> *search;
299 /* HashTable cannot handle 0 as a key */
304 unsigned int index = hash_function(key);
306 index &= capacitymask;
307 search = &table[index];
312 if (equals(search->key, key))
320 * @brief Resize the table
321 * @param newsize The new size of the table
323 void resize(unsigned int newsize) {
324 struct hashlistnode<_Key, _Val> *oldtable = table;
325 struct hashlistnode<_Key, _Val> *newtable;
326 unsigned int oldcapacity = capacity;
328 if ((newtable = (struct hashlistnode<_Key, _Val> *)_calloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
329 model_print("calloc error %s %d\n", __FILE__, __LINE__);
333 table = newtable; // Update the global hashtable upon resize()
335 capacitymask = newsize - 1;
337 threshold = (unsigned int)(newsize * loadfactor);
339 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
340 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
341 for (;bin < lastbin;bin++) {
344 struct hashlistnode<_Key, _Val> *search;
348 unsigned int index = hash_function(key);
350 index &= capacitymask;
351 search = &table[index];
353 } while (search->key);
356 search->val = bin->val;
359 _free(oldtable); // Free the memory of the old hash table
361 double getLoadFactor() {return loadfactor;}
362 unsigned int getCapacity() {return capacity;}
363 struct hashlistnode<_Key, _Val> *table;
364 struct hashlistnode<_Key, _Val> *zero;
365 unsigned int capacity;
368 unsigned int capacitymask;
369 unsigned int threshold;
373 #endif /* __HASHTABLE_H__ */