Skip to content

Commit

Permalink
IsSuperSet()
Browse files Browse the repository at this point in the history
  • Loading branch information
symonk committed Jun 30, 2024
1 parent ea61bb9 commit 6ff9ed0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion hashset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Difference()
DifferenceUpdate()
Intersection()
IntersectionUpdate()
IsSuperset()
SymmetricDifference()
SymmetricDifferenceUpdate()
Union()
Expand Down Expand Up @@ -107,6 +106,7 @@ func (s *Set[T]) IsDisjoint(other *Set[T]) bool {

// IsSubset tests whether every element in this set
// is contained in the other set.
// empty sets are considered subsets of each other.
func (s *Set[T]) IsSubset(other *Set[T]) bool {
if other.Len() < s.Len() {
return false
Expand All @@ -120,3 +120,10 @@ func (s *Set[T]) IsSubset(other *Set[T]) bool {
return true

}

// IsSuperset tests whether every element in other is
// contained within this set.
// empty sets are considered supersets of each other.
func (s *Set[T]) IsSuperSet(other *Set[T]) bool {
return other.IsSubset(s)
}
21 changes: 21 additions & 0 deletions hashset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,24 @@ func TestIsSubsetNo(t *testing.T) {
func TestEmptySetSubset(t *testing.T) {
assert.True(t, New[int](0).IsSubset(New[int](0)))
}

func TestIsSuperSetYes(t *testing.T) {
a := New(3, 1, 2, 3)
b := New(3, 3, 2, 1)
assert.True(t, a.IsSuperSet(b))
assert.True(t, b.IsSuperSet(a))

c := New(3, 1, 2, 3, 4, 5)
d := New(3, 2, 3, 4)
assert.True(t, c.IsSuperSet(d))
}

func TestIsSuperSetNo(t *testing.T) {
a := New(3, 1, 2, 3)
b := New(3, 2, 3)
assert.False(t, b.IsSuperSet(a))
}

func TestEmptySetSuperset(t *testing.T) {
assert.True(t, New[string](0).IsSuperSet(New[string](0)))
}

0 comments on commit 6ff9ed0

Please sign in to comment.