timerange: added some helper functions for comparing timeranges and rationals

This commit is contained in:
itsmattkc
2020-06-05 04:30:59 +10:00
parent cb0d89c09e
commit 8051a42fcc
2 changed files with 35 additions and 13 deletions
+31 -10
View File
@@ -84,7 +84,7 @@ bool TimeRange::OverlapsWith(const TimeRange &a, bool in_inclusive, bool out_inc
return !(overlaps_in || overlaps_out); return !(overlaps_in || overlaps_out);
} }
TimeRange TimeRange::CombineWith(const TimeRange &a) const TimeRange TimeRange::Combined(const TimeRange &a) const
{ {
return Combine(a, *this); return Combine(a, *this);
} }
@@ -98,12 +98,28 @@ bool TimeRange::Contains(const TimeRange &compare, bool in_inclusive, bool out_i
return contains_in && contains_out; return contains_in && contains_out;
} }
bool TimeRange::Contains(const rational &r) const
{
return r >= in_ && r < out_;
}
TimeRange TimeRange::Combine(const TimeRange &a, const TimeRange &b) TimeRange TimeRange::Combine(const TimeRange &a, const TimeRange &b)
{ {
return TimeRange(qMin(a.in(), b.in()), return TimeRange(qMin(a.in(), b.in()),
qMax(a.out(), b.out())); qMax(a.out(), b.out()));
} }
TimeRange TimeRange::Intersected(const TimeRange &a) const
{
return Intersect(a, *this);
}
TimeRange TimeRange::Intersect(const TimeRange &a, const TimeRange &b)
{
return TimeRange(qMax(a.in(), b.in()),
qMin(a.out(), b.out()));
}
TimeRange TimeRange::operator+(const rational &rhs) const TimeRange TimeRange::operator+(const rational &rhs) const
{ {
TimeRange answer(*this); TimeRange answer(*this);
@@ -162,22 +178,27 @@ void TimeRangeList::InsertTimeRange(const TimeRange &range)
void TimeRangeList::RemoveTimeRange(const TimeRange &remove) void TimeRangeList::RemoveTimeRange(const TimeRange &remove)
{ {
RemoveTimeRange(this, remove); int sz = this->size();
}
void TimeRangeList::RemoveTimeRange(QList<TimeRange> *list, const TimeRange &remove) for (int i=0;i<sz;i++) {
{ TimeRange compare = (*this)[i];
for (int i=0;i<list->size();i++) {
TimeRange& compare = (*list)[i];
if (remove.Contains(compare)) { if (remove.Contains(compare)) {
// This element is entirely encompassed in this range, remove it // This element is entirely encompassed in this range, remove it
list->removeAt(i); this->removeAt(i);
i--; i--;
sz--;
} else if (compare.Contains(remove, false, false)) { } else if (compare.Contains(remove, false, false)) {
// The remove range is within this element, only choice is to split the element into two // The remove range is within this element, only choice is to split the element into two
list->append(TimeRange(remove.out(), compare.out())); TimeRange before(compare.in(), remove.in());
compare.set_out(remove.in()); TimeRange after(remove.out(), compare.out());
this->removeAt(i);
i--;
sz--;
InsertTimeRange(before);
InsertTimeRange(after);
} else if (compare.in() < remove.in() && compare.out() > remove.in()) { } else if (compare.in() < remove.in() && compare.out() > remove.in()) {
// This element's out point overlaps the range's in, we'll trim it // This element's out point overlaps the range's in, we'll trim it
compare.set_out(remove.in()); compare.set_out(remove.in());
+4 -3
View File
@@ -43,9 +43,12 @@ public:
bool OverlapsWith(const TimeRange& a, bool in_inclusive = true, bool out_inclusive = true) const; bool OverlapsWith(const TimeRange& a, bool in_inclusive = true, bool out_inclusive = true) const;
bool Contains(const TimeRange& a, bool in_inclusive = true, bool out_inclusive = true) const; bool Contains(const TimeRange& a, bool in_inclusive = true, bool out_inclusive = true) const;
bool Contains(const rational& r) const;
TimeRange CombineWith(const TimeRange& a) const; TimeRange Combined(const TimeRange& a) const;
static TimeRange Combine(const TimeRange &a, const TimeRange &b); static TimeRange Combine(const TimeRange &a, const TimeRange &b);
TimeRange Intersected(const TimeRange& a) const;
static TimeRange Intersect(const TimeRange &a, const TimeRange &b);
TimeRange operator+(const rational& rhs) const; TimeRange operator+(const rational& rhs) const;
TimeRange operator-(const rational& rhs) const; TimeRange operator-(const rational& rhs) const;
@@ -75,8 +78,6 @@ public:
void RemoveTimeRange(const TimeRange& remove); void RemoveTimeRange(const TimeRange& remove);
static void RemoveTimeRange(QList<TimeRange>* list, const TimeRange& remove);
bool ContainsTimeRange(const TimeRange& range, bool in_inclusive = true, bool out_inclusive = true) const; bool ContainsTimeRange(const TimeRange& range, bool in_inclusive = true, bool out_inclusive = true) const;
TimeRangeList Intersects(const TimeRange& range) const; TimeRangeList Intersects(const TimeRange& range) const;