2 * @brief Hashtable. Standard chained bucket variety.
5 #ifndef __HASHTABLE_H__
6 #define __HASHTABLE_H__
15 * @brief HashTable node
17 * @tparam _Key Type name for the key
18 * @tparam _Val Type name for the values to be stored
20 template<typename _Key, typename _Val>
27 * @brief A simple, custom hash table
29 * By default it is snapshotting, but you can pass in your own allocation
30 * functions. Note that this table does not support the value 0 (NULL) used as
31 * a key and is designed primarily with pointer-based keys in mind. Other
32 * primitive key types are supported only for non-zero values.
34 * @tparam _Key Type name for the key
35 * @tparam _Val Type name for the values to be stored
36 * @tparam _KeyInt Integer type that is at least as large as _Key. Used for key
37 * manipulation and storage.
38 * @tparam _Shift Logical shift to apply to all keys. Default 0.
39 * @tparam _malloc Provide your own 'malloc' for the table, or default to
41 * @tparam _calloc Provide your own 'calloc' for the table, or default to
43 * @tparam _free Provide your own 'free' for the table, or default to
46 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>
50 * @brief Hash table constructor
51 * @param initialcapacity Sets the initial capacity of the hash table.
53 * @param factor Sets the percentage full before the hashtable is
54 * resized. Default ratio 0.5.
56 HashTable(unsigned int initialcapacity = 1024, double factor = 0.5) {
57 // Allocate space for the hash table
58 table = (struct hashlistnode<_Key, _Val> *)_calloc(initialcapacity, sizeof(struct hashlistnode<_Key, _Val>));
60 capacity = initialcapacity;
61 capacitymask = initialcapacity - 1;
63 threshold = (unsigned int)(initialcapacity * loadfactor);
64 size = 0; // Initial number of elements in the hash
67 /** @brief Hash table destructor */
72 /** Override: new operator */
73 void * operator new(size_t size) {
77 /** Override: delete operator */
78 void operator delete(void *p, size_t size) {
82 /** Override: new[] operator */
83 void * operator new[](size_t size) {
87 /** Override: delete[] operator */
88 void operator delete[](void *p, size_t size) {
92 /** @brief Reset the table to its initial state. */
94 memset(table, 0, capacity * sizeof(struct hashlistnode<_Key, _Val>));
99 * @brief Put a key/value pair into the table
100 * @param key The key for the new value; must not be 0 or NULL
101 * @param val The value to store in the table
103 void put(_Key key, _Val val) {
104 /* HashTable cannot handle 0 as a key */
107 if (size > threshold)
108 resize(capacity << 1);
110 struct hashlistnode<_Key, _Val> *search;
112 unsigned int index = ((_KeyInt)key) >> _Shift;
114 index &= capacitymask;
115 search = &table[index];
116 if (search->key == key) {
121 } while (search->key);
129 * @brief Lookup the corresponding value for the given key
130 * @param key The key for finding the value; must not be 0 or NULL
131 * @return The value in the table, if the key is found; otherwise 0
133 _Val get(_Key key) const {
134 struct hashlistnode<_Key, _Val> *search;
136 /* HashTable cannot handle 0 as a key */
139 unsigned int index = ((_KeyInt)key) >> _Shift;
141 index &= capacitymask;
142 search = &table[index];
143 if (search->key == key)
146 } while (search->key);
151 * @brief Check whether the table contains a value for the given key
152 * @param key The key for finding the value; must not be 0 or NULL
153 * @return True, if the key is found; false otherwise
155 bool contains(_Key key) const {
156 struct hashlistnode<_Key, _Val> *search;
158 /* HashTable cannot handle 0 as a key */
161 unsigned int index = ((_KeyInt)key) >> _Shift;
163 index &= capacitymask;
164 search = &table[index];
165 if (search->key == key)
168 } while (search->key);
173 * @brief Resize the table
174 * @param newsize The new size of the table
176 void resize(unsigned int newsize) {
177 struct hashlistnode<_Key, _Val> *oldtable = table;
178 struct hashlistnode<_Key, _Val> *newtable;
179 unsigned int oldcapacity = capacity;
181 if ((newtable = (struct hashlistnode<_Key, _Val> *)_calloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
182 model_print("calloc error %s %d\n", __FILE__, __LINE__);
186 table = newtable; // Update the global hashtable upon resize()
188 capacitymask = newsize - 1;
190 threshold = (unsigned int)(newsize * loadfactor);
192 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
193 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
194 for (; bin < lastbin; bin++) {
197 struct hashlistnode<_Key, _Val> *search;
199 unsigned int index = ((_KeyInt)key) >> _Shift;
201 index &= capacitymask;
202 search = &table[index];
204 } while (search->key);
207 search->val = bin->val;
210 _free(oldtable); // Free the memory of the old hash table
214 struct hashlistnode<_Key, _Val> *table;
215 unsigned int capacity;
217 unsigned int capacitymask;
218 unsigned int threshold;
222 #endif /* __HASHTABLE_H__ */