shithub: mc

Download patch

ref: 003c3c95490986905616bd7c79b9ab2a9d65e115
parent: 6f8ceb3bf2f16c16cf566b4dc99b09dd2b538479
author: Ori Bernstein <ori@markovcorp.com>
date: Fri Mar 17 13:59:11 EDT 2017

Don't count trailing elements when hashing bitsets.

	These don't logically affect the contents, so we shouldn't
	care what they have.

--- a/lib/std/bitset.myr
+++ b/lib/std/bitset.myr
@@ -156,7 +156,13 @@
 }
 
 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
--- a/lib/std/test/bitset.myr
+++ b/lib/std/test/bitset.myr
@@ -46,6 +46,15 @@
 			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)
 		}]
 	][:])
 }