ref: eeb8989f663c7c2f7ed7e5f92e38bced908cf805
parent: ce10ff586027ea9dcac0f074b16a23f9888cc6df
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jun 13 16:01:33 EDT 2018
Reuse the old key when the old keyval exists. This makes it easier to not leak memory when inserting the same key repeatedly.
--- a/lib/std/htab.myr
+++ b/lib/std/htab.myr
@@ -131,23 +131,25 @@
generic htput = {ht, k, v
var i, di
var h
- var neltincr
+ var found
di = 0
h = xhash(k)
i = h & (ht.keys.len - 1)
- neltincr = 1
+ found = false
while ht.hashes[i] != 0 && !ht.dead[i]
if ht.hashes[i] == h && eq(ht.keys[i], k)
- neltincr = 0
+ found = true
break
;;
di++
i = (h + di) & (ht.keys.len - 1)
;;
- ht.nelt += neltincr
+ if !found
+ ht.keys[i] = k
+ ht.nelt++
+ ;;
ht.hashes[i] = h
- ht.keys[i] = k
ht.vals[i] = v
ht.dead[i] = false
if ht.keys.len < ht.nelt * 2