编译时生成的表
Compile time generated tables
由于一些技巧,我能够在编译时生成一个 table,但是 table 中的值不是很有用。例如 table 5x5 看起来像这样:
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
逗号是为了清楚起见。创建此 table 的代码如下:
#include <iostream>
using ll = long long;
template<typename type,type...data>
struct sequence
{
static type seq_data[sizeof...(data)];
static const ll size;
type operator[](ll index){
return seq_data[size-index-1];
}
};
template<typename type,type...data>
type sequence<type,data...>::seq_data[sizeof...(data)] = { data... };
template<typename type,type...data>
const ll sequence<type,data...>::size{sizeof...(data)};
template<ll n,ll l,ll...data> struct create_row
{
typedef typename create_row<n-1,l+1,l,data...>::value value;
};
template<ll l,ll...data>
struct create_row<0,l,data...>
{
typedef sequence<ll,data...> value;
};
template<ll cols,ll rows>
struct table
{
typename create_row<cols,1>::value row_data[rows];
static const ll size;
};
template<ll cols,ll rows>
const ll table<cols,rows>::size{cols*rows};
using namespace std;
int main()
{
table<5,5> my_table;
for(int i{0};i<5;i++)
{
for(int j{0};j<5;j++)
{
cout<<my_table.row_data[i][j]<<",";
}
cout<<endl;
}
}
如您所见,为了创建单行,我不得不在 结构 table 中使用硬编码值“1”,因此 create_table总是return相同的sequence,从1到n的一系列数字。因此,table 中的每一行都具有相同的值。
我想做的是在编译时为每一行编码一个不同的起始值,以便有一个 table 看起来像这样:
1,2,3,4,5,
6,7,8,9,10,
11 <CUT>
我找不到任何方法来创建这种 table。
你知道怎么做吗?
最后我不确定你的 post 你是否感兴趣
在:-
- 正在生成一个矩阵,该矩阵在编译时填充了连续的
某些函数的值
f(i)
以行优先顺序环绕矩阵,例如
Cols = 3; Rows = 3; f(i) = 2i; Vals = (1,2,3,4,5,6,7,8,9) ->
|02|04|06|
----------
|08|10|12|
----------
|14|16|18|
或:-
- 生成一个矩阵,其中连续的行在编译时填充
对于某些指定的初始
i
,具有某些函数 f(i)
的连续值
每行,例如
Cols = 3; f(i) = 3i; First_Vals = (4,7,10) ->
|12|15|18|
----------
|21|24|27|
----------
|30|33|36|
无论如何,有两种方法可以做到,这里是您可以使用的方法
符合 C++14 标准的编译器。 (正如@AndyG 评论的那样,适当的
编译时矩阵的实现——利用标准库
- 是 std::array
的 std::array
。)
#include <array>
#include <utility>
namespace detail {
template<typename IntType, IntType(*Step)(IntType), IntType Start, IntType ...Is>
constexpr auto make_integer_array(std::integer_sequence<IntType,Is...>)
{
return std::array<IntType,sizeof...(Is)>{{Step(Start + Is)...}};
}
template<typename IntType, IntType(*Step)(IntType), IntType Start, std::size_t Length>
constexpr auto make_integer_array()
{
return make_integer_array<IntType,Step,Start>(
std::make_integer_sequence<IntType,Length>());
}
template<
typename IntType, std::size_t Cols,
IntType(*Step)(IntType),IntType Start, std::size_t ...Rs
>
constexpr auto make_integer_matrix(std::index_sequence<Rs...>)
{
return std::array<std::array<IntType,Cols>,sizeof...(Rs)>
{{make_integer_array<IntType,Step,Start + (Rs * Cols),Cols>()...}};
}
} // namespace detail
/*
Return a compiletime initialized matrix (`std::array` of std::array`)
of `Cols` columns by `Rows` rows. Ascending elements from [0,0]
in row-first order are populated with successive values of the
constexpr function `IntType Step(IntType i)` for `i` in
`[Start + 0,Start + (Rows * Cols))`
*/
template<
typename IntType, std::size_t Cols, std::size_t Rows,
IntType(*Step)(IntType), IntType Start
>
constexpr auto make_integer_matrix()
{
return detail::make_integer_matrix<IntType,Cols,Step,Start>(
std::make_index_sequence<Rows>());
}
/*
Return a compiletime initialized matrix (`std::array` of std::array`)
of `Cols` columns by `sizeof...(Starts)` rows. Successive rows are populated
with successive values of the constexpr function `IntType Step(IntType i)`
for `i` in `[start + 0,start + Cols)`, for `start` successively in `...Starts`.
*/
template<typename IntType, std::size_t Cols, IntType(*Step)(IntType), IntType ...Starts>
constexpr auto make_integer_matrix()
{
return std::array<std::array<IntType,Cols>,sizeof...(Starts)>
{{detail::make_integer_array<IntType,Step,Starts,Cols>()...}};
}
您可以通过附加以下内容来制作演示程序:
#include <iostream>
using namespace std;
template<typename IntType>
constexpr auto times_3(IntType i)
{
return i * 3;
}
static constexpr auto m4x6 = make_integer_matrix<int,4,6,×_3<int>,4>();
static constexpr auto m5x1 = make_integer_matrix<int,5,×_3<int>,7>();
static constexpr auto m6x5 = make_integer_matrix<int,6,×_3<int>,11,13,17,19,23>();
static_assert(m4x6[0][0] == 12,"");
int main()
{
cout << "A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4" << endl;
for (auto const & ar : m4x6) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
cout << endl;
cout << "A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>"
<< endl;
for (auto const & ar : m6x5) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
cout << endl;
cout << "A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>"
<< endl;
for (auto const & ar : m5x1) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
return 0;
}
应该输出:
A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4
12 15 18 21
24 27 30 33
36 39 42 45
48 51 54 57
60 63 66 69
72 75 78 81
A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>
33 36 39 42 45 48
39 42 45 48 51 54
51 54 57 60 63 66
57 60 63 66 69 72
69 72 75 78 81 84
A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>
21 24 27 30 33
看到了at ideone
感兴趣
由于一些技巧,我能够在编译时生成一个 table,但是 table 中的值不是很有用。例如 table 5x5 看起来像这样:
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
逗号是为了清楚起见。创建此 table 的代码如下:
#include <iostream>
using ll = long long;
template<typename type,type...data>
struct sequence
{
static type seq_data[sizeof...(data)];
static const ll size;
type operator[](ll index){
return seq_data[size-index-1];
}
};
template<typename type,type...data>
type sequence<type,data...>::seq_data[sizeof...(data)] = { data... };
template<typename type,type...data>
const ll sequence<type,data...>::size{sizeof...(data)};
template<ll n,ll l,ll...data> struct create_row
{
typedef typename create_row<n-1,l+1,l,data...>::value value;
};
template<ll l,ll...data>
struct create_row<0,l,data...>
{
typedef sequence<ll,data...> value;
};
template<ll cols,ll rows>
struct table
{
typename create_row<cols,1>::value row_data[rows];
static const ll size;
};
template<ll cols,ll rows>
const ll table<cols,rows>::size{cols*rows};
using namespace std;
int main()
{
table<5,5> my_table;
for(int i{0};i<5;i++)
{
for(int j{0};j<5;j++)
{
cout<<my_table.row_data[i][j]<<",";
}
cout<<endl;
}
}
如您所见,为了创建单行,我不得不在 结构 table 中使用硬编码值“1”,因此 create_table总是return相同的sequence,从1到n的一系列数字。因此,table 中的每一行都具有相同的值。
我想做的是在编译时为每一行编码一个不同的起始值,以便有一个 table 看起来像这样:
1,2,3,4,5,
6,7,8,9,10,
11 <CUT>
我找不到任何方法来创建这种 table。
你知道怎么做吗?
最后我不确定你的 post 你是否感兴趣 在:-
- 正在生成一个矩阵,该矩阵在编译时填充了连续的
某些函数的值
f(i)
以行优先顺序环绕矩阵,例如
Cols = 3; Rows = 3; f(i) = 2i; Vals = (1,2,3,4,5,6,7,8,9) -> |02|04|06| ---------- |08|10|12| ---------- |14|16|18|
或:-
- 生成一个矩阵,其中连续的行在编译时填充
对于某些指定的初始
i
,具有某些函数f(i)
的连续值 每行,例如
Cols = 3; f(i) = 3i; First_Vals = (4,7,10) -> |12|15|18| ---------- |21|24|27| ---------- |30|33|36|
无论如何,有两种方法可以做到,这里是您可以使用的方法
符合 C++14 标准的编译器。 (正如@AndyG 评论的那样,适当的
编译时矩阵的实现——利用标准库
- 是 std::array
的 std::array
。)
#include <array>
#include <utility>
namespace detail {
template<typename IntType, IntType(*Step)(IntType), IntType Start, IntType ...Is>
constexpr auto make_integer_array(std::integer_sequence<IntType,Is...>)
{
return std::array<IntType,sizeof...(Is)>{{Step(Start + Is)...}};
}
template<typename IntType, IntType(*Step)(IntType), IntType Start, std::size_t Length>
constexpr auto make_integer_array()
{
return make_integer_array<IntType,Step,Start>(
std::make_integer_sequence<IntType,Length>());
}
template<
typename IntType, std::size_t Cols,
IntType(*Step)(IntType),IntType Start, std::size_t ...Rs
>
constexpr auto make_integer_matrix(std::index_sequence<Rs...>)
{
return std::array<std::array<IntType,Cols>,sizeof...(Rs)>
{{make_integer_array<IntType,Step,Start + (Rs * Cols),Cols>()...}};
}
} // namespace detail
/*
Return a compiletime initialized matrix (`std::array` of std::array`)
of `Cols` columns by `Rows` rows. Ascending elements from [0,0]
in row-first order are populated with successive values of the
constexpr function `IntType Step(IntType i)` for `i` in
`[Start + 0,Start + (Rows * Cols))`
*/
template<
typename IntType, std::size_t Cols, std::size_t Rows,
IntType(*Step)(IntType), IntType Start
>
constexpr auto make_integer_matrix()
{
return detail::make_integer_matrix<IntType,Cols,Step,Start>(
std::make_index_sequence<Rows>());
}
/*
Return a compiletime initialized matrix (`std::array` of std::array`)
of `Cols` columns by `sizeof...(Starts)` rows. Successive rows are populated
with successive values of the constexpr function `IntType Step(IntType i)`
for `i` in `[start + 0,start + Cols)`, for `start` successively in `...Starts`.
*/
template<typename IntType, std::size_t Cols, IntType(*Step)(IntType), IntType ...Starts>
constexpr auto make_integer_matrix()
{
return std::array<std::array<IntType,Cols>,sizeof...(Starts)>
{{detail::make_integer_array<IntType,Step,Starts,Cols>()...}};
}
您可以通过附加以下内容来制作演示程序:
#include <iostream>
using namespace std;
template<typename IntType>
constexpr auto times_3(IntType i)
{
return i * 3;
}
static constexpr auto m4x6 = make_integer_matrix<int,4,6,×_3<int>,4>();
static constexpr auto m5x1 = make_integer_matrix<int,5,×_3<int>,7>();
static constexpr auto m6x5 = make_integer_matrix<int,6,×_3<int>,11,13,17,19,23>();
static_assert(m4x6[0][0] == 12,"");
int main()
{
cout << "A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4" << endl;
for (auto const & ar : m4x6) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
cout << endl;
cout << "A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>"
<< endl;
for (auto const & ar : m6x5) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
cout << endl;
cout << "A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>"
<< endl;
for (auto const & ar : m5x1) {
for (auto const & i : ar) {
cout << i << ' ';
}
cout << endl;
}
return 0;
}
应该输出:
A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4
12 15 18 21
24 27 30 33
36 39 42 45
48 51 54 57
60 63 66 69
72 75 78 81
A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>
33 36 39 42 45 48
39 42 45 48 51 54
51 54 57 60 63 66
57 60 63 66 69 72
69 72 75 78 81 84
A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>
21 24 27 30 33
看到了at ideone
感兴趣