diff options
author | Am Laher <amir.laher@tixtrack.com> | 2022-12-28 21:49:50 +1300 |
---|---|---|
committer | Am Laher <amir.laher@tixtrack.com> | 2022-12-28 21:49:50 +1300 |
commit | 90fd331c75708d076d6e347294e06b244214db2b (patch) | |
tree | 11208329fc4edf7c9b7167a1e7148b9e4ca69864 /sets.go | |
parent | SortedMap: set multiple items at once (diff) | |
download | pds-90fd331c75708d076d6e347294e06b244214db2b.tar.gz pds-90fd331c75708d076d6e347294e06b244214db2b.tar.xz |
set/sorted-set: Items() to return slice of items
Diffstat (limited to 'sets.go')
-rw-r--r-- | sets.go | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -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()} |