diff options
author | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2014-07-26 22:10:15 +0200 |
---|---|---|
committer | Grzegorz Adam Hankiewicz <gradha@imap.cc> | 2014-07-26 22:11:27 +0200 |
commit | 0bfe956c05f1bbf21b96b4c7319adfabfced600d (patch) | |
tree | 81075c65891474270855ba2586ed36584cb1b3c4 /lib/pure/collections/sets.nim | |
parent | d21b682268d22e38186ddd8a24b5ad8d78442e5c (diff) | |
download | Nim-0bfe956c05f1bbf21b96b4c7319adfabfced600d.tar.gz |
Adds TSet.init(), wraps initSet around it.
Diffstat (limited to 'lib/pure/collections/sets.nim')
-rw-r--r-- | lib/pure/collections/sets.nim | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 4cdc8cf51..988a68b98 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -23,7 +23,11 @@ type TSlotEnum = enum seEmpty, seFilled, seDeleted TKeyValuePair[A] = tuple[slot: TSlotEnum, key: A] TKeyValuePairSeq[A] = seq[TKeyValuePair[A]] - TSet* {.final, myShallow.}[A] = object ## a generic hash set + TSet* {.final, myShallow.}[A] = object ## \ + ## A generic hash set. + ## + ## Use `init() <#init,TSet[A]>`_ or `initSet[type]() <#initSet>`_ before + ## calling other procs on it. data: TKeyValuePairSeq[A] counter: int @@ -156,12 +160,39 @@ proc containsOrIncl*[A](s: var TSet[A], key: A): bool = assert s.isValid, "The set needs to be initialized." containsOrInclImpl() -proc initSet*[A](initialSize=64): TSet[A] = - ## creates a new hash set that is empty. `initialSize` needs to be - ## a power of two. +proc init*[A](s: var TSet[A], initialSize=64) = + ## Initializes a 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,TSet[A]>`_ and `len() <#len,TSet[A]>`_. + ## + ## You can call this method on a previously initialized hash set, which will + ## discard all its values. This might be more convenient than iterating over + ## existing values and calling `excl() <#excl,TSet[A],A>`_ on them. Example: + ## + ## .. code-block :: + ## var a: TSet[int] + ## a.init(4) + ## a.incl(2) + ## a.init + ## assert a.len == 0 and a.isValid assert isPowerOfTwo(initialSize) - result.counter = 0 - newSeq(result.data, initialSize) + s.counter = 0 + newSeq(s.data, initialSize) + +proc initSet*[A](initialSize=64): TSet[A] = + ## Convenience wrapper around `init() <#init,TSet[A]>`_. + ## + ## Returns an empty hash set you can assign directly in ``var`` blocks in a + ## single line. Example: + ## + ## .. code-block :: + ## var a = initSet[int](4) + ## a.incl(2) + result.init(initialSize) proc toSet*[A](keys: openArray[A]): TSet[A] = ## creates a new hash set that contains the given `keys`. @@ -520,6 +551,15 @@ proc testModule() = a.incl(2) assert a.len == 1 + var b: TSet[int] + b.init(4) + b.incl(2) + b.init + assert b.len == 0 and b.isValid + b = initSet[int](4) + b.incl(2) + assert b.len == 1 + echo "Micro tests run successfully." when isMainModule and not defined(release): testModule() |