-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval_closed.go
186 lines (158 loc) · 4.44 KB
/
interval_closed.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
package intervals
import (
"golang.org/x/exp/constraints"
"github.com/relvox/iridescence_go/maths"
)
type closedInterval[T constraints.Ordered] [2]T
func NewClosed[T constraints.Ordered](min, max T) Interval[T] {
if max < min {
return NewNull[T]()
}
if max == min {
return NewSingleton(min)
}
return closedInterval[T]{min, max}
}
func (ci closedInterval[T]) Min() T { return ci[0] }
func (ci closedInterval[T]) Max() T { return ci[1] }
func (ci closedInterval[T]) IncludeLR() (bool, bool) { return true, true }
func (ci closedInterval[T]) IsEmpty() bool { return false }
func (ci closedInterval[T]) IsSingleton() bool { return false }
func (ci closedInterval[T]) Enumerate(step T) []T {
if step == *new(T) {
step = maths.One[T]()
}
return enumerateFromToStep(ci[0], ci[1]+step, step)
}
func (ci closedInterval[T]) Intervals() []Interval[T] { return nil }
func (ci closedInterval[T]) Contains(value T) bool { return value >= ci[0] && value <= ci[1] }
func (ci closedInterval[T]) overlapsOne(other Interval[T]) bool {
inclMin, inclMax := other.IncludeLR()
if ci.Min() > other.Max() || (ci.Min() == other.Max() && !inclMax) {
return false
}
if ci.Max() < other.Min() || (ci.Max() == other.Min() && !inclMin) {
return false
}
return true
}
func (ci closedInterval[T]) Overlaps(other Interval[T]) bool {
if other == nil || other.IsEmpty() {
return false
}
if other.IsSingleton() {
return ci.Contains(other.Min())
}
subIntervals := other.Intervals()
if subIntervals == nil {
return ci.overlapsOne(other)
}
for _, subItv := range subIntervals {
if !ci.overlapsOne(subItv) {
continue
}
return true
}
return false
}
func (ci closedInterval[T]) Equals(other Interval[T]) bool {
if other == nil || other.IsEmpty() || other.IsSingleton() || other.Intervals() != nil {
return false
}
if inclMin, inclMax := other.IncludeLR(); !inclMin || !inclMax {
return false
}
return ci[0] == other.Min() && ci[1] == other.Max()
}
func (ci closedInterval[T]) Union(other Interval[T]) Interval[T] {
if other == nil || other.IsEmpty() ||
(other.IsSingleton() && ci.Contains(other.Min())) {
return ci
}
return NewMerged(other, ci)
}
func (ci closedInterval[T]) Intersection(other Interval[T]) Interval[T] {
if other == nil || other.IsEmpty() {
return other
}
if other.IsSingleton() {
if ci.Contains(other.Max()) {
return other
}
return NewNull[T]()
}
subIntervals := other.Intervals()
if subIntervals == nil {
if !ci.overlapsOne(other) {
return NewNull[T]()
}
newMin := max(ci[0], other.Min())
newMax := min(ci[1], other.Max())
return NewInterval(newMin, other.Contains(newMin), newMax, other.Contains(newMax))
}
var res []Interval[T]
for _, subItv := range subIntervals {
if !ci.overlapsOne(subItv) {
continue
}
newMin := max(ci[0], other.Min())
newMax := min(ci[1], other.Max())
res = append(res, NewInterval(newMin, other.Contains(newMin), newMax, other.Contains(newMax)))
}
return NewMerged(res...)
}
func (ci closedInterval[T]) Difference(other Interval[T]) Interval[T] {
if other == nil || other.IsEmpty() {
return ci
}
if other.IsSingleton() {
if !ci.Contains(other.Max()) {
return ci
}
if ci[0] == other.Min() {
return LOpenInterval[T](ci)
}
if ci[1] == other.Max() {
return ROpenInterval[T](ci)
}
return NewMerged(NewROpen(ci.Min(), other.Max()), NewLOpen(other.Min(), ci.Max()))
}
subIntervals := other.Intervals()
if subIntervals == nil {
if !ci.overlapsOne(other) {
return ci
}
if other.Contains(ci.Min()) {
if !ci.Contains(other.Max()) {
return NewNull[T]()
}
if other.Contains(ci.Max()) {
return NewNull[T]()
}
return NewSingleton(ci.Max())
}
minMin := max(ci[0], other.Min())
minMax := min(ci[1], other.Max())
// maxMin := max(other.Min(), ci[0])
maxMax := min(other.Max(), ci[1])
if minMin == minMax {
return NewNull[T]()
}
if minMin == ci[0] && maxMax == ci[1] {
// other is fully contained within ci
// Create two open intervals on either side of other
return NewMerged(NewROpen(ci.Min(), other.Max()), NewLOpen(other.Min(), ci.Max()))
}
return NewClosed(minMin, maxMax) // Remaining case: a closed interval within ci
}
var res []Interval[T]
for _, subItv := range subIntervals {
if !ci.overlapsOne(subItv) {
continue
}
// Calculate the difference between ci and each sub-interval
diff := ci.Difference(subItv)
res = append(res, diff)
}
return NewMerged(res...)
}