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 <ostream> | 8 #include <ostream> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/time.h" |
13 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
14 | 15 |
15 namespace media { | 16 namespace media { |
16 | 17 |
17 // Ranges allows holding an ordered list of ranges of [start,end) intervals. | 18 // Ranges allows holding an ordered list of ranges of [start,end) intervals. |
18 // The canonical example use-case is holding the list of ranges of buffered | 19 // The canonical example use-case is holding the list of ranges of buffered |
19 // bytes or times in a <video> tag. | 20 // bytes or times in a <video> tag. |
20 template<class T> // Endpoint type; typically a base::TimeDelta or an int64. | 21 template<class T> // Endpoint type; typically a base::TimeDelta or an int64. |
21 class Ranges { | 22 class Ranges { |
22 public: | 23 public: |
23 // Allow copy & assign. | 24 // Allow copy & assign. |
24 | 25 |
25 // Add (start,end) to this object, coallescing overlaps as appropriate. | 26 // Add (start,end) to this object, coallescing overlaps as appropriate. |
26 // Returns the number of stored ranges, post coallescing. | 27 // Returns the number of stored ranges, post coallescing. |
27 size_t Add(T start, T end); | 28 size_t Add(T start, T end); |
28 | 29 |
29 // Return the number of disjoint ranges. | 30 // Return the number of disjoint ranges. |
30 size_t size() const; | 31 size_t size() const; |
31 | 32 |
32 // Return the "i"'th range's start & end (0-based). | 33 // Return the "i"'th range's start & end (0-based). |
33 T start(int i) const; | 34 T start(int i) const; |
34 T end(int i) const; | 35 T end(int i) const; |
35 | 36 |
36 // Clear all ranges. | 37 // Clear all ranges. |
37 void clear(); | 38 void clear(); |
38 | 39 |
39 private: | 40 private: |
| 41 // Wrapper around DCHECK_LT allowing comparisons of operator<<'able T's. |
| 42 void DCheckLT(const T& lhs, const T& rhs) const; |
| 43 |
40 // Disjoint, in increasing order of start. | 44 // Disjoint, in increasing order of start. |
41 std::vector<std::pair<T, T> > ranges_; | 45 std::vector<std::pair<T, T> > ranges_; |
42 }; | 46 }; |
43 | 47 |
44 ////////////////////////////////////////////////////////////////////// | 48 ////////////////////////////////////////////////////////////////////// |
45 // EVERYTHING BELOW HERE IS IMPLEMENTATION DETAIL!! | 49 // EVERYTHING BELOW HERE IS IMPLEMENTATION DETAIL!! |
46 ////////////////////////////////////////////////////////////////////// | 50 ////////////////////////////////////////////////////////////////////// |
47 | 51 |
48 template<class T> | 52 template<class T> |
49 size_t Ranges<T>::Add(T start, T end) { | 53 size_t Ranges<T>::Add(T start, T end) { |
50 if (start == end) // Nothing to be done with empty ranges. | 54 if (start == end) // Nothing to be done with empty ranges. |
51 return ranges_.size(); | 55 return ranges_.size(); |
52 | 56 |
53 DCHECK(start < end); | 57 DCheckLT(start, end); |
54 size_t i; | 58 size_t i; |
55 // Walk along the array of ranges until |start| is no longer larger than the | 59 // Walk along the array of ranges until |start| is no longer larger than the |
56 // current interval's end. | 60 // current interval's end. |
57 for (i = 0; i < ranges_.size() && ranges_[i].second < start; ++i) { | 61 for (i = 0; i < ranges_.size() && ranges_[i].second < start; ++i) { |
58 // Empty body | 62 // Empty body |
59 } | 63 } |
60 | 64 |
61 // Now we know |start| belongs in the i'th slot. | 65 // Now we know |start| belongs in the i'th slot. |
62 // If i is the end of the range, append new range and done. | 66 // If i is the end of the range, append new range and done. |
63 if (i == ranges_.size()) { | 67 if (i == ranges_.size()) { |
(...skipping 24 matching lines...) Expand all Loading... |
88 // original loop went too far. | 92 // original loop went too far. |
89 while ((i + 1) < ranges_.size() && | 93 while ((i + 1) < ranges_.size() && |
90 ranges_[i + 1].first <= ranges_[i].second) { | 94 ranges_[i + 1].first <= ranges_[i].second) { |
91 ranges_[i].second = std::max(ranges_[i].second, ranges_[i + 1].second); | 95 ranges_[i].second = std::max(ranges_[i].second, ranges_[i + 1].second); |
92 ranges_.erase(ranges_.begin() + i + 1); | 96 ranges_.erase(ranges_.begin() + i + 1); |
93 } | 97 } |
94 | 98 |
95 return ranges_.size(); | 99 return ranges_.size(); |
96 } | 100 } |
97 | 101 |
| 102 template<> |
| 103 void Ranges<base::TimeDelta>::DCheckLT(const base::TimeDelta& lhs, |
| 104 const base::TimeDelta& rhs) const; |
| 105 |
| 106 template<class T> |
| 107 void Ranges<T>::DCheckLT(const T& lhs, const T& rhs) const { |
| 108 DCHECK_LT(lhs, rhs); |
| 109 } |
| 110 |
98 template<class T> | 111 template<class T> |
99 size_t Ranges<T>::size() const { | 112 size_t Ranges<T>::size() const { |
100 return ranges_.size(); | 113 return ranges_.size(); |
101 } | 114 } |
102 | 115 |
103 template<class T> | 116 template<class T> |
104 T Ranges<T>::start(int i) const { | 117 T Ranges<T>::start(int i) const { |
105 return ranges_[i].first; | 118 return ranges_[i].first; |
106 } | 119 } |
107 | 120 |
108 template<class T> | 121 template<class T> |
109 T Ranges<T>::end(int i) const { | 122 T Ranges<T>::end(int i) const { |
110 return ranges_[i].second; | 123 return ranges_[i].second; |
111 } | 124 } |
112 | 125 |
113 template<class T> | 126 template<class T> |
114 void Ranges<T>::clear() { | 127 void Ranges<T>::clear() { |
115 ranges_.clear(); | 128 ranges_.clear(); |
116 } | 129 } |
117 | 130 |
118 } // namespace media | 131 } // namespace media |
119 | 132 |
120 #endif // MEDIA_BASE_RANGES_H_ | 133 #endif // MEDIA_BASE_RANGES_H_ |
OLD | NEW |