MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
uninitialized.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_MEMORY_UNINITIALIZED_HPP__
2#define MSTL_CORE_MEMORY_UNINITIALIZED_HPP__
3
11
15
21
24
36template <typename Iterator1, typename Iterator2, enable_if_t<
37 is_trivially_copy_assignable_v<iter_value_t<Iterator1>>, int> = 0>
38MSTL_CONSTEXPR20 Iterator2 __uninitialized_copy_aux(Iterator1 first, Iterator1 last, Iterator2 result) {
39 return _MSTL copy(first, last, result);
40}
41
55template <typename Iterator1, typename Iterator2, enable_if_t<
56 !is_trivially_copy_assignable_v<iter_value_t<Iterator1>>, int> = 0>
57MSTL_CONSTEXPR20 Iterator2 __uninitialized_copy_aux(Iterator1 first, Iterator1 last, Iterator2 result) {
58 Iterator2 cur = result;
59 try {
60 for (; first != last; ++first, ++cur) {
61 _MSTL construct(&*cur, *first);
62 }
63 } catch (...) {
64 for (; result != cur; --cur) {
65 _MSTL destroy(&*cur);
66 }
67 throw_exception(memory_exception("uninitialized copy failed."));
68 }
69 return cur;
70}
71
74
88template <typename Iterator1, typename Iterator2, enable_if_t<is_ranges_fwd_iter_v<Iterator2>, int> = 0>
89MSTL_CONSTEXPR20 Iterator2 uninitialized_copy(Iterator1 first, Iterator1 last, Iterator2 result) {
90 if (first == last) return result;
91 return _INNER __uninitialized_copy_aux(first, last, result);
92}
93
96
107template <typename Iterator1, typename Iterator2, enable_if_t<!is_ranges_rnd_iter_v<Iterator1>, int> = 0>
108MSTL_CONSTEXPR20 pair<Iterator1, Iterator2> __uninitialized_copy_n_aux(
109 Iterator1 first, size_t count, Iterator2 result) {
110 Iterator2 cur = result;
111 try {
112 for (; count > 0; --count, ++first, ++cur) {
113 _MSTL construct(&*cur, *first);
114 }
115 } catch (...) {
116 for (; result != cur; --cur) {
117 _MSTL destroy(&*cur);
118 }
119 throw_exception(memory_exception("uninitialized copy n failed."));
120 }
121 return pair<Iterator1, Iterator2>(first, cur);
122}
123
133template <typename Iterator1, typename Iterator2, enable_if_t<is_ranges_rnd_iter_v<Iterator1>, int> = 0>
134MSTL_CONSTEXPR20 pair<Iterator1, Iterator2> __uninitialized_copy_n_aux(
135 Iterator1 first, size_t count, Iterator2 result) {
136 Iterator1 last = first + count;
137 return _MSTL make_pair(last, _MSTL uninitialized_copy(first, last, result));
138}
139
142
155template <typename Iterator1, typename Iterator2, enable_if_t<is_ranges_fwd_iter_v<Iterator2>, int> = 0>
157 Iterator1 first, size_t count, Iterator2 result) {
158 return _INNER __uninitialized_copy_n_aux(first, count, result);
159}
160
163
172template <typename Iterator, typename T, enable_if_t<
173 is_trivially_copy_assignable_v<iter_value_t<Iterator>>, int> = 0>
174MSTL_CONSTEXPR20 void __uninitialized_fill_aux(Iterator first, Iterator last, const T& x) {
175 _MSTL fill(first, last, x);
176}
177
187template <typename Iterator, typename T, enable_if_t<
188 !is_trivially_copy_assignable_v<iter_value_t<Iterator>>, int> = 0>
189MSTL_CONSTEXPR20 void __uninitialized_fill_aux(Iterator first, Iterator last, const T& x) {
190 Iterator cur = first;
191 try {
192 for (; cur != last; ++cur) {
193 _MSTL construct(&*cur, x);
194 }
195 } catch (...) {
196 for (; cur != first; --cur) {
197 _MSTL destroy(&*cur);
198 }
199 throw_exception(memory_exception("uninitialized fill failed."));
200 }
201}
202
205
218template <typename Iterator, typename T, enable_if_t<is_ranges_fwd_iter_v<Iterator>, int> = 0>
219MSTL_CONSTEXPR20 void uninitialized_fill(Iterator first, Iterator last, const T& x) {
220 if (first == last) return;
221 _INNER __uninitialized_fill_aux(first, last, x);
222}
223
226
236template <typename Iterator, typename T, enable_if_t<
237 is_trivially_copy_assignable_v<iter_value_t<Iterator>>, int> = 0>
238MSTL_CONSTEXPR20 Iterator __uninitialized_fill_n_aux(Iterator first, size_t n, const T& x) {
239 return _MSTL fill_n(first, n, x);
240}
241
252template <typename Iterator, typename T, enable_if_t<
253 !is_trivially_copy_assignable_v<iter_value_t<Iterator>>, int> = 0>
254MSTL_CONSTEXPR20 Iterator __uninitialized_fill_n_aux(Iterator first, size_t n, const T& x) {
255 Iterator cur = first;
256 try{
257 for (; n > 0; --n, ++cur) {
258 _MSTL construct(&*cur, x);
259 }
260 } catch (...) {
261 for (; cur != first; --cur) {
262 _MSTL destroy(&*cur);
263 }
264 throw_exception(memory_exception("uninitialized fill n failed."));
265 }
266 return cur;
267}
268
271
284template <typename Iterator, typename T, enable_if_t<is_ranges_fwd_iter_v<Iterator>, int> = 0>
285MSTL_CONSTEXPR20 Iterator uninitialized_fill_n(Iterator first, size_t n, const T& x) {
286 return _INNER __uninitialized_fill_n_aux(first, n, x);
287}
288
291
301template <typename Iterator1, typename Iterator2, enable_if_t<
302 is_trivially_copy_assignable_v<iter_value_t<Iterator1>>, int> = 0>
303MSTL_CONSTEXPR20 Iterator2 __uninitialized_move_aux(Iterator1 first, Iterator1 last, Iterator2 result) {
304 return _MSTL move(first, last, result);
305}
306
319template <typename Iterator1, typename Iterator2, enable_if_t<
320 !is_trivially_copy_assignable_v<iter_value_t<Iterator1>>, int> = 0>
321MSTL_CONSTEXPR20 Iterator2 __uninitialized_move_aux(Iterator1 first, Iterator1 last, Iterator2 result) {
322 Iterator2 cur = result;
323 try{
324 for (; first != last; ++first, ++cur) {
325 _MSTL construct(&*cur, _MSTL move(*first));
326 }
327 } catch (...) {
328 for (; result != cur; --cur) {
329 _MSTL destroy(&*cur);
330 }
331 throw_exception(memory_exception("uninitialized move failed."));
332 }
333 return cur;
334}
337
351template <typename Iterator1, typename Iterator2, enable_if_t<
353MSTL_CONSTEXPR20 Iterator2 uninitialized_move(Iterator1 first, Iterator1 last, Iterator2 result) {
354 if (first == last) return result;
355 return _INNER __uninitialized_move_aux(first, last, result);
356}
357
360
371template <typename Iterator1, typename Iterator2, enable_if_t<!is_ranges_rnd_iter_v<Iterator1>, int> = 0>
372MSTL_CONSTEXPR20 pair<Iterator1, Iterator2> __uninitialized_move_n_aux(
373 Iterator1 first, size_t count, Iterator2 result) {
374 Iterator2 cur = result;
375 try{
376 for (; count > 0; --count, ++first, ++cur) {
377 _MSTL construct(&*cur, _MSTL move(*first));
378 }
379 } catch (...) {
380 for (; result != cur; --cur) {
381 _MSTL destroy(&*cur);
382 }
383 throw_exception(memory_exception("uninitialized move n failed."));
384 }
385 return pair<Iterator1, Iterator2>(first, cur);
386}
387
397template <typename Iterator1, typename Iterator2, enable_if_t<is_ranges_rnd_iter_v<Iterator1>, int> = 0>
398MSTL_CONSTEXPR20 pair<Iterator1, Iterator2> __uninitialized_move_n_aux(
399 Iterator1 first, size_t count, Iterator2 result) {
400 Iterator1 last = first + count;
401 return _MSTL make_pair(last, _MSTL uninitialized_move(first, last, result));
402}
403
406
417template <typename Iterator1, typename Iterator2, enable_if_t<
420 Iterator1 first, size_t count, Iterator2 result) {
421 return _INNER __uninitialized_move_n_aux(first, count, result);
422}
423 // UninitializedAlgorithms
425
427#endif // MSTL_CORE_MEMORY_UNINITIALIZED_HPP__
MSTL内存构造和销毁函数
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
MSTL_CONSTEXPR20 enable_if_t< is_constructible_v< T, Args... >, void * > construct(T *ptr, Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
在指定内存位置构造对象
MSTL_CONSTEXPR20 void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
MSTL_INLINE17 constexpr bool is_ranges_fwd_iter_v
检查是否为范围前向迭代器
MSTL_INLINE17 constexpr bool is_ranges_input_iter_v
检查是否为范围输入迭代器
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
constexpr pair< unwrap_ref_decay_t< T1 >, unwrap_ref_decay_t< T2 > > make_pair(T1 &&x, T2 &&y) noexcept(conjunction< is_nothrow_constructible< unwrap_ref_decay_t< T1 >, T1 >, is_nothrow_constructible< unwrap_ref_decay_t< T2 >, T2 > >::value)
创建pair的辅助函数
constexpr void fill(Iterator first, Iterator last, const T &value)
填充范围元素
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
constexpr Iterator2 copy(Iterator1 first, Iterator1 last, Iterator2 result)
复制范围元素
constexpr Iterator fill_n(Iterator first, size_t n, const T &value)
填充指定数量的元素
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL_CONSTEXPR20 pair< Iterator1, Iterator2 > uninitialized_copy_n(Iterator1 first, size_t count, Iterator2 result)
复制指定数量的元素到未初始化内存
MSTL_CONSTEXPR20 Iterator2 uninitialized_move(Iterator1 first, Iterator1 last, Iterator2 result)
在未初始化内存中移动元素
MSTL_CONSTEXPR20 Iterator uninitialized_fill_n(Iterator first, size_t n, const T &x)
在未初始化内存中用指定值填充指定数量的元素
MSTL_CONSTEXPR20 pair< Iterator1, Iterator2 > uninitialized_move_n(Iterator1 first, size_t count, Iterator2 result)
在未初始化内存中移动指定数量的元素
MSTL_CONSTEXPR20 void uninitialized_fill(Iterator first, Iterator last, const T &x)
在未初始化内存上填充值
MSTL_CONSTEXPR20 Iterator2 uninitialized_copy(Iterator1 first, Iterator1 last, Iterator2 result)
复制元素到未初始化内存
MSTL移位和修改算法
内存操作异常
存储两个值的元组对