-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinterval.py
More file actions
275 lines (230 loc) · 8.33 KB
/
interval.py
File metadata and controls
275 lines (230 loc) · 8.33 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
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
from functools import total_ordering
__author__ = ''
@total_ordering
class Interval:
"""
Represent a real interval [min, max]
"""
__slots__ = ('_min', '_max')
def __init__(self, _min=float('-inf'), _max=float('inf')):
"""
:param _min: lower bound
:param _max: upper bound
"""
self._min = _min
self._max = _max
@property
def left(self):
"""
Gets a left value (minimum) of interval
"""
return self._min
@property
def right(self):
"""
Gets a right value (maximum) of interval
"""
return self._max
@property
def size(self):
"""
Gets the length of interval
"""
return self._max - self._min
@property
def middle(self):
"""
Gets a middle value of interval
"""
return (self._max - self._min) / 2
def empty_or_singleton(self):
"""
Returns true if interval has at most only one number
"""
return self._min >= self._max
def singleton(self):
"""
Returns true if interval has exactly one number
"""
return self._min == self._max
def empty(self):
"""
Returns true if interval is empty
"""
return self._min > self._max
def sampling(self, step):
"""
Gets an enumerable of numbers in interval
:param step: separation between each number
"""
if self._min == float('-inf') or self._max == float('inf'):
yield from (self._min, self._max)
else:
current = self._min - step
_max = self._max
while current < _max:
current += step
yield current
def __bool__(self):
return self._min < self._max
#region Interval operations
def __contains__(self, item):
if isinstance(item, Interval):
return self._min <= item._min and item._max <= self._max
if isinstance(item, tuple) and len(item)==2:
return self._min <= item[0] and item[1] <= self._max
#return self & item == item
return self._min <= item <= self._max
def intersect(self, other):
"""
Gets an intersection of self and other intervals
:param other: interval or 2-tuple
"""
#assert isinstance(other, Interval), 'intersect operation is defined only between Intervals'
if isinstance(other, Interval):
_min = max(self._min, other._min)
_max = min(self._max, other._max)
elif isinstance(other, tuple) and len(other)==2:
_min = max(self._min, other[0])
_max = min(self._max, other[1])
else:
raise Exception('intersect operation not defined with {0}'.format(type(other)))
return Interval(_min, _max)
def union(self, other):
"""
Gets an union of self and other intervals
:param other: interval or 2-tuple
"""
if isinstance(other, Interval):
if self._min > other._max:
return Compound(other, self)
if other._min > self._max:
return Compound(self, other)
_min = min(self._min, other._min)
_max = max(self._max, other._max)
elif isinstance(other, tuple) and len(other)==2:
if self._min > other[1]:
return Compound(Interval(*other), self)
if other[0] > self._max:
return Compound(self, Interval(*other))
_min = min(self._min, other[0])
_max = max(self._max, other[1])
else:
raise Exception('union operation not defined with {0}'.format(type(other)))
return Interval(_min, _max)
def __and__(self, other):
return self.intersect(other)
def __or__(self, other):
return self.union(other)
def __mul__(self, other):
if isinstance(other, float):
_min = other*self._min
_max = other*self._max
return Interval(_min, _max) if other > 0 else Interval(_max, _min)
#endregion
#region Comparisons
def __lt__(self, other):
if isinstance(other, Interval):
if self._min == other._min:
return self._max < other._max
return self._min < other._min
if isinstance(other, tuple) and len(other)==2:
if self._min == other[0]:
return self._max < other[1]
return self._min < other[0]
return TypeError('Interval unsupported type for comparison')
def __eq__(self, other):
if isinstance(other, Interval):
return self._min == other._min and self._max == other._max
if isinstance(other, tuple) and len(other)==2:
return self._min == other[0] and self._min == other[1]
return False
#endregion
def __iter__(self):
yield from (self._min, self._max)
def __repr__(self):
if self.empty(): return 'Empty Interval'
return 'Interval(%s, %s)' % (self._min, self._max)
def __str__(self):
return '({0} .. {1})'.format(self._min, self._max)
class Compound(Interval):
"""
Represent a Compound Interval which is a list of intervals
"""
__slots__ = ('_intervals', )
def __init__(self, *intervals):
super(Compound, self).__init__()
self._intervals = []
self.add_intervals(intervals)
def add_interval(self, interval):
if not interval.empty() and interval not in self._intervals:
self._intervals.append(interval)
self._intervals.sort()
self._min = self._intervals[0]._min
self._max = self._intervals[-1]._max
def add_intervals(self, intervals):
for interval in intervals:
self.add_interval(interval)
def __contains__(self, item):
for interval in self._intervals:
if item in interval:
return True
return False
def empty(self):
if not self._intervals:
return True
for interval in self._intervals:
if not interval.empty():
return False
return True
def intersect(self, other):
if isinstance(other, Compound):
#if self._min > other._max or self._max < other._min:
#return Interval()
res = []
for other_interval in other._intervals:
for interval in self._intervals:
intersection = interval.intersect(other_interval)
if not intersection.empty():
res.append(intersection)
return Compound(*res)
#if isinstance(other, Interval):
res = []
for interval in self._intervals:
intersection = interval.intersect(other)
if not intersection.empty():
res.append(intersection)
return Compound(*res)
def union(self, other):
if isinstance(other, Compound):
res = []
for other_interval in other._intervals:
for interval in self._intervals:
union = interval.union(other_interval)
if union.empty():
continue
if isinstance(union, Compound):
for i in union._intervals:
res.append(i)
else:
res.append(union)
return Compound(*res)
res = []
for interval in self._intervals:
union = interval.union(other)
if union.empty():
continue
if isinstance(union, Compound):
for i in union._intervals:
res.append(i)
else:
res.append(union)
return Compound(*res)
def __iter__(self):
yield from self._intervals
def __repr__(self):
if self.empty():
return 'Empty Compound Interval'
s = map(repr, self._intervals)
return 'Compound: ' + ' U '.join(s)
__str__ = __repr__