aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAm Laher <amir.laher@tixtrack.com>2022-12-28 21:49:50 +1300
committerAm Laher <amir.laher@tixtrack.com>2022-12-28 21:49:50 +1300
commit90fd331c75708d076d6e347294e06b244214db2b (patch)
tree11208329fc4edf7c9b7167a1e7148b9e4ca69864
parentSortedMap: set multiple items at once (diff)
downloadpds-90fd331c75708d076d6e347294e06b244214db2b.tar.gz
pds-90fd331c75708d076d6e347294e06b244214db2b.tar.xz
set/sorted-set: Items() to return slice of items
-rw-r--r--sets.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/sets.go b/sets.go
index b333bb8..fc30ffb 100644
--- a/sets.go
+++ b/sets.go
@@ -58,6 +58,17 @@ func (s Set[K]) Len() int {
return s.m.Len()
}
+// Items returns a slice of the items inside the set
+func (s Set[T]) Items() []T {
+ r := make([]T, 0, s.Len())
+ itr := s.Iterator()
+ for !itr.Done() {
+ v, _ := itr.Next()
+ r = append(r, v)
+ }
+ return r
+}
+
// Iterator returns a new iterator for this set positioned at the first value.
func (s Set[T]) Iterator() *SetIterator[T] {
itr := &SetIterator[T]{mi: s.m.Iterator()}
@@ -166,6 +177,17 @@ func (s SortedSet[K]) Len() int {
return s.m.Len()
}
+// Items returns a slice of the items inside the set
+func (s SortedSet[T]) Items() []T {
+ r := make([]T, 0, s.Len())
+ itr := s.Iterator()
+ for !itr.Done() {
+ v, _ := itr.Next()
+ r = append(r, v)
+ }
+ return r
+}
+
// Iterator returns a new iterator for this set positioned at the first value.
func (s SortedSet[T]) Iterator() *SortedSetIterator[T] {
itr := &SortedSetIterator[T]{mi: s.m.Iterator()}