-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval_pair.go
49 lines (42 loc) · 1.14 KB
/
interval_pair.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
package intervals
import (
"golang.org/x/exp/constraints"
)
func NewInterval[T constraints.Ordered](min T, includeMin bool, max T, includeMax bool) Interval[T] {
if max == min && includeMin && includeMax {
return NewSingleton(min)
}
if max <= min {
return NewNull[T]()
}
switch {
case includeMin && includeMax:
return closedInterval[T]{min, max}
case includeMin && !includeMax:
return ROpenInterval[T]{min, max}
case !includeMin && includeMax:
return LOpenInterval[T]{min, max}
case !includeMin && !includeMax:
return OpenInterval[T]{min, max}
default:
panic("impossible configuration")
}
}
func enumerateFromToStep[T constraints.Ordered](from, to, step T) []T {
var res []T
for i := from; i < to; i += step {
res = append(res, i)
}
return res
}
func overlapAtomic[T constraints.Ordered](one, other Interval[T]) bool {
inclOneMin, inclOneMax := one.IncludeLR()
inclOtherMin, inclOtherMax := other.IncludeLR()
if one.Min() > other.Max() ||
one.Max() < other.Min() ||
(one.Min() == other.Max() && !(inclOneMin && inclOtherMax)) ||
(one.Max() == other.Min() && !(inclOneMax && inclOtherMin)) {
return false
}
return true
}