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 model_malloc, 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) = model_malloc, void * (* _calloc)(size_t, size_t) = model_calloc, void (*_free)(void *) = model_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 // Initial number of elements in the hash
88 /** @brief Hash table destructor */
95 /** Override: new operator */
96 void * operator new(size_t size) {
100 /** Override: delete operator */
101 void operator delete(void *p, size_t size) {
105 /** Override: new[] operator */
106 void * operator new[](size_t size) {
107 return _malloc(size);
110 /** Override: delete[] operator */
111 void operator delete[](void *p, size_t size) {
115 /** @brief Reset the table to its initial state. */
117 memset(table, 0, capacity * sizeof(struct hashlistnode<_Key, _Val>));
125 void resetanddelete() {
126 for(unsigned int i=0;i<capacity;i++) {
127 struct hashlistnode<_Key, _Val> *bin = &table[i];
128 if (bin->key != NULL) {
130 if (bin->val != NULL) {
137 if (zero->val != NULL)
145 void resetandfree() {
146 for(unsigned int i=0;i<capacity;i++) {
147 struct hashlistnode<_Key, _Val> *bin = &table[i];
148 if (bin->key != NULL) {
150 if (bin->val != NULL) {
157 if (zero->val != NULL)
166 * @brief Put a key/value pair into the table
167 * @param key The key for the new value; must not be 0 or NULL
168 * @param val The value to store in the table
170 void put(_Key key, _Val val) {
171 /* HashTable cannot handle 0 as a key */
174 zero=(struct hashlistnode<_Key, _Val> *)_malloc(sizeof(struct hashlistnode<_Key, _Val>));
182 if (size > threshold)
183 resize(capacity << 1);
185 struct hashlistnode<_Key, _Val> *search;
187 unsigned int index = hash_function(key);
189 index &= capacitymask;
190 search = &table[index];
192 //key is null, probably done
195 if (equals(search->key, key)) {
208 * @brief Lookup the corresponding value for the given key
209 * @param key The key for finding the value; must not be 0 or NULL
210 * @return The value in the table, if the key is found; otherwise 0
212 _Val get(_Key key) const {
213 struct hashlistnode<_Key, _Val> *search;
215 /* HashTable cannot handle 0 as a key */
223 unsigned int oindex = hash_function(key) & capacitymask;
224 unsigned int index=oindex;
226 search = &table[index];
231 if (equals(search->key, key))
234 index &= capacitymask;
242 * @brief Remove the given key and return the corresponding value
243 * @param key The key for finding the value; must not be 0 or NULL
244 * @return The value in the table, if the key is found; otherwise 0
246 _Val remove(_Key key) {
247 struct hashlistnode<_Key, _Val> *search;
249 /* HashTable cannot handle 0 as a key */
263 unsigned int index = hash_function(key);
265 index &= capacitymask;
266 search = &table[index];
271 if (equals(search->key, key)) {
274 search->val=(_Val) 1;
284 unsigned int getSize() const {
290 * @brief Check whether the table contains a value for the given key
291 * @param key The key for finding the value; must not be 0 or NULL
292 * @return True, if the key is found; false otherwise
294 bool contains(_Key key) const {
295 struct hashlistnode<_Key, _Val> *search;
297 /* HashTable cannot handle 0 as a key */
302 unsigned int index = hash_function(key);
304 index &= capacitymask;
305 search = &table[index];
310 if (equals(search->key, key))
318 * @brief Resize the table
319 * @param newsize The new size of the table
321 void resize(unsigned int newsize) {
322 struct hashlistnode<_Key, _Val> *oldtable = table;
323 struct hashlistnode<_Key, _Val> *newtable;
324 unsigned int oldcapacity = capacity;
326 if ((newtable = (struct hashlistnode<_Key, _Val> *)_calloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
327 model_print("calloc error %s %d\n", __FILE__, __LINE__);
332 // Update the global hashtable upon resize()
334 capacitymask = newsize - 1;
336 threshold = (unsigned int)(newsize * loadfactor);
338 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
339 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
340 for (;bin < lastbin;bin++) {
343 struct hashlistnode<_Key, _Val> *search;
347 unsigned int index = hash_function(key);
349 index &= capacitymask;
350 search = &table[index];
352 } while (search->key);
355 search->val = bin->val;
359 // 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__ */