Package ucar.nc2.iosp

Class IndexChunker

java.lang.Object
ucar.nc2.iosp.IndexChunker

public class IndexChunker extends Object
Iterator to read/write subsets of a multidimensional array, finding the contiguous chunks. The iteration is monotonic in both src and dest positions.

Example for Integers:

 int[] read(IndexChunker index, int[] src) {
   int[] dest = new int[index.getTotalNelems()];
   while (index.hasNext()) {
     Indexer2.Chunk chunk = index.next();
     System.arraycopy(src, chunk.getSrcElem(), dest, chunk.getDestElem(), chunk.getNelems());
   }
   return dest;
 }
 
 int[] read(IndexChunker index, RandomAccessFile raf, long start_pos) {
   int[] dest = new int[index.getTotalNelems()];
   while (index.hasNext()) {
     Indexer2.Chunk chunk = index.next();
     raf.seek(start_pos + chunk.getSrcElem() * 4);
     raf.readInt(dest, chunk.getDestElem(), chunk.getNelems());
   }
   return dest;
 }
 
 // note src and dest misnamed
 void write(IndexChunker index, int[] src, RandomAccessFile raf, long start_pos) {
   while (index.hasNext()) {
     Indexer2.Chunk chunk = index.next();
     raf.seek(start_pos + chunk.getSrcElem() * 4);
     raf.writeInt(src, chunk.getDestElem(), chunk.getNelems());
   }
 }
 
Since:
Jan 2, 2008
  • Constructor Details

    • IndexChunker

      public IndexChunker(int[] srcShape, @Nullable Section wantSection) throws InvalidRangeException
      Constructor
      Parameters:
      srcShape - the shape of the source, eg Variable.getShape()
      wantSection - the wanted section in srcShape, ie must be subset of srcShape.
      Throws:
      InvalidRangeException - if wantSection is incorrect
  • Method Details

    • getTotalNelems

      public long getTotalNelems()
      Get total number of elements in wantSection
      Returns:
      total number of elements in wantSection
    • hasNext

      public boolean hasNext()
      If there are more chunks to process
      Returns:
      true if there are more chunks to process
    • next

      public IndexChunker.Chunk next()
      Get the next chunk
      Returns:
      the next chunk
    • toString

      public String toString()
      Overrides:
      toString in class Object