Skip to content

Commit

Permalink
sets minor improvement (nim-lang#16087)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored and mildred committed Jan 11, 2021
1 parent 0863dcf commit a4d7f78
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 269 deletions.
270 changes: 1 addition & 269 deletions lib/pure/collections/sets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ proc missingOrExcl*[A](s: var HashSet[A], key: A): bool =
exclImpl(s, key)

proc pop*[A](s: var HashSet[A]): A =
## Remove and return an arbitrary element from the set `s`.
## Removes and returns an arbitrary element from the set `s`.
##
## Raises KeyError if the set `s` is empty.
##
Expand Down Expand Up @@ -915,271 +915,3 @@ iterator pairs*[A](s: OrderedSet[A]): tuple[a: int, b: A] =

forAllOrderedPairs:
yield (idx, s.data[h].key)

# -----------------------------------------------------------------------



when isMainModule and not defined(release):
proc testModule() =
## Internal micro test to validate docstrings and such.
block lenTest:
var values: HashSet[int]
assert values.len == 0
assert values.card == 0

block setIterator:
type pair = tuple[a, b: int]
var a, b = initHashSet[pair]()
a.incl((2, 3))
a.incl((3, 2))
a.incl((2, 3))
for x, y in a.items:
b.incl((x - 2, y + 1))
assert a.len == b.card
assert a.len == 2
#echo b

block setContains:
var values = initHashSet[int]()
assert(not values.contains(2))
values.incl(2)
assert values.contains(2)
values.excl(2)
assert(not values.contains(2))

values.incl(4)
var others = toHashSet([6, 7])
values.incl(others)
assert values.len == 3

values.init
assert values.containsOrIncl(2) == false
assert values.containsOrIncl(2) == true
var
a = toHashSet([1, 2])
b = toHashSet([1])
b.incl(2)
assert a == b

block exclusions:
var s = toHashSet([2, 3, 6, 7])
s.excl(2)
s.excl(2)
assert s.len == 3

var
numbers = toHashSet([1, 2, 3, 4, 5])
even = toHashSet([2, 4, 6, 8])
numbers.excl(even)
#echo numbers
# --> {1, 3, 5}

block toSeqAndString:
var a = toHashSet([2, 7, 5])
var b = initHashSet[int](a.len)
for x in [2, 7, 5]: b.incl(x)
assert($a == $b)
#echo a
#echo toHashSet(["no", "esc'aping", "is \" provided"])

#block orderedToSeqAndString:
# echo toOrderedSet([2, 4, 5])
# echo toOrderedSet(["no", "esc'aping", "is \" provided"])

block setOperations:
var
a = toHashSet(["a", "b"])
b = toHashSet(["b", "c"])
c = union(a, b)
assert c == toHashSet(["a", "b", "c"])
var d = intersection(a, b)
assert d == toHashSet(["b"])
var e = difference(a, b)
assert e == toHashSet(["a"])
var f = symmetricDifference(a, b)
assert f == toHashSet(["a", "c"])
assert d < a and d < b
assert((a < a) == false)
assert d <= a and d <= b
assert((a <= a))
# Alias test.
assert a + b == toHashSet(["a", "b", "c"])
assert a * b == toHashSet(["b"])
assert a - b == toHashSet(["a"])
assert a -+- b == toHashSet(["a", "c"])
assert disjoint(a, b) == false
assert disjoint(a, b - a) == true

block mapSet:
var a = toHashSet([1, 2, 3])
var b = a.map(proc (x: int): string = $x)
assert b == toHashSet(["1", "2", "3"])

block lenTest:
var values: OrderedSet[int]
assert values.len == 0
assert values.card == 0

block setIterator:
type pair = tuple[a, b: int]
var a, b = initOrderedSet[pair]()
a.incl((2, 3))
a.incl((3, 2))
a.incl((2, 3))
for x, y in a.items:
b.incl((x - 2, y + 1))
assert a.len == b.card
assert a.len == 2

block setPairsIterator:
var s = toOrderedSet([1, 3, 5, 7])
var items = newSeq[tuple[a: int, b: int]]()
for idx, item in s: items.add((idx, item))
assert items == @[(0, 1), (1, 3), (2, 5), (3, 7)]

block exclusions:
var s = toOrderedSet([1, 2, 3, 6, 7, 4])

s.excl(3)
s.excl(3)
s.excl(1)
s.excl(4)

var items = newSeq[int]()
for item in s: items.add item
assert items == @[2, 6, 7]

block: #9005
var s = initOrderedSet[(int, int)]()
for i in 0 .. 30: incl(s, (i, 0))
for i in 0 .. 30: excl(s, (i, 0))
doAssert s.len == 0

#block orderedSetIterator:
# var a = initOrderedSet[int]()
# for value in [9, 2, 1, 5, 1, 8, 4, 2]:
# a.incl(value)
# for value in a.items:
# echo "Got ", value

block setContains:
var values = initOrderedSet[int]()
assert(not values.contains(2))
values.incl(2)
assert values.contains(2)

block toSeqAndString:
var a = toOrderedSet([2, 4, 5])
var b = initOrderedSet[int]()
for x in [2, 4, 5]: b.incl(x)
assert($a == $b)
assert(a == b) # https://github.com/Araq/Nim/issues/1413

block initBlocks:
var a: OrderedSet[int]
a.init(4)
a.incl(2)
a.init
assert a.len == 0
a = initOrderedSet[int](4)
a.incl(2)
assert a.len == 1

var b: HashSet[int]
b.init(4)
b.incl(2)
b.init
assert b.len == 0
b = initHashSet[int](4)
b.incl(2)
assert b.len == 1

block missingOrExcl:
var s = toOrderedSet([2, 3, 6, 7])
assert s.missingOrExcl(4) == true
assert s.missingOrExcl(6) == false

block orderedSetEquality:
type pair = tuple[a, b: int]

var aa = initOrderedSet[pair]()
var bb = initOrderedSet[pair]()

var x = (a: 1, b: 2)
var y = (a: 3, b: 4)

aa.incl(x)
aa.incl(y)

bb.incl(x)
bb.incl(y)
assert aa == bb

block setsWithoutInit:
var
a: HashSet[int]
b: HashSet[int]
c: HashSet[int]
d: HashSet[int]
e: HashSet[int]

doAssert a.containsOrIncl(3) == false
doAssert a.contains(3)
doAssert a.len == 1
doAssert a.containsOrIncl(3)
a.incl(3)
doAssert a.len == 1
a.incl(6)
doAssert a.len == 2

b.incl(5)
doAssert b.len == 1
b.excl(5)
b.excl(c)
doAssert b.missingOrExcl(5)
doAssert b.disjoint(c)

d = b + c
doAssert d.len == 0
d = b * c
doAssert d.len == 0
d = b - c
doAssert d.len == 0
d = b -+- c
doAssert d.len == 0

doAssert (d < e) == false
doAssert d <= e
doAssert d == e

block setsWithoutInit:
var
a: OrderedSet[int]
b: OrderedSet[int]
c: OrderedSet[int]
d: HashSet[int]


doAssert a.containsOrIncl(3) == false
doAssert a.contains(3)
doAssert a.len == 1
doAssert a.containsOrIncl(3)
a.incl(3)
doAssert a.len == 1
a.incl(6)
doAssert a.len == 2

b.incl(5)
doAssert b.len == 1
doAssert b.missingOrExcl(5) == false
doAssert b.missingOrExcl(5)

doAssert c.missingOrExcl(9)
d.incl(c)
doAssert d.len == 0

when not defined(testing):
echo "Micro tests run successfully."

testModule()
Loading

0 comments on commit a4d7f78

Please sign in to comment.