cdm/src/main/java/ucar/ma2/Range.java
public Range intersect(Range r) throws InvalidRangeException {
    if ((length() == 0) || (r.length() == 0))
      return EMPTY;
    if (this == VLEN || r == VLEN)
      return VLEN;

    int last = Math.min(this.last(), r.last());
    int resultStride = stride * r.stride();

    int useFirst;
    if (resultStride == 1) {  // both strides are 1
      useFirst = Math.max(this.first(), r.first());

    } else if (stride == 1) { // then r has a stride

      if (r.first() >= first())
        useFirst = r.first();
      else {
        int incr = (first() - r.first()) / resultStride;
        useFirst = r.first() + incr * resultStride;
        if (useFirst < first()) useFirst += resultStride;
      }

    } else if (r.stride == 1) { // then this has a stride

      if (first() >= r.first())
        useFirst = first();
      else {
        int incr = (r.first() - first()) / resultStride;
        useFirst = first() + incr * resultStride;
        if (useFirst < r.first()) useFirst += resultStride;
      }

    } else {
      throw new UnsupportedOperationException("Intersection when both ranges have a stride");
    }

    if (useFirst > last)
      return EMPTY;
    return new Range(name, useFirst, last, resultStride);
  }
public Range intersect(Range r) throws InvalidRangeException {
    if ((length() == 0) || (r.length() == 0))
      return EMPTY;
    if (this == VLEN || r == VLEN)
      return VLEN;

    int last = Math.min(this.last(), r.last());
    int resultStride = stride * r.stride();

    int useFirst;
    if (resultStride == 1) {  // both strides are 1
      useFirst = Math.max(this.first(), r.first());

    } else if (stride == 1) { // then r has a stride

      if (r.first() >= first())
        useFirst = r.first();
      else {
        int incr = (first() - r.first()) / resultStride;
        useFirst = r.first() + incr * resultStride;
        if (useFirst < first()) useFirst += resultStride;
      }

    } else if (r.stride == 1) { // then this has a stride

      if (first() >= r.first())
        useFirst = first();
      else {
        int incr = (r.first() - first()) / resultStride;
        useFirst = first() + incr * resultStride;
        if (useFirst < r.first()) useFirst += resultStride;
      }

    } else {
      throw new UnsupportedOperationException("Intersection when both ranges have a stride");
    }

    if (useFirst > last)
      return EMPTY;
    return new Range(name, useFirst, last, resultStride);
  }
public Range intersect ( Range r ) throws InvalidRangeException { if ( ( length ( ) == 0 ) || ( r . length ( ) == 0 ) ) return EMPTY ; if ( this == VLEN || r == VLEN ) return VLEN ; int last = Math . min ( this . last ( ) , r . last ( ) ) ; int resultStride = stride * r . stride ( ) ; int useFirst ; if ( resultStride == 1 ) { // both strides are 1 useFirst = Math . max ( this . first ( ) , r . first ( ) ) ; } else if ( stride == 1 ) { // then r has a stride if ( r . first ( ) >= first ( ) ) useFirst = r . first ( ) ; else { int incr = ( first ( ) - r . first ( ) ) / resultStride ; useFirst = r . first ( ) + incr * resultStride ; if ( useFirst < first ( ) ) useFirst += resultStride ; } } else if ( r . stride == 1 ) { // then this has a stride if ( first ( ) >= r . first ( ) ) useFirst = first ( ) ; else { int incr = ( r . first ( ) - first ( ) ) / resultStride ; useFirst = first ( ) + incr * resultStride ; if ( useFirst < r . first ( ) ) useFirst += resultStride ; } } else { throw new UnsupportedOperationException ( "Intersection when both ranges have a stride" ) ; } if ( useFirst > last ) return EMPTY ; return new Range ( name , useFirst , last , resultStride ) ; }
Create a new Range by intersecting with a Range using same interval as this Range.
NOTE: we dont yet support intersection when both Ranges have strides

@param r range to intersect
@return intersected Range, may be EMPTY
@throws InvalidRangeException elements must be nonnegative
Create a new Range by intersecting with a Range using same interval as this Range . NOTE : we dont yet support intersection when both Ranges have strides