How to specify degenerate dimension of boost multi_array at runtime?

前端 未结 2 2293
独厮守ぢ
独厮守ぢ 2021-02-20 14:06

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 t

2条回答
  •  一个人的身影
    2021-02-20 14:33

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

    #include                                                                                                                               
    
    typedef boost::multi_array 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;                                                                                                                                                 
    }
    

提交回复
热议问题