X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=hashtable.h;h=81772a8f039addae09c726c0f7bf3e37ab626ef1;hb=f4917b6ec74b4c36c19ad7dc92fe888a94357486;hp=f71203887a6c871fbae10c272d4915b488680f38;hpb=2fb6087b4a961be34e5bc69e1eee770b0f7439a8;p=c11tester.git diff --git a/hashtable.h b/hashtable.h index f7120388..81772a8f 100644 --- a/hashtable.h +++ b/hashtable.h @@ -62,7 +62,7 @@ inline bool default_equals(_Key key1, _Key key2) { * @tparam _free Provide your own 'free' for the table, or default to * snapshotting. */ -template, bool (*equals)(_Key, _Key) = default_equals<_Key> > +template, bool (*equals)(_Key, _Key) = default_equals<_Key> > class HashTable { public: /** @@ -182,24 +182,38 @@ public: resize(capacity << 1); struct hashlistnode<_Key, _Val> *search; + struct hashlistnode<_Key, _Val> *first = NULL; - unsigned int index = hash_function(key); + unsigned int index = hash_function(key) & capacitymask; + unsigned int oindex = index; do { - index &= capacitymask; search = &table[index]; if (!search->key) { //key is null, probably done - break; + if (!search->val) + break; + if (first == NULL) + first = search; } if (equals(search->key, key)) { search->val = val; return; } - index++; + index = (index + 1) & capacitymask; + if (index == oindex) { + if (first == NULL) + exit(-1); + break; + } } while (true); - search->key = key; - search->val = val; + if (first != NULL) { + first->key = key; + first->val = val; + } else { + search->key = key; + search->val = val; + } size++; } @@ -220,7 +234,7 @@ public: } unsigned int oindex = hash_function(key) & capacitymask; - unsigned int index=oindex; + unsigned int index = oindex; do { search = &table[index]; if (!search->key) { @@ -284,6 +298,9 @@ public: return size; } + bool isEmpty() { + return size == 0; + } /** * @brief Check whether the table contains a value for the given key