Package ucar.ma2
Class Range
java.lang.Object
ucar.ma2.Range
- All Implemented Interfaces:
Iterable<Integer>,RangeIterator
Represents a set of integers, used as an index for arrays.
No duplicates are allowed.
It should be considered as a subset of the interval of integers [first(), last()] inclusive.
For example Range(1:11:3) represents the set of integers {1,4,7,10}
Note that Range no longer is always strided or monotonic.
Immutable.
Elements must be nonnegative and unique. EMPTY is the empty Range. VLEN is for variable length dimensions.
Standard iteration is
for (int idx : range) {
...
}
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRange(int length) Create a range starting at zero, with unit stride.Range(int first, int last) Create a range with unit stride.Range(int first, int last, int stride) Create a range with a specified values.Create a named range with unit stride.Create a named range with a specified name and values. -
Method Summary
Modifier and TypeMethodDescriptioncompact()Create a new Range by compacting this Range by removing the stride.Create a new Range by composing a Range that is relative to this Range.booleancontains(int want) Is want contained in this Range?copyWithName(String name) Make a copy with a different name.copyWithStride(int stride) Make a copy with a different stride.intelement(int i) Get ith elementbooleanRange elements with same first, last, stride are equal.intfirst()intgetFirstInInterval(int start) Find the first element in a strided array after some index start.Deprecated.use iterator() or foreachgetName()The name of this Range iterator.inthashCode()Override Object.hashCode() to implement equals.intindex(int want) Get the index for this element: inverse of elementCreate a new Range by intersecting with a Range using same interval as this Range.booleanintersects(Range r) Determine if a given Range intersects this one.iterator()intlast()intlength()The number of index in this iterator.static Rangemake(int first, int last) static RangebooleanIf this range is completely past the wanted rangeDeprecated.use copyWithName()setStride(int stride) Deprecated.use copyWithStride()shiftOrigin(int origin) Create a new Range shifting this range by a constant factor.intstride()toString()Create a new Range by making the union with a Range using same interval as this Range.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
EMPTY
-
ONE
-
VLEN
-
-
Constructor Details
-
Range
Create a range with unit stride.- Parameters:
first- first value in rangelast- last value in range, inclusive- Throws:
InvalidRangeException- elements must be nonnegative, 0 <= first <= last
-
Range
public Range(int length) Create a range starting at zero, with unit stride.- Parameters:
length- number of elements in the Range
-
Range
Create a named range with unit stride.- Parameters:
name- name of Rangefirst- first value in rangelast- last value in range, inclusive- Throws:
InvalidRangeException- elements must be nonnegative, 0 <= first <= last
-
Range
Create a range with a specified values.- Parameters:
first- first value in rangelast- last value in range, inclusivestride- stride between consecutive elements, must be > 0- Throws:
InvalidRangeException- elements must be nonnegative: 0 <= first <= last, stride > 0
-
Range
Create a named range with a specified name and values.- Parameters:
name- name of Rangefirst- first value in rangelast- last value in range, inclusivestride- stride between consecutive elements, must be > 0- Throws:
InvalidRangeException- elements must be nonnegative: 0 <= first <= last, stride > 0
-
-
Method Details
-
make
-
make
-
setStride
Deprecated.use copyWithStride()- Throws:
InvalidRangeException
-
copyWithStride
Make a copy with a different stride.- Throws:
InvalidRangeException
-
setName
Deprecated.use copyWithName()- Specified by:
setNamein interfaceRangeIterator
-
copyWithName
Make a copy with a different name.- Specified by:
copyWithNamein interfaceRangeIterator
-
getName
Description copied from interface:RangeIteratorThe name of this Range iterator.- Specified by:
getNamein interfaceRangeIterator- Returns:
- name, or null if none
-
first
public int first()- Returns:
- first in range
-
last
public int last()- Returns:
- last in range, inclusive
-
length
public int length()Description copied from interface:RangeIteratorThe number of index in this iterator.- Specified by:
lengthin interfaceRangeIterator- Returns:
- the number of elements in the range.
-
stride
public int stride()- Returns:
- stride, must be >= 1 when evenly strided, -1 if not // * @deprecated use iterator(), dont assume evenly strided
-
contains
public boolean contains(int want) Is want contained in this Range?- Parameters:
want- index in the original Range- Returns:
- true if the ith element would be returned by the Range iterator
-
compose
Create a new Range by composing a Range that is relative to this Range. Revised 2013/04/19 by Dennis Heimbigner to handle edge cases. See the commentary associated with the netcdf-c file dceconstraints.h, function dceslicecompose().- Parameters:
r- range relative to base- Returns:
- combined Range, may be EMPTY
- Throws:
InvalidRangeException- elements must be nonnegative, 0 <= first <= last
-
compact
Create a new Range by compacting this Range by removing the stride. first = first/stride, last=last/stride, stride=1.- Returns:
- compacted Range
- Throws:
InvalidRangeException- elements must be nonnegative, 0 <= first <= last
-
element
Get ith element- Parameters:
i- index of the element- Returns:
- the i-th element of a range.
- Throws:
InvalidRangeException- i must be: 0 <= i < length
-
index
Get the index for this element: inverse of element- Parameters:
want- the element of the range- Returns:
- index
- Throws:
InvalidRangeException- if illegal elem
-
intersect
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- Parameters:
r- range to intersect- Returns:
- intersected Range, may be EMPTY
- Throws:
InvalidRangeException- elements must be nonnegative
-
intersects
Determine if a given Range intersects this one. NOTE: we dont yet support intersection when both Ranges have strides- Parameters:
r- range to intersect- Returns:
- true if they intersect
- Throws:
UnsupportedOperationException- if both Ranges have strides
-
past
If this range is completely past the wanted range- Parameters:
want- desired range- Returns:
- true if first() > want.last()
-
shiftOrigin
Create a new Range shifting this range by a constant factor.- Parameters:
origin- subtract this from each element- Returns:
- shifted range
- Throws:
InvalidRangeException- elements must be nonnegative, 0 <= first <= last
-
union
Create a new Range by making the union with a Range using same interval as this Range. NOTE: no strides- Parameters:
r- range to add- Returns:
- intersected Range, may be EMPTY
- Throws:
InvalidRangeException- elements must be nonnegative
-
getFirstInInterval
public int getFirstInInterval(int start) Find the first element in a strided array after some index start. Return the smallest element k in the Range, such that- k >= first
- k >= start
- k <= last
- k = element of this Range
- Parameters:
start- starting index- Returns:
- first in interval, else -1 if there is no such element.
-
toString
-
equals
Range elements with same first, last, stride are equal. -
hashCode
public int hashCode()Override Object.hashCode() to implement equals. -
getIterator
Deprecated.use iterator() or foreachIterate over Range index- Returns:
- Iterator over element indices
-
iterator
- Specified by:
iteratorin interfaceIterable<Integer>- Specified by:
iteratorin interfaceRangeIterator
-