Index: media/base/ranges.h |
diff --git a/media/base/ranges.h b/media/base/ranges.h |
index c412b259b8ced5e73b434f4a99196c0f836c6e0c..1117ae58b8db86321df815f07f1646d996f3a299 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,30 @@ void Ranges<T>::clear() { |
ranges_.clear(); |
} |
+template<class T> |
+Ranges<T> Ranges<T>::IntersectionWith(const Ranges<T>& other) { |
+ Ranges<T> 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)) |
+ ++i; |
+ else |
+ ++j; |
+ } |
+ |
+ return result; |
+} |
+ |
} // namespace media |
#endif // MEDIA_BASE_RANGES_H_ |