2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "TimeRanges.h"
30 #include "ExceptionCode.h"
33 using namespace WebCore;
36 TimeRanges::TimeRanges(float start, float end)
41 PassRefPtr<TimeRanges> TimeRanges::copy() const
43 RefPtr<TimeRanges> newSession = TimeRanges::create();
45 unsigned size = m_ranges.size();
46 for (unsigned i = 0; i < size; i++)
47 newSession->add(m_ranges[i].m_start, m_ranges[i].m_end);
49 return newSession.release();
52 void TimeRanges::invert()
54 RefPtr<TimeRanges> inverted = TimeRanges::create();
55 float posInf = std::numeric_limits<float>::infinity();
56 float negInf = -std::numeric_limits<float>::infinity();
59 inverted->add(negInf, posInf);
61 if (float start = m_ranges.first().m_start != negInf)
62 inverted->add(negInf, start);
64 for (size_t index = 0; index + 1 < m_ranges.size(); ++index)
65 inverted->add(m_ranges[index].m_end, m_ranges[index + 1].m_start);
67 if (float end = m_ranges.last().m_end != posInf)
68 inverted->add(end, posInf);
71 m_ranges.swap(inverted->m_ranges);
74 void TimeRanges::intersectWith(const TimeRanges* other)
77 RefPtr<TimeRanges> inverted = copy();
78 RefPtr<TimeRanges> invertedOther = other->copy();
79 inverted->unionWith(invertedOther.get());
82 m_ranges.swap(inverted->m_ranges);
85 void TimeRanges::unionWith(const TimeRanges* other)
88 RefPtr<TimeRanges> unioned = copy();
89 for (size_t index = 0; index < other->m_ranges.size(); ++index) {
90 const Range& range = other->m_ranges[index];
91 unioned->add(range.m_start, range.m_end);
94 m_ranges.swap(unioned->m_ranges);
97 float TimeRanges::start(unsigned index, ExceptionCode& ec) const
99 if (index >= length()) {
103 return m_ranges[index].m_start;
106 float TimeRanges::end(unsigned index, ExceptionCode& ec) const
108 if (index >= length()) {
112 return m_ranges[index].m_end;
115 void TimeRanges::add(float start, float end)
117 ASSERT(start <= end);
118 unsigned int overlappingArcIndex;
119 Range addedRange(start, end);
121 // For each present range check if we need to:
122 // - merge with the added range, in case we are overlapping or contiguous
123 // - Need to insert in place, we we are completely, not overlapping and not contiguous
124 // in between two ranges.
126 // TODO: Given that we assume that ranges are correctly ordered, this could be optimized.
128 for (overlappingArcIndex = 0; overlappingArcIndex < m_ranges.size(); overlappingArcIndex++) {
129 if (addedRange.isOverlappingRange(m_ranges[overlappingArcIndex])
130 || addedRange.isContiguousWithRange(m_ranges[overlappingArcIndex])) {
131 // We need to merge the addedRange and that range.
132 addedRange = addedRange.unionWithOverlappingOrContiguousRange(m_ranges[overlappingArcIndex]);
133 m_ranges.remove(overlappingArcIndex);
134 overlappingArcIndex--;
136 // Check the case for which there is no more to do
137 if (!overlappingArcIndex) {
138 if (addedRange.isBeforeRange(m_ranges[0])) {
139 // First index, and we are completely before that range (and not contiguous, nor overlapping).
140 // We just need to be inserted here.
144 if (m_ranges[overlappingArcIndex - 1].isBeforeRange(addedRange)
145 && addedRange.isBeforeRange(m_ranges[overlappingArcIndex])) {
146 // We are exactly after the current previous range, and before the current range, while
147 // not overlapping with none of them. Insert here.
154 // Now that we are sure we don't overlap with any range, just add it.
155 m_ranges.insert(overlappingArcIndex, addedRange);
158 bool TimeRanges::contain(float time) const
160 ExceptionCode unused;
161 for (unsigned n = 0; n < length(); n++) {
162 if (time >= start(n, unused) && time <= end(n, unused))
168 float TimeRanges::nearest(float time) const
170 ExceptionCode unused;
172 unsigned count = length();
173 for (unsigned ndx = 0; ndx < count; ndx++) {
174 float startTime = start(ndx, unused);
175 float endTime = end(ndx, unused);
176 if (time >= startTime && time <= endTime)
178 if (fabs(startTime - time) < closest)
179 closest = fabsf(startTime - time);
180 else if (fabs(endTime - time) < closest)
181 closest = fabsf(endTime - time);