c++ - boost::multi_array 上的维度无关循环?

标签 c++ algorithm boost multidimensional-array

假设我有一个 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;
    }
  }

我使用了一个索引对象来访问元素,因为我认为这比这里的 []-operator 更灵活。

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

最佳答案

好的,基于 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 提供的默认内存布局。如果阵列基础或存储顺序发生变化,则必须进行调整。

关于c++ - boost::multi_array 上的维度无关循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789560/

相关文章:

python - 数组中最小和最大元素相距小于 K 的最大子集

c++ - 将变量发送到 void 指针或 C++ 中的 void 指针引用

c++ - boost c++ 中的纪元时间到日期/时间格式

c++ - 如何按不同 std::vector 的值对 std::vector 进行排序?

c++ - 线程没有被销毁

c++ - 如何从 Visual Studio 远程构建 32 位 linux 模块?

c++ - head在这个链表中的状态

c# - 如何检查只有信用卡号码有效而不是借记卡..?

algorithm - 查找列表中值的最低总和以形成目标因子

c++ - C++控制台应用程序未返回