-
Notifications
You must be signed in to change notification settings - Fork 7
/
interval.go
321 lines (266 loc) · 9.23 KB
/
interval.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package spaniel
import (
"sort"
"time"
)
// EndPointType represents whether the start or end of an interval is Closed or Open.
type EndPointType int
const (
// Open means that the interval does not include a value
Open EndPointType = iota
// Closed means that the interval does include a value
Closed
)
// EndPoint represents an extreme of an interval, and whether it is inclusive or exclusive (Closed or Open)
type EndPoint struct {
Element time.Time
Type EndPointType
}
// Span represents a basic span, with a start and end time.
type Span interface {
Start() time.Time
StartType() EndPointType
End() time.Time
EndType() EndPointType
}
// Spans represents a list of spans, on which other functions operate.
type Spans []Span
// ByStart sorts a list of spans by their start point
type ByStart Spans
func (s ByStart) Len() int { return len(s) }
func (s ByStart) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByStart) Less(i, j int) bool { return s[i].Start().Before(s[j].Start()) }
// ByEnd sorts a list of spans by their end point
type ByEnd Spans
func (s ByEnd) Len() int { return len(s) }
func (s ByEnd) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByEnd) Less(i, j int) bool { return s[i].End().Before(s[j].End()) }
// UnionHandlerFunc is used by UnionWithHandler to allow for custom functionality when two spans are merged.
// It is passed the two spans to be merged, and span which will result from the union.
type UnionHandlerFunc func(mergeInto, mergeFrom, mergeSpan Span) Span
// IntersectionHandlerFunc is used by IntersectionWithHandler to allow for custom functionality when two spans
// intersect. It is passed the two spans that intersect, and span representing the intersection.
type IntersectionHandlerFunc func(intersectingEvent1, intersectingEvent2, intersectionSpan Span) Span
func getLoosestIntervalType(x, y EndPointType) EndPointType {
if x > y {
return x
}
return y
}
func getTightestIntervalType(x, y EndPointType) EndPointType {
if x < y {
return x
}
return y
}
func getMin(a, b EndPoint) EndPoint {
if a.Element.Before(b.Element) {
return a
}
return b
}
func getMax(a, b EndPoint) EndPoint {
if a.Element.After(b.Element) {
return a
}
return b
}
func filter(spans Spans, filterFunc func(Span) bool) Spans {
filtered := Spans{}
for _, span := range spans {
if !filterFunc(span) {
filtered = append(filtered, span)
}
}
return filtered
}
// IsInstant returns true if the interval is deemed instantaneous
func IsInstant(a Span) bool {
return a.Start().Equal(a.End())
}
// Returns true if two spans are side by side
func contiguous(a, b Span) bool {
// [1,2,3,4] [4,5,6,7] - not contiguous
// [1,2,3,4) [4,5,6,7] - contiguous
// [1,2,3,4] (4,5,6,7] - contiguous
// [1,2,3,4) (4,5,6,7] - not contiguous
// [1,2,3] [5,6,7] - not contiguous
// [1] (1,2,3] - contiguous
// Two instants can't be contiguous
if IsInstant(a) && IsInstant(b) {
return false
}
if b.Start().Before(a.Start()) {
a, b = b, a
}
aStartType := a.StartType()
aEndType := a.EndType()
bStartType := b.StartType()
if IsInstant(a) {
aEndType = Closed
aStartType = Closed
}
if IsInstant(b) {
bStartType = Closed
}
// If a and b start at the same point, just check that their start types are different.
if a.Start().Equal(b.Start()) {
return aStartType != bStartType
}
// To be contiguous the ranges have to overlap on the first/last point
if !(a.End().Equal(b.Start())) {
return false
}
if aEndType == bStartType {
return false
}
return true
}
// Returns true if two spans overlap
func overlap(a, b Span) bool {
// [1,2,3,4] [4,5,6,7] - intersects
// [1,2,3,4) [4,5,6,7] - doesn't intersect
// [1,2,3,4] (4,5,6,7] - doesn't intersect
// [1,2,3,4) (4,5,6,7] - doesn't intersect
aStartType := a.StartType()
aEndType := a.EndType()
bStartType := b.StartType()
bEndType := b.EndType()
if IsInstant(a) {
aStartType = Closed
aEndType = Closed
}
if IsInstant(b) {
bStartType = Closed
bEndType = Closed
}
// Given [a_s,a_e] and [b_s,b_e]
// If a_s > b_e || a_e < b_s, overlap == false
c1 := false // is a_s after b_e
if a.Start().After(b.End()) {
c1 = true
} else if a.Start().Equal(b.End()) {
c1 = (aStartType == Open || bEndType == Open)
}
c2 := false // is a_e before b_s
if a.End().Before(b.Start()) {
c2 = true
} else if a.End().Equal(b.Start()) {
c2 = (aEndType == Open || bStartType == Open)
}
if c1 || c2 {
return false
}
return true
}
// UnionWithHandler returns a list of Spans representing the union of all of the spans.
// For example, given a list [A,B] where A and B overlap, a list [C] would be returned, with the span C spanning
// both A and B. The provided handler is passed the source and destination spans, and the currently merged empty span.
func (s Spans) UnionWithHandler(unionHandlerFunc UnionHandlerFunc) Spans {
if len(s) < 2 {
return s
}
var sorted Spans
sorted = append(sorted, s...)
sort.Stable(ByStart(sorted))
result := Spans{sorted[0]}
for _, b := range sorted[1:] {
// A: current span in merged array; B: current span in sorted array
// If B overlaps with A, it can be merged with A.
a := result[len(result)-1]
if overlap(a, b) || contiguous(a, b) {
spanStart := getMin(EndPoint{a.Start(), a.StartType()}, EndPoint{b.Start(), b.StartType()})
spanEnd := getMax(EndPoint{a.End(), a.EndType()}, EndPoint{b.End(), b.EndType()})
if a.Start().Equal(b.Start()) {
spanStart.Type = getLoosestIntervalType(a.StartType(), b.StartType())
}
if a.End().Equal(b.End()) {
spanEnd.Type = getLoosestIntervalType(a.EndType(), b.EndType())
}
span := NewWithTypes(spanStart.Element, spanEnd.Element, spanStart.Type, spanEnd.Type)
result[len(result)-1] = unionHandlerFunc(a, b, span)
continue
}
result = append(result, b)
}
return result
}
// Union returns a list of Spans representing the union of all of the spans.
// For example, given a list [A,B] where A and B overlap, a list [C] would be returned, with the span C spanning
// both A and B.
func (s Spans) Union() Spans {
return s.UnionWithHandler(func(mergeInto, mergeFrom, mergeSpan Span) Span {
return mergeSpan
})
}
// IntersectionWithHandler returns a list of Spans representing the overlaps between the contained spans.
// For example, given a list [A,B] where A and B overlap, a list [C] would be returned, with the span C covering
// the intersection of the A and B. The provided handler function is notified of the two spans that have been found
// to overlap, and the span representing the overlap.
func (s Spans) IntersectionWithHandler(intersectHandlerFunc IntersectionHandlerFunc) Spans {
var sorted Spans
sorted = append(sorted, s...)
sort.Stable(ByStart(sorted))
actives := Spans{sorted[0]}
intersections := Spans{}
for _, b := range sorted[1:] {
// Tidy up the active span list
actives = filter(actives, func(t Span) bool {
// If this value is identical to one in actives, don't filter it.
if b.Start() == t.Start() && b.End() == t.End() {
return false
}
// If this value starts after the one in actives finishes, filter the active.
return b.Start().After(t.End())
})
for _, a := range actives {
if overlap(a, b) {
spanStart := getMax(EndPoint{a.Start(), a.StartType()}, EndPoint{b.Start(), b.StartType()})
spanEnd := getMin(EndPoint{a.End(), a.EndType()}, EndPoint{b.End(), b.EndType()})
if a.Start().Equal(b.Start()) {
spanStart.Type = getTightestIntervalType(a.StartType(), b.StartType())
}
if a.End().Equal(b.End()) {
spanEnd.Type = getTightestIntervalType(a.EndType(), b.EndType())
}
span := NewWithTypes(spanStart.Element, spanEnd.Element, spanStart.Type, spanEnd.Type)
intersection := intersectHandlerFunc(a, b, span)
intersections = append(intersections, intersection)
}
}
actives = append(actives, b)
}
return intersections
}
// Intersection returns a list of Spans representing the overlaps between the contained spans.
// For example, given a list [A,B] where A and B overlap, a list [C] would be returned,
// with the span C covering the intersection of A and B.
func (s Spans) Intersection() Spans {
return s.IntersectionWithHandler(func(intersectingEvent1, intersectingEvent2, intersectionSpan Span) Span {
return intersectionSpan
})
}
// IntersectionBetweenWithHandler returns a list of pointers to Spans representing the overlaps between the contained spans
// and a given set of spans. It calls intersectHandlerFunc for each pair of spans that are intersected.
func (s Spans) IntersectionBetweenWithHandler(candidates Spans, intersectHandlerFunc IntersectionHandlerFunc) Spans {
intersections := Spans{}
for _, candidate := range candidates {
for _, span := range s {
i := Spans{candidate, span}.IntersectionWithHandler(func(a, b, s Span) Span {
if a == candidate {
return intersectHandlerFunc(a, b, s)
}
return intersectHandlerFunc(b, a, s)
})
intersections = append(intersections, i...)
}
}
return intersections
}
// IntersectionBetween returns the slice of spans representing the overlaps between the contained spans
// and a given set of spans.
func (s Spans) IntersectionBetween(b Spans) Spans {
return s.IntersectionBetweenWithHandler(b, func(intersectingEvent1, intersectingEvent2, intersectionSpan Span) Span {
return intersectionSpan
})
}