Source Edit

The packedsets module implements an efficient Ordinal set implemented as a sparse bit set.

Supports any Ordinal type.

See also

Imports

since, hashes

Types

  1. PackedSet[A] = object

An efficient set of Ordinal types implemented as a sparse bit set. Source Edit

Procs

  1. proc `$`[A](s: PackedSet[A]): string

Converts s to a string.

Example:

  1. let a = [1, 2, 3].toPackedSet
  2. assert $a == "{1, 2, 3}"

Source Edit

  1. proc `*`[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.}

Alias for intersection(s1, s2). Source Edit

  1. proc `+`[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.}

Alias for union(s1, s2). Source Edit

  1. proc `-`[A](s1, s2: PackedSet[A]): PackedSet[A] {.inline.}

Alias for difference(s1, s2). Source Edit

  1. proc `<`[A](s1, s2: PackedSet[A]): bool

Returns true if s1 is a proper subset of s2.

A strict or proper subset s1 has all of its elements in s2, but s2 has more elements than s1.

Example:

  1. let
  2. a = [1].toPackedSet
  3. b = [1, 2].toPackedSet
  4. c = [1, 3].toPackedSet
  5. assert a < b
  6. assert not (b < b)
  7. assert not (c < b)

Source Edit

  1. proc `<=`[A](s1, s2: PackedSet[A]): bool

Returns true if s1 is a subset of s2.

A subset s1 has all of its elements in s2, but s2 doesn’t necessarily have more elements than s1. That is, s1 can be equal to s2.

Example:

  1. let
  2. a = [1].toPackedSet
  3. b = [1, 2].toPackedSet
  4. c = [1, 3].toPackedSet
  5. assert a <= b
  6. assert b <= b
  7. assert not (c <= b)

Source Edit

  1. proc `==`[A](s1, s2: PackedSet[A]): bool

Returns true if both s1 and s2 have the same elements and set size.

Example:

  1. assert [1, 2].toPackedSet == [2, 1].toPackedSet
  2. assert [1, 2].toPackedSet == [2, 1, 2].toPackedSet

Source Edit

  1. proc `=copy`[A](dest: var PackedSet[A]; src: PackedSet[A])

Copies src to dest. dest does not need to be initialized by the initPackedSet proc. Source Edit

  1. proc assign[A](dest: var PackedSet[A]; src: PackedSet[A]) {.inline, ...deprecated.}

Deprecated

Copies src to dest. dest does not need to be initialized by the initPackedSet proc.

Example:

  1. var
  2. a = initPackedSet[int]()
  3. b = initPackedSet[int]()
  4. b.incl(5)
  5. b.incl(7)
  6. a.assign(b)
  7. assert len(a) == 2

Source Edit

  1. proc card[A](s: PackedSet[A]): int {.inline.}

Alias for len().

Card stands for the cardinality of a set.

Source Edit

  1. proc clear[A](result: var PackedSet[A])

Clears the PackedSet[A] back to an empty state.

Example:

  1. var a = [5, 7].toPackedSet
  2. clear(a)
  3. assert len(a) == 0

Source Edit

  1. proc contains[A](s: PackedSet[A]; key: A): bool

Returns true if key is in s.

This allows the usage of the in operator.

Example:

  1. type ABCD = enum A, B, C, D
  2. let a = [1, 3, 5].toPackedSet
  3. assert a.contains(3)
  4. assert 3 in a
  5. assert not a.contains(8)
  6. assert 8 notin a
  7. let letters = [A, C].toPackedSet
  8. assert A in letters
  9. assert C in letters
  10. assert B notin letters

Source Edit

  1. proc containsOrIncl[A](s: var PackedSet[A]; key: A): bool

Includes key in the set s and tells if key was already in s.

The difference with regards to the incl proc is that this proc returns true if s already contained key. The proc will return false if key was added as a new value to s during this call.

See also:

Example:

  1. var a = initPackedSet[int]()
  2. assert a.containsOrIncl(3) == false
  3. assert a.containsOrIncl(3) == true
  4. assert a.containsOrIncl(4) == false

Source Edit

  1. proc difference[A](s1, s2: PackedSet[A]): PackedSet[A]

Returns the difference of the sets s1 and s2.

The same as s1 - s2.

Example:

  1. let
  2. a = [1, 2, 3].toPackedSet
  3. b = [3, 4, 5].toPackedSet
  4. c = difference(a, b)
  5. assert c.len == 2
  6. assert c == [1, 2].toPackedSet

Source Edit

  1. proc disjoint[A](s1, s2: PackedSet[A]): bool

Returns true if the sets s1 and s2 have no items in common.

Example:

  1. let
  2. a = [1, 2].toPackedSet
  3. b = [2, 3].toPackedSet
  4. c = [3, 4].toPackedSet
  5. assert disjoint(a, b) == false
  6. assert disjoint(a, c) == true

Source Edit

  1. proc excl[A](s: var PackedSet[A]; key: A)

Excludes key from the set s.

This doesn’t do anything if key is not found in s.

See also:

Example:

  1. var a = [3].toPackedSet
  2. a.excl(3)
  3. a.excl(3)
  4. a.excl(99)
  5. assert len(a) == 0

Source Edit

  1. proc excl[A](s: var PackedSet[A]; other: PackedSet[A])

Excludes all elements from other from s.

This is the in-place version of s - other.

See also:

Example:

  1. var a = [1, 5].toPackedSet
  2. a.excl([5].toPackedSet)
  3. assert len(a) == 1
  4. assert 5 notin a

Source Edit

  1. proc incl[A](s: var PackedSet[A]; key: A)

Includes an element key in s.

This doesn’t do anything if key is already in s.

See also:

Example:

  1. var a = initPackedSet[int]()
  2. a.incl(3)
  3. a.incl(3)
  4. assert len(a) == 1

Source Edit

  1. proc incl[A](s: var PackedSet[A]; other: PackedSet[A])

Includes all elements from other into s.

This is the in-place version of s + other.

See also:

Example:

  1. var a = [1].toPackedSet
  2. a.incl([5].toPackedSet)
  3. assert len(a) == 2
  4. assert 5 in a

Source Edit

  1. proc initPackedSet[A](): PackedSet[A]

Returns an empty PackedSet[A]. A must be Ordinal.

See also:

Example:

  1. let a = initPackedSet[int]()
  2. assert len(a) == 0
  3. type Id = distinct int
  4. var ids = initPackedSet[Id]()
  5. ids.incl(3.Id)

Source Edit

  1. proc intersection[A](s1, s2: PackedSet[A]): PackedSet[A]

Returns the intersection of the sets s1 and s2.

The same as s1 * s2.

Example:

  1. let
  2. a = [1, 2, 3].toPackedSet
  3. b = [3, 4, 5].toPackedSet
  4. c = intersection(a, b)
  5. assert c.len == 1
  6. assert c == [3].toPackedSet

Source Edit

  1. proc isNil[A](x: PackedSet[A]): bool {.inline.}

Returns true if x is empty, false otherwise.

Example:

  1. var a = initPackedSet[int]()
  2. assert a.isNil
  3. a.incl(2)
  4. assert not a.isNil
  5. a.excl(2)
  6. assert a.isNil

Source Edit

  1. proc len[A](s: PackedSet[A]): int {.inline.}

Returns the number of elements in s.

Example:

  1. let a = [1, 3, 5].toPackedSet
  2. assert len(a) == 3

Source Edit

  1. proc missingOrExcl[A](s: var PackedSet[A]; key: A): bool

Excludes key from the set s and tells if key was already missing from s.

The difference with regards to the excl proc is that this proc returns true if key was missing from s. The proc will return false if key was in s and it was removed during this call.

See also:

Example:

  1. var a = [5].toPackedSet
  2. assert a.missingOrExcl(5) == false
  3. assert a.missingOrExcl(5) == true

Source Edit

  1. proc symmetricDifference[A](s1, s2: PackedSet[A]): PackedSet[A]

Returns the symmetric difference of the sets s1 and s2.

Example:

  1. let
  2. a = [1, 2, 3].toPackedSet
  3. b = [3, 4, 5].toPackedSet
  4. c = symmetricDifference(a, b)
  5. assert c.len == 4
  6. assert c == [1, 2, 4, 5].toPackedSet

Source Edit

  1. proc toPackedSet[A](x: openArray[A]): PackedSet[A]

Creates a new PackedSet[A] that contains the elements of x.

Duplicates are removed.

See also:

Example:

  1. let a = [5, 6, 7, 8, 8].toPackedSet
  2. assert len(a) == 4
  3. assert $a == "{5, 6, 7, 8}"

Source Edit

  1. proc union[A](s1, s2: PackedSet[A]): PackedSet[A]

Returns the union of the sets s1 and s2.

The same as s1 + s2.

Example:

  1. let
  2. a = [1, 2, 3].toPackedSet
  3. b = [3, 4, 5].toPackedSet
  4. c = union(a, b)
  5. assert c.len == 5
  6. assert c == [1, 2, 3, 4, 5].toPackedSet

Source Edit

Iterators

  1. iterator items[A](s: PackedSet[A]): A {.inline.}

Iterates over any included element of s. Source Edit