diff options
author | Andrii Riabushenko <cdome@bk.ru> | 2018-12-05 21:33:15 +0000 |
---|---|---|
committer | Andrii Riabushenko <cdome@bk.ru> | 2018-12-05 21:33:15 +0000 |
commit | 69347f6c95b35e8969cf650b59644b20ef1528de (patch) | |
tree | 97a6d1dfa1d17152dd713ab5165842510791cef2 /lib | |
parent | 938d3ffad7cd3b76692c81ae983c3ce82b10c2d1 (diff) | |
download | Nim-69347f6c95b35e8969cf650b59644b20ef1528de.tar.gz |
implement everything
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/collections/tables.nim | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index f46a368b1..9597d18fd 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -248,6 +248,7 @@ template withValue*[A, B](t: var Table[A, B], key: A, else: body2 + iterator allValues*[A, B](t: Table[A, B]; key: A): B = ## iterates over any value in the table ``t`` that belongs to the given ``key``. var h: Hash = genHash(key) and high(t.data) @@ -879,6 +880,51 @@ proc del*[A, B](t: var OrderedTableRef[A, B], key: A) = ## if the key does not exist. t[].del(key) + +template withValue*[A, B](t: var OrderedTable[A, B], key: A, value, body: untyped) = + ## retrieves the value at ``t[key]``. + ## ``value`` can be modified in the scope of the ``withValue`` call. + ## + ## .. code-block:: nim + ## + ## orderedTable.withValue(key, value) do: + ## # block is executed only if ``key`` in ``t`` + ## value.name = "username" + ## value.uid = 1000 + ## + mixin rawGet + var hc: Hash + var index = rawGet(t, key, hc) + let hasKey = index >= 0 + if hasKey: + var value {.inject.} = addr(t.data[index].val) + body + +template withValue*[A, B](t: var OrderedTable[A, B], key: A, + value, body1, body2: untyped) = + ## retrieves the value at ``t[key]``. + ## ``value`` can be modified in the scope of the ``withValue`` call. + ## + ## .. code-block:: nim + ## + ## orderedTable.withValue(key, value) do: + ## # block is executed only if ``key`` in ``t`` + ## value.name = "username" + ## value.uid = 1000 + ## do: + ## # block is executed when ``key`` not in ``t`` + ## raise newException(KeyError, "Key not found") + ## + mixin rawGet + var hc: Hash + var index = rawGet(t, key, hc) + let hasKey = index >= 0 + if hasKey: + var value {.inject.} = addr(t.data[index].val) + body1 + else: + body2 + # ------------------------------ count tables ------------------------------- type |