diff options
author | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2014-07-26 21:40:10 +0200 |
---|---|---|
committer | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2014-07-26 22:11:27 +0200 |
commit | d21b682268d22e38186ddd8a24b5ad8d78442e5c (patch) | |
tree | 43209f36352d07915b25a3e9896de0004fb33be0 /lib/pure/collections/sets.nim | |
parent | 6c3b967de358e0b6504ccd8e138799369aab9d1c (diff) | |
download | Nim-d21b682268d22e38186ddd8a24b5ad8d78442e5c.tar.gz |
Adds TOrderedSet.init(), wraps initOrderedSet around it.
Diffstat (limited to 'lib/pure/collections/sets.nim')
-rw-r--r-- | lib/pure/collections/sets.nim | 57 |
1 files changed, 49 insertions, 8 deletions
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 4af2b7ad4..4cdc8cf51 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -266,7 +266,11 @@ type slot: TSlotEnum, next: int, key: A] TOrderedKeyValuePairSeq[A] = seq[TOrderedKeyValuePair[A]] TOrderedSet* {. - final, myShallow.}[A] = object ## set that remembers insertion order + final, myShallow.}[A] = object ## \ + ## A generic hash set that remembers insertion order. + ## + ## Use `init() <#init,TOrderedSet[A]>`_ or `initOrderedSet[type]() + ## <#initOrderedSet>`_ before calling other procs on it. data: TOrderedKeyValuePairSeq[A] counter, first, last: int @@ -353,14 +357,41 @@ proc containsOrIncl*[A](s: var TOrderedSet[A], key: A): bool = assert s.isValid, "The set needs to be initialized." containsOrInclImpl() -proc initOrderedSet*[A](initialSize=64): TOrderedSet[A] = - ## creates a new ordered hash set that is empty. `initialSize` needs to be - ## a power of two. +proc init*[A](s: var TOrderedSet[A], initialSize=64) = + ## Initializes an ordered hash set. + ## + ## The `initialSize` parameter needs to be a power of too. You can use + ## `math.nextPowerOfTwo() <math.html#nextPowerOfTwo>`_ to guarantee that at + ## runtime. All set variables have to be initialized before you can use them + ## with other procs from this module with the exception of `isValid() + ## <#isValid,TOrderedSet[A]>`_ and `len() <#len,TOrderedSet[A]>`_. + ## + ## You can call this method on a previously initialized ordered hash set to + ## discard its values. At the moment this is the only method to remove + ## elements from an ordered hash set. Example: + ## + ## .. code-block :: + ## var a: TOrderedSet[int] + ## a.init(4) + ## a.incl(2) + ## a.init + ## assert a.len == 0 and a.isValid assert isPowerOfTwo(initialSize) - result.counter = 0 - result.first = -1 - result.last = -1 - newSeq(result.data, initialSize) + s.counter = 0 + s.first = -1 + s.last = -1 + newSeq(s.data, initialSize) + +proc initOrderedSet*[A](initialSize=64): TOrderedSet[A] = + ## Convenience wrapper around `init() <#init,TOrderedSet[A]>`_. + ## + ## Returns an empty ordered hash set you can assign directly in ``var`` + ## blocks in a single line. Example: + ## + ## .. code-block :: + ## var a = initOrderedSet[int](4) + ## a.incl(2) + result.init(initialSize) proc toOrderedSet*[A](keys: openArray[A]): TOrderedSet[A] = ## creates a new ordered hash set that contains the given `keys`. @@ -479,6 +510,16 @@ proc testModule() = assert($a == $b) # assert(a == b) # https://github.com/Araq/Nimrod/issues/1413 + block initBlocks: + var a: TOrderedSet[int] + a.init(4) + a.incl(2) + a.init + assert a.len == 0 and a.isValid + a = initOrderedSet[int](4) + a.incl(2) + assert a.len == 1 + echo "Micro tests run successfully." when isMainModule and not defined(release): testModule() |