| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 #include "TimeRanges.h" | 28 #include "TimeRanges.h" |
| 29 | 29 |
| 30 #include "ExceptionCode.h" | 30 #include "ExceptionCode.h" |
| 31 #include "ExceptionCodePlaceholder.h" | 31 #include "ExceptionCodePlaceholder.h" |
| 32 #include <math.h> | 32 #include <math.h> |
| 33 | 33 |
| 34 using namespace WebCore; | 34 using namespace WebCore; |
| 35 using namespace std; | 35 using namespace std; |
| 36 | 36 |
| 37 TimeRanges::TimeRanges(float start, float end) | 37 TimeRanges::TimeRanges(double start, double end) |
| 38 { | 38 { |
| 39 add(start, end); | 39 add(start, end); |
| 40 } | 40 } |
| 41 | 41 |
| 42 PassRefPtr<TimeRanges> TimeRanges::copy() const | 42 PassRefPtr<TimeRanges> TimeRanges::copy() const |
| 43 { | 43 { |
| 44 RefPtr<TimeRanges> newSession = TimeRanges::create(); | 44 RefPtr<TimeRanges> newSession = TimeRanges::create(); |
| 45 | 45 |
| 46 unsigned size = m_ranges.size(); | 46 unsigned size = m_ranges.size(); |
| 47 for (unsigned i = 0; i < size; i++) | 47 for (unsigned i = 0; i < size; i++) |
| 48 newSession->add(m_ranges[i].m_start, m_ranges[i].m_end); | 48 newSession->add(m_ranges[i].m_start, m_ranges[i].m_end); |
| 49 | 49 |
| 50 return newSession.release(); | 50 return newSession.release(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void TimeRanges::invert() | 53 void TimeRanges::invert() |
| 54 { | 54 { |
| 55 RefPtr<TimeRanges> inverted = TimeRanges::create(); | 55 RefPtr<TimeRanges> inverted = TimeRanges::create(); |
| 56 float posInf = std::numeric_limits<float>::infinity(); | 56 double posInf = std::numeric_limits<double>::infinity(); |
| 57 float negInf = -std::numeric_limits<float>::infinity(); | 57 double negInf = -std::numeric_limits<double>::infinity(); |
| 58 | 58 |
| 59 if (!m_ranges.size()) | 59 if (!m_ranges.size()) |
| 60 inverted->add(negInf, posInf); | 60 inverted->add(negInf, posInf); |
| 61 else { | 61 else { |
| 62 if (float start = m_ranges.first().m_start != negInf) | 62 if (double start = m_ranges.first().m_start != negInf) |
| 63 inverted->add(negInf, start); | 63 inverted->add(negInf, start); |
| 64 | 64 |
| 65 for (size_t index = 0; index + 1 < m_ranges.size(); ++index) | 65 for (size_t index = 0; index + 1 < m_ranges.size(); ++index) |
| 66 inverted->add(m_ranges[index].m_end, m_ranges[index + 1].m_start); | 66 inverted->add(m_ranges[index].m_end, m_ranges[index + 1].m_start); |
| 67 | 67 |
| 68 if (float end = m_ranges.last().m_end != posInf) | 68 if (double end = m_ranges.last().m_end != posInf) |
| 69 inverted->add(end, posInf); | 69 inverted->add(end, posInf); |
| 70 } | 70 } |
| 71 | 71 |
| 72 m_ranges.swap(inverted->m_ranges); | 72 m_ranges.swap(inverted->m_ranges); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void TimeRanges::intersectWith(const TimeRanges* other) | 75 void TimeRanges::intersectWith(const TimeRanges* other) |
| 76 { | 76 { |
| 77 ASSERT(other); | 77 ASSERT(other); |
| 78 RefPtr<TimeRanges> inverted = copy(); | 78 RefPtr<TimeRanges> inverted = copy(); |
| 79 RefPtr<TimeRanges> invertedOther = other->copy(); | 79 RefPtr<TimeRanges> invertedOther = other->copy(); |
| 80 inverted->unionWith(invertedOther.get()); | 80 inverted->unionWith(invertedOther.get()); |
| 81 inverted->invert(); | 81 inverted->invert(); |
| 82 | 82 |
| 83 m_ranges.swap(inverted->m_ranges); | 83 m_ranges.swap(inverted->m_ranges); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void TimeRanges::unionWith(const TimeRanges* other) | 86 void TimeRanges::unionWith(const TimeRanges* other) |
| 87 { | 87 { |
| 88 ASSERT(other); | 88 ASSERT(other); |
| 89 RefPtr<TimeRanges> unioned = copy(); | 89 RefPtr<TimeRanges> unioned = copy(); |
| 90 for (size_t index = 0; index < other->m_ranges.size(); ++index) { | 90 for (size_t index = 0; index < other->m_ranges.size(); ++index) { |
| 91 const Range& range = other->m_ranges[index]; | 91 const Range& range = other->m_ranges[index]; |
| 92 unioned->add(range.m_start, range.m_end); | 92 unioned->add(range.m_start, range.m_end); |
| 93 } | 93 } |
| 94 | 94 |
| 95 m_ranges.swap(unioned->m_ranges); | 95 m_ranges.swap(unioned->m_ranges); |
| 96 } | 96 } |
| 97 | 97 |
| 98 float TimeRanges::start(unsigned index, ExceptionCode& ec) const | 98 double TimeRanges::start(unsigned index, ExceptionCode& ec) const |
| 99 { | 99 { |
| 100 if (index >= length()) { | 100 if (index >= length()) { |
| 101 ec = INDEX_SIZE_ERR; | 101 ec = INDEX_SIZE_ERR; |
| 102 return 0; | 102 return 0; |
| 103 } | 103 } |
| 104 return m_ranges[index].m_start; | 104 return m_ranges[index].m_start; |
| 105 } | 105 } |
| 106 | 106 |
| 107 float TimeRanges::end(unsigned index, ExceptionCode& ec) const | 107 double TimeRanges::end(unsigned index, ExceptionCode& ec) const |
| 108 { | 108 { |
| 109 if (index >= length()) { | 109 if (index >= length()) { |
| 110 ec = INDEX_SIZE_ERR; | 110 ec = INDEX_SIZE_ERR; |
| 111 return 0; | 111 return 0; |
| 112 } | 112 } |
| 113 return m_ranges[index].m_end; | 113 return m_ranges[index].m_end; |
| 114 } | 114 } |
| 115 | 115 |
| 116 void TimeRanges::add(float start, float end) | 116 void TimeRanges::add(double start, double end) |
| 117 { | 117 { |
| 118 ASSERT(start <= end); | 118 ASSERT(start <= end); |
| 119 unsigned int overlappingArcIndex; | 119 unsigned int overlappingArcIndex; |
| 120 Range addedRange(start, end); | 120 Range addedRange(start, end); |
| 121 | 121 |
| 122 // For each present range check if we need to: | 122 // For each present range check if we need to: |
| 123 // - merge with the added range, in case we are overlapping or contiguous | 123 // - merge with the added range, in case we are overlapping or contiguous |
| 124 // - Need to insert in place, we we are completely, not overlapping and not
contiguous | 124 // - Need to insert in place, we we are completely, not overlapping and not
contiguous |
| 125 // in between two ranges. | 125 // in between two ranges. |
| 126 // | 126 // |
| (...skipping 22 matching lines...) Expand all Loading... |
| 149 break; | 149 break; |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Now that we are sure we don't overlap with any range, just add it. | 155 // Now that we are sure we don't overlap with any range, just add it. |
| 156 m_ranges.insert(overlappingArcIndex, addedRange); | 156 m_ranges.insert(overlappingArcIndex, addedRange); |
| 157 } | 157 } |
| 158 | 158 |
| 159 bool TimeRanges::contain(float time) const | 159 bool TimeRanges::contain(double time) const |
| 160 { | 160 { |
| 161 for (unsigned n = 0; n < length(); n++) { | 161 for (unsigned n = 0; n < length(); n++) { |
| 162 if (time >= start(n, IGNORE_EXCEPTION) && time <= end(n, IGNORE_EXCEPTIO
N)) | 162 if (time >= start(n, IGNORE_EXCEPTION) && time <= end(n, IGNORE_EXCEPTIO
N)) |
| 163 return true; | 163 return true; |
| 164 } | 164 } |
| 165 return false; | 165 return false; |
| 166 } | 166 } |
| 167 | 167 |
| 168 float TimeRanges::nearest(float time) const | 168 double TimeRanges::nearest(double time) const |
| 169 { | 169 { |
| 170 float closest = 0; | 170 double closest = 0; |
| 171 unsigned count = length(); | 171 unsigned count = length(); |
| 172 for (unsigned ndx = 0; ndx < count; ndx++) { | 172 for (unsigned ndx = 0; ndx < count; ndx++) { |
| 173 float startTime = start(ndx, IGNORE_EXCEPTION); | 173 double startTime = start(ndx, IGNORE_EXCEPTION); |
| 174 float endTime = end(ndx, IGNORE_EXCEPTION); | 174 double endTime = end(ndx, IGNORE_EXCEPTION); |
| 175 if (time >= startTime && time <= endTime) | 175 if (time >= startTime && time <= endTime) |
| 176 return time; | 176 return time; |
| 177 if (fabs(startTime - time) < closest) | 177 if (fabs(startTime - time) < closest) |
| 178 closest = fabsf(startTime - time); | 178 closest = fabsf(startTime - time); |
| 179 else if (fabs(endTime - time) < closest) | 179 else if (fabs(endTime - time) < closest) |
| 180 closest = fabsf(endTime - time); | 180 closest = fabsf(endTime - time); |
| 181 } | 181 } |
| 182 return closest; | 182 return closest; |
| 183 } | 183 } |
| OLD | NEW |