diff --git a/src/types/hashMap.nim b/src/types/hashMap.nim index 80f63ef..07d3bbc 100755 --- a/src/types/hashMap.nim +++ b/src/types/hashMap.nim @@ -87,13 +87,24 @@ proc findEntry[K, V](self: ptr UncheckedArray[ptr Entry[K, V]], key: K, capacity var idx = uint64(key.hash()) mod capacity while true: result = self[idx] - if result.key.isNone() or result.tombstone: - # If we got here, we either found an - # empty bucket or a tombstone. In both cases, - # we're done so we just make sure to reset - # the tombstone field of the entry and just - # exit the loop + if result.key.isNone(): + # We found an empty bucket break + elif result.tombstone: + # We found a previously deleted + # entry. In this case, we need + # to make sure the tombstone + # will get overwritten when the + # user wants to add a new value + # that would replace it BUT also + # for it to not stop our linear + # probe sequence. Hence, if the + # key of the tombstone isn't + # the same as the one we're looking + # for, we break out of the loop, otherwise + # we keep probing + if result.key == key: + break elif result.key.get() == key: # This if will never error out because if # an entry is a tombstone, its values are diff --git a/src/types/simpleHashMap.nim b/src/types/simpleHashMap.nim index 7b1a852..3bd1ca7 100755 --- a/src/types/simpleHashMap.nim +++ b/src/types/simpleHashMap.nim @@ -58,8 +58,11 @@ proc findEntry(self: ptr UncheckedArray[ptr SimpleEntry], key: ptr Obj, capacity var idx = uint64(key.hash()) mod capacity while true: result = self[idx] - if system.`==`(result.key, nil) or result.tombstone: + if system.`==`(result.key, nil): break + elif result.tombstone: + if result.key == key: + break elif result.key == key: break idx = (idx + 1) mod capacity @@ -166,4 +169,4 @@ proc `$`*(self: ptr SimpleHashMap): string = if i < self.len() - 1: result &= ", " i += 1 - result &= "}" + result &= "}" \ No newline at end of file