diff options
Diffstat (limited to 'media/base/ranges.h')
-rw-r--r-- | media/base/ranges.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/media/base/ranges.h b/media/base/ranges.h index c412b25..1117ae5 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_ |