summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorJonathan <jonathan.frere@gmail.com>2015-05-05 23:26:58 +0100
committerJonathan <jonathan.frere@gmail.com>2015-05-05 23:26:58 +0100
commit8e4af5ace0286e0440916d5394f117561b583191 (patch)
tree6a1d4ea1284122aaf46eb75a5fd9477d376388b8 /lib
parentb9e02b1efc14a309e613ec4d4cf8c793503bc797 (diff)
downloadNim-8e4af5ace0286e0440916d5394f117561b583191.tar.gz
Add contains proc for tables to allow usage of `in`
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/collections/tables.nim24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index a9357ce67..9496fa2fe 100644
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -215,6 +215,10 @@ proc hasKey*[A, B](t: Table[A, B], key: A): bool =
   var hc: THash
   result = rawGet(t, key, hc) >= 0
 
+proc contains*[A, B](t: Table[A, B], key: A): bool =
+  ## alias of `hasKey` for use with the `in` operator.
+  return hasKey[A, B](t, key)
+
 proc rawInsert[A, B](t: var Table[A, B], data: var KeyValuePairSeq[A, B],
                      key: A, val: B, hc: THash, h: THash) =
   rawInsertImpl()
@@ -411,6 +415,10 @@ proc hasKey*[A, B](t: TableRef[A, B], key: A): bool =
   ## returns true iff `key` is in the table `t`.
   result = t[].hasKey(key)
 
+proc contains*[A, B](t: TableRef[A, B], key: A): bool =
+  ## alias of `hasKey` for use with the `in` operator.
+  return hasKey[A, B](t, key)
+
 proc `[]=`*[A, B](t: TableRef[A, B], key: A, val: B) =
   ## puts a (key, value)-pair into `t`.
   t[][key] = val
@@ -532,6 +540,10 @@ proc hasKey*[A, B](t: OrderedTable[A, B], key: A): bool =
   var hc: THash
   result = rawGet(t, key, hc) >= 0
 
+proc contains*[A, B](t: OrderedTable[A, B], key: A): bool =
+  ## alias of `hasKey` for use with the `in` operator.
+  return hasKey[A, B](t, key)
+
 proc rawInsert[A, B](t: var OrderedTable[A, B],
                      data: var OrderedKeyValuePairSeq[A, B],
                      key: A, val: B, hc: THash, h: THash) =
@@ -704,6 +716,10 @@ proc hasKey*[A, B](t: OrderedTableRef[A, B], key: A): bool =
   ## returns true iff `key` is in the table `t`.
   result = t[].hasKey(key)
 
+proc contains*[A, B](t: OrderedTableRef[A, B], key: A): bool =
+  ## alias of `hasKey` for use with the `in` operator.
+  return hasKey[A, B](t, key)
+
 proc `[]=`*[A, B](t: OrderedTableRef[A, B], key: A, val: B) =
   ## puts a (key, value)-pair into `t`.
   t[][key] = val
@@ -804,6 +820,10 @@ proc hasKey*[A](t: CountTable[A], key: A): bool =
   ## returns true iff `key` is in the table `t`.
   result = rawGet(t, key) >= 0
 
+proc contains*[A](t: CountTable[A], key: A): bool =
+  ## alias of `hasKey` for use with the `in` operator.
+  return hasKey[A](t, key)
+
 proc rawInsert[A](t: CountTable[A], data: var seq[tuple[key: A, val: int]],
                   key: A, val: int) =
   var h: THash = hash(key) and high(data)
@@ -945,6 +965,10 @@ proc hasKey*[A](t: CountTableRef[A], key: A): bool =
   ## returns true iff `key` is in the table `t`.
   result = t[].hasKey(key)
 
+proc contains*[A](t: CountTableRef[A], key: A): bool =
+  ## alias of `hasKey` for use with the `in` operator.
+  return hasKey[A](t, key)
+
 proc `[]=`*[A](t: CountTableRef[A], key: A, val: int) =
   ## puts a (key, value)-pair into `t`. `val` has to be positive.
   assert val > 0