2 * @brief Hashtable. Standard chained bucket variety.
13 * Hashtable linked node class, for chained storage of hash table conflicts. By
14 * default it is snapshotting, but you can pass in your own allocation
17 * @tparam _Key Type name for the key
18 * @tparam _Val Type name for the values to be stored
19 * @tparam _malloc Provide your own 'malloc' for the table, or default to
21 * @tparam _calloc Provide your own 'calloc' for the table, or default to
23 * @tparam _free Provide your own 'free' for the table, or default to
26 template<typename _Key, typename _Val, void * (* _malloc)(size_t), void * (* _calloc)(size_t, size_t), void (*_free)(void *)>
30 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *next;
32 void * operator new(size_t size) {
36 void operator delete(void *p, size_t size) {
40 void * operator new[](size_t size) {
44 void operator delete[](void *p, size_t size) {
50 * Hashtable class. By default it is snapshotting, but you can pass in your own
51 * allocation functions.
53 * @tparam _Key Type name for the key
54 * @tparam _Val Type name for the values to be stored
55 * @tparam _KeyInt Integer type that is at least as large as _Key. Used for key
56 * manipulation and storage.
57 * @tparam _Shift Logical shift to apply to all keys. Default 0.
58 * @tparam _malloc Provide your own 'malloc' for the table, or default to
60 * @tparam _calloc Provide your own 'calloc' for the table, or default to
62 * @tparam _free Provide your own 'free' for the table, or default to
65 template<typename _Key, typename _Val, typename _KeyInt, int _Shift=0, void * (* _malloc)(size_t)=snapshot_malloc, void * (* _calloc)(size_t, size_t)=snapshot_calloc, void (*_free)(void *)=snapshot_free>
70 * @param initialcapacity Sets the initial capacity of the hash table.
72 * @param factor Sets the percentage full before the hashtable is
73 * resized. Default ratio 0.5.
75 HashTable(unsigned int initialcapacity=1024, double factor=0.5) {
76 // Allocate space for the hash table
77 table = (struct hashlistnode<_Key,_Val, _malloc, _calloc,_free> **) _calloc(initialcapacity, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *));
79 capacity = initialcapacity;
80 threshold = (unsigned int) (initialcapacity*loadfactor);
81 mask = (capacity << _Shift)-1;
82 size = 0; // Initial number of elements in the hash
87 for(unsigned int i=0;i<capacity;i++) {
88 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
90 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
98 /** Override: new operator */
99 void * operator new(size_t size) {
100 return _malloc(size);
103 /** Override: delete operator */
104 void operator delete(void *p, size_t size) {
108 /** Override: new[] operator */
109 void * operator new[](size_t size) {
110 return _malloc(size);
113 /** Override: delete[] operator */
114 void operator delete[](void *p, size_t size) {
118 /** Reset the table to its initial state. */
120 for(unsigned int i=0;i<capacity;i++) {
121 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * bin = table[i];
123 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> * next=bin->next;
128 memset(table, 0, capacity*sizeof(struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> *));
132 /** Put a key value pair into the table. */
133 void put(_Key key, _Val val) {
134 if (size > threshold)
135 resize(capacity << 1);
137 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
138 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
140 while(search!=NULL) {
141 if (search->key==key) {
148 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val,_malloc,_calloc,_free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
152 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
157 * @brief Get a valid pointer to a value corresponding to a given key
159 * Ensure that key is present in the hash table, then return a pointer
160 * to its value bin. This may require either creating a new bin for
161 * this key (with a default-constructed value) or simply locating and
162 * returning a pointer to an existing value.
163 * @param key The key to check
164 * @return A pointer to the value in the table
166 _Val * get_safe_ptr(_Key key) {
167 if (size > threshold)
168 resize(capacity << 1);
170 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *ptr = table[(((_KeyInt)key) & mask)>>_Shift];
171 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = ptr;
173 while(search!=NULL) {
174 if (search->key==key) {
180 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *newptr=(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *)new struct hashlistnode<_Key,_Val, _malloc, _calloc, _free>;
183 table[(((_KeyInt)key)&mask)>>_Shift]=newptr;
188 /** Lookup the corresponding value for the given key. */
190 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
192 while(search!=NULL) {
193 if (search->key==key) {
201 /** Lookup the corresponding value for the given key. */
202 _Val * getptr(_Key key) {
203 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
205 while(search!=NULL) {
206 if (search->key==key) {
207 return & search->val;
211 return (_Val *) NULL;
214 /** Check whether the table contains a value for the given key. */
215 bool contains(_Key key) {
216 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *search = table[(((_KeyInt)key) & mask)>>_Shift];
218 while(search!=NULL) {
219 if (search->key==key) {
227 /** Resize the table. */
228 void resize(unsigned int newsize) {
229 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** oldtable = table;
230 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> ** newtable;
231 unsigned int oldcapacity = capacity;
233 if((newtable = (struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **) _calloc(newsize, sizeof(struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> *))) == NULL) {
234 printf("Calloc error %s %d\n", __FILE__, __LINE__);
238 table = newtable; //Update the global hashtable upon resize()
240 threshold = (unsigned int) (newsize * loadfactor);
241 mask = (newsize << _Shift)-1;
243 for(unsigned int i = 0; i < oldcapacity; i++) {
244 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * bin = oldtable[i];
248 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * next=bin->next;
250 unsigned int index = (((_KeyInt)key) & mask) >>_Shift;
251 struct hashlistnode<_Key, _Val, _malloc, _calloc, _free> * tmp=newtable[index];
258 _free(oldtable); //Free the memory of the old hash table
262 struct hashlistnode<_Key,_Val, _malloc, _calloc, _free> **table;
263 unsigned int capacity;
266 unsigned int threshold;