【问题标题】:Dimension-independent loop over boost::multi_array?boost::multi_array 上的维度无关循环?
【发布时间】:2012-04-05 01:59:24
【问题描述】:

假设我有一个 N 维 boost::multi_array(为简单起见为 int 类型),其中 N 在编译时已知但可以变化(即是非类型模板参数)。让我们假设所有维度的大小都相同m

typedef boost::multi_array<int, N> tDataArray;
boost::array<tDataArray::index, N> shape;
shape.fill(m);
tDataArray A(shape);

现在我想遍历A 中的所有条目,例如打印它们。例如,如果 N 为 2,我想我会写这样的东西

  boost::array<tDataArray::index, 2> index;
  for ( int i = 0; i < m; i++ )
  {
    for ( int j = 0; j < m; j++ )
    {
      index = {{ i, j }};
      cout << A ( index ) << endl;
    }
  }

我使用索引对象来访问元素,因为我认为这比这里的 [] 运算符更灵活。

但是我怎么能在不知道维数N的情况下写这个。有没有内置的方法? multi_array 的文档对存在哪些类型的迭代器等不是很清楚。 还是我必须求助于一些带有自定义指针的自定义方法,从指针计算索引等?如果是这样 - 有什么建议这样的算法会是什么样子?

【问题讨论】:

  • @SamDeHaan 有点——不过,并非一直如此。我现在才意识到这个例子(我以前见过)使用了一个自定义版本的for_each,它在 multi_array 例子的另一个文件中声明。我确实尝试了 std::for_each 并提升了 FOR_EACH 并且无法让它工作 - 我现在将研究这个自定义解决方案。但是我仍然需要一种方法来在遍历数组时获取实际索引(可能作为 tDataArray::index 的数组)。这是因为我需要的不仅仅是打印..
  • 顺便说一句:stackoverflow.com/questions/6434678/… 也是相关的。但即使问题被标记为已解决,但实际上并非如此(据我所知)。
  • 这个groups.google.com/group/boost-list/browse_thread/thread/…(第二个答案)看起来很有趣。虽然本质上这将通过索引算法回到“多维数组”。我会尝试弄清楚我是否可以将其中一些建议应用于手头的问题。

标签: c++ algorithm boost multidimensional-array


【解决方案1】:

好的,基于已在其中一个 cmets 中提到的 Google groups discussion 和库本身中的 one of the examples,这是一个可能的解决方案,可让您在单个循环中迭代多数组中的所有值 并且提供了一种方法来检索每个元素的索引(以防其他一些东西需要它,就像我的场景一样)。

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

const unsigned short int DIM = 3;
typedef double tValue;
typedef boost::multi_array<tValue,DIM> tArray;
typedef tArray::index tIndex;
typedef boost::array<tIndex, DIM> tIndexArray;

tIndex getIndex(const tArray& m, const tValue* requestedElement, const unsigned short int direction)
{
  int offset = requestedElement - m.origin();
  return(offset / m.strides()[direction] % m.shape()[direction] +  m.index_bases()[direction]); 
}

tIndexArray getIndexArray( const tArray& m, const tValue* requestedElement )
{
  tIndexArray _index;
  for ( unsigned int dir = 0; dir < DIM; dir++ )
  {
    _index[dir] = getIndex( m, requestedElement, dir );
  }

  return _index;
}


int main()
{ 
  double* exampleData = new double[24];
  for ( int i = 0; i < 24; i++ ) { exampleData[i] = i; }

  tArray A( boost::extents[2][3][4] );
  A.assign(exampleData,exampleData+24);

  tValue* p = A.data();
  tIndexArray index;
  for ( int i = 0; i < A.num_elements(); i++ )
  {
    index = getIndexArray( A, p );
    std::cout << index[0] << " " << index[1] << " " << index[2] << " value = " << A(index) << "  check = " << *p << std::endl;
    ++p;
  }

  return 0;
}

输出应该是

0 0 0 value = 0 check = 0
0 0 1 value = 1 check = 1
0 0 2 value = 2 check = 2
0 0 3 value = 3 check = 3
0 1 0 value = 4 check = 4
0 1 1 value = 5 check = 5
0 1 2 value = 6 check = 6
0 1 3 value = 7 check = 7
0 2 0 value = 8 check = 8
0 2 1 value = 9 check = 9
0 2 2 value = 10 check = 10
0 2 3 value = 11 check = 11
1 0 0 value = 12 check = 12
1 0 1 value = 13 check = 13
1 0 2 value = 14 check = 14
1 0 3 value = 15 check = 15
1 1 0 value = 16 check = 16
1 1 1 value = 17 check = 17
1 1 2 value = 18 check = 18
1 1 3 value = 19 check = 19
1 2 0 value = 20 check = 20
1 2 1 value = 21 check = 21
1 2 2 value = 22 check = 22
1 2 3 value = 23 check = 23

所以内存布局从外部索引到内部索引。请注意,getIndex 函数依赖于 boost::multi_array 提供的默认内存布局。如果数组基数或存储顺序发生变化,则必须进行调整。

【讨论】:

  • 这很棒 - 从文档中不清楚如何做到这一点。
  • 其实我不明白为什么循环计数器i没有出现在num_elements()循环体中?
  • @DavidDoria 他正在递增p,这只是一个指向数组的原始指针,然后根据该指针重建索引。循环计数器只是运行循环正确的次数。
【解决方案2】:

如果你不需要索引,你可以简单地做:

  for (unsigned int i = 0; i < A.num_elements(); i++ )
  {
    tValue item = A.data()[i];
    std::cout << item << std::endl;
  }

【讨论】:

    【解决方案3】:

    根据我为 boost::multi_arrays 生成插入运算符的这个不错的重载版本之前的答案

    using namespace std;
    using namespace boost::detail::multi_array;
    
    template <typename T , unsigned long K>
    ostream &operator<<( ostream &os , const boost::multi_array<T , K> &A )
    {
     const T* p = A.data();
     for( boost::multi_array_types::size_type i = A.num_elements() ; i-- ; ++p )
     {
      os << "[ ";
      for( boost::multi_array_types::size_type k = 0 ; k < K ; ) {
       os << ( p - A.origin()  ) / A.strides()[ k ] % A.shape()[ k ]
             +  A.index_bases()[ k ];
       if( ++k < K )
        os << ", ";
        }
      os << " ] = " << *p << endl;
      }
    
     return os;
     }
    

    它只是答案 1 的简化版本,除了它应该适用于具有工作运算符

     typedef boost::multi_array<double, 3> array_type;
     typedef array_type::index index;
    
     index x = 3;
     index y = 2;
     index z = 3;
    
     array_type A( boost::extents[ x ][ y ][ z ] );
    
     // Assign values to the elements
     int values = 0;
     for( index i = 0 ; i < x ; ++i ) 
      for( index j = 0 ; j < y ; ++j )
       for( index k = 0 ; k < z ; ++k )
        A[ i ][ j ][ k ] = values++;
    
     // print the results
     cout << A << endl;
    

    它似乎有效:

    [ 0, 0, 0 ] = 0
    [ 0, 0, 1 ] = 1
    [ 0, 0, 2 ] = 2
    [ 0, 1, 0 ] = 3
    [ 0, 1, 1 ] = 4
    [ 0, 1, 2 ] = 5
    [ 1, 0, 0 ] = 6
    [ 1, 0, 1 ] = 7
    [ 1, 0, 2 ] = 8
    [ 1, 1, 0 ] = 9
    [ 1, 1, 1 ] = 10
    [ 1, 1, 2 ] = 11
    [ 2, 0, 0 ] = 12
    [ 2, 0, 1 ] = 13
    [ 2, 0, 2 ] = 14
    [ 2, 1, 0 ] = 15
    [ 2, 1, 1 ] = 16
    [ 2, 1, 2 ] = 17
    

    希望这对某人有用,非常感谢原始答案:它对我非常有用。

    【讨论】:

    • 这个答案很好,因为您使用模板的值 K 来推断函数中的维数。这不是函数能够进行迭代工作的非常强大的方法,因为函数无法调查索引,但并非所有函数都需要它。因此,如果您使它更简洁,我会赞成您的回答。在编写 SO 问题/答案时,尽量不要在示例中变得过于“现实”——它们变得不必要地复杂。最简单的就是最好的。
    【解决方案4】:

    缺少简单的 boost 多数组示例。所以这里有一个非常简单的例子,说明如何使用索引填充 boost 多数组以及如何使用单个指针读取所有条目。

    typedef boost::multi_array<double, 2> array_type;
    typedef array_type::index index;
    
    array_type A(boost::extents[3][2]);
    
    //  ------> x 
    // | 0 2 4
    // | 1 3 5
    // v
    // y
    double value = 0;
    for(index x = 0; x < 3; ++x) 
        for(index y = 0; y < 2; ++y)
            A[x][y] = value++;
    
    double* it  = A.origin();
    double* end = A.origin() + A.num_elements();
    for(; it != end; ++it){
        std::cout << *it << " ";
    }
    
    // -> 0 1 2 3 4 5
    

    【讨论】:

      猜你喜欢
      • 2012-02-29
      • 2011-09-20
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      • 2014-07-08
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多