Search code examples
c++templatesboostmultidimensional-arrayboost-multi-array

Iterating over the dimensions of a boost::multi_array


I'm trying to write some dimension-independent code for a template class in c++, using a boost::multi_array (though if other containers/data structures are better at this, I'd be happy to hear about it).

Given a dimension, I would like to iterate over the full range of every other dimension, returning a 1d view along the selected dimension. This is fairly straightforward, or at least it appears to be from the boost documentation.

What I can't figure out how to do is iterate the selected dimension over all of the dimensions of the array, when the dimension of the array is known at compile time.

Any advice on how to do this?


Solution

  • Normally you can do it with boost::multi_array, heres some sample code to create a 2D view of a 3D multi_array:

    #include "boost/multi_array.hpp"
    #include <cassert>
    #include <iostream>
    
    int main()
    {
        typedef boost::multi_array<double, 3> array_type;
        typedef array_type::index index;
    
        array_type myArray3D(boost::extents[3][4][2]);
    
        // Assign values to the elements
        int values = 0;
        for (index i = 0; i != 3; ++i)
        {
                for (index j = 0; j != 4; ++j)
                {
                        for (index k = 0; k != 2; ++k)
                        {
                                myArray3D[i][j][k] = values++;
                        }
                }
        }
        // Verify values
        int verify = 0;
        for (index i = 0; i != 3; ++i)
        {
                for (index j = 0; j != 4; ++j)
                {
                        for (index k = 0; k != 2; ++k)
                        {
                                std::cout << "[" << i << "]";
                                std::cout << "[" << j << "]";
                                std::cout << "[" << k << "] = ";
                                std::cout << myArray3D[i][j][k] << std::endl;
                                assert(myArray3D[i][j][k] == verify++);
                        }
                }
        }
    
        typedef boost::multi_array_types::index_range range;
        array_type::index_gen indices;
    
        // Create a new view with 2 dimentions fixing the 2nd dimention to 1
        array_type::array_view<2>::type myView =
        myArray3D[indices[range()][1][range()]];
        std::size_t numDims = myView.size();
        std::cout << "numDims = " << numDims << std::endl;
    
        for (index i = 0; i != 3; ++i)
        {
                for (index j = 0; j != 2; ++j)
                {
                        std::cout << "[" << i << "]";
                        std::cout << "[" << j << "] = ";
                        std::cout << myView[i][j] << std::endl;
                }
        }
    
        return 0;
    }