Package uk.ac.starlink.array
Class ChunkStepper
- java.lang.Object
-
- uk.ac.starlink.array.ChunkStepper
-
public class ChunkStepper extends java.lang.Object
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 ); }
TheBufferIterator
class provides very similar functionality in a way which may be slightly more convenient to use.- Version:
- $Id$
- Author:
- Mark Taylor
- See Also:
BufferIterator
-
-
Field Summary
Fields Modifier and Type Field Description static int
defaultChunkSize
The default size of chunks if not otherwise specified.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Constructor Detail
-
ChunkStepper
public ChunkStepper(long length, int chunkSize)
Create a new ChunkStepper with a given chunk size.- Parameters:
length
- the total number of elements to iterate overchunkSize
- the size of chunk which will be used (except perhaps for the last chunk)- Throws:
java.lang.IllegalArgumentException
- if chunkSize<=0 or length<0
-
ChunkStepper
public ChunkStepper(long length)
Create a new ChunkStepper with the default chunk size.- Parameters:
length
- the total number of elements to iterate over
-
-
Method Detail
-
hasNext
public boolean hasNext()
See if iteration has finished.- Returns:
- true iff there are more chunks
-
getSize
public int getSize()
Get the size of the current chunk. It will be equal to the size specified in the constructor (or the default if none was specified), except for the last chunk, when it may be smaller.- Returns:
- the current chunk size
-
getBase
public long getBase()
The offset of the base of the current chunk. Zero for the first chunk, and increasing by getSize with each iteration after that.- Returns:
- the base of the current chunk
-
next
public void next()
Iterates to the next chunk.- Throws:
java.util.NoSuchElementException
- if hasNext would return false
-
getTotalLength
public long getTotalLength()
Returns the length of this ChunkStepper as supplied to the constructor - the total number of elements over which it will iterate.
-
-