summary refs log tree commit diff stats
path: root/lib/pure/collections
diff options
context:
space:
mode:
authorskilchen <skilchen@users.noreply.github.com>2018-07-21 19:51:14 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-07-21 19:51:14 +0200
commit8fe8bed9c3e94cf5f6068e652b99a050f84d6b3f (patch)
treea5731ad89b39208cff9268a3837ca0b8e2e498c5 /lib/pure/collections
parentf3926b5af1dc91573d5425698a9491204ae92e0a (diff)
downloadNim-8fe8bed9c3e94cf5f6068e652b99a050f84d6b3f.tar.gz
add sets.pop procedure (analogue to python) (#8383)
Diffstat (limited to 'lib/pure/collections')
-rw-r--r--lib/pure/collections/sets.nim12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim
index 59c90bc2b..fdc3b4b03 100644
--- a/lib/pure/collections/sets.nim
+++ b/lib/pure/collections/sets.nim
@@ -347,6 +347,18 @@ proc excl*[A](s: var HashSet[A], other: HashSet[A]) =
   assert other.isValid, "The set `other` needs to be initialized."
   for item in other: discard exclImpl(s, item)
 
+proc pop*[A](s: var HashSet[A]): A =
+  ## Remove and return an arbitrary element from the set `s`.
+  ##
+  ## Raises KeyError if the set `s` is empty.
+  ##
+  for h in 0..high(s.data):
+    if isFilled(s.data[h].hcode):
+      result = s.data[h].key
+      excl(s, result)
+      return result
+  raise newException(KeyError, "set is empty")
+
 proc containsOrIncl*[A](s: var HashSet[A], key: A): bool =
   ## Includes `key` in the set `s` and tells if `key` was added to `s`.
   ##