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 | #ifndef _STL_PAIR_H |
57 | #define _STL_PAIR_H 1 |
58 | |
59 | #include <bits/move.h> |
60 | |
61 | #if __cplusplus >= 201103L |
62 | #include <type_traits> |
63 | #endif |
64 | |
65 | namespace std _GLIBCXX_VISIBILITY(default) |
66 | { |
67 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | #if __cplusplus >= 201103L |
75 | |
76 | struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; |
77 | |
78 | |
79 | _GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct = |
80 | piecewise_construct_t(); |
81 | |
82 | |
83 | template<typename...> |
84 | class tuple; |
85 | |
86 | template<std::size_t...> |
87 | struct _Index_tuple; |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | template <bool, typename _T1, typename _T2> |
95 | struct _PCC |
96 | { |
97 | template <typename _U1, typename _U2> |
98 | static constexpr bool _ConstructiblePair() |
99 | { |
100 | return __and_<is_constructible<_T1, const _U1&>, |
101 | is_constructible<_T2, const _U2&>>::value; |
102 | } |
103 | |
104 | template <typename _U1, typename _U2> |
105 | static constexpr bool _ImplicitlyConvertiblePair() |
106 | { |
107 | return __and_<is_convertible<const _U1&, _T1>, |
108 | is_convertible<const _U2&, _T2>>::value; |
109 | } |
110 | |
111 | template <typename _U1, typename _U2> |
112 | static constexpr bool _MoveConstructiblePair() |
113 | { |
114 | return __and_<is_constructible<_T1, _U1&&>, |
115 | is_constructible<_T2, _U2&&>>::value; |
116 | } |
117 | |
118 | template <typename _U1, typename _U2> |
119 | static constexpr bool _ImplicitlyMoveConvertiblePair() |
120 | { |
121 | return __and_<is_convertible<_U1&&, _T1>, |
122 | is_convertible<_U2&&, _T2>>::value; |
123 | } |
124 | |
125 | template <bool __implicit, typename _U1, typename _U2> |
126 | static constexpr bool _CopyMovePair() |
127 | { |
128 | using __do_converts = __and_<is_convertible<const _U1&, _T1>, |
129 | is_convertible<_U2&&, _T2>>; |
130 | using __converts = typename conditional<__implicit, |
131 | __do_converts, |
132 | __not_<__do_converts>>::type; |
133 | return __and_<is_constructible<_T1, const _U1&>, |
134 | is_constructible<_T2, _U2&&>, |
135 | __converts |
136 | >::value; |
137 | } |
138 | |
139 | template <bool __implicit, typename _U1, typename _U2> |
140 | static constexpr bool _MoveCopyPair() |
141 | { |
142 | using __do_converts = __and_<is_convertible<_U1&&, _T1>, |
143 | is_convertible<const _U2&, _T2>>; |
144 | using __converts = typename conditional<__implicit, |
145 | __do_converts, |
146 | __not_<__do_converts>>::type; |
147 | return __and_<is_constructible<_T1, _U1&&>, |
148 | is_constructible<_T2, const _U2&&>, |
149 | __converts |
150 | >::value; |
151 | } |
152 | }; |
153 | |
154 | template <typename _T1, typename _T2> |
155 | struct _PCC<false, _T1, _T2> |
156 | { |
157 | template <typename _U1, typename _U2> |
158 | static constexpr bool _ConstructiblePair() |
159 | { |
160 | return false; |
161 | } |
162 | |
163 | template <typename _U1, typename _U2> |
164 | static constexpr bool _ImplicitlyConvertiblePair() |
165 | { |
166 | return false; |
167 | } |
168 | |
169 | template <typename _U1, typename _U2> |
170 | static constexpr bool _MoveConstructiblePair() |
171 | { |
172 | return false; |
173 | } |
174 | |
175 | template <typename _U1, typename _U2> |
176 | static constexpr bool _ImplicitlyMoveConvertiblePair() |
177 | { |
178 | return false; |
179 | } |
180 | }; |
181 | |
182 | |
183 | |
184 | |
185 | struct __nonesuch_no_braces : std::__nonesuch { |
186 | explicit __nonesuch_no_braces(const __nonesuch&) = delete; |
187 | }; |
188 | #endif |
189 | |
190 | template<typename _U1, typename _U2> class __pair_base |
191 | { |
192 | #if __cplusplus >= 201103L |
193 | template<typename _T1, typename _T2> friend struct pair; |
194 | __pair_base() = default; |
195 | ~__pair_base() = default; |
196 | __pair_base(const __pair_base&) = default; |
197 | __pair_base& operator=(const __pair_base&) = delete; |
198 | #endif |
199 | }; |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | |
206 | |
207 | template<typename _T1, typename _T2> |
208 | struct pair |
209 | : private __pair_base<_T1, _T2> |
210 | { |
211 | typedef _T1 first_type; |
212 | typedef _T2 second_type; |
213 | |
214 | _T1 first; |
215 | _T2 second; |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | #if __cplusplus >= 201103L |
222 | template <typename _U1 = _T1, |
223 | typename _U2 = _T2, |
224 | typename enable_if<__and_< |
225 | __is_implicitly_default_constructible<_U1>, |
226 | __is_implicitly_default_constructible<_U2>> |
227 | ::value, bool>::type = true> |
228 | #endif |
229 | _GLIBCXX_CONSTEXPR pair() |
230 | : first(), second() { } |
231 | |
232 | #if __cplusplus >= 201103L |
233 | template <typename _U1 = _T1, |
234 | typename _U2 = _T2, |
235 | typename enable_if<__and_< |
236 | is_default_constructible<_U1>, |
237 | is_default_constructible<_U2>, |
238 | __not_< |
239 | __and_<__is_implicitly_default_constructible<_U1>, |
240 | __is_implicitly_default_constructible<_U2>>>> |
241 | ::value, bool>::type = false> |
242 | explicit constexpr pair() |
243 | : first(), second() { } |
244 | #endif |
245 | |
246 | |
247 | #if __cplusplus < 201103L |
248 | pair(const _T1& __a, const _T2& __b) |
249 | : first(__a), second(__b) { } |
250 | #else |
251 | |
252 | using _PCCP = _PCC<true, _T1, _T2>; |
253 | |
254 | template<typename _U1 = _T1, typename _U2=_T2, typename |
255 | enable_if<_PCCP::template |
256 | _ConstructiblePair<_U1, _U2>() |
257 | && _PCCP::template |
258 | _ImplicitlyConvertiblePair<_U1, _U2>(), |
259 | bool>::type=true> |
260 | constexpr pair(const _T1& __a, const _T2& __b) |
261 | : first(__a), second(__b) { } |
262 | |
263 | template<typename _U1 = _T1, typename _U2=_T2, typename |
264 | enable_if<_PCCP::template |
265 | _ConstructiblePair<_U1, _U2>() |
266 | && !_PCCP::template |
267 | _ImplicitlyConvertiblePair<_U1, _U2>(), |
268 | bool>::type=false> |
269 | explicit constexpr pair(const _T1& __a, const _T2& __b) |
270 | : first(__a), second(__b) { } |
271 | #endif |
272 | |
273 | |
274 | #if __cplusplus < 201103L |
275 | template<typename _U1, typename _U2> |
276 | pair(const pair<_U1, _U2>& __p) |
277 | : first(__p.first), second(__p.second) { } |
278 | #else |
279 | |
280 | template <typename _U1, typename _U2> |
281 | using _PCCFP = _PCC<!is_same<_T1, _U1>::value |
282 | || !is_same<_T2, _U2>::value, |
283 | _T1, _T2>; |
284 | |
285 | template<typename _U1, typename _U2, typename |
286 | enable_if<_PCCFP<_U1, _U2>::template |
287 | _ConstructiblePair<_U1, _U2>() |
288 | && _PCCFP<_U1, _U2>::template |
289 | _ImplicitlyConvertiblePair<_U1, _U2>(), |
290 | bool>::type=true> |
291 | constexpr pair(const pair<_U1, _U2>& __p) |
292 | : first(__p.first), second(__p.second) { } |
293 | |
294 | template<typename _U1, typename _U2, typename |
295 | enable_if<_PCCFP<_U1, _U2>::template |
296 | _ConstructiblePair<_U1, _U2>() |
297 | && !_PCCFP<_U1, _U2>::template |
298 | _ImplicitlyConvertiblePair<_U1, _U2>(), |
299 | bool>::type=false> |
300 | explicit constexpr pair(const pair<_U1, _U2>& __p) |
301 | : first(__p.first), second(__p.second) { } |
302 | |
303 | constexpr pair(const pair&) = default; |
304 | constexpr pair(pair&&) = default; |
305 | |
306 | |
307 | template<typename _U1, typename |
308 | enable_if<_PCCP::template |
309 | _MoveCopyPair<true, _U1, _T2>(), |
310 | bool>::type=true> |
311 | constexpr pair(_U1&& __x, const _T2& __y) |
312 | : first(std::forward<_U1>(__x)), second(__y) { } |
313 | |
314 | template<typename _U1, typename |
315 | enable_if<_PCCP::template |
316 | _MoveCopyPair<false, _U1, _T2>(), |
317 | bool>::type=false> |
318 | explicit constexpr pair(_U1&& __x, const _T2& __y) |
319 | : first(std::forward<_U1>(__x)), second(__y) { } |
320 | |
321 | template<typename _U2, typename |
322 | enable_if<_PCCP::template |
323 | _CopyMovePair<true, _T1, _U2>(), |
324 | bool>::type=true> |
325 | constexpr pair(const _T1& __x, _U2&& __y) |
326 | : first(__x), second(std::forward<_U2>(__y)) { } |
327 | |
328 | template<typename _U2, typename |
329 | enable_if<_PCCP::template |
330 | _CopyMovePair<false, _T1, _U2>(), |
331 | bool>::type=false> |
332 | explicit pair(const _T1& __x, _U2&& __y) |
333 | : first(__x), second(std::forward<_U2>(__y)) { } |
334 | |
335 | template<typename _U1, typename _U2, typename |
336 | enable_if<_PCCP::template |
337 | _MoveConstructiblePair<_U1, _U2>() |
338 | && _PCCP::template |
339 | _ImplicitlyMoveConvertiblePair<_U1, _U2>(), |
340 | bool>::type=true> |
341 | constexpr pair(_U1&& __x, _U2&& __y) |
342 | : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } |
343 | |
344 | template<typename _U1, typename _U2, typename |
345 | enable_if<_PCCP::template |
346 | _MoveConstructiblePair<_U1, _U2>() |
347 | && !_PCCP::template |
348 | _ImplicitlyMoveConvertiblePair<_U1, _U2>(), |
349 | bool>::type=false> |
350 | explicit constexpr pair(_U1&& __x, _U2&& __y) |
351 | : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } |
352 | |
353 | |
354 | template<typename _U1, typename _U2, typename |
355 | enable_if<_PCCFP<_U1, _U2>::template |
356 | _MoveConstructiblePair<_U1, _U2>() |
357 | && _PCCFP<_U1, _U2>::template |
358 | _ImplicitlyMoveConvertiblePair<_U1, _U2>(), |
359 | bool>::type=true> |
360 | constexpr pair(pair<_U1, _U2>&& __p) |
361 | : first(std::forward<_U1>(__p.first)), |
362 | second(std::forward<_U2>(__p.second)) { } |
363 | |
364 | template<typename _U1, typename _U2, typename |
365 | enable_if<_PCCFP<_U1, _U2>::template |
366 | _MoveConstructiblePair<_U1, _U2>() |
367 | && !_PCCFP<_U1, _U2>::template |
368 | _ImplicitlyMoveConvertiblePair<_U1, _U2>(), |
369 | bool>::type=false> |
370 | explicit constexpr pair(pair<_U1, _U2>&& __p) |
371 | : first(std::forward<_U1>(__p.first)), |
372 | second(std::forward<_U2>(__p.second)) { } |
373 | |
374 | template<typename... _Args1, typename... _Args2> |
375 | pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>); |
376 | |
377 | pair& |
378 | operator=(typename conditional< |
379 | __and_<is_copy_assignable<_T1>, |
380 | is_copy_assignable<_T2>>::value, |
381 | const pair&, const __nonesuch_no_braces&>::type __p) |
382 | { |
383 | first = __p.first; |
384 | second = __p.second; |
385 | return *this; |
386 | } |
387 | |
388 | pair& |
389 | operator=(typename conditional< |
390 | __and_<is_move_assignable<_T1>, |
391 | is_move_assignable<_T2>>::value, |
392 | pair&&, __nonesuch_no_braces&&>::type __p) |
393 | noexcept(__and_<is_nothrow_move_assignable<_T1>, |
394 | is_nothrow_move_assignable<_T2>>::value) |
395 | { |
396 | first = std::forward<first_type>(__p.first); |
397 | second = std::forward<second_type>(__p.second); |
398 | return *this; |
399 | } |
400 | |
401 | template<typename _U1, typename _U2> |
402 | typename enable_if<__and_<is_assignable<_T1&, const _U1&>, |
403 | is_assignable<_T2&, const _U2&>>::value, |
404 | pair&>::type |
405 | operator=(const pair<_U1, _U2>& __p) |
406 | { |
407 | first = __p.first; |
408 | second = __p.second; |
409 | return *this; |
410 | } |
411 | |
412 | template<typename _U1, typename _U2> |
413 | typename enable_if<__and_<is_assignable<_T1&, _U1&&>, |
414 | is_assignable<_T2&, _U2&&>>::value, |
415 | pair&>::type |
416 | operator=(pair<_U1, _U2>&& __p) |
417 | { |
418 | first = std::forward<_U1>(__p.first); |
419 | second = std::forward<_U2>(__p.second); |
420 | return *this; |
421 | } |
422 | |
423 | void |
424 | swap(pair& __p) |
425 | noexcept(__and_<__is_nothrow_swappable<_T1>, |
426 | __is_nothrow_swappable<_T2>>::value) |
427 | { |
428 | using std::swap; |
429 | swap(first, __p.first); |
430 | swap(second, __p.second); |
431 | } |
432 | |
433 | private: |
434 | template<typename... _Args1, std::size_t... _Indexes1, |
435 | typename... _Args2, std::size_t... _Indexes2> |
436 | pair(tuple<_Args1...>&, tuple<_Args2...>&, |
437 | _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>); |
438 | #endif |
439 | }; |
440 | |
441 | #if __cpp_deduction_guides >= 201606 |
442 | template<typename _T1, typename _T2> pair(_T1, _T2) -> pair<_T1, _T2>; |
443 | #endif |
444 | |
445 | |
446 | template<typename _T1, typename _T2> |
447 | inline _GLIBCXX_CONSTEXPR bool |
448 | operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
449 | { return __x.first == __y.first && __x.second == __y.second; } |
450 | |
451 | |
452 | template<typename _T1, typename _T2> |
453 | inline _GLIBCXX_CONSTEXPR bool |
454 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
455 | { return __x.first < __y.first |
456 | || (!(__y.first < __x.first) && __x.second < __y.second); } |
457 | |
458 | |
459 | template<typename _T1, typename _T2> |
460 | inline _GLIBCXX_CONSTEXPR bool |
461 | operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
462 | { return !(__x == __y); } |
463 | |
464 | |
465 | template<typename _T1, typename _T2> |
466 | inline _GLIBCXX_CONSTEXPR bool |
467 | operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
468 | { return __y < __x; } |
469 | |
470 | |
471 | template<typename _T1, typename _T2> |
472 | inline _GLIBCXX_CONSTEXPR bool |
473 | operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
474 | { return !(__y < __x); } |
475 | |
476 | |
477 | template<typename _T1, typename _T2> |
478 | inline _GLIBCXX_CONSTEXPR bool |
479 | operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
480 | { return !(__x < __y); } |
481 | |
482 | #if __cplusplus >= 201103L |
483 | |
484 | |
485 | |
486 | template<typename _T1, typename _T2> |
487 | inline |
488 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
489 | |
490 | typename enable_if<__and_<__is_swappable<_T1>, |
491 | __is_swappable<_T2>>::value>::type |
492 | #else |
493 | void |
494 | #endif |
495 | swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) |
496 | noexcept(noexcept(__x.swap(__y))) |
497 | { __x.swap(__y); } |
498 | |
499 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) |
500 | template<typename _T1, typename _T2> |
501 | typename enable_if<!__and_<__is_swappable<_T1>, |
502 | __is_swappable<_T2>>::value>::type |
503 | swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete; |
504 | #endif |
505 | #endif |
506 | |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | |
518 | |
519 | #if __cplusplus >= 201103L |
520 | |
521 | template<typename _T1, typename _T2> |
522 | constexpr pair<typename __decay_and_strip<_T1>::__type, |
523 | typename __decay_and_strip<_T2>::__type> |
524 | make_pair(_T1&& __x, _T2&& __y) |
525 | { |
526 | typedef typename __decay_and_strip<_T1>::__type __ds_type1; |
527 | typedef typename __decay_and_strip<_T2>::__type __ds_type2; |
528 | typedef pair<__ds_type1, __ds_type2> __pair_type; |
529 | return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y)); |
530 | } |
531 | #else |
532 | template<typename _T1, typename _T2> |
533 | inline pair<_T1, _T2> |
534 | make_pair(_T1 __x, _T2 __y) |
535 | { return pair<_T1, _T2>(__x, __y); } |
536 | #endif |
537 | |
538 | |
539 | |
540 | _GLIBCXX_END_NAMESPACE_VERSION |
541 | } |
542 | |
543 | #endif |
544 | |