6.1: Indirection |
I
of
rows and a list J
of columns, and you want to modify
the array at all (i,j) positions where i is in I
and
j is in J
. This is a cartesian product of
the index sets I
and J
.
In all cases, Blitz++ expects a Standard Template Library
container. Some useful STL containers are
list<>
, vector<>
, deque<>
and set<>
.
Documentation of these classes is often provided
with your compiler, or see also the good documentation
at http://www.sgi.com/Technology/STL/.
STL containers are used because they are widely available
and provide easier manipulation of "sets" than Blitz++
arrays. For example, you can easily expand and merge
sets which are stored in STL containers; doing this is
not so easy with Blitz++ arrays, which are designed for
numerical work.
STL containers are generally included by writing
#include <list> // for list<> #include <vector> // for vector<> #include <deque> // for deque<> #include <set> // for set<>
6.2: 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:
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 (The
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 ]
.
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
tensor::i
notation is explained in the section on
index placeholders 3.6).
6.3: Cartesian-product indirection |
The Cartesian product of the sets I, J and K is the
set of (i,j,k) tuples for which i is in I, j is in J,
and k is in K.
Blitz++ implements cartesian-product
indirection using an adaptor which takes a
set of STL containers and iterates through their
Cartesian product. Note that the cartesian product
is never explicitly created. You create the
Cartesian-product adaptor by calling the
function:
template<class T_container>
indexSet(T_container& c1, T_container& c2, ...)
The returned adaptor can then be used in
the []
operator of an array object.
6.4: Indirection with lists of strips |
You can also do indirection with a container of
one-dimensional strips. This is useful
when you want to manipulate some arbitrarily-shaped,
well-connected subdomain of an array. By
representing the subdomain as a list of strips,
you allow Blitz++ to operate on vectors, rather
than scattered points; this is much more efficient.