refactor(set): pop method randomly removes an element and return (#202)

This commit is contained in:
Cannian authored and GitHub committed 2024-03-17 10:28:25 +08:00
1 parent 004dbdc32e
commit 2b765b49e0
2 files changed
+28 -18

No files matched your search

+5 -4
View File
@@ -187,10 +187,11 @@ func (s Set[T]) EachWithBreak(iteratee func(item T) bool) {
// Pop delete the top element of set then return it, if set is empty, return nil-value of T and false.
func (s Set[T]) Pop() (v T, ok bool) {
if len(s) > 0 {
items := s.Values()
item := items[len(s)-1]
delete(s, item)
return item, true
for item := range s {
v = item
delete(s, item)
return v, true
}
}
return v, false