fork download
  1. #include <algorithm>
  2. #include <utility>
  3. #include <iostream>
  4.  
  5. struct my_cmp
  6. {
  7. bool operator()(unsigned char a, unsigned char b)
  8. {
  9. return a < b;
  10. }
  11. };
  12.  
  13. struct my_cmp2
  14. {
  15. bool operator()(unsigned char a, unsigned char b)
  16. {
  17. std::cerr << (int) a << ' ' << (int) b << '\n';
  18. return a < b;
  19. }
  20. };
  21.  
  22. template<unsigned int Len>
  23. struct reverse_if
  24. {
  25. template<typename T, typename P>
  26. void operator()(T* a, P pred)
  27. {
  28. using std::swap;
  29. if (pred(*(a + Len - 1), *a)) swap(*a, *(a + Len - 1));
  30. reverse_if<Len - 2> rif;
  31. rif(a + 1, pred);
  32. }
  33. };
  34.  
  35. template<>
  36. struct reverse_if<0>
  37. {
  38. template<typename T, typename P>
  39. void operator()(T* a, P pred) {}
  40. };
  41.  
  42.  
  43. template<unsigned int Len, unsigned int Count>
  44. struct shift_swap_if_help
  45. {
  46. template<typename T, typename P>
  47. void operator()(T* a, P pred)
  48. {
  49. using std::swap;
  50. if (pred(*(a + Len), *a)) swap(*a, *(a + Len));
  51. shift_swap_if_help<Len, Count - 1> ssif;
  52. ssif(a + 1, pred);
  53. }
  54. };
  55.  
  56. template<unsigned int Len>
  57. struct shift_swap_if_help<Len, 0>
  58. {
  59. template<typename T, typename P>
  60. void operator()(T* a, P Pred) {}
  61. };
  62.  
  63. template<unsigned int Len>
  64. struct shift_swap_if
  65. {
  66. template<typename T, typename P>
  67. void operator()(T* a, P pred)
  68. {
  69. shift_swap_if_help<Len, Len> ssif;
  70. ssif(a, pred);
  71. }
  72. };
  73.  
  74. template<unsigned int Len>
  75. struct shift_chain
  76. {
  77. template<typename T, typename P>
  78. void operator()(T* a, P pred)
  79. {
  80. shift_swap_if<Len/2> ssif;
  81. ssif(a, pred);
  82.  
  83. shift_chain<Len / 2> upper;
  84. shift_chain<Len / 2> lower;
  85. upper(a, pred);
  86. lower(a + Len/2, pred);
  87. }
  88. };
  89.  
  90. template<>
  91. struct shift_chain<0>
  92. {
  93. template<typename T, typename P>
  94. void operator()(T* a, P pred) {}
  95. };
  96.  
  97. template<unsigned int Len>
  98. struct block
  99. {
  100. template<typename T, typename P>
  101. void operator()(T* a, P pred)
  102. {
  103. reverse_if<Len> rif;
  104. rif(a, pred);
  105.  
  106. shift_chain<Len / 2> upper;
  107. shift_chain<Len / 2> lower;
  108. upper(a, pred);
  109. lower(a + Len/2, pred);
  110. }
  111. };
  112.  
  113. template<unsigned int Depth>
  114. struct bitonic_sort
  115. {
  116.  
  117. template<typename T, typename P>
  118. void operator()(T* a, P pred)
  119. {
  120. bitonic_sort<Depth / 2> upper;
  121. bitonic_sort<Depth / 2> lower;
  122. upper(a, pred);
  123. lower(a + (Depth / 2), pred);
  124.  
  125. block<Depth> blk;
  126. blk(a, pred);
  127. }
  128. };
  129.  
  130. template<>
  131. struct bitonic_sort<1>
  132. {
  133.  
  134. template<typename T, typename P>
  135. void operator()(T* a, P pred) {}
  136. };
  137.  
  138. template<unsigned int Len>
  139. void test_bitonic(unsigned int idx)
  140. {
  141. unsigned char a[Len];
  142. for (auto& e : a) e = 0;
  143. a[idx] = 1;
  144.  
  145. for (auto e : a) std::cerr << (int) e << ' ';
  146. std::cerr << " -> ";
  147.  
  148. bitonic_sort<Len> bsort;
  149. bsort(a, my_cmp{});
  150.  
  151. for (auto e : a) std::cerr << (int) e << ' ';
  152. std::cerr << '\n';
  153. }
  154.  
  155. template<unsigned int Len>
  156. void test_bitonic_index()
  157. {
  158. unsigned char a[Len];
  159. for (unsigned int i = 0; i < Len; ++i)
  160. a[i] = i;
  161.  
  162. for (auto e : a) std::cerr << (int) e << ' ';
  163. std::cerr << " -> ";
  164.  
  165. bitonic_sort<Len> bsort;
  166. bsort(a, my_cmp2{});
  167.  
  168. for (auto e : a) std::cerr << (int) e << ' ';
  169. std::cerr << '\n';
  170. }
  171.  
  172.  
  173.  
  174. int main()
  175. {
  176. //test_bitonic<4>(2);
  177. //std::cerr << '\n';
  178. //test_bitonic_index<4>();
  179.  
  180. test_bitonic<1>(0);
  181.  
  182. std::cerr << '\n';
  183.  
  184. test_bitonic<2>(0);
  185. test_bitonic<2>(1);
  186.  
  187. std::cerr << '\n';
  188.  
  189. for(unsigned int i = 0; i < 4; ++i)
  190. test_bitonic<4>(i);
  191. std::cerr << '\n';
  192.  
  193. for(unsigned int i = 0; i < 8; ++i)
  194. test_bitonic<8>(i);
  195. std::cerr << '\n';
  196.  
  197. for(unsigned int i = 0; i < 16; ++i)
  198. test_bitonic<16>(i);
  199. std::cerr << '\n';
  200.  
  201. return 0;
  202. }
Success #stdin #stdout #stderr 0s 3296KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
1  ->  1 

1 0  ->  0 1 
0 1  ->  0 1 

1 0 0 0  ->  0 0 0 1 
0 1 0 0  ->  0 0 0 1 
0 0 1 0  ->  0 0 0 1 
0 0 0 1  ->  0 0 0 1 

1 0 0 0 0 0 0 0  ->  0 0 0 0 0 0 0 1 
0 1 0 0 0 0 0 0  ->  0 0 0 0 0 0 0 1 
0 0 1 0 0 0 0 0  ->  0 0 0 0 0 0 0 1 
0 0 0 1 0 0 0 0  ->  0 0 0 0 0 0 0 1 
0 0 0 0 1 0 0 0  ->  0 0 0 0 0 0 0 1 
0 0 0 0 0 1 0 0  ->  0 0 0 0 0 0 0 1 
0 0 0 0 0 0 1 0  ->  0 0 0 0 0 0 0 1 
0 0 0 0 0 0 0 1  ->  0 0 0 0 0 0 0 1 

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0  ->  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0  ->  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0  ->  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0  ->  0 0 0 0 0 0 0 0 0 0 0 0 0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