Skip to content

Commit

Permalink
Make method Set inlinable to avoid an allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Jan 25, 2024
1 parent a8777fa commit 7359e13
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,34 @@ func (r Interval[E]) Equal(r2 Interval[E]) bool {
// A valid Interval r either satisfies r.Low.Compare(r.High) < 0 or equals to
// the zero value.
func (r Interval[E]) IsValid() bool {
c := r.Low.Compare(r.High)
return c < 0 || c == 0 && r.Equal(Interval[E]{})
return r.cmp() <= 0
}

// Set returns the set of elements that are in r.
// If r is the zero value, Set returns an empty set.
// If r is an invalid Interval, Set panics.
func (r Interval[E]) Set() Set[E] {
switch c := r.Low.Compare(r.High); {
case c < 0:
switch r.cmp() {
case -1:
return Set[E]{r}
case c == 0 && r.Equal(Interval[E]{}):
case 0:
return nil
}

panic("invalid Interval")
}

func (r Interval[E]) cmp() int {
switch c := r.Low.Compare(r.High); {
case c < 0:
return -1
case c == 0 && r.Equal(Interval[E]{}):
return 0
default:
return +1
}
}

// A Set is a slice of separate intervals sorted in ascending order.
// The zero value for a Set, i.e. a nil Set, is an empty set.
//
Expand Down

0 comments on commit 7359e13

Please sign in to comment.