Skip to content

Commit

Permalink
packedsets fix regression introduced in nim-lang#15564 (nim-lang#16060)
Browse files Browse the repository at this point in the history
* packedsets fix regression introduced in nim-lang#15564

* add tests
  • Loading branch information
timotheecour authored and mildred committed Jan 11, 2021
1 parent 227129d commit 24b871b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/std/packedsets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ proc incl*[A](s: var PackedSet[A], other: PackedSet[A]) =
assert len(a) == 2
assert 5 in a

for item in other: incl(s, item)
for item in other.items: incl(s, item)

proc toPackedSet*[A](x: openArray[A]): PackedSet[A] {.since: (1, 3).} =
## Creates a new PackedSet[A] that contains the elements of `x`.
Expand Down Expand Up @@ -358,7 +358,7 @@ proc excl*[A](s: var PackedSet[A], other: PackedSet[A]) =
assert len(a) == 1
assert 5 notin a

for item in other:
for item in other.items:
excl(s, item)

proc len*[A](s: PackedSet[A]): int {.inline.} =
Expand All @@ -367,7 +367,8 @@ proc len*[A](s: PackedSet[A]): int {.inline.} =
result = s.elems
else:
result = 0
for _ in s:
for _ in s.items:
# pending bug #11167; when fixed, check each explicit `items` to see if it can be removed
inc(result)

proc missingOrExcl*[A](s: var PackedSet[A], key: A): bool =
Expand Down Expand Up @@ -488,7 +489,7 @@ proc intersection*[A](s1, s2: PackedSet[A]): PackedSet[A] =
## {3}

result = initPackedSet[A]()
for item in s1:
for item in s1.items:
if contains(s2, item):
incl(result, item)

Expand All @@ -506,7 +507,7 @@ proc difference*[A](s1, s2: PackedSet[A]): PackedSet[A] =
## {1, 2}

result = initPackedSet[A]()
for item in s1:
for item in s1.items:
if not contains(s2, item):
incl(result, item)

Expand All @@ -522,7 +523,7 @@ proc symmetricDifference*[A](s1, s2: PackedSet[A]): PackedSet[A] =
## {1, 2, 4, 5}

result.assign(s1)
for item in s2:
for item in s2.items:
if containsOrIncl(result, item): excl(result, item)

proc `+`*[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.} =
Expand All @@ -549,7 +550,7 @@ proc disjoint*[A](s1, s2: PackedSet[A]): bool =
b.excl(2)
assert disjoint(a, b) == true

for item in s1:
for item in s1.items:
if contains(s2, item):
return false
return true
Expand All @@ -575,7 +576,7 @@ proc `<=`*[A](s1, s2: PackedSet[A]): bool =
a.incl(3)
assert(not (a <= b))

for item in s1:
for item in s1.items:
if not s2.contains(item):
return false
return true
Expand Down
10 changes: 10 additions & 0 deletions tests/stdlib/mintsets.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import std/intsets

proc test1*[]() =
let a = initIntSet()
doAssert len(a) == 0

proc test2*[]() =
var a = initIntSet()
var b = initIntSet()
a.incl b
6 changes: 6 additions & 0 deletions tests/stdlib/tintsets.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ./mintsets

block: # bug https://github.com/nim-lang/Nim/pull/15564#issuecomment-729878104
# related to bug #11167
test1()
test2()

0 comments on commit 24b871b

Please sign in to comment.