public void Add(DoubleRange range) { var left = new List<DoubleRange>(); var right = new List<DoubleRange>(); foreach (var rng in this.ranges) { if (range.IntersectsExclusive(rng) || range.Touches(rng)) { range = range.JoinWith(rng); } else if (rng.Upper < range.Lower) { left.Add(rng); } else if (range.Upper < rng.Lower) { right.Add(rng); } else { throw new System.InvalidOperationException("Internal Error"); } } this.ranges = left.Concat(EnumerableUtil.Single(range)).Concat(right).ToList(); }
/// <summary> /// Tests if this range touches another. For example (1-2) touches (3,5) but (1,2) does not touch (4,5) /// </summary> /// <param name="range">the other range</param> /// <returns>true if they touch</returns> public bool Touches(DoubleRange range) { var val = _touches(this, range) || _touches(range, this); return(val); }
private static bool _touches(DoubleRange range1, DoubleRange range2) { var val = (range1.Upper == range2.Lower); return(val); }
public bool IntersectsInclusive(DoubleRange range) { bool val = _intersects_inclusive(this, range) || _intersects_inclusive(range, this); return(val); }
private static bool _intersects_inclusive(DoubleRange range1, DoubleRange range2) { bool val = (range1.Lower <= range2.Lower) && (range1.Upper >= range2.Upper); return(val); }
/// <summary> /// Tests if this range touches another. For example (1-2) touches (3,5) but (1,2) does not touch (4,5) /// </summary> /// <param name="range">the other range</param> /// <returns>true if they touch</returns> public bool Touches(DoubleRange range) { var val = _touches(this, range) || _touches(range, this); return val; }
private static bool _touches(DoubleRange range1, DoubleRange range2) { var val = (range1.Upper == range2.Lower); return val; }
public bool IntersectsInclusive(DoubleRange range) { bool val = _intersects_inclusive(this, range) || _intersects_inclusive(range, this); return val; }
private static bool _intersects_inclusive(DoubleRange range1, DoubleRange range2) { bool val = (range1.Lower <= range2.Lower) && (range1.Upper >= range2.Upper); return val; }
/// <summary> /// Join this range with another and return a single range that contains them both. The ranges must either touch or interest. /// for example (0,2) amd (3,7) will yield (0,7) /// </summary> /// <param name="range">the other range</param> /// <returns>the merged range</returns> public DoubleRange JoinWith(DoubleRange range) { if (this.IntersectsExclusive(range) || this.Touches(range)) { double new_Upper = System.Math.Max(this.Upper, range.Upper); double new_Lower = System.Math.Min(this.Lower, range.Lower); return DoubleRange.FromEndpoints(new_Lower, new_Upper); } else { throw new System.ArgumentException("Ranges cannot be joined because they do not touch or overlap"); } }
public void AddInclusive(double lower, double upper) { var rng = DoubleRange.FromEndpoints(lower, upper); this.Add(rng); }
public void RemoveInclusive(int lower, int upper) { var rng = DoubleRange.FromEndpoints(lower, upper); this.RemoveInclusive(rng); }
public void RemoveInclusive(DoubleRange range) { // if the range doesn't intersect this collection do nothing if (!range.IntersectsInclusive(DoubleRange.FromEndpoints(this.Lower, this.Upper))) { return; } var middle = new List<DoubleRange>(); foreach (var S in this.ranges) { if (!range.IntersectsInclusive(S)) { middle.Add(S); continue; } if ((range.Lower <= S.Lower) && (range.Upper >= S.Upper)) { // disregard S completely continue; } if (range.Lower > S.Lower) { if (S.Lower <= (range.Lower - double.Epsilon)) { var X = DoubleRange.FromEndpoints(S.Lower, range.Lower); middle.Add(X); } } if (range.Upper <= S.Upper) { if ((range.Upper + double.Epsilon) <= S.Upper) { var X = DoubleRange.FromEndpoints(range.Upper, S.Upper); middle.Add(X); } } else { throw new System.InvalidOperationException("internal error"); } } this.ranges = middle; }