-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuninitialized.h
255 lines (233 loc) · 8.25 KB
/
uninitialized.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#ifndef MYTINYSTL_UNINITIALIZED_H_
#define MYTINYSTL_UNINITIALIZED_H_
// 这个头文件用于对未初始化空间构造元素
#include "algobase.h"
#include "construct.h"
#include "iterator.h"
#include "type_traits.h"
#include "util.h"
namespace mystl
{
/*****************************************************************************************/
// uninitialized_copy
// 把 [first, last) 上的内容复制到以 result 为起始处的空间,返回复制结束的位置
/*****************************************************************************************/
template <class InputIter, class ForwardIter>
ForwardIter
unchecked_uninit_copy(InputIter first, InputIter last, ForwardIter result, std::true_type)
{
return mystl::copy(first, last, result);
}
template <class InputIter, class ForwardIter>
ForwardIter
unchecked_uninit_copy(InputIter first, InputIter last, ForwardIter result, std::false_type)
{
auto cur = result;
try
{
for (; first != last; ++first, ++cur)
{
mystl::construct(&*cur, *first);
}
}
catch (...)
{
for (; result != cur; ++result)
mystl::destroy(&*result);
}
return cur;
}
template <class InputIter, class ForwardIter>
ForwardIter uninitialized_copy(InputIter first, InputIter last, ForwardIter result)
{
return mystl::unchecked_uninit_copy(first, last, result,
std::is_trivially_copy_assignable<
typename iterator_traits<ForwardIter>::
value_type>{});
}
/*****************************************************************************************/
// uninitialized_copy_n
// 把 [first, first + n) 上的内容复制到以 result 为起始处的空间,返回复制结束的位置
/*****************************************************************************************/
template <class InputIter, class Size, class ForwardIter>
ForwardIter
unchecked_uninit_copy_n(InputIter first, Size n, ForwardIter result, std::true_type)
{
return mystl::copy_n(first, n, result).second;
}
template <class InputIter, class Size, class ForwardIter>
ForwardIter
unchecked_uninit_copy_n(InputIter first, Size n, ForwardIter result, std::false_type)
{
auto cur = result;
try
{
for (; n > 0; --n, ++cur, ++first)
{
mystl::construct(&*cur, *first);
}
}
catch (...)
{
for (; result != cur; ++result)
mystl::destroy(&*result);
}
return cur;
}
template <class InputIter, class Size, class ForwardIter>
ForwardIter uninitialized_copy_n(InputIter first, Size n, ForwardIter result)
{
return mystl::unchecked_uninit_copy_n(first, n, result,
std::is_trivially_copy_assignable<
typename iterator_traits<InputIter>::
value_type>{});
}
/*****************************************************************************************/
// uninitialized_fill
// 在 [first, last) 区间内填充元素值
/*****************************************************************************************/
template <class ForwardIter, class T>
void
unchecked_uninit_fill(ForwardIter first, ForwardIter last, const T& value, std::true_type)
{
mystl::fill(first, last, value);
}
template <class ForwardIter, class T>
void
unchecked_uninit_fill(ForwardIter first, ForwardIter last, const T& value, std::false_type)
{
auto cur = first;
try
{
for (; cur != last; ++cur)
{
mystl::construct(&*cur, value);
}
}
catch (...)
{
for (; first != cur; ++first)
mystl::destroy(&*first);
}
}
template <class ForwardIter, class T>
void uninitialized_fill(ForwardIter first, ForwardIter last, const T& value)
{
mystl::unchecked_uninit_fill(first, last, value,
std::is_trivially_copy_assignable<
typename iterator_traits<ForwardIter>::
value_type>{});
}
/*****************************************************************************************/
// uninitialized_fill_n
// 从 first 位置开始,填充 n 个元素值,返回填充结束的位置
/*****************************************************************************************/
template <class ForwardIter, class Size, class T>
ForwardIter
unchecked_uninit_fill_n(ForwardIter first, Size n, const T& value, std::true_type)
{
return mystl::fill_n(first, n, value);
}
template <class ForwardIter, class Size, class T>
ForwardIter
unchecked_uninit_fill_n(ForwardIter first, Size n, const T& value, std::false_type)
{
auto cur = first;
try
{
for (; n > 0; --n, ++cur)
{
mystl::construct(&*cur, value);
}
}
catch (...)
{
for (; first != cur; ++first)
mystl::destroy(&*first);
}
return cur;
}
template <class ForwardIter, class Size, class T>
ForwardIter uninitialized_fill_n(ForwardIter first, Size n, const T& value)
{
return mystl::unchecked_uninit_fill_n(first, n, value,
std::is_trivially_copy_assignable<
typename iterator_traits<ForwardIter>::
value_type>{});
}
/*****************************************************************************************/
// uninitialized_move
// 把[first, last)上的内容移动到以 result 为起始处的空间,返回移动结束的位置
/*****************************************************************************************/
template <class InputIter, class ForwardIter>
ForwardIter
unchecked_uninit_move(InputIter first, InputIter last, ForwardIter result, std::true_type)
{
return mystl::move(first, last, result);
}
template <class InputIter, class ForwardIter>
ForwardIter
unchecked_uninit_move(InputIter first, InputIter last, ForwardIter result, std::false_type)
{
ForwardIter cur = result;
try
{
for (; first != last; ++first, ++cur)
{
mystl::construct(&*cur, mystl::move(*first));
}
}
catch (...)
{
mystl::destroy(result, cur);
}
return cur;
}
template <class InputIter, class ForwardIter>
ForwardIter uninitialized_move(InputIter first, InputIter last, ForwardIter result)
{
return mystl::unchecked_uninit_move(first, last, result,
std::is_trivially_move_assignable<
typename iterator_traits<InputIter>::
value_type>{});
}
/*****************************************************************************************/
// uninitialized_move_n
// 把[first, first + n)上的内容移动到以 result 为起始处的空间,返回移动结束的位置
/*****************************************************************************************/
template <class InputIter, class Size, class ForwardIter>
ForwardIter
unchecked_uninit_move_n(InputIter first, Size n, ForwardIter result, std::true_type)
{
return mystl::move(first, first + n, result);
}
template <class InputIter, class Size, class ForwardIter>
ForwardIter
unchecked_uninit_move_n(InputIter first, Size n, ForwardIter result, std::false_type)
{
auto cur = result;
try
{
for (; n > 0; --n, ++first, ++cur)
{
mystl::construct(&*cur, mystl::move(*first));
}
}
catch (...)
{
for (; result != cur; ++result)
mystl::destroy(&*result);
throw;
}
return cur;
}
template <class InputIter, class Size, class ForwardIter>
ForwardIter uninitialized_move_n(InputIter first, Size n, ForwardIter result)
{
return mystl::unchecked_uninit_move_n(first, n, result,
std::is_trivially_move_assignable<
typename iterator_traits<InputIter>::
value_type>{});
}
} // namespace mystl
#endif // !MYTINYSTL_UNINITIALIZED_H_