-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.go
247 lines (226 loc) · 6.14 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
// Copyright 2025. Silvano DAL ZILIO. All rights reserved.
// Use of this source code is governed by the AGPL license
// that can be found in the LICENSE file.
package nets
import (
"bytes"
"fmt"
"strconv"
)
// Bkind is the type of possible time constraints bounds
type Bkind uint8
// Bkind is an enumeration describing the three different types of (time)
// interval bounds. BINFTY, as a right bound, is used for infinite intervals. As
// a left bound, it is used to denote empty intervals (errors).
const (
BINFTY Bkind = iota // ..,w[
BCLOSE // [a,..
BOPEN // ]a,..
)
// Bound is the type of bounds in a time interval.
type Bound struct {
Bkind
Value int
}
func (b Bound) String() string {
switch b.Bkind {
case BINFTY:
return "w"
case BCLOSE:
return fmt.Sprintf("=%d", b.Value)
default:
return fmt.Sprintf("x%d", b.Value)
}
}
// PrintLowerBound returns a textual representation of a time interval bound
// used as a lower bound constraint, such as "4 <" or "5 ≤". We return the
// string "∞" if b is infinite (which should not happen in practice).
func (b Bound) PrintLowerBound() string {
switch b.Bkind {
case BINFTY:
return "∞"
case BCLOSE:
return fmt.Sprintf("%d ≤", b.Value)
default:
return fmt.Sprintf("%d <", b.Value)
}
}
// PrintUpperBound is the dual of PrintLowerBound and returns a representation
// of a time interval bound used as a lower bound constraint, such as "< 4" or
// "≤ 5". We return the string "< ∞" if b is infinite.
func (b Bound) PrintUpperBound() string {
switch b.Bkind {
case BINFTY:
return "< ∞"
case BCLOSE:
return fmt.Sprintf("≤ %d", b.Value)
default:
return fmt.Sprintf("< %d", b.Value)
}
}
// TimeInterval is the type of time intervals.
type TimeInterval struct {
Left, Right Bound
}
func (i *TimeInterval) String() string {
if i.Left.Bkind == BINFTY {
// it means interval was never set
return "[0,w["
}
var buf bytes.Buffer
if i.Left.Bkind == BCLOSE {
buf.WriteRune('[')
} else {
buf.WriteRune(']')
}
buf.WriteString(strconv.Itoa(int(i.Left.Value)))
buf.WriteRune(',')
if i.Right.Bkind == BINFTY {
buf.WriteString("w[")
} else {
buf.WriteString(strconv.Itoa(int(i.Right.Value)))
if i.Right.Bkind == BCLOSE {
buf.WriteRune(']')
} else {
buf.WriteRune('[')
}
}
return buf.String()
}
/*****************************************************************************/
// BSubstract computes the diference, b1 - b2, between its time bounds
// parameters. We return an infinite bound when b2 is infinite.
func BSubstract(b1, b2 Bound) Bound {
if b1.Bkind == BINFTY {
return b1
}
if b2.Bkind == BINFTY {
return b2
}
diff := b1.Value - b2.Value
if b1.Bkind == BOPEN || b2.Bkind == BOPEN {
return Bound{BOPEN, diff}
}
return Bound{BCLOSE, diff}
}
// BAdd returns the sum of two time bounds.
func BAdd(b1, b2 Bound) Bound {
if b1.Bkind == BINFTY || b2.Bkind == BINFTY {
return Bound{BINFTY, 0}
}
add := b1.Value + b2.Value
if b1.Bkind == BOPEN || b2.Bkind == BOPEN {
return Bound{BOPEN, add}
}
return Bound{BCLOSE, add}
}
// BCompare returns an integer comparing two bounds. The result will be 0 if a
// and b are equal, negative if a < b, and positive otherwise. We return the
// difference between the bounds values, with some exceptions. We always return
// +1 when b a infinite or when a and b have same values, but a is open whereas
// b is closed. For intance, the bound [1,.. is considered strictly greater than
// ]1,.. with our choice. We return -1 in the symetric cases.
func BCompare(a, b Bound) int {
if a.Bkind == BINFTY {
return +1
}
if b.Bkind == BINFTY {
return -1
}
if a.Value != b.Value {
return a.Value - b.Value
}
if a.Bkind == b.Bkind {
return 0
}
if a.Bkind == BOPEN && b.Bkind == BCLOSE {
return -1
}
return +1
}
// BIsPositive returns true if b1 is greater or equal to 0.
func BIsPositive(b Bound) bool {
if b.Value > 0 || b.Bkind == BINFTY {
return true
}
if b.Value == 0 && b.Bkind == BCLOSE {
return true
}
return false
}
// BMax returns the max of a and b.
func BMax(a, b Bound) Bound {
if BCompare(a, b) <= 0 {
return b
}
return a
}
// BMin returns the min of a and b.
func BMin(a, b Bound) Bound {
if BCompare(a, b) <= 0 {
return a
}
return b
}
/*****************************************************************************/
// Trivial is true if the time interval i is of the form [0, w[ or if the
// interval is un-initialized (meaning the left part of the interval is of kind
// BINFTY)
func (i *TimeInterval) Trivial() bool {
if i.Left.Bkind == BINFTY {
return true
}
if i.Right.Bkind != BINFTY {
return false
}
if i.Left.Bkind != BCLOSE {
return false
}
if i.Left.Value != 0 {
return false
}
return true
}
// intersectWith sets interval i to the intersection of i and j. We return an
// error if the intersection is empty.
func (i *TimeInterval) intersectWith(j TimeInterval) error {
if i.Left.Bkind == BINFTY {
// it means we are initializing the interval
i.Left.Bkind = j.Left.Bkind
i.Left.Value = j.Left.Value
i.Right.Bkind = j.Right.Bkind
i.Right.Value = j.Right.Value
}
if j.Left.Bkind == BINFTY {
return fmt.Errorf("bad time interval when computing intersection")
}
// we compute the max of the left parts
if j.Left.Value >= i.Left.Value {
if j.Left.Value > i.Left.Value || (j.Left.Value == i.Left.Value && j.Left.Bkind == BOPEN) {
// we update the left part
i.Left.Bkind = j.Left.Bkind
i.Left.Value = j.Left.Value
}
}
if j.Right.Bkind == BINFTY {
// we do not need to update the right part
return nil
}
if i.Right.Bkind == BINFTY {
i.Right.Bkind = j.Right.Bkind
i.Right.Value = j.Right.Value
return nil
}
// when both intervals are right-bounded we take the min of their right parts
if j.Right.Value <= i.Right.Value {
if j.Right.Value < i.Right.Value || (j.Right.Value == i.Right.Value && j.Right.Bkind == BOPEN) {
i.Right.Bkind = j.Right.Bkind
i.Right.Value = j.Right.Value
}
}
// we need to test if the result is empty
if i.Right.Value < i.Left.Value || (i.Right.Value == i.Left.Value && (i.Left.Bkind == BOPEN || i.Right.Bkind == BOPEN)) {
return fmt.Errorf("empty time interval when computing intersection")
}
return nil
}