use initHashSet and toHashSet, fixes #10730 (#10736)

This commit is contained in:
Miran 2019-02-25 16:53:05 +01:00 • committed by Andreas Rumpf
commit fb863a147a

View file

@ -15,8 +15,8 @@
## `hashed <hashes.html>`_ and they don't contain duplicate entries. ## `hashed <hashes.html>`_ and they don't contain duplicate entries.
## ##
## Common usages of sets: ## Common usages of sets:
## * removing duplicates from a container by converting it with `toSet proc ## * removing duplicates from a container by converting it with `toHashSet proc
## <#toSet,openArray[A]>`_ (see also `sequtils.deduplicate proc ## <#toHashSet,openArray[A]>`_ (see also `sequtils.deduplicate proc
## <sequtils.html#deduplicate,openArray[T],bool>`_) ## <sequtils.html#deduplicate,openArray[T],bool>`_)
## * membership testing ## * membership testing
## * mathematical operations on two sets, such as ## * mathematical operations on two sets, such as
@ -26,12 +26,12 @@
## `symmetric difference <#symmetricDifference,HashSet[A],HashSet[A]>`_ ## `symmetric difference <#symmetricDifference,HashSet[A],HashSet[A]>`_
## ##
## .. code-block:: ## .. code-block::
## echo toSet([9, 5, 1]) # {9, 1, 5} ## echo toHashSet([9, 5, 1]) # {9, 1, 5}
## echo toOrderedSet([9, 5, 1]) # {9, 5, 1} ## echo toOrderedSet([9, 5, 1]) # {9, 5, 1}
## ##
## let ## let
## s1 = toSet([9, 5, 1]) ## s1 = toHashSet([9, 5, 1])
## s2 = toSet([3, 5, 7]) ## s2 = toHashSet([3, 5, 7])
## ##
## echo s1 + s2 # {9, 1, 3, 5, 7} ## echo s1 + s2 # {9, 1, 3, 5, 7}
## echo s1 - s2 # {1, 9} ## echo s1 - s2 # {1, 9}
@ -64,7 +64,7 @@ type
HashSet* {.myShallow.} [A] = object ## \ HashSet* {.myShallow.} [A] = object ## \
## A generic hash set. ## A generic hash set.
## ##
## Use `init proc <#init,HashSet[A],int>`_ or `initSet proc <#initSet,int>`_ ## Use `init proc <#init,HashSet[A],int>`_ or `initHashSet proc <#initHashSet,int>`_
## before calling other procs on it. ## before calling other procs on it.
data: KeyValuePairSeq[A] data: KeyValuePairSeq[A]
counter: int counter: int
@ -230,8 +230,8 @@ proc init*[A](s: var HashSet[A], initialSize=64) =
## existing values and calling `excl() <#excl,HashSet[A],A>`_ on them. ## existing values and calling `excl() <#excl,HashSet[A],A>`_ on them.
## ##
## See also: ## See also:
## * `initSet proc <#initSet,int>`_ ## * `initHashSet proc <#initHashSet,int>`_
## * `toSet proc <#toSet,openArray[A]>`_ ## * `toHashSet proc <#toHashSet,openArray[A]>`_
runnableExamples: runnableExamples:
var a: HashSet[int] var a: HashSet[int]
assert(not a.isValid) assert(not a.isValid)
@ -242,7 +242,7 @@ proc init*[A](s: var HashSet[A], initialSize=64) =
s.counter = 0 s.counter = 0
newSeq(s.data, initialSize) newSeq(s.data, initialSize)
proc initSet*[A](initialSize=64): HashSet[A] = proc initHashSet*[A](initialSize=64): HashSet[A] =
## Wrapper around `init proc <#init,HashSet[A],int>`_ for initialization of ## Wrapper around `init proc <#init,HashSet[A],int>`_ for initialization of
## hash sets. ## hash sets.
## ##
@ -250,37 +250,45 @@ proc initSet*[A](initialSize=64): HashSet[A] =
## single line. ## single line.
## ##
## See also: ## See also:
## * `toSet proc <#toSet,openArray[A]>`_ ## * `toHashSet proc <#toHashSet,openArray[A]>`_
runnableExamples: runnableExamples:
var a = initSet[int]() var a = initHashSet[int]()
assert a.isValid assert a.isValid
a.incl(3) a.incl(3)
assert len(a) == 1 assert len(a) == 1
result.init(initialSize) result.init(initialSize)
proc toSet*[A](keys: openArray[A]): HashSet[A] = proc toHashSet*[A](keys: openArray[A]): HashSet[A] =
## Creates a new hash set that contains the members of the given ## Creates a new hash set that contains the members of the given
## collection (seq, array, or string) `keys`. ## collection (seq, array, or string) `keys`.
## ##
## Duplicates are removed. ## Duplicates are removed.
## ##
## See also: ## See also:
## * `initSet proc <#initSet,int>`_ ## * `initHashSet proc <#initHashSet,int>`_
runnableExamples: runnableExamples:
let let
a = toSet([5, 3, 2]) a = toHashSet([5, 3, 2])
b = toSet("abracadabra") b = toHashSet("abracadabra")
assert len(a) == 3 assert len(a) == 3
## a == {2, 3, 5} ## a == {2, 3, 5}
assert len(b) == 5 assert len(b) == 5
## b == {'a', 'b', 'c', 'd', 'r'} ## b == {'a', 'b', 'c', 'd', 'r'}
result = initSet[A](rightSize(keys.len)) result = initHashSet[A](rightSize(keys.len))
for key in items(keys): result.incl(key) for key in items(keys): result.incl(key)
proc initSet*[A](initialSize=64): HashSet[A] {.deprecated:
"Deprecated since v0.20, use `initHashSet`"} = initHashSet[A](initialSize)
## Deprecated since v0.20, use `initHashSet`.
proc toSet*[A](keys: openArray[A]): HashSet[A] {.deprecated:
"Deprecated since v0.20, use `toHashSet`"} = toHashSet[A](keys)
## Deprecated since v0.20, use `toHashSet`.
proc isValid*[A](s: HashSet[A]): bool = proc isValid*[A](s: HashSet[A]): bool =
## Returns `true` if the set has been initialized (with `initSet proc ## Returns `true` if the set has been initialized (with `initHashSet proc
## <#initSet,int>`_ or `init proc <#init,HashSet[A],int>`_). ## <#initHashSet,int>`_ or `init proc <#init,HashSet[A],int>`_).
## ##
## Most operations over an uninitialized set will crash at runtime and ## Most operations over an uninitialized set will crash at runtime and
## `assert <system.html#assert>`_ in debug builds. You can use this proc in ## `assert <system.html#assert>`_ in debug builds. You can use this proc in
@ -320,7 +328,7 @@ proc contains*[A](s: HashSet[A], key: A): bool =
## * `incl proc <#incl,HashSet[A],A>`_ ## * `incl proc <#incl,HashSet[A],A>`_
## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_ ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
runnableExamples: runnableExamples:
var values = initSet[int]() var values = initHashSet[int]()
assert(not values.contains(2)) assert(not values.contains(2))
assert 2 notin values assert 2 notin values
@ -343,7 +351,7 @@ proc incl*[A](s: var HashSet[A], key: A) =
## * `incl proc <#incl,HashSet[A],HashSet[A]>`_ for including other set ## * `incl proc <#incl,HashSet[A],HashSet[A]>`_ for including other set
## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_ ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
runnableExamples: runnableExamples:
var values = initSet[int]() var values = initHashSet[int]()
values.incl(2) values.incl(2)
values.incl(2) values.incl(2)
assert values.len == 1 assert values.len == 1
@ -362,8 +370,8 @@ proc incl*[A](s: var HashSet[A], other: HashSet[A]) =
## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_ ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
runnableExamples: runnableExamples:
var var
values = toSet([1, 2, 3]) values = toHashSet([1, 2, 3])
others = toSet([3, 4, 5]) others = toHashSet([3, 4, 5])
values.incl(others) values.incl(others)
assert values.len == 5 assert values.len == 5
@ -384,7 +392,7 @@ proc containsOrIncl*[A](s: var HashSet[A], key: A): bool =
## * `incl proc <#incl,HashSet[A],HashSet[A]>`_ for including other set ## * `incl proc <#incl,HashSet[A],HashSet[A]>`_ for including other set
## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_ ## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_
runnableExamples: runnableExamples:
var values = initSet[int]() var values = initHashSet[int]()
assert values.containsOrIncl(2) == false assert values.containsOrIncl(2) == false
assert values.containsOrIncl(2) == true assert values.containsOrIncl(2) == true
assert values.containsOrIncl(3) == false assert values.containsOrIncl(3) == false
@ -402,7 +410,7 @@ proc excl*[A](s: var HashSet[A], key: A) =
## * `excl proc <#excl,HashSet[A],HashSet[A]>`_ for excluding other set ## * `excl proc <#excl,HashSet[A],HashSet[A]>`_ for excluding other set
## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_ ## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_
runnableExamples: runnableExamples:
var s = toSet([2, 3, 6, 7]) var s = toHashSet([2, 3, 6, 7])
s.excl(2) s.excl(2)
s.excl(2) s.excl(2)
assert s.len == 3 assert s.len == 3
@ -419,8 +427,8 @@ proc excl*[A](s: var HashSet[A], other: HashSet[A]) =
## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_ ## * `missingOrExcl proc <#missingOrExcl,HashSet[A],A>`_
runnableExamples: runnableExamples:
var var
numbers = toSet([1, 2, 3, 4, 5]) numbers = toHashSet([1, 2, 3, 4, 5])
even = toSet([2, 4, 6, 8]) even = toHashSet([2, 4, 6, 8])
numbers.excl(even) numbers.excl(even)
assert len(numbers) == 3 assert len(numbers) == 3
## numbers == {1, 3, 5} ## numbers == {1, 3, 5}
@ -442,7 +450,7 @@ proc missingOrExcl*[A](s: var HashSet[A], key: A): bool =
## * `excl proc <#excl,HashSet[A],HashSet[A]>`_ for excluding other set ## * `excl proc <#excl,HashSet[A],HashSet[A]>`_ for excluding other set
## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_ ## * `containsOrIncl proc <#containsOrIncl,HashSet[A],A>`_
runnableExamples: runnableExamples:
var s = toSet([2, 3, 6, 7]) var s = toHashSet([2, 3, 6, 7])
assert s.missingOrExcl(4) == true assert s.missingOrExcl(4) == true
assert s.missingOrExcl(6) == false assert s.missingOrExcl(6) == false
assert s.missingOrExcl(6) == true assert s.missingOrExcl(6) == true
@ -456,7 +464,7 @@ proc pop*[A](s: var HashSet[A]): A =
## See also: ## See also:
## * `clear proc <#clear,HashSet[A]>`_ ## * `clear proc <#clear,HashSet[A]>`_
runnableExamples: runnableExamples:
var s = toSet([2, 1]) var s = toHashSet([2, 1])
assert s.pop == 1 assert s.pop == 1
assert s.pop == 2 assert s.pop == 2
doAssertRaises(KeyError, echo s.pop) doAssertRaises(KeyError, echo s.pop)
@ -477,7 +485,7 @@ proc clear*[A](s: var HashSet[A]) =
## See also: ## See also:
## * `pop proc <#pop,HashSet[A]>`_ ## * `pop proc <#pop,HashSet[A]>`_
runnableExamples: runnableExamples:
var s = toSet([3, 5, 7]) var s = toHashSet([3, 5, 7])
clear(s) clear(s)
assert len(s) == 0 assert len(s) == 0
@ -495,7 +503,7 @@ proc len*[A](s: HashSet[A]): int =
runnableExamples: runnableExamples:
var a: HashSet[string] var a: HashSet[string]
assert len(a) == 0 assert len(a) == 0
let s = toSet([3, 5, 7]) let s = toHashSet([3, 5, 7])
assert len(s) == 3 assert len(s) == 3
result = s.counter result = s.counter
@ -521,10 +529,10 @@ proc union*[A](s1, s2: HashSet[A]): HashSet[A] =
## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_ ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
runnableExamples: runnableExamples:
let let
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
c = union(a, b) c = union(a, b)
assert c == toSet(["a", "b", "c"]) assert c == toHashSet(["a", "b", "c"])
assert s1.isValid, "The set `s1` needs to be initialized." assert s1.isValid, "The set `s1` needs to be initialized."
assert s2.isValid, "The set `s2` needs to be initialized." assert s2.isValid, "The set `s2` needs to be initialized."
@ -546,14 +554,14 @@ proc intersection*[A](s1, s2: HashSet[A]): HashSet[A] =
## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_ ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
runnableExamples: runnableExamples:
let let
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
c = intersection(a, b) c = intersection(a, b)
assert c == toSet(["b"]) assert c == toHashSet(["b"])
assert s1.isValid, "The set `s1` needs to be initialized." assert s1.isValid, "The set `s1` needs to be initialized."
assert s2.isValid, "The set `s2` needs to be initialized." assert s2.isValid, "The set `s2` needs to be initialized."
result = initSet[A](min(s1.data.len, s2.data.len)) result = initHashSet[A](min(s1.data.len, s2.data.len))
for item in s1: for item in s1:
if item in s2: incl(result, item) if item in s2: incl(result, item)
@ -571,14 +579,14 @@ proc difference*[A](s1, s2: HashSet[A]): HashSet[A] =
## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_ ## * `symmetricDifference proc <#symmetricDifference,HashSet[A],HashSet[A]>`_
runnableExamples: runnableExamples:
let let
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
c = difference(a, b) c = difference(a, b)
assert c == toSet(["a"]) assert c == toHashSet(["a"])
assert s1.isValid, "The set `s1` needs to be initialized." assert s1.isValid, "The set `s1` needs to be initialized."
assert s2.isValid, "The set `s2` needs to be initialized." assert s2.isValid, "The set `s2` needs to be initialized."
result = initSet[A]() result = initHashSet[A]()
for item in s1: for item in s1:
if not contains(s2, item): if not contains(s2, item):
incl(result, item) incl(result, item)
@ -598,10 +606,10 @@ proc symmetricDifference*[A](s1, s2: HashSet[A]): HashSet[A] =
## * `difference proc <#difference,HashSet[A],HashSet[A]>`_ ## * `difference proc <#difference,HashSet[A],HashSet[A]>`_
runnableExamples: runnableExamples:
let let
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
c = symmetricDifference(a, b) c = symmetricDifference(a, b)
assert c == toSet(["a", "c"]) assert c == toHashSet(["a", "c"])
assert s1.isValid, "The set `s1` needs to be initialized." assert s1.isValid, "The set `s1` needs to be initialized."
assert s2.isValid, "The set `s2` needs to be initialized." assert s2.isValid, "The set `s2` needs to be initialized."
@ -630,8 +638,8 @@ proc disjoint*[A](s1, s2: HashSet[A]): bool =
## Returns `true` if the sets `s1` and `s2` have no items in common. ## Returns `true` if the sets `s1` and `s2` have no items in common.
runnableExamples: runnableExamples:
let let
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
assert disjoint(a, b) == false assert disjoint(a, b) == false
assert disjoint(a, b - a) == true assert disjoint(a, b - a) == true
@ -648,8 +656,8 @@ proc `<`*[A](s, t: HashSet[A]): bool =
## more elements than `s`. ## more elements than `s`.
runnableExamples: runnableExamples:
let let
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
c = intersection(a, b) c = intersection(a, b)
assert c < a and c < b assert c < a and c < b
assert(not (a < a)) assert(not (a < a))
@ -662,8 +670,8 @@ proc `<=`*[A](s, t: HashSet[A]): bool =
## have more members than `s`. That is, `s` can be equal to `t`. ## have more members than `s`. That is, `s` can be equal to `t`.
runnableExamples: runnableExamples:
let let
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
c = intersection(a, b) c = intersection(a, b)
assert c <= a and c <= b assert c <= a and c <= b
assert a <= a assert a <= a
@ -680,8 +688,8 @@ proc `==`*[A](s, t: HashSet[A]): bool =
## Returns true if both `s` and `t` have the same members and set size. ## Returns true if both `s` and `t` have the same members and set size.
runnableExamples: runnableExamples:
var var
a = toSet([1, 2]) a = toHashSet([1, 2])
b = toSet([2, 1]) b = toHashSet([2, 1])
assert a == b assert a == b
s.counter == t.counter and s <= t s.counter == t.counter and s <= t
@ -692,11 +700,11 @@ proc map*[A, B](data: HashSet[A], op: proc (x: A): B {.closure.}): HashSet[B] =
## You can use this proc to transform the elements from a set. ## You can use this proc to transform the elements from a set.
runnableExamples: runnableExamples:
let let
a = toSet([1, 2, 3]) a = toHashSet([1, 2, 3])
b = a.map(proc (x: int): string = $x) b = a.map(proc (x: int): string = $x)
assert b == toSet(["1", "2", "3"]) assert b == toHashSet(["1", "2", "3"])
result = initSet[B]() result = initHashSet[B]()
for item in data: result.incl(op(item)) for item in data: result.incl(op(item))
proc hash*[A](s: HashSet[A]): Hash = proc hash*[A](s: HashSet[A]): Hash =
@ -715,9 +723,9 @@ proc `$`*[A](s: HashSet[A]): string =
## **Examples:** ## **Examples:**
## ##
## .. code-block:: ## .. code-block::
## echo toSet([2, 4, 5]) ## echo toHashSet([2, 4, 5])
## # --> {2, 4, 5} ## # --> {2, 4, 5}
## echo toSet(["no", "esc'aping", "is \" provided"]) ## echo toHashSet(["no", "esc'aping", "is \" provided"])
## # --> {no, esc'aping, is " provided} ## # --> {no, esc'aping, is " provided}
assert s.isValid, "The set needs to be initialized." assert s.isValid, "The set needs to be initialized."
dollarImpl() dollarImpl()
@ -743,7 +751,7 @@ iterator items*[A](s: HashSet[A]): A =
## type ## type
## pair = tuple[a, b: int] ## pair = tuple[a, b: int]
## var ## var
## a, b = initSet[pair]() ## a, b = initHashSet[pair]()
## a.incl((2, 3)) ## a.incl((2, 3))
## a.incl((3, 2)) ## a.incl((3, 2))
## a.incl((2, 3)) ## a.incl((2, 3))
@ -917,7 +925,7 @@ proc toOrderedSet*[A](keys: openArray[A]): OrderedSet[A] =
for key in items(keys): result.incl(key) for key in items(keys): result.incl(key)
proc isValid*[A](s: OrderedSet[A]): bool = proc isValid*[A](s: OrderedSet[A]): bool =
## Returns `true` if the set has been initialized (with `initSet proc ## Returns `true` if the set has been initialized (with `initHashSet proc
## <#initOrderedSet,int>`_ or `init proc <#init,OrderedSet[A],int>`_). ## <#initOrderedSet,int>`_ or `init proc <#init,OrderedSet[A],int>`_).
## ##
## Most operations over an uninitialized set will crash at runtime and ## Most operations over an uninitialized set will crash at runtime and
@ -982,7 +990,7 @@ proc incl*[A](s: var HashSet[A], other: OrderedSet[A]) =
## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_ ## * `containsOrIncl proc <#containsOrIncl,OrderedSet[A],A>`_
runnableExamples: runnableExamples:
var var
values = toSet([1, 2, 3]) values = toHashSet([1, 2, 3])
others = toOrderedSet([3, 4, 5]) others = toOrderedSet([3, 4, 5])
values.incl(others) values.incl(others)
assert values.len == 5 assert values.len == 5
@ -1071,7 +1079,7 @@ proc len*[A](s: OrderedSet[A]): int {.inline.} =
runnableExamples: runnableExamples:
var a: OrderedSet[string] var a: OrderedSet[string]
assert len(a) == 0 assert len(a) == 0
let s = toSet([3, 5, 7]) let s = toHashSet([3, 5, 7])
assert len(s) == 3 assert len(s) == 3
result = s.counter result = s.counter
@ -1181,7 +1189,7 @@ when isMainModule and not defined(release):
var options: HashSet[string] var options: HashSet[string]
proc savePreferences(options: HashSet[string]) = proc savePreferences(options: HashSet[string]) =
assert options.isValid, "Pass an initialized set!" assert options.isValid, "Pass an initialized set!"
options = initSet[string]() options = initHashSet[string]()
options.savePreferences options.savePreferences
block lenTest: block lenTest:
@ -1192,7 +1200,7 @@ when isMainModule and not defined(release):
block setIterator: block setIterator:
type pair = tuple[a, b: int] type pair = tuple[a, b: int]
var a, b = initSet[pair]() var a, b = initHashSet[pair]()
a.incl((2, 3)) a.incl((2, 3))
a.incl((3, 2)) a.incl((3, 2))
a.incl((2, 3)) a.incl((2, 3))
@ -1203,7 +1211,7 @@ when isMainModule and not defined(release):
#echo b #echo b
block setContains: block setContains:
var values = initSet[int]() var values = initHashSet[int]()
assert(not values.contains(2)) assert(not values.contains(2))
values.incl(2) values.incl(2)
assert values.contains(2) assert values.contains(2)
@ -1211,7 +1219,7 @@ when isMainModule and not defined(release):
assert(not values.contains(2)) assert(not values.contains(2))
values.incl(4) values.incl(4)
var others = toSet([6, 7]) var others = toHashSet([6, 7])
values.incl(others) values.incl(others)
assert values.len == 3 assert values.len == 3
@ -1219,31 +1227,31 @@ when isMainModule and not defined(release):
assert values.containsOrIncl(2) == false assert values.containsOrIncl(2) == false
assert values.containsOrIncl(2) == true assert values.containsOrIncl(2) == true
var var
a = toSet([1, 2]) a = toHashSet([1, 2])
b = toSet([1]) b = toHashSet([1])
b.incl(2) b.incl(2)
assert a == b assert a == b
block exclusions: block exclusions:
var s = toSet([2, 3, 6, 7]) var s = toHashSet([2, 3, 6, 7])
s.excl(2) s.excl(2)
s.excl(2) s.excl(2)
assert s.len == 3 assert s.len == 3
var var
numbers = toSet([1, 2, 3, 4, 5]) numbers = toHashSet([1, 2, 3, 4, 5])
even = toSet([2, 4, 6, 8]) even = toHashSet([2, 4, 6, 8])
numbers.excl(even) numbers.excl(even)
#echo numbers #echo numbers
# --> {1, 3, 5} # --> {1, 3, 5}
block toSeqAndString: block toSeqAndString:
var a = toSet([2, 4, 5]) var a = toHashSet([2, 4, 5])
var b = initSet[int]() var b = initHashSet[int]()
for x in [2, 4, 5]: b.incl(x) for x in [2, 4, 5]: b.incl(x)
assert($a == $b) assert($a == $b)
#echo a #echo a
#echo toSet(["no", "esc'aping", "is \" provided"]) #echo toHashSet(["no", "esc'aping", "is \" provided"])
#block orderedToSeqAndString: #block orderedToSeqAndString:
# echo toOrderedSet([2, 4, 5]) # echo toOrderedSet([2, 4, 5])
@ -1251,32 +1259,32 @@ when isMainModule and not defined(release):
block setOperations: block setOperations:
var var
a = toSet(["a", "b"]) a = toHashSet(["a", "b"])
b = toSet(["b", "c"]) b = toHashSet(["b", "c"])
c = union(a, b) c = union(a, b)
assert c == toSet(["a", "b", "c"]) assert c == toHashSet(["a", "b", "c"])
var d = intersection(a, b) var d = intersection(a, b)
assert d == toSet(["b"]) assert d == toHashSet(["b"])
var e = difference(a, b) var e = difference(a, b)
assert e == toSet(["a"]) assert e == toHashSet(["a"])
var f = symmetricDifference(a, b) var f = symmetricDifference(a, b)
assert f == toSet(["a", "c"]) assert f == toHashSet(["a", "c"])
assert d < a and d < b assert d < a and d < b
assert((a < a) == false) assert((a < a) == false)
assert d <= a and d <= b assert d <= a and d <= b
assert((a <= a)) assert((a <= a))
# Alias test. # Alias test.
assert a + b == toSet(["a", "b", "c"]) assert a + b == toHashSet(["a", "b", "c"])
assert a * b == toSet(["b"]) assert a * b == toHashSet(["b"])
assert a - b == toSet(["a"]) assert a - b == toHashSet(["a"])
assert a -+- b == toSet(["a", "c"]) assert a -+- b == toHashSet(["a", "c"])
assert disjoint(a, b) == false assert disjoint(a, b) == false
assert disjoint(a, b - a) == true assert disjoint(a, b - a) == true
block mapSet: block mapSet:
var a = toSet([1, 2, 3]) var a = toHashSet([1, 2, 3])
var b = a.map(proc (x: int): string = $x) var b = a.map(proc (x: int): string = $x)
assert b == toSet(["1", "2", "3"]) assert b == toHashSet(["1", "2", "3"])
block isValidTest: block isValidTest:
var cards: OrderedSet[string] var cards: OrderedSet[string]
@ -1361,7 +1369,7 @@ when isMainModule and not defined(release):
b.incl(2) b.incl(2)
b.init b.init
assert b.len == 0 and b.isValid assert b.len == 0 and b.isValid
b = initSet[int](4) b = initHashSet[int](4)
b.incl(2) b.incl(2)
assert b.len == 1 assert b.len == 1