-
Notifications
You must be signed in to change notification settings - Fork 4
/
Interval.py
executable file
·172 lines (141 loc) · 5.04 KB
/
Interval.py
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
#=========================================================================
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
# Copyright (C)2016 William H. Majoros ([email protected]).
#=========================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
import sys
from Rex import Rex
rex=Rex()
#=========================================================================
# Attributes:
# begin
# end
# Methods:
# i=Interval(begin,end)
# s=interval.toString()
# print(file=STDOUT)
# bool=interval.overlaps(other)
# bool=interval.contains(position)
# bool=interval.containsInterval(other)
# distance=interval.distance(other)
# distance=interval.distanceFromPoint(x)
# intersection=interval.intersect(other)
# union=interval.union(other) # returns an array of intervals
# diff=interval.minus(other) # returns an array of intervals
# length=interval.getLength()
# begin=interval.getBegin()
# end=interval.getEnd()
# length=interval.length()
# bool=interval.equals(other)
# other=interval.clone()
# bool=interval.isEmpty()
# d=interval.relativeDistanceFromBegin(pos)
# d=interval.relativeDistanceFromEnd(pos)
# interval.shift(delta)
# center=interval.floatCenter()
# center=interval.intCenter()
# center=interval.center() # same as floatCenter()
# Class methods:
# interval=Interval.parseInt("(10,15)")
# interval=Interval.parseFloat("(3.5,7.2)")
#=========================================================================
class Interval:
""""Interval represents an interval [a,b) in which a is inclusive and
b is not
"""
def __init__(self,begin=0,end=0):
self.begin=begin
self.end=end
@classmethod
def parseInt(cls,interval):
if(rex.find("\(([^,]+),([^\)]+)\)",interval)):
return Interval(int(rex[1]),int(rex[2]))
if(rex.find("([^,]+):([^\)]+)",interval)):
return Interval(int(rex[1]),int(rex[2]))
if(rex.find("([^,]+)-([^\)]+)",interval)):
return Interval(int(rex[1]),int(rex[2]))
return None
@classmethod
def parseFloat(cls,interval):
if(rex.find("\(([^,]+),([^\)]+)\)",interval)):
return Interval(float(rex[1]),float(rex[2]))
return None
def print(self,file=sys.stdout):
print("(",self.begin,",",self.end,")",sep="",end="",file=file)
def toString(self):
return "("+str(self.begin)+","+str(self.end)+")"
def overlaps(self,other):
return self.begin<other.end and other.begin<self.end
def distanceFromPoint(self,x):
if(self.contains(x)): return 0
d1=abs(self.begin-x)
d2=abs(self.end-x)
return d1 if d1<d2 else d2
def distance(self,other):
if(self.overlaps(other)): return 0
d=self.begin-other.end
if(d>0): return d
return other.begin-self.end
def contains(self,index):
return index>=self.begin and index<self.end
def containsInterval(self,other):
return self.begin<=other.begin and self.end>=other.end
def isEmpty(self):
return self.begin>=self.end
def clone(self):
return Interval(self.begin,self.end)
def intersect(self,other):
if(self.isEmpty()): return self.clone()
if(other.isEmpty()): return other.clone()
begin=max(self.begin,other.begin)
end=min(self.end,other.end)
return Interval(begin,end)
def length(self):
L=self.end-self.begin
return 0 if L<0 else L
def getLength(self):
return self.length()
def getBegin(self):
return self.begin
def getEnd(self):
return self.end
def equals(self,other):
return self.begin==other.begin and self.end==other.end
def relativeDistanceFromBegin(self,pos):
if(not self.contains(pos)): raise IndexError(pos)
return (pos-self.begin)/self.length()
def relativeDistanceFromEnd(self,pos):
if(not self.contains(pos)): raise IndexError(pos)
return (self.end-pos)/self.length()
def union(self,other):
s=[]
if(self.overlaps(other)):
s.append(Interval(self.begin,other.end))
else:
s.append(self,other)
return s
def minus(self,other):
if(not self.overlaps(other)): return [self]
s=[]
if(self.begin<other.begin):
s.append(Interval(self.begin,other.begin))
if(self.end>other.end):
s.append(Interval(other.end,self.end))
return s
def __str__(self):
return "("+str(self.begin)+","+str(self.end)+")"
def __repr__(self):
return "("+str(self.begin)+","+str(self.end)+")"
def shift(self,delta):
self.begin+=delta
self.end+=delta
def floatCenter(self):
return float(self.begin+self.end)/2.0
def intCenter(self):
return int((self.begin+self.end)/2)
def center(self):
return self.floatCenter()