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;
122 template<class T, T... I>
123 struct integer_sequence
125 typedef T value_type;
127 static constexpr size_t size() noexcept;
130 template<size_t... I>
131 using index_sequence = integer_sequence<size_t, I...>;
133 template<class T, T N>
134 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
136 using make_index_sequence = make_integer_sequence<size_t, N>;
139 using index_sequence_for = make_index_sequence<sizeof...(T)>;
147 #include <type_traits>
149 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
150 #pragma GCC system_header
153 _LIBCPP_BEGIN_NAMESPACE_STD
159 inline _LIBCPP_INLINE_VISIBILITY
161 operator!=(const _Tp& __x, const _Tp& __y)
163 return !(__x == __y);
167 inline _LIBCPP_INLINE_VISIBILITY
169 operator> (const _Tp& __x, const _Tp& __y)
175 inline _LIBCPP_INLINE_VISIBILITY
177 operator<=(const _Tp& __x, const _Tp& __y)
183 inline _LIBCPP_INLINE_VISIBILITY
185 operator>=(const _Tp& __x, const _Tp& __y)
194 template <class _ForwardIterator1, class _ForwardIterator2>
195 inline _LIBCPP_INLINE_VISIBILITY
197 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
199 for(; __first1 != __last1; ++__first1, ++__first2)
200 swap(*__first1, *__first2);
204 template<class _Tp, size_t _Np>
205 inline _LIBCPP_INLINE_VISIBILITY
207 swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
209 _VSTD::swap_ranges(__a, __a + _Np, __b);
213 inline _LIBCPP_INLINE_VISIBILITY
214 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
217 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
221 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
224 move_if_noexcept(_Tp& __x) _NOEXCEPT
226 return _VSTD::move(__x);
229 struct _LIBCPP_TYPE_VIS piecewise_construct_t { };
230 #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY)
231 extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
233 constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
236 template <class _T1, class _T2>
237 struct _LIBCPP_TYPE_VIS pair
239 typedef _T1 first_type;
240 typedef _T2 second_type;
245 // pair(const pair&) = default;
246 // pair(pair&&) = default;
248 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR pair() : first(), second() {}
250 _LIBCPP_INLINE_VISIBILITY pair(const _T1& __x, const _T2& __y)
251 : first(__x), second(__y) {}
253 template<class _U1, class _U2>
254 _LIBCPP_INLINE_VISIBILITY
255 pair(const pair<_U1, _U2>& __p
256 #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
257 ,typename enable_if<is_convertible<const _U1&, _T1>::value &&
258 is_convertible<const _U2&, _T2>::value>::type* = 0
261 : first(__p.first), second(__p.second) {}
263 _LIBCPP_INLINE_VISIBILITY
264 pair(const pair& __p)
265 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
266 is_nothrow_copy_constructible<second_type>::value)
272 _LIBCPP_INLINE_VISIBILITY
273 pair& operator=(const pair& __p)
274 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
275 is_nothrow_copy_assignable<second_type>::value)
282 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
284 template <class _U1, class _U2,
285 class = typename enable_if<is_convertible<_U1, first_type>::value &&
286 is_convertible<_U2, second_type>::value>::type>
287 _LIBCPP_INLINE_VISIBILITY
288 pair(_U1&& __u1, _U2&& __u2)
289 : first(_VSTD::forward<_U1>(__u1)),
290 second(_VSTD::forward<_U2>(__u2))
293 template<class _U1, class _U2>
294 _LIBCPP_INLINE_VISIBILITY
295 pair(pair<_U1, _U2>&& __p,
296 typename enable_if<is_convertible<_U1, _T1>::value &&
297 is_convertible<_U2, _T2>::value>::type* = 0)
298 : first(_VSTD::forward<_U1>(__p.first)),
299 second(_VSTD::forward<_U2>(__p.second)) {}
301 _LIBCPP_INLINE_VISIBILITY
302 pair(pair&& __p) _NOEXCEPT_(is_nothrow_move_constructible<first_type>::value &&
303 is_nothrow_move_constructible<second_type>::value)
304 : first(_VSTD::forward<first_type>(__p.first)),
305 second(_VSTD::forward<second_type>(__p.second))
309 _LIBCPP_INLINE_VISIBILITY
311 operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
312 is_nothrow_move_assignable<second_type>::value)
314 first = _VSTD::forward<first_type>(__p.first);
315 second = _VSTD::forward<second_type>(__p.second);
319 #ifndef _LIBCPP_HAS_NO_VARIADICS
321 template<class _Tuple,
322 class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
323 _LIBCPP_INLINE_VISIBILITY
325 : first(_VSTD::forward<typename tuple_element<0,
326 typename __make_tuple_types<_Tuple>::type>::type>(get<0>(__p))),
327 second(_VSTD::forward<typename tuple_element<1,
328 typename __make_tuple_types<_Tuple>::type>::type>(get<1>(__p)))
333 template <class... _Args1, class... _Args2>
334 _LIBCPP_INLINE_VISIBILITY
335 pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
336 tuple<_Args2...> __second_args)
337 : pair(__pc, __first_args, __second_args,
338 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
339 typename __make_tuple_indices<sizeof...(_Args2) >::type())
342 template <class _Tuple,
343 class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type>
344 _LIBCPP_INLINE_VISIBILITY
346 operator=(_Tuple&& __p)
348 typedef typename __make_tuple_types<_Tuple>::type _TupleRef;
349 typedef typename tuple_element<0, _TupleRef>::type _U0;
350 typedef typename tuple_element<1, _TupleRef>::type _U1;
351 first = _VSTD::forward<_U0>(_VSTD::get<0>(__p));
352 second = _VSTD::forward<_U1>(_VSTD::get<1>(__p));
356 #endif // _LIBCPP_HAS_NO_VARIADICS
358 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
359 _LIBCPP_INLINE_VISIBILITY
361 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
362 __is_nothrow_swappable<second_type>::value)
364 _VSTD::iter_swap(&first, &__p.first);
365 _VSTD::iter_swap(&second, &__p.second);
369 #ifndef _LIBCPP_HAS_NO_VARIADICS
370 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
371 _LIBCPP_INLINE_VISIBILITY
372 pair(piecewise_construct_t,
373 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
374 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
375 #endif // _LIBCPP_HAS_NO_VARIADICS
378 template <class _T1, class _T2>
379 inline _LIBCPP_INLINE_VISIBILITY
381 operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
383 return __x.first == __y.first && __x.second == __y.second;
386 template <class _T1, class _T2>
387 inline _LIBCPP_INLINE_VISIBILITY
389 operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
391 return !(__x == __y);
394 template <class _T1, class _T2>
395 inline _LIBCPP_INLINE_VISIBILITY
397 operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
399 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
402 template <class _T1, class _T2>
403 inline _LIBCPP_INLINE_VISIBILITY
405 operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
410 template <class _T1, class _T2>
411 inline _LIBCPP_INLINE_VISIBILITY
413 operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
418 template <class _T1, class _T2>
419 inline _LIBCPP_INLINE_VISIBILITY
421 operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
426 template <class _T1, class _T2>
427 inline _LIBCPP_INLINE_VISIBILITY
430 __is_swappable<_T1>::value &&
431 __is_swappable<_T2>::value,
434 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
435 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
436 __is_nothrow_swappable<_T2>::value))
441 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
443 template <class _Tp> class _LIBCPP_TYPE_VIS reference_wrapper;
446 struct ___make_pair_return
452 struct ___make_pair_return<reference_wrapper<_Tp>>
458 struct __make_pair_return
460 typedef typename ___make_pair_return<typename decay<_Tp>::type>::type type;
463 template <class _T1, class _T2>
464 inline _LIBCPP_INLINE_VISIBILITY
465 pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
466 make_pair(_T1&& __t1, _T2&& __t2)
468 return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
469 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
472 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
474 template <class _T1, class _T2>
475 inline _LIBCPP_INLINE_VISIBILITY
477 make_pair(_T1 __x, _T2 __y)
479 return pair<_T1, _T2>(__x, __y);
482 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
484 template <class _T1, class _T2>
485 class _LIBCPP_TYPE_VIS tuple_size<pair<_T1, _T2> >
486 : public integral_constant<size_t, 2> {};
488 template <class _T1, class _T2>
489 class _LIBCPP_TYPE_VIS tuple_size<const pair<_T1, _T2> >
490 : public integral_constant<size_t, 2> {};
492 template <class _T1, class _T2>
493 class _LIBCPP_TYPE_VIS tuple_element<0, pair<_T1, _T2> >
499 template <class _T1, class _T2>
500 class _LIBCPP_TYPE_VIS tuple_element<1, pair<_T1, _T2> >
506 template <class _T1, class _T2>
507 class _LIBCPP_TYPE_VIS tuple_element<0, const pair<_T1, _T2> >
510 typedef const _T1 type;
513 template <class _T1, class _T2>
514 class _LIBCPP_TYPE_VIS tuple_element<1, const pair<_T1, _T2> >
517 typedef const _T2 type;
520 template <size_t _Ip> struct __get_pair;
525 template <class _T1, class _T2>
527 _LIBCPP_INLINE_VISIBILITY
529 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
531 template <class _T1, class _T2>
533 _LIBCPP_INLINE_VISIBILITY
535 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
537 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
539 template <class _T1, class _T2>
541 _LIBCPP_INLINE_VISIBILITY
543 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
545 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
551 template <class _T1, class _T2>
553 _LIBCPP_INLINE_VISIBILITY
555 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
557 template <class _T1, class _T2>
559 _LIBCPP_INLINE_VISIBILITY
561 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
563 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
565 template <class _T1, class _T2>
567 _LIBCPP_INLINE_VISIBILITY
569 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
571 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
574 template <size_t _Ip, class _T1, class _T2>
575 _LIBCPP_INLINE_VISIBILITY inline
576 typename tuple_element<_Ip, pair<_T1, _T2> >::type&
577 get(pair<_T1, _T2>& __p) _NOEXCEPT
579 return __get_pair<_Ip>::get(__p);
582 template <size_t _Ip, class _T1, class _T2>
583 _LIBCPP_INLINE_VISIBILITY inline
584 const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
585 get(const pair<_T1, _T2>& __p) _NOEXCEPT
587 return __get_pair<_Ip>::get(__p);
590 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
592 template <size_t _Ip, class _T1, class _T2>
593 _LIBCPP_INLINE_VISIBILITY inline
594 typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
595 get(pair<_T1, _T2>&& __p) _NOEXCEPT
597 return __get_pair<_Ip>::get(_VSTD::move(__p));
600 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
602 #if _LIBCPP_STD_VER > 11
604 template<class _Tp, _Tp... _Ip>
605 struct _LIBCPP_TYPE_VIS integer_sequence
607 typedef _Tp value_type;
608 static_assert( is_integral<_Tp>::value,
609 "std::integer_sequence can only be instantiated with an integral type" );
611 _LIBCPP_INLINE_VISIBILITY
614 size() noexcept { return sizeof...(_Ip); }
617 template<size_t... _Ip>
618 using index_sequence = integer_sequence<size_t, _Ip...>;
622 template<typename _Tp, size_t ..._Extra> struct __repeat;
623 template<typename _Tp, _Tp ..._Np, size_t ..._Extra> struct __repeat<integer_sequence<_Tp, _Np...>, _Extra...> {
624 typedef integer_sequence<_Tp,
626 sizeof...(_Np) + _Np...,
627 2 * sizeof...(_Np) + _Np...,
628 3 * sizeof...(_Np) + _Np...,
629 4 * sizeof...(_Np) + _Np...,
630 5 * sizeof...(_Np) + _Np...,
631 6 * sizeof...(_Np) + _Np...,
632 7 * sizeof...(_Np) + _Np...,
636 template<size_t _Np> struct __parity;
637 template<size_t _Np> struct __make : __parity<_Np % 8>::template __pmake<_Np> {};
639 template<> struct __make<0> { typedef integer_sequence<size_t> type; };
640 template<> struct __make<1> { typedef integer_sequence<size_t, 0> type; };
641 template<> struct __make<2> { typedef integer_sequence<size_t, 0, 1> type; };
642 template<> struct __make<3> { typedef integer_sequence<size_t, 0, 1, 2> type; };
643 template<> struct __make<4> { typedef integer_sequence<size_t, 0, 1, 2, 3> type; };
644 template<> struct __make<5> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4> type; };
645 template<> struct __make<6> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5> type; };
646 template<> struct __make<7> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5, 6> type; };
648 template<> struct __parity<0> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type> {}; };
649 template<> struct __parity<1> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 1> {}; };
650 template<> struct __parity<2> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 2, _Np - 1> {}; };
651 template<> struct __parity<3> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 3, _Np - 2, _Np - 1> {}; };
652 template<> struct __parity<4> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
653 template<> struct __parity<5> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
654 template<> struct __parity<6> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
655 template<> struct __parity<7> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 7, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
657 template<typename _Tp, typename _Up> struct __convert {
658 template<typename> struct __result;
659 template<_Tp ..._Np> struct __result<integer_sequence<_Tp, _Np...> > { typedef integer_sequence<_Up, _Np...> type; };
661 template<typename _Tp> struct __convert<_Tp, _Tp> { template<typename _Up> struct __result { typedef _Up type; }; };
665 template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked =
666 typename __detail::__convert<size_t, _Tp>::template __result<typename __detail::__make<_Np>::type>::type;
668 template <class _Tp, _Tp _Ep>
669 struct __make_integer_sequence
671 static_assert(is_integral<_Tp>::value,
672 "std::make_integer_sequence can only be instantiated with an integral type" );
673 static_assert(0 <= _Ep, "std::make_integer_sequence input shall not be negative");
674 typedef __make_integer_sequence_unchecked<_Tp, _Ep> type;
677 template<class _Tp, _Tp _Np>
678 using make_integer_sequence = typename __make_integer_sequence<_Tp, _Np>::type;
681 using make_index_sequence = make_integer_sequence<size_t, _Np>;
683 template<class... _Tp>
684 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
686 #endif // _LIBCPP_STD_VER > 11
688 _LIBCPP_END_NAMESPACE_STD
690 #endif // _LIBCPP_UTILITY