如何在运行时指定升压的multi_array的退化维度? [英] How to specify degenerate dimension of boost multi_array at runtime?

查看:180
本文介绍了如何在运行时指定升压的multi_array的退化维度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D的multi_array,我想作在系统运行时指定的尺寸2D片。我知道退化维度的指标,我想在退化的维度来提取切片的索引。目前,丑陋的解决方法看起来像:

I have a 3D multi_array and I would like to make 2D slices using dimensions specified at runtime. I know the index of degenerate dimension and the index of a slice that I want to extract in that degenerate dimension. Currently the ugly workaround looks like that:

if (0 == degenerate_dimension)
{
    Slice slice = input_array[boost::indices[slice_index][range()][range()]];
}
else if (1 == degenerate_dimension)
{
    Slice slice = input_array[boost::indices[range()][slice_index][range()]];
}
else if (2 == degenerate_dimension)
{
    Slice slice = input_array[boost::indices[range()][range()][slice_index]];
}

有构造为index_gen对象的更华丽的方式?
这样的事情:

Is there a more beautiful way to construct index_gen object? Something like that:

var slicer;
for(int i = 0; i < 3; ++i) {
    if (degenerate_dimension == i)
        slicer = boost::indices[slice_index];
    else
        slicer = boost::indices[range()];
}
Slice slice = input_array[slicer];

这似乎每次后续调用,以提高::指数:: operator []的返回不同类型的视维(即previous电话号码),所以没有办法用一个变量,可容纳临时为index_gen对象。

It seems each subsequent call to boost::indices::operator[] returns a different type depending on the dimensionality (i.e. number of previous calls), so there's no way to use a single variable, that can hold the temporary index_gen object.

推荐答案

请,试试这个。 Сode有一个缺点 - 它是指ranges_数组变量在升压::详细声明:: multi_array的命名空间。

Please, try this. Сode has one disadvantage - it refers to ranges_ array variable declared at boost::detail:: multi_array namespace.

#include <boost/multi_array.hpp>                                                                                                                              

typedef boost::multi_array<double, 3> array_type;                                                                                                             
typedef boost::multi_array_types::index_gen::gen_type<2,3>::type index_gen_type;                                                                                   
typedef boost::multi_array_types::index_range range;                                                                                                          

index_gen_type                                                                                                                                                     
func(int degenerate_dimension, int slice_index)                                                                                                               
{                                                                                                                                                             
    index_gen_type slicer;                                                                                                                                         
    int i;                                                                                                                                                    
    for(int i = 0; i < 3; ++i) {                                                                                                                              
        if (degenerate_dimension == i)                                                                                                                        
            slicer.ranges_[i] = range(slice_index);                                                                                                           
        else                                                                                                                                                  
            slicer.ranges_[i] = range();                                                                                                                      
    }                                                                                                                                                         
    return slicer;                                                                                                                                            
}                                                                                                                                                             

int main(int argc, char **argv)                                                                                                                               
{                                                                                                                                                             
    array_type myarray(boost::extents[3][3][3]);                                                                                                              
    array_type::array_view<2>::type myview = myarray[ func(2, 1) ];                                                                                           
    return 0;                                                                                                                                                 
}

这篇关于如何在运行时指定升压的multi_array的退化维度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆