[英]Nesting boost::multi_array?
I have a template class which does some computation and returns multi_array, a bit oversimplified like this:我有一个模板类,它进行一些计算并返回 multi_array,有点过于简单,如下所示:
template <typename T>
class C
{
public:
typedef boost::multi_array<T, 2> result_type;
void do_something(T const& data, unsigned i, unsigned j); // add result to R[i][j]
result_type R;
};
Instantiating the class with a simple type T=double
works fine.使用简单类型
T=double
实例化类可以正常工作。 Now, I want to instantiate with " T=boost::multi_array<double, 1>
", but such that the result type is boost::multi_array<double, 3>
.现在,我想用“
T=boost::multi_array<double, 1>
”进行实例化,但是结果类型是boost::multi_array<double, 3>
。
The definition multi_array<multi_array<T, N>, M>>
does clearly not result in multi_array<T, N+M>
, it is just a N-dimensional multi_array with elements being M-dimensional multi_arrays ...定义
multi_array<multi_array<T, N>, M>>
显然不会导致multi_array<T, N+M>
,它只是一个 N 维 multi_array 元素是 M 维 multi_arrays ...
The idea to generate such a type is motivated by the Boost manual saying生成这种类型的想法是由 Boost 手册中所说的
MultiArray is recursively defined;
MultiArray 是递归定义的; the containers at each level of the container hierarchy model MultiArray as well.
容器层次模型 MultiArray 的每个级别的容器也是如此。 Actually, it turns out that the "elements" of the intermediate
multi_array
levels are of typesubarray
.实际上,中间
multi_array
级别的“元素”是subarray
类型。
Can one use subarray
to generate a multi_array
type with an effective dimensionality N+M
?可以使用
subarray
生成具有有效维度N+M
的multi_array
类型吗? Maybe somehow along the following lines:也许以某种方式沿着以下几行:
typedef typename boost::multi_array<double, 3>::subarray<1>::type value_type;
boost::multi_array<value_type, 2> a;
I'm looking for a relatively clean solution (not a lengthy hack), if this is not possible with the multi_array interface I will better rethink the design of what I'm going to implement.我正在寻找一个相对干净的解决方案(不是冗长的 hack),如果 multi_array 接口无法做到这一点,我会更好地重新考虑我将要实现的设计。
I think it makes no sense to instantiate multi_array
with elements of type multi_array
(although it might compile).我认为用
multi_array
类型的元素实例化multi_array
是没有意义的(尽管它可能会编译)。 For example, it would not lead to a contiguous memory layout since the elements manage memory themselves.例如,它不会导致连续的内存布局,因为元素自己管理内存。
To solve the problem that motivated my question, I came up with the following solution:为了解决激发我的问题的问题,我提出了以下解决方案:
template <typename T>
class C
{
enum { extra_rank = get_rank<T>() };
public:
typedef boost::multi_array<T, 2 + extra_rank> result_type;
}
The class defines a multi_array
with extra dimensions depending on the type of T
.该类根据
T
的类型定义了具有额外维度的multi_array
。 The helper function get_rank
checks whether T
is multi_array
and returns its dimensionality
, otherwise it returns 0.辅助函数
get_rank
检查T
是否为multi_array
并返回其dimensionality
,否则返回 0。
Of course it makes sense to instantiate a nested Boost.MA, multi_array<multi_array<T, N>, M>>
.当然,实例化嵌套的 Boost.MA,
multi_array<multi_array<T, N>, M>>
是有意义的。 However you are mistaken to think that this is equivalent to multi_array<T, N + M>
, it is not.但是,您错误地认为这等效于
multi_array<T, N + M>
,事实并非如此。
Conceptually, this is because in the former case the nested arrays of dimensions M
can have all different sizes.从概念上讲,这是因为在前一种情况下,维度为
M
的嵌套数组可以具有所有不同的大小。
In practice because, even if all have same sizes, the nested array will not be contiguous (or regularly strided) memory.在实践中,因为即使所有数组都具有相同的大小,嵌套数组也不会是连续的(或定期跨步的)内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.