summaryrefslogtreecommitdiffstats
path: root/dom/media/Intervals.h
blob: 84d0e4e76e0ac3d888f140b26efb9d6b22ade820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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"

// Specialization for nsTArray CopyChooser.
namespace mozilla {
namespace media {
template<class T>
class IntervalSet;
} // namespace media
} // namespace mozilla

template<class E>
struct nsTArray_CopyChooser<mozilla::media::IntervalSet<E>>
{
  typedef nsTArray_CopyWithConstructors<mozilla::media::IntervalSet<E>> Type;
};

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)
    : mStart(Forward<StartArg>(aStart))
    , mEnd(Forward<EndArg>(aEnd))
    , mFuzz()
  {
    MOZ_ASSERT(aStart <= aEnd);
  }

  template<typename StartArg, typename EndArg, typename FuzzArg>
  Interval(StartArg&& aStart, EndArg&& aEnd, FuzzArg&& aFuzz)
    : mStart(Forward<StartArg>(aStart))
    , mEnd(Forward<EndArg>(aEnd))
    , mFuzz(Forward<FuzzArg>(aFuzz))
  {
    MOZ_ASSERT(aStart <= aEnd);
  }

  Interval(const SelfType& aOther)
    : mStart(aOther.mStart)
    , mEnd(aOther.mEnd)
    , mFuzz(aOther.mFuzz)
  { }

  Interval(SelfType&& aOther)
    : mStart(Move(aOther.mStart))
    , mEnd(Move(aOther.mEnd))
    , mFuzz(Move(aOther.mFuzz))
  { }

  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();
    new(this) Interval(Move(aOther));
    return *this;
  }

  // Basic interval arithmetic operator definition.
  SelfType operator+ (const SelfType& aOther) const
  {
    return SelfType(mStart + aOther.mStart,
                    mEnd + aOther.mEnd,
                    mFuzz + aOther.mFuzz);
  }

  // Basic interval arithmetic operator definition.
  SelfType operator- (const SelfType& aOther) const
  {
    return SelfType(mStart - aOther.mEnd,
                    mEnd - aOther.mStart,
                    mFuzz + aOther.mFuzz);
  }

  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;
  }

  bool ContainsWithStrictEnd(const T& aX) const
  {
    return mStart - mFuzz <= aX && aX < mEnd;
  }

  bool Contains(const SelfType& aOther) const
  {
    return (mStart - mFuzz <= aOther.mStart + aOther.mFuzz)
           && (aOther.mEnd - aOther.mFuzz <= mEnd + mFuzz);
  }

  bool ContainsStrict(const SelfType& aOther) const
  {
    return mStart <= aOther.mStart && aOther.mEnd <= mEnd;
  }

  bool ContainsWithStrictEnd(const SelfType& aOther) const
  {
    return (mStart - mFuzz <= aOther.mStart + aOther.mFuzz)
           && aOther.mEnd <= mEnd;
  }

  bool Intersects(const SelfType& aOther) const
  {
    return (mStart - mFuzz < aOther.mEnd + aOther.mFuzz)
           && (aOther.mStart - aOther.mFuzz < mEnd + mFuzz);
  }

  bool IntersectsStrict(const SelfType& aOther) const
  {
    return mStart < aOther.mEnd && aOther.mStart < mEnd;
  }

  // Same as Intersects, but including the boundaries.
  bool Touches(const SelfType& aOther) const
  {
    return (mStart - mFuzz <= aOther.mEnd + aOther.mFuzz)
           && (aOther.mStart - aOther.mFuzz <= mEnd + mFuzz);
  }

  // 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;
  }

  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;
  }

  SelfType Span(const SelfType& aOther) const
  {
    if (IsEmpty()) {
      return aOther;
    }
    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;
  }

  void SetFuzz(const T& aFuzz)
  {
    mFuzz = aFuzz;
  }

  // Returns true if the two intervals intersect with this being on the right
  // of aOther
  bool TouchesOnRight(const SelfType& aOther) const
  {
    return aOther.mStart <= mStart
           && (mStart - mFuzz <= aOther.mEnd + aOther.mFuzz)
           && (aOther.mStart - aOther.mFuzz <= mEnd + mFuzz);
  }

  T mStart;
  T mEnd;
  T mFuzz;

private:
};

// An IntervalSet in a collection of Intervals. The IntervalSet is always
// normalized.
template<typename T>
class IntervalSet
{
public:
  typedef IntervalSet<T> SelfType;
  typedef Interval<T> ElemType;
  typedef AutoTArray<ElemType,4> ContainerType;
  typedef typename ContainerType::index_type IndexType;

  IntervalSet()
  {
  }
  virtual ~IntervalSet()
  {
  }

  IntervalSet(const SelfType& aOther)
    : mIntervals(aOther.mIntervals)
  {
  }

  IntervalSet(SelfType&& aOther)
  {
    mIntervals.AppendElements(Move(aOther.mIntervals));
  }

  explicit IntervalSet(const ElemType& aOther)
  {
    if (!aOther.IsEmpty()) {
      mIntervals.AppendElement(aOther);
    }
  }

  explicit IntervalSet(ElemType&& aOther)
  {
    if (!aOther.IsEmpty()) {
      mIntervals.AppendElement(Move(aOther));
    }
  }

  bool operator== (const SelfType& aOther) const
  {
    return mIntervals == aOther.mIntervals;
  }

  bool operator!= (const SelfType& aOther) const
  {
    return mIntervals != aOther.mIntervals;
  }

  SelfType& operator= (const SelfType& aOther)
  {
    mIntervals = aOther.mIntervals;
    return *this;
  }

  SelfType& operator= (SelfType&& aOther)
  {
    MOZ_ASSERT(&aOther != this, "self-moves are prohibited");
    this->~IntervalSet();
    new(this) IntervalSet(Move(aOther));
    return *this;
  }

  SelfType& operator= (const ElemType& aInterval)
  {
    mIntervals.Clear();
    if (!aInterval.IsEmpty()) {
      mIntervals.AppendElement(aInterval);
    }
    return *this;
  }

  SelfType& operator= (ElemType&& aInterval)
  {
    mIntervals.Clear();
    if (!aInterval.IsEmpty()) {
      mIntervals.AppendElement(Move(aInterval));
    }
    return *this;
  }

  SelfType& Add(const SelfType& aIntervals)
  {
    mIntervals.AppendElements(aIntervals.mIntervals);
    Normalize();
    return *this;
  }

  SelfType& Add(const ElemType& aInterval)
  {
    if (aInterval.IsEmpty()) {
      return *this;
    }
    if (mIntervals.IsEmpty()) {
      mIntervals.AppendElement(aInterval);
      return *this;
    }
    ElemType& last = mIntervals.LastElement();
    if (aInterval.TouchesOnRight(last)) {
      last = last.Span(aInterval);
      return *this;
    }
    // Most of our actual usage is adding an interval that will be outside the
    // range. We can speed up normalization here.
    if (aInterval.RightOf(last)) {
      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 {
        normalized.AppendElement(Move(interval));
      }
    }
    normalized.AppendElement(Move(current));
    for (; i < mIntervals.Length(); i++) {
      normalized.AppendElement(Move(mIntervals[i]));
    }
    mIntervals.Clear();
    mIntervals.AppendElements(Move(normalized));

    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;
  }

  SelfType operator+ (const ElemType& aInterval) const
  {
    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;
  }

  // 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);
    ElemType startInterval(mIntervals[0].mStart, firstEnd);
    ElemType endInterval(secondStart, mIntervals.LastElement().mEnd);
    SelfType intervals(Move(startInterval));
    intervals += Move(endInterval);
    return Intersection(intervals);
  }

  SelfType& operator-= (const SelfType& aIntervals)
  {
    for (const auto& interval : aIntervals.mIntervals) {
      *this -= interval;
    }
    return *this;
  }

  SelfType operator- (const SelfType& aInterval) const
  {
    SelfType intervals(*this);
    intervals -= aInterval;
    return intervals;
  }

  SelfType operator- (const ElemType& aInterval) const
  {
    SelfType intervals(*this);
    intervals -= aInterval;
    return intervals;
  }

  // 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();) {
      if (mIntervals[i].IntersectsStrict(other[j])) {
        intersection.AppendElement(mIntervals[i].Intersection(other[j]));
      }
      if (mIntervals[i].mEnd < other[j].mEnd) {
        i++;
      } else {
        j++;
      }
    }
    mIntervals.Clear();
    mIntervals.AppendElements(Move(intersection));
    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();
    }
  }

  bool Contains(const ElemType& aInterval) const
  {
    for (const auto& interval : mIntervals) {
      if (interval.Contains(aInterval)) {
        return true;
      }
    }
    return false;
  }

  bool ContainsStrict(const ElemType& aInterval) const
  {
    for (const auto& interval : mIntervals) {
      if (interval.ContainsStrict(aInterval)) {
        return true;
      }
    }
    return false;
  }

  bool Contains(const T& aX) const
  {
    for (const auto& interval : mIntervals)
    {
      if (interval.Contains(aX)) {
        return true;
      }
    }
    return false;
  }

  bool ContainsStrict(const T& aX) const
  {
    for (const auto& interval : mIntervals) {
      if (interval.ContainsStrict(aX)) {
        return true;
      }
    }
    return false;
  }

  bool ContainsWithStrictEnd(const T& aX) const
  {
    for (const auto& interval : mIntervals) {
      if (interval.ContainsWithStrictEnd(aX)) {
        return true;
      }
    }
    return false;
  }

  // Shift all values by aOffset.
  SelfType& Shift(const T& aOffset)
  {
    for (auto& interval : mIntervals) {
      interval.mStart = interval.mStart + aOffset;
      interval.mEnd = interval.mEnd + aOffset;
    }
    return *this;
  }

  void SetFuzz(const T& aFuzz)
  {
    for (auto& interval : mIntervals) {
      interval.SetFuzz(aFuzz);
    }
    Normalize();
  }

  static const IndexType NoIndex = IndexType(-1);

  IndexType Find(const T& aValue) const
  {
    for (IndexType i = 0; i < mIntervals.Length(); i++) {
      if (mIntervals[i].Contains(aValue)) {
        return i;
      }
    }
    return NoIndex;
  }

  // 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();
  }

  ElemType& LastInterval()
  {
    MOZ_ASSERT(!mIntervals.IsEmpty());
    return mIntervals.LastElement();
  }

  const ElemType& LastInterval() const
  {
    MOZ_ASSERT(!mIntervals.IsEmpty());
    return mIntervals.LastElement();
  }

  void Clear()
  {
    mIntervals.Clear();
  }

protected:
  ContainerType mIntervals;

private:
  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 {
          normalized.AppendElement(Move(current));
          current = Move(interval);
        }
      }
      normalized.AppendElement(Move(current));

      mIntervals.Clear();
      mIntervals.AppendElements(Move(normalized));
    }
  }

  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