summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-06-13 20:01:33 -0700
committerOri Bernstein <ori@markovcorp.com>2018-06-15 11:38:49 -0700
commiteeb8989f663c7c2f7ed7e5f92e38bced908cf805 (patch)
treeb9f45a6ad884a3c2c94291bbb0905277f1188e20
parentce10ff586027ea9dcac0f074b16a23f9888cc6df (diff)
downloadmc-eeb8989f663c7c2f7ed7e5f92e38bced908cf805.tar.gz
Reuse the old key when the old keyval exists.
This makes it easier to not leak memory when inserting the same key repeatedly.
-rw-r--r--lib/std/htab.myr12
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/std/htab.myr b/lib/std/htab.myr
index 771f47f..9c00da5 100644
--- a/lib/std/htab.myr
+++ b/lib/std/htab.myr
@@ -131,23 +131,25 @@ generic htfree = {ht
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