question: public void sub ( NamedRangeSet other ) { List < NamedRange > newRanges = new ArrayList < NamedRange > ( ) ; OUTER_LOOP : for ( NamedRange range : ranges ) { NamedRange thisRange = new NamedRange ( range . start , range . end , range . name ) ; for ( NamedRange otherRange : other . ranges ) { if ( otherRange . start <= thisRange . start && otherRange . end >= thisRange . end ) { continue OUTER_LOOP ; } if ( otherRange . end < thisRange . start || otherRange . start > thisRange . end ) { } else if ( otherRange . start <= thisRange . start ) { thisRange . start = otherRange . end + 1 ; } else if ( otherRange . end >= thisRange . end ) { thisRange . end = otherRange . start - 1 ; } else { newRanges . add ( new NamedRange ( thisRange . start , otherRange . start - 1 , thisRange . name ) ) ; thisRange . start = otherRange . end + 1 ; } } newRanges . add ( thisRange ) ; } ranges = newRanges ; }
target:  Assumptions: ranges are disjoint, non-contiguous, and ordered. Names of ranges are not necessarily preserved.