2 * @brief Hashtable. Standard chained bucket variety.
11 template<typename _Key, typename _Val>
15 struct hashlistnode<_Key,_Val> *next;
18 /** Hashtable class. By default it is snapshotting, but you can pass
19 in your own allocation functions. */
21 template<typename _Key, typename _Val, typename _KeyInt, int _Shift, void * (* _malloc)(size_t)=malloc, void * (* _calloc)(size_t, size_t)=calloc, void (*_free)(void *)=free>
24 HashTable(unsigned int initialcapacity=1024, double factor=0.5) {
25 // Allocate space for the hash table
26 table = (struct hashlistnode<_Key,_Val> **) _calloc(initialcapacity, sizeof(struct hashlistnode<_Key,_Val> *));
28 capacity = initialcapacity;
29 threshold = (unsigned int) (initialcapacity*loadfactor);
30 mask = (capacity << _Shift)-1;
31 size = 0; // Initial number of elements in the hash
35 for(unsigned int i=0;i<capacity;i++) {
36 struct hashlistnode<_Key,_Val> * bin = table[i];
38 struct hashlistnode<_Key,_Val> * next=bin->next;
46 void * operator new(size_t size) {
50 void operator delete(void *p, size_t size) {
54 void * operator new[](size_t size) {
58 void operator delete[](void *p, size_t size) {\
62 /** Reset the table to its initial state. */
64 for(int i=0;i<capacity;i++) {
65 struct hashlistnode<_Key,_Val> * bin = table[i];
67 struct hashlistnode<_Key,_Val> * next=bin->next;
72 memset(table, 0, capacity*sizeof(struct hashlistnode<_Key, _Val> *));
76 /** Put a key value pair into the table. */
77 void put(_Key key, _Val val) {
78 if(size > threshold) {
80 unsigned int newsize = capacity << 1;
84 struct hashlistnode<_Key,_Val> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
86 struct hashlistnode<_Key,_Val> *search = ptr;
89 if (search->key==key) {
96 struct hashlistnode<_Key,_Val> *newptr=(struct hashlistnode<_Key,_Val> *)_malloc(sizeof(struct hashlistnode<_Key,_Val>));
100 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
103 /** Lookup the corresponding value for the given key. */
105 struct hashlistnode<_Key,_Val> *search = table[(((_KeyInt)key) & mask)>>_Shift];
107 while(search!=NULL) {
108 if (search->key==key) {
116 /** Check whether the table contains a value for the given key. */
117 bool contains(_Key key) {
118 struct hashlistnode<_Key,_Val> *search = table[(((_KeyInt)key) & mask)>>_Shift];
120 while(search!=NULL) {
121 if (search->key==key) {
129 /** Resize the table. */
130 void resize(unsigned int newsize) {
131 struct hashlistnode<_Key,_Val> ** oldtable = table;
132 struct hashlistnode<_Key,_Val> ** newtable;
133 unsigned int oldcapacity = capacity;
135 if((newtable = (struct hashlistnode<_Key,_Val> **) _calloc(newsize, sizeof(struct hashlistnode<_Key,_Val> *))) == NULL) {
136 printf("Calloc error %s %d\n", __FILE__, __LINE__);
140 table = newtable; //Update the global hashtable upon resize()
142 threshold = (unsigned int) (newsize * loadfactor);
143 mask = (newsize << _Shift)-1;
145 for(unsigned int i = 0; i < oldcapacity; i++) {
146 struct hashlistnode<_Key, _Val> * bin = oldtable[i];
150 struct hashlistnode<_Key, _Val> * next=bin->next;
152 unsigned int index = (((_KeyInt)key) & mask) >>_Shift;
153 struct hashlistnode<_Key, _Val> * tmp=newtable[index];
160 _free(oldtable); //Free the memory of the old hash table
164 struct hashlistnode<_Key,_Val> **table;
165 unsigned int capacity;
168 unsigned int threshold;