set_union.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <algorithm>
  21. #include <functional>
  22. #include <vector>
  23. #include <range/v3/core.hpp>
  24. #include <range/v3/algorithm/fill.hpp>
  25. #include <range/v3/algorithm/set_algorithm.hpp>
  26. #include <range/v3/algorithm/lexicographical_compare.hpp>
  27. #include "../simple_test.hpp"
  28. #include "../test_utils.hpp"
  29. #include "../test_iterators.hpp"
  30. template<class Iter1, class Iter2, class OutIter>
  31. void
  32. test()
  33. {
  34. int ia[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
  35. static const int sa = sizeof(ia)/sizeof(ia[0]);
  36. int ib[] = {2, 4, 4, 6};
  37. static const int sb = sizeof(ib)/sizeof(ib[0]);
  38. int ic[20];
  39. int ir[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6};
  40. static const int sr = sizeof(ir)/sizeof(ir[0]);
  41. using R = ranges::set_union_result<Iter1, Iter2, OutIter>;
  42. auto set_union = make_testable_2(ranges::set_union);
  43. auto checker = [&](R res)
  44. {
  45. CHECK((base(res.out) - ic) == sr);
  46. CHECK(std::lexicographical_compare(ic, base(res.out), ir, ir+sr) == false);
  47. ranges::fill(ic, 0);
  48. };
  49. set_union(Iter1(ia), Iter1(ia+sa),
  50. Iter2(ib), Iter2(ib+sb), OutIter(ic)).check(checker);
  51. set_union(Iter1(ib), Iter1(ib+sb),
  52. Iter2(ia), Iter2(ia+sa), OutIter(ic)).check(checker);
  53. set_union(Iter1(ia), Iter1(ia+sa),
  54. Iter2(ib), Iter2(ib+sb), OutIter(ic), std::less<int>()).check(checker);
  55. set_union(Iter1(ib), Iter1(ib+sb),
  56. Iter2(ia), Iter2(ia+sa), OutIter(ic), std::less<int>()).check(checker);
  57. }
  58. struct S
  59. {
  60. int i;
  61. };
  62. struct T
  63. {
  64. int j;
  65. };
  66. struct U
  67. {
  68. int k;
  69. U& operator=(S s) { k = s.i; return *this;}
  70. U& operator=(T t) { k = t.j; return *this;}
  71. };
  72. constexpr bool test_constexpr()
  73. {
  74. using namespace ranges;
  75. int ia[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
  76. int ib[] = {2, 4, 4, 6};
  77. int ic[20] = {0};
  78. int ir[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6};
  79. const int sr = sizeof(ir) / sizeof(ir[0]);
  80. const auto res1 = set_union(ia, ib, ic, less{});
  81. STATIC_CHECK_RETURN(res1.in1 == end(ia));
  82. STATIC_CHECK_RETURN(res1.in2 == end(ib));
  83. STATIC_CHECK_RETURN((res1.out - begin(ic)) == sr);
  84. STATIC_CHECK_RETURN(lexicographical_compare(ic, res1.out, ir, ir + sr, less{}) == 0);
  85. fill(ic, 0);
  86. const auto res2 = set_union(ib, ia, ic, less{});
  87. STATIC_CHECK_RETURN(res2.in1 == end(ib));
  88. STATIC_CHECK_RETURN(res2.in2 == end(ia));
  89. STATIC_CHECK_RETURN(res2.out - begin(ic) == sr);
  90. STATIC_CHECK_RETURN(lexicographical_compare(ic, res2.out, ir, ir + sr, less{}) == 0);
  91. return true;
  92. }
  93. int main()
  94. {
  95. #ifdef SET_UNION_1
  96. test<InputIterator<const int*>, InputIterator<const int*>, OutputIterator<int*> >();
  97. test<InputIterator<const int*>, InputIterator<const int*>, ForwardIterator<int*> >();
  98. test<InputIterator<const int*>, InputIterator<const int*>, BidirectionalIterator<int*> >();
  99. test<InputIterator<const int*>, InputIterator<const int*>, RandomAccessIterator<int*> >();
  100. test<InputIterator<const int*>, InputIterator<const int*>, int*>();
  101. test<InputIterator<const int*>, ForwardIterator<const int*>, OutputIterator<int*> >();
  102. test<InputIterator<const int*>, ForwardIterator<const int*>, ForwardIterator<int*> >();
  103. test<InputIterator<const int*>, ForwardIterator<const int*>, BidirectionalIterator<int*> >();
  104. test<InputIterator<const int*>, ForwardIterator<const int*>, RandomAccessIterator<int*> >();
  105. test<InputIterator<const int*>, ForwardIterator<const int*>, int*>();
  106. test<InputIterator<const int*>, BidirectionalIterator<const int*>, OutputIterator<int*> >();
  107. test<InputIterator<const int*>, BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  108. test<InputIterator<const int*>, BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  109. test<InputIterator<const int*>, BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  110. test<InputIterator<const int*>, BidirectionalIterator<const int*>, int*>();
  111. test<InputIterator<const int*>, RandomAccessIterator<const int*>, OutputIterator<int*> >();
  112. test<InputIterator<const int*>, RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  113. test<InputIterator<const int*>, RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  114. test<InputIterator<const int*>, RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  115. test<InputIterator<const int*>, RandomAccessIterator<const int*>, int*>();
  116. test<InputIterator<const int*>, const int*, OutputIterator<int*> >();
  117. test<InputIterator<const int*>, const int*, ForwardIterator<int*> >();
  118. test<InputIterator<const int*>, const int*, BidirectionalIterator<int*> >();
  119. test<InputIterator<const int*>, const int*, RandomAccessIterator<int*> >();
  120. test<InputIterator<const int*>, const int*, int*>();
  121. #endif
  122. #ifdef SET_UNION_2
  123. test<ForwardIterator<const int*>, InputIterator<const int*>, OutputIterator<int*> >();
  124. test<ForwardIterator<const int*>, InputIterator<const int*>, ForwardIterator<int*> >();
  125. test<ForwardIterator<const int*>, InputIterator<const int*>, BidirectionalIterator<int*> >();
  126. test<ForwardIterator<const int*>, InputIterator<const int*>, RandomAccessIterator<int*> >();
  127. test<ForwardIterator<const int*>, InputIterator<const int*>, int*>();
  128. test<ForwardIterator<const int*>, ForwardIterator<const int*>, OutputIterator<int*> >();
  129. test<ForwardIterator<const int*>, ForwardIterator<const int*>, ForwardIterator<int*> >();
  130. test<ForwardIterator<const int*>, ForwardIterator<const int*>, BidirectionalIterator<int*> >();
  131. test<ForwardIterator<const int*>, ForwardIterator<const int*>, RandomAccessIterator<int*> >();
  132. test<ForwardIterator<const int*>, ForwardIterator<const int*>, int*>();
  133. test<ForwardIterator<const int*>, BidirectionalIterator<const int*>, OutputIterator<int*> >();
  134. test<ForwardIterator<const int*>, BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  135. test<ForwardIterator<const int*>, BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  136. test<ForwardIterator<const int*>, BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  137. test<ForwardIterator<const int*>, BidirectionalIterator<const int*>, int*>();
  138. test<ForwardIterator<const int*>, RandomAccessIterator<const int*>, OutputIterator<int*> >();
  139. test<ForwardIterator<const int*>, RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  140. test<ForwardIterator<const int*>, RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  141. test<ForwardIterator<const int*>, RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  142. test<ForwardIterator<const int*>, RandomAccessIterator<const int*>, int*>();
  143. test<ForwardIterator<const int*>, const int*, OutputIterator<int*> >();
  144. test<ForwardIterator<const int*>, const int*, ForwardIterator<int*> >();
  145. test<ForwardIterator<const int*>, const int*, BidirectionalIterator<int*> >();
  146. test<ForwardIterator<const int*>, const int*, RandomAccessIterator<int*> >();
  147. test<ForwardIterator<const int*>, const int*, int*>();
  148. #endif
  149. #ifdef SET_UNION_3
  150. test<BidirectionalIterator<const int*>, InputIterator<const int*>, OutputIterator<int*> >();
  151. test<BidirectionalIterator<const int*>, InputIterator<const int*>, ForwardIterator<int*> >();
  152. test<BidirectionalIterator<const int*>, InputIterator<const int*>, BidirectionalIterator<int*> >();
  153. test<BidirectionalIterator<const int*>, InputIterator<const int*>, RandomAccessIterator<int*> >();
  154. test<BidirectionalIterator<const int*>, InputIterator<const int*>, int*>();
  155. test<BidirectionalIterator<const int*>, ForwardIterator<const int*>, OutputIterator<int*> >();
  156. test<BidirectionalIterator<const int*>, ForwardIterator<const int*>, ForwardIterator<int*> >();
  157. test<BidirectionalIterator<const int*>, ForwardIterator<const int*>, BidirectionalIterator<int*> >();
  158. test<BidirectionalIterator<const int*>, ForwardIterator<const int*>, RandomAccessIterator<int*> >();
  159. test<BidirectionalIterator<const int*>, ForwardIterator<const int*>, int*>();
  160. test<BidirectionalIterator<const int*>, BidirectionalIterator<const int*>, OutputIterator<int*> >();
  161. test<BidirectionalIterator<const int*>, BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  162. test<BidirectionalIterator<const int*>, BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  163. test<BidirectionalIterator<const int*>, BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  164. test<BidirectionalIterator<const int*>, BidirectionalIterator<const int*>, int*>();
  165. test<BidirectionalIterator<const int*>, RandomAccessIterator<const int*>, OutputIterator<int*> >();
  166. test<BidirectionalIterator<const int*>, RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  167. test<BidirectionalIterator<const int*>, RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  168. test<BidirectionalIterator<const int*>, RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  169. test<BidirectionalIterator<const int*>, RandomAccessIterator<const int*>, int*>();
  170. test<BidirectionalIterator<const int*>, const int*, OutputIterator<int*> >();
  171. test<BidirectionalIterator<const int*>, const int*, ForwardIterator<int*> >();
  172. test<BidirectionalIterator<const int*>, const int*, BidirectionalIterator<int*> >();
  173. test<BidirectionalIterator<const int*>, const int*, RandomAccessIterator<int*> >();
  174. test<BidirectionalIterator<const int*>, const int*, int*>();
  175. #endif
  176. #ifdef SET_UNION_4
  177. test<RandomAccessIterator<const int*>, InputIterator<const int*>, OutputIterator<int*> >();
  178. test<RandomAccessIterator<const int*>, InputIterator<const int*>, ForwardIterator<int*> >();
  179. test<RandomAccessIterator<const int*>, InputIterator<const int*>, BidirectionalIterator<int*> >();
  180. test<RandomAccessIterator<const int*>, InputIterator<const int*>, RandomAccessIterator<int*> >();
  181. test<RandomAccessIterator<const int*>, InputIterator<const int*>, int*>();
  182. test<RandomAccessIterator<const int*>, ForwardIterator<const int*>, OutputIterator<int*> >();
  183. test<RandomAccessIterator<const int*>, ForwardIterator<const int*>, ForwardIterator<int*> >();
  184. test<RandomAccessIterator<const int*>, ForwardIterator<const int*>, BidirectionalIterator<int*> >();
  185. test<RandomAccessIterator<const int*>, ForwardIterator<const int*>, RandomAccessIterator<int*> >();
  186. test<RandomAccessIterator<const int*>, ForwardIterator<const int*>, int*>();
  187. test<RandomAccessIterator<const int*>, BidirectionalIterator<const int*>, OutputIterator<int*> >();
  188. test<RandomAccessIterator<const int*>, BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  189. test<RandomAccessIterator<const int*>, BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  190. test<RandomAccessIterator<const int*>, BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  191. test<RandomAccessIterator<const int*>, BidirectionalIterator<const int*>, int*>();
  192. test<RandomAccessIterator<const int*>, RandomAccessIterator<const int*>, OutputIterator<int*> >();
  193. test<RandomAccessIterator<const int*>, RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  194. test<RandomAccessIterator<const int*>, RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  195. test<RandomAccessIterator<const int*>, RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  196. test<RandomAccessIterator<const int*>, RandomAccessIterator<const int*>, int*>();
  197. test<RandomAccessIterator<const int*>, const int*, OutputIterator<int*> >();
  198. test<RandomAccessIterator<const int*>, const int*, ForwardIterator<int*> >();
  199. test<RandomAccessIterator<const int*>, const int*, BidirectionalIterator<int*> >();
  200. test<RandomAccessIterator<const int*>, const int*, RandomAccessIterator<int*> >();
  201. test<RandomAccessIterator<const int*>, const int*, int*>();
  202. #endif
  203. #ifdef SET_UNION_5
  204. test<const int*, InputIterator<const int*>, OutputIterator<int*> >();
  205. test<const int*, InputIterator<const int*>, BidirectionalIterator<int*> >();
  206. test<const int*, InputIterator<const int*>, BidirectionalIterator<int*> >();
  207. test<const int*, InputIterator<const int*>, RandomAccessIterator<int*> >();
  208. test<const int*, InputIterator<const int*>, int*>();
  209. test<const int*, ForwardIterator<const int*>, OutputIterator<int*> >();
  210. test<const int*, ForwardIterator<const int*>, ForwardIterator<int*> >();
  211. test<const int*, ForwardIterator<const int*>, BidirectionalIterator<int*> >();
  212. test<const int*, ForwardIterator<const int*>, RandomAccessIterator<int*> >();
  213. test<const int*, ForwardIterator<const int*>, int*>();
  214. test<const int*, BidirectionalIterator<const int*>, OutputIterator<int*> >();
  215. test<const int*, BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  216. test<const int*, BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  217. test<const int*, BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  218. test<const int*, BidirectionalIterator<const int*>, int*>();
  219. test<const int*, RandomAccessIterator<const int*>, OutputIterator<int*> >();
  220. test<const int*, RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  221. test<const int*, RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  222. test<const int*, RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  223. test<const int*, RandomAccessIterator<const int*>, int*>();
  224. test<const int*, const int*, OutputIterator<int*> >();
  225. test<const int*, const int*, ForwardIterator<int*> >();
  226. test<const int*, const int*, BidirectionalIterator<int*> >();
  227. test<const int*, const int*, RandomAccessIterator<int*> >();
  228. test<const int*, const int*, int*>();
  229. #endif
  230. #ifdef SET_UNION_6
  231. // Test projections
  232. {
  233. S ia[] = {S{1}, S{2}, S{2}, S{3}, S{3}, S{3}, S{4}, S{4}, S{4}, S{4}};
  234. T ib[] = {T{2}, T{4}, T{4}, T{6}};
  235. U ic[20];
  236. int ir[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6};
  237. static const int sr = sizeof(ir)/sizeof(ir[0]);
  238. using R = ranges::set_union_result<S *, T*, U*>;
  239. R res = ranges::set_union(ia, ib, ic, std::less<int>(), &S::i, &T::j);
  240. CHECK((res.out - ic) == sr);
  241. CHECK(ranges::lexicographical_compare(ic, res.out, ir, ir+sr, std::less<int>(), &U::k) == false);
  242. ranges::fill(ic, U{0});
  243. using R2 = ranges::set_union_result<T *, S*, U*>;
  244. R2 res2 = ranges::set_union(ib, ia, ic, std::less<int>(), &T::j, &S::i);
  245. CHECK((res2.out - ic) == sr);
  246. CHECK(ranges::lexicographical_compare(ic, res2.out, ir, ir+sr, std::less<int>(), &U::k) == false);
  247. }
  248. // Test projections
  249. #ifndef RANGES_WORKAROUND_MSVC_573728
  250. {
  251. S ia[] = {S{1}, S{2}, S{2}, S{3}, S{3}, S{3}, S{4}, S{4}, S{4}, S{4}};
  252. T ib[] = {T{2}, T{4}, T{4}, T{6}};
  253. U ic[20];
  254. int ir[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6};
  255. static const int sr = sizeof(ir)/sizeof(ir[0]);
  256. auto res = ranges::set_union(std::move(ia), ranges::views::all(ib), ic, std::less<int>(), &S::i, &T::j);
  257. CHECK(::is_dangling(res.in1));
  258. CHECK(res.in2 == ranges::end(ib));
  259. CHECK((res.out - ic) == sr);
  260. CHECK(ranges::lexicographical_compare(ic, res.out, ir, ir+sr, std::less<int>(), &U::k) == false);
  261. ranges::fill(ic, U{0});
  262. auto res2 = ranges::set_union(std::move(ib), ranges::views::all(ia), ic, std::less<int>(), &T::j, &S::i);
  263. CHECK(res2.in2 == ranges::end(ia));
  264. CHECK(::is_dangling(res2.in1));
  265. CHECK((res2.out - ic) == sr);
  266. CHECK(ranges::lexicographical_compare(ic, res2.out, ir, ir+sr, std::less<int>(), &U::k) == false);
  267. }
  268. #endif // RANGES_WORKAROUND_MSVC_573728
  269. {
  270. std::vector<S> ia{S{1}, S{2}, S{2}, S{3}, S{3}, S{3}, S{4}, S{4}, S{4}, S{4}};
  271. std::vector<T> ib{T{2}, T{4}, T{4}, T{6}};
  272. U ic[20];
  273. int ir[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6};
  274. static const int sr = sizeof(ir)/sizeof(ir[0]);
  275. auto res = ranges::set_union(std::move(ia), ranges::views::all(ib), ic, std::less<int>(), &S::i, &T::j);
  276. CHECK(::is_dangling(res.in1));
  277. CHECK(res.in2 == ranges::end(ib));
  278. CHECK((res.out - ic) == sr);
  279. CHECK(ranges::lexicographical_compare(ic, res.out, ir, ir+sr, std::less<int>(), &U::k) == false);
  280. ranges::fill(ic, U{0});
  281. auto res2 = ranges::set_union(std::move(ib), ranges::views::all(ia), ic, std::less<int>(), &T::j, &S::i);
  282. CHECK(res2.in2 == ranges::end(ia));
  283. CHECK(::is_dangling(res2.in1));
  284. CHECK((res2.out - ic) == sr);
  285. CHECK(ranges::lexicographical_compare(ic, res2.out, ir, ir+sr, std::less<int>(), &U::k) == false);
  286. }
  287. {
  288. STATIC_CHECK(test_constexpr());
  289. }
  290. #endif
  291. return ::test_result();
  292. }