From 455c3c19cac3e2964fb4c2e89409f5780b09cf0b Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sun, 13 Apr 2014 00:33:06 +0200 Subject: [PATCH] added mget for TSet --- lib/pure/collections/sets.nim | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index ad3fe72183..bc249ed635 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)