edits
[iotcloud.git] / version2 / src / C / hashtable.h
index b27dde245c6a6acdf4a48184cfa4539ca2bf20bd..c642804f99433c1ab221ae6c220c2345459d2346 100644 (file)
@@ -57,7 +57,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, 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;
        }
 
        /**