On this page
sets
The sets module implements an efficient hash set and ordered hash set.
Hash sets are different from the built in set type. Sets allow you to store any value that can be hashed and they don't contain duplicate entries.
Common usages of sets:
- removing duplicates from a container by converting it with toHashSet proc (see also sequtils.deduplicate proc)
- membership testing
- mathematical operations on two sets, such as union, intersection, difference, and symmetric difference
echo toHashSet([9, 5, 1]) # {9, 1, 5}
echo toOrderedSet([9, 5, 1]) # {9, 5, 1}
let
s1 = toHashSet([9, 5, 1])
s2 = toHashSet([3, 5, 7])
echo s1 + s2 # {9, 1, 3, 5, 7}
echo s1 - s2 # {1, 9}
echo s1 * s2 # {5}
echo s1 -+- s2 # {9, 1, 3, 7}
Note: The data types declared here have value semantics: This means that = performs a copy of the set.
See also:
- intsets module for efficient int sets
- tables module for hash tables
Imports
Types
-
HashSet[A] {...}{..} = object data: KeyValuePairSeq[A] counter: int -
A generic hash set.
Use init proc or initHashSet proc before calling other procs on it.
Source Edit -
OrderedSet[A] {...}{..} = object data: OrderedKeyValuePairSeq[A] counter, first, last: int -
A generic hash set that remembers insertion order.
Use init proc or initOrderedSet proc before calling other procs on it.
Source Edit -
SomeSet[A] = HashSet[A] | OrderedSet[A] -
Type union representing
HashSetorOrderedSet. Source Edit
Consts
Procs
-
proc rightSize(count: Natural): int {...}{.inline, deprecated: "Deprecated since 1.4.0", raises: [], tags: [].} -
Deprecated since Nim v1.4.0, it is not needed anymore because picking the correct size is done internally.
Return the value of
initialSizeto supportcountitems.If more items are expected to be added, simply add that expected extra amount to the parameter before calling this.
Source Edit -
proc init[A](s: var HashSet[A]; initialSize = defaultInitialSize) -
Initializes a hash set.
Starting from Nim v0.20, sets are initialized by default and it is not necessary to call this function explicitly.
You can call this proc on a previously initialized hash set, which will discard all its values. This might be more convenient than iterating over existing values and calling excl() on them.
See also:
Example:
Source Editvar a: HashSet[int] init(a) -
proc initHashSet[A](initialSize = defaultInitialSize): HashSet[A] -
Wrapper around init proc for initialization of hash sets.
Returns an empty hash set you can assign directly in
varblocks in a single line.Starting from Nim v0.20, sets are initialized by default and it is not necessary to call this function explicitly.
See also:
Example:
Source Editvar a = initHashSet[int]() a.incl(3) assert len(a) == 1 -
proc `[]`[A](s: var HashSet[A]; key: A): var A -
Returns the element that is actually stored in
swhich has the same value askeyor raises theKeyErrorexception.This is useful when one overloaded
Source Edithashand==but still needs reference semantics for sharing. -
proc contains[A](s: HashSet[A]; key: A): bool -
Returns true if
keyis ins.This allows the usage of
inoperator.See also:
Example:
Source Editvar values = initHashSet[int]() assert(not values.contains(2)) assert 2 notin values values.incl(2) assert values.contains(2) assert 2 in values -
proc incl[A](s: var HashSet[A]; key: A) -
Includes an element
keyins.This doesn't do anything if
keyis already ins.See also:
- excl proc for excluding an element
- incl proc for including other set
- containsOrIncl proc
Example:
Source Editvar values = initHashSet[int]() values.incl(2) values.incl(2) assert values.len == 1 -
proc incl[A](s: var HashSet[A]; other: HashSet[A]) -
Includes all elements from
otherset intos(must be declared asvar).This is the in-place version of s + other.
See also:
- excl proc for excluding other set
- incl proc for including an element
- containsOrIncl proc
Example:
Source Editvar values = toHashSet([1, 2, 3]) others = toHashSet([3, 4, 5]) values.incl(others) assert values.len == 5 -
proc toHashSet[A](keys: openArray[A]): HashSet[A] -
Creates a new hash set that contains the members of the given collection (seq, array, or string)
keys.Duplicates are removed.
See also:
Example:
Source Editlet a = toHashSet([5, 3, 2]) b = toHashSet("abracadabra") assert len(a) == 3 ## a == {2, 3, 5} assert len(b) == 5 ## b == {'a', 'b', 'c', 'd', 'r'} -
proc containsOrIncl[A](s: var HashSet[A]; key: A): bool -
Includes
keyin the setsand tells ifkeywas already ins.The difference with regards to the incl proc is that this proc returns
trueifsalready containedkey. The proc will returnfalseifkeywas added as a new value tosduring this call.See also:
- incl proc for including an element
- incl proc for including other set
- missingOrExcl proc
Example:
Source Editvar values = initHashSet[int]() assert values.containsOrIncl(2) == false assert values.containsOrIncl(2) == true assert values.containsOrIncl(3) == false -
proc excl[A](s: var HashSet[A]; key: A) -
Excludes
keyfrom the sets.This doesn't do anything if
keyis not found ins.See also:
- incl proc for including an element
- excl proc for excluding other set
- missingOrExcl proc
Example:
Source Editvar s = toHashSet([2, 3, 6, 7]) s.excl(2) s.excl(2) assert s.len == 3 -
proc excl[A](s: var HashSet[A]; other: HashSet[A]) -
Excludes all elements of
otherset froms.This is the in-place version of s - other.
See also:
- incl proc for including other set
- excl proc for excluding an element
- missingOrExcl proc
Example:
Source Editvar numbers = toHashSet([1, 2, 3, 4, 5]) even = toHashSet([2, 4, 6, 8]) numbers.excl(even) assert len(numbers) == 3 ## numbers == {1, 3, 5} -
proc missingOrExcl[A](s: var HashSet[A]; key: A): bool -
Excludes
keyin the setsand tells ifkeywas already missing froms.The difference with regards to the excl proc is that this proc returns
trueifkeywas missing froms. The proc will returnfalseifkeywas insand it was removed during this call.See also:
- excl proc for excluding an element
- excl proc for excluding other set
- containsOrIncl proc
Example:
Source Editvar s = toHashSet([2, 3, 6, 7]) assert s.missingOrExcl(4) == true assert s.missingOrExcl(6) == false assert s.missingOrExcl(6) == true -
proc pop[A](s: var HashSet[A]): A -
Removes and returns an arbitrary element from the set
s.Raises KeyError if the set
sis empty.See also:
Example:
Source Editvar s = toHashSet([2, 1]) assert [s.pop, s.pop] in [[1, 2], [2,1]] # order unspecified doAssertRaises(KeyError, echo s.pop) -
proc clear[A](s: var HashSet[A]) -
Clears the HashSet back to an empty state, without shrinking any of the existing storage.
O(n)operation, wherenis the size of the hash bucket.See also:
Example:
Source Editvar s = toHashSet([3, 5, 7]) clear(s) assert len(s) == 0 -
proc len[A](s: HashSet[A]): int -
Returns the number of elements in
s.Due to an implementation detail you can call this proc on variables which have not been initialized yet. The proc will return zero as the length then.
Example:
Source Editvar a: HashSet[string] assert len(a) == 0 let s = toHashSet([3, 5, 7]) assert len(s) == 3 -
proc card[A](s: HashSet[A]): int -
Alias for len().
Card stands for the cardinality of a set.
Source Edit -
proc union[A](s1, s2: HashSet[A]): HashSet[A] -
Returns the union of the sets
s1ands2.The same as s1 + s2.
The union of two sets is represented mathematically as A ∪ B and is the set of all objects that are members of
s1,s2or both.See also:
Example:
Source Editlet a = toHashSet(["a", "b"]) b = toHashSet(["b", "c"]) c = union(a, b) assert c == toHashSet(["a", "b", "c"]) -
proc intersection[A](s1, s2: HashSet[A]): HashSet[A] -
Returns the intersection of the sets
s1ands2.The same as s1 * s2.
The intersection of two sets is represented mathematically as A ∩ B and is the set of all objects that are members of
s1ands2at the same time.See also:
Example:
Source Editlet a = toHashSet(["a", "b"]) b = toHashSet(["b", "c"]) c = intersection(a, b) assert c == toHashSet(["b"]) -
proc difference[A](s1, s2: HashSet[A]): HashSet[A] -
Returns the difference of the sets
s1ands2.The same as s1 - s2.
The difference of two sets is represented mathematically as A ∖ B and is the set of all objects that are members of
s1and not members ofs2.See also:
Example:
Source Editlet a = toHashSet(["a", "b"]) b = toHashSet(["b", "c"]) c = difference(a, b) assert c == toHashSet(["a"]) -
proc symmetricDifference[A](s1, s2: HashSet[A]): HashSet[A] -
Returns the symmetric difference of the sets
s1ands2.The same as s1 -+- s2.
The symmetric difference of two sets is represented mathematically as A △ B or A ⊖ B and is the set of all objects that are members of
s1ors2but not both at the same time.See also:
Example:
Source Editlet a = toHashSet(["a", "b"]) b = toHashSet(["b", "c"]) c = symmetricDifference(a, b) assert c == toHashSet(["a", "c"]) -
proc `+`[A](s1, s2: HashSet[A]): HashSet[A] {...}{.inline.} - Alias for union(s1, s2). Source Edit
-
proc `*`[A](s1, s2: HashSet[A]): HashSet[A] {...}{.inline.} - Alias for intersection(s1, s2). Source Edit
-
proc `-`[A](s1, s2: HashSet[A]): HashSet[A] {...}{.inline.} - Alias for difference(s1, s2). Source Edit
-
proc `-+-`[A](s1, s2: HashSet[A]): HashSet[A] {...}{.inline.} - Alias for symmetricDifference(s1, s2). Source Edit
-
proc disjoint[A](s1, s2: HashSet[A]): bool -
Returns
trueif the setss1ands2have no items in common.Example:
Source Editlet a = toHashSet(["a", "b"]) b = toHashSet(["b", "c"]) assert disjoint(a, b) == false assert disjoint(a, b - a) == true -
proc `<`[A](s, t: HashSet[A]): bool -
Returns true if
sis a strict or proper subset oft.A strict or proper subset
shas all of its members intbutthas more elements thans.Example:
Source Editlet a = toHashSet(["a", "b"]) b = toHashSet(["b", "c"]) c = intersection(a, b) assert c < a and c < b assert(not (a < a)) -
proc `<=`[A](s, t: HashSet[A]): bool -
Returns true if
sis a subset oft.A subset
shas all of its members intandtdoesn't necessarily have more members thans. That is,scan be equal tot.Example:
Source Editlet a = toHashSet(["a", "b"]) b = toHashSet(["b", "c"]) c = intersection(a, b) assert c <= a and c <= b assert a <= a -
proc `==`[A](s, t: HashSet[A]): bool -
Returns true if both
sandthave the same members and set size.Example:
Source Editvar a = toHashSet([1, 2]) b = toHashSet([2, 1]) assert a == b -
proc map[A, B](data: HashSet[A]; op: proc (x: A): B {...}{.closure.}): HashSet[B] -
Returns a new set after applying
opproc on each of the elements ofdataset.You can use this proc to transform the elements from a set.
Example:
Source Editlet a = toHashSet([1, 2, 3]) b = a.map(proc (x: int): string = $x) assert b == toHashSet(["1", "2", "3"]) -
proc hash[A](s: HashSet[A]): Hash - Hashing of HashSet. Source Edit
-
proc `$`[A](s: HashSet[A]): string -
Converts the set
sto a string, mostly for logging and printing purposes.Don't use this proc for serialization, the representation may change at any moment and values are not escaped.
Examples:
Source Editecho toHashSet([2, 4, 5]) # --> {2, 4, 5} echo toHashSet(["no", "esc'aping", "is \" provided"]) # --> {no, esc'aping, is " provided} -
proc initSet[A](initialSize = defaultInitialSize): HashSet[A] {...}{. deprecated: "Deprecated since v0.20, use \'initHashSet\'".} - Source Edit
-
proc toSet[A](keys: openArray[A]): HashSet[A] {...}{. deprecated: "Deprecated since v0.20, use \'toHashSet\'".} - Source Edit
-
proc isValid[A](s: HashSet[A]): bool {...}{.deprecated: "Deprecated since v0.20; sets are initialized by default".} -
Returns
trueif the set has been initialized (with initHashSet proc or init proc).Examples:
Source Editproc savePreferences(options: HashSet[string]) = assert options.isValid, "Pass an initialized set!" # Do stuff here, may crash in release builds! -
proc init[A](s: var OrderedSet[A]; initialSize = defaultInitialSize) -
Initializes an ordered hash set.
Starting from Nim v0.20, sets are initialized by default and it is not necessary to call this function explicitly.
You can call this proc on a previously initialized hash set, which will discard all its values. This might be more convenient than iterating over existing values and calling excl() on them.
See also:
Example:
Source Editvar a: OrderedSet[int] init(a) -
proc initOrderedSet[A](initialSize = defaultInitialSize): OrderedSet[A] -
Wrapper around init proc for initialization of ordered hash sets.
Returns an empty ordered hash set you can assign directly in
varblocks in a single line.Starting from Nim v0.20, sets are initialized by default and it is not necessary to call this function explicitly.
See also:
Example:
Source Editvar a = initOrderedSet[int]() a.incl(3) assert len(a) == 1 -
proc toOrderedSet[A](keys: openArray[A]): OrderedSet[A] -
Creates a new hash set that contains the members of the given collection (seq, array, or string)
keys.Duplicates are removed.
See also:
Example:
Source Editlet a = toOrderedSet([5, 3, 2]) b = toOrderedSet("abracadabra") assert len(a) == 3 ## a == {5, 3, 2} # different than in HashSet assert len(b) == 5 ## b == {'a', 'b', 'r', 'c', 'd'} # different than in HashSet -
proc contains[A](s: OrderedSet[A]; key: A): bool -
Returns true if
keyis ins.This allows the usage of
inoperator.See also:
Example:
Source Editvar values = initOrderedSet[int]() assert(not values.contains(2)) assert 2 notin values values.incl(2) assert values.contains(2) assert 2 in values -
proc incl[A](s: var OrderedSet[A]; key: A) -
Includes an element
keyins.This doesn't do anything if
keyis already ins.See also:
- excl proc for excluding an element
- incl proc for including other set
- containsOrIncl proc
Example:
Source Editvar values = initOrderedSet[int]() values.incl(2) values.incl(2) assert values.len == 1 -
proc incl[A](s: var HashSet[A]; other: OrderedSet[A]) -
Includes all elements from the OrderedSet
otherinto HashSets(must be declared asvar).See also:
- incl proc for including an element
- containsOrIncl proc
Example:
Source Editvar values = toHashSet([1, 2, 3]) others = toOrderedSet([3, 4, 5]) values.incl(others) assert values.len == 5 -
proc containsOrIncl[A](s: var OrderedSet[A]; key: A): bool -
Includes
keyin the setsand tells ifkeywas already ins.The difference with regards to the incl proc is that this proc returns
trueifsalready containedkey. The proc will return false ifkeywas added as a new value tosduring this call.See also:
- incl proc for including an element
- missingOrExcl proc
Example:
Source Editvar values = initOrderedSet[int]() assert values.containsOrIncl(2) == false assert values.containsOrIncl(2) == true assert values.containsOrIncl(3) == false -
proc excl[A](s: var OrderedSet[A]; key: A) -
Excludes
keyfrom the sets. Efficiency:O(n).This doesn't do anything if
keyis not found ins.See also:
- incl proc for including an element
- missingOrExcl proc
Example:
Source Editvar s = toOrderedSet([2, 3, 6, 7]) s.excl(2) s.excl(2) assert s.len == 3 -
proc missingOrExcl[A](s: var OrderedSet[A]; key: A): bool -
Excludes
keyin the setsand tells ifkeywas already missing froms. Efficiency: O(n).The difference with regards to the excl proc is that this proc returns
trueifkeywas missing froms. The proc will returnfalseifkeywas insand it was removed during this call.See also:
Example:
Source Editvar s = toOrderedSet([2, 3, 6, 7]) assert s.missingOrExcl(4) == true assert s.missingOrExcl(6) == false assert s.missingOrExcl(6) == true -
proc clear[A](s: var OrderedSet[A]) -
Clears the OrderedSet back to an empty state, without shrinking any of the existing storage.
O(n)operation wherenis the size of the hash bucket.Example:
Source Editvar s = toOrderedSet([3, 5, 7]) clear(s) assert len(s) == 0 -
proc len[A](s: OrderedSet[A]): int {...}{.inline.} -
Returns the number of elements in
s.Due to an implementation detail you can call this proc on variables which have not been initialized yet. The proc will return zero as the length then.
Example:
Source Editvar a: OrderedSet[string] assert len(a) == 0 let s = toHashSet([3, 5, 7]) assert len(s) == 3 -
proc card[A](s: OrderedSet[A]): int {...}{.inline.} -
Alias for len().
Card stands for the cardinality of a set.
Source Edit -
proc `==`[A](s, t: OrderedSet[A]): bool -
Equality for ordered sets.
Example:
Source Editlet a = toOrderedSet([1, 2]) b = toOrderedSet([2, 1]) assert(not (a == b)) -
proc hash[A](s: OrderedSet[A]): Hash - Hashing of OrderedSet. Source Edit
-
proc `$`[A](s: OrderedSet[A]): string -
Converts the ordered hash set
sto a string, mostly for logging and printing purposes.Don't use this proc for serialization, the representation may change at any moment and values are not escaped.
Examples:
Source Editecho toOrderedSet([2, 4, 5]) # --> {2, 4, 5} echo toOrderedSet(["no", "esc'aping", "is \" provided"]) # --> {no, esc'aping, is " provided}
Iterators
-
iterator items[A](s: HashSet[A]): A -
Iterates over elements of the set
s.If you need a sequence with the elements you can use sequtils.toSeq template.
Source Edittype 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 == 2 echo b # --> {(a: 1, b: 3), (a: 0, b: 4)} -
iterator items[A](s: OrderedSet[A]): A -
Iterates over keys in the ordered set
sin insertion order.If you need a sequence with the elements you can use sequtils.toSeq template.
Source Editvar 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 # --> Got 9 # --> Got 2 # --> Got 1 # --> Got 5 # --> Got 8 # --> Got 4 -
iterator pairs[A](s: OrderedSet[A]): tuple[a: int, b: A] -
Iterates through (position, value) tuples of OrderedSet
s.Example:
Source Editlet a = toOrderedSet("abracadabra") var p = newSeq[(int, char)]() for x in pairs(a): p.add(x) assert p == @[(0, 'a'), (1, 'b'), (2, 'r'), (3, 'c'), (4, 'd')]
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/sets.html