/** Interval objects are used readonly so share all with the * same single value a==b up to some max size. Use an array as a perfect hash. * Return shared object for 0..INTERVAL_POOL_MAX_VALUE or a new * Interval object with a..a in it. On Java.g, 218623 IntervalSets * have a..a (set with 1 element). */ public static Interval Create( int a, int b ) { //return new Interval(a,b); // cache just a..a if ( a != b || a < 0 || a > INTERVAL_POOL_MAX_VALUE ) { return new Interval( a, b ); } if ( cache[a] == null ) { cache[a] = new Interval( a, a ); } return cache[a]; }
/** Return the interval with elements from this not in other; * other must not be totally enclosed (properly contained) * within this, which would result in two disjoint intervals * instead of the single one returned by this method. */ public virtual Interval DifferenceNotProperlyContained( Interval other ) { Interval diff = null; // other.a to left of this.a (or same) if ( other.StartsBeforeNonDisjoint( this ) ) { diff = Interval.Create( Math.Max( this.a, other.b + 1 ), this.b ); } // other.a to right of this.a else if ( other.StartsAfterNonDisjoint( this ) ) { diff = Interval.Create( this.a, other.a - 1 ); } return diff; }
/** Are two intervals adjacent such as 0..41 and 42..42? */ public virtual bool Adjacent( Interval other ) { return this.a == other.b + 1 || this.b == other.a - 1; }
/** Return the interval computed from combining this and other */ public virtual Interval Union( Interval other ) { return Interval.Create( Math.Min( a, other.a ), Math.Max( b, other.b ) ); }
/** Does this start after other? NonDisjoint */ public virtual bool StartsAfterNonDisjoint( Interval other ) { return this.a > other.a && this.a <= other.b; // this.b>=other.b implied }
/** Does this start at or before other? Nondisjoint */ public virtual bool StartsBeforeNonDisjoint( Interval other ) { return this.a <= other.a && this.b >= other.a; }
/** Does this.a start after other.b? May or may not be disjoint */ public virtual bool StartsAfter( Interval other ) { return this.a > other.a; }
/** Does this start completely after other? Disjoint */ public virtual bool StartsAfterDisjoint( Interval other ) { return this.a > other.b; }
public virtual bool ProperlyContains( Interval other ) { return other.a >= this.a && other.b <= this.b; }
/** Are both ranges disjoint? I.e., no overlap? */ public virtual bool Disjoint( Interval other ) { return StartsBeforeDisjoint( other ) || StartsAfterDisjoint( other ); }
/** Return the interval computed from combining this and other */ public Interval Union( Interval other ) { return Interval.FromBounds( Math.Min( a, other.a ), Math.Max( b, other.b ) ); }
/** Does this start completely before other? Disjoint */ public bool StartsBeforeDisjoint( Interval other ) { return this.a < other.a && this.b < other.a; }
public bool Equals(Interval other) { return this.a == other.a && this.b == other.b; }