Skip to content

Commit a891cd8

Browse files
committed
Refactor method AddRange and DeleteRange
1 parent 2e5127d commit a891cd8

File tree

2 files changed

+44
-86
lines changed

2 files changed

+44
-86
lines changed

intervalset.go

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Package intervals is a library for manipulating sets of intervals.
22
package intervals
33

4-
import "sort"
4+
import (
5+
"slices"
6+
"sort"
7+
)
58

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

173-
if i == j { // Case 3 (where lo and hi overlap with each other).
174-
if lo.Compare(hi) < 0 {
175-
s = append(s, Interval[E]{})
176-
copy(s[i+1:], s[i:])
177-
s[i] = Interval[E]{lo, hi}
178-
*x = s
176+
if i == j { // Case 3 (where lo and hi overlap).
177+
if lo.Compare(hi) >= 0 { // Get rid of the devil first.
178+
return
179179
}
180-
181-
return
182180
}
183181

184-
s[i] = Interval[E]{lo, hi}
185-
s = append(s[:i+1], s[j:]...)
182+
s = slices.Replace(s, i, j, Range(lo, hi))
186183
*x = s
187184
}
188185

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

234231
if i == j-1 { // Case 4.
235-
if r := &s[i]; r.Low.Compare(lo) < 0 {
236-
if r.High.Compare(hi) > 0 {
237-
if lo.Compare(hi) < 0 {
238-
s = append(s, Interval[E]{})
239-
copy(s[j:], s[i:])
240-
s[i].High = lo
241-
s[j].Low = hi
242-
*x = s
243-
}
244-
} else {
245-
r.High = lo
246-
}
247-
} else {
248-
if r.High.Compare(hi) > 0 {
249-
r.Low = hi
250-
} else {
251-
s = append(s[:i], s[j:]...)
252-
*x = s
253-
}
232+
if lo.Compare(hi) >= 0 { // Get rid of the devil first.
233+
return
254234
}
255-
256-
return
257235
}
258236

259-
// Case 5.
237+
// Case 4 and 5.
238+
239+
v := make([]Interval[E], 0, 2)
260240

261241
if r := &s[i]; r.Low.Compare(lo) < 0 {
262-
r.High = lo
263-
i++
242+
v = append(v, Range(r.Low, lo))
264243
}
265244

266245
if r := &s[j-1]; r.High.Compare(hi) > 0 {
267-
r.Low = hi
268-
j--
246+
v = append(v, Range(hi, r.High))
269247
}
270248

271-
s = append(s[:i], s[j:]...)
249+
s = slices.Replace(s, i, j, v...)
272250
*x = s
273251
}
274252

intervalset_test.go

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ func TestCreation(t *testing.T) {
1313
testCases := []struct {
1414
Actual, Expected Set[E]
1515
}{
16+
{
17+
One[E](1).Set(),
18+
Set[E]{{1, 2}},
19+
},
1620
{
1721
Range[E](1, 5).Set(),
1822
Set[E]{{1, 5}},
@@ -67,36 +71,28 @@ func TestAdd(t *testing.T) {
6771
Actual, Expected Set[E]
6872
}{
6973
{
70-
add(Set[E]{{1, 4}, {9, 12}}, Range[E](5, 8)),
71-
Set[E]{{1, 4}, {5, 8}, {9, 12}},
72-
},
73-
{
74-
add(Set[E]{{1, 4}, {9, 12}}, One[E](6)),
75-
Set[E]{{1, 4}, {6, 7}, {9, 12}},
76-
},
77-
{
78-
add(Set[E]{{1, 4}, {9, 12}}, Range[E](4, 8)),
79-
Set[E]{{1, 8}, {9, 12}},
74+
add(Set[E]{{1, 5}, {11, 15}}, Range[E](5, 11)),
75+
Set[E]{{1, 15}},
8076
},
8177
{
82-
add(Set[E]{{1, 4}, {9, 12}}, Range[E](5, 9)),
83-
Set[E]{{1, 4}, {5, 12}},
78+
add(Set[E]{{1, 5}, {11, 15}}, Range[E](5, 9)),
79+
Set[E]{{1, 9}, {11, 15}},
8480
},
8581
{
86-
add(Set[E]{{1, 4}, {9, 12}}, Range[E](4, 9)),
87-
Set[E]{{1, 12}},
82+
add(Set[E]{{1, 5}, {11, 15}}, Range[E](7, 11)),
83+
Set[E]{{1, 5}, {7, 15}},
8884
},
8985
{
90-
add(Set[E]{{1, 4}, {9, 12}}, One[E](10)),
91-
Set[E]{{1, 4}, {9, 12}},
86+
add(Set[E]{{1, 5}, {11, 15}}, Range[E](7, 9)),
87+
Set[E]{{1, 5}, {7, 9}, {11, 15}},
9288
},
9389
{
94-
add(Set[E]{{1, 4}, {9, 12}}, Range[E](9, 12)),
95-
Set[E]{{1, 4}, {9, 12}},
90+
add(Set[E]{{1, 5}, {11, 15}}, Range[E](9, 7)),
91+
Set[E]{{1, 5}, {11, 15}},
9692
},
9793
{
98-
add(Set[E]{{1, 4}, {9, 12}}, Range[E](12, 9)),
99-
Set[E]{{1, 4}, {9, 12}},
94+
add(Set[E]{{1, 5}, {11, 15}}, Range[E](15, 11)),
95+
Set[E]{{1, 5}, {11, 15}},
10096
},
10197
}
10298

@@ -120,44 +116,28 @@ func TestDelete(t *testing.T) {
120116
Actual, Expected Set[E]
121117
}{
122118
{
123-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](7, 10)),
124-
Set[E]{{1, 4}, {13, 16}},
125-
},
126-
{
127-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](7, 9)),
128-
Set[E]{{1, 4}, {9, 10}, {13, 16}},
129-
},
130-
{
131-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](8, 10)),
132-
Set[E]{{1, 4}, {7, 8}, {13, 16}},
133-
},
134-
{
135-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, One[E](8)),
136-
Set[E]{{1, 4}, {7, 8}, {9, 10}, {13, 16}},
137-
},
138-
{
139-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](1, 16)),
140-
Set[E]{},
119+
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](5, 11)),
120+
Set[E]{{1, 3}, {13, 15}},
141121
},
142122
{
143-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](1, 15)),
144-
Set[E]{{15, 16}},
123+
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](5, 9)),
124+
Set[E]{{1, 3}, {9, 11}, {13, 15}},
145125
},
146126
{
147-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](2, 16)),
148-
Set[E]{{1, 2}},
127+
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](7, 11)),
128+
Set[E]{{1, 3}, {5, 7}, {13, 15}},
149129
},
150130
{
151-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, One[E](5)),
152-
Set[E]{{1, 4}, {7, 10}, {13, 16}},
131+
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](7, 9)),
132+
Set[E]{{1, 3}, {5, 7}, {9, 11}, {13, 15}},
153133
},
154134
{
155-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](4, 7)),
156-
Set[E]{{1, 4}, {7, 10}, {13, 16}},
135+
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](9, 7)),
136+
Set[E]{{1, 3}, {5, 11}, {13, 15}},
157137
},
158138
{
159-
del(Set[E]{{1, 4}, {7, 10}, {13, 16}}, Range[E](7, 4)),
160-
Set[E]{{1, 4}, {7, 10}, {13, 16}},
139+
del(Set[E]{{1, 3}, {5, 11}, {13, 15}}, Range[E](5, 3)),
140+
Set[E]{{1, 3}, {5, 11}, {13, 15}},
161141
},
162142
}
163143

0 commit comments

Comments
 (0)