diff options
Diffstat (limited to 'lib/pure/collections/intsets.nim')
-rw-r--r-- | lib/pure/collections/intsets.nim | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/lib/pure/collections/intsets.nim b/lib/pure/collections/intsets.nim index def96b8f7..9d7950ea6 100644 --- a/lib/pure/collections/intsets.nim +++ b/lib/pure/collections/intsets.nim @@ -9,9 +9,12 @@ ## The ``intsets`` module implements an efficient int set implemented as a ## `sparse bit set`:idx:. -## **Note**: Since Nim currently does not allow the assignment operator to -## be overloaded, ``=`` for int sets performs some rather meaningless shallow -## copy; use ``assign`` to get a deep copy. + +## **Note**: Currently the assignment operator ``=`` for ``intsets`` +## performs some rather meaningless shallow copy. Since Nim currently does +## not allow the assignment operator to be overloaded, use ``assign`` to +## get a deep copy. + import hashes, math @@ -94,7 +97,7 @@ proc intSetPut(t: var IntSet, key: int): PTrunk = t.data[h] = result proc contains*(s: IntSet, key: int): bool = - ## returns true iff `key` is in `s`. + ## Returns true iff `key` is in `s`. if s.elems <= s.a.len: for i in 0..<s.elems: if s.a[i] == key: return true @@ -107,7 +110,7 @@ proc contains*(s: IntSet, key: int): bool = result = false iterator items*(s: IntSet): int {.inline.} = - ## iterates over any included element of `s`. + ## Iterates over any included element of `s`. if s.elems <= s.a.len: for i in 0..<s.elems: yield s.a[i] @@ -135,7 +138,7 @@ proc bitincl(s: var IntSet, key: int) {.inline.} = `shl`(1, u and IntMask) proc incl*(s: var IntSet, key: int) = - ## includes an element `key` in `s`. + ## Includes an element `key` in `s`. if s.elems <= s.a.len: for i in 0..<s.elems: if s.a[i] == key: return @@ -170,7 +173,7 @@ proc exclImpl(s: var IntSet, key: int) = not `shl`(1, u and IntMask) proc excl*(s: var IntSet, key: int) = - ## excludes `key` from the set `s`. + ## Excludes `key` from the set `s`. exclImpl(s, key) proc excl*(s: var IntSet, other: IntSet) = @@ -178,14 +181,14 @@ proc excl*(s: var IntSet, other: IntSet) = for item in other: excl(s, item) proc missingOrExcl*(s: var IntSet, key: int) : bool = - ## returns true if `s` does not contain `key`, otherwise + ## Returns true if `s` does not contain `key`, otherwise ## `key` is removed from `s` and false is returned. var count = s.elems exclImpl(s, key) result = count == s.elems proc containsOrIncl*(s: var IntSet, key: int): bool = - ## returns true if `s` contains `key`, otherwise `key` is included in `s` + ## Returns true if `s` contains `key`, otherwise `key` is included in `s` ## and false is returned. if s.elems <= s.a.len: for i in 0..<s.elems: @@ -206,23 +209,28 @@ proc containsOrIncl*(s: var IntSet, key: int): bool = result = false proc initIntSet*: IntSet = - ## creates a new int set that is empty. - - #newSeq(result.data, InitIntSetSize) - #result.max = InitIntSetSize-1 - when defined(nimNoNilSeqs): - result.data = @[] - else: - result.data = nil - result.max = 0 - result.counter = 0 - result.head = nil - result.elems = 0 + ## Returns an empty IntSet. Example: + ## + ## .. code-block :: + ## var a = initIntSet() + ## a.incl(2) + + # newSeq(result.data, InitIntSetSize) + # result.max = InitIntSetSize-1 + result = IntSet( + elems: 0, + counter: 0, + max: 0, + head: nil, + data: when defined(nimNoNilSeqs): @[] else: nil) + # a: array[0..33, int] # profiling shows that 34 elements are enough proc clear*(result: var IntSet) = - #setLen(result.data, InitIntSetSize) - #for i in 0..InitIntSetSize-1: result.data[i] = nil - #result.max = InitIntSetSize-1 + ## Clears the IntSet back to an empty state. + + # setLen(result.data, InitIntSetSize) + # for i in 0..InitIntSetSize-1: result.data[i] = nil + # result.max = InitIntSetSize-1 when defined(nimNoNilSeqs): result.data = @[] else: @@ -304,7 +312,7 @@ proc `-`*(s1, s2: IntSet): IntSet {.inline.} = result = difference(s1, s2) proc disjoint*(s1, s2: IntSet): bool = - ## Returns true iff the sets `s1` and `s2` have no items in common. + ## Returns true if the sets `s1` and `s2` have no items in common. for item in s1: if contains(s2, item): return false @@ -320,7 +328,7 @@ proc len*(s: IntSet): int {.inline.} = inc(result) proc card*(s: IntSet): int {.inline.} = - ## alias for `len() <#len>` _. + ## Alias for `len() <#len>` _. result = s.len() proc `<=`*(s1, s2: IntSet): bool = @@ -349,12 +357,6 @@ proc `$`*(s: IntSet): string = ## The `$` operator for int sets. dollarImpl() -proc empty*(s: IntSet): bool {.inline, deprecated.} = - ## returns true if `s` is empty. This is safe to call even before - ## the set has been initialized with `initIntSet`. Note this never - ## worked reliably and so is deprecated. - result = s.counter == 0 - when isMainModule: import sequtils, algorithm |