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 = 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 = index&capacitymask;
133 search = &table[index];
134 if (search->key == key) {
138 } while (search->key);
142 /** Check whether the table contains a value for the given key. */
143 bool contains(_Key key) const {
144 struct hashlistnode<_Key, _Val> *search;
146 unsigned int index = ((_KeyInt)key) >> _Shift;
148 index = index & capacitymask;
149 search = &table[index];
150 if (search->key == key) {
154 } while (search->key);
158 /** Resize the table. */
159 void resize(unsigned int newsize) {
160 struct hashlistnode<_Key, _Val> *oldtable = table;
161 struct hashlistnode<_Key, _Val> *newtable;
162 unsigned int oldcapacity = capacity;
164 if ((newtable = (struct hashlistnode<_Key, _Val> *) _calloc(newsize, sizeof(struct hashlistnode<_Key, _Val>))) == NULL) {
165 model_print("Calloc error %s %d\n", __FILE__, __LINE__);
169 table = newtable; //Update the global hashtable upon resize()
171 capacitymask = newsize - 1;
173 threshold = (unsigned int) (newsize * loadfactor);
175 struct hashlistnode<_Key, _Val> *bin = &oldtable[0];
176 struct hashlistnode<_Key, _Val> *lastbin = &oldtable[oldcapacity];
177 for (; bin < lastbin; bin++) {
180 struct hashlistnode<_Key, _Val> *search;
182 unsigned int index = ((_KeyInt)key) >> _Shift;
184 index = index & capacitymask;
185 search = &table[index];
187 } while (search->key);
190 search->val = bin->val;
193 _free(oldtable); //Free the memory of the old hash table
197 struct hashlistnode<_Key, _Val> *table;
198 unsigned int capacity;
200 unsigned int capacitymask;
201 unsigned int threshold;