Skip to content

Commit 9880595

Browse files
committed
packedsets: add == implementation and fix <=
The default equivalence check does not come through sometimes, so implement our own. Also fix wrong set comparison operator used for comparing words.
1 parent e97fe71 commit 9880595

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

lib/std/packedsets.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ proc `<=`*[A](s1, s2: PackedSet[A]): bool {.inline.} =
379379

380380
result = true
381381
for high, word in s1.bitmap.pairs:
382-
if high notin s2.bitmap or word > s2.bitmap[high]:
382+
if high notin s2.bitmap or not (word <= s2.bitmap[high]):
383383
return false
384384

385385
proc `<`*[A](s1, s2: PackedSet[A]): bool {.inline.} =
@@ -398,6 +398,20 @@ proc `<`*[A](s1, s2: PackedSet[A]): bool {.inline.} =
398398

399399
s1.len < s2.len and s1 <= s2
400400

401+
proc `==`*[A](s1, s2: PackedSet[A]): bool =
402+
## Returns true if both `s1` and `s2` have the same elements and set size.
403+
runnableExamples:
404+
assert [1, 2].toPackedSet == [2, 1].toPackedSet
405+
assert [1, 2].toPackedSet == [2, 1, 2].toPackedSet
406+
407+
if s1.bitmap.len != s2.bitmap.len:
408+
return false
409+
410+
result = true
411+
for high, word in s1.bitmap.pairs:
412+
if high notin s2.bitmap or word != s2.bitmap[high]:
413+
return false
414+
401415
iterator items*[A](s: PackedSet[A]): A {.inline.} =
402416
## Iterates over included elements of `s`.
403417
for high, word in s.bitmap.pairs:

0 commit comments

Comments
 (0)