|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.rit.util.Range
public class Range
Class Range provides a range of type int. A range object has the following attributes: lower bound L, upper bound U, stride S, and length N. A range object represents the following set of integers: {L, L+S, L+2*S, . . . , L+(N-1)*S}, where U = L+(N-1)*S.
You construct a range object by specifying the lower bound, upper bound, and stride. If the stride is omitted, the default stride is 1. The length is determined automatically. If the lower bound is greater than the upper bound, the range's length is 0 (an empty range).
You can use a range object to control a for loop like this:
Range range = new Range (0, N-1);
int lb = range.lb();
int ub = range.ub();
for (int i = lb; i <= ub; ++ i)
. . .
Note that the range is from lb() to ub() inclusive, so the
appropriate test in the for loop is i <= ub. Also note that it
usually reduces the running time to call ub() once, store the result
in a local variable, and use the local variable in the for loop test, than to
call ub() directly in the for loop test.
You can use a range object with a stride greater than 1 to control a for loop like this:
Range range = new Range (0, N-1, 2);
int lb = range.lb();
int ub = range.ub();
int stride = range.stride();
for (int i = lb; i <= ub; i += stride)
. . .
| Constructor Summary | |
|---|---|
Range()
Construct a new range object representing an empty range. |
|
Range(int lb,
int ub)
Construct a new range object with the given lower bound and upper bound. |
|
Range(int lb,
int ub,
int stride)
Construct a new range object with the given lower bound, upper bound, and stride. |
|
Range(Range range)
Construct a new range object that is a copy of the given range object. |
|
| Method Summary | |
|---|---|
Range |
chunk(int N1,
int N2)
Slice off a chunk of this range and return the chunk. |
boolean |
contains(int value)
Determine if this range contains the given value. |
boolean |
contains(Range range)
Determine if this range contains the given range. |
boolean |
equals(Object obj)
Determine if this range is equal to the given object. |
int |
hashCode()
Returns a hash code for this range. |
int |
lb()
Returns this range's lower bound. |
int |
length()
Returns this range's length. |
void |
readExternal(ObjectInput in)
Read this range from the given object input stream. |
int |
stride()
Returns this range's stride. |
Range |
subrange(int size,
int rank)
Partition this range and return one subrange. |
Range[] |
subranges(int size)
Partition this range and return all the subranges. |
String |
toString()
Returns a string version of this range. |
int |
ub()
Returns this range's upper bound. |
void |
writeExternal(ObjectOutput out)
Write this range to the given object output stream. |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public Range()
public Range(int lb,
int ub)
Note: L > U is allowed and stands for an empty range.
lb - Lower bound L.ub - Upper bound U.
public Range(int lb,
int ub,
int stride)
Note: L > U is allowed and stands for an empty range.
lb - Lower bound L.ub - Upper bound U.stride - Stride S >= 1.
IllegalArgumentException - (unchecked exception) Thrown if S < 1.public Range(Range range)
range - Range object to copy.| Method Detail |
|---|
public int lb()
public int ub()
public int stride()
public int length()
public boolean contains(int value)
value - Value to test.
public boolean contains(Range range)
range - Range to test.
public Range subrange(int size,
int rank)
Note that if size is greater than the length of this range, the returned subrange may be empty.
size - Number of subranges, size >= 1.rank - Rank of the desired subrange, 0 <= rank <
size.
IllegalArgumentException - (unchecked exception) Thrown if size or rank is out
of bounds.public Range[] subranges(int size)
Note that if size is greater than the length of this range, some of the returned subranges may be empty.
size - Number of subranges, size >= 1.
IllegalArgumentException - (unchecked exception) Thrown if size is out of bounds.
public Range chunk(int N1,
int N2)
N1 - Number of integers to discard (must be >= 0).N2 - Number of integers to include in the chunk (must be >= 0).
IllegalArgumentException - (unchecked exception) Thrown if N1 or N2 is out of
bounds.public boolean equals(Object obj)
equals in class Objectobj - Object to test.
public int hashCode()
hashCode in class Objectpublic String toString()
toString in class Object
public void writeExternal(ObjectOutput out)
throws IOException
writeExternal in interface Externalizableout - Object output stream.
IOException - Thrown if an I/O error occurred.
public void readExternal(ObjectInput in)
throws IOException
readExternal in interface Externalizablein - Object input stream.
IOException - Thrown if an I/O error occurred.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||