fork(1) download
  1. #include <iostream>
  2.  
  3. #pragma mark - Tape
  4.  
  5. constexpr int Blank = -1;
  6.  
  7. template<int... xs>
  8. class Tape {
  9. public:
  10. using type = Tape<xs...>;
  11. constexpr static int length = sizeof...(xs);
  12. };
  13.  
  14. #pragma mark - Print
  15.  
  16. template<class T>
  17. void print(T);
  18.  
  19. template<>
  20. void print(Tape<>) {
  21. std::cout << std::endl;
  22. }
  23.  
  24. template<int x, int... xs>
  25. void print(Tape<x, xs...>) {
  26. if (x == Blank) {
  27. std::cout << "_ ";
  28. } else {
  29. std::cout << x << " ";
  30. }
  31. print(Tape<xs...>());
  32. }
  33.  
  34. #pragma mark - Concatenate
  35.  
  36. template<class, class>
  37. class Concatenate;
  38.  
  39. template<int... xs, int... ys>
  40. class Concatenate<Tape<xs...>, Tape<ys...>> {
  41. public:
  42. using type = Tape<xs..., ys...>;
  43. };
  44.  
  45. #pragma mark - Invert
  46.  
  47. template<class>
  48. class Invert;
  49.  
  50. template<>
  51. class Invert<Tape<>> {
  52. public:
  53. using type = Tape<>;
  54. };
  55.  
  56. template<int x, int... xs>
  57. class Invert<Tape<x, xs...>> {
  58. public:
  59. using type = typename Concatenate<
  60. typename Invert<Tape<xs...>>::type,
  61. Tape<x>
  62. >::type;
  63. };
  64.  
  65. #pragma mark - Read
  66.  
  67. template<int, class>
  68. class Read;
  69.  
  70. template<int n, int x, int... xs>
  71. class Read<n, Tape<x, xs...>> {
  72. public:
  73. using type = typename std::conditional<
  74. (n == 0),
  75. std::integral_constant<int, x>,
  76. Read<n - 1, Tape<xs...>>
  77. >::type::type;
  78. };
  79.  
  80. #pragma mark - N first and N last
  81.  
  82. template<int, class>
  83. class NLast;
  84.  
  85. template<int n, int x, int... xs>
  86. class NLast<n, Tape<x, xs...>> {
  87. public:
  88. using type = typename std::conditional<
  89. (n == sizeof...(xs)),
  90. Tape<xs...>,
  91. NLast<n, Tape<xs...>>
  92. >::type::type;
  93. };
  94.  
  95. template<int, class>
  96. class NFirst;
  97.  
  98. template<int n, int... xs>
  99. class NFirst<n, Tape<xs...>> {
  100. public:
  101. using type = typename Invert<
  102. typename NLast<
  103. n, typename Invert<Tape<xs...>>::type
  104. >::type
  105. >::type;
  106. };
  107.  
  108. #pragma mark - Write
  109.  
  110. template<int, int, class>
  111. class Write;
  112.  
  113. template<int pos, int x, int... xs>
  114. class Write<pos, x, Tape<xs...>> {
  115. public:
  116. using type = typename Concatenate<
  117. typename Concatenate<
  118. typename NFirst<pos, Tape<xs...>>::type,
  119. Tape<x>
  120. >::type,
  121. typename NLast<(sizeof...(xs) - pos - 1), Tape<xs...>>::type
  122. >::type;
  123. };
  124.  
  125. #pragma mark - Move
  126.  
  127. template<int, class>
  128. class Hold;
  129.  
  130. template<int pos, int... xs>
  131. class Hold<pos, Tape<xs...>> {
  132. public:
  133. constexpr static int position = pos;
  134. using tape = Tape<xs...>;
  135. };
  136.  
  137. template<int, class>
  138. class Left;
  139.  
  140. template<int pos, int... xs>
  141. class Left<pos, Tape<xs...>> {
  142. public:
  143. constexpr static int position = typename std::conditional<
  144. (pos > 0),
  145. std::integral_constant<int, pos - 1>,
  146. std::integral_constant<int, 0>
  147. >::type();
  148.  
  149. using tape = typename std::conditional<
  150. (pos > 0),
  151. Tape<xs...>,
  152. Tape<Blank, xs...>
  153. >::type;
  154. };
  155.  
  156. template<int, class>
  157. class Right;
  158.  
  159. template<int pos, int... xs>
  160. class Right<pos, Tape<xs...>> {
  161. public:
  162. constexpr static int position = pos + 1;
  163.  
  164. using tape = typename std::conditional<
  165. (pos < sizeof...(xs) - 1),
  166. Tape<xs...>,
  167. Tape<xs..., Blank>
  168. >::type;
  169. };
  170.  
  171. int main() {
  172. using tape = Tape<1, 2, 3, 4, 5>;
  173.  
  174. std::cout << Left<3, tape>::position << std::endl;
  175. std::cout << Left<0, tape>::position << std::endl;
  176. print(Left<0, tape>::tape());
  177.  
  178. std::cout << Right<3, tape>::position << std::endl;
  179. std::cout << Right<4, tape>::position << std::endl;
  180. print(Right<4, tape>::tape());
  181.  
  182. return 0;
  183. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
2
0
_ 1 2 3 4 5 
4
5
1 2 3 4 5 _