tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / html / TimeRanges.cpp
1 /*
2  * Copyright (C) 2007, 2009, 2010 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27
28 #include "TimeRanges.h"
29
30 #include "ExceptionCode.h"
31 #include <math.h>
32
33 using namespace WebCore;
34 using namespace std;
35
36 TimeRanges::TimeRanges(float start, float end)
37 {
38     add(start, end);
39 }
40
41 PassRefPtr<TimeRanges> TimeRanges::copy() const
42 {
43     RefPtr<TimeRanges> newSession = TimeRanges::create();
44     
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);
48     
49     return newSession.release();
50 }
51
52 void TimeRanges::invert()
53 {
54     RefPtr<TimeRanges> inverted = TimeRanges::create();
55     float posInf = std::numeric_limits<float>::infinity();
56     float negInf = -std::numeric_limits<float>::infinity();
57
58     if (!m_ranges.size())
59         inverted->add(negInf, posInf);
60     else {
61         if (float start = m_ranges.first().m_start != negInf)
62             inverted->add(negInf, start);
63
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);
66
67         if (float end = m_ranges.last().m_end != posInf)
68             inverted->add(end, posInf);
69     }
70
71     m_ranges.swap(inverted->m_ranges);
72 }
73
74 void TimeRanges::intersectWith(const TimeRanges* other)
75 {
76     ASSERT(other);
77     RefPtr<TimeRanges> inverted = copy();
78     RefPtr<TimeRanges> invertedOther = other->copy();
79     inverted->unionWith(invertedOther.get());
80     inverted->invert();
81
82     m_ranges.swap(inverted->m_ranges);
83 }
84
85 void TimeRanges::unionWith(const TimeRanges* other)
86 {
87     ASSERT(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);
92     }
93
94     m_ranges.swap(unioned->m_ranges);
95 }
96
97 float TimeRanges::start(unsigned index, ExceptionCode& ec) const 
98
99     if (index >= length()) {
100         ec = INDEX_SIZE_ERR;
101         return 0;
102     }
103     return m_ranges[index].m_start;
104 }
105
106 float TimeRanges::end(unsigned index, ExceptionCode& ec) const 
107
108     if (index >= length()) {
109         ec = INDEX_SIZE_ERR;
110         return 0;
111     }
112     return m_ranges[index].m_end;
113 }
114
115 void TimeRanges::add(float start, float end) 
116 {
117     ASSERT(start <= end);
118     unsigned int overlappingArcIndex;
119     Range addedRange(start, end);
120
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.
125     //
126     // TODO: Given that we assume that ranges are correctly ordered, this could be optimized.
127
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--;
135         } else {
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.
141                     break;
142                 }
143             } else {
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.
148                     break;
149                 }
150             }
151         }
152     }
153
154     // Now that we are sure we don't overlap with any range, just add it.
155     m_ranges.insert(overlappingArcIndex, addedRange);
156 }
157
158 bool TimeRanges::contain(float time) const
159
160     ExceptionCode unused;
161     for (unsigned n = 0; n < length(); n++) {
162         if (time >= start(n, unused) && time <= end(n, unused))
163             return true;
164     }
165     return false;
166 }
167
168 float TimeRanges::nearest(float time) const
169
170     ExceptionCode unused;
171     float closest = 0;
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)
177             return time;
178         if (fabs(startTime - time) < closest)
179             closest = fabsf(startTime - time);
180         else if (fabs(endTime - time) < closest)
181             closest = fabsf(endTime - time);
182     }
183     return closest;
184 }