Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/mapset: relax the constraint on the argument to MapSet.Equal to just be a Container instance #43

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions internal/mapset/immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions internal/mapset/immutable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions internal/mapset/mapset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
Loading