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(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) {
135 unsigned int newsize = capacity << 1;
139 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
141 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
143 while(search!=NULL) {
144 if (search->key==key) {
151 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val,_malloc,_calloc,_free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
155 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
158 /** Put a key entry into the table. */
159 _Val * ensureptr(_Key key) {
160 if (size > threshold) {
162 unsigned int newsize = capacity << 1;
166 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
168 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
170 while(search!=NULL) {
171 if (search->key==key) {
177 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
180 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
184 /** Lookup the corresponding value for the given key. */
186 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
188 while(search!=NULL) {
189 if (search->key==key) {
197 /** Lookup the corresponding value for the given key. */
198 _Val * getptr(_Key key) {
199 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
201 while(search!=NULL) {
202 if (search->key==key) {
203 return & search->val;
207 return (_Val *) NULL;
210 /** Check whether the table contains a value for the given key. */
211 bool contains(_Key key) {
212 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
214 while(search!=NULL) {
215 if (search->key==key) {
223 /** Resize the table. */
224 void resize(unsigned int newsize) {
225 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** oldtable = table;
226 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** newtable;
227 unsigned int oldcapacity = capacity;
229 if((newtable = (struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **) _calloc(newsize, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *))) == NULL) {
230 printf("Calloc error %s %d\n", __FILE__, __LINE__);
234 table = newtable; //Update the global hashtable upon resize()
236 threshold = (unsigned int) (newsize * loadfactor);
237 mask = (newsize << _Shift)-1;
239 for(unsigned int i = 0; i < oldcapacity; i++) {
240 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * bin = oldtable[i];
244 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * next=bin->next;
246 unsigned int index = (((_KeyInt)key) & mask) >>_Shift;
247 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * tmp=newtable[index];
254 _free(oldtable); //Free the memory of the old hash table
258 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **table;
259 unsigned int capacity;
262 unsigned int threshold;