equal_range.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. //===----------------------------------------------------------------------===//
  13. //
  14. // The LLVM Compiler Infrastructure
  15. //
  16. // This file is dual licensed under the MIT and the University of Illinois Open
  17. // Source Licenses. See LICENSE.TXT for details.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include <vector>
  21. #include <iterator>
  22. #include <range/v3/core.hpp>
  23. #include <range/v3/algorithm/copy.hpp>
  24. #include <range/v3/algorithm/equal_range.hpp>
  25. #include <range/v3/functional/identity.hpp>
  26. #include <range/v3/functional/invoke.hpp>
  27. #include <range/v3/iterator/operations.hpp>
  28. #include <range/v3/iterator/insert_iterators.hpp>
  29. #include <range/v3/view/iota.hpp>
  30. #include <range/v3/view/join.hpp>
  31. #include <range/v3/view/repeat_n.hpp>
  32. #include <range/v3/view/take.hpp>
  33. #include <range/v3/view/transform.hpp>
  34. #include "../array.hpp"
  35. #include "../simple_test.hpp"
  36. #include "../test_iterators.hpp"
  37. struct my_int
  38. {
  39. int value;
  40. };
  41. bool compare(my_int lhs, my_int rhs)
  42. {
  43. return lhs.value < rhs.value;
  44. }
  45. void not_totally_ordered()
  46. {
  47. // This better compile!
  48. std::vector<my_int> vec;
  49. ranges::equal_range(vec, my_int{10}, compare);
  50. }
  51. template<class Iter, class Sent, class T, class Proj = ranges::identity>
  52. void
  53. test_it(Iter first, Sent last, const T& value, Proj proj = Proj{})
  54. {
  55. ranges::subrange<Iter, Iter> i =
  56. ranges::equal_range(first, last, value, ranges::less{}, proj);
  57. for (Iter j = first; j != i.begin(); ++j)
  58. CHECK(ranges::invoke(proj, *j) < value);
  59. for (Iter j = i.begin(); j != last; ++j)
  60. CHECK(!(ranges::invoke(proj, *j) < value));
  61. for (Iter j = first; j != i.end(); ++j)
  62. CHECK(!(value < ranges::invoke(proj, *j)));
  63. for (Iter j = i.end(); j != last; ++j)
  64. CHECK(value < ranges::invoke(proj, *j));
  65. auto res = ranges::equal_range(
  66. ranges::make_subrange(first, last), value, ranges::less{}, proj);
  67. for (Iter j = first; j != res.begin(); ++j)
  68. CHECK(ranges::invoke(proj, *j) < value);
  69. for (Iter j = res.begin(); j != last; ++j)
  70. CHECK(!(ranges::invoke(proj, *j) < value));
  71. for (Iter j = first; j != res.end(); ++j)
  72. CHECK(!(value < ranges::invoke(proj, *j)));
  73. for (Iter j = res.end(); j != last; ++j)
  74. CHECK(value < ranges::invoke(proj, *j));
  75. }
  76. template<class Iter, class Sent = Iter>
  77. void
  78. test_it()
  79. {
  80. using namespace ranges::views;
  81. static constexpr unsigned M = 10;
  82. std::vector<int> v;
  83. auto input = ints | take(100) | transform([](int i){return repeat_n(i,M);}) | join;
  84. ranges::copy(input, ranges::back_inserter(v));
  85. for (int x = 0; x <= (int)M; ++x)
  86. test_it(Iter(v.data()), Sent(v.data()+v.size()), x);
  87. }
  88. template<class Iter, class Sent, class T>
  89. constexpr bool test_constexpr(Iter first, Sent last, const T & value)
  90. {
  91. bool result = true;
  92. const auto i = ranges::equal_range(first, last, value);
  93. for(Iter j = first; j != i.begin(); ++j)
  94. {
  95. STATIC_CHECK_RETURN(*j < value);
  96. }
  97. for(Iter j = i.begin(); j != last; ++j)
  98. {
  99. STATIC_CHECK_RETURN(!(*j < value));
  100. }
  101. for(Iter j = first; j != i.end(); ++j)
  102. {
  103. STATIC_CHECK_RETURN(!(value < *j));
  104. }
  105. for(Iter j = i.end(); j != last; ++j)
  106. {
  107. STATIC_CHECK_RETURN(value < *j);
  108. }
  109. const auto res = ranges::equal_range(ranges::make_subrange(first, last), value);
  110. for(Iter j = first; j != res.begin(); ++j)
  111. {
  112. STATIC_CHECK_RETURN(*j < value);
  113. }
  114. for(Iter j = res.begin(); j != last; ++j)
  115. {
  116. STATIC_CHECK_RETURN(!(*j < value));
  117. }
  118. for(Iter j = first; j != res.end(); ++j)
  119. {
  120. STATIC_CHECK_RETURN(!(value < *j));
  121. }
  122. for(Iter j = res.end(); j != last; ++j)
  123. {
  124. STATIC_CHECK_RETURN(value < *j);
  125. }
  126. return result;
  127. }
  128. template<class Iter, class Sent = Iter>
  129. constexpr bool test_constexpr()
  130. {
  131. constexpr unsigned M = 10;
  132. constexpr unsigned N = 10;
  133. test::array<int, N * M> v{{0}};
  134. for(unsigned i = 0; i < N; ++i)
  135. {
  136. for(unsigned j = 0; j < M; ++j)
  137. {
  138. v[i * M + j] = (int)i;
  139. }
  140. }
  141. for(int x = 0; x <= (int)M; ++x)
  142. {
  143. STATIC_CHECK_RETURN(test_constexpr(Iter(v.data()), Sent(v.data() + v.size()), x));
  144. }
  145. return true;
  146. }
  147. constexpr bool test_constexpr_some()
  148. {
  149. constexpr int d[] = {0, 1, 2, 3};
  150. for(auto e = ranges::begin(d); e < ranges::end(d); ++e)
  151. {
  152. for(int x = -1; x <= 4; ++x)
  153. {
  154. STATIC_CHECK_RETURN(test_constexpr(d, e, x));
  155. }
  156. }
  157. return true;
  158. }
  159. int main()
  160. {
  161. int d[] = {0, 1, 2, 3};
  162. for (int* e = d; e <= d+4; ++e)
  163. for (int x = -1; x <= 4; ++x)
  164. test_it(d, e, x);
  165. test_it<ForwardIterator<const int*> >();
  166. test_it<BidirectionalIterator<const int*> >();
  167. test_it<RandomAccessIterator<const int*> >();
  168. test_it<const int*>();
  169. test_it<ForwardIterator<const int*>, Sentinel<const int*> >();
  170. test_it<BidirectionalIterator<const int*>, Sentinel<const int*> >();
  171. test_it<RandomAccessIterator<const int*>, Sentinel<const int*> >();
  172. {
  173. struct foo { int i; };
  174. foo some_foos[] = {{1}, {2}, {4}};
  175. test_it(some_foos, some_foos + 3, 2, &foo::i);
  176. }
  177. {
  178. STATIC_CHECK(test_constexpr_some());
  179. STATIC_CHECK(test_constexpr<ForwardIterator<const int *>>());
  180. STATIC_CHECK(test_constexpr<BidirectionalIterator<const int *>>());
  181. STATIC_CHECK(test_constexpr<RandomAccessIterator<const int *>>());
  182. STATIC_CHECK(test_constexpr<const int *>());
  183. STATIC_CHECK(test_constexpr<ForwardIterator<const int *>, Sentinel<const int *>>());
  184. STATIC_CHECK(test_constexpr<BidirectionalIterator<const int *>, Sentinel<const int *>>());
  185. STATIC_CHECK(test_constexpr<RandomAccessIterator<const int *>, Sentinel<const int *>>());
  186. }
  187. return ::test_result();
  188. }