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) {
35 * Hashtable class. By default it is snapshotting, but you can pass in your
36 * own allocation functions.
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>
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> *));
45 capacity = initialcapacity;
46 threshold = (unsigned int) (initialcapacity*loadfactor);
47 mask = (capacity << _Shift)-1;
48 size = 0; // Initial number of elements in the hash
52 for(unsigned int i=0;i<capacity;i++) {
53 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
55 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
63 void * operator new(size_t size) {
67 void operator delete(void *p, size_t size) {
71 void * operator new[](size_t size) {
75 void operator delete[](void *p, size_t size) {
79 /** Reset the table to its initial state. */
81 for(int i=0;i<capacity;i++) {
82 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
84 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
89 memset(table, 0, capacity*sizeof(struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> *));
93 /** Put a key value pair into the table. */
94 void put(_Key key, _Val val) {
95 if(size > threshold) {
97 unsigned int newsize = capacity << 1;
101 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
103 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
105 while(search!=NULL) {
106 if (search->key==key) {
113 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val,_malloc,_calloc,_free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
117 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
120 /** Put a key entry into the table. */
121 _Val * ensureptr(_Key key) {
122 if (size > threshold) {
124 unsigned int newsize = capacity << 1;
128 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
130 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
132 while(search!=NULL) {
133 if (search->key==key) {
139 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
142 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
146 /** Lookup the corresponding value for the given key. */
148 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
150 while(search!=NULL) {
151 if (search->key==key) {
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];
163 while(search!=NULL) {
164 if (search->key==key) {
165 return & search->val;
169 return (_Val *) NULL;
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];
176 while(search!=NULL) {
177 if (search->key==key) {
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;
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__);
196 table = newtable; //Update the global hashtable upon resize()
198 threshold = (unsigned int) (newsize * loadfactor);
199 mask = (newsize << _Shift)-1;
201 for(unsigned int i = 0; i < oldcapacity; i++) {
202 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * bin = oldtable[i];
206 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * next=bin->next;
208 unsigned int index = (((_KeyInt)key) & mask) >>_Shift;
209 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * tmp=newtable[index];
216 _free(oldtable); //Free the memory of the old hash table
220 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **table;
221 unsigned int capacity;
224 unsigned int threshold;