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 array;
[...]
boost::array
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
#include
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
boost::detail::multi_array::extent_gen<NumDims> ArrayToExtentGen(
const boost::array& array)
{
return ArrayToExtentGenRecurse::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& array)
{
return ArrayToExtentGenRecurse
::Apply(array)[array[NumDimsOut-1]];
} //Apply
}; //ArrayToExtentGen
//! Partial specialisation of the above, to stop recursing at 0 dimensions
template
class ArrayToExtentGenRecurse {
public:
static boost::detail::multi_array::extent_gen<0> Apply(
const boost::array& array)
{
return boost::detail::multi_array::extent_gen<0>();
} //Apply
}; //ArrayToExtentGen
} //namespace YungChin
#endif