You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 line
6.6KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ALGORITHM_UNIQUE_COPY_HPP
  14. #define RANGES_V3_ALGORITHM_UNIQUE_COPY_HPP
  15. #include <meta/meta.hpp>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/algorithm/result_types.hpp>
  18. #include <range/v3/functional/comparisons.hpp>
  19. #include <range/v3/functional/identity.hpp>
  20. #include <range/v3/functional/invoke.hpp>
  21. #include <range/v3/iterator/concepts.hpp>
  22. #include <range/v3/iterator/traits.hpp>
  23. #include <range/v3/range/access.hpp>
  24. #include <range/v3/range/concepts.hpp>
  25. #include <range/v3/range/dangling.hpp>
  26. #include <range/v3/range/traits.hpp>
  27. #include <range/v3/utility/static_const.hpp>
  28. namespace ranges
  29. {
  30. /// \addtogroup group-algorithms
  31. /// @{
  32. template<typename I, typename O>
  33. using unique_copy_result = detail::in_out_result<I, O>;
  34. /// \cond
  35. namespace detail
  36. {
  37. template<typename I, typename S, typename O, typename C, typename P>
  38. unique_copy_result<I, O> unique_copy_impl(I first, S last, O out, C pred, P proj,
  39. detail::input_iterator_tag_,
  40. std::false_type)
  41. {
  42. if(first != last)
  43. {
  44. // Must save a copy into a local because we will need this value
  45. // even after we advance the input iterator.
  46. iter_value_t<I> value =
  47. *first; // This is guaranteed by indirectly_copyable
  48. *out = value;
  49. ++out;
  50. while(++first != last)
  51. {
  52. auto && x = *first;
  53. if(!invoke(pred, invoke(proj, value), invoke(proj, x)))
  54. {
  55. value = (decltype(x) &&)x;
  56. *out = value;
  57. ++out;
  58. }
  59. }
  60. }
  61. return {first, out};
  62. }
  63. template<typename I, typename S, typename O, typename C, typename P>
  64. unique_copy_result<I, O> unique_copy_impl(I first, S last, O out, C pred, P proj,
  65. detail::forward_iterator_tag_,
  66. std::false_type)
  67. {
  68. if(first != last)
  69. {
  70. I tmp = first;
  71. *out = *tmp;
  72. ++out;
  73. while(++first != last)
  74. {
  75. auto && x = *first;
  76. if(!invoke(pred, invoke(proj, *tmp), invoke(proj, x)))
  77. {
  78. *out = (decltype(x) &&)x;
  79. ++out;
  80. tmp = first;
  81. }
  82. }
  83. }
  84. return {first, out};
  85. }
  86. template<typename I, typename S, typename O, typename C, typename P>
  87. unique_copy_result<I, O> unique_copy_impl(I first, S last, O out, C pred, P proj,
  88. detail::input_iterator_tag_,
  89. std::true_type)
  90. {
  91. if(first != last)
  92. {
  93. *out = *first;
  94. while(++first != last)
  95. {
  96. auto && x = *first;
  97. if(!invoke(pred, invoke(proj, *out), invoke(proj, x)))
  98. *++out = (decltype(x) &&)x;
  99. }
  100. ++out;
  101. }
  102. return {first, out};
  103. }
  104. } // namespace detail
  105. /// \endcond
  106. RANGES_BEGIN_NIEBLOID(unique_copy)
  107. /// \brief template function unique_copy
  108. ///
  109. /// range-based version of the `unique_copy` std algorithm
  110. ///
  111. /// \pre `Rng` is a model of the `input_range` concept
  112. /// \pre `O` is a model of the `weakly_incrementable` concept
  113. /// \pre `C` is a model of the `relation` concept
  114. template<typename I,
  115. typename S,
  116. typename O,
  117. typename C = equal_to,
  118. typename P = identity>
  119. auto RANGES_FUN_NIEBLOID(unique_copy)(
  120. I first, S last, O out, C pred = C{}, P proj = P{}) //
  121. ->CPP_ret(unique_copy_result<I, O>)( //
  122. requires input_iterator<I> && sentinel_for<S, I> &&
  123. indirect_relation<C, projected<I, P>> && weakly_incrementable<O> &&
  124. indirectly_copyable<I, O> &&
  125. (forward_iterator<I> || forward_iterator<O> ||
  126. indirectly_copyable_storable<I, O>))
  127. {
  128. return detail::unique_copy_impl(std::move(first),
  129. std::move(last),
  130. std::move(out),
  131. std::move(pred),
  132. std::move(proj),
  133. iterator_tag_of<I>(),
  134. meta::bool_<forward_iterator<O>>{});
  135. }
  136. /// \overload
  137. template<typename Rng, typename O, typename C = equal_to, typename P = identity>
  138. auto RANGES_FUN_NIEBLOID(unique_copy)(
  139. Rng && rng, O out, C pred = C{}, P proj = P{}) //
  140. ->CPP_ret(unique_copy_result<safe_iterator_t<Rng>, O>)( //
  141. requires input_range<Rng> &&
  142. indirect_relation<C, projected<iterator_t<Rng>, P>> &&
  143. weakly_incrementable<O> && indirectly_copyable<iterator_t<Rng>, O> &&
  144. (forward_iterator<iterator_t<Rng>> || forward_iterator<O> ||
  145. indirectly_copyable_storable<iterator_t<Rng>, O>))
  146. {
  147. return detail::unique_copy_impl(begin(rng),
  148. end(rng),
  149. std::move(out),
  150. std::move(pred),
  151. std::move(proj),
  152. iterator_tag_of<iterator_t<Rng>>(),
  153. meta::bool_<forward_iterator<O>>{});
  154. }
  155. RANGES_END_NIEBLOID(unique_copy)
  156. namespace cpp20
  157. {
  158. using ranges::unique_copy;
  159. using ranges::unique_copy_result;
  160. } // namespace cpp20
  161. /// @}
  162. } // namespace ranges
  163. #endif // include guard