2 * @brief Hashtable. Standard chained bucket variety.
15 * Hashtable linked node class, for chained storage of hash table conflicts. By
16 * default it is snapshotting, but you can pass in your own allocation
19 * @tparam _Key Type name for the key
20 * @tparam _Val Type name for the values to be stored
21 * @tparam _malloc Provide your own 'malloc' for the table, or default to
23 * @tparam _calloc Provide your own 'calloc' for the table, or default to
25 * @tparam _free Provide your own 'free' for the table, or default to
28 template<typename _Key, typename _Val>
36 * Hashtable class. By default it is snapshotting, but you can pass in your own
37 * allocation functions.
39 * @tparam _Key Type name for the key
40 * @tparam _Val Type name for the values to be stored
41 * @tparam _KeyInt Integer type that is at least as large as _Key. Used for key
42 * manipulation and storage.
43 * @tparam _Shift Logical shift to apply to all keys. Default 0.
44 * @tparam _malloc Provide your own 'malloc' for the table, or default to
46 * @tparam _calloc Provide your own 'calloc' for the table, or default to
48 * @tparam _free Provide your own 'free' for the table, or default to
51 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>
56 * @param initialcapacity Sets the initial capacity of the hash table.
58 * @param factor Sets the percentage full before the hashtable is
59 * resized. Default ratio 0.5.
61 HashTable(unsigned int initialcapacity = 1024, double factor = 0.5) {
62 // Allocate space for the hash table
63 table = (struct hashlistnode<_Key, _Val> *)_calloc(initialcapacity, sizeof(struct hashlistnode<_Key, _Val>));
65 capacity = initialcapacity;
66 capacitymask = initialcapacity - 1;
68 threshold = (unsigned int)(initialcapacity * loadfactor);
69 size = 0; // Initial number of elements in the hash
77 /** Override: new operator */
78 void * operator new(size_t size) {
82 /** Override: delete operator */
83 void operator delete(void *p, size_t size) {
87 /** Override: new[] operator */
88 void * operator new[](size_t size) {
92 /** Override: delete[] operator */
93 void operator delete[](void *p, size_t size) {
97 /** Reset the table to its initial state. */
99 memset(table, 0, capacity * sizeof(struct hashlistnode<_Key, _Val>));
103 /** Put a key value pair into the table. */
104 void put(_Key key, _Val val) {
105 if (size > threshold)
106 resize(capacity << 1);
108 struct hashlistnode<_Key, _Val> *search;
110 unsigned int index = ((_KeyInt)key) >> _Shift;
112 index &= capacitymask;
113 search = &table[index];
114 if (search->key == key) {
119 } while (search->key);
126 /** Lookup the corresponding value for the given key. */
127 _Val get(_Key key) const {
128 struct hashlistnode<_Key, _Val> *search;
130 unsigned int index = ((_KeyInt)key) >> _Shift;
132 index &= capacitymask;
133 search = &table[index];
134 if (search->key == key)
137 } while (search->key);
141 /** Check whether the table contains a value for the given key. */
142 bool contains(_Key key) const {
143 struct hashlistnode<_Key, _Val> *search;
145 unsigned int index = ((_KeyInt)key) >> _Shift;
147 index &= capacitymask;
148 search = &table[index];
149 if (search->key == key)
152 } while (search->key);
156 /** Resize the table. */
157 void resize(unsigned int newsize) {
158 struct hashlistnode<_Key, _Val> *oldtable = table;
159 struct hashlistnode<_Key, _Val> *newtable;
160 unsigned int oldcapacity = capacity;
162 if ((newtable = (struct hashlistnode<_Key, _Val> *)_calloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
163 model_print("calloc error %s %d\n", __FILE__, __LINE__);
167 table = newtable; // Update the global hashtable upon resize()
169 capacitymask = newsize - 1;
171 threshold = (unsigned int)(newsize * loadfactor);
173 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
174 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
175 for (; bin < lastbin; bin++) {
178 struct hashlistnode<_Key, _Val> *search;
180 unsigned int index = ((_KeyInt)key) >> _Shift;
182 index &= capacitymask;
183 search = &table[index];
185 } while (search->key);
188 search->val = bin->val;
191 _free(oldtable); // Free the memory of the old hash table
195 struct hashlistnode<_Key, _Val> *table;
196 unsigned int capacity;
198 unsigned int capacitymask;
199 unsigned int threshold;