edits
[iotcloud.git] / version2 / src / C / hashtable.h
index 3ded5b5993f7b9568501f874bcf08fa6ba8cc31f..91d1e5a465c9358364e547a7da55c1ee2b01645a 100644 (file)
@@ -31,8 +31,8 @@ struct Hashlistnode {
        _Key key;
        _Val val;
        uint hashcode;
-       struct Hashlistnode<_Key, _Val> * next;
-       struct Hashlistnode<_Key, _Val> * prev;
+       struct Hashlistnode<_Key, _Val> *next;
+       struct Hashlistnode<_Key, _Val> *prev;
 };
 
 template<typename _Key, int _Shift, typename _KeyInt>
@@ -59,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:
@@ -82,6 +83,17 @@ public:
                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);
@@ -302,16 +314,16 @@ public:
                                return (_Val)0;
                        } else {
                                _Val v = zero->val;
-                               if (zero -> next != NULL)
-                                       zero -> next -> prev = zero ->prev;
+                               if (zero->next != NULL)
+                                       zero->next->prev = zero->prev;
                                else
-                                       tail = zero -> prev;
+                                       tail = zero->prev;
 
-                               if (zero -> prev != NULL)
-                                       zero -> prev -> next = zero -> next;
+                               if (zero->prev != NULL)
+                                       zero->prev->next = zero->next;
                                else
                                        list = zero->next;
-                                       
+
                                ourfree(zero);
                                zero = NULL;
                                Size--;
@@ -335,17 +347,17 @@ public:
                                        //empty out this bin
                                        search->val = (_Val) 1;
                                        search->key = 0;
-                                       
-                                       if (search -> next != NULL)
-                                               search -> next -> prev = search ->prev;
+
+                                       if (search->next != NULL)
+                                               search->next->prev = search->prev;
                                        else
-                                               tail = search -> prev;
-                                       
-                                       if (search -> prev != NULL)
-                                               search -> prev -> next = search -> next;
+                                               tail = search->prev;
+
+                                       if (search->prev != NULL)
+                                               search->prev->next = search->next;
                                        else
                                                list = search->next;
-                                       
+
                                        Size--;
                                        return v;
                                }
@@ -428,9 +440,9 @@ public:
 
                        if (tail == NULL)
                                tail = search;
-                       search -> next = list;
+                       search->next = list;
                        if (list != NULL)
-                               list -> prev = search;
+                               list->prev = search;
                        list = search;
                        search->hashcode = hashcode;
                        search->key = key;
@@ -443,9 +455,9 @@ 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;
+       struct Hashlistnode<_Key, _Val> *list;
+       struct Hashlistnode<_Key, _Val> *tail;
+       unsigned int capacity;
        unsigned int Size;
 private:
        unsigned int capacitymask;