Skip to content

Commit

Permalink
implement IsDisjoint()
Browse files Browse the repository at this point in the history
  • Loading branch information
symonk committed Jun 29, 2024
1 parent 9770df6 commit a02fcd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hashset.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ func (s *Set[T]) Pop() (T, error) {
var falsy T
return falsy, ErrPopFromEmptySet
}

// IsDisjoint returns true if this set and other
// have a null intersection.
func (s *Set[T]) IsDisjoint(other *Set[T]) bool {
for element := range other.store {
if s.Contains(element) {
return false
}
}
return true
}
11 changes: 11 additions & 0 deletions hashset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ func TestPopRemovesARandomElement(t *testing.T) {
assert.ErrorIs(t, err, ErrPopFromEmptySet)
assert.ErrorContains(t, err, "pop from an empty set")
}

func TestIsNotDisjoint(t *testing.T) {
a := New(1, 2, 3, 4, 5)
b := New(5, 4, 3, 2, 1)
isDisjoint := a.IsDisjoint(b)
assert.False(t, isDisjoint)
}

func TestIsDisjoint(t *testing.T) {
assert.True(t, New(1, 2, 3).IsDisjoint(New(4, 5, 6)))
}

0 comments on commit a02fcd0

Please sign in to comment.