1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___ALGORITHM_RANGES_UNIQUE_COPY_H
10#define _LIBCPP___ALGORITHM_RANGES_UNIQUE_COPY_H
11
12#include <__algorithm/in_out_result.h>
13#include <__algorithm/iterator_operations.h>
14#include <__algorithm/make_projected.h>
15#include <__algorithm/unique_copy.h>
16#include <__concepts/same_as.h>
17#include <__config>
18#include <__functional/identity.h>
19#include <__functional/invoke.h>
20#include <__functional/ranges_operations.h>
21#include <__iterator/concepts.h>
22#include <__iterator/iterator_traits.h>
23#include <__iterator/projected.h>
24#include <__ranges/access.h>
25#include <__ranges/concepts.h>
26#include <__ranges/dangling.h>
27#include <__utility/forward.h>
28#include <__utility/move.h>
29
30#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31# pragma GCC system_header
32#endif
33
34_LIBCPP_PUSH_MACROS
35#include <__undef_macros>
36
37#if _LIBCPP_STD_VER >= 20
38
39_LIBCPP_BEGIN_NAMESPACE_STD
40
41namespace ranges {
42
43template <class _InIter, class _OutIter>
44using unique_copy_result = in_out_result<_InIter, _OutIter>;
45
46template <class _InIter, class _OutIter>
47concept __can_reread_from_output = (input_iterator<_OutIter> && same_as<iter_value_t<_InIter>, iter_value_t<_OutIter>>);
48
49struct __unique_copy {
50 template <class _InIter, class _OutIter>
51 static consteval auto __get_algo_tag() {
52 if constexpr (forward_iterator<_InIter>) {
53 return __unique_copy_tags::__reread_from_input_tag{};
54 } else if constexpr (__can_reread_from_output<_InIter, _OutIter>) {
55 return __unique_copy_tags::__reread_from_output_tag{};
56 } else if constexpr (indirectly_copyable_storable<_InIter, _OutIter>) {
57 return __unique_copy_tags::__read_from_tmp_value_tag{};
58 }
59 }
60
61 template <class _InIter, class _OutIter>
62 using __algo_tag_t _LIBCPP_NODEBUG = decltype(__get_algo_tag<_InIter, _OutIter>());
63
64 template <input_iterator _InIter,
65 sentinel_for<_InIter> _Sent,
66 weakly_incrementable _OutIter,
67 class _Proj = identity,
68 indirect_equivalence_relation<projected<_InIter, _Proj>> _Comp = ranges::equal_to>
69 requires indirectly_copyable<_InIter, _OutIter> &&
70 (forward_iterator<_InIter> ||
71 (input_iterator<_OutIter> && same_as<iter_value_t<_InIter>, iter_value_t<_OutIter>>) ||
72 indirectly_copyable_storable<_InIter, _OutIter>)
73 _LIBCPP_HIDE_FROM_ABI constexpr unique_copy_result<_InIter, _OutIter>
74 operator()(_InIter __first, _Sent __last, _OutIter __result, _Comp __comp = {}, _Proj __proj = {}) const {
75 return std::__unique_copy<_RangeAlgPolicy>(
76 std::move(__first),
77 std::move(__last),
78 std::move(__result),
79 std::__make_projected(__comp, __proj),
80 __algo_tag_t<_InIter, _OutIter>());
81 }
82
83 template <input_range _Range,
84 weakly_incrementable _OutIter,
85 class _Proj = identity,
86 indirect_equivalence_relation<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
87 requires indirectly_copyable<iterator_t<_Range>, _OutIter> &&
88 (forward_iterator<iterator_t<_Range>> ||
89 (input_iterator<_OutIter> && same_as<range_value_t<_Range>, iter_value_t<_OutIter>>) ||
90 indirectly_copyable_storable<iterator_t<_Range>, _OutIter>)
91 _LIBCPP_HIDE_FROM_ABI constexpr unique_copy_result<borrowed_iterator_t<_Range>, _OutIter>
92 operator()(_Range&& __range, _OutIter __result, _Comp __comp = {}, _Proj __proj = {}) const {
93 return std::__unique_copy<_RangeAlgPolicy>(
94 ranges::begin(__range),
95 ranges::end(__range),
96 std::move(__result),
97 std::__make_projected(__comp, __proj),
98 __algo_tag_t<iterator_t<_Range>, _OutIter>());
99 }
100};
101
102inline namespace __cpo {
103inline constexpr auto unique_copy = __unique_copy{};
104} // namespace __cpo
105} // namespace ranges
106
107_LIBCPP_END_NAMESPACE_STD
108
109#endif // _LIBCPP_STD_VER >= 20
110
111_LIBCPP_POP_MACROS
112
113#endif // _LIBCPP___ALGORITHM_RANGES_UNIQUE_COPY_H
114