|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectuk.ac.starlink.array.ChunkStepper
public class ChunkStepper
Allows convenient stepping through an array. This class is provided as a convenience for applications code which wishes to iterate through an array in sections, at each stage obtaining a java primitive array containing a contiguous chunk of its pixels. This may be more efficient than using the single-element read/write methods of NDArray.
This class does not do anything very clever; it simply provides at each iteration base and length of a block which will take you from the start to the end of an array of given size over the lifetime of the iterator. These blocks will be of the same (user-defined or default) size with the possible exception of the last one, which will just mop up any remaining elements.
The simplest use of this class would therefore look something like this
ArrayAccess acc = nda.getAccess(); long npix = acc.getShape().getNumPixels(); for ( ChunkStepper cIt = new ChunkStepper( npix ); cIt.hasNext(); cIt.next() ) { int size = cIt.getSize(); Object buffer = acc.getType().newArray( size ); acc.read( buffer, 0, size ); doStuff( buffer ); }A more efficient loop would reuse the same buffer array to save on object creation/collection costs as follows:
ChunkStepper cIt = new ChunkStepper( npix ); Object buffer = acc.getType().newArray( cIt.getSize() ); for ( ; cIt.hasNext(); cIt.next() ) { acc.read( buffer, 0, cIt.getSize() ); doStuff( buffer ); }The
BufferIterator
class provides very similar functionality
in a way which may be slightly more convenient to use.
BufferIterator
Field Summary | |
---|---|
static int |
defaultChunkSize
The default size of chunks if not otherwise specified. |
Constructor Summary | |
---|---|
ChunkStepper(long length)
Create a new ChunkStepper with the default chunk size. |
|
ChunkStepper(long length,
int chunkSize)
Create a new ChunkStepper with a given chunk size. |
Method Summary | |
---|---|
long |
getBase()
The offset of the base of the current chunk. |
int |
getSize()
Get the size of the current chunk. |
long |
getTotalLength()
Returns the length of this ChunkStepper as supplied to the constructor - the total number of elements over which it will iterate. |
boolean |
hasNext()
See if iteration has finished. |
void |
next()
Iterates to the next chunk. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static int defaultChunkSize
Constructor Detail |
---|
public ChunkStepper(long length, int chunkSize)
length
- the total number of elements to iterate overchunkSize
- the size of chunk which will be used (except
perhaps for the last chunk)
IllegalArgumentException
- if chunkSize<=0
or length<0public ChunkStepper(long length)
length
- the total number of elements to iterate overMethod Detail |
---|
public boolean hasNext()
public int getSize()
public long getBase()
public void next()
NoSuchElementException
- if hasNext would return falsepublic long getTotalLength()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |