我必须n
在编译期间计算一维数组内维矩阵中元素的位置。简而言之,每个索引的array[i][j][k]....[n] -> array[...]
, where i,j,k,...,n
in和维度的映射是。这意味着,每一行和每一列都有 3 个元素。{1,2,3}
DIM = 3
我的主要问题是编写n
索引(参数包)的总和,作为模板并constexpr
用于在编译时评估总和。
我在其他堆栈帖子中的研究得出了以下 3 维公式:
a[i][j][k] -> a[(i*DIM*DIM) + (j*DIM) + k]
如果我们将其扩展为n
维度,则会得到以下公式:
a[i][j][k]....[n] -> a[(n*DIM ^ (indexAmount-1)] +... + (i*DIM*DIM) + (j*DIM) + k].
此外,i
使用模板和 编写代码来生成和的加数,constexpr
如下面的代码所示。
/**
* calculates DIM3^(indexPos)
*/
template<auto count>
int constexpr multiple_dim(){
if constexpr (count == 0){
return 1;
}else{
return DIM3 * multiple_dim<count-1>();
}
}
/**
*
*calculates addends for the end summation
* e.g if we have 3 indices i,j,k. j would be at position 2
* and j = 1. The parameters would be IndexPos = 2, index = 1.
*/
template<auto indexPos, auto index>
int constexpr calculate_flattened_index(){
if constexpr (indexPos == 0){
return (index-1);
}else{
return (index-1) * multiple_dim<indexPos>();
}
}
/**
* calculates the position of an element inside a
* nD matrix and maps it to a position in 1D
* A[i][j]..[n] -> ???? not implemented yet
* @tparam Args
* @return
*/
template<auto ...Args>
[[maybe_unused]] auto constexpr pos_nd_to_1d(){
/* maybe iterate over all indices inside the parameter pack?
const int count = 1;
for(int x : {Args...}){
}
return count;
*/
}
3D 矩阵中元素的示例输出A
。
A111
, A121
, A131
. 3 个元素的总和将是 1D 中的位置。例如A121 -> 0 + 3 + 0 = 3
。A111
将被放置在 的一维数组中array[3]
。
std::cout << "Matrix A111" << std::endl;
//A111
std::cout << calculate_flattened_index<0 , 1>() << std::endl;
std::cout << calculate_flattened_index<1 , 1>() << std::endl;
std::cout << calculate_flattened_index<2 , 1>() << std::endl;
std::cout << "Matrix A121" << std::endl;
//A121
std::cout << calculate_flattened_index<0 , 1>() << std::endl;
std::cout << calculate_flattened_index<1 , 2>() << std::endl;
std::cout << calculate_flattened_index<2 , 1>() << std::endl;
std::cout << "Matrix A131" << std::endl;
//A131
std::cout << calculate_flattened_index<0 , 1>() << std::endl;
std::cout << calculate_flattened_index<1 , 3>() << std::endl;
std::cout << calculate_flattened_index<2 , 1>() << std::endl;
Output:
Matrix A111
0
0
0
Matrix A121
0
3
0
Matrix A131
0
6
0
所需的输出可能类似于以下代码:
函数调用
pos_nd_to_1d<1,1,1>() //A111
pos_nd_to_1d<1,2,1>() //A121
pos_nd_to_1d<1,3,1>() //A131
输出:
0 //0+0+0
3 //0+3+0
6 //0+6+0