2 * @brief Hashtable. Standard chained bucket variety.
11 template<typename _Key, typename _Val, void * (* _malloc)(size_t), void * (* _calloc)(size_t, size_t), void (*_free)(void *)>
15 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *next;
17 void * operator new(size_t size) {
21 void operator delete(void *p, size_t size) {
25 void * operator new[](size_t size) {
29 void operator delete[](void *p, size_t size) {
34 /** Hashtable class. By default it is snapshotting, but you can pass
35 in your own allocation functions. */
37 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>
40 HashTable(unsigned int initialcapacity=1024, double factor=0.5) {
41 // Allocate space for the hash table
42 table = (struct hashlistnode<_Key,_Val, _malloc, _calloc,_free> **) _calloc(initialcapacity, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *));
44 capacity = initialcapacity;
45 threshold = (unsigned int) (initialcapacity*loadfactor);
46 mask = (capacity << _Shift)-1;
47 size = 0; // Initial number of elements in the hash
51 for(unsigned int i=0;i<capacity;i++) {
52 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
54 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
62 void * operator new(size_t size) {
66 void operator delete(void *p, size_t size) {
70 void * operator new[](size_t size) {
74 void operator delete[](void *p, size_t size) {
78 /** Reset the table to its initial state. */
80 for(int i=0;i<capacity;i++) {
81 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
83 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
88 memset(table, 0, capacity*sizeof(struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> *));
92 /** Put a key value pair into the table. */
93 void put(_Key key, _Val val) {
94 if(size > threshold) {
96 unsigned int newsize = capacity << 1;
100 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
102 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
104 while(search!=NULL) {
105 if (search->key==key) {
112 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val,_malloc,_calloc,_free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
116 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
119 /** Put a key entry into the table. */
120 _Val * ensureptr(_Key key) {
121 if(size > threshold) {
123 unsigned int newsize = capacity << 1;
127 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
129 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
131 while(search!=NULL) {
132 if (search->key==key) {
138 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
141 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
145 /** Lookup the corresponding value for the given key. */
147 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
149 while(search!=NULL) {
150 if (search->key==key) {
158 /** Lookup the corresponding value for the given key. */
159 _Val * getptr(_Key key) {
160 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
162 while(search!=NULL) {
163 if (search->key==key) {
164 return & search->val;
168 return (_Val *) NULL;
171 /** Check whether the table contains a value for the given key. */
172 bool contains(_Key key) {
173 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
175 while(search!=NULL) {
176 if (search->key==key) {
184 /** Resize the table. */
185 void resize(unsigned int newsize) {
186 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** oldtable = table;
187 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** newtable;
188 unsigned int oldcapacity = capacity;
190 if((newtable = (struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **) _calloc(newsize, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *))) == NULL) {
191 printf("Calloc error %s %d\n", __FILE__, __LINE__);
195 table = newtable; //Update the global hashtable upon resize()
197 threshold = (unsigned int) (newsize * loadfactor);
198 mask = (newsize << _Shift)-1;
200 for(unsigned int i = 0; i < oldcapacity; i++) {
201 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * bin = oldtable[i];
205 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * next=bin->next;
207 unsigned int index = (((_KeyInt)key) & mask) >>_Shift;
208 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * tmp=newtable[index];
215 _free(oldtable); //Free the memory of the old hash table
219 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **table;
220 unsigned int capacity;
223 unsigned int threshold;