ref: 6380f0c2b7a4c6e950c5fbced2fd24749850bea5
parent: fb8e2abee34bebf7f8d1d0b2754894b838414bee
author: Ori Bernstein <[email protected]>
date: Fri Oct 18 13:17:52 EDT 2013
Add htkeys() function
--- a/libstd/htab.myr
+++ b/libstd/htab.myr
@@ -18,6 +18,7 @@
generic htfree : (ht : htab(@k, @v)# -> void)
generic htput : (ht : htab(@k, @v)#, k : @k, v : @v -> void)
generic htget : (ht : htab(@k, @v)#, k : @k -> option(@v))
+ generic htkeys : (ht : htab(@k, @v)# -> @k[:])
;;
const Initsz = 32
@@ -138,8 +139,13 @@
generic htdel = {ht, k
match idx(ht, k)
- `Some i: ht.dead[i] = true;;
- _: /* do nothing */;;
+ `Some i:
+ ht.dead[i] = true
+ ht.nelt--
+ ;;
+ _:
+ /* do nothing */
+ ;;
;;
}
@@ -156,3 +162,18 @@
`None: -> false;;
;;
}
+
+generic htkeys = {ht
+ var keys
+ var i
+ var j
+
+ keys = slalloc(ht.nelt)
+ for i = 0; i < ht.keys.len; i++
+ if ht.hashes[i] != 0 && !ht.dead[i]
+ keys[j++] = ht.keys[i]
+ ;;
+ ;;
+ -> keys
+}
+