Boost logo

Boost Users :

Subject: [Boost-users] [multi_array] Looking for elegant way to convert between array and extent_gen objects
From: Oei, YC (oei.yungchin_at_[hidden])
Date: 2009-05-21 16:48:31


Dear list members,

I've been working with multi_array lately, and am trying to
exclusively use boost::arrays as the indexing object when accessing
multi_arrays, in order to keep my code dimension-independent. There is
a little issue however, in that not all multi_array methods take
arrays as parameters; e.g. multi_array::resize() only takes an
extent_gen parameter.

I've come up with a workaround, which converts an array to the
corresponding extent_gen object, and this works.... but it is an ugly
amount of code for such a little task. I was hoping I might ask if you
have better suggestions? Is there a way to make extents-notation
dimension-independent?

The content of the header file with my current solution is pasted
below to illustrate what I'm trying to achieve - a (somewhat overly
trivial) use case is as follows:

#include "ArrayToExtentGen.h"
[...]
boost::multi_array<SomeValueType, SomeNumberOfDimensions> array;
[...]
boost::array<boost::multi_array_types::index, SomeNumberOfDimensions>
    position = {{0, 0, 0, 0, 0, 0, 0, 0}};
array.resize(ArrayToExtentGen(position));
[...]

Thanks for your thoughts!
Yung-Chin

-- ArrayToExtentGen.h --

#ifndef GUARD_ArrayToExtentGen_h
#define GUARD_ArrayToExtentGen_h

#include <boost/array.hpp>
#include <boost/multi_array.hpp>

namespace YungChin {

//! Helper class for ArrayToExtentGen() to recurse through dimensions
template <
    size_t NumDimsIn,
    size_t NumDimsOut>
class ArrayToExtentGenRecurse;

//! Convert boost::array (used for addressing on multi_array) into extent_gen
template<size_t NumDims>
boost::detail::multi_array::extent_gen<NumDims> ArrayToExtentGen(
    const boost::array<boost::multi_array_types::index, NumDims>& array)
{
    return ArrayToExtentGenRecurse<NumDims, NumDims>::Apply(array);
} //ArrayToExtentGen

template <
    size_t NumDimsIn,
    size_t NumDimsOut>
class ArrayToExtentGenRecurse {
public:
    static boost::detail::multi_array::extent_gen<NumDimsOut> Apply(
        const boost::array<boost::multi_array_types::index, NumDimsIn>& array)
    {
        return ArrayToExtentGenRecurse<NumDimsIn, NumDimsOut-1>
                   ::Apply(array)[array[NumDimsOut-1]];
    } //Apply
}; //ArrayToExtentGen

//! Partial specialisation of the above, to stop recursing at 0 dimensions
template <size_t NumDimsIn>
class ArrayToExtentGenRecurse<NumDimsIn, 0> {
public:
    static boost::detail::multi_array::extent_gen<0> Apply(
        const boost::array<boost::multi_array_types::index, NumDimsIn>& array)
    {
        return boost::detail::multi_array::extent_gen<0>();
    } //Apply
}; //ArrayToExtentGen

} //namespace YungChin

#endif


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