Guest User

Untitled

a guest
Nov 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. template<int I, int J, int N>
  2. struct MagicNumber
  3. {
  4. public:
  5. static const int value = 1 + ((I - J + (N - 1) / 2) % N) * N + ((I + J +
  6. (N + 1) / 2) % N);
  7. };
  8.  
  9. #include <boost/mpl/vector_c.hpp>
  10. #include <boost/mpl/vector.hpp>
  11. #include <boost/mpl/range_c.hpp>
  12. #include <boost/mpl/transform.hpp>
  13. #include <boost/mpl/copy.hpp>
  14. #include <boost/mpl/at.hpp>
  15.  
  16. template <class T>
  17. struct check_type; // Для проверки значения типа во время компиляции
  18.  
  19. // Исходный заполнитель
  20. template<int I, int J, int N>
  21. struct magic_number_t
  22. : boost::mpl::int_<1 + ((I - J + (N - 1) / 2) % N) * N + ((I + J + (N + 1) / 2) % N)>
  23. { };
  24.  
  25. // Заполнитель строки (для строки известно I и N, переменная - J; I,J,N передаются как mpl::int_
  26. template<class I, class N>
  27. struct element_in_row_mfn{
  28. template<class J>
  29. struct apply: magic_number_t<I::value, J::value, N::value> {};
  30. };
  31.  
  32. // Генерирует I-ю строку, N - постоянная
  33. template<std::size_t N>
  34. struct make_row_mfn {
  35. template<class I> struct apply{
  36. typedef boost::mpl::range_c<int, 0 , N> col_range_;
  37.  
  38. // результат метафункции
  39. typedef typename boost::mpl::transform<col_range_, // Источник 0, 1, ...N-1
  40. element_in_row_mfn<I, boost::mpl::int_<N> >, // Метафункция
  41. boost::mpl::back_inserter<boost::mpl::vector<> > // Потребитель
  42. >::type type;
  43. };
  44. };
  45.  
  46. enum {N = 3}; // input
  47.  
  48. // Применение метофункции make_columns<N> для каждого заголовка строки генерирует столбец
  49. typedef typename boost::mpl::transform<
  50. boost::mpl::range_c <int, 0, N>, // Источник 0, 1, ...N-1
  51. make_row_mfn<N>, // Метафункция
  52. boost::mpl::back_inserter<boost::mpl::vector<> > // Потребитель
  53. >::type matrix; // Magic matrix
  54.  
  55. // это уже с++11
  56. template <int i, class row>
  57. using row_at = typename boost::mpl::at<row, boost::mpl::int_<i>>::type;
  58.  
  59. template <int i, int j, class Matrix>
  60. using matrix_at = row_at<j, row_at<i, Matrix>>;
  61.  
  62. int main()
  63. {
  64. // Вывод результата в виде сообщений об ошибке. Можно воспользоваться stsatic_assert вместо вывода
  65. check_type<matrix_at<0, 0, matrix > > v00;
  66. check_type<matrix_at<0, 1, matrix > > v01;
  67. check_type<matrix_at<0, 2, matrix > > v02;
  68.  
  69. check_type<matrix_at<1, 0, matrix > > v10;
  70. check_type<matrix_at<1, 1, matrix > > v11;
  71. check_type<matrix_at<1, 2, matrix > > v12;
  72.  
  73. check_type<matrix_at<2, 0, matrix > > v20;
  74. check_type<matrix_at<2, 1, matrix > > v21;
  75. check_type<matrix_at<2, 2, matrix > > v22;
  76.  
  77. return 0;
  78. }
  79.  
  80. main.cpp: In function 'int main()':
  81.  
  82. main.cpp:55:43: error: aggregate 'check_type<mpl_::int_<6> > v00' has incomplete type and cannot be defined
  83.  
  84. check_type<matrix_at<0, 0, matrix > > v00;
  85.  
  86. ^~~
  87.  
  88. main.cpp:56:43: error: aggregate 'check_type<mpl_::int_<1> > v01' has incomplete type and cannot be defined
  89.  
  90. check_type<matrix_at<0, 1, matrix > > v01;
  91.  
  92. ^~~
  93.  
  94. main.cpp:57:43: error: aggregate 'check_type<mpl_::int_<-1> > v02' has incomplete type and cannot be defined
  95.  
  96. check_type<matrix_at<0, 2, matrix > > v02;
  97.  
  98. ^~~
  99.  
  100. main.cpp:59:43: error: aggregate 'check_type<mpl_::int_<7> > v10' has incomplete type and cannot be defined
  101.  
  102. check_type<matrix_at<1, 0, matrix > > v10;
  103.  
  104. ^~~
  105.  
  106. main.cpp:60:43: error: aggregate 'check_type<mpl_::int_<5> > v11' has incomplete type and cannot be defined
  107.  
  108. check_type<matrix_at<1, 1, matrix > > v11;
  109.  
  110. ^~~
  111.  
  112. main.cpp:61:43: error: aggregate 'check_type<mpl_::int_<3> > v12' has incomplete type and cannot be defined
  113.  
  114. check_type<matrix_at<1, 2, matrix > > v12;
  115.  
  116. ^~~
  117.  
  118. main.cpp:63:43: error: aggregate 'check_type<mpl_::int_<2> > v20' has incomplete type and cannot be defined
  119.  
  120. check_type<matrix_at<2, 0, matrix > > v20;
  121.  
  122. ^~~
  123.  
  124. main.cpp:64:43: error: aggregate 'check_type<mpl_::int_<9> > v21' has incomplete type and cannot be defined
  125.  
  126. check_type<matrix_at<2, 1, matrix > > v21;
  127.  
  128. ^~~
  129.  
  130. main.cpp:65:43: error: aggregate 'check_type<mpl_::int_<4> > v22' has incomplete type and cannot be defined
  131.  
  132. check_type<matrix_at<2, 2, matrix > > v22;
Add Comment
Please, Sign In to add comment