diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | immutable.go | 10 | ||||
-rw-r--r-- | sets.go | 32 | ||||
-rw-r--r-- | sets_test.go | 1 |
4 files changed, 17 insertions, 30 deletions
@@ -286,8 +286,8 @@ Please see the internal `defaultComparer` for an example, bearing in mind that i ## Set -The `Set` represents a collection of unique values. It uses a `map[T]struct{}`, so it carries over some characteristics from the built-in Go `map` type. -Values neeed to be `comparable`. +The `Set` represents a collection of unique values, and it is implemented as a +wrapper around a `Map[T, struct{}]`. Like Maps, Sets require a `Hasher` to hash keys and check for equality. There are built-in hasher implementations for most primitive types such as `int`, `uint`, and diff --git a/immutable.go b/immutable.go index b3d7dcb..4a42e33 100644 --- a/immutable.go +++ b/immutable.go @@ -757,11 +757,10 @@ func (m *Map[K, V]) Set(key K, value V) *Map[K, V] { // This function will return a new map even if the updated value is the same as // the existing value because Map does not track value equality. func (m *Map[K, V]) SetMany(entries map[K]V) *Map[K, V] { - n := m.clone() for k, v := range entries { - n.set(k, v, true) + m = m.Set(k, v) } - return n + return m } func (m *Map[K, V]) set(key K, value V, mutable bool) *Map[K, V] { @@ -1644,11 +1643,10 @@ func (m *SortedMap[K, V]) Set(key K, value V) *SortedMap[K, V] { // SetMany returns a map with the keys set to the new values. func (m *SortedMap[K, V]) SetMany(entries map[K]V) *SortedMap[K, V] { - n := m.clone() for k, v := range entries { - n.set(k, v, true) + m = m.Set(k, v) } - return n + return m } func (m *SortedMap[K, V]) set(key K, value V, mutable bool) *SortedMap[K, V] { @@ -18,7 +18,7 @@ func NewSet[T comparable](hasher Hasher[T], values ...T) Set[T] { m: NewMap[T, struct{}](hasher), } for _, value := range values { - s.m.set(value, struct{}{}, true) + s.m = s.m.set(value, struct{}{}, true) } return s } @@ -27,24 +27,18 @@ func NewSet[T comparable](hasher Hasher[T], values ...T) Set[T] { // // 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(), - } for _, value := range values { - n.m.set(value, struct{}{}, true) + s.m = s.m.Set(value, struct{}{}) } - return n + return s } // 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(), - } for _, value := range values { - n.m.delete(value, true) + s.m = s.m.Delete(value) } - return n + return s } // Has returns true when the set contains the given value @@ -137,7 +131,7 @@ func NewSortedSet[T comparable](comparer Comparer[T], values ...T) SortedSet[T] m: NewSortedMap[T, struct{}](comparer), } for _, value := range values { - s.m.set(value, struct{}{}, true) + s.m = s.m.set(value, struct{}{}, true) } return s } @@ -146,24 +140,18 @@ func NewSortedSet[T comparable](comparer Comparer[T], values ...T) SortedSet[T] // // 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(), - } for _, value := range values { - n.m.set(value, struct{}{}, true) + s.m = s.m.Set(value, struct{}{}) } - return n + return s } // 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(), - } for _, value := range values { - n.m.delete(value, true) + s.m = s.m.Delete(value) } - return n + return s } // Has returns true when the set contains the given value diff --git a/sets_test.go b/sets_test.go index b3900fc..5a83eb9 100644 --- a/sets_test.go +++ b/sets_test.go @@ -7,6 +7,7 @@ import ( func TestSetsPut(t *testing.T) { s := NewSet[string](nil) s2 := s.Set("1").Set("1") + s2.Set("2") if s.Len() != 0 { t.Fatalf("Unexpected mutation of set") } |