summary refs log tree commit diff stats
path: root/lib/pure/collections/sets.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <ar@kimeta.de>2014-04-13 00:33:06 +0200
committerAndreas Rumpf <ar@kimeta.de>2014-04-13 00:33:06 +0200
commit455c3c19cac3e2964fb4c2e89409f5780b09cf0b (patch)
tree670f6fea94e432095d62312c6187aed88925831d /lib/pure/collections/sets.nim
parentf862e80be96289ad0f54c1d73a3e32734ed48cc3 (diff)
downloadNim-455c3c19cac3e2964fb4c2e89409f5780b09cf0b.tar.gz
added mget for TSet
Diffstat (limited to 'lib/pure/collections/sets.nim')
-rw-r--r--lib/pure/collections/sets.nim9
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim
index ad3fe7218..bc249ed63 100644
--- a/lib/pure/collections/sets.nim
+++ b/lib/pure/collections/sets.nim
@@ -68,6 +68,15 @@ template rawInsertImpl() {.dirty.} =
 proc rawGet[A](s: TSet[A], key: A): int =
   rawGetImpl()
 
+proc mget*[A](s: var TSet[A], key: A): var A =
+  ## returns the element that is actually stored in 's' which has the same
+  ## value as 'key' or raises the ``EInvalidKey`` exception. This is useful
+  ## when one overloaded 'hash' and '==' but still needs reference semantics
+  ## for sharing.
+  var index = rawGet(s, key)
+  if index >= 0: result = t.data[index].key
+  else: raise newException(EInvalidKey, "key not found: " & $key)
+
 proc contains*[A](s: TSet[A], key: A): bool =
   ## returns true iff `key` is in `s`.
   var index = rawGet(s, key)