mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
feat: add Pop for set
This commit is contained in:
1 parent
70d0adde42
commit
965e5fbcda
4 files changed
+256
-162
No files matched your search
@@ -181,3 +181,15 @@ 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
|
||||
}
|
||||
|
||||
return v, false
|
||||
}
|
||||
Reference in new issue
Block a user