Skip to content

Commit

Permalink
Merge pull request #43 from strongdm/use-container-type-for-equal
Browse files Browse the repository at this point in the history
internal/mapset: relax the constraint on the argument to MapSet.Equal to just be a Container instance
  • Loading branch information
patjakdev authored Sep 30, 2024
2 parents 609f098 + 49348da commit 8b860c7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
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

0 comments on commit 8b860c7

Please sign in to comment.