Skip to content

Commit

Permalink
Refactor method AddRange and DeleteRange
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Jan 21, 2024
1 parent 2e5127d commit a891cd8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 86 deletions.
54 changes: 16 additions & 38 deletions intervalset.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Package intervals is a library for manipulating sets of intervals.
package intervals

import "sort"
import (
"slices"
"sort"
)

// Elem is the type set containing all supported element types.
type Elem[E any] interface {
Expand Down Expand Up @@ -170,19 +173,13 @@ func (x *Set[E]) AddRange(lo, hi E) {
}
}

if i == j { // Case 3 (where lo and hi overlap with each other).
if lo.Compare(hi) < 0 {
s = append(s, Interval[E]{})
copy(s[i+1:], s[i:])
s[i] = Interval[E]{lo, hi}
*x = s
if i == j { // Case 3 (where lo and hi overlap).
if lo.Compare(hi) >= 0 { // Get rid of the devil first.
return
}

return
}

s[i] = Interval[E]{lo, hi}
s = append(s[:i+1], s[j:]...)
s = slices.Replace(s, i, j, Range(lo, hi))
*x = s
}

Expand Down Expand Up @@ -232,43 +229,24 @@ func (x *Set[E]) DeleteRange(lo, hi E) {
}

if i == j-1 { // Case 4.
if r := &s[i]; r.Low.Compare(lo) < 0 {
if r.High.Compare(hi) > 0 {
if lo.Compare(hi) < 0 {
s = append(s, Interval[E]{})
copy(s[j:], s[i:])
s[i].High = lo
s[j].Low = hi
*x = s
}
} else {
r.High = lo
}
} else {
if r.High.Compare(hi) > 0 {
r.Low = hi
} else {
s = append(s[:i], s[j:]...)
*x = s
}
if lo.Compare(hi) >= 0 { // Get rid of the devil first.
return
}

return
}

// Case 5.
// Case 4 and 5.

v := make([]Interval[E], 0, 2)

if r := &s[i]; r.Low.Compare(lo) < 0 {
r.High = lo
i++
v = append(v, Range(r.Low, lo))
}

if r := &s[j-1]; r.High.Compare(hi) > 0 {
r.Low = hi
j--
v = append(v, Range(hi, r.High))
}

s = append(s[:i], s[j:]...)
s = slices.Replace(s, i, j, v...)
*x = s
}

Expand Down
76 changes: 28 additions & 48 deletions intervalset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func TestCreation(t *testing.T) {
testCases := []struct {
Actual, Expected Set[E]
}{
{
One[E](1).Set(),
Set[E]{{1, 2}},
},
{
Range[E](1, 5).Set(),
Set[E]{{1, 5}},
Expand Down Expand Up @@ -67,36 +71,28 @@ func TestAdd(t *testing.T) {
Actual, Expected Set[E]
}{
{
add(Set[E]{{1, 4}, {9, 12}}, Range[E](5, 8)),
Set[E]{{1, 4}, {5, 8}, {9, 12}},
},
{
add(Set[E]{{1, 4}, {9, 12}}, One[E](6)),
Set[E]{{1, 4}, {6, 7}, {9, 12}},
},
{
add(Set[E]{{1, 4}, {9, 12}}, Range[E](4, 8)),
Set[E]{{1, 8}, {9, 12}},
add(Set[E]{{1, 5}, {11, 15}}, Range[E](5, 11)),
Set[E]{{1, 15}},
},
{
add(Set[E]{{1, 4}, {9, 12}}, Range[E](5, 9)),
Set[E]{{1, 4}, {5, 12}},
add(Set[E]{{1, 5}, {11, 15}}, Range[E](5, 9)),
Set[E]{{1, 9}, {11, 15}},
},
{
add(Set[E]{{1, 4}, {9, 12}}, Range[E](4, 9)),
Set[E]{{1, 12}},
add(Set[E]{{1, 5}, {11, 15}}, Range[E](7, 11)),
Set[E]{{1, 5}, {7, 15}},
},
{
add(Set[E]{{1, 4}, {9, 12}}, One[E](10)),
Set[E]{{1, 4}, {9, 12}},
add(Set[E]{{1, 5}, {11, 15}}, Range[E](7, 9)),
Set[E]{{1, 5}, {7, 9}, {11, 15}},
},
{
add(Set[E]{{1, 4}, {9, 12}}, Range[E](9, 12)),
Set[E]{{1, 4}, {9, 12}},
add(Set[E]{{1, 5}, {11, 15}}, Range[E](9, 7)),
Set[E]{{1, 5}, {11, 15}},
},
{
add(Set[E]{{1, 4}, {9, 12}}, Range[E](12, 9)),
Set[E]{{1, 4}, {9, 12}},
add(Set[E]{{1, 5}, {11, 15}}, Range[E](15, 11)),
Set[E]{{1, 5}, {11, 15}},
},
}

Expand All @@ -120,44 +116,28 @@ func TestDelete(t *testing.T) {
Actual, Expected Set[E]
}{
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](7, 10)),
Set[E]{{1, 4}, {13, 16}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](7, 9)),
Set[E]{{1, 4}, {9, 10}, {13, 16}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](8, 10)),
Set[E]{{1, 4}, {7, 8}, {13, 16}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, One[E](8)),
Set[E]{{1, 4}, {7, 8}, {9, 10}, {13, 16}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](1, 16)),
Set[E]{},
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](5, 11)),
Set[E]{{1, 3}, {13, 15}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](1, 15)),
Set[E]{{15, 16}},
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](5, 9)),
Set[E]{{1, 3}, {9, 11}, {13, 15}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](2, 16)),
Set[E]{{1, 2}},
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](7, 11)),
Set[E]{{1, 3}, {5, 7}, {13, 15}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, One[E](5)),
Set[E]{{1, 4}, {7, 10}, {13, 16}},
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](7, 9)),
Set[E]{{1, 3}, {5, 7}, {9, 11}, {13, 15}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](4, 7)),
Set[E]{{1, 4}, {7, 10}, {13, 16}},
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](9, 7)),
Set[E]{{1, 3}, {5, 11}, {13, 15}},
},
{
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](7, 4)),
Set[E]{{1, 4}, {7, 10}, {13, 16}},
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](5, 3)),
Set[E]{{1, 3}, {5, 11}, {13, 15}},
},
}

Expand Down

0 comments on commit a891cd8

Please sign in to comment.