merge.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <memory>
  16. #include <utility>
  17. #include <algorithm>
  18. #include <range/v3/core.hpp>
  19. #include <range/v3/algorithm/merge.hpp>
  20. #include <range/v3/algorithm/is_sorted.hpp>
  21. #include "../array.hpp"
  22. #include "../simple_test.hpp"
  23. #include "../test_iterators.hpp"
  24. RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION
  25. constexpr bool test_constexpr()
  26. {
  27. using namespace ranges;
  28. constexpr unsigned N = 100;
  29. test::array<int, N> ia{{0}};
  30. test::array<int, N> ib{{0}};
  31. test::array<int, 2 * N> ic{{0}};
  32. for(unsigned i = 0; i < N; ++i)
  33. ia[i] = 2 * i;
  34. for(unsigned i = 0; i < N; ++i)
  35. ib[i] = 2 * i + 1;
  36. auto r = merge(ia, ib, begin(ic));
  37. STATIC_CHECK_RETURN(r.in1 == end(ia));
  38. STATIC_CHECK_RETURN(r.in2 == end(ib));
  39. STATIC_CHECK_RETURN(r.out == end(ic));
  40. STATIC_CHECK_RETURN(ic[0] == 0);
  41. STATIC_CHECK_RETURN(ic[2 * N - 1] == (int)(2 * N - 1));
  42. STATIC_CHECK_RETURN(is_sorted(ic));
  43. return true;
  44. }
  45. int main()
  46. {
  47. {
  48. int N = 100000;
  49. std::unique_ptr<int[]> ia{new int[N]};
  50. std::unique_ptr<int[]> ib{new int[N]};
  51. std::unique_ptr<int[]> ic{new int[2 * N]};
  52. for(int i = 0; i < N; ++i)
  53. ia[i] = 2 * i;
  54. for(int i = 0; i < N; ++i)
  55. ib[i] = 2 * i + 1;
  56. auto r = ranges::merge(ia.get(), ia.get() + N,
  57. ib.get(), ib.get() + N, ic.get());
  58. CHECK(r.in1 == ia.get() + N);
  59. CHECK(r.in2 == ib.get() + N);
  60. CHECK(r.out == ic.get() + 2 * N);
  61. CHECK(ic[0] == 0);
  62. CHECK(ic[2 * N - 1] == 2 * N - 1);
  63. CHECK(std::is_sorted(ic.get(), ic.get() + 2 * N));
  64. }
  65. {
  66. int N = 100000;
  67. std::unique_ptr<int[]> ia{new int[N]};
  68. std::unique_ptr<int[]> ib{new int[N]};
  69. std::unique_ptr<int[]> ic{new int[2 * N]};
  70. for(int i = 0; i < N; ++i)
  71. ia[i] = 2 * i;
  72. for(int i = 0; i < N; ++i)
  73. ib[i] = 2 * i + 1;
  74. auto r0 = ranges::make_subrange(ia.get(), ia.get() + N);
  75. auto r1 = ranges::make_subrange(ib.get(), ib.get() + N);
  76. auto r = ranges::merge(r0, r1, ic.get());
  77. CHECK(r.in1 == ia.get() + N);
  78. CHECK(r.in2 == ib.get() + N);
  79. CHECK(r.out == ic.get() + 2 * N);
  80. CHECK(ic[0] == 0);
  81. CHECK(ic[2 * N - 1] == 2 * N - 1);
  82. CHECK(std::is_sorted(ic.get(), ic.get() + 2 * N));
  83. }
  84. {
  85. int N = 100000;
  86. std::unique_ptr<int[]> ia{new int[N]};
  87. std::unique_ptr<int[]> ib{new int[N]};
  88. std::unique_ptr<int[]> ic{new int[2 * N]};
  89. for(int i = 0; i < N; ++i)
  90. ia[i] = 2 * i;
  91. for(int i = 0; i < N; ++i)
  92. ib[i] = 2 * i + 1;
  93. auto r0 = ::MakeTestRange(ia.get(), ia.get() + N);
  94. auto r1 = ::MakeTestRange(ib.get(), ib.get() + N);
  95. auto r = ranges::merge(std::move(r0), std::move(r1), ic.get());
  96. CHECK(::is_dangling(r.in1));
  97. CHECK(::is_dangling(r.in2));
  98. CHECK(r.out == ic.get() + 2 * N);
  99. CHECK(ic[0] == 0);
  100. CHECK(ic[2 * N - 1] == 2 * N - 1);
  101. CHECK(std::is_sorted(ic.get(), ic.get() + 2 * N));
  102. static_assert(std::is_same<decltype(r),
  103. ranges::merge_result<ranges::dangling, ranges::dangling, int *>>::value, "");
  104. }
  105. {
  106. STATIC_CHECK(test_constexpr());
  107. }
  108. return ::test_result();
  109. }