diff options
author | Oskar Haarklou Veileborg <ohv1020@hotmail.com> | 2023-01-12 10:22:46 +0100 |
---|---|---|
committer | Oskar Haarklou Veileborg <ohv1020@hotmail.com> | 2023-01-12 10:25:31 +0100 |
commit | a74b83e3f275e44f686066081c7fdb665180ff9e (patch) | |
tree | 1b38c8dd10962017a600b46d704b11cde3ba7c00 /sets.go | |
parent | Ensure immutability of sets (and maps with SetMany) (diff) | |
download | pds-a74b83e3f275e44f686066081c7fdb665180ff9e.tar.gz pds-a74b83e3f275e44f686066081c7fdb665180ff9e.tar.xz |
sets & maps: remove varargs APIs & SetMany variants
The implementation behind the API is not more efficient than manually
looping over a data structure and inserting elements one-by-one.
Diffstat (limited to 'sets.go')
-rw-r--r-- | sets.go | 48 |
1 files changed, 16 insertions, 32 deletions
@@ -14,31 +14,23 @@ type Set[T comparable] struct { // 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), - } + m := NewMap[T, struct{}](hasher) for _, value := range values { - s.m = s.m.set(value, struct{}{}, true) + m = m.set(value, struct{}{}, true) } - return s + return Set[T]{m} } -// Set returns a set containing the new value. +// Add 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] { - for _, value := range values { - s.m = s.m.Set(value, struct{}{}) - } - return s +func (s Set[T]) Add(value T) Set[T] { + return Set[T]{s.m.Set(value, struct{}{})} } // Delete returns a set with the given key removed. -func (s Set[T]) Delete(values ...T) Set[T] { - for _, value := range values { - s.m = s.m.Delete(value) - } - return s +func (s Set[T]) Delete(value T) Set[T] { + return Set[T]{s.m.Delete(value)} } // Has returns true when the set contains the given value @@ -127,31 +119,23 @@ type SortedSet[T comparable] struct { // 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), - } + m := NewSortedMap[T, struct{}](comparer) for _, value := range values { - s.m = s.m.set(value, struct{}{}, true) + m = m.set(value, struct{}{}, true) } - return s + return SortedSet[T]{m} } -// Set returns a set containing the new value. +// Add 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] { - for _, value := range values { - s.m = s.m.Set(value, struct{}{}) - } - return s +func (s SortedSet[T]) Add(value T) SortedSet[T] { + return SortedSet[T]{s.m.Set(value, struct{}{})} } // Delete returns a set with the given key removed. -func (s SortedSet[T]) Delete(values ...T) SortedSet[T] { - for _, value := range values { - s.m = s.m.Delete(value) - } - return s +func (s SortedSet[T]) Delete(value T) SortedSet[T] { + return SortedSet[T]{s.m.Delete(value)} } // Has returns true when the set contains the given value |