next_permutation.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. //
  12. // Copyright 2005 - 2007 Adobe Systems Incorporated
  13. // Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
  14. // or a copy at http://stlab.adobe.com/licenses.html)
  15. //===----------------------------------------------------------------------===//
  16. //
  17. // The LLVM Compiler Infrastructure
  18. //
  19. // This file is dual licensed under the MIT and the University of Illinois Open
  20. // Source Licenses. See LICENSE.TXT for details.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include <cstring>
  24. #include <utility>
  25. #include <algorithm>
  26. #include <range/v3/core.hpp>
  27. #include <range/v3/algorithm/permutation.hpp>
  28. #include <range/v3/algorithm/equal.hpp>
  29. #include "../simple_test.hpp"
  30. #include "../test_utils.hpp"
  31. #include "../test_iterators.hpp"
  32. int factorial(int x)
  33. {
  34. int r = 1;
  35. for (; x; --x)
  36. r *= x;
  37. return r;
  38. }
  39. template<typename Iter, typename Sent = Iter>
  40. void test_iter()
  41. {
  42. int ia[] = {1, 2, 3, 4, 5, 6};
  43. const int sa = sizeof(ia)/sizeof(ia[0]);
  44. int prev[sa];
  45. for (int e = 0; e <= sa; ++e)
  46. {
  47. int count = 0;
  48. bool x;
  49. do
  50. {
  51. std::copy(ia, ia+e, prev);
  52. x = ranges::next_permutation(Iter(ia), Sent(ia+e));
  53. if(e > 1)
  54. {
  55. if(x)
  56. CHECK(std::lexicographical_compare(prev, prev+e, ia, ia+e));
  57. else
  58. CHECK(std::lexicographical_compare(ia, ia+e, prev, prev+e));
  59. }
  60. ++count;
  61. } while(x);
  62. CHECK(count == factorial(e));
  63. }
  64. }
  65. template<typename Iter, typename Sent = Iter>
  66. void test_range()
  67. {
  68. int ia[] = {1, 2, 3, 4, 5, 6};
  69. const int sa = sizeof(ia)/sizeof(ia[0]);
  70. int prev[sa];
  71. for (int e = 0; e <= sa; ++e)
  72. {
  73. int count = 0;
  74. bool x;
  75. do
  76. {
  77. std::copy(ia, ia+e, prev);
  78. x = ranges::next_permutation(ranges::make_subrange(Iter(ia), Sent(ia+e)));
  79. if(e > 1)
  80. {
  81. if(x)
  82. CHECK(std::lexicographical_compare(prev, prev+e, ia, ia+e));
  83. else
  84. CHECK(std::lexicographical_compare(ia, ia+e, prev, prev+e));
  85. }
  86. ++count;
  87. } while(x);
  88. CHECK(count == factorial(e));
  89. }
  90. }
  91. template<typename Iter, typename Sent = Iter>
  92. void test_iter_comp()
  93. {
  94. typedef std::greater<int> C;
  95. int ia[] = {6, 5, 4, 3, 2, 1};
  96. const int sa = sizeof(ia)/sizeof(ia[0]);
  97. int prev[sa];
  98. for(int e = 0; e <= sa; ++e)
  99. {
  100. int count = 0;
  101. bool x;
  102. do
  103. {
  104. std::copy(ia, ia+e, prev);
  105. x = ranges::next_permutation(Iter(ia), Sent(ia+e), C());
  106. if(e > 1)
  107. {
  108. if (x)
  109. CHECK(std::lexicographical_compare(prev, prev+e, ia, ia+e, C()));
  110. else
  111. CHECK(std::lexicographical_compare(ia, ia+e, prev, prev+e, C()));
  112. }
  113. ++count;
  114. } while (x);
  115. CHECK(count == factorial(e));
  116. }
  117. }
  118. template<typename Iter, typename Sent = Iter>
  119. void test_range_comp()
  120. {
  121. typedef std::greater<int> C;
  122. int ia[] = {6, 5, 4, 3, 2, 1};
  123. const int sa = sizeof(ia)/sizeof(ia[0]);
  124. int prev[sa];
  125. for(int e = 0; e <= sa; ++e)
  126. {
  127. int count = 0;
  128. bool x;
  129. do
  130. {
  131. std::copy(ia, ia+e, prev);
  132. x = ranges::next_permutation(ranges::make_subrange(Iter(ia), Sent(ia+e)), C());
  133. if(e > 1)
  134. {
  135. if (x)
  136. CHECK(std::lexicographical_compare(prev, prev+e, ia, ia+e, C()));
  137. else
  138. CHECK(std::lexicographical_compare(ia, ia+e, prev, prev+e, C()));
  139. }
  140. ++count;
  141. } while (x);
  142. CHECK(count == factorial(e));
  143. }
  144. }
  145. struct c_str
  146. {
  147. char const * value;
  148. friend bool operator==(c_str a, c_str b)
  149. {
  150. return 0 == std::strcmp(a.value, b.value);
  151. }
  152. friend bool operator!=(c_str a, c_str b)
  153. {
  154. return !(a == b);
  155. }
  156. };
  157. // For debugging the projection test
  158. std::ostream &operator<<(std::ostream& sout, std::pair<int, c_str> p)
  159. {
  160. return sout << "{" << p.first << "," << p.second.value << "}";
  161. }
  162. constexpr bool test_constexpr()
  163. {
  164. using namespace ranges;
  165. int ia[] = {6, 5, 4, 3, 2, 1};
  166. using IL = std::initializer_list<int>;
  167. next_permutation(ia, greater{});
  168. STATIC_CHECK_RETURN(equal(ia, IL{6, 5, 4, 3, 1, 2}));
  169. next_permutation(ia, greater{});
  170. STATIC_CHECK_RETURN(equal(ia, IL{6, 5, 4, 2, 3, 1}));
  171. next_permutation(ia, greater{});
  172. STATIC_CHECK_RETURN(equal(ia, IL{6, 5, 4, 2, 1, 3}));
  173. return true;
  174. }
  175. int main()
  176. {
  177. test_iter<BidirectionalIterator<int*> >();
  178. test_iter<RandomAccessIterator<int*> >();
  179. test_iter<int*>();
  180. test_iter<BidirectionalIterator<int*>, Sentinel<int*> >();
  181. test_iter<RandomAccessIterator<int*>, Sentinel<int*> >();
  182. test_iter_comp<BidirectionalIterator<int*> >();
  183. test_iter_comp<RandomAccessIterator<int*> >();
  184. test_iter_comp<int*>();
  185. test_iter_comp<BidirectionalIterator<int*>, Sentinel<int*> >();
  186. test_iter_comp<RandomAccessIterator<int*>, Sentinel<int*> >();
  187. test_range<BidirectionalIterator<int*> >();
  188. test_range<RandomAccessIterator<int*> >();
  189. test_range<int*>();
  190. test_range<BidirectionalIterator<int*>, Sentinel<int*> >();
  191. test_range<RandomAccessIterator<int*>, Sentinel<int*> >();
  192. test_range_comp<BidirectionalIterator<int*> >();
  193. test_range_comp<RandomAccessIterator<int*> >();
  194. test_range_comp<int*>();
  195. test_range_comp<BidirectionalIterator<int*>, Sentinel<int*> >();
  196. test_range_comp<RandomAccessIterator<int*>, Sentinel<int*> >();
  197. // Test projection
  198. using C = std::greater<int>;
  199. using I = std::initializer_list<std::pair<int, c_str>>;
  200. std::pair<int, c_str> ia[] = {{6, {"six"}}, {5,{"five"}}, {4,{"four"}}, {3,{"three"}}, {2,{"two"}}, {1,{"one"}}};
  201. CHECK(ranges::next_permutation(ia, C(), &std::pair<int,c_str>::first));
  202. ::check_equal(ia, I{{6, {"six"}}, {5,{"five"}}, {4,{"four"}}, {3,{"three"}}, {1,{"one"}}, {2,{"two"}}});
  203. CHECK(ranges::next_permutation(ia, C(), &std::pair<int,c_str>::first));
  204. ::check_equal(ia, I{{6, {"six"}}, {5,{"five"}}, {4,{"four"}}, {2,{"two"}}, {3,{"three"}}, {1,{"one"}}});
  205. CHECK(ranges::next_permutation(ia, C(), &std::pair<int,c_str>::first));
  206. ::check_equal(ia, I{{6, {"six"}}, {5,{"five"}}, {4,{"four"}}, {2,{"two"}}, {1,{"one"}}, {3,{"three"}}});
  207. // etc..
  208. {
  209. STATIC_CHECK(test_constexpr());
  210. }
  211. return ::test_result();
  212. }