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. int main() {
  126. using tape = Tape<1, 2, 3, 4, 5>;
  127. print(Write<0, 2, tape>::type());
  128. print(Write<2, 4, tape>::type());
  129. print(Write<4, 8, tape>::type());
  130. return 0;
  131. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
2 2 3 4 5 
1 2 4 4 5 
1 2 3 4 8