Skip to content

Commit

Permalink
internal/mapset: reduce the functionality of Intersection to just a b…
Browse files Browse the repository at this point in the history
…oolean Intersects

Signed-off-by: Patrick Jakubowski <[email protected]>
  • Loading branch information
patjakdev committed Sep 24, 2024
1 parent 99f7de5 commit 67c1266
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/eval/evalers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ func entityInSet(env *Env, entity types.EntityUID, parents mapset.Container[type
var candidate = entity
for {
if fe, ok := env.Entities[candidate]; ok {
if fe.Parents.Intersection(parents).Len() > 0 {
if fe.Parents.Intersects(parents) {
return true
}
fe.Parents.Iterate(func(k types.EntityUID) bool {
Expand Down
6 changes: 3 additions & 3 deletions internal/mapset/immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func (h ImmutableMapSet[T]) Contains(item T) bool {
return MapSet[T](h).Contains(item)
}

// Intersection returns the items common to both h and o.
func (h ImmutableMapSet[T]) Intersection(o Container[T]) ImmutableMapSet[T] {
return ImmutableMapSet[T](*MapSet[T](h).Intersection(o))
// Intersects returns whether any items in this set exist in o
func (h ImmutableMapSet[T]) Intersects(o Container[T]) bool {
return MapSet[T](h).Intersects(o)
}

// Iterate the items in the set, calling callback for each item. If the callback returns false, iteration is halted.
Expand Down
6 changes: 2 additions & 4 deletions internal/mapset/immutable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,14 @@ func TestImmutableHashSet(t *testing.T) {
s1 := Immutable(1, 2, 3)
s2 := Immutable(2, 3, 4)

s3 := s1.Intersection(s2)
testutil.Equals(t, s3, Immutable(2, 3))
testutil.Equals(t, s1.Intersects(s2), true)
})

t.Run("intersection disjoint", func(t *testing.T) {
s1 := Immutable(1, 2)
s2 := Immutable(3, 4)

s3 := s1.Intersection(s2)
testutil.Equals(t, s3.Len(), 0)
testutil.Equals(t, s1.Intersects(s2), false)
})

t.Run("encode nil set", func(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions internal/mapset/mapset.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ type Container[T comparable] interface {
Contains(T) bool
}

// Intersection returns the items common to both h and o.
func (h MapSet[T]) Intersection(o Container[T]) *MapSet[T] {
intersection := Make[T]()
// Intersects returns whether any items in this set exist in o
func (h MapSet[T]) Intersects(o Container[T]) bool {
for item := range h.m {
if o.Contains(item) {
intersection.Add(item)
return true
}
}
return intersection
return false
}

// Iterate the items in the set, calling callback for each item. If the callback returns false, iteration is halted.
Expand Down
12 changes: 2 additions & 10 deletions internal/mapset/mapset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,14 @@ func TestHashSet(t *testing.T) {
s1 := FromItems(1, 2, 3)
s2 := FromItems(2, 3, 4)

s3 := s1.Intersection(s2)
testutil.Equals(t, s3, FromItems(2, 3))

s4 := s1.Intersection(s2)
testutil.Equals(t, s4, FromItems(2, 3))
testutil.Equals(t, s1.Intersects(s2), true)
})

t.Run("intersection disjoint", func(t *testing.T) {
s1 := FromItems(1, 2)
s2 := FromItems(3, 4)

s3 := s1.Intersection(s2)
testutil.Equals(t, s3.Len(), 0)

s4 := s1.Intersection(s2)
testutil.Equals(t, s4.Len(), 0)
testutil.Equals(t, s1.Intersects(s2), false)
})

t.Run("encode nil set", func(t *testing.T) {
Expand Down

0 comments on commit 67c1266

Please sign in to comment.