From b95d2c9b7a98ac9efd694fc82dc9e3f7160410b4 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 1 Mar 2023 20:08:29 +0000 Subject: expose sorted sets builder --- sets.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'sets.go') diff --git a/sets.go b/sets.go index c8ce39f..b41bd37 100644 --- a/sets.go +++ b/sets.go @@ -194,13 +194,13 @@ func (itr *SortedSetIterator[T]) Next() (val T, ok bool) { return } -// Next moves the iterator to the previous value. +// Prev moves the iterator to the previous value. func (itr *SortedSetIterator[T]) Prev() (val T, ok bool) { val, _, ok = itr.mi.Prev() return } -// Next moves the iterator to the given value. +// Seek moves the iterator to the given value. // // If the value does not exist then the next value is used. If no more keys exist // then the iterator is marked as done. @@ -209,11 +209,12 @@ func (itr *SortedSetIterator[T]) Seek(val T) { } type SortedSetBuilder[T any] struct { - s SortedSet[T] + s *SortedSet[T] } func NewSortedSetBuilder[T any](comparer Comparer[T]) *SortedSetBuilder[T] { - return &SortedSetBuilder[T]{s: NewSortedSet(comparer)} + s := NewSortedSet(comparer) + return &SortedSetBuilder[T]{s: &s} } func (s SortedSetBuilder[T]) Set(val T) { @@ -231,3 +232,12 @@ func (s SortedSetBuilder[T]) Has(val T) bool { func (s SortedSetBuilder[T]) Len() int { return s.s.Len() } + +// SortedSet returns the current copy of the set. +// The builder should not be used again after the list after this call. +func (s SortedSetBuilder[T]) SortedSet() SortedSet[T] { + assert(s.s != nil, "immutable.SortedSetBuilder.SortedSet(): duplicate call to fetch sorted set") + set := s.s + s.s = nil + return *set +} -- cgit v1.2.3