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 size_t i = 0; |
| 127 size_t j = 0; |
| 128 |
| 129 while (i < size() && j < other.size()) { |
| 130 T max_start = std::max(start(i), other.start(j)); |
| 131 T min_end = std::min(end(i), other.end(j)); |
| 132 |
| 133 // Add an intersection range to the result if the ranges overlap. |
| 134 if (max_start < min_end) |
| 135 result.Add(max_start, min_end); |
| 136 |
| 137 if (end(i) < other.end(j)) |
| 138 ++i; |
| 139 else |
| 140 ++j; |
| 141 } |
| 142 |
| 143 return result; |
| 144 } |
| 145 |
118 } // namespace media | 146 } // namespace media |
119 | 147 |
120 #endif // MEDIA_BASE_RANGES_H_ | 148 #endif // MEDIA_BASE_RANGES_H_ |
OLD | NEW |