Implements tables.clear.
This commit is contained in:
parent
af1e941872
commit
2b162c04b1
2 changed files with 34 additions and 5 deletions
|
|
@ -133,3 +133,8 @@ template delImpl() {.dirty, immediate.} =
|
||||||
t.data[j] = t.data[i]
|
t.data[j] = t.data[i]
|
||||||
else:
|
else:
|
||||||
shallowCopy(t.data[j], t.data[i]) # data[j] will be marked EMPTY next loop
|
shallowCopy(t.data[j], t.data[i]) # data[j] will be marked EMPTY next loop
|
||||||
|
|
||||||
|
template clearImpl() {.dirty, immediate.} =
|
||||||
|
for i in 0 .. <t.data.len:
|
||||||
|
t.data[i].hcode = 0
|
||||||
|
t.counter = 0
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,10 @@ template dataLen(t): expr = len(t.data)
|
||||||
|
|
||||||
include tableimpl
|
include tableimpl
|
||||||
|
|
||||||
|
proc clear*[A, B](t: Table[A, B] | TableRef[A, B]) =
|
||||||
|
## Resets the table so that it is empty.
|
||||||
|
clearImpl()
|
||||||
|
|
||||||
proc rightSize*(count: Natural): int {.inline.} =
|
proc rightSize*(count: Natural): int {.inline.} =
|
||||||
## Return the value of `initialSize` to support `count` items.
|
## Return the value of `initialSize` to support `count` items.
|
||||||
##
|
##
|
||||||
|
|
@ -138,14 +142,14 @@ proc getOrDefault*[A, B](t: Table[A, B], key: A): B = getOrDefaultImpl(t, key)
|
||||||
|
|
||||||
template withValue*[A, B](t: var Table[A, B], key: A,
|
template withValue*[A, B](t: var Table[A, B], key: A,
|
||||||
value, body: untyped) =
|
value, body: untyped) =
|
||||||
## retrieves the value at ``t[key]``.
|
## retrieves the value at ``t[key]``.
|
||||||
## `value` can be modified in the scope of the ``withValue`` call.
|
## `value` can be modified in the scope of the ``withValue`` call.
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
##
|
##
|
||||||
## sharedTable.withValue(key, value) do:
|
## sharedTable.withValue(key, value) do:
|
||||||
## # block is executed only if ``key`` in ``t``
|
## # block is executed only if ``key`` in ``t``
|
||||||
## value.name = "username"
|
## value.name = "username"
|
||||||
## value.uid = 1000
|
## value.uid = 1000
|
||||||
##
|
##
|
||||||
mixin rawGet
|
mixin rawGet
|
||||||
|
|
@ -158,14 +162,14 @@ template withValue*[A, B](t: var Table[A, B], key: A,
|
||||||
|
|
||||||
template withValue*[A, B](t: var Table[A, B], key: A,
|
template withValue*[A, B](t: var Table[A, B], key: A,
|
||||||
value, body1, body2: untyped) =
|
value, body1, body2: untyped) =
|
||||||
## retrieves the value at ``t[key]``.
|
## retrieves the value at ``t[key]``.
|
||||||
## `value` can be modified in the scope of the ``withValue`` call.
|
## `value` can be modified in the scope of the ``withValue`` call.
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
##
|
##
|
||||||
## table.withValue(key, value) do:
|
## table.withValue(key, value) do:
|
||||||
## # block is executed only if ``key`` in ``t``
|
## # block is executed only if ``key`` in ``t``
|
||||||
## value.name = "username"
|
## value.name = "username"
|
||||||
## value.uid = 1000
|
## value.uid = 1000
|
||||||
## do:
|
## do:
|
||||||
## # block is executed when ``key`` not in ``t``
|
## # block is executed when ``key`` not in ``t``
|
||||||
|
|
@ -421,6 +425,12 @@ proc len*[A, B](t: OrderedTable[A, B]): int {.inline.} =
|
||||||
## returns the number of keys in `t`.
|
## returns the number of keys in `t`.
|
||||||
result = t.counter
|
result = t.counter
|
||||||
|
|
||||||
|
proc clear*[A, B](t: OrderedTable[A, B] | OrderedTableRef[A, B]) =
|
||||||
|
## Resets the table so that it is empty.
|
||||||
|
clearImpl()
|
||||||
|
t.first = -1
|
||||||
|
t.last = -1
|
||||||
|
|
||||||
template forAllOrderedPairs(yieldStmt: stmt) {.dirty, immediate.} =
|
template forAllOrderedPairs(yieldStmt: stmt) {.dirty, immediate.} =
|
||||||
var h = t.first
|
var h = t.first
|
||||||
while h >= 0:
|
while h >= 0:
|
||||||
|
|
@ -744,6 +754,11 @@ proc len*[A](t: CountTable[A]): int =
|
||||||
## returns the number of keys in `t`.
|
## returns the number of keys in `t`.
|
||||||
result = t.counter
|
result = t.counter
|
||||||
|
|
||||||
|
proc clear*[A](t: CountTable[A] | CountTable[A]) =
|
||||||
|
## Resets the table so that it is empty.
|
||||||
|
clearImpl()
|
||||||
|
t.counter = 0
|
||||||
|
|
||||||
iterator pairs*[A](t: CountTable[A]): (A, int) =
|
iterator pairs*[A](t: CountTable[A]): (A, int) =
|
||||||
## iterates over any (key, value) pair in the table `t`.
|
## iterates over any (key, value) pair in the table `t`.
|
||||||
for h in 0..high(t.data):
|
for h in 0..high(t.data):
|
||||||
|
|
@ -1126,3 +1141,12 @@ when isMainModule:
|
||||||
doAssert 0 == t.getOrDefault(testKey)
|
doAssert 0 == t.getOrDefault(testKey)
|
||||||
t.inc(testKey,3)
|
t.inc(testKey,3)
|
||||||
doAssert 3 == t.getOrDefault(testKey)
|
doAssert 3 == t.getOrDefault(testKey)
|
||||||
|
|
||||||
|
# Clear tests
|
||||||
|
var clearTable = newTable[int, string]()
|
||||||
|
clearTable[42] = "asd"
|
||||||
|
clearTable[123123] = "piuyqwb "
|
||||||
|
doAssert clearTable[42] == "asd"
|
||||||
|
clearTable.clear()
|
||||||
|
doAssert(not clearTable.hasKey(123123))
|
||||||
|
doAssert clearTable.getOrDefault(42) == nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue