Boost logo

Boost Users :

From: chris dion (evolocity_at_[hidden])
Date: 2007-08-22 18:06:24


Hi

I'm having a problem with the boost/multi_array and I'm hoping someone here
can help, or at least explain why the following doesn't work.

The problem: I have a multi_array of ints that I want to be able to view as
being either row major or column major to simplify a number algorithms. I
don't want to change the internal representation of the array (the default
c++ row major storage layout is fine), I just want to be able to interchange
the indexing order from array[row][col] to array[col][row] for the same
multi_array.

To do so, I attempted to define 2 array_views of the same multi_array with
different stride values, like this:

#include <iostream>
using namespace std;

#include <boost/multi_array.hpp>
using namespace boost;

int main(void)
{
        typedef multi_array<int, 2> intarray2;
        typedef intarray2::array_view<2>::type intarray2view2;
        typedef intarray2::array_view<1>::type intarray2view1;
        typedef intarray2::index_range intarray2range;

        intarray2 arr(extents[3][3]);

        // test data
        int a = 0;
        for (int r = 0; r < 3; r++)
        {
                for (int c = 0; c < 3; c++)
                {
                        arr[r][c] = a++;
                }
        }

        intarray2view2 rows =
arr[indices[intarray2range().stride(3)][intarray2range().stride(1)]];
        intarray2view2 cols =
arr[indices[intarray2range().stride(1)][intarray2range().stride(3)]];

        /*
        0 1 2
        3 4 5
        6 7 8
        */
        cout << rows[2][1]; // i expect this to print 7
        cout << cols[2][1]; // i expect this to print 5
}

Unfortunately, this doesn't work at all (an assert fails when indexing the
views). I debugged (using Visual C 2005), and found that the 'rows'
array_view has a stride collection of [9, 1], while the 'cols' array_view
stride collection is [3, 3]. Both are different than what I specified
during construction. It appears that they're being multiplied by the
extents of the first dimension of the multi_view array (in this example: 3).

I can produce a column major view of a single column of the array using a 1
dimensional view constructed using a degenerate range, like this:

intarray2view1 col= arr[indices[1][intarray2range()]]; // column 1 in arr

but that's not what I'm hoping to achieve.

Can someone explain the behavior above and/or suggest a way of creating a 2d
column major array_view of a 2d multi_array?

Thanks
- Chris Dion

_________________________________________________________________
Show Your Messenger Buddies How You Really Feel
http://www.freemessengeremoticons.ca/?icid=EMENCA122


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net