2015-05-07 03:24:10 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef INTERVALS_H
|
|
|
|
#define INTERVALS_H
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
// Specialization for nsTArray CopyChooser.
|
|
|
|
namespace mozilla {
|
|
|
|
namespace media {
|
|
|
|
template <class T>
|
|
|
|
class IntervalSet;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|
2015-05-25 08:09:16 +03:00
|
|
|
|
|
|
|
template <class E>
|
|
|
|
struct nsTArray_CopyChooser<mozilla::media::IntervalSet<E>> {
|
|
|
|
typedef nsTArray_CopyWithConstructors<mozilla::media::IntervalSet<E>> Type;
|
|
|
|
};
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
/* Interval defines an interval between two points. Unlike a traditional
|
|
|
|
interval [A,B] where A <= x <= B, the upper boundary B is exclusive: A <= x <
|
|
|
|
B (e.g [A,B[ or [A,B) depending on where you're living) It provides basic
|
|
|
|
interval arithmetic and fuzzy edges. The type T must provides a default
|
|
|
|
constructor and +, -, <, <= and == operators.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class Interval {
|
|
|
|
public:
|
|
|
|
typedef Interval<T> SelfType;
|
|
|
|
|
|
|
|
Interval() : mStart(T()), mEnd(T()), mFuzz(T()) {}
|
|
|
|
|
|
|
|
template <typename StartArg, typename EndArg>
|
|
|
|
Interval(StartArg&& aStart, EndArg&& aEnd)
|
2018-06-01 19:30:30 +03:00
|
|
|
: mStart(std::forward<StartArg>(aStart)),
|
|
|
|
mEnd(std::forward<EndArg>(aEnd)),
|
2015-05-07 03:24:10 +03:00
|
|
|
mFuzz() {
|
2019-02-26 09:41:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStart <= aEnd, "Invalid Interval");
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename StartArg, typename EndArg, typename FuzzArg>
|
|
|
|
Interval(StartArg&& aStart, EndArg&& aEnd, FuzzArg&& aFuzz)
|
2018-06-01 19:30:30 +03:00
|
|
|
: mStart(std::forward<StartArg>(aStart)),
|
|
|
|
mEnd(std::forward<EndArg>(aEnd)),
|
|
|
|
mFuzz(std::forward<FuzzArg>(aFuzz)) {
|
2019-02-26 09:41:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStart <= aEnd, "Invalid Interval");
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Interval(const SelfType& aOther)
|
|
|
|
: mStart(aOther.mStart), mEnd(aOther.mEnd), mFuzz(aOther.mFuzz) {}
|
|
|
|
|
|
|
|
Interval(SelfType&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mStart(std::move(aOther.mStart)),
|
|
|
|
mEnd(std::move(aOther.mEnd)),
|
|
|
|
mFuzz(std::move(aOther.mFuzz)) {}
|
2015-05-07 03:24:10 +03:00
|
|
|
|
|
|
|
SelfType& operator=(const SelfType& aOther) {
|
|
|
|
mStart = aOther.mStart;
|
|
|
|
mEnd = aOther.mEnd;
|
|
|
|
mFuzz = aOther.mFuzz;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator=(SelfType&& aOther) {
|
|
|
|
MOZ_ASSERT(&aOther != this, "self-moves are prohibited");
|
|
|
|
this->~Interval();
|
2018-05-30 22:15:35 +03:00
|
|
|
new (this) Interval(std::move(aOther));
|
2015-05-07 03:24:10 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic interval arithmetic operator definition.
|
|
|
|
SelfType operator+(const SelfType& aOther) const {
|
|
|
|
return SelfType(mStart + aOther.mStart, mEnd + aOther.mEnd,
|
|
|
|
mFuzz + aOther.mFuzz);
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:31:00 +03:00
|
|
|
SelfType operator+(const T& aVal) const {
|
|
|
|
return SelfType(mStart + aVal, mEnd + aVal, mFuzz);
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
SelfType operator-(const SelfType& aOther) const {
|
|
|
|
return SelfType(mStart - aOther.mEnd, mEnd - aOther.mStart,
|
|
|
|
mFuzz + aOther.mFuzz);
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:31:00 +03:00
|
|
|
SelfType operator-(const T& aVal) const {
|
|
|
|
return SelfType(mStart - aVal, mEnd - aVal, mFuzz);
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:19:18 +03:00
|
|
|
SelfType& operator+=(const SelfType& aOther) {
|
|
|
|
mStart += aOther.mStart;
|
|
|
|
mEnd += aOther.mEnd;
|
|
|
|
mFuzz += aOther.mFuzz;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator+=(const T& aVal) {
|
|
|
|
mStart += aVal;
|
|
|
|
mEnd += aVal;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator-=(const SelfType& aOther) {
|
|
|
|
mStart -= aOther.mStart;
|
|
|
|
mEnd -= aOther.mEnd;
|
|
|
|
mFuzz += aOther.mFuzz;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator-=(const T& aVal) {
|
|
|
|
mStart -= aVal;
|
|
|
|
mEnd -= aVal;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
bool operator==(const SelfType& aOther) const {
|
|
|
|
return mStart == aOther.mStart && mEnd == aOther.mEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SelfType& aOther) const { return !(*this == aOther); }
|
|
|
|
|
|
|
|
bool Contains(const T& aX) const {
|
|
|
|
return mStart - mFuzz <= aX && aX < mEnd + mFuzz;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContainsStrict(const T& aX) const { return mStart <= aX && aX < mEnd; }
|
|
|
|
|
2015-06-12 02:26:58 +03:00
|
|
|
bool ContainsWithStrictEnd(const T& aX) const {
|
|
|
|
return mStart - mFuzz <= aX && aX < mEnd;
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
bool Contains(const SelfType& aOther) const {
|
2017-09-04 12:27:43 +03:00
|
|
|
return (mStart - mFuzz <= aOther.mStart + aOther.mFuzz) &&
|
|
|
|
(aOther.mEnd - aOther.mFuzz <= mEnd + mFuzz);
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ContainsStrict(const SelfType& aOther) const {
|
|
|
|
return mStart <= aOther.mStart && aOther.mEnd <= mEnd;
|
|
|
|
}
|
|
|
|
|
2015-06-12 02:26:58 +03:00
|
|
|
bool ContainsWithStrictEnd(const SelfType& aOther) const {
|
2017-09-04 12:27:43 +03:00
|
|
|
return (mStart - mFuzz <= aOther.mStart + aOther.mFuzz) &&
|
|
|
|
aOther.mEnd <= mEnd;
|
2015-06-12 02:26:58 +03:00
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
bool Intersects(const SelfType& aOther) const {
|
2017-09-04 12:27:43 +03:00
|
|
|
return (mStart - mFuzz < aOther.mEnd + aOther.mFuzz) &&
|
|
|
|
(aOther.mStart - aOther.mFuzz < mEnd + mFuzz);
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 13:17:27 +03:00
|
|
|
bool IntersectsStrict(const SelfType& aOther) const {
|
|
|
|
return mStart < aOther.mEnd && aOther.mStart < mEnd;
|
|
|
|
}
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
// Same as Intersects, but including the boundaries.
|
|
|
|
bool Touches(const SelfType& aOther) const {
|
2017-09-04 12:27:43 +03:00
|
|
|
return (mStart - mFuzz <= aOther.mEnd + aOther.mFuzz) &&
|
|
|
|
(aOther.mStart - aOther.mFuzz <= mEnd + mFuzz);
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if aOther is strictly to the right of this and contiguous.
|
|
|
|
// This operation isn't commutative.
|
|
|
|
bool Contiguous(const SelfType& aOther) const {
|
|
|
|
return mEnd <= aOther.mStart &&
|
|
|
|
aOther.mStart - mEnd <= mFuzz + aOther.mFuzz;
|
|
|
|
}
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
bool RightOf(const SelfType& aOther) const {
|
|
|
|
return aOther.mEnd - aOther.mFuzz <= mStart + mFuzz;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LeftOf(const SelfType& aOther) const {
|
|
|
|
return mEnd - mFuzz <= aOther.mStart + aOther.mFuzz;
|
|
|
|
}
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
SelfType Span(const SelfType& aOther) const {
|
2015-11-25 08:37:34 +03:00
|
|
|
if (IsEmpty()) {
|
|
|
|
return aOther;
|
|
|
|
}
|
2015-05-07 03:24:10 +03:00
|
|
|
SelfType result(*this);
|
|
|
|
if (aOther.mStart < mStart) {
|
|
|
|
result.mStart = aOther.mStart;
|
|
|
|
}
|
|
|
|
if (mEnd < aOther.mEnd) {
|
|
|
|
result.mEnd = aOther.mEnd;
|
|
|
|
}
|
|
|
|
if (mFuzz < aOther.mFuzz) {
|
|
|
|
result.mFuzz = aOther.mFuzz;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType Intersection(const SelfType& aOther) const {
|
|
|
|
const T& s = std::max(mStart, aOther.mStart);
|
|
|
|
const T& e = std::min(mEnd, aOther.mEnd);
|
|
|
|
const T& f = std::max(mFuzz, aOther.mFuzz);
|
|
|
|
if (s < e) {
|
|
|
|
return SelfType(s, e, f);
|
|
|
|
}
|
|
|
|
// Return an empty interval.
|
|
|
|
return SelfType();
|
|
|
|
}
|
|
|
|
|
|
|
|
T Length() const { return mEnd - mStart; }
|
|
|
|
|
|
|
|
bool IsEmpty() const { return mStart == mEnd; }
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
void SetFuzz(const T& aFuzz) { mFuzz = aFuzz; }
|
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
// Returns true if the two intervals intersect with this being on the right
|
|
|
|
// of aOther
|
|
|
|
bool TouchesOnRight(const SelfType& aOther) const {
|
2017-09-04 12:27:43 +03:00
|
|
|
return aOther.mStart <= mStart &&
|
|
|
|
(mStart - mFuzz <= aOther.mEnd + aOther.mFuzz) &&
|
|
|
|
(aOther.mStart - aOther.mFuzz <= mEnd + mFuzz);
|
2015-05-25 08:09:16 +03:00
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
T mStart;
|
|
|
|
T mEnd;
|
|
|
|
T mFuzz;
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
// An IntervalSet in a collection of Intervals. The IntervalSet is always
|
|
|
|
// normalized.
|
2015-05-07 03:24:10 +03:00
|
|
|
template <typename T>
|
|
|
|
class IntervalSet {
|
|
|
|
public:
|
|
|
|
typedef IntervalSet<T> SelfType;
|
|
|
|
typedef Interval<T> ElemType;
|
2016-02-02 18:36:30 +03:00
|
|
|
typedef AutoTArray<ElemType, 4> ContainerType;
|
2015-05-07 03:24:10 +03:00
|
|
|
typedef typename ContainerType::index_type IndexType;
|
|
|
|
|
|
|
|
IntervalSet() {}
|
2015-05-07 14:04:29 +03:00
|
|
|
virtual ~IntervalSet() {}
|
2015-05-07 03:24:10 +03:00
|
|
|
|
|
|
|
IntervalSet(const SelfType& aOther) : mIntervals(aOther.mIntervals) {}
|
|
|
|
|
|
|
|
IntervalSet(SelfType&& aOther) {
|
2018-05-30 22:15:35 +03:00
|
|
|
mIntervals.AppendElements(std::move(aOther.mIntervals));
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit IntervalSet(const ElemType& aOther) {
|
2015-05-18 09:13:20 +03:00
|
|
|
if (!aOther.IsEmpty()) {
|
|
|
|
mIntervals.AppendElement(aOther);
|
|
|
|
}
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit IntervalSet(ElemType&& aOther) {
|
2015-05-18 09:13:20 +03:00
|
|
|
if (!aOther.IsEmpty()) {
|
2018-05-30 22:15:35 +03:00
|
|
|
mIntervals.AppendElement(std::move(aOther));
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
2015-06-18 01:07:46 +03:00
|
|
|
bool operator==(const SelfType& aOther) const {
|
|
|
|
return mIntervals == aOther.mIntervals;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SelfType& aOther) const {
|
|
|
|
return mIntervals != aOther.mIntervals;
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
SelfType& operator=(const SelfType& aOther) {
|
|
|
|
mIntervals = aOther.mIntervals;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator=(SelfType&& aOther) {
|
|
|
|
MOZ_ASSERT(&aOther != this, "self-moves are prohibited");
|
|
|
|
this->~IntervalSet();
|
2018-05-30 22:15:35 +03:00
|
|
|
new (this) IntervalSet(std::move(aOther));
|
2015-05-07 03:24:10 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator=(const ElemType& aInterval) {
|
|
|
|
mIntervals.Clear();
|
2015-07-15 13:17:27 +03:00
|
|
|
if (!aInterval.IsEmpty()) {
|
|
|
|
mIntervals.AppendElement(aInterval);
|
|
|
|
}
|
2015-05-07 03:24:10 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator=(ElemType&& aInterval) {
|
|
|
|
mIntervals.Clear();
|
2015-07-15 13:17:27 +03:00
|
|
|
if (!aInterval.IsEmpty()) {
|
2018-05-30 22:15:35 +03:00
|
|
|
mIntervals.AppendElement(std::move(aInterval));
|
2015-07-15 13:17:27 +03:00
|
|
|
}
|
2015-05-07 03:24:10 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& Add(const SelfType& aIntervals) {
|
|
|
|
mIntervals.AppendElements(aIntervals.mIntervals);
|
2015-05-18 09:13:20 +03:00
|
|
|
Normalize();
|
2015-05-07 03:24:10 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& Add(const ElemType& aInterval) {
|
2015-05-18 09:13:20 +03:00
|
|
|
if (aInterval.IsEmpty()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
if (mIntervals.IsEmpty()) {
|
|
|
|
mIntervals.AppendElement(aInterval);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-05-25 08:09:16 +03:00
|
|
|
ElemType& last = mIntervals.LastElement();
|
|
|
|
if (aInterval.TouchesOnRight(last)) {
|
|
|
|
last = last.Span(aInterval);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-05-18 09:13:20 +03:00
|
|
|
// Most of our actual usage is adding an interval that will be outside the
|
|
|
|
// range. We can speed up normalization here.
|
2015-05-25 08:09:16 +03:00
|
|
|
if (aInterval.RightOf(last)) {
|
2015-05-18 09:13:20 +03:00
|
|
|
mIntervals.AppendElement(aInterval);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContainerType normalized;
|
|
|
|
ElemType current(aInterval);
|
|
|
|
IndexType i = 0;
|
|
|
|
for (; i < mIntervals.Length(); i++) {
|
|
|
|
ElemType& interval = mIntervals[i];
|
|
|
|
if (current.Touches(interval)) {
|
|
|
|
current = current.Span(interval);
|
|
|
|
} else if (current.LeftOf(interval)) {
|
|
|
|
break;
|
|
|
|
} else {
|
2018-05-30 22:15:35 +03:00
|
|
|
normalized.AppendElement(std::move(interval));
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-30 22:15:35 +03:00
|
|
|
normalized.AppendElement(std::move(current));
|
2015-05-18 09:13:20 +03:00
|
|
|
for (; i < mIntervals.Length(); i++) {
|
2018-05-30 22:15:35 +03:00
|
|
|
normalized.AppendElement(std::move(mIntervals[i]));
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
|
|
|
mIntervals.Clear();
|
2018-05-30 22:15:35 +03:00
|
|
|
mIntervals.AppendElements(std::move(normalized));
|
2015-05-18 09:13:20 +03:00
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator+=(const SelfType& aIntervals) {
|
|
|
|
Add(aIntervals);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& operator+=(const ElemType& aInterval) {
|
|
|
|
Add(aInterval);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType operator+(const SelfType& aIntervals) const {
|
|
|
|
SelfType intervals(*this);
|
|
|
|
intervals.Add(aIntervals);
|
|
|
|
return intervals;
|
|
|
|
}
|
|
|
|
|
2015-11-25 12:46:11 +03:00
|
|
|
SelfType operator+(const ElemType& aInterval) const {
|
2015-05-07 03:24:10 +03:00
|
|
|
SelfType intervals(*this);
|
|
|
|
intervals.Add(aInterval);
|
|
|
|
return intervals;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend SelfType operator+(const ElemType& aInterval,
|
|
|
|
const SelfType& aIntervals) {
|
|
|
|
SelfType intervals;
|
|
|
|
intervals.Add(aInterval);
|
|
|
|
intervals.Add(aIntervals);
|
|
|
|
return intervals;
|
|
|
|
}
|
|
|
|
|
2015-06-11 08:49:49 +03:00
|
|
|
// Excludes an interval from an IntervalSet.
|
|
|
|
// This is done by inverting aInterval within the bounds of mIntervals
|
|
|
|
// and then doing the intersection.
|
|
|
|
SelfType& operator-=(const ElemType& aInterval) {
|
|
|
|
if (aInterval.IsEmpty() || mIntervals.IsEmpty()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
T firstEnd = std::max(mIntervals[0].mStart, aInterval.mStart);
|
|
|
|
T secondStart = std::min(mIntervals.LastElement().mEnd, aInterval.mEnd);
|
2015-08-11 11:13:43 +03:00
|
|
|
ElemType startInterval(mIntervals[0].mStart, firstEnd);
|
|
|
|
ElemType endInterval(secondStart, mIntervals.LastElement().mEnd);
|
2018-05-30 22:15:35 +03:00
|
|
|
SelfType intervals(std::move(startInterval));
|
|
|
|
intervals += std::move(endInterval);
|
2015-06-11 08:49:49 +03:00
|
|
|
return Intersection(intervals);
|
|
|
|
}
|
|
|
|
|
2015-06-28 05:07:16 +03:00
|
|
|
SelfType& operator-=(const SelfType& aIntervals) {
|
|
|
|
for (const auto& interval : aIntervals.mIntervals) {
|
|
|
|
*this -= interval;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-11-25 12:46:11 +03:00
|
|
|
SelfType operator-(const SelfType& aInterval) const {
|
|
|
|
SelfType intervals(*this);
|
|
|
|
intervals -= aInterval;
|
|
|
|
return intervals;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType operator-(const ElemType& aInterval) const {
|
2015-06-11 08:49:49 +03:00
|
|
|
SelfType intervals(*this);
|
|
|
|
intervals -= aInterval;
|
|
|
|
return intervals;
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
// Mutate this IntervalSet to be the union of this and aOther.
|
|
|
|
SelfType& Union(const SelfType& aOther) {
|
|
|
|
Add(aOther);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& Union(const ElemType& aInterval) {
|
|
|
|
Add(aInterval);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutate this TimeRange to be the intersection of this and aOther.
|
|
|
|
SelfType& Intersection(const SelfType& aOther) {
|
|
|
|
ContainerType intersection;
|
|
|
|
|
|
|
|
const ContainerType& other = aOther.mIntervals;
|
|
|
|
IndexType i = 0, j = 0;
|
|
|
|
for (; i < mIntervals.Length() && j < other.Length();) {
|
2015-07-15 13:17:27 +03:00
|
|
|
if (mIntervals[i].IntersectsStrict(other[j])) {
|
2015-05-07 03:24:10 +03:00
|
|
|
intersection.AppendElement(mIntervals[i].Intersection(other[j]));
|
|
|
|
}
|
|
|
|
if (mIntervals[i].mEnd < other[j].mEnd) {
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 09:13:20 +03:00
|
|
|
mIntervals.Clear();
|
2018-05-30 22:15:35 +03:00
|
|
|
mIntervals.AppendElements(std::move(intersection));
|
2015-05-07 03:24:10 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelfType& Intersection(const ElemType& aInterval) {
|
|
|
|
SelfType intervals(aInterval);
|
|
|
|
return Intersection(intervals);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ElemType& operator[](IndexType aIndex) const {
|
|
|
|
return mIntervals[aIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the start boundary of the first interval. Or a default constructed
|
|
|
|
// T if IntervalSet is empty (and aExists if provided will be set to false).
|
|
|
|
T GetStart(bool* aExists = nullptr) const {
|
|
|
|
bool exists = !mIntervals.IsEmpty();
|
|
|
|
|
|
|
|
if (aExists) {
|
|
|
|
*aExists = exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
return mIntervals[0].mStart;
|
|
|
|
} else {
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the end boundary of the last interval. Or a default constructed T
|
|
|
|
// if IntervalSet is empty (and aExists if provided will be set to false).
|
|
|
|
T GetEnd(bool* aExists = nullptr) const {
|
|
|
|
bool exists = !mIntervals.IsEmpty();
|
|
|
|
if (aExists) {
|
|
|
|
*aExists = exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
return mIntervals.LastElement().mEnd;
|
|
|
|
} else {
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexType Length() const { return mIntervals.Length(); }
|
|
|
|
|
|
|
|
T Start(IndexType aIndex) const { return mIntervals[aIndex].mStart; }
|
|
|
|
|
|
|
|
T Start(IndexType aIndex, bool& aExists) const {
|
|
|
|
aExists = aIndex < mIntervals.Length();
|
|
|
|
|
|
|
|
if (aExists) {
|
|
|
|
return mIntervals[aIndex].mStart;
|
|
|
|
} else {
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T End(IndexType aIndex) const { return mIntervals[aIndex].mEnd; }
|
|
|
|
|
|
|
|
T End(IndexType aIndex, bool& aExists) const {
|
|
|
|
aExists = aIndex < mIntervals.Length();
|
|
|
|
|
|
|
|
if (aExists) {
|
|
|
|
return mIntervals[aIndex].mEnd;
|
|
|
|
} else {
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 09:59:17 +03:00
|
|
|
bool Contains(const ElemType& aInterval) const {
|
2015-05-18 09:13:20 +03:00
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.Contains(aInterval)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-29 09:59:17 +03:00
|
|
|
bool ContainsStrict(const ElemType& aInterval) const {
|
2016-08-27 13:24:31 +03:00
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.ContainsStrict(aInterval)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-29 09:59:17 +03:00
|
|
|
bool Contains(const T& aX) const {
|
|
|
|
for (const auto& interval : mIntervals) {
|
2015-05-07 03:24:10 +03:00
|
|
|
if (interval.Contains(aX)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-29 09:59:17 +03:00
|
|
|
bool ContainsStrict(const T& aX) const {
|
2015-05-07 03:24:10 +03:00
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.ContainsStrict(aX)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-29 09:59:17 +03:00
|
|
|
bool ContainsWithStrictEnd(const T& aX) const {
|
2015-06-12 02:26:58 +03:00
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.ContainsWithStrictEnd(aX)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-16 20:30:45 +03:00
|
|
|
bool ContainsWithStrictEnd(const ElemType& aInterval) const {
|
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.ContainsWithStrictEnd(aInterval)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:18:04 +03:00
|
|
|
bool Intersects(const ElemType& aInterval) const {
|
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.Intersects(aInterval)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IntersectsStrict(const ElemType& aInterval) const {
|
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.IntersectsStrict(aInterval)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IntersectsWithStrictEnd(const ElemType& aInterval) const {
|
|
|
|
for (const auto& interval : mIntervals) {
|
|
|
|
if (interval.IntersectsWithStrictEnd(aInterval)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
// Shift all values by aOffset.
|
2015-05-25 12:27:21 +03:00
|
|
|
SelfType& Shift(const T& aOffset) {
|
2015-05-07 03:24:10 +03:00
|
|
|
for (auto& interval : mIntervals) {
|
2015-05-18 09:15:47 +03:00
|
|
|
interval.mStart = interval.mStart + aOffset;
|
|
|
|
interval.mEnd = interval.mEnd + aOffset;
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
2015-05-25 12:27:21 +03:00
|
|
|
return *this;
|
2015-05-07 03:24:10 +03:00
|
|
|
}
|
|
|
|
|
2016-08-29 09:59:17 +03:00
|
|
|
void SetFuzz(const T& aFuzz) {
|
2015-05-18 09:13:20 +03:00
|
|
|
for (auto& interval : mIntervals) {
|
|
|
|
interval.SetFuzz(aFuzz);
|
|
|
|
}
|
|
|
|
Normalize();
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
static const IndexType NoIndex = IndexType(-1);
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
IndexType Find(const T& aValue) const {
|
2015-05-07 03:24:10 +03:00
|
|
|
for (IndexType i = 0; i < mIntervals.Length(); i++) {
|
|
|
|
if (mIntervals[i].Contains(aValue)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
2015-06-15 04:48:47 +03:00
|
|
|
// Methods for range-based for loops.
|
|
|
|
typename ContainerType::iterator begin() { return mIntervals.begin(); }
|
|
|
|
|
|
|
|
typename ContainerType::const_iterator begin() const {
|
|
|
|
return mIntervals.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
typename ContainerType::iterator end() { return mIntervals.end(); }
|
|
|
|
|
|
|
|
typename ContainerType::const_iterator end() const {
|
|
|
|
return mIntervals.end();
|
|
|
|
}
|
|
|
|
|
2015-11-24 11:41:20 +03:00
|
|
|
ElemType& LastInterval() {
|
|
|
|
MOZ_ASSERT(!mIntervals.IsEmpty());
|
|
|
|
return mIntervals.LastElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ElemType& LastInterval() const {
|
|
|
|
MOZ_ASSERT(!mIntervals.IsEmpty());
|
|
|
|
return mIntervals.LastElement();
|
|
|
|
}
|
|
|
|
|
2015-11-24 11:44:14 +03:00
|
|
|
void Clear() { mIntervals.Clear(); }
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
protected:
|
|
|
|
ContainerType mIntervals;
|
|
|
|
|
|
|
|
private:
|
2015-05-18 09:13:20 +03:00
|
|
|
void Normalize() {
|
|
|
|
if (mIntervals.Length() >= 2) {
|
|
|
|
ContainerType normalized;
|
|
|
|
|
|
|
|
mIntervals.Sort(CompareIntervals());
|
|
|
|
|
|
|
|
// This merges the intervals.
|
|
|
|
ElemType current(mIntervals[0]);
|
|
|
|
for (IndexType i = 1; i < mIntervals.Length(); i++) {
|
|
|
|
ElemType& interval = mIntervals[i];
|
|
|
|
if (current.Touches(interval)) {
|
|
|
|
current = current.Span(interval);
|
|
|
|
} else {
|
2018-05-30 22:15:35 +03:00
|
|
|
normalized.AppendElement(std::move(current));
|
|
|
|
current = std::move(interval);
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-30 22:15:35 +03:00
|
|
|
normalized.AppendElement(std::move(current));
|
2015-05-18 09:13:20 +03:00
|
|
|
|
|
|
|
mIntervals.Clear();
|
2018-05-30 22:15:35 +03:00
|
|
|
mIntervals.AppendElements(std::move(normalized));
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
struct CompareIntervals {
|
|
|
|
bool Equals(const ElemType& aT1, const ElemType& aT2) const {
|
|
|
|
return aT1.mStart == aT2.mStart && aT1.mEnd == aT2.mEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LessThan(const ElemType& aT1, const ElemType& aT2) const {
|
|
|
|
return aT1.mStart - aT1.mFuzz < aT2.mStart + aT2.mFuzz;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// clang doesn't allow for this to be defined inline of IntervalSet.
|
|
|
|
template <typename T>
|
|
|
|
IntervalSet<T> Union(const IntervalSet<T>& aIntervals1,
|
|
|
|
const IntervalSet<T>& aIntervals2) {
|
|
|
|
IntervalSet<T> intervals(aIntervals1);
|
|
|
|
intervals.Union(aIntervals2);
|
|
|
|
return intervals;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
IntervalSet<T> Intersection(const IntervalSet<T>& aIntervals1,
|
|
|
|
const IntervalSet<T>& aIntervals2) {
|
|
|
|
IntervalSet<T> intersection(aIntervals1);
|
|
|
|
intersection.Intersection(aIntervals2);
|
|
|
|
return intersection;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // INTERVALS_H
|