xref: /freebsd-12.1/contrib/libc++/include/utility (revision b5893f02)
17a984708SDavid Chisnall// -*- C++ -*-
27a984708SDavid Chisnall//===-------------------------- utility -----------------------------------===//
37a984708SDavid Chisnall//
47a984708SDavid Chisnall//                     The LLVM Compiler Infrastructure
57a984708SDavid Chisnall//
67a984708SDavid Chisnall// This file is dual licensed under the MIT and the University of Illinois Open
77a984708SDavid Chisnall// Source Licenses. See LICENSE.TXT for details.
87a984708SDavid Chisnall//
97a984708SDavid Chisnall//===----------------------------------------------------------------------===//
107a984708SDavid Chisnall
117a984708SDavid Chisnall#ifndef _LIBCPP_UTILITY
127a984708SDavid Chisnall#define _LIBCPP_UTILITY
137a984708SDavid Chisnall
147a984708SDavid Chisnall/*
157a984708SDavid Chisnall    utility synopsis
167a984708SDavid Chisnall
174ba319b5SDimitry Andric#include <initializer_list>
184ba319b5SDimitry Andric
197a984708SDavid Chisnallnamespace std
207a984708SDavid Chisnall{
217a984708SDavid Chisnall
227a984708SDavid Chisnalltemplate <class T>
237a984708SDavid Chisnall    void
247a984708SDavid Chisnall    swap(T& a, T& b);
257a984708SDavid Chisnall
267a984708SDavid Chisnallnamespace rel_ops
277a984708SDavid Chisnall{
287a984708SDavid Chisnall    template<class T> bool operator!=(const T&, const T&);
297a984708SDavid Chisnall    template<class T> bool operator> (const T&, const T&);
307a984708SDavid Chisnall    template<class T> bool operator<=(const T&, const T&);
317a984708SDavid Chisnall    template<class T> bool operator>=(const T&, const T&);
327a984708SDavid Chisnall}
337a984708SDavid Chisnall
347a984708SDavid Chisnalltemplate<class T>
357a984708SDavid Chisnallvoid
367a984708SDavid Chisnallswap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
377a984708SDavid Chisnall                          is_nothrow_move_assignable<T>::value);
387a984708SDavid Chisnall
397a984708SDavid Chisnalltemplate <class T, size_t N>
407a984708SDavid Chisnallvoid
417a984708SDavid Chisnallswap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
427a984708SDavid Chisnall
434f7ab58eSDimitry Andrictemplate <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;  // constexpr in C++14
444f7ab58eSDimitry Andrictemplate <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
457a984708SDavid Chisnall
464f7ab58eSDimitry Andrictemplate <class T> typename remove_reference<T>::type&& move(T&&) noexcept;      // constexpr in C++14
477a984708SDavid Chisnall
487a984708SDavid Chisnalltemplate <class T>
497a984708SDavid Chisnall    typename conditional
507a984708SDavid Chisnall    <
517a984708SDavid Chisnall        !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
527a984708SDavid Chisnall        const T&,
537a984708SDavid Chisnall        T&&
547a984708SDavid Chisnall    >::type
554f7ab58eSDimitry Andric    move_if_noexcept(T& x) noexcept; // constexpr in C++14
567a984708SDavid Chisnall
574ba319b5SDimitry Andrictemplate <class T> constexpr add_const_t<T>& as_const(T& t) noexcept;      // C++17
589729cf09SDimitry Andrictemplate <class T>                      void as_const(const T&&) = delete; // C++17
599729cf09SDimitry Andric
607a984708SDavid Chisnalltemplate <class T> typename add_rvalue_reference<T>::type declval() noexcept;
617a984708SDavid Chisnall
627a984708SDavid Chisnalltemplate <class T1, class T2>
637a984708SDavid Chisnallstruct pair
647a984708SDavid Chisnall{
657a984708SDavid Chisnall    typedef T1 first_type;
667a984708SDavid Chisnall    typedef T2 second_type;
677a984708SDavid Chisnall
687a984708SDavid Chisnall    T1 first;
697a984708SDavid Chisnall    T2 second;
707a984708SDavid Chisnall
717a984708SDavid Chisnall    pair(const pair&) = default;
727a984708SDavid Chisnall    pair(pair&&) = default;
737a984708SDavid Chisnall    constexpr pair();
744f7ab58eSDimitry Andric    pair(const T1& x, const T2& y);                          // constexpr in C++14
754f7ab58eSDimitry Andric    template <class U, class V> pair(U&& x, V&& y);          // constexpr in C++14
764f7ab58eSDimitry Andric    template <class U, class V> pair(const pair<U, V>& p);   // constexpr in C++14
774f7ab58eSDimitry Andric    template <class U, class V> pair(pair<U, V>&& p);        // constexpr in C++14
787a984708SDavid Chisnall    template <class... Args1, class... Args2>
797a984708SDavid Chisnall        pair(piecewise_construct_t, tuple<Args1...> first_args,
807a984708SDavid Chisnall             tuple<Args2...> second_args);
817a984708SDavid Chisnall
827a984708SDavid Chisnall    template <class U, class V> pair& operator=(const pair<U, V>& p);
837a984708SDavid Chisnall    pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
847a984708SDavid Chisnall                                       is_nothrow_move_assignable<T2>::value);
857a984708SDavid Chisnall    template <class U, class V> pair& operator=(pair<U, V>&& p);
867a984708SDavid Chisnall
877c82a1ecSDimitry Andric    void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
887c82a1ecSDimitry Andric                                is_nothrow_swappable_v<T2>);
897a984708SDavid Chisnall};
907a984708SDavid Chisnall
914f7ab58eSDimitry Andrictemplate <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
924f7ab58eSDimitry Andrictemplate <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
934f7ab58eSDimitry Andrictemplate <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
944f7ab58eSDimitry Andrictemplate <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
954f7ab58eSDimitry Andrictemplate <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
964f7ab58eSDimitry Andrictemplate <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
977a984708SDavid Chisnall
984f7ab58eSDimitry Andrictemplate <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);   // constexpr in C++14
997a984708SDavid Chisnalltemplate <class T1, class T2>
1007a984708SDavid Chisnallvoid
1017a984708SDavid Chisnallswap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
1027a984708SDavid Chisnall
1037a984708SDavid Chisnallstruct piecewise_construct_t { };
10430785c0eSDimitry Andricinline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
1057a984708SDavid Chisnall
106*b5893f02SDimitry Andrictemplate <class T> struct tuple_size;
1077a984708SDavid Chisnalltemplate <size_t I, class T> class tuple_element;
1087a984708SDavid Chisnall
109d72607e9SDimitry Andrictemplate <class T1, class T2> struct tuple_size<pair<T1, T2> >;
110d72607e9SDimitry Andrictemplate <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
111d72607e9SDimitry Andrictemplate <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
1127a984708SDavid Chisnall
1137a984708SDavid Chisnalltemplate<size_t I, class T1, class T2>
114d72607e9SDimitry Andric    typename tuple_element<I, pair<T1, T2> >::type&
115d72607e9SDimitry Andric    get(pair<T1, T2>&) noexcept; // constexpr in C++14
1167a984708SDavid Chisnall
1177a984708SDavid Chisnalltemplate<size_t I, class T1, class T2>
1189729cf09SDimitry Andric    const typename tuple_element<I, pair<T1, T2> >::type&
119d72607e9SDimitry Andric    get(const pair<T1, T2>&) noexcept; // constexpr in C++14
1207a984708SDavid Chisnall
1217a984708SDavid Chisnalltemplate<size_t I, class T1, class T2>
122d72607e9SDimitry Andric    typename tuple_element<I, pair<T1, T2> >::type&&
123d72607e9SDimitry Andric    get(pair<T1, T2>&&) noexcept; // constexpr in C++14
1244f7ab58eSDimitry Andric
1259729cf09SDimitry Andrictemplate<size_t I, class T1, class T2>
1269729cf09SDimitry Andric    const typename tuple_element<I, pair<T1, T2> >::type&&
1279729cf09SDimitry Andric    get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
1289729cf09SDimitry Andric
1294f7ab58eSDimitry Andrictemplate<class T1, class T2>
130d72607e9SDimitry Andric    constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
1314f7ab58eSDimitry Andric
1329729cf09SDimitry Andrictemplate<class T1, class T2>
1339729cf09SDimitry Andric    constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
1344f7ab58eSDimitry Andric
1359729cf09SDimitry Andrictemplate<class T1, class T2>
136d72607e9SDimitry Andric    constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
1377a984708SDavid Chisnall
1389729cf09SDimitry Andrictemplate<class T1, class T2>
1399729cf09SDimitry Andric    constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
1409729cf09SDimitry Andric
1419729cf09SDimitry Andrictemplate<class T1, class T2>
1429729cf09SDimitry Andric    constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
1439729cf09SDimitry Andric
1449729cf09SDimitry Andrictemplate<class T1, class T2>
1459729cf09SDimitry Andric    constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
1469729cf09SDimitry Andric
1479729cf09SDimitry Andrictemplate<class T1, class T2>
1489729cf09SDimitry Andric    constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
1499729cf09SDimitry Andric
1509729cf09SDimitry Andrictemplate<class T1, class T2>
1519729cf09SDimitry Andric    constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
1529729cf09SDimitry Andric
1534bab9fd9SDavid Chisnall// C++14
1544bab9fd9SDavid Chisnall
1554bab9fd9SDavid Chisnalltemplate<class T, T... I>
1564bab9fd9SDavid Chisnallstruct integer_sequence
1574bab9fd9SDavid Chisnall{
1584bab9fd9SDavid Chisnall    typedef T value_type;
1594bab9fd9SDavid Chisnall
1604bab9fd9SDavid Chisnall    static constexpr size_t size() noexcept;
1614bab9fd9SDavid Chisnall};
1624bab9fd9SDavid Chisnall
1634bab9fd9SDavid Chisnalltemplate<size_t... I>
1644bab9fd9SDavid Chisnall  using index_sequence = integer_sequence<size_t, I...>;
1654bab9fd9SDavid Chisnall
1664bab9fd9SDavid Chisnalltemplate<class T, T N>
1674bab9fd9SDavid Chisnall  using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
1684bab9fd9SDavid Chisnalltemplate<size_t N>
1694bab9fd9SDavid Chisnall  using make_index_sequence = make_integer_sequence<size_t, N>;
1704bab9fd9SDavid Chisnall
1714bab9fd9SDavid Chisnalltemplate<class... T>
1724bab9fd9SDavid Chisnall  using index_sequence_for = make_index_sequence<sizeof...(T)>;
1734bab9fd9SDavid Chisnall
1744f7ab58eSDimitry Andrictemplate<class T, class U=T>
1754f7ab58eSDimitry Andric    T exchange(T& obj, U&& new_value);
176aed8d94eSDimitry Andric
177aed8d94eSDimitry Andric// 20.2.7, in-place construction // C++17
178aed8d94eSDimitry Andricstruct in_place_t {
179aed8d94eSDimitry Andric  explicit in_place_t() = default;
180aed8d94eSDimitry Andric};
181aed8d94eSDimitry Andricinline constexpr in_place_t in_place{};
182aed8d94eSDimitry Andrictemplate <class T>
183aed8d94eSDimitry Andric  struct in_place_type_t {
184aed8d94eSDimitry Andric    explicit in_place_type_t() = default;
185aed8d94eSDimitry Andric  };
186aed8d94eSDimitry Andrictemplate <class T>
187aed8d94eSDimitry Andric  inline constexpr in_place_type_t<T> in_place_type{};
188aed8d94eSDimitry Andrictemplate <size_t I>
189aed8d94eSDimitry Andric  struct in_place_index_t {
190aed8d94eSDimitry Andric    explicit in_place_index_t() = default;
191aed8d94eSDimitry Andric  };
192aed8d94eSDimitry Andrictemplate <size_t I>
193aed8d94eSDimitry Andric  inline constexpr in_place_index_t<I> in_place_index{};
194aed8d94eSDimitry Andric
1957a984708SDavid Chisnall}  // std
1967a984708SDavid Chisnall
1977a984708SDavid Chisnall*/
1987a984708SDavid Chisnall
1997a984708SDavid Chisnall#include <__config>
2007a984708SDavid Chisnall#include <__tuple>
2017a984708SDavid Chisnall#include <type_traits>
2027c82a1ecSDimitry Andric#include <initializer_list>
203540d2a8bSDimitry Andric#include <cstddef>
204540d2a8bSDimitry Andric#include <cstring>
205540d2a8bSDimitry Andric#include <cstdint>
206*b5893f02SDimitry Andric#include <version>
207aed8d94eSDimitry Andric#include <__debug>
2087a984708SDavid Chisnall
2097a984708SDavid Chisnall#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2107a984708SDavid Chisnall#pragma GCC system_header
2117a984708SDavid Chisnall#endif
2127a984708SDavid Chisnall
2137a984708SDavid Chisnall_LIBCPP_BEGIN_NAMESPACE_STD
2147a984708SDavid Chisnall
2157a984708SDavid Chisnallnamespace rel_ops
2167a984708SDavid Chisnall{
2177a984708SDavid Chisnall
2187a984708SDavid Chisnalltemplate<class _Tp>
2197a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
2207a984708SDavid Chisnallbool
2217a984708SDavid Chisnalloperator!=(const _Tp& __x, const _Tp& __y)
2227a984708SDavid Chisnall{
2237a984708SDavid Chisnall    return !(__x == __y);
2247a984708SDavid Chisnall}
2257a984708SDavid Chisnall
2267a984708SDavid Chisnalltemplate<class _Tp>
2277a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
2287a984708SDavid Chisnallbool
2297a984708SDavid Chisnalloperator> (const _Tp& __x, const _Tp& __y)
2307a984708SDavid Chisnall{
2317a984708SDavid Chisnall    return __y < __x;
2327a984708SDavid Chisnall}
2337a984708SDavid Chisnall
2347a984708SDavid Chisnalltemplate<class _Tp>
2357a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
2367a984708SDavid Chisnallbool
2377a984708SDavid Chisnalloperator<=(const _Tp& __x, const _Tp& __y)
2387a984708SDavid Chisnall{
2397a984708SDavid Chisnall    return !(__y < __x);
2407a984708SDavid Chisnall}
2417a984708SDavid Chisnall
2427a984708SDavid Chisnalltemplate<class _Tp>
2437a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
2447a984708SDavid Chisnallbool
2457a984708SDavid Chisnalloperator>=(const _Tp& __x, const _Tp& __y)
2467a984708SDavid Chisnall{
2477a984708SDavid Chisnall    return !(__x < __y);
2487a984708SDavid Chisnall}
2497a984708SDavid Chisnall
2507a984708SDavid Chisnall}  // rel_ops
2517a984708SDavid Chisnall
2527a984708SDavid Chisnall// swap_ranges
2537a984708SDavid Chisnall
254854fa44bSDimitry Andric
2557a984708SDavid Chisnalltemplate <class _ForwardIterator1, class _ForwardIterator2>
2567a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
2577a984708SDavid Chisnall_ForwardIterator2
2587a984708SDavid Chisnallswap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
2597a984708SDavid Chisnall{
260d72607e9SDimitry Andric    for(; __first1 != __last1; ++__first1, (void) ++__first2)
2617a984708SDavid Chisnall        swap(*__first1, *__first2);
2627a984708SDavid Chisnall    return __first2;
2637a984708SDavid Chisnall}
2647a984708SDavid Chisnall
2657c82a1ecSDimitry Andric// forward declared in <type_traits>
26694e3ee44SDavid Chisnalltemplate<class _Tp, size_t _Np>
2677a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
2687c82a1ecSDimitry Andrictypename enable_if<
2697c82a1ecSDimitry Andric    __is_swappable<_Tp>::value
2707c82a1ecSDimitry Andric>::type
27194e3ee44SDavid Chisnallswap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
2727a984708SDavid Chisnall{
27394e3ee44SDavid Chisnall    _VSTD::swap_ranges(__a, __a + _Np, __b);
2747a984708SDavid Chisnall}
2757a984708SDavid Chisnall
2767a984708SDavid Chisnalltemplate <class _Tp>
2774f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
278540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
2797a984708SDavid Chisnalltypename conditional
2807a984708SDavid Chisnall<
2817a984708SDavid Chisnall    !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
2827a984708SDavid Chisnall    const _Tp&,
2837a984708SDavid Chisnall    _Tp&&
2847a984708SDavid Chisnall>::type
285540d2a8bSDimitry Andric#else  // _LIBCPP_CXX03_LANG
2867a984708SDavid Chisnallconst _Tp&
2877a984708SDavid Chisnall#endif
2887a984708SDavid Chisnallmove_if_noexcept(_Tp& __x) _NOEXCEPT
2897a984708SDavid Chisnall{
2907a984708SDavid Chisnall    return _VSTD::move(__x);
2917a984708SDavid Chisnall}
2927a984708SDavid Chisnall
2939729cf09SDimitry Andric#if _LIBCPP_STD_VER > 14
2949729cf09SDimitry Andrictemplate <class _Tp> constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
2959729cf09SDimitry Andrictemplate <class _Tp>                        void as_const(const _Tp&&) = delete;
2969729cf09SDimitry Andric#endif
2979729cf09SDimitry Andric
298aed8d94eSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS piecewise_construct_t { };
2994ba319b5SDimitry Andric#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
300*b5893f02SDimitry Andricextern _LIBCPP_EXPORTED_FROM_ABI const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
301b03f91a8SDavid Chisnall#else
30230785c0eSDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
303b03f91a8SDavid Chisnall#endif
3047a984708SDavid Chisnall
305aed8d94eSDimitry Andric#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
306*b5893f02SDimitry Andrictemplate <class, class>
307aed8d94eSDimitry Andricstruct __non_trivially_copyable_base {
308aed8d94eSDimitry Andric  _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
309aed8d94eSDimitry Andric  __non_trivially_copyable_base() _NOEXCEPT {}
310aed8d94eSDimitry Andric  _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
311aed8d94eSDimitry Andric  __non_trivially_copyable_base(__non_trivially_copyable_base const&) _NOEXCEPT {}
312aed8d94eSDimitry Andric};
313aed8d94eSDimitry Andric#endif
314aed8d94eSDimitry Andric
3157a984708SDavid Chisnalltemplate <class _T1, class _T2>
316aed8d94eSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS pair
317aed8d94eSDimitry Andric#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
318*b5893f02SDimitry Andric: private __non_trivially_copyable_base<_T1, _T2>
319aed8d94eSDimitry Andric#endif
3207a984708SDavid Chisnall{
3217a984708SDavid Chisnall    typedef _T1 first_type;
3227a984708SDavid Chisnall    typedef _T2 second_type;
3237a984708SDavid Chisnall
3247a984708SDavid Chisnall    _T1 first;
3257a984708SDavid Chisnall    _T2 second;
3267a984708SDavid Chisnall
327aed8d94eSDimitry Andric#if !defined(_LIBCPP_CXX03_LANG)
3287c82a1ecSDimitry Andric    pair(pair const&) = default;
3297c82a1ecSDimitry Andric    pair(pair&&) = default;
3307c82a1ecSDimitry Andric#else
3317c82a1ecSDimitry Andric  // Use the implicitly declared copy constructor in C++03
3324f7ab58eSDimitry Andric#endif
3337a984708SDavid Chisnall
334aed8d94eSDimitry Andric#ifdef _LIBCPP_CXX03_LANG
3357a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
336aed8d94eSDimitry Andric    pair() : first(), second() {}
337aed8d94eSDimitry Andric
338aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
339aed8d94eSDimitry Andric    pair(_T1 const& __t1, _T2 const& __t2) : first(__t1), second(__t2) {}
340aed8d94eSDimitry Andric
341aed8d94eSDimitry Andric    template <class _U1, class _U2>
342aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
343aed8d94eSDimitry Andric    pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
344aed8d94eSDimitry Andric
345aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
346aed8d94eSDimitry Andric    pair& operator=(pair const& __p) {
347aed8d94eSDimitry Andric        first = __p.first;
348aed8d94eSDimitry Andric        second = __p.second;
349aed8d94eSDimitry Andric        return *this;
350aed8d94eSDimitry Andric    }
351aed8d94eSDimitry Andric#else
352aed8d94eSDimitry Andric    template <bool _Val>
353aed8d94eSDimitry Andric    using _EnableB = typename enable_if<_Val, bool>::type;
354aed8d94eSDimitry Andric
355aed8d94eSDimitry Andric    struct _CheckArgs {
356aed8d94eSDimitry Andric      template <class _U1, class _U2>
357aed8d94eSDimitry Andric      static constexpr bool __enable_default() {
358aed8d94eSDimitry Andric          return is_default_constructible<_U1>::value
359aed8d94eSDimitry Andric              && is_default_constructible<_U2>::value;
360aed8d94eSDimitry Andric      }
361aed8d94eSDimitry Andric
362aed8d94eSDimitry Andric      template <class _U1, class _U2>
363aed8d94eSDimitry Andric      static constexpr bool __enable_explicit() {
364aed8d94eSDimitry Andric          return is_constructible<first_type, _U1>::value
365aed8d94eSDimitry Andric              && is_constructible<second_type, _U2>::value
366aed8d94eSDimitry Andric              && (!is_convertible<_U1, first_type>::value
367aed8d94eSDimitry Andric                  || !is_convertible<_U2, second_type>::value);
368aed8d94eSDimitry Andric      }
369aed8d94eSDimitry Andric
370aed8d94eSDimitry Andric      template <class _U1, class _U2>
371aed8d94eSDimitry Andric      static constexpr bool __enable_implicit() {
372aed8d94eSDimitry Andric          return is_constructible<first_type, _U1>::value
373aed8d94eSDimitry Andric              && is_constructible<second_type, _U2>::value
374aed8d94eSDimitry Andric              && is_convertible<_U1, first_type>::value
375aed8d94eSDimitry Andric              && is_convertible<_U2, second_type>::value;
376aed8d94eSDimitry Andric      }
377aed8d94eSDimitry Andric    };
378aed8d94eSDimitry Andric
379aed8d94eSDimitry Andric    template <bool _MaybeEnable>
380aed8d94eSDimitry Andric    using _CheckArgsDep = typename conditional<
381aed8d94eSDimitry Andric      _MaybeEnable, _CheckArgs, __check_tuple_constructor_fail>::type;
382aed8d94eSDimitry Andric
383aed8d94eSDimitry Andric    struct _CheckTupleLikeConstructor {
384aed8d94eSDimitry Andric        template <class _Tuple>
385aed8d94eSDimitry Andric        static constexpr bool __enable_implicit() {
386aed8d94eSDimitry Andric            return __tuple_convertible<_Tuple, pair>::value;
387aed8d94eSDimitry Andric        }
388aed8d94eSDimitry Andric
389aed8d94eSDimitry Andric        template <class _Tuple>
390aed8d94eSDimitry Andric        static constexpr bool __enable_explicit() {
391aed8d94eSDimitry Andric            return __tuple_constructible<_Tuple, pair>::value
392aed8d94eSDimitry Andric               && !__tuple_convertible<_Tuple, pair>::value;
393aed8d94eSDimitry Andric        }
394aed8d94eSDimitry Andric
395aed8d94eSDimitry Andric        template <class _Tuple>
396aed8d94eSDimitry Andric        static constexpr bool __enable_assign() {
397aed8d94eSDimitry Andric            return __tuple_assignable<_Tuple, pair>::value;
398aed8d94eSDimitry Andric        }
399aed8d94eSDimitry Andric    };
400aed8d94eSDimitry Andric
401aed8d94eSDimitry Andric    template <class _Tuple>
402aed8d94eSDimitry Andric    using _CheckTLC = typename conditional<
403aed8d94eSDimitry Andric        __tuple_like_with_size<_Tuple, 2>::value
404aed8d94eSDimitry Andric            && !is_same<typename decay<_Tuple>::type, pair>::value,
405aed8d94eSDimitry Andric        _CheckTupleLikeConstructor,
406aed8d94eSDimitry Andric        __check_tuple_constructor_fail
407aed8d94eSDimitry Andric    >::type;
408aed8d94eSDimitry Andric
409aed8d94eSDimitry Andric    template<bool _Dummy = true, _EnableB<
410aed8d94eSDimitry Andric            _CheckArgsDep<_Dummy>::template __enable_default<_T1, _T2>()
411aed8d94eSDimitry Andric    > = false>
412aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
413*b5893f02SDimitry Andric    pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
414*b5893f02SDimitry Andric                      is_nothrow_default_constructible<second_type>::value)
415*b5893f02SDimitry Andric        : first(), second() {}
416aed8d94eSDimitry Andric
417aed8d94eSDimitry Andric    template <bool _Dummy = true, _EnableB<
418aed8d94eSDimitry Andric             _CheckArgsDep<_Dummy>::template __enable_explicit<_T1 const&, _T2 const&>()
419aed8d94eSDimitry Andric    > = false>
420aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
421aed8d94eSDimitry Andric    explicit pair(_T1 const& __t1, _T2 const& __t2)
422*b5893f02SDimitry Andric        _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
423*b5893f02SDimitry Andric                   is_nothrow_copy_constructible<second_type>::value)
424aed8d94eSDimitry Andric        : first(__t1), second(__t2) {}
425aed8d94eSDimitry Andric
426aed8d94eSDimitry Andric    template<bool _Dummy = true, _EnableB<
427aed8d94eSDimitry Andric            _CheckArgsDep<_Dummy>::template __enable_implicit<_T1 const&, _T2 const&>()
428aed8d94eSDimitry Andric    > = false>
429aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
430aed8d94eSDimitry Andric    pair(_T1 const& __t1, _T2 const& __t2)
431*b5893f02SDimitry Andric        _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
432*b5893f02SDimitry Andric                   is_nothrow_copy_constructible<second_type>::value)
433aed8d94eSDimitry Andric        : first(__t1), second(__t2) {}
434aed8d94eSDimitry Andric
435aed8d94eSDimitry Andric    template<class _U1, class _U2, _EnableB<
436aed8d94eSDimitry Andric             _CheckArgs::template __enable_explicit<_U1, _U2>()
437aed8d94eSDimitry Andric    > = false>
438aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
439aed8d94eSDimitry Andric    explicit pair(_U1&& __u1, _U2&& __u2)
440*b5893f02SDimitry Andric        _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
441*b5893f02SDimitry Andric                    is_nothrow_constructible<second_type, _U2>::value))
442aed8d94eSDimitry Andric        : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
443aed8d94eSDimitry Andric
444aed8d94eSDimitry Andric    template<class _U1, class _U2, _EnableB<
445aed8d94eSDimitry Andric            _CheckArgs::template __enable_implicit<_U1, _U2>()
446aed8d94eSDimitry Andric    > = false>
447aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
448aed8d94eSDimitry Andric    pair(_U1&& __u1, _U2&& __u2)
449*b5893f02SDimitry Andric        _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
450*b5893f02SDimitry Andric                    is_nothrow_constructible<second_type, _U2>::value))
451aed8d94eSDimitry Andric        : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
452aed8d94eSDimitry Andric
453aed8d94eSDimitry Andric    template<class _U1, class _U2, _EnableB<
454aed8d94eSDimitry Andric            _CheckArgs::template __enable_explicit<_U1 const&, _U2 const&>()
455aed8d94eSDimitry Andric    > = false>
456aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
457aed8d94eSDimitry Andric    explicit pair(pair<_U1, _U2> const& __p)
458*b5893f02SDimitry Andric        _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
459*b5893f02SDimitry Andric                    is_nothrow_constructible<second_type, _U2 const&>::value))
460aed8d94eSDimitry Andric        : first(__p.first), second(__p.second) {}
461aed8d94eSDimitry Andric
462aed8d94eSDimitry Andric    template<class _U1, class _U2, _EnableB<
463aed8d94eSDimitry Andric            _CheckArgs::template __enable_implicit<_U1 const&, _U2 const&>()
464aed8d94eSDimitry Andric    > = false>
465aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
466aed8d94eSDimitry Andric    pair(pair<_U1, _U2> const& __p)
467*b5893f02SDimitry Andric        _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
468*b5893f02SDimitry Andric                    is_nothrow_constructible<second_type, _U2 const&>::value))
469aed8d94eSDimitry Andric        : first(__p.first), second(__p.second) {}
470aed8d94eSDimitry Andric
471aed8d94eSDimitry Andric    template<class _U1, class _U2, _EnableB<
472aed8d94eSDimitry Andric            _CheckArgs::template __enable_explicit<_U1, _U2>()
473aed8d94eSDimitry Andric    > = false>
474aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
475aed8d94eSDimitry Andric    explicit pair(pair<_U1, _U2>&&__p)
476*b5893f02SDimitry Andric        _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
477*b5893f02SDimitry Andric                    is_nothrow_constructible<second_type, _U2&&>::value))
478aed8d94eSDimitry Andric        : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
479aed8d94eSDimitry Andric
480aed8d94eSDimitry Andric    template<class _U1, class _U2, _EnableB<
481aed8d94eSDimitry Andric            _CheckArgs::template __enable_implicit<_U1, _U2>()
482aed8d94eSDimitry Andric    > = false>
483aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
484aed8d94eSDimitry Andric    pair(pair<_U1, _U2>&& __p)
485*b5893f02SDimitry Andric        _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
486*b5893f02SDimitry Andric                    is_nothrow_constructible<second_type, _U2&&>::value))
487aed8d94eSDimitry Andric        : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
488aed8d94eSDimitry Andric
489aed8d94eSDimitry Andric    template<class _Tuple, _EnableB<
490aed8d94eSDimitry Andric            _CheckTLC<_Tuple>::template __enable_explicit<_Tuple>()
491aed8d94eSDimitry Andric    > = false>
492aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
493aed8d94eSDimitry Andric    explicit pair(_Tuple&& __p)
494aed8d94eSDimitry Andric        : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
495aed8d94eSDimitry Andric          second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
496aed8d94eSDimitry Andric
497aed8d94eSDimitry Andric    template<class _Tuple, _EnableB<
498aed8d94eSDimitry Andric            _CheckTLC<_Tuple>::template __enable_implicit<_Tuple>()
499aed8d94eSDimitry Andric    > = false>
500aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
501aed8d94eSDimitry Andric    pair(_Tuple&& __p)
502aed8d94eSDimitry Andric        : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
503aed8d94eSDimitry Andric          second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
504aed8d94eSDimitry Andric
505aed8d94eSDimitry Andric    template <class... _Args1, class... _Args2>
506aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
507aed8d94eSDimitry Andric    pair(piecewise_construct_t __pc,
508aed8d94eSDimitry Andric         tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
509*b5893f02SDimitry Andric        _NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value &&
510*b5893f02SDimitry Andric                    is_nothrow_constructible<second_type, _Args2...>::value))
511aed8d94eSDimitry Andric        : pair(__pc, __first_args, __second_args,
512aed8d94eSDimitry Andric                typename __make_tuple_indices<sizeof...(_Args1)>::type(),
513aed8d94eSDimitry Andric                typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
514aed8d94eSDimitry Andric
515aed8d94eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
516aed8d94eSDimitry Andric    pair& operator=(typename conditional<
517aed8d94eSDimitry Andric                        is_copy_assignable<first_type>::value &&
518aed8d94eSDimitry Andric                        is_copy_assignable<second_type>::value,
519aed8d94eSDimitry Andric                    pair, __nat>::type const& __p)
5207a984708SDavid Chisnall        _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
5217a984708SDavid Chisnall                   is_nothrow_copy_assignable<second_type>::value)
5227a984708SDavid Chisnall    {
5237a984708SDavid Chisnall        first = __p.first;
5247a984708SDavid Chisnall        second = __p.second;
5257a984708SDavid Chisnall        return *this;
5267a984708SDavid Chisnall    }
5277a984708SDavid Chisnall
5287a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
529aed8d94eSDimitry Andric    pair& operator=(typename conditional<
530aed8d94eSDimitry Andric                        is_move_assignable<first_type>::value &&
531aed8d94eSDimitry Andric                        is_move_assignable<second_type>::value,
532aed8d94eSDimitry Andric                    pair, __nat>::type&& __p)
533aed8d94eSDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
5347a984708SDavid Chisnall                   is_nothrow_move_assignable<second_type>::value)
5357a984708SDavid Chisnall    {
5367a984708SDavid Chisnall        first = _VSTD::forward<first_type>(__p.first);
5377a984708SDavid Chisnall        second = _VSTD::forward<second_type>(__p.second);
5387a984708SDavid Chisnall        return *this;
5397a984708SDavid Chisnall    }
5407a984708SDavid Chisnall
541aed8d94eSDimitry Andric    template <class _Tuple, _EnableB<
542aed8d94eSDimitry Andric            _CheckTLC<_Tuple>::template __enable_assign<_Tuple>()
543aed8d94eSDimitry Andric     > = false>
5447a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
545aed8d94eSDimitry Andric    pair& operator=(_Tuple&& __p) {
546aed8d94eSDimitry Andric        first = _VSTD::get<0>(_VSTD::forward<_Tuple>(__p));
547aed8d94eSDimitry Andric        second = _VSTD::get<1>(_VSTD::forward<_Tuple>(__p));
5487a984708SDavid Chisnall        return *this;
5497a984708SDavid Chisnall    }
550aed8d94eSDimitry Andric#endif
5517a984708SDavid Chisnall
5527a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
5537a984708SDavid Chisnall    void
5547a984708SDavid Chisnall    swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
5557a984708SDavid Chisnall                               __is_nothrow_swappable<second_type>::value)
5567a984708SDavid Chisnall    {
5579729cf09SDimitry Andric        using _VSTD::swap;
5589729cf09SDimitry Andric        swap(first,  __p.first);
5599729cf09SDimitry Andric        swap(second, __p.second);
5607a984708SDavid Chisnall    }
5617a984708SDavid Chisnallprivate:
5627a984708SDavid Chisnall
563aed8d94eSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
5647a984708SDavid Chisnall    template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
5657a984708SDavid Chisnall        _LIBCPP_INLINE_VISIBILITY
5667a984708SDavid Chisnall        pair(piecewise_construct_t,
5677a984708SDavid Chisnall             tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
5687a984708SDavid Chisnall             __tuple_indices<_I1...>, __tuple_indices<_I2...>);
569aed8d94eSDimitry Andric#endif
5707a984708SDavid Chisnall};
5717a984708SDavid Chisnall
572b2c7081bSDimitry Andric#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
573b2c7081bSDimitry Andrictemplate<class _T1, class _T2>
574b2c7081bSDimitry Andricpair(_T1, _T2) -> pair<_T1, _T2>;
575b2c7081bSDimitry Andric#endif // _LIBCPP_HAS_NO_DEDUCTION_GUIDES
576b2c7081bSDimitry Andric
5777a984708SDavid Chisnalltemplate <class _T1, class _T2>
5784f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
5797a984708SDavid Chisnallbool
5807a984708SDavid Chisnalloperator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
5817a984708SDavid Chisnall{
5827a984708SDavid Chisnall    return __x.first == __y.first && __x.second == __y.second;
5837a984708SDavid Chisnall}
5847a984708SDavid Chisnall
5857a984708SDavid Chisnalltemplate <class _T1, class _T2>
5864f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
5877a984708SDavid Chisnallbool
5887a984708SDavid Chisnalloperator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
5897a984708SDavid Chisnall{
5907a984708SDavid Chisnall    return !(__x == __y);
5917a984708SDavid Chisnall}
5927a984708SDavid Chisnall
5937a984708SDavid Chisnalltemplate <class _T1, class _T2>
5944f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
5957a984708SDavid Chisnallbool
5967a984708SDavid Chisnalloperator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
5977a984708SDavid Chisnall{
5987a984708SDavid Chisnall    return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
5997a984708SDavid Chisnall}
6007a984708SDavid Chisnall
6017a984708SDavid Chisnalltemplate <class _T1, class _T2>
6024f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
6037a984708SDavid Chisnallbool
6047a984708SDavid Chisnalloperator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
6057a984708SDavid Chisnall{
6067a984708SDavid Chisnall    return __y < __x;
6077a984708SDavid Chisnall}
6087a984708SDavid Chisnall
6097a984708SDavid Chisnalltemplate <class _T1, class _T2>
6104f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
6117a984708SDavid Chisnallbool
6127a984708SDavid Chisnalloperator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
6137a984708SDavid Chisnall{
6147a984708SDavid Chisnall    return !(__x < __y);
6157a984708SDavid Chisnall}
6167a984708SDavid Chisnall
6177a984708SDavid Chisnalltemplate <class _T1, class _T2>
6184f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
6197a984708SDavid Chisnallbool
6207a984708SDavid Chisnalloperator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
6217a984708SDavid Chisnall{
6227a984708SDavid Chisnall    return !(__y < __x);
6237a984708SDavid Chisnall}
6247a984708SDavid Chisnall
6257a984708SDavid Chisnalltemplate <class _T1, class _T2>
6267a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
6277a984708SDavid Chisnalltypename enable_if
6287a984708SDavid Chisnall<
6297a984708SDavid Chisnall    __is_swappable<_T1>::value &&
6307a984708SDavid Chisnall    __is_swappable<_T2>::value,
6317a984708SDavid Chisnall    void
6327a984708SDavid Chisnall>::type
6337a984708SDavid Chisnallswap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
6347a984708SDavid Chisnall                     _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
6357a984708SDavid Chisnall                                 __is_nothrow_swappable<_T2>::value))
6367a984708SDavid Chisnall{
6377a984708SDavid Chisnall    __x.swap(__y);
6387a984708SDavid Chisnall}
6397a984708SDavid Chisnall
640*b5893f02SDimitry Andrictemplate <class _Tp>
641*b5893f02SDimitry Andricstruct __unwrap_reference { typedef _Tp type; };
642*b5893f02SDimitry Andric
643*b5893f02SDimitry Andrictemplate <class _Tp>
644*b5893f02SDimitry Andricstruct __unwrap_reference<reference_wrapper<_Tp> > { typedef _Tp& type; };
645*b5893f02SDimitry Andric
646*b5893f02SDimitry Andric#if _LIBCPP_STD_VER > 17
647*b5893f02SDimitry Andrictemplate <class _Tp>
648*b5893f02SDimitry Andricstruct unwrap_reference : __unwrap_reference<_Tp> { };
649*b5893f02SDimitry Andric
650*b5893f02SDimitry Andrictemplate <class _Tp>
651*b5893f02SDimitry Andricstruct unwrap_ref_decay : unwrap_reference<typename decay<_Tp>::type> { };
652*b5893f02SDimitry Andric#endif // > C++17
653*b5893f02SDimitry Andric
654*b5893f02SDimitry Andrictemplate <class _Tp>
655*b5893f02SDimitry Andricstruct __unwrap_ref_decay
656*b5893f02SDimitry Andric#if _LIBCPP_STD_VER > 17
657*b5893f02SDimitry Andric    : unwrap_ref_decay<_Tp>
658*b5893f02SDimitry Andric#else
659*b5893f02SDimitry Andric    : __unwrap_reference<typename decay<_Tp>::type>
660*b5893f02SDimitry Andric#endif
661*b5893f02SDimitry Andric{ };
662*b5893f02SDimitry Andric
663540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
6647a984708SDavid Chisnall
6657a984708SDavid Chisnalltemplate <class _T1, class _T2>
6664f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
667*b5893f02SDimitry Andricpair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
6687a984708SDavid Chisnallmake_pair(_T1&& __t1, _T2&& __t2)
6697a984708SDavid Chisnall{
670*b5893f02SDimitry Andric    return pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
6717a984708SDavid Chisnall               (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
6727a984708SDavid Chisnall}
6737a984708SDavid Chisnall
674540d2a8bSDimitry Andric#else  // _LIBCPP_CXX03_LANG
6757a984708SDavid Chisnall
6767a984708SDavid Chisnalltemplate <class _T1, class _T2>
6777a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
6787a984708SDavid Chisnallpair<_T1,_T2>
6797a984708SDavid Chisnallmake_pair(_T1 __x, _T2 __y)
6807a984708SDavid Chisnall{
6817a984708SDavid Chisnall    return pair<_T1, _T2>(__x, __y);
6827a984708SDavid Chisnall}
6837a984708SDavid Chisnall
684540d2a8bSDimitry Andric#endif  // _LIBCPP_CXX03_LANG
6857a984708SDavid Chisnall
6867a984708SDavid Chisnalltemplate <class _T1, class _T2>
687*b5893f02SDimitry Andric  struct _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
6887a984708SDavid Chisnall    : public integral_constant<size_t, 2> {};
6897a984708SDavid Chisnall
69024d58133SDimitry Andrictemplate <size_t _Ip, class _T1, class _T2>
69124d58133SDimitry Andricclass _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
69224d58133SDimitry Andric{
69324d58133SDimitry Andric    static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
69424d58133SDimitry Andric};
69524d58133SDimitry Andric
6967a984708SDavid Chisnalltemplate <class _T1, class _T2>
697aed8d94eSDimitry Andricclass _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
6987a984708SDavid Chisnall{
6997a984708SDavid Chisnallpublic:
7007a984708SDavid Chisnall    typedef _T1 type;
7017a984708SDavid Chisnall};
7027a984708SDavid Chisnall
7037a984708SDavid Chisnalltemplate <class _T1, class _T2>
704aed8d94eSDimitry Andricclass _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> >
7057a984708SDavid Chisnall{
7067a984708SDavid Chisnallpublic:
7077a984708SDavid Chisnall    typedef _T2 type;
7087a984708SDavid Chisnall};
7097a984708SDavid Chisnall
7107a984708SDavid Chisnalltemplate <size_t _Ip> struct __get_pair;
7117a984708SDavid Chisnall
7127a984708SDavid Chisnalltemplate <>
7137a984708SDavid Chisnallstruct __get_pair<0>
7147a984708SDavid Chisnall{
7157a984708SDavid Chisnall    template <class _T1, class _T2>
7167a984708SDavid Chisnall    static
7174f7ab58eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7187a984708SDavid Chisnall    _T1&
7197a984708SDavid Chisnall    get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
7207a984708SDavid Chisnall
7217a984708SDavid Chisnall    template <class _T1, class _T2>
7227a984708SDavid Chisnall    static
7234f7ab58eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7247a984708SDavid Chisnall    const _T1&
7257a984708SDavid Chisnall    get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
7267a984708SDavid Chisnall
727540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
7287a984708SDavid Chisnall    template <class _T1, class _T2>
7297a984708SDavid Chisnall    static
7304f7ab58eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7317a984708SDavid Chisnall    _T1&&
7327a984708SDavid Chisnall    get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
7337a984708SDavid Chisnall
7349729cf09SDimitry Andric    template <class _T1, class _T2>
7359729cf09SDimitry Andric    static
7369729cf09SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7379729cf09SDimitry Andric    const _T1&&
7389729cf09SDimitry Andric    get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T1>(__p.first);}
739540d2a8bSDimitry Andric#endif  // _LIBCPP_CXX03_LANG
7407a984708SDavid Chisnall};
7417a984708SDavid Chisnall
7427a984708SDavid Chisnalltemplate <>
7437a984708SDavid Chisnallstruct __get_pair<1>
7447a984708SDavid Chisnall{
7457a984708SDavid Chisnall    template <class _T1, class _T2>
7467a984708SDavid Chisnall    static
7474f7ab58eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7487a984708SDavid Chisnall    _T2&
7497a984708SDavid Chisnall    get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
7507a984708SDavid Chisnall
7517a984708SDavid Chisnall    template <class _T1, class _T2>
7527a984708SDavid Chisnall    static
7534f7ab58eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7547a984708SDavid Chisnall    const _T2&
7557a984708SDavid Chisnall    get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
7567a984708SDavid Chisnall
757540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
7587a984708SDavid Chisnall    template <class _T1, class _T2>
7597a984708SDavid Chisnall    static
7604f7ab58eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7617a984708SDavid Chisnall    _T2&&
7627a984708SDavid Chisnall    get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
7637a984708SDavid Chisnall
7649729cf09SDimitry Andric    template <class _T1, class _T2>
7659729cf09SDimitry Andric    static
7669729cf09SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7679729cf09SDimitry Andric    const _T2&&
7689729cf09SDimitry Andric    get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T2>(__p.second);}
769540d2a8bSDimitry Andric#endif  // _LIBCPP_CXX03_LANG
7707a984708SDavid Chisnall};
7717a984708SDavid Chisnall
7727a984708SDavid Chisnalltemplate <size_t _Ip, class _T1, class _T2>
7734f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7747a984708SDavid Chisnalltypename tuple_element<_Ip, pair<_T1, _T2> >::type&
7757a984708SDavid Chisnallget(pair<_T1, _T2>& __p) _NOEXCEPT
7767a984708SDavid Chisnall{
7777a984708SDavid Chisnall    return __get_pair<_Ip>::get(__p);
7787a984708SDavid Chisnall}
7797a984708SDavid Chisnall
7807a984708SDavid Chisnalltemplate <size_t _Ip, class _T1, class _T2>
7814f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7827a984708SDavid Chisnallconst typename tuple_element<_Ip, pair<_T1, _T2> >::type&
7837a984708SDavid Chisnallget(const pair<_T1, _T2>& __p) _NOEXCEPT
7847a984708SDavid Chisnall{
7857a984708SDavid Chisnall    return __get_pair<_Ip>::get(__p);
7867a984708SDavid Chisnall}
7877a984708SDavid Chisnall
788540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
7897a984708SDavid Chisnalltemplate <size_t _Ip, class _T1, class _T2>
7904f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7917a984708SDavid Chisnalltypename tuple_element<_Ip, pair<_T1, _T2> >::type&&
7927a984708SDavid Chisnallget(pair<_T1, _T2>&& __p) _NOEXCEPT
7937a984708SDavid Chisnall{
7947a984708SDavid Chisnall    return __get_pair<_Ip>::get(_VSTD::move(__p));
7957a984708SDavid Chisnall}
7967a984708SDavid Chisnall
7979729cf09SDimitry Andrictemplate <size_t _Ip, class _T1, class _T2>
7989729cf09SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7999729cf09SDimitry Andricconst typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
8009729cf09SDimitry Andricget(const pair<_T1, _T2>&& __p) _NOEXCEPT
8019729cf09SDimitry Andric{
8029729cf09SDimitry Andric    return __get_pair<_Ip>::get(_VSTD::move(__p));
8039729cf09SDimitry Andric}
804540d2a8bSDimitry Andric#endif  // _LIBCPP_CXX03_LANG
8057a984708SDavid Chisnall
8064bab9fd9SDavid Chisnall#if _LIBCPP_STD_VER > 11
8074f7ab58eSDimitry Andrictemplate <class _T1, class _T2>
8084f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8094f7ab58eSDimitry Andricconstexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
8104f7ab58eSDimitry Andric{
8114f7ab58eSDimitry Andric    return __get_pair<0>::get(__p);
8124f7ab58eSDimitry Andric}
8134f7ab58eSDimitry Andric
8144f7ab58eSDimitry Andrictemplate <class _T1, class _T2>
8154f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8164f7ab58eSDimitry Andricconstexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
8174f7ab58eSDimitry Andric{
8184f7ab58eSDimitry Andric    return __get_pair<0>::get(__p);
8194f7ab58eSDimitry Andric}
8204f7ab58eSDimitry Andric
8214f7ab58eSDimitry Andrictemplate <class _T1, class _T2>
8224f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8234f7ab58eSDimitry Andricconstexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
8244f7ab58eSDimitry Andric{
8254f7ab58eSDimitry Andric    return __get_pair<0>::get(_VSTD::move(__p));
8264f7ab58eSDimitry Andric}
8274f7ab58eSDimitry Andric
8284f7ab58eSDimitry Andrictemplate <class _T1, class _T2>
8294f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8309729cf09SDimitry Andricconstexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
8319729cf09SDimitry Andric{
8329729cf09SDimitry Andric    return __get_pair<0>::get(_VSTD::move(__p));
8339729cf09SDimitry Andric}
8349729cf09SDimitry Andric
8359729cf09SDimitry Andrictemplate <class _T1, class _T2>
8369729cf09SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8374f7ab58eSDimitry Andricconstexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
8384f7ab58eSDimitry Andric{
8394f7ab58eSDimitry Andric    return __get_pair<1>::get(__p);
8404f7ab58eSDimitry Andric}
8414f7ab58eSDimitry Andric
8424f7ab58eSDimitry Andrictemplate <class _T1, class _T2>
8434f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8444f7ab58eSDimitry Andricconstexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
8454f7ab58eSDimitry Andric{
8464f7ab58eSDimitry Andric    return __get_pair<1>::get(__p);
8474f7ab58eSDimitry Andric}
8484f7ab58eSDimitry Andric
8494f7ab58eSDimitry Andrictemplate <class _T1, class _T2>
8504f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8514f7ab58eSDimitry Andricconstexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
8524f7ab58eSDimitry Andric{
8534f7ab58eSDimitry Andric    return __get_pair<1>::get(_VSTD::move(__p));
8544f7ab58eSDimitry Andric}
8554f7ab58eSDimitry Andric
8569729cf09SDimitry Andrictemplate <class _T1, class _T2>
8579729cf09SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8589729cf09SDimitry Andricconstexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
8599729cf09SDimitry Andric{
8609729cf09SDimitry Andric    return __get_pair<1>::get(_VSTD::move(__p));
8619729cf09SDimitry Andric}
8629729cf09SDimitry Andric
8634f7ab58eSDimitry Andric#endif
8644f7ab58eSDimitry Andric
8654f7ab58eSDimitry Andric#if _LIBCPP_STD_VER > 11
8664bab9fd9SDavid Chisnall
8674bab9fd9SDavid Chisnalltemplate<class _Tp, _Tp... _Ip>
868aed8d94eSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS integer_sequence
8694bab9fd9SDavid Chisnall{
8704bab9fd9SDavid Chisnall    typedef _Tp value_type;
8714bab9fd9SDavid Chisnall    static_assert( is_integral<_Tp>::value,
8724bab9fd9SDavid Chisnall                  "std::integer_sequence can only be instantiated with an integral type" );
8734bab9fd9SDavid Chisnall    static
8744bab9fd9SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
8754bab9fd9SDavid Chisnall    constexpr
8764bab9fd9SDavid Chisnall    size_t
8774bab9fd9SDavid Chisnall    size() noexcept { return sizeof...(_Ip); }
8784bab9fd9SDavid Chisnall};
8794bab9fd9SDavid Chisnall
8804bab9fd9SDavid Chisnalltemplate<size_t... _Ip>
8814bab9fd9SDavid Chisnall    using index_sequence = integer_sequence<size_t, _Ip...>;
8824bab9fd9SDavid Chisnall
8839729cf09SDimitry Andric#if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
8849729cf09SDimitry Andric
8859729cf09SDimitry Andrictemplate <class _Tp, _Tp _Ep>
8867c82a1ecSDimitry Andricusing __make_integer_sequence = __make_integer_seq<integer_sequence, _Tp, _Ep>;
8879729cf09SDimitry Andric
8889729cf09SDimitry Andric#else
8899729cf09SDimitry Andric
8904bab9fd9SDavid Chisnalltemplate<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked =
8917c82a1ecSDimitry Andric  typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>;
8924bab9fd9SDavid Chisnall
8934bab9fd9SDavid Chisnalltemplate <class _Tp, _Tp _Ep>
8947c82a1ecSDimitry Andricstruct __make_integer_sequence_checked
8954bab9fd9SDavid Chisnall{
8964bab9fd9SDavid Chisnall    static_assert(is_integral<_Tp>::value,
8974bab9fd9SDavid Chisnall                  "std::make_integer_sequence can only be instantiated with an integral type" );
8989729cf09SDimitry Andric    static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
8999729cf09SDimitry Andric    // Workaround GCC bug by preventing bad installations when 0 <= _Ep
9009729cf09SDimitry Andric    // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
9019729cf09SDimitry Andric    typedef __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
9024bab9fd9SDavid Chisnall};
9034bab9fd9SDavid Chisnall
9047c82a1ecSDimitry Andrictemplate <class _Tp, _Tp _Ep>
9057c82a1ecSDimitry Andricusing __make_integer_sequence = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
9067c82a1ecSDimitry Andric
9079729cf09SDimitry Andric#endif
9089729cf09SDimitry Andric
9094bab9fd9SDavid Chisnalltemplate<class _Tp, _Tp _Np>
9107c82a1ecSDimitry Andric    using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
9114bab9fd9SDavid Chisnall
9124bab9fd9SDavid Chisnalltemplate<size_t _Np>
9134bab9fd9SDavid Chisnall    using make_index_sequence = make_integer_sequence<size_t, _Np>;
9144bab9fd9SDavid Chisnall
9154bab9fd9SDavid Chisnalltemplate<class... _Tp>
9164bab9fd9SDavid Chisnall    using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
9174bab9fd9SDavid Chisnall
9184bab9fd9SDavid Chisnall#endif  // _LIBCPP_STD_VER > 11
9194bab9fd9SDavid Chisnall
9204f7ab58eSDimitry Andric#if _LIBCPP_STD_VER > 11
9214f7ab58eSDimitry Andrictemplate<class _T1, class _T2 = _T1>
9224ba319b5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
9234f7ab58eSDimitry Andric_T1 exchange(_T1& __obj, _T2 && __new_value)
9244f7ab58eSDimitry Andric{
9254f7ab58eSDimitry Andric    _T1 __old_value = _VSTD::move(__obj);
9264f7ab58eSDimitry Andric    __obj = _VSTD::forward<_T2>(__new_value);
9274f7ab58eSDimitry Andric    return __old_value;
9284f7ab58eSDimitry Andric}
9294f7ab58eSDimitry Andric#endif  // _LIBCPP_STD_VER > 11
9304f7ab58eSDimitry Andric
931aed8d94eSDimitry Andric#if _LIBCPP_STD_VER > 14
932aed8d94eSDimitry Andric
933aed8d94eSDimitry Andricstruct _LIBCPP_TYPE_VIS in_place_t {
934aed8d94eSDimitry Andric    explicit in_place_t() = default;
935aed8d94eSDimitry Andric};
93630785c0eSDimitry Andric_LIBCPP_INLINE_VAR constexpr in_place_t in_place{};
937aed8d94eSDimitry Andric
938aed8d94eSDimitry Andrictemplate <class _Tp>
939540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS in_place_type_t {
940aed8d94eSDimitry Andric    explicit in_place_type_t() = default;
941aed8d94eSDimitry Andric};
942aed8d94eSDimitry Andrictemplate <class _Tp>
94330785c0eSDimitry Andric_LIBCPP_INLINE_VAR constexpr in_place_type_t<_Tp> in_place_type{};
944aed8d94eSDimitry Andric
945aed8d94eSDimitry Andrictemplate <size_t _Idx>
946aed8d94eSDimitry Andricstruct _LIBCPP_TYPE_VIS in_place_index_t {
947aed8d94eSDimitry Andric    explicit in_place_index_t() = default;
948aed8d94eSDimitry Andric};
949aed8d94eSDimitry Andrictemplate <size_t _Idx>
95030785c0eSDimitry Andric_LIBCPP_INLINE_VAR constexpr in_place_index_t<_Idx> in_place_index{};
951aed8d94eSDimitry Andric
952aed8d94eSDimitry Andrictemplate <class _Tp> struct __is_inplace_type_imp : false_type {};
953aed8d94eSDimitry Andrictemplate <class _Tp> struct __is_inplace_type_imp<in_place_type_t<_Tp>> : true_type {};
954aed8d94eSDimitry Andric
955aed8d94eSDimitry Andrictemplate <class _Tp>
956aed8d94eSDimitry Andricusing __is_inplace_type = __is_inplace_type_imp<__uncvref_t<_Tp>>;
957aed8d94eSDimitry Andric
95824d58133SDimitry Andrictemplate <class _Tp> struct __is_inplace_index_imp : false_type {};
95924d58133SDimitry Andrictemplate <size_t _Idx> struct __is_inplace_index_imp<in_place_index_t<_Idx>> : true_type {};
96024d58133SDimitry Andric
96124d58133SDimitry Andrictemplate <class _Tp>
96224d58133SDimitry Andricusing __is_inplace_index = __is_inplace_index_imp<__uncvref_t<_Tp>>;
96324d58133SDimitry Andric
964aed8d94eSDimitry Andric#endif // _LIBCPP_STD_VER > 14
965aed8d94eSDimitry Andric
966540d2a8bSDimitry Andrictemplate <class _Arg, class _Result>
967540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS unary_function
968540d2a8bSDimitry Andric{
969540d2a8bSDimitry Andric    typedef _Arg    argument_type;
970540d2a8bSDimitry Andric    typedef _Result result_type;
971540d2a8bSDimitry Andric};
972540d2a8bSDimitry Andric
973540d2a8bSDimitry Andrictemplate <class _Size>
974540d2a8bSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
975540d2a8bSDimitry Andric_Size
976540d2a8bSDimitry Andric__loadword(const void* __p)
977540d2a8bSDimitry Andric{
978540d2a8bSDimitry Andric    _Size __r;
979540d2a8bSDimitry Andric    std::memcpy(&__r, __p, sizeof(__r));
980540d2a8bSDimitry Andric    return __r;
981540d2a8bSDimitry Andric}
982540d2a8bSDimitry Andric
983540d2a8bSDimitry Andric// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
984540d2a8bSDimitry Andric// is 64 bits.  This is because cityhash64 uses 64bit x 64bit
985540d2a8bSDimitry Andric// multiplication, which can be very slow on 32-bit systems.
986540d2a8bSDimitry Andrictemplate <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
987540d2a8bSDimitry Andricstruct __murmur2_or_cityhash;
988540d2a8bSDimitry Andric
989540d2a8bSDimitry Andrictemplate <class _Size>
990540d2a8bSDimitry Andricstruct __murmur2_or_cityhash<_Size, 32>
991540d2a8bSDimitry Andric{
992540d2a8bSDimitry Andric    inline _Size operator()(const void* __key, _Size __len)
993540d2a8bSDimitry Andric         _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
994540d2a8bSDimitry Andric};
995540d2a8bSDimitry Andric
996540d2a8bSDimitry Andric// murmur2
997540d2a8bSDimitry Andrictemplate <class _Size>
998540d2a8bSDimitry Andric_Size
999540d2a8bSDimitry Andric__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
1000540d2a8bSDimitry Andric{
1001540d2a8bSDimitry Andric    const _Size __m = 0x5bd1e995;
1002540d2a8bSDimitry Andric    const _Size __r = 24;
1003540d2a8bSDimitry Andric    _Size __h = __len;
1004540d2a8bSDimitry Andric    const unsigned char* __data = static_cast<const unsigned char*>(__key);
1005540d2a8bSDimitry Andric    for (; __len >= 4; __data += 4, __len -= 4)
1006540d2a8bSDimitry Andric    {
1007540d2a8bSDimitry Andric        _Size __k = __loadword<_Size>(__data);
1008540d2a8bSDimitry Andric        __k *= __m;
1009540d2a8bSDimitry Andric        __k ^= __k >> __r;
1010540d2a8bSDimitry Andric        __k *= __m;
1011540d2a8bSDimitry Andric        __h *= __m;
1012540d2a8bSDimitry Andric        __h ^= __k;
1013540d2a8bSDimitry Andric    }
1014540d2a8bSDimitry Andric    switch (__len)
1015540d2a8bSDimitry Andric    {
1016540d2a8bSDimitry Andric    case 3:
1017540d2a8bSDimitry Andric        __h ^= __data[2] << 16;
1018*b5893f02SDimitry Andric        _LIBCPP_FALLTHROUGH();
1019540d2a8bSDimitry Andric    case 2:
1020540d2a8bSDimitry Andric        __h ^= __data[1] << 8;
1021*b5893f02SDimitry Andric        _LIBCPP_FALLTHROUGH();
1022540d2a8bSDimitry Andric    case 1:
1023540d2a8bSDimitry Andric        __h ^= __data[0];
1024540d2a8bSDimitry Andric        __h *= __m;
1025540d2a8bSDimitry Andric    }
1026540d2a8bSDimitry Andric    __h ^= __h >> 13;
1027540d2a8bSDimitry Andric    __h *= __m;
1028540d2a8bSDimitry Andric    __h ^= __h >> 15;
1029540d2a8bSDimitry Andric    return __h;
1030540d2a8bSDimitry Andric}
1031540d2a8bSDimitry Andric
1032540d2a8bSDimitry Andrictemplate <class _Size>
1033540d2a8bSDimitry Andricstruct __murmur2_or_cityhash<_Size, 64>
1034540d2a8bSDimitry Andric{
1035540d2a8bSDimitry Andric    inline _Size operator()(const void* __key, _Size __len)  _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
1036540d2a8bSDimitry Andric
1037540d2a8bSDimitry Andric private:
1038540d2a8bSDimitry Andric  // Some primes between 2^63 and 2^64.
1039540d2a8bSDimitry Andric  static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
1040540d2a8bSDimitry Andric  static const _Size __k1 = 0xb492b66fbe98f273ULL;
1041540d2a8bSDimitry Andric  static const _Size __k2 = 0x9ae16a3b2f90404fULL;
1042540d2a8bSDimitry Andric  static const _Size __k3 = 0xc949d7c7509e6557ULL;
1043540d2a8bSDimitry Andric
1044540d2a8bSDimitry Andric  static _Size __rotate(_Size __val, int __shift) {
1045540d2a8bSDimitry Andric    return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
1046540d2a8bSDimitry Andric  }
1047540d2a8bSDimitry Andric
1048540d2a8bSDimitry Andric  static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
1049540d2a8bSDimitry Andric    return (__val >> __shift) | (__val << (64 - __shift));
1050540d2a8bSDimitry Andric  }
1051540d2a8bSDimitry Andric
1052540d2a8bSDimitry Andric  static _Size __shift_mix(_Size __val) {
1053540d2a8bSDimitry Andric    return __val ^ (__val >> 47);
1054540d2a8bSDimitry Andric  }
1055540d2a8bSDimitry Andric
1056540d2a8bSDimitry Andric  static _Size __hash_len_16(_Size __u, _Size __v)
1057540d2a8bSDimitry Andric     _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1058540d2a8bSDimitry Andric  {
1059540d2a8bSDimitry Andric    const _Size __mul = 0x9ddfea08eb382d69ULL;
1060540d2a8bSDimitry Andric    _Size __a = (__u ^ __v) * __mul;
1061540d2a8bSDimitry Andric    __a ^= (__a >> 47);
1062540d2a8bSDimitry Andric    _Size __b = (__v ^ __a) * __mul;
1063540d2a8bSDimitry Andric    __b ^= (__b >> 47);
1064540d2a8bSDimitry Andric    __b *= __mul;
1065540d2a8bSDimitry Andric    return __b;
1066540d2a8bSDimitry Andric  }
1067540d2a8bSDimitry Andric
1068540d2a8bSDimitry Andric  static _Size __hash_len_0_to_16(const char* __s, _Size __len)
1069540d2a8bSDimitry Andric     _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1070540d2a8bSDimitry Andric  {
1071540d2a8bSDimitry Andric    if (__len > 8) {
1072540d2a8bSDimitry Andric      const _Size __a = __loadword<_Size>(__s);
1073540d2a8bSDimitry Andric      const _Size __b = __loadword<_Size>(__s + __len - 8);
1074540d2a8bSDimitry Andric      return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
1075540d2a8bSDimitry Andric    }
1076540d2a8bSDimitry Andric    if (__len >= 4) {
1077540d2a8bSDimitry Andric      const uint32_t __a = __loadword<uint32_t>(__s);
1078540d2a8bSDimitry Andric      const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
1079540d2a8bSDimitry Andric      return __hash_len_16(__len + (__a << 3), __b);
1080540d2a8bSDimitry Andric    }
1081540d2a8bSDimitry Andric    if (__len > 0) {
1082540d2a8bSDimitry Andric      const unsigned char __a = __s[0];
1083540d2a8bSDimitry Andric      const unsigned char __b = __s[__len >> 1];
1084540d2a8bSDimitry Andric      const unsigned char __c = __s[__len - 1];
1085540d2a8bSDimitry Andric      const uint32_t __y = static_cast<uint32_t>(__a) +
1086540d2a8bSDimitry Andric                           (static_cast<uint32_t>(__b) << 8);
1087540d2a8bSDimitry Andric      const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
1088540d2a8bSDimitry Andric      return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
1089540d2a8bSDimitry Andric    }
1090540d2a8bSDimitry Andric    return __k2;
1091540d2a8bSDimitry Andric  }
1092540d2a8bSDimitry Andric
1093540d2a8bSDimitry Andric  static _Size __hash_len_17_to_32(const char *__s, _Size __len)
1094540d2a8bSDimitry Andric     _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1095540d2a8bSDimitry Andric  {
1096540d2a8bSDimitry Andric    const _Size __a = __loadword<_Size>(__s) * __k1;
1097540d2a8bSDimitry Andric    const _Size __b = __loadword<_Size>(__s + 8);
1098540d2a8bSDimitry Andric    const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
1099540d2a8bSDimitry Andric    const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
1100540d2a8bSDimitry Andric    return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
1101540d2a8bSDimitry Andric                         __a + __rotate(__b ^ __k3, 20) - __c + __len);
1102540d2a8bSDimitry Andric  }
1103540d2a8bSDimitry Andric
1104540d2a8bSDimitry Andric  // Return a 16-byte hash for 48 bytes.  Quick and dirty.
1105540d2a8bSDimitry Andric  // Callers do best to use "random-looking" values for a and b.
1106540d2a8bSDimitry Andric  static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
1107540d2a8bSDimitry Andric      _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b)
1108540d2a8bSDimitry Andric        _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1109540d2a8bSDimitry Andric  {
1110540d2a8bSDimitry Andric    __a += __w;
1111540d2a8bSDimitry Andric    __b = __rotate(__b + __a + __z, 21);
1112540d2a8bSDimitry Andric    const _Size __c = __a;
1113540d2a8bSDimitry Andric    __a += __x;
1114540d2a8bSDimitry Andric    __a += __y;
1115540d2a8bSDimitry Andric    __b += __rotate(__a, 44);
1116540d2a8bSDimitry Andric    return pair<_Size, _Size>(__a + __z, __b + __c);
1117540d2a8bSDimitry Andric  }
1118540d2a8bSDimitry Andric
1119540d2a8bSDimitry Andric  // Return a 16-byte hash for s[0] ... s[31], a, and b.  Quick and dirty.
1120540d2a8bSDimitry Andric  static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
1121540d2a8bSDimitry Andric      const char* __s, _Size __a, _Size __b)
1122540d2a8bSDimitry Andric    _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1123540d2a8bSDimitry Andric  {
1124540d2a8bSDimitry Andric    return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
1125540d2a8bSDimitry Andric                                         __loadword<_Size>(__s + 8),
1126540d2a8bSDimitry Andric                                         __loadword<_Size>(__s + 16),
1127540d2a8bSDimitry Andric                                         __loadword<_Size>(__s + 24),
1128540d2a8bSDimitry Andric                                         __a,
1129540d2a8bSDimitry Andric                                         __b);
1130540d2a8bSDimitry Andric  }
1131540d2a8bSDimitry Andric
1132540d2a8bSDimitry Andric  // Return an 8-byte hash for 33 to 64 bytes.
1133540d2a8bSDimitry Andric  static _Size __hash_len_33_to_64(const char *__s, size_t __len)
1134540d2a8bSDimitry Andric    _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1135540d2a8bSDimitry Andric  {
1136540d2a8bSDimitry Andric    _Size __z = __loadword<_Size>(__s + 24);
1137540d2a8bSDimitry Andric    _Size __a = __loadword<_Size>(__s) +
1138540d2a8bSDimitry Andric                (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
1139540d2a8bSDimitry Andric    _Size __b = __rotate(__a + __z, 52);
1140540d2a8bSDimitry Andric    _Size __c = __rotate(__a, 37);
1141540d2a8bSDimitry Andric    __a += __loadword<_Size>(__s + 8);
1142540d2a8bSDimitry Andric    __c += __rotate(__a, 7);
1143540d2a8bSDimitry Andric    __a += __loadword<_Size>(__s + 16);
1144540d2a8bSDimitry Andric    _Size __vf = __a + __z;
1145540d2a8bSDimitry Andric    _Size __vs = __b + __rotate(__a, 31) + __c;
1146540d2a8bSDimitry Andric    __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
1147540d2a8bSDimitry Andric    __z += __loadword<_Size>(__s + __len - 8);
1148540d2a8bSDimitry Andric    __b = __rotate(__a + __z, 52);
1149540d2a8bSDimitry Andric    __c = __rotate(__a, 37);
1150540d2a8bSDimitry Andric    __a += __loadword<_Size>(__s + __len - 24);
1151540d2a8bSDimitry Andric    __c += __rotate(__a, 7);
1152540d2a8bSDimitry Andric    __a += __loadword<_Size>(__s + __len - 16);
1153540d2a8bSDimitry Andric    _Size __wf = __a + __z;
1154540d2a8bSDimitry Andric    _Size __ws = __b + __rotate(__a, 31) + __c;
1155540d2a8bSDimitry Andric    _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
1156540d2a8bSDimitry Andric    return __shift_mix(__r * __k0 + __vs) * __k2;
1157540d2a8bSDimitry Andric  }
1158540d2a8bSDimitry Andric};
1159540d2a8bSDimitry Andric
1160540d2a8bSDimitry Andric// cityhash64
1161540d2a8bSDimitry Andrictemplate <class _Size>
1162540d2a8bSDimitry Andric_Size
1163540d2a8bSDimitry Andric__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
1164540d2a8bSDimitry Andric{
1165540d2a8bSDimitry Andric  const char* __s = static_cast<const char*>(__key);
1166540d2a8bSDimitry Andric  if (__len <= 32) {
1167540d2a8bSDimitry Andric    if (__len <= 16) {
1168540d2a8bSDimitry Andric      return __hash_len_0_to_16(__s, __len);
1169540d2a8bSDimitry Andric    } else {
1170540d2a8bSDimitry Andric      return __hash_len_17_to_32(__s, __len);
1171540d2a8bSDimitry Andric    }
1172540d2a8bSDimitry Andric  } else if (__len <= 64) {
1173540d2a8bSDimitry Andric    return __hash_len_33_to_64(__s, __len);
1174540d2a8bSDimitry Andric  }
1175540d2a8bSDimitry Andric
1176540d2a8bSDimitry Andric  // For strings over 64 bytes we hash the end first, and then as we
1177540d2a8bSDimitry Andric  // loop we keep 56 bytes of state: v, w, x, y, and z.
1178540d2a8bSDimitry Andric  _Size __x = __loadword<_Size>(__s + __len - 40);
1179540d2a8bSDimitry Andric  _Size __y = __loadword<_Size>(__s + __len - 16) +
1180540d2a8bSDimitry Andric              __loadword<_Size>(__s + __len - 56);
1181540d2a8bSDimitry Andric  _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
1182540d2a8bSDimitry Andric                          __loadword<_Size>(__s + __len - 24));
1183540d2a8bSDimitry Andric  pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
1184540d2a8bSDimitry Andric  pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
1185540d2a8bSDimitry Andric  __x = __x * __k1 + __loadword<_Size>(__s);
1186540d2a8bSDimitry Andric
1187540d2a8bSDimitry Andric  // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
1188540d2a8bSDimitry Andric  __len = (__len - 1) & ~static_cast<_Size>(63);
1189540d2a8bSDimitry Andric  do {
1190540d2a8bSDimitry Andric    __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
1191540d2a8bSDimitry Andric    __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
1192540d2a8bSDimitry Andric    __x ^= __w.second;
1193540d2a8bSDimitry Andric    __y += __v.first + __loadword<_Size>(__s + 40);
1194540d2a8bSDimitry Andric    __z = __rotate(__z + __w.first, 33) * __k1;
1195540d2a8bSDimitry Andric    __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
1196540d2a8bSDimitry Andric    __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
1197540d2a8bSDimitry Andric                                        __y + __loadword<_Size>(__s + 16));
1198540d2a8bSDimitry Andric    std::swap(__z, __x);
1199540d2a8bSDimitry Andric    __s += 64;
1200540d2a8bSDimitry Andric    __len -= 64;
1201540d2a8bSDimitry Andric  } while (__len != 0);
1202540d2a8bSDimitry Andric  return __hash_len_16(
1203540d2a8bSDimitry Andric      __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
1204540d2a8bSDimitry Andric      __hash_len_16(__v.second, __w.second) + __x);
1205540d2a8bSDimitry Andric}
1206540d2a8bSDimitry Andric
1207540d2a8bSDimitry Andrictemplate <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
1208540d2a8bSDimitry Andricstruct __scalar_hash;
1209540d2a8bSDimitry Andric
1210540d2a8bSDimitry Andrictemplate <class _Tp>
1211540d2a8bSDimitry Andricstruct __scalar_hash<_Tp, 0>
1212540d2a8bSDimitry Andric    : public unary_function<_Tp, size_t>
1213540d2a8bSDimitry Andric{
1214540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1215540d2a8bSDimitry Andric    size_t operator()(_Tp __v) const _NOEXCEPT
1216540d2a8bSDimitry Andric    {
1217540d2a8bSDimitry Andric        union
1218540d2a8bSDimitry Andric        {
1219540d2a8bSDimitry Andric            _Tp    __t;
1220540d2a8bSDimitry Andric            size_t __a;
1221540d2a8bSDimitry Andric        } __u;
1222540d2a8bSDimitry Andric        __u.__a = 0;
1223540d2a8bSDimitry Andric        __u.__t = __v;
1224540d2a8bSDimitry Andric        return __u.__a;
1225540d2a8bSDimitry Andric    }
1226540d2a8bSDimitry Andric};
1227540d2a8bSDimitry Andric
1228540d2a8bSDimitry Andrictemplate <class _Tp>
1229540d2a8bSDimitry Andricstruct __scalar_hash<_Tp, 1>
1230540d2a8bSDimitry Andric    : public unary_function<_Tp, size_t>
1231540d2a8bSDimitry Andric{
1232540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1233540d2a8bSDimitry Andric    size_t operator()(_Tp __v) const _NOEXCEPT
1234540d2a8bSDimitry Andric    {
1235540d2a8bSDimitry Andric        union
1236540d2a8bSDimitry Andric        {
1237540d2a8bSDimitry Andric            _Tp    __t;
1238540d2a8bSDimitry Andric            size_t __a;
1239540d2a8bSDimitry Andric        } __u;
1240540d2a8bSDimitry Andric        __u.__t = __v;
1241540d2a8bSDimitry Andric        return __u.__a;
1242540d2a8bSDimitry Andric    }
1243540d2a8bSDimitry Andric};
1244540d2a8bSDimitry Andric
1245540d2a8bSDimitry Andrictemplate <class _Tp>
1246540d2a8bSDimitry Andricstruct __scalar_hash<_Tp, 2>
1247540d2a8bSDimitry Andric    : public unary_function<_Tp, size_t>
1248540d2a8bSDimitry Andric{
1249540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1250540d2a8bSDimitry Andric    size_t operator()(_Tp __v) const _NOEXCEPT
1251540d2a8bSDimitry Andric    {
1252540d2a8bSDimitry Andric        union
1253540d2a8bSDimitry Andric        {
1254540d2a8bSDimitry Andric            _Tp __t;
1255540d2a8bSDimitry Andric            struct
1256540d2a8bSDimitry Andric            {
1257540d2a8bSDimitry Andric                size_t __a;
1258540d2a8bSDimitry Andric                size_t __b;
1259540d2a8bSDimitry Andric            } __s;
1260540d2a8bSDimitry Andric        } __u;
1261540d2a8bSDimitry Andric        __u.__t = __v;
1262540d2a8bSDimitry Andric        return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1263540d2a8bSDimitry Andric    }
1264540d2a8bSDimitry Andric};
1265540d2a8bSDimitry Andric
1266540d2a8bSDimitry Andrictemplate <class _Tp>
1267540d2a8bSDimitry Andricstruct __scalar_hash<_Tp, 3>
1268540d2a8bSDimitry Andric    : public unary_function<_Tp, size_t>
1269540d2a8bSDimitry Andric{
1270540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1271540d2a8bSDimitry Andric    size_t operator()(_Tp __v) const _NOEXCEPT
1272540d2a8bSDimitry Andric    {
1273540d2a8bSDimitry Andric        union
1274540d2a8bSDimitry Andric        {
1275540d2a8bSDimitry Andric            _Tp __t;
1276540d2a8bSDimitry Andric            struct
1277540d2a8bSDimitry Andric            {
1278540d2a8bSDimitry Andric                size_t __a;
1279540d2a8bSDimitry Andric                size_t __b;
1280540d2a8bSDimitry Andric                size_t __c;
1281540d2a8bSDimitry Andric            } __s;
1282540d2a8bSDimitry Andric        } __u;
1283540d2a8bSDimitry Andric        __u.__t = __v;
1284540d2a8bSDimitry Andric        return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1285540d2a8bSDimitry Andric    }
1286540d2a8bSDimitry Andric};
1287540d2a8bSDimitry Andric
1288540d2a8bSDimitry Andrictemplate <class _Tp>
1289540d2a8bSDimitry Andricstruct __scalar_hash<_Tp, 4>
1290540d2a8bSDimitry Andric    : public unary_function<_Tp, size_t>
1291540d2a8bSDimitry Andric{
1292540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1293540d2a8bSDimitry Andric    size_t operator()(_Tp __v) const _NOEXCEPT
1294540d2a8bSDimitry Andric    {
1295540d2a8bSDimitry Andric        union
1296540d2a8bSDimitry Andric        {
1297540d2a8bSDimitry Andric            _Tp __t;
1298540d2a8bSDimitry Andric            struct
1299540d2a8bSDimitry Andric            {
1300540d2a8bSDimitry Andric                size_t __a;
1301540d2a8bSDimitry Andric                size_t __b;
1302540d2a8bSDimitry Andric                size_t __c;
1303540d2a8bSDimitry Andric                size_t __d;
1304540d2a8bSDimitry Andric            } __s;
1305540d2a8bSDimitry Andric        } __u;
1306540d2a8bSDimitry Andric        __u.__t = __v;
1307540d2a8bSDimitry Andric        return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1308540d2a8bSDimitry Andric    }
1309540d2a8bSDimitry Andric};
1310540d2a8bSDimitry Andric
1311540d2a8bSDimitry Andricstruct _PairT {
1312540d2a8bSDimitry Andric  size_t first;
1313540d2a8bSDimitry Andric  size_t second;
1314540d2a8bSDimitry Andric};
1315540d2a8bSDimitry Andric
1316540d2a8bSDimitry Andric_LIBCPP_INLINE_VISIBILITY
1317540d2a8bSDimitry Andricinline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
1318540d2a8bSDimitry Andric    typedef __scalar_hash<_PairT> _HashT;
1319540d2a8bSDimitry Andric    const _PairT __p = {__lhs, __rhs};
1320540d2a8bSDimitry Andric    return _HashT()(__p);
1321540d2a8bSDimitry Andric}
1322540d2a8bSDimitry Andric
1323540d2a8bSDimitry Andrictemplate<class _Tp>
1324540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<_Tp*>
1325540d2a8bSDimitry Andric    : public unary_function<_Tp*, size_t>
1326540d2a8bSDimitry Andric{
1327540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1328540d2a8bSDimitry Andric    size_t operator()(_Tp* __v) const _NOEXCEPT
1329540d2a8bSDimitry Andric    {
1330540d2a8bSDimitry Andric        union
1331540d2a8bSDimitry Andric        {
1332540d2a8bSDimitry Andric            _Tp* __t;
1333540d2a8bSDimitry Andric            size_t __a;
1334540d2a8bSDimitry Andric        } __u;
1335540d2a8bSDimitry Andric        __u.__t = __v;
1336540d2a8bSDimitry Andric        return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1337540d2a8bSDimitry Andric    }
1338540d2a8bSDimitry Andric};
1339540d2a8bSDimitry Andric
1340540d2a8bSDimitry Andric
1341540d2a8bSDimitry Andrictemplate <>
1342540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<bool>
1343540d2a8bSDimitry Andric    : public unary_function<bool, size_t>
1344540d2a8bSDimitry Andric{
1345540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1346540d2a8bSDimitry Andric    size_t operator()(bool __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1347540d2a8bSDimitry Andric};
1348540d2a8bSDimitry Andric
1349540d2a8bSDimitry Andrictemplate <>
1350540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<char>
1351540d2a8bSDimitry Andric    : public unary_function<char, size_t>
1352540d2a8bSDimitry Andric{
1353540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1354540d2a8bSDimitry Andric    size_t operator()(char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1355540d2a8bSDimitry Andric};
1356540d2a8bSDimitry Andric
1357540d2a8bSDimitry Andrictemplate <>
1358540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<signed char>
1359540d2a8bSDimitry Andric    : public unary_function<signed char, size_t>
1360540d2a8bSDimitry Andric{
1361540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1362540d2a8bSDimitry Andric    size_t operator()(signed char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1363540d2a8bSDimitry Andric};
1364540d2a8bSDimitry Andric
1365540d2a8bSDimitry Andrictemplate <>
1366540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<unsigned char>
1367540d2a8bSDimitry Andric    : public unary_function<unsigned char, size_t>
1368540d2a8bSDimitry Andric{
1369540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1370540d2a8bSDimitry Andric    size_t operator()(unsigned char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1371540d2a8bSDimitry Andric};
1372540d2a8bSDimitry Andric
1373540d2a8bSDimitry Andric#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1374540d2a8bSDimitry Andric
1375540d2a8bSDimitry Andrictemplate <>
1376540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<char16_t>
1377540d2a8bSDimitry Andric    : public unary_function<char16_t, size_t>
1378540d2a8bSDimitry Andric{
1379540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1380540d2a8bSDimitry Andric    size_t operator()(char16_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1381540d2a8bSDimitry Andric};
1382540d2a8bSDimitry Andric
1383540d2a8bSDimitry Andrictemplate <>
1384540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<char32_t>
1385540d2a8bSDimitry Andric    : public unary_function<char32_t, size_t>
1386540d2a8bSDimitry Andric{
1387540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1388540d2a8bSDimitry Andric    size_t operator()(char32_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1389540d2a8bSDimitry Andric};
1390540d2a8bSDimitry Andric
1391540d2a8bSDimitry Andric#endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
1392540d2a8bSDimitry Andric
1393540d2a8bSDimitry Andrictemplate <>
1394540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<wchar_t>
1395540d2a8bSDimitry Andric    : public unary_function<wchar_t, size_t>
1396540d2a8bSDimitry Andric{
1397540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1398540d2a8bSDimitry Andric    size_t operator()(wchar_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1399540d2a8bSDimitry Andric};
1400540d2a8bSDimitry Andric
1401540d2a8bSDimitry Andrictemplate <>
1402540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<short>
1403540d2a8bSDimitry Andric    : public unary_function<short, size_t>
1404540d2a8bSDimitry Andric{
1405540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1406540d2a8bSDimitry Andric    size_t operator()(short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1407540d2a8bSDimitry Andric};
1408540d2a8bSDimitry Andric
1409540d2a8bSDimitry Andrictemplate <>
1410540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<unsigned short>
1411540d2a8bSDimitry Andric    : public unary_function<unsigned short, size_t>
1412540d2a8bSDimitry Andric{
1413540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1414540d2a8bSDimitry Andric    size_t operator()(unsigned short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1415540d2a8bSDimitry Andric};
1416540d2a8bSDimitry Andric
1417540d2a8bSDimitry Andrictemplate <>
1418540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<int>
1419540d2a8bSDimitry Andric    : public unary_function<int, size_t>
1420540d2a8bSDimitry Andric{
1421540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1422540d2a8bSDimitry Andric    size_t operator()(int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1423540d2a8bSDimitry Andric};
1424540d2a8bSDimitry Andric
1425540d2a8bSDimitry Andrictemplate <>
1426540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<unsigned int>
1427540d2a8bSDimitry Andric    : public unary_function<unsigned int, size_t>
1428540d2a8bSDimitry Andric{
1429540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1430540d2a8bSDimitry Andric    size_t operator()(unsigned int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1431540d2a8bSDimitry Andric};
1432540d2a8bSDimitry Andric
1433540d2a8bSDimitry Andrictemplate <>
1434540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<long>
1435540d2a8bSDimitry Andric    : public unary_function<long, size_t>
1436540d2a8bSDimitry Andric{
1437540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1438540d2a8bSDimitry Andric    size_t operator()(long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1439540d2a8bSDimitry Andric};
1440540d2a8bSDimitry Andric
1441540d2a8bSDimitry Andrictemplate <>
1442540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<unsigned long>
1443540d2a8bSDimitry Andric    : public unary_function<unsigned long, size_t>
1444540d2a8bSDimitry Andric{
1445540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1446540d2a8bSDimitry Andric    size_t operator()(unsigned long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1447540d2a8bSDimitry Andric};
1448540d2a8bSDimitry Andric
1449540d2a8bSDimitry Andrictemplate <>
1450540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<long long>
1451540d2a8bSDimitry Andric    : public __scalar_hash<long long>
1452540d2a8bSDimitry Andric{
1453540d2a8bSDimitry Andric};
1454540d2a8bSDimitry Andric
1455540d2a8bSDimitry Andrictemplate <>
1456540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<unsigned long long>
1457540d2a8bSDimitry Andric    : public __scalar_hash<unsigned long long>
1458540d2a8bSDimitry Andric{
1459540d2a8bSDimitry Andric};
1460540d2a8bSDimitry Andric
1461540d2a8bSDimitry Andric#ifndef _LIBCPP_HAS_NO_INT128
1462540d2a8bSDimitry Andric
1463540d2a8bSDimitry Andrictemplate <>
1464540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<__int128_t>
1465540d2a8bSDimitry Andric    : public __scalar_hash<__int128_t>
1466540d2a8bSDimitry Andric{
1467540d2a8bSDimitry Andric};
1468540d2a8bSDimitry Andric
1469540d2a8bSDimitry Andrictemplate <>
1470540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<__uint128_t>
1471540d2a8bSDimitry Andric    : public __scalar_hash<__uint128_t>
1472540d2a8bSDimitry Andric{
1473540d2a8bSDimitry Andric};
1474540d2a8bSDimitry Andric
1475540d2a8bSDimitry Andric#endif
1476540d2a8bSDimitry Andric
1477540d2a8bSDimitry Andrictemplate <>
1478540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<float>
1479540d2a8bSDimitry Andric    : public __scalar_hash<float>
1480540d2a8bSDimitry Andric{
1481540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1482540d2a8bSDimitry Andric    size_t operator()(float __v) const _NOEXCEPT
1483540d2a8bSDimitry Andric    {
1484540d2a8bSDimitry Andric        // -0.0 and 0.0 should return same hash
1485*b5893f02SDimitry Andric       if (__v == 0.0)
1486540d2a8bSDimitry Andric           return 0;
1487540d2a8bSDimitry Andric        return __scalar_hash<float>::operator()(__v);
1488540d2a8bSDimitry Andric    }
1489540d2a8bSDimitry Andric};
1490540d2a8bSDimitry Andric
1491540d2a8bSDimitry Andrictemplate <>
1492540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<double>
1493540d2a8bSDimitry Andric    : public __scalar_hash<double>
1494540d2a8bSDimitry Andric{
1495540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1496540d2a8bSDimitry Andric    size_t operator()(double __v) const _NOEXCEPT
1497540d2a8bSDimitry Andric    {
1498540d2a8bSDimitry Andric        // -0.0 and 0.0 should return same hash
1499*b5893f02SDimitry Andric       if (__v == 0.0)
1500540d2a8bSDimitry Andric           return 0;
1501540d2a8bSDimitry Andric        return __scalar_hash<double>::operator()(__v);
1502540d2a8bSDimitry Andric    }
1503540d2a8bSDimitry Andric};
1504540d2a8bSDimitry Andric
1505540d2a8bSDimitry Andrictemplate <>
1506540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<long double>
1507540d2a8bSDimitry Andric    : public __scalar_hash<long double>
1508540d2a8bSDimitry Andric{
1509540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1510540d2a8bSDimitry Andric    size_t operator()(long double __v) const _NOEXCEPT
1511540d2a8bSDimitry Andric    {
1512540d2a8bSDimitry Andric        // -0.0 and 0.0 should return same hash
1513*b5893f02SDimitry Andric        if (__v == 0.0)
1514540d2a8bSDimitry Andric            return 0;
1515540d2a8bSDimitry Andric#if defined(__i386__)
1516540d2a8bSDimitry Andric        // Zero out padding bits
1517540d2a8bSDimitry Andric        union
1518540d2a8bSDimitry Andric        {
1519540d2a8bSDimitry Andric            long double __t;
1520540d2a8bSDimitry Andric            struct
1521540d2a8bSDimitry Andric            {
1522540d2a8bSDimitry Andric                size_t __a;
1523540d2a8bSDimitry Andric                size_t __b;
1524540d2a8bSDimitry Andric                size_t __c;
1525540d2a8bSDimitry Andric                size_t __d;
1526540d2a8bSDimitry Andric            } __s;
1527540d2a8bSDimitry Andric        } __u;
1528540d2a8bSDimitry Andric        __u.__s.__a = 0;
1529540d2a8bSDimitry Andric        __u.__s.__b = 0;
1530540d2a8bSDimitry Andric        __u.__s.__c = 0;
1531540d2a8bSDimitry Andric        __u.__s.__d = 0;
1532540d2a8bSDimitry Andric        __u.__t = __v;
1533540d2a8bSDimitry Andric        return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
1534540d2a8bSDimitry Andric#elif defined(__x86_64__)
1535540d2a8bSDimitry Andric        // Zero out padding bits
1536540d2a8bSDimitry Andric        union
1537540d2a8bSDimitry Andric        {
1538540d2a8bSDimitry Andric            long double __t;
1539540d2a8bSDimitry Andric            struct
1540540d2a8bSDimitry Andric            {
1541540d2a8bSDimitry Andric                size_t __a;
1542540d2a8bSDimitry Andric                size_t __b;
1543540d2a8bSDimitry Andric            } __s;
1544540d2a8bSDimitry Andric        } __u;
1545540d2a8bSDimitry Andric        __u.__s.__a = 0;
1546540d2a8bSDimitry Andric        __u.__s.__b = 0;
1547540d2a8bSDimitry Andric        __u.__t = __v;
1548540d2a8bSDimitry Andric        return __u.__s.__a ^ __u.__s.__b;
1549540d2a8bSDimitry Andric#else
1550540d2a8bSDimitry Andric        return __scalar_hash<long double>::operator()(__v);
1551540d2a8bSDimitry Andric#endif
1552540d2a8bSDimitry Andric    }
1553540d2a8bSDimitry Andric};
1554540d2a8bSDimitry Andric
1555540d2a8bSDimitry Andric#if _LIBCPP_STD_VER > 11
1556540d2a8bSDimitry Andric
1557540d2a8bSDimitry Andrictemplate <class _Tp, bool = is_enum<_Tp>::value>
1558540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __enum_hash
1559540d2a8bSDimitry Andric    : public unary_function<_Tp, size_t>
1560540d2a8bSDimitry Andric{
1561540d2a8bSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1562540d2a8bSDimitry Andric    size_t operator()(_Tp __v) const _NOEXCEPT
1563540d2a8bSDimitry Andric    {
1564540d2a8bSDimitry Andric        typedef typename underlying_type<_Tp>::type type;
1565540d2a8bSDimitry Andric        return hash<type>{}(static_cast<type>(__v));
1566540d2a8bSDimitry Andric    }
1567540d2a8bSDimitry Andric};
1568540d2a8bSDimitry Andrictemplate <class _Tp>
1569540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __enum_hash<_Tp, false> {
1570540d2a8bSDimitry Andric    __enum_hash() = delete;
1571540d2a8bSDimitry Andric    __enum_hash(__enum_hash const&) = delete;
1572540d2a8bSDimitry Andric    __enum_hash& operator=(__enum_hash const&) = delete;
1573540d2a8bSDimitry Andric};
1574540d2a8bSDimitry Andric
1575540d2a8bSDimitry Andrictemplate <class _Tp>
1576540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp>
1577540d2a8bSDimitry Andric{
1578540d2a8bSDimitry Andric};
1579540d2a8bSDimitry Andric#endif
1580540d2a8bSDimitry Andric
1581540d2a8bSDimitry Andric#if _LIBCPP_STD_VER > 14
1582540d2a8bSDimitry Andric
1583540d2a8bSDimitry Andrictemplate <>
1584540d2a8bSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS hash<nullptr_t>
1585540d2a8bSDimitry Andric  : public unary_function<nullptr_t, size_t>
1586540d2a8bSDimitry Andric{
1587540d2a8bSDimitry Andric  _LIBCPP_INLINE_VISIBILITY
1588540d2a8bSDimitry Andric  size_t operator()(nullptr_t) const _NOEXCEPT {
1589540d2a8bSDimitry Andric    return 662607004ull;
1590540d2a8bSDimitry Andric  }
1591540d2a8bSDimitry Andric};
1592540d2a8bSDimitry Andric#endif
1593540d2a8bSDimitry Andric
1594540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG
1595540d2a8bSDimitry Andrictemplate <class _Key, class _Hash>
1596540d2a8bSDimitry Andricusing __check_hash_requirements = integral_constant<bool,
1597540d2a8bSDimitry Andric    is_copy_constructible<_Hash>::value &&
1598540d2a8bSDimitry Andric    is_move_constructible<_Hash>::value &&
1599540d2a8bSDimitry Andric    __invokable_r<size_t, _Hash, _Key const&>::value
1600540d2a8bSDimitry Andric>;
1601540d2a8bSDimitry Andric
1602540d2a8bSDimitry Andrictemplate <class _Key, class _Hash = std::hash<_Key> >
1603540d2a8bSDimitry Andricusing __has_enabled_hash = integral_constant<bool,
1604540d2a8bSDimitry Andric    __check_hash_requirements<_Key, _Hash>::value &&
1605540d2a8bSDimitry Andric    is_default_constructible<_Hash>::value
1606540d2a8bSDimitry Andric>;
1607540d2a8bSDimitry Andric
1608540d2a8bSDimitry Andric#if _LIBCPP_STD_VER > 14
1609540d2a8bSDimitry Andrictemplate <class _Type, class>
1610540d2a8bSDimitry Andricusing __enable_hash_helper_imp = _Type;
1611540d2a8bSDimitry Andric
1612540d2a8bSDimitry Andrictemplate <class _Type, class ..._Keys>
1613540d2a8bSDimitry Andricusing __enable_hash_helper = __enable_hash_helper_imp<_Type,
1614540d2a8bSDimitry Andric  typename enable_if<__all<__has_enabled_hash<_Keys>::value...>::value>::type
1615540d2a8bSDimitry Andric>;
1616540d2a8bSDimitry Andric#else
1617540d2a8bSDimitry Andrictemplate <class _Type, class ...>
1618540d2a8bSDimitry Andricusing __enable_hash_helper = _Type;
1619540d2a8bSDimitry Andric#endif
1620540d2a8bSDimitry Andric
1621540d2a8bSDimitry Andric#endif // !_LIBCPP_CXX03_LANG
1622540d2a8bSDimitry Andric
16237a984708SDavid Chisnall_LIBCPP_END_NAMESPACE_STD
16247a984708SDavid Chisnall
16257a984708SDavid Chisnall#endif  // _LIBCPP_UTILITY
1626