From 67c1266e5f4269e7de58aee1261e15610685c766 Mon Sep 17 00:00:00 2001 From: Patrick Jakubowski Date: Tue, 24 Sep 2024 15:41:47 -0700 Subject: [PATCH] internal/mapset: reduce the functionality of Intersection to just a boolean Intersects Signed-off-by: Patrick Jakubowski --- internal/eval/evalers.go | 2 +- internal/mapset/immutable.go | 6 +++--- internal/mapset/immutable_test.go | 6 ++---- internal/mapset/mapset.go | 9 ++++----- internal/mapset/mapset_test.go | 12 ++---------- 5 files changed, 12 insertions(+), 23 deletions(-) diff --git a/internal/eval/evalers.go b/internal/eval/evalers.go index 6783666..bb864aa 100644 --- a/internal/eval/evalers.go +++ b/internal/eval/evalers.go @@ -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 { diff --git a/internal/mapset/immutable.go b/internal/mapset/immutable.go index 3be21d9..357c1d9 100644 --- a/internal/mapset/immutable.go +++ b/internal/mapset/immutable.go @@ -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. diff --git a/internal/mapset/immutable_test.go b/internal/mapset/immutable_test.go index 3baf661..fc3aaee 100644 --- a/internal/mapset/immutable_test.go +++ b/internal/mapset/immutable_test.go @@ -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) { diff --git a/internal/mapset/mapset.go b/internal/mapset/mapset.go index 09965d2..91c4ea5 100644 --- a/internal/mapset/mapset.go +++ b/internal/mapset/mapset.go @@ -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. diff --git a/internal/mapset/mapset_test.go b/internal/mapset/mapset_test.go index 1b23ed6..21d43df 100644 --- a/internal/mapset/mapset_test.go +++ b/internal/mapset/mapset_test.go @@ -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) {