-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
296 lines (253 loc) · 7.76 KB
/
util.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef MYTINYSTL_UTIL_H_
#define MYTINYSTL_UTIL_H_
// 这个文件包含一些通用工具,包括 move, forward, swap 等函数,以及 pair 等
#include <cstddef>
#include "type_traits.h"
namespace mystl
{
// move
template <class T>
typename std::remove_reference<T>::type&& move(T&& arg) noexcept
{
return static_cast<typename std::remove_reference<T>::type&&>(arg);
}
// forward
template <class T>
T&& forward(typename std::remove_reference<T>::type& arg) noexcept
{
return static_cast<T&&>(arg);
}
template <class T>
T&& forward(typename std::remove_reference<T>::type&& arg) noexcept
{
static_assert(!std::is_lvalue_reference<T>::value, "bad forward");
return static_cast<T&&>(arg);
}
// swap
template <class Tp>
void swap(Tp& lhs, Tp& rhs)
{
auto tmp(mystl::move(lhs));
lhs = mystl::move(rhs);
rhs = mystl::move(tmp);
}
template <class ForwardIter1, class ForwardIter2>
ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2)
{
for (; first1 != last1; ++first1, (void) ++first2)
mystl::swap(*first1, *first2);
return first2;
}
template <class Tp, size_t N>
void swap(Tp(&a)[N], Tp(&b)[N])
{
mystl::swap_range(a, a + N, b);
}
// --------------------------------------------------------------------------------------
// pair
// 结构体模板 : pair
// 两个模板参数分别表示两个数据的类型
// 用 first 和 second 来分别取出第一个数据和第二个数据
template <class Ty1, class Ty2>
struct pair
{
typedef Ty1 first_type;
typedef Ty2 second_type;
first_type first; // 保存第一个数据
second_type second; // 保存第二个数据
// default constructiable
template <class Other1 = Ty1, class Other2 = Ty2,
typename = typename std::enable_if<
std::is_default_constructible<Other1>::value &&
std::is_default_constructible<Other2>::value, void>::type>
constexpr pair()
: first(), second()
{
}
// implicit constructiable for this type
template <class U1 = Ty1, class U2 = Ty2,
typename std::enable_if<
std::is_copy_constructible<U1>::value &&
std::is_copy_constructible<U2>::value &&
std::is_convertible<const U1&, Ty1>::value &&
std::is_convertible<const U2&, Ty2>::value, int>::type = 0>
constexpr pair(const Ty1& a, const Ty2& b)
: first(a), second(b)
{
}
// explicit constructible for this type
template <class U1 = Ty1, class U2 = Ty2,
typename std::enable_if<
std::is_copy_constructible<U1>::value &&
std::is_copy_constructible<U2>::value &&
(!std::is_convertible<const U1&, Ty1>::value ||
!std::is_convertible<const U2&, Ty2>::value), int>::type = 0>
explicit constexpr pair(const Ty1& a, const Ty2& b)
: first(a), second(b)
{
}
pair(const pair& rhs) = default;
pair(pair&& rhs) = default;
// implicit constructiable for other type
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, Other1>::value &&
std::is_constructible<Ty2, Other2>::value &&
std::is_convertible<Other1&&, Ty1>::value &&
std::is_convertible<Other2&&, Ty2>::value, int>::type = 0>
constexpr pair(Other1&& a, Other2&& b)
: first(mystl::forward<Other1>(a)),
second(mystl::forward<Other2>(b))
{
}
// explicit constructiable for other type
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, Other1>::value &&
std::is_constructible<Ty2, Other2>::value &&
(!std::is_convertible<Other1, Ty1>::value ||
!std::is_convertible<Other2, Ty2>::value), int>::type = 0>
explicit constexpr pair(Other1&& a, Other2&& b)
: first(mystl::forward<Other1>(a)),
second(mystl::forward<Other2>(b))
{
}
// implicit constructiable for other pair
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, const Other1&>::value &&
std::is_constructible<Ty2, const Other2&>::value &&
std::is_convertible<const Other1&, Ty1>::value &&
std::is_convertible<const Other2&, Ty2>::value, int>::type = 0>
constexpr pair(const pair<Other1, Other2>& other)
: first(other.first),
second(other.second)
{
}
// explicit constructiable for other pair
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, const Other1&>::value &&
std::is_constructible<Ty2, const Other2&>::value &&
(!std::is_convertible<const Other1&, Ty1>::value ||
!std::is_convertible<const Other2&, Ty2>::value), int>::type = 0>
explicit constexpr pair(const pair<Other1, Other2>& other)
: first(other.first),
second(other.second)
{
}
// implicit constructiable for other pair
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, Other1>::value &&
std::is_constructible<Ty2, Other2>::value &&
std::is_convertible<Other1, Ty1>::value &&
std::is_convertible<Other2, Ty2>::value, int>::type = 0>
constexpr pair(pair<Other1, Other2>&& other)
: first(mystl::forward<Other1>(other.first)),
second(mystl::forward<Other2>(other.second))
{
}
// explicit constructiable for other pair
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, Other1>::value &&
std::is_constructible<Ty2, Other2>::value &&
(!std::is_convertible<Other1, Ty1>::value ||
!std::is_convertible<Other2, Ty2>::value), int>::type = 0>
explicit constexpr pair(pair<Other1, Other2>&& other)
: first(mystl::forward<Other1>(other.first)),
second(mystl::forward<Other2>(other.second))
{
}
// copy assign for this pair
pair& operator=(const pair& rhs)
{
if (this != &rhs)
{
first = rhs.first;
second = rhs.second;
}
return *this;
}
// move assign for this pair
pair& operator=(pair&& rhs)
{
if (this != &rhs)
{
first = mystl::move(rhs.first);
second = mystl::move(rhs.second);
}
return *this;
}
// copy assign for other pair
template <class Other1, class Other2>
pair& operator=(const pair<Other1, Other2>& other)
{
first = other.first;
second = other.second;
return *this;
}
// move assign for other pair
template <class Other1, class Other2>
pair& operator=(pair<Other1, Other2>&& other)
{
first = mystl::forward<Other1>(other.first);
second = mystl::forward<Other2>(other.second);
return *this;
}
~pair() = default;
void swap(pair& other)
{
if (this != &other)
{
mystl::swap(first, other.first);
mystl::swap(second, other.second);
}
}
};
// 重载比较操作符
template <class Ty1, class Ty2>
bool operator==(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)
{
return lhs.first == rhs.first && lhs.second == rhs.second;
}
template <class Ty1, class Ty2>
bool operator<(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)
{
return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second);
}
template <class Ty1, class Ty2>
bool operator!=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)
{
return !(lhs == rhs);
}
template <class Ty1, class Ty2>
bool operator>(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)
{
return rhs < lhs;
}
template <class Ty1, class Ty2>
bool operator<=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)
{
return !(rhs < lhs);
}
template <class Ty1, class Ty2>
bool operator>=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)
{
return !(lhs < rhs);
}
// 重载 mystl 的 swap
template <class Ty1, class Ty2>
void swap(pair<Ty1, Ty2>& lhs, pair<Ty1, Ty2>& rhs)
{
lhs.swap(rhs);
}
// 全局函数,让两个数据成为一个 pair
template <class Ty1, class Ty2>
pair<Ty1, Ty2> make_pair(Ty1&& first, Ty2&& second)
{
return pair<Ty1, Ty2>(mystl::forward<Ty1>(first), mystl::forward<Ty2>(second));
}
}
#endif // !MYTINYSTL_UTIL_H_