Skip to content

Commit

Permalink
Rewrite Interval[E]{a, b} -> Range(a, b)
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Jan 21, 2024
1 parent a891cd8 commit 7ce4e11
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
8 changes: 4 additions & 4 deletions difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ func difference[E Elem[E]](x, y, out Set[E]) Set[E] {
switch lo := x[0].Low; lo.Compare(r.Low) {
case -1:
if !inv {
z = append(z, Interval[E]{lo, r.Low})
z = append(z, Range(lo, r.Low))
}
case +1:
if inv {
z = append(z, Interval[E]{r.Low, lo})
z = append(z, Range(r.Low, lo))
}
}

if inv {
for i := 0; i < j-1; i++ {
z = append(z, Interval[E]{x[i].High, x[i+1].Low})
z = append(z, Range(x[i].High, x[i+1].Low))
}
}

Expand All @@ -82,7 +82,7 @@ func difference[E Elem[E]](x, y, out Set[E]) Set[E] {
switch hi.Compare(r.High) {
case -1:
if inv {
z = append(z, Interval[E]{hi, r.High})
z = append(z, Range(hi, r.High))
}
case +1:
r.Low, r.High = r.High, hi
Expand Down
7 changes: 2 additions & 5 deletions intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Interval[E Elem[E]] struct {
//
// If v is the maximum value of E, One returns an invalid Interval.
func One[E Enum[E]](v E) Interval[E] {
return Interval[E]{v, v.Next()}
return Range(v, v.Next())
}

// Range returns an Interval of range [lo, hi).
Expand Down Expand Up @@ -290,8 +290,5 @@ func (x Set[E]) Extent() Interval[E] {
return Interval[E]{}
}

return Interval[E]{
Low: x[0].Low,
High: x[len(x)-1].High,
}
return Range(x[0].Low, x[len(x)-1].High)
}
2 changes: 1 addition & 1 deletion intervalset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestExtent(t *testing.T) {
}{
{
Set[E]{{1, 3}, {5, 7}}.Extent(),
Interval[E]{1, 7},
Range[E](1, 7),
},
{
Set[E]{}.Extent(),
Expand Down
8 changes: 4 additions & 4 deletions symdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ func symmetricDifference[E Elem[E]](x, y, out Set[E]) Set[E] {

switch lo := x[0].Low; lo.Compare(r.Low) {
case -1:
z = appendInterval(z, Interval[E]{lo, r.Low})
z = appendInterval(z, Range(lo, r.Low))
case +1:
z = appendInterval(z, Interval[E]{r.Low, lo})
z = appendInterval(z, Range(r.Low, lo))
}

for i := 0; i < j-1; i++ {
z = append(z, Interval[E]{x[i].High, x[i+1].Low})
z = append(z, Range(x[i].High, x[i+1].Low))
}

hi := x[j-1].High
x = x[j:]

switch hi.Compare(r.High) {
case -1:
z = append(z, Interval[E]{hi, r.High})
z = append(z, Range(hi, r.High))
case +1:
r.Low, r.High = r.High, hi
x, y = y, x
Expand Down

0 comments on commit 7ce4e11

Please sign in to comment.