xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/ranges_unique_copy.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
1753f127fSDimitry Andric //===----------------------------------------------------------------------===//
2753f127fSDimitry Andric //
3753f127fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4753f127fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5753f127fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6753f127fSDimitry Andric //
7753f127fSDimitry Andric //===----------------------------------------------------------------------===//
8753f127fSDimitry Andric 
9753f127fSDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_UNIQUE_COPY_H
10753f127fSDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_UNIQUE_COPY_H
11753f127fSDimitry Andric 
12753f127fSDimitry Andric #include <__algorithm/in_out_result.h>
1361cfbce3SDimitry Andric #include <__algorithm/iterator_operations.h>
14753f127fSDimitry Andric #include <__algorithm/make_projected.h>
15753f127fSDimitry Andric #include <__algorithm/unique_copy.h>
16753f127fSDimitry Andric #include <__concepts/same_as.h>
17753f127fSDimitry Andric #include <__config>
18753f127fSDimitry Andric #include <__functional/identity.h>
19753f127fSDimitry Andric #include <__functional/invoke.h>
20753f127fSDimitry Andric #include <__functional/ranges_operations.h>
21753f127fSDimitry Andric #include <__iterator/concepts.h>
22753f127fSDimitry Andric #include <__iterator/iterator_traits.h>
23753f127fSDimitry Andric #include <__iterator/projected.h>
24753f127fSDimitry Andric #include <__ranges/access.h>
25753f127fSDimitry Andric #include <__ranges/concepts.h>
26753f127fSDimitry Andric #include <__ranges/dangling.h>
27753f127fSDimitry Andric #include <__utility/forward.h>
28753f127fSDimitry Andric #include <__utility/move.h>
29bdd1243dSDimitry Andric #include <__utility/pair.h>
30753f127fSDimitry Andric 
31753f127fSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32753f127fSDimitry Andric #  pragma GCC system_header
33753f127fSDimitry Andric #endif
34753f127fSDimitry Andric 
35*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
36*b3edf446SDimitry Andric #include <__undef_macros>
37*b3edf446SDimitry Andric 
3806c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
39753f127fSDimitry Andric 
40753f127fSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
41753f127fSDimitry Andric 
42753f127fSDimitry Andric namespace ranges {
43753f127fSDimitry Andric 
44753f127fSDimitry Andric template <class _InIter, class _OutIter>
45753f127fSDimitry Andric using unique_copy_result = in_out_result<_InIter, _OutIter>;
46753f127fSDimitry Andric 
47753f127fSDimitry Andric namespace __unique_copy {
48753f127fSDimitry Andric 
4961cfbce3SDimitry Andric template <class _InIter, class _OutIter>
5061cfbce3SDimitry Andric concept __can_reread_from_output = (input_iterator<_OutIter> && same_as<iter_value_t<_InIter>, iter_value_t<_OutIter>>);
51753f127fSDimitry Andric 
5261cfbce3SDimitry Andric struct __fn {
5361cfbce3SDimitry Andric   template <class _InIter, class _OutIter>
__get_algo_tag__fn5461cfbce3SDimitry Andric   static consteval auto __get_algo_tag() {
5561cfbce3SDimitry Andric     if constexpr (forward_iterator<_InIter>) {
5661cfbce3SDimitry Andric       return __unique_copy_tags::__reread_from_input_tag{};
5761cfbce3SDimitry Andric     } else if constexpr (__can_reread_from_output<_InIter, _OutIter>) {
5861cfbce3SDimitry Andric       return __unique_copy_tags::__reread_from_output_tag{};
5961cfbce3SDimitry Andric     } else if constexpr (indirectly_copyable_storable<_InIter, _OutIter>) {
6061cfbce3SDimitry Andric       return __unique_copy_tags::__read_from_tmp_value_tag{};
6161cfbce3SDimitry Andric     }
6261cfbce3SDimitry Andric   }
6361cfbce3SDimitry Andric 
6461cfbce3SDimitry Andric   template <class _InIter, class _OutIter>
6561cfbce3SDimitry Andric   using __algo_tag_t = decltype(__get_algo_tag<_InIter, _OutIter>());
6661cfbce3SDimitry Andric 
6761cfbce3SDimitry Andric   template <input_iterator _InIter,
6861cfbce3SDimitry Andric             sentinel_for<_InIter> _Sent,
6961cfbce3SDimitry Andric             weakly_incrementable _OutIter,
7061cfbce3SDimitry Andric             class _Proj                                                    = identity,
71753f127fSDimitry Andric             indirect_equivalence_relation<projected<_InIter, _Proj>> _Comp = ranges::equal_to>
72753f127fSDimitry Andric     requires indirectly_copyable<_InIter, _OutIter> &&
73753f127fSDimitry Andric              (forward_iterator<_InIter> ||
74753f127fSDimitry Andric               (input_iterator<_OutIter> && same_as<iter_value_t<_InIter>, iter_value_t<_OutIter>>) ||
75753f127fSDimitry Andric               indirectly_copyable_storable<_InIter, _OutIter>)
7661cfbce3SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr unique_copy_result<_InIter, _OutIter>
operator__fn77753f127fSDimitry Andric   operator()(_InIter __first, _Sent __last, _OutIter __result, _Comp __comp = {}, _Proj __proj = {}) const {
7861cfbce3SDimitry Andric     auto __ret = std::__unique_copy<_RangeAlgPolicy>(
7961cfbce3SDimitry Andric         std::move(__first),
8061cfbce3SDimitry Andric         std::move(__last),
8161cfbce3SDimitry Andric         std::move(__result),
8261cfbce3SDimitry Andric         std::__make_projected(__comp, __proj),
8361cfbce3SDimitry Andric         __algo_tag_t<_InIter, _OutIter>());
8461cfbce3SDimitry Andric     return {std::move(__ret.first), std::move(__ret.second)};
85753f127fSDimitry Andric   }
86753f127fSDimitry Andric 
8761cfbce3SDimitry Andric   template <input_range _Range,
8861cfbce3SDimitry Andric             weakly_incrementable _OutIter,
8961cfbce3SDimitry Andric             class _Proj                                                               = identity,
90753f127fSDimitry Andric             indirect_equivalence_relation<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
91753f127fSDimitry Andric     requires indirectly_copyable<iterator_t<_Range>, _OutIter> &&
92753f127fSDimitry Andric              (forward_iterator<iterator_t<_Range>> ||
93753f127fSDimitry Andric               (input_iterator<_OutIter> && same_as<range_value_t<_Range>, iter_value_t<_OutIter>>) ||
94753f127fSDimitry Andric               indirectly_copyable_storable<iterator_t<_Range>, _OutIter>)
9561cfbce3SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr unique_copy_result<borrowed_iterator_t<_Range>, _OutIter>
operator__fn96753f127fSDimitry Andric   operator()(_Range&& __range, _OutIter __result, _Comp __comp = {}, _Proj __proj = {}) const {
9761cfbce3SDimitry Andric     auto __ret = std::__unique_copy<_RangeAlgPolicy>(
9861cfbce3SDimitry Andric         ranges::begin(__range),
9961cfbce3SDimitry Andric         ranges::end(__range),
10061cfbce3SDimitry Andric         std::move(__result),
10161cfbce3SDimitry Andric         std::__make_projected(__comp, __proj),
10261cfbce3SDimitry Andric         __algo_tag_t<iterator_t<_Range>, _OutIter>());
10361cfbce3SDimitry Andric     return {std::move(__ret.first), std::move(__ret.second)};
104753f127fSDimitry Andric   }
105753f127fSDimitry Andric };
106753f127fSDimitry Andric 
107753f127fSDimitry Andric } // namespace __unique_copy
108753f127fSDimitry Andric 
109753f127fSDimitry Andric inline namespace __cpo {
110753f127fSDimitry Andric inline constexpr auto unique_copy = __unique_copy::__fn{};
111753f127fSDimitry Andric } // namespace __cpo
112753f127fSDimitry Andric } // namespace ranges
113753f127fSDimitry Andric 
114753f127fSDimitry Andric _LIBCPP_END_NAMESPACE_STD
115753f127fSDimitry Andric 
11606c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
117753f127fSDimitry Andric 
118*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
119*b3edf446SDimitry Andric 
120753f127fSDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_UNIQUE_COPY_H
121