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 <ostream> | 9 #include <ostream> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/logging.h" | 13 #include "base/logging.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. |
(...skipping 11 matching lines...) Expand all Loading... | |
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 |
40 // Computes the intersection between this range and |other|. | |
41 Ranges<T> IntersectionWith(const Ranges<T>& other); | |
42 | |
39 private: | 43 private: |
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> |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 template<class T> | 112 template<class T> |
109 T Ranges<T>::end(int i) const { | 113 T Ranges<T>::end(int i) const { |
110 return ranges_[i].second; | 114 return ranges_[i].second; |
111 } | 115 } |
112 | 116 |
113 template<class T> | 117 template<class T> |
114 void Ranges<T>::clear() { | 118 void Ranges<T>::clear() { |
115 ranges_.clear(); | 119 ranges_.clear(); |
116 } | 120 } |
117 | 121 |
122 template<class T> | |
123 Ranges<T> Ranges<T>::IntersectionWith(const Ranges<T>& other) { | |
124 Ranges<T> result; | |
125 | |
126 if (!size() || !other.size()) | |
Ami GONE FROM CHROMIUM
2012/06/19 17:40:37
Unnecessary?
acolwell GONE FROM CHROMIUM
2012/06/19 19:50:15
Done.
| |
127 return result; | |
128 | |
129 size_t i = 0; | |
130 size_t j = 0; | |
131 | |
132 while (i < size() && j < other.size()) { | |
133 T max_start = std::max(start(i), other.start(j)); | |
134 T min_end = std::min(end(i), other.end(j)); | |
135 | |
136 // Add an intersection range to the result if the ranges overlap. | |
137 if (max_start < min_end) | |
138 result.Add(max_start, min_end); | |
139 | |
140 if (end(i) < other.end(j)) { | |
Ami GONE FROM CHROMIUM
2012/06/19 17:40:37
l.140-146 would be shorter and clearer, IMO, as:
i
acolwell GONE FROM CHROMIUM
2012/06/19 19:50:15
Done.
| |
141 i++; | |
142 continue; | |
143 } | |
144 | |
145 j++; | |
146 } | |
147 | |
148 return result; | |
149 } | |
150 | |
118 } // namespace media | 151 } // namespace media |
119 | 152 |
120 #endif // MEDIA_BASE_RANGES_H_ | 153 #endif // MEDIA_BASE_RANGES_H_ |
OLD | NEW |