diff --git a/internal/mapset/immutable.go b/internal/mapset/immutable.go index 73b54a1..d80f8db 100644 --- a/internal/mapset/immutable.go +++ b/internal/mapset/immutable.go @@ -36,9 +36,8 @@ func (h ImmutableMapSet[T]) Len() int { } // Equal returns whether the same items exist in both h and o -func (h ImmutableMapSet[T]) Equal(o ImmutableMapSet[T]) bool { - om := MapSet[T](o) - return MapSet[T](h).Equal(&om) +func (h ImmutableMapSet[T]) Equal(o Container[T]) bool { + return MapSet[T](h).Equal(o) } // MarshalJSON serializes a MapSet as a JSON array. Elements are ordered lexicographically by their marshaled value. diff --git a/internal/mapset/immutable_test.go b/internal/mapset/immutable_test.go index 663b8d6..b58b716 100644 --- a/internal/mapset/immutable_test.go +++ b/internal/mapset/immutable_test.go @@ -60,6 +60,16 @@ func TestImmutableHashSet(t *testing.T) { testutil.Equals(t, s1.Equal(s3), false) }) + t.Run("equality of MapSet and ImmutableMapSet", func(t *testing.T) { + s1 := FromItems(1, 2, 3, 4) + s2 := Immutable(1, 2, 3, 4) + s3 := Immutable(1, 2, 3) + testutil.Equals(t, s1.Equal(s2), true) + testutil.Equals(t, s2.Equal(s1), true) + testutil.Equals(t, s1.Equal(s3), false) + testutil.Equals(t, s3.Equal(s1), false) + }) + t.Run("iterate", func(t *testing.T) { s1 := Immutable(1, 2, 3) diff --git a/internal/mapset/mapset.go b/internal/mapset/mapset.go index 8807064..40ed608 100644 --- a/internal/mapset/mapset.go +++ b/internal/mapset/mapset.go @@ -77,6 +77,7 @@ func (h MapSet[T]) Contains(item T) bool { type Container[T comparable] interface { Contains(T) bool + Len() int } // Intersects returns whether any items in this set exist in o @@ -112,8 +113,8 @@ func (h MapSet[T]) Len() int { } // Equal returns whether the same items exist in both h and o -func (h MapSet[T]) Equal(o *MapSet[T]) bool { - if len(h.m) != len(o.m) { +func (h MapSet[T]) Equal(o Container[T]) bool { + if len(h.m) != o.Len() { return false }