-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintervalset.go
295 lines (249 loc) · 10.2 KB
/
intervalset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Package intervals is a library for manipulating sets of intervals.
package intervals
import (
"slices"
"sort"
)
// Elem is the type set containing all supported element types.
type Elem[E any] interface {
Compare(E) int
}
// An Enum is a computably enumerable Elem. There exists a computable
// enumeration that lists elements, in a valid Interval of Enum, in an
// increasing ordering determined by the Compare method.
type Enum[E any] interface {
Elem[E]
Next() E
}
// An Interval is a half-open continuous range of elements.
type Interval[E Elem[E]] struct {
Low E // inclusive
High E // exclusive
}
// Unit returns an Interval that only contains a single element v.
// If there is no element next to v, Unit returns an invalid Interval.
// Note that calling Set method on an invalid Interval panics.
func Unit[E Enum[E]](v E) Interval[E] {
return Range(v, v.Next())
}
// Range returns an Interval of range [lo, hi).
// If lo.Compare(hi) >= 0, Range returns an invalid Interval.
// Note that calling Set method on an invalid Interval panics.
func Range[E Elem[E]](lo, hi E) Interval[E] {
return Interval[E]{lo, hi}
}
// Equal reports whether r is identical to r2.
func (r Interval[E]) Equal(r2 Interval[E]) bool {
return r.Low.Compare(r2.Low) == 0 && r.High.Compare(r2.High) == 0
}
// IsValid reports whether r is a valid Interval.
// A valid Interval r either satisfies r.Low.Compare(r.High) < 0 or equals to
// the zero value.
func (r Interval[E]) IsValid() bool {
return r.test() <= 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 r.test() {
case -1:
return Set[E]{r}
case 0:
return nil
}
panic("invalid Interval")
}
func (r Interval[E]) test() 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.
//
// Since Intervals are half-open, the maximum value of E cannot be added into
// a Set.
type Set[E Elem[E]] []Interval[E]
// Collect returns the set of elements that are in any of s.
//
// Collect performs better if s are sorted in ascending order by the Low field.
func Collect[E Elem[E]](s ...Interval[E]) Set[E] {
return CollectInto(nil, s...)
}
// CollectInto returns the set of elements that are in any of s, overwriting x.
// x and s can be the same slice. x must not be used after.
//
// CollectInto performs better if s are sorted in ascending order by the Low
// field.
func CollectInto[E Elem[E]](x Set[E], s ...Interval[E]) Set[E] {
x = x[:0]
for _, r := range s {
if len(x) == 0 {
if r.Low.Compare(r.High) < 0 {
x = append(x, r)
}
continue
}
switch r1 := &x[len(x)-1]; {
case r1.High.Compare(r.Low) < 0:
if r.Low.Compare(r.High) < 0 {
x = append(x, r)
}
case r1.Low.Compare(r.Low) <= 0:
if r1.High.Compare(r.High) < 0 {
r1.High = r.High
}
default:
x = addRange(x, r.Low, r.High)
}
}
return x
}
// Add adds zero or more Intervals into x, returning the modified Set.
func Add[E Elem[E]](x Set[E], s ...Interval[E]) Set[E] {
for _, r := range s {
x = addRange(x, r.Low, r.High)
}
return x
}
// addRange adds range [lo, hi) into x, returning the modified Set.
func addRange[E Elem[E]](x Set[E], lo, hi E) Set[E] {
i := sort.Search(len(x), func(i int) bool { return x[i].Low.Compare(lo) > 0 })
// j := sort.Search(len(x), func(i int) bool { return x[i].High.Compare(hi) > 0 })
// ┌────────┬─────────────────────────────────────────┐
// │ │ j-1 j i-1 i │
// │ Case 1 │ |-----| |-----| ~ |-----| |-----| │
// │ │ |<- hi ->| |<- lo ->| │
// ├────────┼─────────────────────────────────────────┤
// │ │ j-1 j i │
// │ Case 2 │ |-----| |-----| |-----| │
// │ │ |<- lo ->| │
// │ │ |<- hi ->| │
// ├────────┼─────────────────────────────────────────┤
// │ │ i-1 i,j │
// │ Case 3 │ |-----| |-----| │
// │ │ |<- lo ->| │
// │ │ |<- hi ->| │
// ├────────┼─────────────────────────────────────────┤
// │ │ i-1 i j │
// │ Case 4 │ |-----| |-----| |-----| │
// │ │ |<- lo ->| |<- hi ->| │
// │ │ │
// ├────────┼─────────────────────────────────────────┤
// │ │ i-1 i j-1 j │
// │ Case 5 │ |-----| |-----| ~ |-----| |-----| │
// │ │ |<- lo ->| |<- hi ->| │
// └────────┴─────────────────────────────────────────┘
// Optimized: j >= i-1.
off := max(0, i-1)
z := x[off:]
j := off + sort.Search(len(z), func(i int) bool { return z[i].High.Compare(hi) > 0 })
if i > j { // Case 1 and 2.
return x
}
// Case 3, 4 and 5.
if i > 0 {
if r := &x[i-1]; r.High.Compare(lo) >= 0 {
lo = r.Low
i--
}
}
if j < len(x) {
if r := &x[j]; r.Low.Compare(hi) <= 0 {
hi = r.High
j++
}
}
if i == j { // Case 3 (where lo and hi overlap).
if lo.Compare(hi) >= 0 { // Get rid of the devil first.
return x
}
}
return slices.Replace(x, i, j, Range(lo, hi))
}
// Delete removes zero or more Intervals from x, returning the modified Set.
func Delete[E Elem[E]](x Set[E], s ...Interval[E]) Set[E] {
for _, r := range s {
x = deleteRange(x, r.Low, r.High)
}
return x
}
// deleteRange removes range [lo, hi) from x, returning the modified Set.
func deleteRange[E Elem[E]](x Set[E], lo, hi E) Set[E] {
i := sort.Search(len(x), func(i int) bool { return x[i].High.Compare(lo) > 0 })
// j := sort.Search(len(x), func(i int) bool { return x[i].Low.Compare(hi) > 0 })
// ┌────────┬─────────────────────────────────────────┐
// │ │ j-1 j i-1 i │
// │ Case 1 │ |-----| |-----| ~ |-----| |-----| │
// │ │ |<- hi ->| |<- lo ->| │
// ├────────┼─────────────────────────────────────────┤
// │ │ j-1 j i │
// │ Case 2 │ |-----| |-----| |-----| │
// │ │ |<- hi ->| |<- lo ->| │
// │ │ │
// ├────────┼─────────────────────────────────────────┤
// │ │ i-1 i,j │
// │ Case 3 │ |-----| |-----| │
// │ │ |<- lo ->| │
// │ │ |<- hi ->| │
// ├────────┼─────────────────────────────────────────┤
// │ │ i-1 i j │
// │ Case 4 │ |-----| |-----| |-----| │
// │ │ |<- lo ->| │
// │ │ |<- hi ->| │
// ├────────┼─────────────────────────────────────────┤
// │ │ i-1 i j-1 j │
// │ Case 5 │ |-----| |-----| ~ |-----| |-----| │
// │ │ |<- lo ->| |<- hi ->| │
// └────────┴─────────────────────────────────────────┘
// Optimized: j >= i.
z := x[i:]
j := i + sort.Search(len(z), func(i int) bool { return z[i].Low.Compare(hi) > 0 })
if i == j { // Case 1, 2 and 3.
return x
}
if i == j-1 { // Case 4.
if lo.Compare(hi) >= 0 { // Get rid of the devil first.
return x
}
}
// Case 4 and 5.
v := make([]Interval[E], 0, 2)
if r := &x[i]; r.Low.Compare(lo) < 0 {
v = append(v, Range(r.Low, lo))
}
if r := &x[j-1]; r.High.Compare(hi) > 0 {
v = append(v, Range(hi, r.High))
}
return slices.Replace(x, i, j, v...)
}
// Contains reports whether x contains every element in range [r.Low, r.High).
func (x Set[E]) Contains(r Interval[E]) bool {
x = x[sort.Search(len(x), func(i int) bool { return x[i].High.Compare(r.Low) > 0 }):]
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)
}
// Extent returns the smallest Interval that contains every element in x.
//
// If x is empty, Extent returns the zero value.
func (x Set[E]) Extent() Interval[E] {
if len(x) == 0 {
return Interval[E]{}
}
return Range(x[0].Low, x[len(x)-1].High)
}