Boost Users :

Subject: [Boost-users] multi_array iteration problem
From: Petros (pmamales_at_[hidden])
Date: 2012-06-18 13:54:28

Hi,
I would greatly appreciate your help with this, as I have spent considerable time and have run out of ideas:
In the following piece of code, I want to recursively visit the elements of a 4-dimensional array.
If all the dimensions are the same, there is no problem.
However, if there is a dimension of different length the program crashes !
TIA,
Petros

ps: This is only a toy problem for the purpose of showing the issue. [ using boost 1.49, with msvc 2010, on win 7 ].
Please, toggle the comment in the line :
shape[1] = 3;
to see the problem.

The program :

int test_ma_iterationDIM(){
const size_t DIM = 4 ;
typedef multi_array< int, DIM > IntMADIM;

array<size_t, DIM> shape;
fill( shape.begin(), shape.end(), 2 ) ;
// shape[1] = 3;

// Setting a[ijkl] = 1000*L + 100*K +10*J +I :
for ( size_t i=0; i!= shape[0]; ++i )
for ( size_t j=0; j!= shape[1]; ++j )
for ( size_t k=0; k!= shape[2]; ++k )
for ( size_t l=0; l!= shape[3]; ++l )
a[i][j][k][l] = (i+1) + (j+1) * 10 + (k + 1) * 100 + (l+1)*1000;

int * data = a.data();

array< index, DIM > lowerBounds, upperBounds;
for ( size_t i = 0; i != DIM; ++i ){
lowerBounds[i] = 0;
upperBounds[i] = shape[i] ;
}

typedef iterator3::value_type::const_iterator iterator2;
typedef iterator2::value_type::const_iterator iterator1;
typedef iterator1::value_type::const_iterator iterator0;

iterator3 begin3 = a.begin() + lowerBounds[3], end3 = a.begin() + upperBounds[3];
for ( iterator3 i3 = begin3; i3 != end3; ++i3 ){

iterator2 begin2 = i3->begin() + lowerBounds[2], end2 = i3->begin() + upperBounds[2];
for ( iterator2 i2 = begin2; i2 != end2; ++i2 ){
iterator1 begin1 = i2->begin() + lowerBounds[1], end1 = i2->begin() + upperBounds[1];
for ( iterator1 i1 = begin1; i1 != end1; ++i1 ){

iterator0 begin0 = i1->begin() + lowerBounds[0], end0 = i1->begin() + upperBounds[0];
for ( iterator0 i0 = begin0; i0 != end0; ++i0 ){

cout << *i0 << endl;
}
}
}
}
return 1;
}