2 //===-------------------------- utility -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_UTILITY
12 #define _LIBCPP_UTILITY
26 template<class T> bool operator!=(const T&, const T&);
27 template<class T> bool operator> (const T&, const T&);
28 template<class T> bool operator<=(const T&, const T&);
29 template<class T> bool operator>=(const T&, const T&);
34 swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
35 is_nothrow_move_assignable<T>::value);
37 template <class T, size_t N>
39 swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
41 template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;
42 template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept;
44 template <class T> typename remove_reference<T>::type&& move(T&&) noexcept;
49 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
53 move_if_noexcept(T& x) noexcept;
55 template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
57 template <class T1, class T2>
60 typedef T1 first_type;
61 typedef T2 second_type;
66 pair(const pair&) = default;
67 pair(pair&&) = default;
69 pair(const T1& x, const T2& y);
70 template <class U, class V> pair(U&& x, V&& y);
71 template <class U, class V> pair(const pair<U, V>& p);
72 template <class U, class V> pair(pair<U, V>&& p);
73 template <class... Args1, class... Args2>
74 pair(piecewise_construct_t, tuple<Args1...> first_args,
75 tuple<Args2...> second_args);
77 template <class U, class V> pair& operator=(const pair<U, V>& p);
78 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
79 is_nothrow_move_assignable<T2>::value);
80 template <class U, class V> pair& operator=(pair<U, V>&& p);
82 void swap(pair& p) noexcept(noexcept(swap(first, p.first)) &&
83 noexcept(swap(second, p.second)));
86 template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&);
87 template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&);
88 template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
89 template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
90 template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&);
91 template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&);
93 template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);
94 template <class T1, class T2>
96 swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
98 struct piecewise_construct_t { };
99 constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
101 template <class T> class tuple_size;
102 template <size_t I, class T> class tuple_element;
104 template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >;
105 template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >;
106 template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
108 template<size_t I, class T1, class T2>
109 typename tuple_element<I, std::pair<T1, T2> >::type&
110 get(std::pair<T1, T2>&) noexcept;
112 template<size_t I, class T1, class T2>
113 const typename const tuple_element<I, std::pair<T1, T2> >::type&
114 get(const std::pair<T1, T2>&) noexcept;
116 template<size_t I, class T1, class T2>
117 typename tuple_element<I, std::pair<T1, T2> >::type&&
118 get(std::pair<T1, T2>&&) noexcept;
126 #include <type_traits>
128 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
129 #pragma GCC system_header
132 _LIBCPP_BEGIN_NAMESPACE_STD
138 inline _LIBCPP_INLINE_VISIBILITY
140 operator!=(const _Tp& __x, const _Tp& __y)
142 return !(__x == __y);
146 inline _LIBCPP_INLINE_VISIBILITY
148 operator> (const _Tp& __x, const _Tp& __y)
154 inline _LIBCPP_INLINE_VISIBILITY
156 operator<=(const _Tp& __x, const _Tp& __y)
162 inline _LIBCPP_INLINE_VISIBILITY
164 operator>=(const _Tp& __x, const _Tp& __y)
173 template <class _ForwardIterator1, class _ForwardIterator2>
174 inline _LIBCPP_INLINE_VISIBILITY
176 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
178 for(; __first1 != __last1; ++__first1, ++__first2)
179 swap(*__first1, *__first2);
183 template<class _Tp, size_t _N>
184 inline _LIBCPP_INLINE_VISIBILITY
186 swap(_Tp (&__a)[_N], _Tp (&__b)[_N]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
188 _VSTD::swap_ranges(__a, __a + _N, __b);
192 inline _LIBCPP_INLINE_VISIBILITY
193 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
196 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
200 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
203 move_if_noexcept(_Tp& __x) _NOEXCEPT
205 return _VSTD::move(__x);
208 struct _LIBCPP_VISIBLE piecewise_construct_t { };
210 extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
212 template <class _T1, class _T2>
213 struct _LIBCPP_VISIBLE pair
215 typedef _T1 first_type;
216 typedef _T2 second_type;
221 // pair(const pair&) = default;
222 // pair(pair&&) = default;
224 _LIBCPP_INLINE_VISIBILITY pair() : first(), second() {}
226 _LIBCPP_INLINE_VISIBILITY pair(const _T1& __x, const _T2& __y)
227 : first(__x), second(__y) {}
229 template<class _U1, class _U2>
230 _LIBCPP_INLINE_VISIBILITY
231 pair(const pair<_U1, _U2>& __p
232 #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
233 ,typename enable_if<is_constructible<_T1, _U1>::value &&
234 is_constructible<_T2, _U2>::value>::type* = 0
237 : first(__p.first), second(__p.second) {}
239 _LIBCPP_INLINE_VISIBILITY
240 pair(const pair& __p)
241 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
242 is_nothrow_copy_constructible<second_type>::value)
248 _LIBCPP_INLINE_VISIBILITY
249 pair& operator=(const pair& __p)
250 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
251 is_nothrow_copy_assignable<second_type>::value)
258 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
260 template <class _U1, class _U2,
261 class = typename enable_if<is_constructible<first_type, _U1 >::value &&
262 is_constructible<second_type, _U2>::value>::type>
263 _LIBCPP_INLINE_VISIBILITY
264 pair(_U1&& __u1, _U2&& __u2)
265 : first(_VSTD::forward<_U1>(__u1)),
266 second(_VSTD::forward<_U2>(__u2))
269 template<class _U1, class _U2>
270 _LIBCPP_INLINE_VISIBILITY
271 pair(pair<_U1, _U2>&& __p,
272 typename enable_if<is_constructible<_T1, _U1>::value &&
273 is_constructible<_T2, _U2>::value>::type* = 0)
274 : first(_VSTD::forward<_U1>(__p.first)),
275 second(_VSTD::forward<_U2>(__p.second)) {}
277 _LIBCPP_INLINE_VISIBILITY
278 pair(pair&& __p) _NOEXCEPT_(is_nothrow_move_constructible<first_type>::value &&
279 is_nothrow_move_constructible<second_type>::value)
280 : first(_VSTD::forward<first_type>(__p.first)),
281 second(_VSTD::forward<second_type>(__p.second))
285 _LIBCPP_INLINE_VISIBILITY
287 operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
288 is_nothrow_move_assignable<second_type>::value)
290 first = _VSTD::forward<first_type>(__p.first);
291 second = _VSTD::forward<second_type>(__p.second);
295 #ifndef _LIBCPP_HAS_NO_VARIADICS
297 template<class _Tuple,
298 class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
299 _LIBCPP_INLINE_VISIBILITY
301 : first(_VSTD::forward<typename tuple_element<0,
302 typename __make_tuple_types<_Tuple>::type>::type>(get<0>(__p))),
303 second(_VSTD::forward<typename tuple_element<1,
304 typename __make_tuple_types<_Tuple>::type>::type>(get<1>(__p)))
309 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
310 _LIBCPP_INLINE_VISIBILITY
311 pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
312 tuple<_Args2...> __second_args)
313 : pair(__pc, __first_args, __second_args,
314 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
315 typename __make_tuple_indices<sizeof...(_Args2) >::type())
318 template <class _Tuple,
319 class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type>
320 _LIBCPP_INLINE_VISIBILITY
322 operator=(_Tuple&& __p)
324 typedef typename __make_tuple_types<_Tuple>::type _TupleRef;
325 typedef typename tuple_element<0, _TupleRef>::type _U0;
326 typedef typename tuple_element<1, _TupleRef>::type _U1;
327 first = _VSTD::forward<_U0>(_VSTD::get<0>(__p));
328 second = _VSTD::forward<_U1>(_VSTD::get<1>(__p));
332 #endif // _LIBCPP_HAS_NO_VARIADICS
334 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
335 _LIBCPP_INLINE_VISIBILITY
337 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
338 __is_nothrow_swappable<second_type>::value)
340 _VSTD::iter_swap(&first, &__p.first);
341 _VSTD::iter_swap(&second, &__p.second);
345 #ifndef _LIBCPP_HAS_NO_VARIADICS
346 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
347 _LIBCPP_INLINE_VISIBILITY
348 pair(piecewise_construct_t,
349 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
350 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
351 #endif // _LIBCPP_HAS_NO_VARIADICS
354 template <class _T1, class _T2>
355 inline _LIBCPP_INLINE_VISIBILITY
357 operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
359 return __x.first == __y.first && __x.second == __y.second;
362 template <class _T1, class _T2>
363 inline _LIBCPP_INLINE_VISIBILITY
365 operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
367 return !(__x == __y);
370 template <class _T1, class _T2>
371 inline _LIBCPP_INLINE_VISIBILITY
373 operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
375 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
378 template <class _T1, class _T2>
379 inline _LIBCPP_INLINE_VISIBILITY
381 operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
386 template <class _T1, class _T2>
387 inline _LIBCPP_INLINE_VISIBILITY
389 operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
394 template <class _T1, class _T2>
395 inline _LIBCPP_INLINE_VISIBILITY
397 operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
402 template <class _T1, class _T2>
403 inline _LIBCPP_INLINE_VISIBILITY
406 __is_swappable<_T1>::value &&
407 __is_swappable<_T2>::value,
410 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
411 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
412 __is_nothrow_swappable<_T2>::value))
417 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
419 template <class _Tp> class reference_wrapper;
422 struct ___make_pair_return
428 struct ___make_pair_return<reference_wrapper<_Tp>>
434 struct __make_pair_return
436 typedef typename ___make_pair_return<typename decay<_Tp>::type>::type type;
439 template <class _T1, class _T2>
440 inline _LIBCPP_INLINE_VISIBILITY
441 pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
442 make_pair(_T1&& __t1, _T2&& __t2)
444 return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
445 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
448 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
450 template <class _T1, class _T2>
451 inline _LIBCPP_INLINE_VISIBILITY
453 make_pair(_T1 __x, _T2 __y)
455 return pair<_T1, _T2>(__x, __y);
458 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
460 #ifndef _LIBCPP_HAS_NO_VARIADICS
462 template <class _T1, class _T2>
463 class _LIBCPP_VISIBLE tuple_size<pair<_T1, _T2> >
464 : public integral_constant<size_t, 2> {};
466 template <class _T1, class _T2>
467 class _LIBCPP_VISIBLE tuple_size<const pair<_T1, _T2> >
468 : public integral_constant<size_t, 2> {};
470 template <class _T1, class _T2>
471 class _LIBCPP_VISIBLE tuple_element<0, pair<_T1, _T2> >
477 template <class _T1, class _T2>
478 class _LIBCPP_VISIBLE tuple_element<1, pair<_T1, _T2> >
484 template <class _T1, class _T2>
485 class _LIBCPP_VISIBLE tuple_element<0, const pair<_T1, _T2> >
488 typedef const _T1 type;
491 template <class _T1, class _T2>
492 class _LIBCPP_VISIBLE tuple_element<1, const pair<_T1, _T2> >
495 typedef const _T2 type;
498 template <size_t _Ip> struct __get_pair;
503 template <class _T1, class _T2>
505 _LIBCPP_INLINE_VISIBILITY
507 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
509 template <class _T1, class _T2>
511 _LIBCPP_INLINE_VISIBILITY
513 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
515 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
517 template <class _T1, class _T2>
519 _LIBCPP_INLINE_VISIBILITY
521 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
523 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
529 template <class _T1, class _T2>
531 _LIBCPP_INLINE_VISIBILITY
533 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
535 template <class _T1, class _T2>
537 _LIBCPP_INLINE_VISIBILITY
539 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
541 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
543 template <class _T1, class _T2>
545 _LIBCPP_INLINE_VISIBILITY
547 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
549 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
552 template <size_t _Ip, class _T1, class _T2>
553 _LIBCPP_INLINE_VISIBILITY inline
554 typename tuple_element<_Ip, pair<_T1, _T2> >::type&
555 get(pair<_T1, _T2>& __p) _NOEXCEPT
557 return __get_pair<_Ip>::get(__p);
560 template <size_t _Ip, class _T1, class _T2>
561 _LIBCPP_INLINE_VISIBILITY inline
562 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
563 get(const pair<_T1, _T2>& __p) _NOEXCEPT
565 return __get_pair<_Ip>::get(__p);
568 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
570 template <size_t _Ip, class _T1, class _T2>
571 _LIBCPP_INLINE_VISIBILITY inline
572 typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
573 get(pair<_T1, _T2>&& __p) _NOEXCEPT
575 return __get_pair<_Ip>::get(_VSTD::move(__p));
578 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
580 #endif // _LIBCPP_HAS_NO_VARIADICS
582 _LIBCPP_END_NAMESPACE_STD
584 #endif // _LIBCPP_UTILITY