Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __ranges / to.h
blobcf162100ee46b72426f5e9ee7c8d76f9c57bd844
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___RANGES_TO_H
11 #define _LIBCPP___RANGES_TO_H
13 #include <__algorithm/ranges_copy.h>
14 #include <__concepts/constructible.h>
15 #include <__concepts/convertible_to.h>
16 #include <__concepts/derived_from.h>
17 #include <__concepts/same_as.h>
18 #include <__config>
19 #include <__functional/bind_back.h>
20 #include <__iterator/back_insert_iterator.h>
21 #include <__iterator/insert_iterator.h>
22 #include <__iterator/iterator_traits.h>
23 #include <__ranges/access.h>
24 #include <__ranges/concepts.h>
25 #include <__ranges/from_range.h>
26 #include <__ranges/range_adaptor.h>
27 #include <__ranges/size.h>
28 #include <__ranges/transform_view.h>
29 #include <__type_traits/add_pointer.h>
30 #include <__type_traits/is_const.h>
31 #include <__type_traits/is_volatile.h>
32 #include <__type_traits/type_identity.h>
33 #include <__utility/declval.h>
34 #include <__utility/forward.h>
35 #include <cstddef>
37 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
38 # pragma GCC system_header
39 #endif
41 _LIBCPP_BEGIN_NAMESPACE_STD
43 #if _LIBCPP_STD_VER >= 23
45 namespace ranges {
47 template <class _Container>
48 constexpr bool __reservable_container =
49 sized_range<_Container> && requires(_Container& __c, range_size_t<_Container> __n) {
50 __c.reserve(__n);
51 { __c.capacity() } -> same_as<decltype(__n)>;
52 { __c.max_size() } -> same_as<decltype(__n)>;
55 template <class _Container, class _Ref>
56 constexpr bool __container_insertable = requires(_Container& __c, _Ref&& __ref) {
57 requires(
58 requires { __c.push_back(std::forward<_Ref>(__ref)); } ||
59 requires { __c.insert(__c.end(), std::forward<_Ref>(__ref)); });
62 template <class _Ref, class _Container>
63 _LIBCPP_HIDE_FROM_ABI constexpr auto __container_inserter(_Container& __c) {
64 if constexpr (requires { __c.push_back(std::declval<_Ref>()); }) {
65 return std::back_inserter(__c);
66 } else {
67 return std::inserter(__c, __c.end());
71 // Note: making this a concept allows short-circuiting the second condition.
72 template <class _Container, class _Range>
73 concept __try_non_recursive_conversion =
74 !input_range<_Container> || convertible_to<range_reference_t<_Range>, range_value_t<_Container>>;
76 template <class _Container, class _Range, class... _Args>
77 concept __constructible_from_iter_pair =
78 common_range<_Range> && requires { typename iterator_traits<iterator_t<_Range>>::iterator_category; } &&
79 derived_from<typename iterator_traits<iterator_t<_Range>>::iterator_category, input_iterator_tag> &&
80 constructible_from<_Container, iterator_t<_Range>, sentinel_t<_Range>, _Args...>;
82 template <class>
83 concept __always_false = false;
85 // `ranges::to` base template -- the `_Container` type is a simple type template parameter.
86 template <class _Container, input_range _Range, class... _Args>
87 requires(!view<_Container>)
88 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Container to(_Range&& __range, _Args&&... __args) {
89 // Mandates: C is a cv-unqualified class type.
90 static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
91 static_assert(
92 !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
94 // First see if the non-recursive case applies -- the conversion target is either:
95 // - a range with a convertible value type;
96 // - a non-range type which might support being created from the input argument(s) (e.g. an `optional`).
97 if constexpr (__try_non_recursive_conversion<_Container, _Range>) {
98 // Case 1 -- construct directly from the given range.
99 if constexpr (constructible_from<_Container, _Range, _Args...>) {
100 return _Container(std::forward<_Range>(__range), std::forward<_Args>(__args)...);
103 // Case 2 -- construct using the `from_range_t` tagged constructor.
104 else if constexpr (constructible_from<_Container, from_range_t, _Range, _Args...>) {
105 return _Container(from_range, std::forward<_Range>(__range), std::forward<_Args>(__args)...);
108 // Case 3 -- construct from a begin-end iterator pair.
109 else if constexpr (__constructible_from_iter_pair<_Container, _Range, _Args...>) {
110 return _Container(ranges::begin(__range), ranges::end(__range), std::forward<_Args>(__args)...);
113 // Case 4 -- default-construct (or construct from the extra arguments) and insert, reserving the size if possible.
114 else if constexpr (constructible_from<_Container, _Args...> &&
115 __container_insertable<_Container, range_reference_t<_Range>>) {
116 _Container __result(std::forward<_Args>(__args)...);
117 if constexpr (sized_range<_Range> && __reservable_container<_Container>) {
118 __result.reserve(static_cast<range_size_t<_Container>>(ranges::size(__range)));
121 ranges::copy(__range, ranges::__container_inserter<range_reference_t<_Range>>(__result));
123 return __result;
125 } else {
126 static_assert(__always_false<_Container>, "ranges::to: unable to convert to the given container type.");
129 // Try the recursive case.
130 } else if constexpr (input_range<range_reference_t<_Range>>) {
131 return ranges::to<_Container>(
132 __range | views::transform([](auto&& __elem) {
133 return ranges::to<range_value_t<_Container>>(std::forward<decltype(__elem)>(__elem));
135 std::forward<_Args>(__args)...);
137 } else {
138 static_assert(__always_false<_Container>, "ranges::to: unable to convert to the given container type.");
142 template <class _Range>
143 struct __minimal_input_iterator {
144 using iterator_category = input_iterator_tag;
145 using value_type = range_value_t<_Range>;
146 using difference_type = ptrdiff_t;
147 using pointer = add_pointer_t<range_reference_t<_Range>>;
148 using reference = range_reference_t<_Range>;
150 reference operator*() const;
151 pointer operator->() const;
152 __minimal_input_iterator& operator++();
153 __minimal_input_iterator operator++(int);
154 bool operator==(const __minimal_input_iterator&) const;
157 // Deduces the full type of the container from the given template template parameter.
158 template <template <class...> class _Container, input_range _Range, class... _Args>
159 struct _Deducer {
160 _LIBCPP_HIDE_FROM_ABI static constexpr auto __deduce_func() {
161 using _InputIter = __minimal_input_iterator<_Range>;
163 // Case 1 -- can construct directly from the given range.
164 if constexpr (requires { _Container(std::declval<_Range>(), std::declval<_Args>()...); }) {
165 using _Result = decltype( //
166 _Container(std::declval<_Range>(), std::declval<_Args>()...));
167 return type_identity<_Result>{};
169 // Case 2 -- can construct from the given range using the `from_range_t` tagged constructor.
170 } else if constexpr ( //
171 requires { _Container(from_range, std::declval<_Range>(), std::declval<_Args>()...); }) {
172 using _Result = //
173 decltype(_Container(from_range, std::declval<_Range>(), std::declval<_Args>()...));
174 return type_identity<_Result>{};
176 // Case 3 -- can construct from a begin-end iterator pair.
177 } else if constexpr ( //
178 requires { _Container(std::declval<_InputIter>(), std::declval<_InputIter>(), std::declval<_Args>()...); }) {
179 using _Result =
180 decltype(_Container(std::declval<_InputIter>(), std::declval<_InputIter>(), std::declval<_Args>()...));
181 return type_identity<_Result>{};
183 } else {
184 static_assert(__always_false<_Range>,
185 "ranges::to: unable to deduce the container type from the template template argument.");
189 using type = typename decltype(__deduce_func())::type;
192 // `ranges::to` specialization -- `_Container` is a template template parameter requiring deduction to figure out the
193 // container element type.
194 template <template <class...> class _Container, input_range _Range, class... _Args>
195 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Range&& __range, _Args&&... __args) {
196 using _DeduceExpr = typename _Deducer<_Container, _Range, _Args...>::type;
197 return ranges::to<_DeduceExpr>(std::forward<_Range>(__range), std::forward<_Args>(__args)...);
200 // Range adaptor closure object 1 -- wrapping the `ranges::to` version where `_Container` is a simple type template
201 // parameter.
202 template <class _Container, class... _Args>
203 requires(!view<_Container>)
204 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Args&&... __args) {
205 // Mandates: C is a cv-unqualified class type.
206 static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
207 static_assert(
208 !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
210 auto __to_func = []<input_range _Range, class... _Tail>(_Range&& __range, _Tail&&... __tail)
211 requires requires { //
212 /**/ ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
214 { return ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...); };
216 return __range_adaptor_closure_t(std::__bind_back(__to_func, std::forward<_Args>(__args)...));
219 // Range adaptor closure object 2 -- wrapping the `ranges::to` version where `_Container` is a template template
220 // parameter.
221 template <template <class...> class _Container, class... _Args>
222 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Args&&... __args) {
223 // clang-format off
224 auto __to_func = []<input_range _Range, class... _Tail,
225 class _DeducedExpr = typename _Deducer<_Container, _Range, _Tail...>::type>
226 (_Range&& __range, _Tail&& ... __tail)
227 requires requires { //
228 /**/ ranges::to<_DeducedExpr>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
231 return ranges::to<_DeducedExpr>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
233 // clang-format on
235 return __range_adaptor_closure_t(std::__bind_back(__to_func, std::forward<_Args>(__args)...));
238 } // namespace ranges
240 #endif // _LIBCPP_STD_VER >= 23
242 _LIBCPP_END_NAMESPACE_STD
244 #endif // _LIBCPP___RANGES_TO_H