On this page
std/packedsets
Source EditThe packedsets module implements an efficient Ordinal set implemented as a sparse bit set.
Supports any Ordinal type.
See also
- sets module for more general hash sets
Imports
Types
Procs
-
proc `*`[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.} - Alias for intersection(s1, s2). Source Edit
-
proc `+`[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.} - Alias for union(s1, s2). Source Edit
-
proc `-`[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.} - Alias for difference(s1, s2). Source Edit
-
proc `<`[A](s1, s2: PackedSet[A]): bool -
Returns true if
s1is a proper subset ofs2.A strict or proper subset
s1has all of its elements ins2, buts2has more elements thans1.Example:
Source Editlet a = [1].toPackedSet b = [1, 2].toPackedSet c = [1, 3].toPackedSet assert a < b assert not (b < b) assert not (c < b) -
proc `<=`[A](s1, s2: PackedSet[A]): bool -
Returns true if
s1is a subset ofs2.A subset
s1has all of its elements ins2, buts2doesn't necessarily have more elements thans1. That is,s1can be equal tos2.Example:
Source Editlet a = [1].toPackedSet b = [1, 2].toPackedSet c = [1, 3].toPackedSet assert a <= b assert b <= b assert not (c <= b) -
proc `=copy`[A](dest: var PackedSet[A]; src: PackedSet[A]) -
Copies
srctodest.destdoes not need to be initialized by the initPackedSet proc. Source Edit -
proc assign[A](dest: var PackedSet[A]; src: PackedSet[A]) {.inline, ...deprecated.} -
Copies
srctodest.destdoes not need to be initialized by the initPackedSet proc.Example:
Source Editvar a = initPackedSet[int]() b = initPackedSet[int]() b.incl(5) b.incl(7) a.assign(b) assert len(a) == 2 -
proc card[A](s: PackedSet[A]): int {.inline.} -
Alias for len().
Card stands for the cardinality of a set.
Source Edit -
proc contains[A](s: PackedSet[A]; key: A): bool -
Returns true if
keyis ins.This allows the usage of the
inoperator.Example:
Source Edittype ABCD = enum A, B, C, D let a = [1, 3, 5].toPackedSet assert a.contains(3) assert 3 in a assert not a.contains(8) assert 8 notin a let letters = [A, C].toPackedSet assert A in letters assert C in letters assert B notin letters -
proc containsOrIncl[A](s: var PackedSet[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 true if
salready 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 a = initPackedSet[int]() assert a.containsOrIncl(3) == false assert a.containsOrIncl(3) == true assert a.containsOrIncl(4) == false -
proc excl[A](s: var PackedSet[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 a set
- missingOrExcl proc
Example:
Source Editvar a = [3].toPackedSet a.excl(3) a.excl(3) a.excl(99) assert len(a) == 0 -
proc excl[A](s: var PackedSet[A]; other: PackedSet[A]) -
Excludes all elements from
otherfroms.This is the in-place version of s - other.
See also:
- incl proc for including a set
- excl proc for excluding an element
- missingOrExcl proc
Example:
Source Editvar a = [1, 5].toPackedSet a.excl([5].toPackedSet) assert len(a) == 1 assert 5 notin a -
proc incl[A](s: var PackedSet[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 a set
- containsOrIncl proc
Example:
Source Editvar a = initPackedSet[int]() a.incl(3) a.incl(3) assert len(a) == 1 -
proc incl[A](s: var PackedSet[A]; other: PackedSet[A]) -
Includes all elements from
otherintos.This is the in-place version of s + other.
See also:
- excl proc for excluding a set
- incl proc for including an element
- containsOrIncl proc
Example:
Source Editvar a = [1].toPackedSet a.incl([5].toPackedSet) assert len(a) == 2 assert 5 in a -
proc missingOrExcl[A](s: var PackedSet[A]; key: A): bool -
Excludes
keyfrom the setsand tells ifkeywas already missing froms.The difference with regards to the excl proc is that this proc returns true if
keywas missing froms. The proc will return false ifkeywas insand it was removed during this call.See also:
- excl proc for excluding an element
- excl proc for excluding a set
- containsOrIncl proc
Example:
Source Editvar a = [5].toPackedSet assert a.missingOrExcl(5) == false assert a.missingOrExcl(5) == true
Iterators
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/packedsets.html