public void Add(Range rangeToAdd) { if (rangeToAdd.IsEmpty) { return; } for (LinkedListNode <Range> i = ranges.First; i != null; i = i.Next) { IntersectStruct r = Range.Intersect(rangeToAdd, i.Value); if (r.RelativePosition < 0) { ranges.AddBefore(i, rangeToAdd); rangeToAdd = new Range(); break; } else if (r.RelativePosition > 0) { } else { if (!r.Leftover1Left.IsEmpty) { ranges.AddBefore(i, r.Leftover1Left); } if (!r.Leftover2Left.IsEmpty) { ranges.AddBefore(i, r.Leftover2Left); } if (!r.Common.IsEmpty) { i.Value = r.Common; } if (!r.Leftover2Right.IsEmpty) { i = ranges.AddAfter(i, r.Leftover2Right); } if (!r.Leftover1Right.IsEmpty) { rangeToAdd = r.Leftover1Right; } else { rangeToAdd = new Range(); break; } } } if (!rangeToAdd.IsEmpty) { ranges.AddLast(rangeToAdd); } MergeDown(); }
public void Remove(Range rangeToRemove) { for (LinkedListNode <Range> i = ranges.First; i != null;) { LinkedListNode <Range> next = i.Next; IntersectStruct r = Range.Intersect(rangeToRemove, i.Value); if (r.RelativePosition == 0) { rangeToRemove = r.Leftover1Right; if (!r.Leftover2Left.IsEmpty) { ranges.AddBefore(i, r.Leftover2Left); } if (!r.Leftover2Right.IsEmpty) { ranges.AddBefore(i, r.Leftover2Right); } ranges.Remove(i); } i = next; } MergeDown(); }
public static IntersectStruct Intersect(Range r1, Range r2) { IntersectStruct ret = new IntersectStruct(); if (r1.Begin <= r2.Begin) { // r1: |---- // r2: |--- if (r2.Begin >= r1.End) { // r1: |---| // r2: |---| ret.RelativePosition = -1; } else { ret.Leftover1Left = new Range(r1.Begin, r2.Begin); ret.RelativePosition = 0; if (r1.End >= r2.End) { // r1: |-------| // r2: |---| ret.Common = new Range(r2.Begin, r2.End); ret.Leftover1Right = new Range(r2.End, r1.End); } else { // r1: |-------| // r2: |-------| ret.Common = new Range(r2.Begin, r1.End); ret.Leftover2Right = new Range(r1.End, r2.End); } } } else { // r1: |---- // r2: |--- if (r1.Begin >= r2.End) { // r1: |---| // r2: |---| ret.RelativePosition = 1; } else { ret.Leftover2Left = new Range(r2.Begin, r1.Begin); ret.RelativePosition = 0; if (r2.End >= r1.End) { // r1: |---| // r2: |---------| ret.Common = new Range(r1.Begin, r1.End); ret.Leftover2Right = new Range(r1.End, r2.End); } else { // r1: |-------| // r2: |-------| ret.Common = new Range(r1.Begin, r2.End); ret.Leftover1Right = new Range(r2.End, r1.End); } } } ret.Leftover1Left.priority = r1.priority; ret.Leftover1Right.priority = r1.priority; ret.Leftover2Left.priority = r2.priority; ret.Leftover2Right.priority = r2.priority; if (!ret.Common.IsEmpty) { ret.Common.priority = Math.Max(r1.priority, r2.priority); } return(ret); }