more reformatting/indentation
[model-checker.git] / hashtable.h
1 /** @file hashtable.h
2  *  @brief Hashtable.  Standard chained bucket variety.
3  */
4
5 #ifndef HASHTABLE_H
6 #define HASHTABLE_H
7
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 template<typename _Key, typename _Val, void * (* _malloc)(size_t), void * (* _calloc)(size_t, size_t), void (*_free)(void *)>
12 struct hashlistnode {
13         _Key key;
14         _Val val;
15         struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *next;
16
17         void * operator new(size_t size) {
18                 return _malloc(size);
19         }
20
21         void operator delete(void *p, size_t size) {
22                 _free(p);
23         }
24
25         void * operator new[](size_t size) {
26                 return _malloc(size);
27         }
28
29         void operator delete[](void *p, size_t size) {
30                 _free(p);
31         }
32 };
33
34 /**
35  * Hashtable class.  By default it is snapshotting, but you can pass in your
36  * own allocation functions.
37  */
38 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>
39         class HashTable {
40  public:
41         HashTable(unsigned int initialcapacity=1024, double factor=0.5) {
42                 // Allocate space for the hash table
43                 table = (struct hashlistnode<_Key,_Val, _malloc, _calloc,_free> **) _calloc(initialcapacity, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *));
44                 loadfactor = factor;
45                 capacity = initialcapacity;
46                 threshold = (unsigned int) (initialcapacity*loadfactor);
47                 mask = (capacity << _Shift)-1;
48                 size = 0; // Initial number of elements in the hash
49         }
50
51         ~HashTable() {
52                 for(unsigned int i=0;i<capacity;i++) {
53                         struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
54                         while(bin!=NULL) {
55                                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
56                                 delete bin;
57                                 bin=next;
58                         }
59                 }
60                 _free(table);
61         }
62
63         void * operator new(size_t size) {
64                 return _malloc(size);
65         }
66
67         void operator delete(void *p, size_t size) {
68                 _free(p);
69         }
70
71         void * operator new[](size_t size) {
72                 return _malloc(size);
73         }
74
75         void operator delete[](void *p, size_t size) {
76                 _free(p);
77         }
78
79         /** Reset the table to its initial state. */
80         void reset() {
81                 for(int i=0;i<capacity;i++) {
82                         struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
83                         while(bin!=NULL) {
84                                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
85                                 delete bin;
86                                 bin=next;
87                         }
88                 }
89                 memset(table, 0, capacity*sizeof(struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> *));
90                 size=0;
91         }
92
93         /** Put a key value pair into the table. */
94         void put(_Key key, _Val val) {
95                 if(size > threshold) {
96                         //Resize
97                         unsigned int newsize = capacity << 1;
98                         resize(newsize);
99                 }
100
101                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
102                 size++;
103                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
104
105                 while(search!=NULL) {
106                         if (search->key==key) {
107                                 search->val=val;
108                                 return;
109                         }
110                         search=search->next;
111                 }
112
113                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val,_malloc,_calloc,_free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
114                 newptr->key=key;
115                 newptr->val=val;
116                 newptr->next=ptr;
117                 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
118         }
119
120         /** Put a key entry into the table. */
121         _Val * ensureptr(_Key key) {
122                 if (size > threshold) {
123                         //Resize
124                         unsigned int newsize = capacity << 1;
125                         resize(newsize);
126                 }
127
128                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
129                 size++;
130                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
131
132                 while(search!=NULL) {
133                         if (search->key==key) {
134                                 return &search->val;
135                         }
136                         search=search->next;
137                 }
138
139                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
140                 newptr->key=key;
141                 newptr->next=ptr;
142                 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
143                 return &newptr->val;
144         }
145
146         /** Lookup the corresponding value for the given key. */
147         _Val get(_Key key) {
148                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
149
150                 while(search!=NULL) {
151                         if (search->key==key) {
152                                 return search->val;
153                         }
154                         search=search->next;
155                 }
156                 return (_Val)0;
157         }
158
159         /** Lookup the corresponding value for the given key. */
160         _Val * getptr(_Key key) {
161                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
162
163                 while(search!=NULL) {
164                         if (search->key==key) {
165                                 return & search->val;
166                         }
167                         search=search->next;
168                 }
169                 return (_Val *) NULL;
170         }
171
172         /** Check whether the table contains a value for the given key. */
173         bool contains(_Key key) {
174                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
175
176                 while(search!=NULL) {
177                         if (search->key==key) {
178                                 return true;
179                         }
180                         search=search->next;
181                 }
182                 return false;
183         }
184
185         /** Resize the table. */
186         void resize(unsigned int newsize) {
187                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** oldtable = table;
188                 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** newtable;
189                 unsigned int oldcapacity = capacity;
190
191                 if((newtable = (struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **) _calloc(newsize, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *))) == NULL) {
192                         printf("Calloc error %s %d\n", __FILE__, __LINE__);
193                         exit(-1);
194                 }
195
196                 table = newtable;          //Update the global hashtable upon resize()
197                 capacity = newsize;
198                 threshold = (unsigned int) (newsize * loadfactor);
199                 mask = (newsize << _Shift)-1;
200
201                 for(unsigned int i = 0; i < oldcapacity; i++) {
202                         struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * bin = oldtable[i];
203
204                         while(bin!=NULL) {
205                                 _Key key=bin->key;
206                                 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * next=bin->next;
207
208                                 unsigned int index = (((_KeyInt)key) & mask) >>_Shift;
209                                 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * tmp=newtable[index];
210                                 bin->next=tmp;
211                                 newtable[index]=bin;
212                                 bin = next;
213                         }
214                 }
215
216                 _free(oldtable);            //Free the memory of the old hash table
217         }
218
219  private:
220         struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **table;
221         unsigned int capacity;
222         _KeyInt mask;
223         unsigned int size;
224         unsigned int threshold;
225         double loadfactor;
226 };
227 #endif