2 * @brief Hashtable. Standard chained bucket variety.
12 * Hashtable linked node class, for chained storage of hash table conflicts. By
13 * default it is snapshotting, but you can pass in your own allocation
16 * @tparam _Key Type name for the key
17 * @tparam _Val Type name for the values to be stored
18 * @tparam _malloc Provide your own 'malloc' for the table, or default to
20 * @tparam _calloc Provide your own 'calloc' for the table, or default to
22 * @tparam _free Provide your own 'free' for the table, or default to
25 template<typename _Key, typename _Val, void * (* _malloc)(size_t), void * (* _calloc)(size_t, size_t), void (*_free)(void *)>
29 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *next;
31 void * operator new(size_t size) {
35 void operator delete(void *p, size_t size) {
39 void * operator new[](size_t size) {
43 void operator delete[](void *p, size_t size) {
49 * Hashtable class. By default it is snapshotting, but you can pass in your own
50 * allocation functions.
52 * @tparam _Key Type name for the key
53 * @tparam _Val Type name for the values to be stored
54 * @tparam _KeyInt Integer type that is at least as large as _Key. Used for key
55 * manipulation and storage.
56 * @tparam _Shift Logical shift to apply to all keys. Default 0.
57 * @tparam _malloc Provide your own 'malloc' for the table, or default to
59 * @tparam _calloc Provide your own 'calloc' for the table, or default to
61 * @tparam _free Provide your own 'free' for the table, or default to
64 template<typename _Key, typename _Val, typename _KeyInt, int _Shift=0, void * (* _malloc)(size_t)=malloc, void * (* _calloc)(size_t, size_t)=calloc, void (*_free)(void *)=free>
69 * @param initialcapacity Sets the initial capacity of the hash table.
71 * @param factor Sets the percentage full before the hashtable is
72 * resized. Default ratio 0.5.
74 HashTable(unsigned int initialcapacity=1024, double factor=0.5) {
75 // Allocate space for the hash table
76 table = (struct hashlistnode<_Key,_Val, _malloc, _calloc,_free> **) _calloc(initialcapacity, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *));
78 capacity = initialcapacity;
79 threshold = (unsigned int) (initialcapacity*loadfactor);
80 mask = (capacity << _Shift)-1;
81 size = 0; // Initial number of elements in the hash
86 for(unsigned int i=0;i<capacity;i++) {
87 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
89 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
97 /** Override: new operator */
98 void * operator new(size_t size) {
102 /** Override: delete operator */
103 void operator delete(void *p, size_t size) {
107 /** Override: new[] operator */
108 void * operator new[](size_t size) {
109 return _malloc(size);
112 /** Override: delete[] operator */
113 void operator delete[](void *p, size_t size) {
117 /** Reset the table to its initial state. */
119 for(unsigned int i=0;i<capacity;i++) {
120 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
122 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
127 memset(table, 0, capacity*sizeof(struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> *));
131 /** Put a key value pair into the table. */
132 void put(_Key key, _Val val) {
133 if (size > threshold)
134 resize(capacity << 1);
136 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
137 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
139 while(search!=NULL) {
140 if (search->key==key) {
147 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val,_malloc,_calloc,_free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
151 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
156 * @brief Get a valid pointer to a value corresponding to a given key
158 * Ensure that key is present in the hash table, then return a pointer
159 * to its value bin. This may require either creating a new bin for
160 * this key (with a default-constructed value) or simply locating and
161 * returning a pointer to an existing value.
162 * @param key The key to check
163 * @return A pointer to the value in the table
165 _Val * get_safe_ptr(_Key key) {
166 if (size > threshold)
167 resize(capacity << 1);
169 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
170 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
172 while(search!=NULL) {
173 if (search->key==key) {
179 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
182 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
187 /** Lookup the corresponding value for the given key. */
189 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
191 while(search!=NULL) {
192 if (search->key==key) {
200 /** Lookup the corresponding value for the given key. */
201 _Val * getptr(_Key key) {
202 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
204 while(search!=NULL) {
205 if (search->key==key) {
206 return & search->val;
210 return (_Val *) NULL;
213 /** Check whether the table contains a value for the given key. */
214 bool contains(_Key key) {
215 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
217 while(search!=NULL) {
218 if (search->key==key) {
226 /** Resize the table. */
227 void resize(unsigned int newsize) {
228 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** oldtable = table;
229 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** newtable;
230 unsigned int oldcapacity = capacity;
232 if((newtable = (struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **) _calloc(newsize, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *))) == NULL) {
233 printf("Calloc error %s %d\n", __FILE__, __LINE__);
237 table = newtable; //Update the global hashtable upon resize()
239 threshold = (unsigned int) (newsize * loadfactor);
240 mask = (newsize << _Shift)-1;
242 for(unsigned int i = 0; i < oldcapacity; i++) {
243 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * bin = oldtable[i];
247 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * next=bin->next;
249 unsigned int index = (((_KeyInt)key) & mask) >>_Shift;
250 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * tmp=newtable[index];
257 _free(oldtable); //Free the memory of the old hash table
261 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **table;
262 unsigned int capacity;
265 unsigned int threshold;