1 # Supports operations only on non-overlapping, adjacent or fully contained ranges
2 # i.e. unioning (10, 30) and (20, 40) may not work
9 def _contained(r1
, r2
):
13 last
= len(self
.r
) - 1
14 for i
, t
in enumerate(self
.r
):
17 if i
!= last
and new
[1] == self
.r
[i
+ 1][0]:
18 self
.r
[i
] = (new
[0], self
.r
[i
+ 1][1])
25 if i
!= 0 and new
[0] == self
.r
[i
- 1][1]:
26 self
.r
[i
] = (self
.r
[i
- 1][0], new
[1])
33 if self
._contained
(r
, self
.r
[i
- 1]):
37 if last
>= 0 and self
._contained
(r
, self
.r
[-1]):
43 return (self
.r
[0][0], self
.r
[-1][1])
48 def str(self
, render
=lambda x
: str(x
)):
49 rlist
= [(render(x
[0]), render(x
[1])) for x
in self
.r
]
52 # This allows to apply list(), but this creates a copy, using
53 # to_list() is more efficient.
61 if __name__
== "__main__":
64 assert r
.to_list() == [(10, 20)]
66 assert r
.to_list() == [(1, 5), (10, 20)]
68 assert r
.to_list() == [(1, 5), (10, 20), (100, 110)]
70 assert r
.to_list() == [(1, 8), (10, 20), (100, 110)]
72 assert r
.to_list() == [(1, 20), (100, 110)]
74 assert r
.to_list() == [(1, 20), (100, 120)]
76 assert r
.to_list() == [(1, 20), (100, 120)]
81 assert r
.to_list() == [(10, 20)]
86 #assert r.to_list() == [(10, 40)]
93 assert r
.to_list() == [(10, 40)]
98 assert r
.to_list() == [(1, 20)]