/* We reset the interval set, add the sentinels and the (certainly existing) first interval.
* If the first interval is too long, we shorten it. */
leftOrderedIntervals.add( INIT[ 0 ] );
leftOrderedIntervals.add( INIT[ 1 ] );
Interval interval;
interval = shortIntervals.dequeue();
if ( interval.length() < intervalMaxLength ) leftOrderedIntervals.add( new SelectedInterval( interval, IntervalType.WHOLE ) );
else {
leftOrderedIntervals.add( new SelectedInterval( Interval.valueOf( interval.left, interval.left + intervalMaxLength / 2 ), IntervalType.PREFIX ) );
leftOrderedIntervals.add( new SelectedInterval( Interval.valueOf( interval.right - intervalMaxLength / 2, interval.right ), IntervalType.SUFFIX ) );
}
/* We now iteratively extract intervals from the queue, check that they do not overlap
* any interval already chosen, and in case add it to the set of chosen intervals. */
//System.err.println( "Starting with " + shortIntervals.size() + " intervals");
ObjectBidirectionalIterator<SelectedInterval> iterator;
SelectedInterval left, right;
while( leftOrderedIntervals.size() - INIT.length < maxIntervals && ! shortIntervals.isEmpty() ) {
//System.err.println( "Map is now: " + leftOrderedIntervals );
interval = shortIntervals.dequeue();
// If all remaining intervals are too large, stop iteration.
if ( interval.length() > intervalMaxLength ) break;
// This iterator falls exactly in the middle of the intervals preceding and following interval.
iterator = leftOrderedIntervals.iterator( new SelectedInterval( interval, null ) );
iterator.previous();
left = iterator.next();