edits
[iotcloud.git] / version2 / src / C / hashtable.h
index b610154dc87d111cc123a1cf309f538e25657f73..91d1e5a465c9358364e547a7da55c1ee2b01645a 100644 (file)
@@ -31,6 +31,8 @@ struct Hashlistnode {
        _Key key;
        _Val val;
        uint hashcode;
+       struct Hashlistnode<_Key, _Val> *next;
+       struct Hashlistnode<_Key, _Val> *prev;
 };
 
 template<typename _Key, int _Shift, typename _KeyInt>
@@ -57,6 +59,7 @@ inline bool defaultEquals(_Key key1, _Key key2) {
  *                 manipulation and storage.
  * @tparam _Shift  Logical shift to apply to all keys. Default 0.
  */
+
 template<typename _Key, typename _Val, typename _KeyInt = uintptr_t, int _Shift = 0, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
 class Hashtable {
 public:
@@ -77,8 +80,20 @@ public:
 
                threshold = (unsigned int)(initialcapacity * loadfactor);
                Size = 0;                                                       // Initial number of elements in the hash
+               tail = list = NULL;
+       }
+
+       Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> *clone() {
+               Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> *ctable = new Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> (capacity, loadfactor);
+               struct Hashlistnode<_Key, _Val> *ptr = list;
+               while (ptr != NULL) {
+                       ctable->put(ptr->key, ptr->val);
+                       ptr = ptr->next;
+               }
+               return ctable;
        }
 
+
        /** @brief Hash table destructor */
        ~Hashtable() {
                ourfree(table);
@@ -114,6 +129,7 @@ public:
                        zero = NULL;
                }
                Size = 0;
+               tail = list = NULL;
        }
 
        /** Doesn't work with zero value */
@@ -143,6 +159,7 @@ public:
                        zero = NULL;
                }
                Size = 0;
+               tail = list = NULL;
        }
 
        void resetAndDeleteVals() {
@@ -163,6 +180,7 @@ public:
                        zero = NULL;
                }
                Size = 0;
+               tail = list = NULL;
        }
 
        void resetAndFreeVals() {
@@ -183,6 +201,7 @@ public:
                        zero = NULL;
                }
                Size = 0;
+               tail = list = NULL;
        }
 
        /**
@@ -196,6 +215,12 @@ public:
                        _Val oldval;
                        if (!zero) {
                                zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
+                               zero->next = list;
+                               if (list != NULL)
+                                       list->prev = zero;
+                               else
+                                       tail = zero;
+                               list = zero;
                                Size++;
                                oldval = (_Val) 0;
                        } else
@@ -231,6 +256,11 @@ public:
                search->key = key;
                search->val = val;
                search->hashcode = hashcode;
+               search->next = list;
+               if (list == NULL)
+                       tail = search;
+               else
+                       list->prev = search;
                Size++;
                return (_Val) 0;
        }
@@ -279,12 +309,21 @@ public:
        _Val remove(_Key key) {
                struct Hashlistnode<_Key, _Val> *search;
 
-               /* Hashtable cannot handle 0 as a key */
                if (!key) {
                        if (!zero) {
                                return (_Val)0;
                        } else {
                                _Val v = zero->val;
+                               if (zero->next != NULL)
+                                       zero->next->prev = zero->prev;
+                               else
+                                       tail = zero->prev;
+
+                               if (zero->prev != NULL)
+                                       zero->prev->next = zero->next;
+                               else
+                                       list = zero->next;
+
                                ourfree(zero);
                                zero = NULL;
                                Size--;
@@ -308,6 +347,17 @@ public:
                                        //empty out this bin
                                        search->val = (_Val) 1;
                                        search->key = 0;
+
+                                       if (search->next != NULL)
+                                               search->next->prev = search->prev;
+                                       else
+                                               tail = search->prev;
+
+                                       if (search->prev != NULL)
+                                               search->prev->next = search->next;
+                                       else
+                                               list = search->next;
+
                                        Size--;
                                        return v;
                                }
@@ -368,7 +418,7 @@ public:
                table = newtable;                                                                                       // Update the global hashtable upon resize()
                capacity = newsize;
                capacitymask = newsize - 1;
-
+               list = tail = NULL;
                threshold = (unsigned int)(newsize * loadfactor);
 
                struct Hashlistnode<_Key, _Val> *bin = &oldtable[0];
@@ -388,6 +438,12 @@ public:
                                index++;
                        } while (search->key);
 
+                       if (tail == NULL)
+                               tail = search;
+                       search->next = list;
+                       if (list != NULL)
+                               list->prev = search;
+                       list = search;
                        search->hashcode = hashcode;
                        search->key = key;
                        search->val = bin->val;
@@ -399,6 +455,8 @@ public:
        unsigned int getCapacity() {return capacity;}
        struct Hashlistnode<_Key, _Val> *table;
        struct Hashlistnode<_Key, _Val> *zero;
+       struct Hashlistnode<_Key, _Val> *list;
+       struct Hashlistnode<_Key, _Val> *tail;
        unsigned int capacity;
        unsigned int Size;
 private: