summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@markovcorp.com>2017-03-17 17:59:11 -0700
committerOri Bernstein <ori@markovcorp.com>2017-03-17 17:59:11 -0700
commit003c3c95490986905616bd7c79b9ab2a9d65e115 (patch)
tree24a08a44ab82f568c4e0c25e74949f266c1f09a9
parent6f8ceb3bf2f16c16cf566b4dc99b09dd2b538479 (diff)
downloadmc-003c3c95490986905616bd7c79b9ab2a9d65e115.tar.gz
Don't count trailing elements when hashing bitsets.
These don't logically affect the contents, so we shouldn't care what they have.
-rw-r--r--lib/std/bitset.myr8
-rw-r--r--lib/std/test/bitset.myr9
2 files changed, 16 insertions, 1 deletions
diff --git a/lib/std/bitset.myr b/lib/std/bitset.myr
index 0b25729..937e65b 100644
--- a/lib/std/bitset.myr
+++ b/lib/std/bitset.myr
@@ -156,7 +156,13 @@ const bseq = {a, b
}
const bshash = {a
- -> std.slhash(a.bits)
+ var end : size
+
+ end = a.bits.len
+ while end > 0 && a.bits[end - 1] == 0
+ end--
+ ;;
+ -> std.slhash(a.bits[:end])
}
const ensurelen = {bs, len
diff --git a/lib/std/test/bitset.myr b/lib/std/test/bitset.myr
index d6c8a8f..0415be8 100644
--- a/lib/std/test/bitset.myr
+++ b/lib/std/test/bitset.myr
@@ -46,6 +46,15 @@ const main = {
var bs = mkset([0, 63, 64, 65, 73, 127, 128, 129][:])
testr.check(ctx, std.bscount(bs) == 8, "wrong element count, expected {}", std.bscount(bs))
std.bsfree(bs)
+ }],
+ [.name="hash", .fn={ctx
+ var bs = mkset([][:])
+ testr.check(ctx, std.bshash(bs) == 493832887, "wrong hash, expected {}", std.bshash(bs))
+ std.bsput(bs, 123456)
+ testr.check(ctx, std.bshash(bs) == 746495367, "wrong hash, expected {}", std.bshash(bs))
+ std.bsdel(bs, 123456)
+ testr.check(ctx, std.bshash(bs) == 493832887, "wrong hash, expected {}", std.bshash(bs))
+ std.bsfree(bs)
}]
][:])
}