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