2 * @brief Hashtable. Standard chained bucket variety.
13 * Hashtable linked node class, for chained storage of hash table conflicts. By
14 * default it is snapshotting, but you can pass in your own allocation
17 * @tparam _Key Type name for the key
18 * @tparam _Val Type name for the values to be stored
19 * @tparam _malloc Provide your own 'malloc' for the table, or default to
21 * @tparam _calloc Provide your own 'calloc' for the table, or default to
23 * @tparam _free Provide your own 'free' for the table, or default to
26 template<typename _Key, typename _Val>
34 * Hashtable class. By default it is snapshotting, but you can pass in your own
35 * allocation functions.
37 * @tparam _Key Type name for the key
38 * @tparam _Val Type name for the values to be stored
39 * @tparam _KeyInt Integer type that is at least as large as _Key. Used for key
40 * manipulation and storage.
41 * @tparam _Shift Logical shift to apply to all keys. Default 0.
42 * @tparam _malloc Provide your own 'malloc' for the table, or default to
44 * @tparam _calloc Provide your own 'calloc' for the table, or default to
46 * @tparam _free Provide your own 'free' for the table, or default to
49 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>
54 * @param initialcapacity Sets the initial capacity of the hash table.
56 * @param factor Sets the percentage full before the hashtable is
57 * resized. Default ratio 0.5.
59 HashTable(unsigned int initialcapacity=1024, double factor=0.5) {
60 // Allocate space for the hash table
61 table = (struct hashlistnode<_Key,_Val> *) _calloc(initialcapacity, sizeof(struct hashlistnode<_Key,_Val>));
63 capacity = initialcapacity;
64 capacitymask = initialcapacity - 1;
66 threshold = (unsigned int) (initialcapacity*loadfactor);
67 size = 0; // Initial number of elements in the hash
75 /** Override: new operator */
76 void * operator new(size_t size) {
80 /** Override: delete operator */
81 void operator delete(void *p, size_t size) {
85 /** Override: new[] operator */
86 void * operator new[](size_t size) {
90 /** Override: delete[] operator */
91 void operator delete[](void *p, size_t size) {
95 /** Reset the table to its initial state. */
97 memset(table, 0, capacity*sizeof(struct hashlistnode<_Key, _Val>));
101 /** Put a key value pair into the table. */
102 void put(_Key key, _Val val) {
103 if (size > threshold)
104 resize(capacity << 1);
106 struct hashlistnode<_Key,_Val> *search;
108 unsigned int index=((_KeyInt)key)>>_Shift;
110 index=index&capacitymask;
111 search = &table[index];
112 if (search->key==key) {
117 } while(search->key);
124 /** Lookup the corresponding value for the given key. */
126 struct hashlistnode<_Key,_Val> *search;
128 unsigned int index=((_KeyInt)key)>>_Shift;
130 index=index&capacitymask;
131 search = &table[index];
132 if (search->key==key) {
136 } while(search->key);
140 /** Check whether the table contains a value for the given key. */
141 bool contains(_Key key) {
142 struct hashlistnode<_Key,_Val> *search;
144 unsigned int index=((_KeyInt)key)>>_Shift;
146 index=index&capacitymask;
147 search = &table[index];
148 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 printf("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=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;