public class ChunkStepper extends Object
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
Modifier and Type | Field and Description |
---|---|
static int |
defaultChunkSize
The default size of chunks if not otherwise specified.
|
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public static int defaultChunkSize
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<0
public ChunkStepper(long length)
length
- the total number of elements to iterate overpublic boolean hasNext()
public int getSize()
public long getBase()
public void next()
NoSuchElementException
- if hasNext would return falsepublic long getTotalLength()
Copyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.