private void addIntervalInternal( Object start, boolean incStart, Object end, boolean incEnd ) { intervals.add( new Interval( start, incStart, end, incEnd ) ); }
public boolean contains( Object value ) { ListIterator iter = intervals.listIterator(); while( iter.hasNext() ) { Interval curr = (Interval) iter.next(); // TODO add early termination if( curr.contains( value ) ) return true; } return false; }
/** * @effects: returns the number of integers in this Interval. */ public int count() { int count = 0; ListIterator iter = intervals.listIterator(); while( iter.hasNext() ) { Interval i = (Interval) iter.next(); int intervalCount = i.count(); if( intervalCount == ValueSpace.INFINITE ) return ValueSpace.INFINITE; count += i.count(); // overflow if( count < 0 ) return ValueSpace.INFINITE; } return count; }
boolean firstInfinite = (first.count() == ValueSpace.INFINITE); Interval last = (Interval) intervals.get( size - 1 ); boolean lastInfinite = (size > 1) && (last.count() == ValueSpace.INFINITE); int iCount = i.count(); if( iCount == ValueSpace.INFINITE ) // did we hit another infinite interval? break; else if( iCount <= n ) // this interval is used up n -= i.count(); else { int index = i.incStart ? n : n + 1; Object result = valueSpace.succ( i.start(), index ); n = (int) Math.floor( n / 2.0 ); if( choose == 0 ) return valueSpace.succ( first.end(), -n ); else return valueSpace.succ( last.start(), n ); else if( !valueSpace.isInfinite( first.start() ) ) return valueSpace.succ( first.start(), n ); else if( !valueSpace.isInfinite( first.end() ) ) return valueSpace.succ( first.end(), -n ); else { if( n == 0 ) valueSpace.succ( last.start(), n );
private void removeIntervalInternal( Object start, boolean incStart, Object end, boolean incEnd ) { ListIterator iter = intervals.listIterator(); Interval toRemove = new Interval( start, incStart, end, incEnd ); while( iter.hasNext() ) { Interval curr = (Interval) iter.next(); if( curr.overlaps( toRemove ) ) { iter.remove(); if( valueSpace.compare( start, curr.start() ) > 0 ) { iter.add( new Interval( curr.start(), curr.incStart(), start, !incStart ) ); if( valueSpace.compare( end, curr.end() ) < 0 ) { iter.add( new Interval( end, !incEnd, curr.end(), curr.incEnd() ) ); } } else { if( valueSpace.compare( end, curr.end() ) < 0 ) { iter.add( new Interval( end, !incEnd, curr.end(), curr.incEnd() ) ); } } } } }
private boolean overlaps( Interval i ) { if( valueSpace.compare( this.start, i.start ) <= 0 ) { int cmp = valueSpace.compare( i.start, this.end ); if( cmp > 0 || (cmp == 0 && (!i.incStart || !this.incEnd)) ) { return false; } else { return true; } } else { return i.overlaps( this ); } }
/** * @requires: end >= start * * @effects: creates a new IntervalList containing the range from <code>start</code> to * <code>end</code>. */ public GenericIntervalList( Object start, Object end, ValueSpace comp ) { valueSpace = comp; intervals = new LinkedList(); intervals.add( new Interval( start, true, end, true ) ); }
/** * @requires: end >= start * * @effects: removes from this all of the numbers which are not between <code>start</code> and * <code>end</code>. */ public void restrictToInterval( Object start, boolean incStart, Object end, boolean incEnd ) { restrictToInterval( new Interval( start, incStart, end, incEnd ) ); }
/** * @effects: returns the lower bound of the lowest range of this, or null if this represents an * empty set of intervals. */ public Object min() { if( intervals.isEmpty() ) { return null; } return ((Interval) intervals.get( 0 )).start(); }
/** * @effects: adds to this all of numbers reprsented by <code>il</code>. */ public void addIntervalList( GenericIntervalList il ) { Iterator iter = il.intervals.iterator(); while( iter.hasNext() ) { Interval i = (Interval) iter.next(); addIntervalInternal( i.start(), i.incStart(), i.end(), i.incEnd() ); } canonicalize(); }
/** * @effects: removes from this all of the numbers represented by <code>il</code>. */ public void removeIntervalList( GenericIntervalList il ) { Iterator iter = il.intervals.iterator(); while( iter.hasNext() ) { Interval i = (Interval) iter.next(); removeIntervalInternal( i.start(), i.incStart(), i.end(), i.incEnd() ); } canonicalize(); }
/** * @effects: returns the upper bound of the highest range of this, or null if this represents an * empty set of intervals. */ public Object max() { if( intervals.isEmpty() ) { return null; } return ((Interval) intervals.get( intervals.size() - 1 )).end(); }