2 * @brief Hashtable. Standard chained bucket variety.
5 #ifndef __HASHTABLE_H__
6 #define __HASHTABLE_H__
11 #include "stl-model.h"
16 * @brief HashTable node
18 * @tparam _Key Type name for the key
19 * @tparam _Val Type name for the values to be stored
21 template<typename _Key, typename _Val>
28 * @brief A simple, custom hash table
30 * By default it is snapshotting, but you can pass in your own allocation
31 * functions. Note that this table does not support the value 0 (NULL) used as
32 * a key and is designed primarily with pointer-based keys in mind. Other
33 * primitive key types are supported only for non-zero values.
35 * @tparam _Key Type name for the key
36 * @tparam _Val Type name for the values to be stored
37 * @tparam _KeyInt Integer type that is at least as large as _Key. Used for key
38 * manipulation and storage.
39 * @tparam _Shift Logical shift to apply to all keys. Default 0.
40 * @tparam _malloc Provide your own 'malloc' for the table, or default to
42 * @tparam _calloc Provide your own 'calloc' for the table, or default to
44 * @tparam _free Provide your own 'free' for the table, or default to
47 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>
51 * @brief Hash table constructor
52 * @param initialcapacity Sets the initial capacity of the hash table.
54 * @param factor Sets the percentage full before the hashtable is
55 * resized. Default ratio 0.5.
57 HashTable(unsigned int initialcapacity = 1024, double factor = 0.5) {
58 // Allocate space for the hash table
59 table = (struct hashlistnode<_Key, _Val> *)_calloc(initialcapacity, sizeof(struct hashlistnode<_Key, _Val>));
60 keyset = (_Key *)_calloc(initialcapacity, sizeof(_Key));
63 capacity = initialcapacity;
64 capacitymask = initialcapacity - 1;
66 threshold = (unsigned int)(initialcapacity * loadfactor);
67 size = 0; // Initial number of elements in the hash
70 /** @brief Hash table destructor */
76 /** Override: new operator */
77 void * operator new(size_t size) {
81 /** Override: delete operator */
82 void operator delete(void *p, size_t size) {
86 /** Override: new[] operator */
87 void * operator new[](size_t size) {
91 /** Override: delete[] operator */
92 void operator delete[](void *p, size_t size) {
96 /** @brief Reset the table to its initial state. */
98 memset(table, 0, capacity * sizeof(struct hashlistnode<_Key, _Val>));
99 memset(keyset, 0, capacity * sizeof(_Key));
104 * @brief Put a key/value pair into the table
105 * @param key The key for the new value; must not be 0 or NULL
106 * @param val The value to store in the table
108 void put(_Key key, _Val val) {
109 /* HashTable cannot handle 0 as a key */
112 if (size > threshold)
113 resize(capacity << 1);
115 struct hashlistnode<_Key, _Val> *search;
117 unsigned int index = ((_KeyInt)key) >> _Shift;
119 index &= capacitymask;
120 search = &table[index];
121 if (search->key == key) {
126 } while (search->key);
136 * @brief Lookup the corresponding value for the given key
137 * @param key The key for finding the value; must not be 0 or NULL
138 * @return The value in the table, if the key is found; otherwise 0
140 _Val get(_Key key) const {
141 struct hashlistnode<_Key, _Val> *search;
143 /* HashTable cannot handle 0 as a key */
146 unsigned int index = ((_KeyInt)key) >> _Shift;
148 index &= capacitymask;
149 search = &table[index];
150 if (search->key == key)
153 } while (search->key);
158 * @brief Check whether the table contains a value for the given key
159 * @param key The key for finding the value; must not be 0 or NULL
160 * @return True, if the key is found; false otherwise
162 bool contains(_Key key) const {
163 struct hashlistnode<_Key, _Val> *search;
165 /* HashTable cannot handle 0 as a key */
168 unsigned int index = ((_KeyInt)key) >> _Shift;
170 index &= capacitymask;
171 search = &table[index];
172 if (search->key == key)
175 } while (search->key);
180 * @brief Resize the table
181 * @param newsize The new size of the table
183 void resize(unsigned int newsize) {
184 struct hashlistnode<_Key, _Val> *oldtable = table;
185 struct hashlistnode<_Key, _Val> *newtable;
186 _Key *oldkeyset = keyset;
189 unsigned int oldcapacity = capacity;
191 if ((newtable = (struct hashlistnode<_Key, _Val> *)_calloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
192 model_print("calloc error %s %d\n", __FILE__, __LINE__);
196 if ((newkeyset = (_Key *)_calloc(newsize, sizeof(_Key))) == NULL ) {
197 model_print("calloc error %s %d\n", __FILE__, __LINE__);
201 table = newtable; // Update the global hashtable upon resize()
203 keyset_end = newkeyset;
206 capacitymask = newsize - 1;
208 threshold = (unsigned int)(newsize * loadfactor);
210 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
211 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
212 for (;bin < lastbin;bin++) {
215 struct hashlistnode<_Key, _Val> *search;
217 unsigned int index = ((_KeyInt)key) >> _Shift;
219 index &= capacitymask;
220 search = &table[index];
222 } while (search->key);
225 search->val = bin->val;
228 memcpy(keyset, oldkeyset, size * sizeof(_Key)); // copy keyset
229 keyset_end = keyset + size; // pointer arithmetic
231 _free(oldtable); // Free the memory of the old hash table
239 unsigned int getSize() {
244 struct hashlistnode<_Key, _Val> *table;
247 unsigned int capacity;
249 unsigned int capacitymask;
250 unsigned int threshold;
254 #endif /* __HASHTABLE_H__ */