Next: , Up: Indirection


6.1 Indirection using lists of array positions

The simplest kind of indirection uses a list of points. For one-dimensional arrays, you can just use an STL container of integers. Example:

       Array<int,1> A(5), B(5);
       A = 0;
       B = 1, 2, 3, 4, 5;
     
       vector<int> I;
       I.push_back(2);
       I.push_back(4);
       I.push_back(1);
     
       A[I] = B;

After this code, the array A contains [ 0 2 3 0 5 ].

Note that arrays on the right-hand-side of the assignment must have the same shape as the array on the left-hand-side (before indirection). In the statement A[I] = B, A and B must have the same shape, not I and B.

For multidimensional arrays, you can use an STL container of TinyVector<int,N_rank> objects. Example:

       Array<int,2> A(4,4), B(4,4);
       A = 0;
       B = 10*tensor::i + tensor::j;
     
       typedef TinyVector<int,2> coord;
     
       list<coord> I;
       I.push_back(coord(1,1));
       I.push_back(coord(2,2));
     
       A[I] = B;

After this code, the array A contains:

       0   0   0   0
       0  11   0   0
       0   0  22   0
       0   0   0   0

(The tensor::i notation is explained in the section on index placeholders Index placeholders).