1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
6 """Represents a min-max bounds."""
13 def CreateFromEvent(event
):
15 bounds
.AddEvent(event
)
22 return "Bounds(min=%s,max=%s)" % (self
.min_
, self
.max_
)
44 return self
.max_
- self
.min_
48 return (self
.min_
+ self
.max_
) * 0.5
50 def Contains(self
, other
):
51 if self
.is_empty
or other
.is_empty
:
53 return self
.min <= other
.min and self
.max >= other
.max
55 def ContainsInterval(self
, start
, end
):
56 return self
.min <= start
and self
.max >= end
58 def Intersects(self
, other
):
59 if self
.is_empty
or other
.is_empty
:
61 return not (other
.max < self
.min or other
.min > self
.max)
68 def AddBounds(self
, bounds
):
71 self
.AddValue(bounds
.min_
)
72 self
.AddValue(bounds
.max_
)
74 def AddValue(self
, value
):
78 self
.is_empty_
= False
81 self
.max_
= max(self
.max_
, value
)
82 self
.min_
= min(self
.min_
, value
)
84 def AddEvent(self
, event
):
85 self
.AddValue(event
.start
)
86 self
.AddValue(event
.start
+ event
.duration
)
89 def CompareByMinTimes(a
, b
):
90 if not a
.is_empty
and not b
.is_empty
:
91 return a
.min_
- b
.min_
93 if a
.is_empty
and not b
.is_empty
:
96 if not a
.is_empty
and b
.is_empty
:
102 def GetOverlapBetweenBounds(first_bounds
, second_bounds
):
103 """Compute the overlap duration between first_bounds and second_bounds."""
104 return Bounds
.GetOverlap(first_bounds
.min_
, first_bounds
.max_
,
105 second_bounds
.min_
, second_bounds
.max_
)
108 def GetOverlap(first_bounds_min
, first_bounds_max
,
109 second_bounds_min
, second_bounds_max
):
110 assert first_bounds_min
<= first_bounds_max
111 assert second_bounds_min
<= second_bounds_max
112 overlapped_range_start
= max(first_bounds_min
, second_bounds_min
)
113 overlapped_range_end
= min(first_bounds_max
, second_bounds_max
)
114 return max(overlapped_range_end
- overlapped_range_start
, 0)