boost::multi_array上的维度无关循环
Dimension-independent loop over boost::multi_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;
}
}
我使用了一个索引对象来访问元素,因为我认为这比这里的[]-运算符更灵活。
但我怎么能在不知道维度N
的数量的情况下写这篇文章呢。有没有内置的方法?multi_array的文档不太清楚存在哪些类型的迭代器等。或者我必须求助于一些带有自定义指针的自定义方法,从指针计算索引,等等。?如果是这样的话,有什么建议吗?
好的,基于其中一条评论中已经提到的谷歌小组讨论以及库本身的一个例子,这里有一个可能的解决方案,可以让您在单个循环中迭代多数组中的所有值,并且提供了一种检索这些元素中每个元素的索引的方法(以防其他内容需要这样做,如在我的场景中)。
#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提供的默认内存布局。如果阵列基础或存储顺序发生更改,则必须对此进行调整。
缺少简单的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
如果您不需要索引,您可以简单地执行:
for (unsigned int i = 0; i < A.num_elements(); i++ )
{
tValue item = A.data()[i];
std::cout << item << std::endl;
}
基于之前的答案,我为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的简化版本,除了它应该与任何具有工作运算符<lt;。我测试得像
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
希望这对某人有用,并非常感谢最初的答案:它对我非常有用。
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- c++如何使用Curl Multi-Interface连续循环