RWalib C Array Library User Guide > Array Indexing > alibGetOffsets
  

alibGetOffsets
For an n-dimensional array, convert a set of n-dimensional indices into a set of one-dimensional indices, i.e., convert a set of multidimensional indices into their flat equivalents. alibIndexNdTo1d and alibGetOffsets do the same thing but differ in the way the n-dimensional indices are supplied. For m conversions, alibIndexNdTo1d takes n pointers to m-element arrays, and alibGetOffsets takes m pointers to n-element arrays.
Prototype
void alibGetOffsets( wvlong n, wvlong *d, wvlong m, wvlong p, wvlong **b, wvlong *i )
Parameters
n — (Input) The number of dimensions for the reference array. This array, possibly virtual, is the array to which the indices refer.
*d — (Input) The n-element array of dimensions for the reference array.
m — (Input) The number of indices to be converted.
p — (Input) The number of coordinates supplied in the set of n-dimensional indices. Normally p = n, but setting p < n lets only the first p coordinates be specified, with the remaining coordinates implicitly assuming the value 0.
**b — (Input) The m-element array of pointers to p-element arrays defining the set of m n-dimensional indices into the reference array.
*i — (Input/Output) The m-element array. On return, i contains the one-dimensional equivalents to the n-dimensional indices b. For a reference array c, the relationship between the sets of indices is:
c[ i[j] ] = c[ b[j][0], ..., b[j][p-1], 0, ..., 0 ]
where if p = n then:
c[ i[j] ] = c[ b[j][0], ..., b[j][n-1] ]
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
   /* array of dimensions to use for two-dimensional, 
      three-dimensional, and four-dimensional examples */
   wvlong dims[4] = {2,2,3,4};
   /* make-up coordinates for a pair of points for 
      two-dimensional, three-dimensional, four-dimensional 
      examples */
   wvlong b2d0[2] = {1,1};
   wvlong b2d1[2] = {2,2};
   wvlong b3d0[3] = {0,1,1};
   wvlong b3d1[3] = {1,2,2};
   wvlong b4d0[4] = {0,0,1,1};
   wvlong b4d1[4] = {1,1,2,2};
   wvlong *b2[2] = {b2d0,b2d1};
   wvlong *b3[2] = {b3d0,b3d1};
   wvlong *b4[2] = {b4d0,b4d1};
   /* make an output array to use for the two-dimensional, 
      three-dimensional, and four-dimensional examples */
   wvlong i[2];
   /* just for reference, make some one-dimensional index data \
      to use in each example */
   UCHAR h[48];
   alibinit( NULL, NULL, NULL, NULL );
   alibIndexb( 48, 0, 1, h );
   printf( "\n\n just for reference, \n" );
   printf( " show a (3,4) array initialized with one-dimensional indices" );
      alibinit( NULL, NULL, NULL, NULL );
      alibPrintArrayb( 1, 1, 3, 4, h, NULL);
   printf( "\n\n for a (3,4) reference array, \n" );
   printf( " convert the pair of two-dimensional indices b2 into one-dimensional indices \n\n\n" );
      alibGetOffsets( 2, &dims[2], 2, 2, b2, i );
      printf( "%8lld %8lld\n", i[0], i[1] );
   printf( "\n\n just for reference, \n" );
   printf( " show a (2,3,4) array initialized with one-dimensional indices" );
      alibPrintArrayb( 1, 2, 3, 4, h, NULL);
   printf( "\n\n for a (2,3,4) reference array, \n" );
   printf( " convert the pair of three-dimensional indices b3 into one-dimensional indices \n\n\n" );
      alibGetOffsets( 3, &dims[1], 2, 3, b3, i );
   printf( "%8lld %8lld\n", i[0], i[1] ); 
   printf( "\n\n just for reference, \n" );
   printf( " show a (2,2,3,4) array initialized with one-dimensional indices" );
      alibPrintArrayb( 2, 2, 3, 4, h, NULL);
   printf( "\n\n for a (2,2,3,4) reference array, \n" );
   printf( " convert the pair of four-dimensional indices b4 into one-dimensional indices \n\n\n" );
      alibGetOffsets( 4, dims, 2, 4, b4, i );
      printf( "%8lld %8lld\n", i[0], i[1] );
}
 
Output:
 
 just for reference, 
 show a (3,4) array initialized with one-dimensional indices
 
   0   1   2   3
   4   5   6   7
   8   9  10  11
 
 for a (3,4) reference array, 
 convert the pair of two-dimensional indices b2 into one-dimensional indices 
 
       5       10
 
 just for reference, 
 show a (2,3,4) array initialized with one-dimensional indices
 
   0   1   2   3
   4   5   6   7
   8   9  10  11
 
  12  13  14  15
  16  17  18  19
  20  21  22  23
 
 for a (2,3,4) reference array, 
 convert the pair of three-dimensional indices b3 into one-dimensional indices 
 
       5       22
 
 just for reference, 
 show a (2,2,3,4) array initialized with one-dimensional indices
 
   0   1   2   3
   4   5   6   7
   8   9  10  11
 
  12  13  14  15
  16  17  18  19
  20  21  22  23
 
  24  25  26  27
  28  29  30  31
  32  33  34  35
 
  36  37  38  39
  40  41  42  43
  44  45  46  47
 
 for a (2,2,3,4) reference array, 
 convert the pair of four-dimensional indices b4 into one-dimensional indices 
 
       5       46

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.