From 5d10a4dcbb8986a12aae62c099494f7fbf277af7 Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Fri, 24 Jan 2025 16:25:39 -0300 Subject: [PATCH 1/3] added isSubset --- datastructures/set/set.go | 9 +++++++++ datastructures/set/set_test.go | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/datastructures/set/set.go b/datastructures/set/set.go index 5c903f0..5c2ce05 100644 --- a/datastructures/set/set.go +++ b/datastructures/set/set.go @@ -83,3 +83,12 @@ func (s *Set[T]) ToSlice() []T { }) return tr } + +func (s *Set[T]) IsSubSet(other Set[T]) bool { + for k := range s.data { + if !other.Contains(k) { + return false + } + } + return true +} diff --git a/datastructures/set/set_test.go b/datastructures/set/set_test.go index 86ee0b4..19ba3b3 100644 --- a/datastructures/set/set_test.go +++ b/datastructures/set/set_test.go @@ -38,4 +38,9 @@ func TestSet(t *testing.T) { n := New[int](4) n.AddFromSlice(asSlice) assert.Equal(t, Define(5, 10, 20, 30), n) + + e := New[int](10) + e.Add(1, 2, 3) + assert.True(t, e.IsSubSet(Define(1, 2, 3, 4, 5, 6, 7, 8, 9))) + assert.False(t, e.IsSubSet(Define(1, 2))) } From 359150149030b2d507a8d168ab5dc6037d6d6cba Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Fri, 24 Jan 2025 17:12:59 -0300 Subject: [PATCH 2/3] compatibility with isSubset on previous version --- datastructures/set/set.go | 5 +++-- datastructures/set/set_test.go | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/datastructures/set/set.go b/datastructures/set/set.go index 5c2ce05..a911d95 100644 --- a/datastructures/set/set.go +++ b/datastructures/set/set.go @@ -84,9 +84,10 @@ func (s *Set[T]) ToSlice() []T { return tr } +// IsSubset returns true if the passed set is a subset of this one func (s *Set[T]) IsSubSet(other Set[T]) bool { - for k := range s.data { - if !other.Contains(k) { + for k := range other.data { + if !s.Contains(k) { return false } } diff --git a/datastructures/set/set_test.go b/datastructures/set/set_test.go index 19ba3b3..6b0c9d4 100644 --- a/datastructures/set/set_test.go +++ b/datastructures/set/set_test.go @@ -40,7 +40,7 @@ func TestSet(t *testing.T) { assert.Equal(t, Define(5, 10, 20, 30), n) e := New[int](10) - e.Add(1, 2, 3) - assert.True(t, e.IsSubSet(Define(1, 2, 3, 4, 5, 6, 7, 8, 9))) - assert.False(t, e.IsSubSet(Define(1, 2))) + e.Add(1, 2, 3, 4, 5, 6, 7, 8, 9) + assert.True(t, e.IsSubSet(Define(1, 2, 3))) + assert.False(t, e.IsSubSet(Define(1, 2, 10))) } From 8dd9162716c6f1eb9d7e906e2c77ef81ceae2a15 Mon Sep 17 00:00:00 2001 From: Matias Melograno Date: Fri, 24 Jan 2025 17:25:57 -0300 Subject: [PATCH 3/3] added isequal --- datastructures/set/set.go | 13 +++++++++++++ datastructures/set/set_test.go | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/datastructures/set/set.go b/datastructures/set/set.go index a911d95..6bb8388 100644 --- a/datastructures/set/set.go +++ b/datastructures/set/set.go @@ -93,3 +93,16 @@ func (s *Set[T]) IsSubSet(other Set[T]) bool { } return true } + +// IsEqual returns true if the passed set is equal to this one +func (s *Set[T]) IsEqual(other Set[T]) bool { + if s.Len() != other.Len() { + return false + } + for k := range s.data { + if !other.Contains(k) { + return false + } + } + return true +} diff --git a/datastructures/set/set_test.go b/datastructures/set/set_test.go index 6b0c9d4..e317e08 100644 --- a/datastructures/set/set_test.go +++ b/datastructures/set/set_test.go @@ -43,4 +43,10 @@ func TestSet(t *testing.T) { e.Add(1, 2, 3, 4, 5, 6, 7, 8, 9) assert.True(t, e.IsSubSet(Define(1, 2, 3))) assert.False(t, e.IsSubSet(Define(1, 2, 10))) + + k := New[int](10) + k.Add(1, 2, 3, 4, 5, 6, 7, 8, 9) + assert.False(t, k.IsEqual(Define(1, 2, 3))) + assert.True(t, k.IsEqual(Define(1, 2, 3, 4, 5, 6, 7, 8, 9))) + assert.False(t, k.IsEqual(Define(1, 2, 3, 4, 5, 6, 7, 8, 10))) }