This repository was archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrangeset.go
266 lines (222 loc) · 8.47 KB
/
rangeset.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
// Package rangeset is a library for manipulating sets of discrete ranges.
//
// Deprecated: Development moved to https://github.com/b97tsk/intervals.
package rangeset
import (
"sort"
"golang.org/x/exp/constraints"
)
// Elem is the type set containing all supported element types.
type Elem constraints.Integer
// A Range is a half-open interval of type E.
type Range[E Elem] struct {
Low E // inclusive
High E // exclusive
}
// A RangeSet is a slice of discrete Ranges sorted in ascending order.
// The zero value for a RangeSet, i.e. a nil RangeSet, is an empty set.
//
// Since Range is half-open, you can never add the maximum value of E into
// a RangeSet.
type RangeSet[E Elem] []Range[E]
// FromRange creates a RangeSet from range [lo, hi).
//
// If lo >= hi, FromRange returns nil.
func FromRange[E Elem](lo, hi E) RangeSet[E] {
if lo >= hi {
return nil
}
return RangeSet[E]{{lo, hi}}
}
// Universal returns the largest RangeSet, which contains every E except one,
// the maximum value of E.
func Universal[E Elem]() RangeSet[E] {
return FromRange(minOf[E](), maxOf[E]())
}
// Add adds a single element into set.
func (set *RangeSet[E]) Add(v E) {
set.AddRange(v, v+1)
}
// AddRange adds range [lo, hi) into set.
func (set *RangeSet[E]) AddRange(lo, hi E) {
s := *set
i := sort.Search(len(s), func(i int) bool { return s[i].Low > lo })
j := sort.Search(len(s), func(i int) bool { return s[i].High > hi })
// ┌────────┬─────────────────────────────────────────┐
// │ │ 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 ->| │
// └────────┴─────────────────────────────────────────┘
if i > j { // Case 1 and 2.
return
}
// Case 3, 4 and 5.
if i > 0 && lo <= s[i-1].High {
lo = s[i-1].Low
i--
}
if j < len(s) && hi >= s[j].Low {
hi = s[j].High
j++
}
if i == j { // Case 3 (where lo and hi overlaps).
if lo < hi {
s = append(s, Range[E]{})
copy(s[i+1:], s[i:])
s[i] = Range[E]{lo, hi}
*set = s
}
return
}
s[i] = Range[E]{lo, hi}
s = append(s[:i+1], s[j:]...)
*set = s
}
// Delete removes a single element from set.
func (set *RangeSet[E]) Delete(v E) {
set.DeleteRange(v, v+1)
}
// DeleteRange removes range [lo, hi) from set.
func (set *RangeSet[E]) DeleteRange(lo, hi E) {
s := *set
i := sort.Search(len(s), func(i int) bool { return s[i].High > lo })
// j := sort.Search(len(s), func(i int) bool { return s[i].Low > hi })
// ┌────────┬─────────────────────────────────────────┐
// │ │ 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.
t := s[i:]
j := i + sort.Search(len(t), func(i int) bool { return t[i].Low > hi })
if i == j { // Case 1, 2 and 3.
return
}
if i == j-1 { // Case 4.
if lo > s[i].Low {
if hi < s[i].High {
if lo < hi {
s = append(s, Range[E]{})
copy(s[j:], s[i:])
s[i].High = lo
s[j].Low = hi
*set = s
}
} else {
s[i].High = lo
}
} else {
if hi < s[i].High {
s[i].Low = hi
} else {
s = append(s[:i], s[j:]...)
*set = s
}
}
return
}
// Case 5.
if lo > s[i].Low {
s[i].High = lo
i++
}
if hi < s[j-1].High {
s[j-1].Low = hi
j--
}
s = append(s[:i], s[j:]...)
*set = s
}
// Contains reports whether set contains a single element.
func (set RangeSet[E]) Contains(v E) bool {
return set.ContainsRange(v, v+1)
}
// ContainsRange reports whether set contains every element in range [lo, hi).
func (set RangeSet[E]) ContainsRange(lo, hi E) bool {
i := sort.Search(len(set), func(i int) bool { return set[i].High > lo })
return i < len(set) && set[i].Low <= lo && hi <= set[i].High && lo < hi
}
// Difference returns the subset of set that having all elements in other
// excluded.
func (set RangeSet[E]) Difference(other RangeSet[E]) RangeSet[E] {
return set.Intersection(other.Complement())
}
// Equal reports whether set is identical to other.
func (set RangeSet[E]) Equal(other RangeSet[E]) bool {
if len(set) != len(other) {
return false
}
for i, r := range set {
if r != other[i] {
return false
}
}
return true
}
// Extent returns the smallest Range that covers the whole set.
//
// If set is empty, Extent returns the zero value.
func (set RangeSet[E]) Extent() Range[E] {
if len(set) == 0 {
return Range[E]{}
}
return Range[E]{
Low: set[0].Low,
High: set[len(set)-1].High,
}
}
// IsSubsetOf reports whether other contains every element in set.
func (set RangeSet[E]) IsSubsetOf(other RangeSet[E]) bool {
for _, r := range set {
if !other.ContainsRange(r.Low, r.High) {
return false
}
}
return true
}
// Count returns the number of element in set.
func (set RangeSet[E]) Count() uint64 {
var count uint64
for _, r := range set {
count += uint64(r.High - r.Low)
}
return count
}