fork download
  1. #include <array>
  2. #include <utility>
  3.  
  4. namespace detail {
  5.  
  6. template<typename IntType, IntType(*Step)(IntType), IntType Start, IntType ...Is>
  7. constexpr auto make_integer_array(std::integer_sequence<IntType,Is...>)
  8. {
  9. return std::array<IntType,sizeof...(Is)>{{Step(Start + Is)...}};
  10. }
  11.  
  12. template<typename IntType, IntType(*Step)(IntType), IntType Start, std::size_t Length>
  13. constexpr auto make_integer_array()
  14. {
  15. return make_integer_array<IntType,Step,Start>(
  16. std::make_integer_sequence<IntType,Length>());
  17. }
  18.  
  19.  
  20. template<
  21. typename IntType, std::size_t Cols,
  22. IntType(*Step)(IntType),IntType Start, std::size_t ...Rs
  23. >
  24. constexpr auto make_integer_matrix(std::index_sequence<Rs...>)
  25. {
  26. return std::array<std::array<IntType,Cols>,sizeof...(Rs)>
  27. {{make_integer_array<IntType,Step,Start + (Rs * Cols),Cols>()...}};
  28. }
  29.  
  30. } // namespace detail
  31.  
  32. /*
  33.   Return a compiletime initialized matrix (`std::array` of std::array`)
  34.   of `Cols` columns by `Rows` rows. Ascending elements from [0,0]
  35.   in row-first order are populated with successive values of the
  36.   constexpr function `IntType Step(IntType i)` for `i` in
  37.   `[Start + 0,Start + (Rows * Cols))`
  38. */
  39. template<
  40. typename IntType, std::size_t Cols, std::size_t Rows,
  41. IntType(*Step)(IntType), IntType Start
  42. >
  43. constexpr auto make_integer_matrix()
  44. {
  45. return detail::make_integer_matrix<IntType,Cols,Step,Start>(
  46. std::make_index_sequence<Rows>());
  47. }
  48.  
  49. /*
  50.   Return a compiletime initialized matrix (`std::array` of std::array`)
  51.   of `Cols` columns by `sizeof...(Starts)` rows. Successive rows are populated
  52.   with successive values of the constexpr function `IntType Step(IntType i)`
  53.   for `i` in `[start + 0,start + Cols)`, for `start` successively in `...Starts`.
  54. */
  55. template<typename IntType, std::size_t Cols, IntType(*Step)(IntType), IntType ...Starts>
  56. constexpr auto make_integer_matrix()
  57. {
  58. return std::array<std::array<IntType,Cols>,sizeof...(Starts)>
  59. {{detail::make_integer_array<IntType,Step,Starts,Cols>()...}};
  60. }
  61.  
  62. #include <iostream>
  63.  
  64. using namespace std;
  65.  
  66. template<typename IntType>
  67. constexpr auto times_3(IntType i)
  68. {
  69. return i * 3;
  70. }
  71.  
  72. static constexpr auto m4x6 = make_integer_matrix<int,4,6,&times_3<int>,4>();
  73. static constexpr auto m5x1 = make_integer_matrix<int,5,&times_3<int>,7>();
  74. static constexpr auto m6x5 = make_integer_matrix<int,6,&times_3<int>,11,13,17,19,23>();
  75. static_assert(m4x6[0][0] == 12,"");
  76.  
  77. int main()
  78. {
  79. cout << "A 4 x 6 matrix that wraps around in steps of `3i` from `i` = 4" << endl;
  80. for (auto const & ar : m4x6) {
  81. for (auto const & i : ar) {
  82. cout << i << ' ';
  83. }
  84. cout << endl;
  85. }
  86. cout << endl;
  87. cout << "A 6 x 5 matrix with rows of `3i` for initial `i` in <11,13,17,19,23>"
  88. << endl;
  89. for (auto const & ar : m6x5) {
  90. for (auto const & i : ar) {
  91. cout << i << ' ';
  92. }
  93. cout << endl;
  94. }
  95. cout << endl;
  96. cout << "A 5 x 1 matrix with rows of of ` 3i` for initial `i` in <7>"
  97. << endl;
  98. for (auto const & ar : m5x1) {
  99. for (auto const & i : ar) {
  100. cout << i << ' ';
  101. }
  102. cout << endl;
  103. }
  104.  
  105. return 0;
  106. }
  107.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
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