Skip to content

Commit

Permalink
Add method ContainsUnit to type Set
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Feb 2, 2024
1 parent 16e8728 commit a37dbc0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ func (x Set[E]) Contains(r Interval[E]) bool {
return len(x) != 0 && x[0].Low.Compare(r.Low) <= 0 && x[0].High.Compare(r.High) >= 0 && r.Low.Compare(r.High) < 0
}

// ContainsUnit reports whether x contains a single element v.
// Faster than x.Contains(Unit(v)).
func (x Set[E]) ContainsUnit(v E) bool {
x = x[sort.Search(len(x), func(i int) bool { return x[i].High.Compare(v) > 0 }):]
return len(x) != 0 && x[0].Low.Compare(v) <= 0
}

// Equal reports whether x is identical to y.
func (x Set[E]) Equal(y Set[E]) bool {
return slices.EqualFunc(x, y, Interval[E].Equal)
Expand Down
16 changes: 8 additions & 8 deletions intervalset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ func TestContains(t *testing.T) {
s := Set[E]{{1, 3}, {5, 7}}

assertions := []bool{
s.Contains(Unit[E](0)) == false,
s.Contains(Unit[E](1)) == true,
s.Contains(Unit[E](2)) == true,
s.Contains(Unit[E](3)) == false,
s.Contains(Unit[E](4)) == false,
s.Contains(Unit[E](5)) == true,
s.Contains(Unit[E](6)) == true,
s.Contains(Unit[E](7)) == false,
s.ContainsUnit(0) == false,
s.ContainsUnit(1) == true,
s.ContainsUnit(2) == true,
s.ContainsUnit(3) == false,
s.ContainsUnit(4) == false,
s.ContainsUnit(5) == true,
s.ContainsUnit(6) == true,
s.ContainsUnit(7) == false,
s.Contains(Range[E](1, 3)) == true,
s.Contains(Range[E](3, 5)) == false,
s.Contains(Range[E](5, 7)) == true,
Expand Down

0 comments on commit a37dbc0

Please sign in to comment.