#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include "mymemory.h"
#include "common.h"
+#include "mymemory.h"
/**
* @brief Hashtable node
*
* @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:
/**
* @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)
}
if (search->hashcode == hashcode)
if (equals(search->key, key)) {
+ _Val oldval = search->val;
search->val = val;
- return;
+ return oldval;
}
index++;
} while (true);
search->val = val;
search->hashcode = hashcode;
size++;
+ return (_Val) 0;
}
/**