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 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___MEMORY_RANGES_UNINITIALIZED_ALGORITHMS_H
11 #define _LIBCPP___MEMORY_RANGES_UNINITIALIZED_ALGORITHMS_H
12 
13 #include <__concepts/constructible.h>
14 #include <__config>
15 #include <__function_like.h>
16 #include <__iterator/incrementable_traits.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__iterator/readable_traits.h>
19 #include <__memory/concepts.h>
20 #include <__memory/uninitialized_algorithms.h>
21 #include <__ranges/access.h>
22 #include <__ranges/concepts.h>
23 #include <__ranges/dangling.h>
24 #include <__utility/move.h>
25 #include <type_traits>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #pragma GCC system_header
29 #endif
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 #if !defined(_LIBCPP_HAS_NO_RANGES)
34 namespace ranges {
35 
36 // uninitialized_default_construct
37 
38 namespace __uninitialized_default_construct {
39 
40 struct __fn final : private __function_like {
41 
42   constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {}
43 
44   template <__nothrow_forward_iterator _ForwardIterator,
45             __nothrow_sentinel_for<_ForwardIterator> _Sentinel>
46     requires default_initializable<iter_value_t<_ForwardIterator>>
47   _ForwardIterator operator()(_ForwardIterator __first, _Sentinel __last) const {
48     using _ValueType = remove_reference_t<iter_reference_t<_ForwardIterator>>;
49     return _VSTD::__uninitialized_default_construct<_ValueType>(
50         _VSTD::move(__first), _VSTD::move(__last));
51   }
52 
53   template <__nothrow_forward_range _ForwardRange>
54     requires default_initializable<range_value_t<_ForwardRange>>
55   borrowed_iterator_t<_ForwardRange> operator()(_ForwardRange&& __range) const {
56     return (*this)(ranges::begin(__range), ranges::end(__range));
57   }
58 
59 };
60 
61 } // namespace __uninitialized_default_construct
62 
63 inline namespace __cpo {
64 inline constexpr auto uninitialized_default_construct =
65     __uninitialized_default_construct::__fn(__function_like::__tag());
66 } // namespace __cpo
67 
68 // uninitialized_default_construct_n
69 
70 namespace __uninitialized_default_construct_n {
71 
72 struct __fn final : private __function_like {
73 
74   constexpr explicit __fn(__tag __x) noexcept :
75       __function_like(__x) {}
76 
77   template <__nothrow_forward_iterator _ForwardIterator>
78     requires default_initializable<iter_value_t<_ForwardIterator>>
79   _ForwardIterator operator()(_ForwardIterator __first,
80                               iter_difference_t<_ForwardIterator> __n) const {
81     using _ValueType = remove_reference_t<iter_reference_t<_ForwardIterator>>;
82     return _VSTD::__uninitialized_default_construct_n<_ValueType>(_VSTD::move(__first), __n);
83   }
84 
85 };
86 
87 } // namespace __uninitialized_default_construct_n
88 
89 inline namespace __cpo {
90 inline constexpr auto uninitialized_default_construct_n =
91     __uninitialized_default_construct_n::__fn(__function_like::__tag());
92 } // namespace __cpo
93 
94 // uninitialized_value_construct
95 
96 namespace __uninitialized_value_construct {
97 
98 struct __fn final : private __function_like {
99 
100   constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {}
101 
102   template <__nothrow_forward_iterator _ForwardIterator,
103             __nothrow_sentinel_for<_ForwardIterator> _Sentinel>
104     requires default_initializable<iter_value_t<_ForwardIterator>>
105   _ForwardIterator operator()(_ForwardIterator __first, _Sentinel __last) const {
106     using _ValueType = remove_reference_t<iter_reference_t<_ForwardIterator>>;
107     return _VSTD::__uninitialized_value_construct<_ValueType>(
108         _VSTD::move(__first), _VSTD::move(__last));
109   }
110 
111   template <__nothrow_forward_range _ForwardRange>
112     requires default_initializable<range_value_t<_ForwardRange>>
113   borrowed_iterator_t<_ForwardRange> operator()(_ForwardRange&& __range) const {
114     return (*this)(ranges::begin(__range), ranges::end(__range));
115   }
116 
117 };
118 
119 } // namespace __uninitialized_value_construct
120 
121 inline namespace __cpo {
122 inline constexpr auto uninitialized_value_construct =
123     __uninitialized_value_construct::__fn(__function_like::__tag());
124 } // namespace __cpo
125 
126 // uninitialized_value_construct_n
127 
128 namespace __uninitialized_value_construct_n {
129 
130 struct __fn final : private __function_like {
131 
132   constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {}
133 
134   template <__nothrow_forward_iterator _ForwardIterator>
135     requires default_initializable<iter_value_t<_ForwardIterator>>
136   _ForwardIterator operator()(_ForwardIterator __first,
137                               iter_difference_t<_ForwardIterator> __n) const {
138     using _ValueType = remove_reference_t<iter_reference_t<_ForwardIterator>>;
139     return _VSTD::__uninitialized_value_construct_n<_ValueType>(_VSTD::move(__first), __n);
140   }
141 
142 };
143 
144 } // namespace __uninitialized_value_construct_n
145 
146 inline namespace __cpo {
147 inline constexpr auto uninitialized_value_construct_n =
148     __uninitialized_value_construct_n::__fn(__function_like::__tag());
149 } // namespace __cpo
150 
151 // uninitialized_fill
152 
153 namespace __uninitialized_fill {
154 
155 struct __fn final : private __function_like {
156 
157   constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {}
158 
159   template <__nothrow_forward_iterator _ForwardIterator,
160             __nothrow_sentinel_for<_ForwardIterator> _Sentinel,
161             class _Tp>
162     requires constructible_from<iter_value_t<_ForwardIterator>, const _Tp&>
163   _ForwardIterator operator()(_ForwardIterator __first, _Sentinel __last, const _Tp& __x) const {
164     using _ValueType = remove_reference_t<iter_reference_t<_ForwardIterator>>;
165     return _VSTD::__uninitialized_fill<_ValueType>(_VSTD::move(__first), _VSTD::move(__last), __x);
166   }
167 
168   template <__nothrow_forward_range _ForwardRange, class _Tp>
169     requires constructible_from<range_value_t<_ForwardRange>, const _Tp&>
170   borrowed_iterator_t<_ForwardRange> operator()(_ForwardRange&& __range, const _Tp& __x) const {
171     return (*this)(ranges::begin(__range), ranges::end(__range), __x);
172   }
173 
174 };
175 
176 } // namespace __uninitialized_fil
177 
178 inline namespace __cpo {
179 inline constexpr auto uninitialized_fill = __uninitialized_fill::__fn(__function_like::__tag());
180 } // namespace __cpo
181 
182 // uninitialized_fill_n
183 
184 namespace __uninitialized_fill_n {
185 
186 struct __fn final : private __function_like {
187 
188   constexpr explicit __fn(__tag __x) noexcept : __function_like(__x) {}
189 
190   template <__nothrow_forward_iterator _ForwardIterator, class _Tp>
191     requires constructible_from<iter_value_t<_ForwardIterator>, const _Tp&>
192   _ForwardIterator operator()(_ForwardIterator __first,
193                               iter_difference_t<_ForwardIterator> __n,
194                               const _Tp& __x) const {
195     using _ValueType = remove_reference_t<iter_reference_t<_ForwardIterator>>;
196     return _VSTD::__uninitialized_fill_n<_ValueType>(_VSTD::move(__first), __n, __x);
197   }
198 
199 };
200 
201 } // namespace __uninitialized_fill_n
202 
203 inline namespace __cpo {
204 inline constexpr auto uninitialized_fill_n = __uninitialized_fill_n::__fn(__function_like::__tag());
205 } // namespace __cpo
206 
207 } // namespace ranges
208 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
209 
210 _LIBCPP_END_NAMESPACE_STD
211 
212 #endif // _LIBCPP___MEMORY_RANGES_UNINITIALIZED_ALGORITHMS_H
213