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.
59 template<typename _Key, typename _Val, typename _KeyInt, int _Shift = 0, unsigned int (*hash_function)(_Key) = default_hash_function<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = default_equals<_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 void resetanddelete() {
119 for(unsigned int i=0;i<capacity;i++) {
120 struct hashlistnode<_Key, _Val> *bin = &table[i];
121 if (bin->key != NULL) {
123 if (bin->val != NULL) {
130 if (zero->val != NULL)
138 void resetandfree() {
139 for(unsigned int i=0;i<capacity;i++) {
140 struct hashlistnode<_Key, _Val> *bin = &table[i];
141 if (bin->key != NULL) {
143 if (bin->val != NULL) {
150 if (zero->val != NULL)
159 * @brief Put a key/value pair into the table
160 * @param key The key for the new value; must not be 0 or NULL
161 * @param val The value to store in the table
163 void put(_Key key, _Val val) {
164 /* HashTable cannot handle 0 as a key */
167 zero=(struct hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct hashlistnode<_Key, _Val>));
175 if (size > threshold)
176 resize(capacity << 1);
178 struct hashlistnode<_Key, _Val> *search;
180 unsigned int index = hash_function(key);
182 index &= capacitymask;
183 search = &table[index];
185 //key is null, probably done
188 if (equals(search->key, key)) {
201 * @brief Lookup the corresponding value for the given key
202 * @param key The key for finding the value; must not be 0 or NULL
203 * @return The value in the table, if the key is found; otherwise 0
205 _Val get(_Key key) const {
206 struct hashlistnode<_Key, _Val> *search;
208 /* HashTable cannot handle 0 as a key */
216 unsigned int oindex = hash_function(key) & capacitymask;
217 unsigned int index=oindex;
219 search = &table[index];
224 if (equals(search->key, key))
227 index &= capacitymask;
235 * @brief Remove the given key and return the corresponding value
236 * @param key The key for finding the value; must not be 0 or NULL
237 * @return The value in the table, if the key is found; otherwise 0
239 _Val remove(_Key key) {
240 struct hashlistnode<_Key, _Val> *search;
242 /* HashTable cannot handle 0 as a key */
256 unsigned int index = hash_function(key);
258 index &= capacitymask;
259 search = &table[index];
264 if (equals(search->key, key)) {
267 search->val=(_Val) 1;
277 unsigned int getSize() const {
283 * @brief Check whether the table contains a value for the given key
284 * @param key The key for finding the value; must not be 0 or NULL
285 * @return True, if the key is found; false otherwise
287 bool contains(_Key key) const {
288 struct hashlistnode<_Key, _Val> *search;
290 /* HashTable cannot handle 0 as a key */
295 unsigned int index = hash_function(key);
297 index &= capacitymask;
298 search = &table[index];
303 if (equals(search->key, key))
311 * @brief Resize the table
312 * @param newsize The new size of the table
314 void resize(unsigned int newsize) {
315 struct hashlistnode<_Key, _Val> *oldtable = table;
316 struct hashlistnode<_Key, _Val> *newtable;
317 unsigned int oldcapacity = capacity;
319 if ((newtable = (struct hashlistnode<_Key, _Val> *)ourcalloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
320 model_print("calloc error %s %d\n", __FILE__, __LINE__);
324 table = newtable; // Update the global hashtable upon resize()
326 capacitymask = newsize - 1;
328 threshold = (unsigned int)(newsize * loadfactor);
330 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
331 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
332 for (;bin < lastbin;bin++) {
335 struct hashlistnode<_Key, _Val> *search;
339 unsigned int index = hash_function(key);
341 index &= capacitymask;
342 search = &table[index];
344 } while (search->key);
347 search->val = bin->val;
350 ourfree(oldtable); // Free the memory of the old hash table
352 double getLoadFactor() {return loadfactor;}
353 unsigned int getCapacity() {return capacity;}
354 struct hashlistnode<_Key, _Val> *table;
355 struct hashlistnode<_Key, _Val> *zero;
356 unsigned int capacity;
359 unsigned int capacitymask;
360 unsigned int threshold;
364 #endif/* __HASHTABLE_H__ */