简体   繁体   English

boost :: multi_array索引到内存偏移量的转换

[英]boost::multi_array index to memory offset conversion

I have three-dimensional boost::multi_array object. 我有三维boost::multi_array对象。 I would like to convert an absolute offset from the origin to a multi-dimensional index and vice versa. 我想将origin的绝对偏移量转换为多维索引,反之亦然。 Is there an easy way to do so using boost's built-in facilities, or I need to compute it myself given the array's shape and strides? 有没有一种简单的方法可以使用boost的内置功能,或者我需要根据阵列的形状和步幅自己计算它?

Of course you can use some library facility. 当然,您可以使用一些图书馆设施。

For the offset, you can easily use pointer arithmetics¹: 对于偏移量,您可以轻松地使用指针算法¹:

size_t offset = &ma[1][2][3] - ma.data();
std::cout << "Offset of ma[1][2][3]: " << offset << "\n";

The documentation divulges a more elaborate way to get the element address: link 该文档详细介绍了获取元素地址的详细方法: 链接

 This expression accesses a specific element of a.index_list is the unique set of indices that address the element returned. It is equivalent to the following code (disregarding intermediate temporaries): // multiply indices by strides std::transform(index_list.begin(), index_list.end(), a.strides(), tmp.begin(), std::multiplies<index>()), // add the sum of the products to the origin *std::accumulate(tmp.begin(), tmp.end(), a.origin()); 

The inverse less simple. 反之则简单。 I couldn't find a direct way. 我找不到直接的方法。

¹ note: use std::addressof to guard against elements overload operator& in generic code ¹注意:使用std::addressof可以防止泛型代码中的元素重载operator&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM