OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_RANGES_H_ | 5 #ifndef MEDIA_BASE_RANGES_H_ |
6 #define MEDIA_BASE_RANGES_H_ | 6 #define MEDIA_BASE_RANGES_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <ostream> | 9 #include <ostream> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/time.h" |
14 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
15 | 16 |
16 namespace media { | 17 namespace media { |
17 | 18 |
18 // Ranges allows holding an ordered list of ranges of [start,end) intervals. | 19 // Ranges allows holding an ordered list of ranges of [start,end) intervals. |
19 // The canonical example use-case is holding the list of ranges of buffered | 20 // The canonical example use-case is holding the list of ranges of buffered |
20 // bytes or times in a <video> tag. | 21 // bytes or times in a <video> tag. |
21 template<class T> // Endpoint type; typically a base::TimeDelta or an int64. | 22 template<class T> // Endpoint type; typically a base::TimeDelta or an int64. |
22 class Ranges { | 23 class Ranges { |
23 public: | 24 public: |
(...skipping 10 matching lines...) Expand all Loading... |
34 T start(int i) const; | 35 T start(int i) const; |
35 T end(int i) const; | 36 T end(int i) const; |
36 | 37 |
37 // Clear all ranges. | 38 // Clear all ranges. |
38 void clear(); | 39 void clear(); |
39 | 40 |
40 // Computes the intersection between this range and |other|. | 41 // Computes the intersection between this range and |other|. |
41 Ranges<T> IntersectionWith(const Ranges<T>& other); | 42 Ranges<T> IntersectionWith(const Ranges<T>& other); |
42 | 43 |
43 private: | 44 private: |
| 45 // Wrapper around DCHECK_LT allowing comparisons of operator<<'able T's. |
| 46 void DCheckLT(const T& lhs, const T& rhs) const; |
| 47 |
44 // Disjoint, in increasing order of start. | 48 // Disjoint, in increasing order of start. |
45 std::vector<std::pair<T, T> > ranges_; | 49 std::vector<std::pair<T, T> > ranges_; |
46 }; | 50 }; |
47 | 51 |
48 ////////////////////////////////////////////////////////////////////// | 52 ////////////////////////////////////////////////////////////////////// |
49 // EVERYTHING BELOW HERE IS IMPLEMENTATION DETAIL!! | 53 // EVERYTHING BELOW HERE IS IMPLEMENTATION DETAIL!! |
50 ////////////////////////////////////////////////////////////////////// | 54 ////////////////////////////////////////////////////////////////////// |
51 | 55 |
52 template<class T> | 56 template<class T> |
53 size_t Ranges<T>::Add(T start, T end) { | 57 size_t Ranges<T>::Add(T start, T end) { |
54 if (start == end) // Nothing to be done with empty ranges. | 58 if (start == end) // Nothing to be done with empty ranges. |
55 return ranges_.size(); | 59 return ranges_.size(); |
56 | 60 |
57 DCHECK(start < end); | 61 DCheckLT(start, end); |
58 size_t i; | 62 size_t i; |
59 // Walk along the array of ranges until |start| is no longer larger than the | 63 // Walk along the array of ranges until |start| is no longer larger than the |
60 // current interval's end. | 64 // current interval's end. |
61 for (i = 0; i < ranges_.size() && ranges_[i].second < start; ++i) { | 65 for (i = 0; i < ranges_.size() && ranges_[i].second < start; ++i) { |
62 // Empty body | 66 // Empty body |
63 } | 67 } |
64 | 68 |
65 // Now we know |start| belongs in the i'th slot. | 69 // Now we know |start| belongs in the i'th slot. |
66 // If i is the end of the range, append new range and done. | 70 // If i is the end of the range, append new range and done. |
67 if (i == ranges_.size()) { | 71 if (i == ranges_.size()) { |
(...skipping 24 matching lines...) Expand all Loading... |
92 // original loop went too far. | 96 // original loop went too far. |
93 while ((i + 1) < ranges_.size() && | 97 while ((i + 1) < ranges_.size() && |
94 ranges_[i + 1].first <= ranges_[i].second) { | 98 ranges_[i + 1].first <= ranges_[i].second) { |
95 ranges_[i].second = std::max(ranges_[i].second, ranges_[i + 1].second); | 99 ranges_[i].second = std::max(ranges_[i].second, ranges_[i + 1].second); |
96 ranges_.erase(ranges_.begin() + i + 1); | 100 ranges_.erase(ranges_.begin() + i + 1); |
97 } | 101 } |
98 | 102 |
99 return ranges_.size(); | 103 return ranges_.size(); |
100 } | 104 } |
101 | 105 |
| 106 template<> |
| 107 void Ranges<base::TimeDelta>::DCheckLT(const base::TimeDelta& lhs, |
| 108 const base::TimeDelta& rhs) const; |
| 109 |
| 110 template<class T> |
| 111 void Ranges<T>::DCheckLT(const T& lhs, const T& rhs) const { |
| 112 DCHECK_LT(lhs, rhs); |
| 113 } |
| 114 |
102 template<class T> | 115 template<class T> |
103 size_t Ranges<T>::size() const { | 116 size_t Ranges<T>::size() const { |
104 return ranges_.size(); | 117 return ranges_.size(); |
105 } | 118 } |
106 | 119 |
107 template<class T> | 120 template<class T> |
108 T Ranges<T>::start(int i) const { | 121 T Ranges<T>::start(int i) const { |
109 return ranges_[i].first; | 122 return ranges_[i].first; |
110 } | 123 } |
111 | 124 |
(...skipping 27 matching lines...) Expand all Loading... |
139 else | 152 else |
140 ++j; | 153 ++j; |
141 } | 154 } |
142 | 155 |
143 return result; | 156 return result; |
144 } | 157 } |
145 | 158 |
146 } // namespace media | 159 } // namespace media |
147 | 160 |
148 #endif // MEDIA_BASE_RANGES_H_ | 161 #endif // MEDIA_BASE_RANGES_H_ |
OLD | NEW |