-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterval.go
More file actions
50 lines (40 loc) · 1.02 KB
/
interval.go
File metadata and controls
50 lines (40 loc) · 1.02 KB
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
package main
import (
"fmt"
)
type Interval struct {
Start, End float64
}
func (i *Interval) Contains(x float64) bool {
return (x >= i.Start) && (x <= i.End)
}
func (i *Interval) String() string {
return fmt.Sprintf("[%v, %v]", i.Start, i.End)
}
func Intersect(x, y Interval) Interval {
// Assume WLOG, x.Start < y.Start
if x.Start > y.Start {
x, y = y, x
}
switch {
case x.End < y.Start:
// x and y do not intersect.
return Interval{}
case x.End < y.Start && x.End < y.End:
// x and y partially overlap.
return Interval{x.End, y.Start}
case x.Start < y.Start && y.End < x.End:
// y is a subinterval of x
return y
}
return Interval{}
}
type IntervalSlice []Interval
func (s IntervalSlice) Len() int { return len(s) }
// Less returns whether the element with index i should sort
// before the element with index j.
func (s IntervalSlice) Less(i, j int) bool {
return s[i].Start < s[j].Start
}
// Swap swaps the elements with indexes i and j.
func (s IntervalSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }