summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/collections/tableimpl.nim5
-rw-r--r--lib/pure/collections/tables.nim34
2 files changed, 34 insertions, 5 deletions
diff --git a/lib/pure/collections/tableimpl.nim b/lib/pure/collections/tableimpl.nim
index e4ec05b1c..cc32fbedc 100644
--- a/lib/pure/collections/tableimpl.nim
+++ b/lib/pure/collections/tableimpl.nim
@@ -133,3 +133,8 @@ template delImpl() {.dirty, immediate.} =
           t.data[j] = t.data[i]
         else:
           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
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index b702f6248..da0b5422f 100644
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -85,6 +85,10 @@ template dataLen(t): expr = len(t.data)
 
 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.} =
   ## 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,
                           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.
   ##
   ## .. code-block:: nim
   ##
   ##   sharedTable.withValue(key, value) do:
   ##     # block is executed only if ``key`` in ``t``
-  ##     value.name = "username" 
+  ##     value.name = "username"
   ##     value.uid = 1000
   ##
   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,
                           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.
-  ## 
+  ##
   ## .. code-block:: nim
   ##
   ##   table.withValue(key, value) do:
   ##     # block is executed only if ``key`` in ``t``
-  ##     value.name = "username" 
+  ##     value.name = "username"
   ##     value.uid = 1000
   ##   do:
   ##     # 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`.
   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.} =
   var h = t.first
   while h >= 0:
@@ -744,6 +754,11 @@ proc len*[A](t: CountTable[A]): int =
   ## returns the number of keys in `t`.
   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) =
   ## iterates over any (key, value) pair in the table `t`.
   for h in 0..high(t.data):
@@ -1126,3 +1141,12 @@ when isMainModule:
     doAssert 0 == t.getOrDefault(testKey)
     t.inc(testKey,3)
     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