timerangelist: fixed bug where ranges might sometimes overlap

This commit is contained in:
itsmattkc
2020-10-05 16:50:38 +11:00
parent 787ea1d548
commit 2d8c605cc9
2 changed files with 15 additions and 17 deletions
+14 -16
View File
@@ -160,20 +160,25 @@ void TimeRange::normalize()
length_ = out_ - in_; length_ = out_ - in_;
} }
void TimeRangeList::InsertTimeRange(const TimeRange &range) void TimeRangeList::InsertTimeRange(TimeRange range_to_add)
{ {
// See if list contains this range
if (ContainsTimeRange(range_to_add)) {
return;
}
// Does not contain range, so we'll almost certainly be adding it in some way
for (int i=0;i<size();i++) { for (int i=0;i<size();i++) {
const TimeRange& compare = at(i); const TimeRange& compare = at(i);
if (compare == range) { if (compare.OverlapsWith(range_to_add)) {
return; range_to_add = TimeRange::Combine(range_to_add, compare);
} else if (range.OverlapsWith(compare)) { removeAt(i);
replace(i, TimeRange::Combine(range, compare)); i--;
return;
} }
} }
append(range); append(range_to_add);
} }
void TimeRangeList::RemoveTimeRange(const TimeRange &remove) void TimeRangeList::RemoveTimeRange(const TimeRange &remove)
@@ -190,15 +195,8 @@ void TimeRangeList::RemoveTimeRange(const TimeRange &remove)
sz--; 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
TimeRange before(compare.in(), remove.in()); this->append(TimeRange(remove.out(), compare.out()));
TimeRange after(remove.out(), compare.out()); compare.set_out(remove.in());
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());
+1 -1
View File
@@ -74,7 +74,7 @@ public:
{ {
} }
void InsertTimeRange(const TimeRange& range); void InsertTimeRange(TimeRange range_to_add);
void RemoveTimeRange(const TimeRange& remove); void RemoveTimeRange(const TimeRange& remove);