edits
[iotcloud.git] / version2 / src / C / hashtable.h
index 0a50bb9f39511aa4beba010c6a56f11ae73149b0..c642804f99433c1ab221ae6c220c2345459d2346 100644 (file)
@@ -17,8 +17,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include "mymemory.h"
 #include "common.h"
+#include "mymemory.h"
 
 /**
  * @brief Hashtable node
@@ -53,11 +53,11 @@ inline bool defaultEquals(_Key key1, _Key key2) {
  *
  * @tparam _Key    Type name for the key
  * @tparam _Val    Type name for the values to be stored
- * @tparam _KeyInt Integer type that is at least as large as _Key. Used for key
+ * @tparam _KeyInt int32_t type that is at least as large as _Key. Used for key
  *                 manipulation and storage.
  * @tparam _Shift  Logical shift to apply to all keys. Default 0.
  */
-template<typename _Key, typename _Val, typename _KeyInt, int _Shift = 0, unsigned int (*hash_function) (_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals) (_Key, _Key) = defaultEquals<_Key> >
+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:
        /**
@@ -190,16 +190,19 @@ public:
         * @param key The key for the new value; must not be 0 or NULL
         * @param val The value to store in the table
         */
-       void put(_Key key, _Val val) {
+       _Val put(_Key key, _Val val) {
                /* Hashtable cannot handle 0 as a key */
                if (!key) {
+                       _Val oldval;
                        if (!zero) {
                                zero = (struct Hashlistnode<_Key, _Val> *)ourmalloc(sizeof(struct Hashlistnode<_Key, _Val>));
                                size++;
-                       }
+                               oldval = (_Val) 0;
+                       } else
+                               oldval = zero->val;
                        zero->key = key;
                        zero->val = val;
-                       return;
+                       return oldval;
                }
 
                if (size > threshold)
@@ -218,8 +221,9 @@ public:
                        }
                        if (search->hashcode == hashcode)
                                if (equals(search->key, key)) {
+                                       _Val oldval = search->val;
                                        search->val = val;
-                                       return;
+                                       return oldval;
                                }
                        index++;
                } while (true);
@@ -228,6 +232,7 @@ public:
                search->val = val;
                search->hashcode = hashcode;
                size++;
+               return (_Val) 0;
        }
 
        /**