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_UNINITIALIZED_ALGORITHMS_H
11 #define _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
12
13 #include <__config>
14 #include <__memory/addressof.h>
15 #include <__memory/construct_at.h>
16 #include <iterator>
17 #include <utility>
18
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
21 #endif
22
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25
26 _LIBCPP_BEGIN_NAMESPACE_STD
27
28 template <class _InputIterator, class _ForwardIterator>
29 _ForwardIterator
uninitialized_copy(_InputIterator __f,_InputIterator __l,_ForwardIterator __r)30 uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
31 {
32 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
33 #ifndef _LIBCPP_NO_EXCEPTIONS
34 _ForwardIterator __s = __r;
35 try
36 {
37 #endif
38 for (; __f != __l; ++__f, (void) ++__r)
39 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
40 #ifndef _LIBCPP_NO_EXCEPTIONS
41 }
42 catch (...)
43 {
44 for (; __s != __r; ++__s)
45 __s->~value_type();
46 throw;
47 }
48 #endif
49 return __r;
50 }
51
52 template <class _InputIterator, class _Size, class _ForwardIterator>
53 _ForwardIterator
uninitialized_copy_n(_InputIterator __f,_Size __n,_ForwardIterator __r)54 uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
55 {
56 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
57 #ifndef _LIBCPP_NO_EXCEPTIONS
58 _ForwardIterator __s = __r;
59 try
60 {
61 #endif
62 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
63 ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f);
64 #ifndef _LIBCPP_NO_EXCEPTIONS
65 }
66 catch (...)
67 {
68 for (; __s != __r; ++__s)
69 __s->~value_type();
70 throw;
71 }
72 #endif
73 return __r;
74 }
75
76 template <class _ForwardIterator, class _Tp>
77 void
uninitialized_fill(_ForwardIterator __f,_ForwardIterator __l,const _Tp & __x)78 uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
79 {
80 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
81 #ifndef _LIBCPP_NO_EXCEPTIONS
82 _ForwardIterator __s = __f;
83 try
84 {
85 #endif
86 for (; __f != __l; ++__f)
87 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
88 #ifndef _LIBCPP_NO_EXCEPTIONS
89 }
90 catch (...)
91 {
92 for (; __s != __f; ++__s)
93 __s->~value_type();
94 throw;
95 }
96 #endif
97 }
98
99 template <class _ForwardIterator, class _Size, class _Tp>
100 _ForwardIterator
uninitialized_fill_n(_ForwardIterator __f,_Size __n,const _Tp & __x)101 uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
102 {
103 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
104 #ifndef _LIBCPP_NO_EXCEPTIONS
105 _ForwardIterator __s = __f;
106 try
107 {
108 #endif
109 for (; __n > 0; ++__f, (void) --__n)
110 ::new ((void*)_VSTD::addressof(*__f)) value_type(__x);
111 #ifndef _LIBCPP_NO_EXCEPTIONS
112 }
113 catch (...)
114 {
115 for (; __s != __f; ++__s)
116 __s->~value_type();
117 throw;
118 }
119 #endif
120 return __f;
121 }
122
123 #if _LIBCPP_STD_VER > 14
124
125 template <class _ForwardIterator>
126 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
destroy(_ForwardIterator __first,_ForwardIterator __last)127 void destroy(_ForwardIterator __first, _ForwardIterator __last) {
128 for (; __first != __last; ++__first)
129 _VSTD::destroy_at(_VSTD::addressof(*__first));
130 }
131
132 template <class _ForwardIterator, class _Size>
133 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
destroy_n(_ForwardIterator __first,_Size __n)134 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
135 for (; __n > 0; (void)++__first, --__n)
136 _VSTD::destroy_at(_VSTD::addressof(*__first));
137 return __first;
138 }
139
140 template <class _ForwardIterator>
141 inline _LIBCPP_INLINE_VISIBILITY
uninitialized_default_construct(_ForwardIterator __first,_ForwardIterator __last)142 void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
143 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
144 auto __idx = __first;
145 #ifndef _LIBCPP_NO_EXCEPTIONS
146 try {
147 #endif
148 for (; __idx != __last; ++__idx)
149 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
150 #ifndef _LIBCPP_NO_EXCEPTIONS
151 } catch (...) {
152 _VSTD::destroy(__first, __idx);
153 throw;
154 }
155 #endif
156 }
157
158 template <class _ForwardIterator, class _Size>
159 inline _LIBCPP_INLINE_VISIBILITY
uninitialized_default_construct_n(_ForwardIterator __first,_Size __n)160 _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
161 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
162 auto __idx = __first;
163 #ifndef _LIBCPP_NO_EXCEPTIONS
164 try {
165 #endif
166 for (; __n > 0; (void)++__idx, --__n)
167 ::new ((void*)_VSTD::addressof(*__idx)) _Vt;
168 return __idx;
169 #ifndef _LIBCPP_NO_EXCEPTIONS
170 } catch (...) {
171 _VSTD::destroy(__first, __idx);
172 throw;
173 }
174 #endif
175 }
176
177
178 template <class _ForwardIterator>
179 inline _LIBCPP_INLINE_VISIBILITY
uninitialized_value_construct(_ForwardIterator __first,_ForwardIterator __last)180 void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
181 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
182 auto __idx = __first;
183 #ifndef _LIBCPP_NO_EXCEPTIONS
184 try {
185 #endif
186 for (; __idx != __last; ++__idx)
187 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
188 #ifndef _LIBCPP_NO_EXCEPTIONS
189 } catch (...) {
190 _VSTD::destroy(__first, __idx);
191 throw;
192 }
193 #endif
194 }
195
196 template <class _ForwardIterator, class _Size>
197 inline _LIBCPP_INLINE_VISIBILITY
uninitialized_value_construct_n(_ForwardIterator __first,_Size __n)198 _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
199 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
200 auto __idx = __first;
201 #ifndef _LIBCPP_NO_EXCEPTIONS
202 try {
203 #endif
204 for (; __n > 0; (void)++__idx, --__n)
205 ::new ((void*)_VSTD::addressof(*__idx)) _Vt();
206 return __idx;
207 #ifndef _LIBCPP_NO_EXCEPTIONS
208 } catch (...) {
209 _VSTD::destroy(__first, __idx);
210 throw;
211 }
212 #endif
213 }
214
215
216 template <class _InputIt, class _ForwardIt>
217 inline _LIBCPP_INLINE_VISIBILITY
uninitialized_move(_InputIt __first,_InputIt __last,_ForwardIt __first_res)218 _ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
219 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
220 auto __idx = __first_res;
221 #ifndef _LIBCPP_NO_EXCEPTIONS
222 try {
223 #endif
224 for (; __first != __last; (void)++__idx, ++__first)
225 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
226 return __idx;
227 #ifndef _LIBCPP_NO_EXCEPTIONS
228 } catch (...) {
229 _VSTD::destroy(__first_res, __idx);
230 throw;
231 }
232 #endif
233 }
234
235 template <class _InputIt, class _Size, class _ForwardIt>
236 inline _LIBCPP_INLINE_VISIBILITY
237 pair<_InputIt, _ForwardIt>
uninitialized_move_n(_InputIt __first,_Size __n,_ForwardIt __first_res)238 uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
239 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
240 auto __idx = __first_res;
241 #ifndef _LIBCPP_NO_EXCEPTIONS
242 try {
243 #endif
244 for (; __n > 0; ++__idx, (void)++__first, --__n)
245 ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first));
246 return {__first, __idx};
247 #ifndef _LIBCPP_NO_EXCEPTIONS
248 } catch (...) {
249 _VSTD::destroy(__first_res, __idx);
250 throw;
251 }
252 #endif
253 }
254
255 #endif // _LIBCPP_STD_VER > 14
256
257 _LIBCPP_END_NAMESPACE_STD
258
259 _LIBCPP_POP_MACROS
260
261 #endif // _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
262