packedsets
nimony/lib/std/packedsets.nim
The packedsets module implements an efficient set of Ordinal values as a sparse bit set:idx:. Values that cluster -- symbol ids, file ids, node positions -- cost about one bit each, while values far apart cost one small block each.
Unlike HashSet no hash is required of the element type: the element's ordinal value is the bit index. That is exactly what Ordinal gives us -- int(x) to find the bit and A(i) to hand the element back out of items -- so Ordinal is the constraint, and it is enough to typecheck every body here up front. Routines needing more say so: $ also asks for Stringable.
See also ========
- sets module for general hash sets
- intsets module for the non-generic
intcase
type PackedSet = object t: Table
func initPackedSet(): PackedSet[A]Returns an empty
PackedSet[A].func contains(s: PackedSet[A]; key: A): boolTrue if
keyis ins. This allows the usage of theinoperator.func incl(s: var PackedSet[A]; key: A)Includes
keyins. Does nothing if it is already in there.func excl(s: var PackedSet[A]; key: A)Excludes
keyfroms. Does nothing if it is not in there.func containsOrIncl(s: var PackedSet[A]; key: A): boolIncludes
keyinsand tells whether it was already in there.func missingOrExcl(s: var PackedSet[A]; key: A): boolExcludes
keyfromsand tells whether it was already missing.iterator items(s: PackedSet[A]): AIterates over every element of
s. Trunks come out in insertion order (nimony'sTableis insertion-ordered) and ascending within a trunk, so the traversal is reproducible across runs.func len(s: PackedSet[A]): int64The number of elements in
s.func card(s: PackedSet[A]): int64Alias for
len: the cardinality of the set.func clear(s: var PackedSet[A])Resets
sback to the empty set.func toPackedSet(x: openArray[A]): PackedSet[A]A new
PackedSet[A]holding the elements ofx; duplicates are removed.func incl(s: var PackedSet[A]; other: PackedSet[A])Includes every element of
otherins-- the in-places + other.func excl(s: var PackedSet[A]; other: PackedSet[A])Excludes every element of
otherfroms-- the in-places - other.func union(s1: PackedSet[A]; s2: PackedSet[A]): PackedSet[A]The union of
s1ands2.func intersection(s1: PackedSet[A]; s2: PackedSet[A]): PackedSet[A]The intersection of
s1ands2.func difference(s1: PackedSet[A]; s2: PackedSet[A]): PackedSet[A]The difference of
s1ands2: everything ins1that is not ins2.func symmetricDifference(s1: PackedSet[A]; s2: PackedSet[A]): PackedSet[A]Everything that is in exactly one of
s1ands2.func +(s1: PackedSet[A]; s2: PackedSet[A]): PackedSet[A]Alias for
union.func *(s1: PackedSet[A]; s2: PackedSet[A]): PackedSet[A]Alias for
intersection.func -(s1: PackedSet[A]; s2: PackedSet[A]): PackedSet[A]Alias for
difference.func disjoint(s1: PackedSet[A]; s2: PackedSet[A]): boolTrue if
s1ands2have no element in common.func <=(s1: PackedSet[A]; s2: PackedSet[A]): boolTrue if
s1is a subset ofs2(s1may equals2).func <(s1: PackedSet[A]; s2: PackedSet[A]): boolTrue if
s1is a proper subset ofs2.func ==(s1: PackedSet[A]; s2: PackedSet[A]): boolTrue if both sets hold the same elements.
func $(s: PackedSet[A]): stringRenders
sas{a, b, c}. Needs$of the element type on top of theOrdinal-ness the container itself requires.