Chromium Code Reviews| Index: media/base/ranges.h |
| diff --git a/media/base/ranges.h b/media/base/ranges.h |
| index c412b259b8ced5e73b434f4a99196c0f836c6e0c..fb8c476cb01a2904466d36be1851f0ccfea79a15 100644 |
| --- a/media/base/ranges.h |
| +++ b/media/base/ranges.h |
| @@ -5,6 +5,7 @@ |
| #ifndef MEDIA_BASE_RANGES_H_ |
| #define MEDIA_BASE_RANGES_H_ |
| +#include <algorithm> |
| #include <ostream> |
| #include <vector> |
| @@ -36,6 +37,9 @@ class Ranges { |
| // Clear all ranges. |
| void clear(); |
| + // Computes the intersection between this range and |other|. |
| + Ranges<T> IntersectionWith(const Ranges<T>& other); |
| + |
| private: |
| // Disjoint, in increasing order of start. |
| std::vector<std::pair<T, T> > ranges_; |
| @@ -115,6 +119,35 @@ void Ranges<T>::clear() { |
| ranges_.clear(); |
| } |
| +template<class T> |
| +Ranges<T> Ranges<T>::IntersectionWith(const Ranges<T>& other) { |
| + Ranges<T> result; |
| + |
| + 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.
|
| + return result; |
| + |
| + size_t i = 0; |
| + size_t j = 0; |
| + |
| + while (i < size() && j < other.size()) { |
| + T max_start = std::max(start(i), other.start(j)); |
| + T min_end = std::min(end(i), other.end(j)); |
| + |
| + // Add an intersection range to the result if the ranges overlap. |
| + if (max_start < min_end) |
| + result.Add(max_start, min_end); |
| + |
| + 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.
|
| + i++; |
| + continue; |
| + } |
| + |
| + j++; |
| + } |
| + |
| + return result; |
| +} |
| + |
| } // namespace media |
| #endif // MEDIA_BASE_RANGES_H_ |