Changeset 2966


Ignore:
Timestamp:
07/12/07 14:53:39 (13 years ago)
Author:
curtis
Message:

Eliminate superfluous array allocation from raster/position methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/FormatTools.java

    r2796 r2966  
    268268 
    269269  /** 
    270    * Computes a unique 1-D index corresponding to the multidimensional 
    271    * position given in the pos array, using the specified lengths array 
    272    * as the maximum value at each positional dimension. 
     270   * Computes a unique 1-D index corresponding 
     271   * to the given multidimensional position. 
     272   * @param lengths the maximum value for each positional dimension 
     273   * @param pos position along each dimensional axis 
     274   * @return rasterized index value 
    273275   */ 
    274276  public static int positionToRaster(int[] lengths, int[] pos) { 
    275     int[] offsets = new int[lengths.length]; 
    276     if (offsets.length > 0) offsets[0] = 1; 
    277     for (int i=1; i<offsets.length; i++) { 
    278       offsets[i] = offsets[i - 1] * lengths[i - 1]; 
    279     } 
     277    int offset = 1; 
    280278    int raster = 0; 
    281     for (int i=0; i<pos.length; i++) raster += offsets[i] * pos[i]; 
     279    for (int i=0; i<pos.length; i++) { 
     280      raster += offset * pos[i]; 
     281      offset *= lengths[i]; 
     282    } 
    282283    return raster; 
    283284  } 
    284285 
    285286  /** 
    286    * Computes a unique 3-D position corresponding to the given raster 
    287    * value, using the specified lengths array as the maximum value at 
    288    * each positional dimension. 
     287   * Computes a unique N-D position corresponding 
     288   * to the given rasterized index value. 
     289   * @param lengths the maximum value at each positional dimension 
     290   * @param raster rasterized index value 
     291   * @return position along each dimensional axis 
    289292   */ 
    290293  public static int[] rasterToPosition(int[] lengths, int raster) { 
    291     int[] offsets = new int[lengths.length]; 
    292     if (offsets.length > 0) offsets[0] = 1; 
    293     for (int i=1; i<offsets.length; i++) { 
    294       offsets[i] = offsets[i - 1] * lengths[i - 1]; 
    295     } 
    296     int[] pos = new int[lengths.length]; 
     294    return rasterToPosition(lengths, raster, new int[lengths.length]); 
     295  } 
     296 
     297  /** 
     298   * Computes a unique N-D position corresponding 
     299   * to the given rasterized index value. 
     300   * @param lengths the maximum value at each positional dimension 
     301   * @param raster rasterized index value 
     302   * @param pos preallocated position array to populate with the result 
     303   * @return position along each dimensional axis 
     304   */ 
     305  public static int[] rasterToPosition(int[] lengths, int raster, int[] pos) { 
     306    int offset = 1; 
    297307    for (int i=0; i<pos.length; i++) { 
    298       int q = i < pos.length - 1 ? raster % offsets[i + 1] : raster; 
    299       pos[i] = q / offsets[i]; 
     308      int offset1 = offset * lengths[i]; 
     309      int q = i < pos.length - 1 ? raster % offset1 : raster; 
     310      pos[i] = q / offset; 
    300311      raster -= q; 
     312      offset = offset1; 
    301313    } 
    302314    return pos; 
Note: See TracChangeset for help on using the changeset viewer.