summary refs log tree commit diff stats
path: root/lib/pure/collections/tables.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-06-12 14:45:56 +0200
committerAraq <rumpf_a@web.de>2014-06-12 14:45:56 +0200
commit9354b8a9d7a4cbc47b0d427c34849dd316b2a65e (patch)
tree9ec4fa232d493d6417b1cf7637fd34775d9bfb34 /lib/pure/collections/tables.nim
parent41e599abb917713a98a6bf8ae39fde46311c9db5 (diff)
downloadNim-9354b8a9d7a4cbc47b0d427c34849dd316b2a65e.tar.gz
added allValues iterator
Diffstat (limited to 'lib/pure/collections/tables.nim')
-rw-r--r--lib/pure/collections/tables.nim11
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index 848f4b8ba..091bf8590 100644
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -145,6 +145,14 @@ proc mget*[A, B](t: var TTable[A, B], key: A): var B =
   if index >= 0: result = t.data[index].val
   else: raise newException(EInvalidKey, "key not found: " & $key)
 
+iterator allValues*[A, B](t: TTable[A, B]; key: A): B =
+  ## iterates over any value in the table `t` that belongs to the given `key`.
+  var h: THash = hash(key) and high(t.data)
+  while t.data[h].slot != seEmpty:
+    if t.data[h].key == key and t.data[h].slot == seFilled:
+      yield t.data[h].val
+    h = nextTry(h, high(t.data))
+
 proc hasKey*[A, B](t: TTable[A, B], key: A): bool =
   ## returns true iff `key` is in the table `t`.
   result = rawGet(t, key) >= 0
@@ -313,8 +321,7 @@ proc newTable*[A, B](initialSize=64): PTable[A, B] =
   new(result)
   result[] = initTable[A, B](initialSize)
 
-proc newTable*[A, B](pairs: openArray[tuple[key: A, 
-                    val: B]]): PTable[A, B] =
+proc newTable*[A, B](pairs: openArray[tuple[key: A, val: B]]): PTable[A, B] =
   ## creates a new hash table that contains the given `pairs`.
   new(result)
   result[] = toTable[A, B](pairs)