diff options
Diffstat (limited to 'sets.go')
-rw-r--r-- | sets.go | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -1,9 +1,18 @@ package immutable +// Set represents a collection of unique values. The set uses a Hasher +// to generate hashes and check for equality of key values. +// +// Internally, the Set stores values as keys of a Map[T,struct{}] type Set[T comparable] struct { m *Map[T, struct{}] } +// NewSet returns a new instance of Set. +// +// If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. +// Default hasher implementations only exist for int, string, and byte slice types. +// NewSet can also take some initial values as varargs. func NewSet[T comparable](hasher Hasher[T], values ...T) Set[T] { s := Set[T]{ m: NewMap[T, struct{}](hasher), @@ -14,6 +23,9 @@ func NewSet[T comparable](hasher Hasher[T], values ...T) Set[T] { return s } +// Set returns a set containing the new value. +// +// This function will return a new set even if the set already contains the value. func (s Set[T]) Set(values ...T) Set[T] { n := Set[T]{ m: s.m.clone(), @@ -24,6 +36,7 @@ func (s Set[T]) Set(values ...T) Set[T] { return n } +// Delete returns a set with the given key removed. func (s Set[T]) Delete(values ...T) Set[T] { n := Set[T]{ m: s.m.clone(), @@ -34,33 +47,41 @@ func (s Set[T]) Delete(values ...T) Set[T] { return n } +// Has returns true when the set contains the given value func (s Set[T]) Has(val T) bool { _, ok := s.m.Get(val) return ok } +// Len returns the number of elements in the underlying map. func (s Set[K]) Len() int { return s.m.Len() } +// 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()} itr.mi.First() return itr } +// SetIterator represents an iterator over a set. +// Iteration can occur in natural or reverse order based on use of Next() or Prev(). type SetIterator[T comparable] struct { mi *MapIterator[T, struct{}] } +// Done returns true if no more values remain in the iterator. func (itr *SetIterator[T]) Done() bool { return itr.mi.Done() } +// First moves the iterator to the first value. func (itr *SetIterator[T]) First() { itr.mi.First() } +// Next moves the iterator to the next value. func (itr *SetIterator[T]) Next() (val T, ok bool) { val, _, ok = itr.mi.Next() return @@ -94,6 +115,12 @@ type SortedSet[T comparable] struct { m *SortedMap[T, struct{}] } +// NewSortedSet returns a new instance of SortedSet. +// +// If comparer is nil then +// a default comparer is set after the first key is inserted. Default comparers +// exist for int, string, and byte slice keys. +// NewSortedSet can also take some initial values as varargs. func NewSortedSet[T comparable](comparer Comparer[T], values ...T) SortedSet[T] { s := SortedSet[T]{ m: NewSortedMap[T, struct{}](comparer), @@ -104,6 +131,9 @@ func NewSortedSet[T comparable](comparer Comparer[T], values ...T) SortedSet[T] return s } +// Set returns a set containing the new value. +// +// This function will return a new set even if the set already contains the value. func (s SortedSet[T]) Set(values ...T) SortedSet[T] { n := SortedSet[T]{ m: s.m.clone(), @@ -114,6 +144,7 @@ func (s SortedSet[T]) Set(values ...T) SortedSet[T] { return n } +// Delete returns a set with the given key removed. func (s SortedSet[T]) Delete(values ...T) SortedSet[T] { n := SortedSet[T]{ m: s.m.clone(), @@ -124,47 +155,61 @@ func (s SortedSet[T]) Delete(values ...T) SortedSet[T] { return n } +// Has returns true when the set contains the given value func (s SortedSet[T]) Has(val T) bool { _, ok := s.m.Get(val) return ok } +// Len returns the number of elements in the underlying map. func (s SortedSet[K]) Len() int { return s.m.Len() } +// 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()} itr.mi.First() return itr } +// SortedSetIterator represents an iterator over a sorted set. +// Iteration can occur in natural or reverse order based on use of Next() or Prev(). type SortedSetIterator[T comparable] struct { mi *SortedMapIterator[T, struct{}] } +// Done returns true if no more values remain in the iterator. func (itr *SortedSetIterator[T]) Done() bool { return itr.mi.Done() } +// First moves the iterator to the first value. func (itr *SortedSetIterator[T]) First() { itr.mi.First() } +// Last moves the iterator to the last value. func (itr *SortedSetIterator[T]) Last() { itr.mi.Last() } +// Next moves the iterator to the next value. func (itr *SortedSetIterator[T]) Next() (val T, ok bool) { val, _, ok = itr.mi.Next() return } +// Next 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. +// +// If the value does not exist then the next value is used. If no more keys exist +// then the iterator is marked as done. func (itr *SortedSetIterator[T]) Seek(val T) { itr.mi.Seek(val) } |