1// -*- C++ -*- 2//===-------------------------- utility -----------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_UTILITY 11#define _LIBCPP_UTILITY 12 13/* 14 utility synopsis 15 16#include <initializer_list> 17 18namespace std 19{ 20 21template <class T> 22 void 23 swap(T& a, T& b); 24 25namespace rel_ops 26{ 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&); 30 template<class T> bool operator>=(const T&, const T&); 31} 32 33template<class T> 34void 35swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value && 36 is_nothrow_move_assignable<T>::value); 37 38template <class T, size_t N> 39void 40swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b))); 41 42template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept; // constexpr in C++14 43template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14 44 45template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14 46 47template <class T> 48 typename conditional 49 < 50 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value, 51 const T&, 52 T&& 53 >::type 54 move_if_noexcept(T& x) noexcept; // constexpr in C++14 55 56template <class T> constexpr add_const_t<T>& as_const(T& t) noexcept; // C++17 57template <class T> void as_const(const T&&) = delete; // C++17 58 59template <class T> typename add_rvalue_reference<T>::type declval() noexcept; 60 61template <class T1, class T2> 62struct pair 63{ 64 typedef T1 first_type; 65 typedef T2 second_type; 66 67 T1 first; 68 T2 second; 69 70 pair(const pair&) = default; 71 pair(pair&&) = default; 72 explicit(see-below) constexpr pair(); 73 explicit(see-below) pair(const T1& x, const T2& y); // constexpr in C++14 74 template <class U, class V> explicit(see-below) pair(U&& x, V&& y); // constexpr in C++14 75 template <class U, class V> explicit(see-below) pair(const pair<U, V>& p); // constexpr in C++14 76 template <class U, class V> explicit(see-below) pair(pair<U, V>&& p); // constexpr in C++14 77 template <class... Args1, class... Args2> 78 pair(piecewise_construct_t, tuple<Args1...> first_args, 79 tuple<Args2...> second_args); // constexpr in C++20 80 81 template <class U, class V> pair& operator=(const pair<U, V>& p); // constexpr in C++20 82 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value && 83 is_nothrow_move_assignable<T2>::value); // constexpr in C++20 84 template <class U, class V> pair& operator=(pair<U, V>&& p); // constexpr in C++20 85 86 void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> && 87 is_nothrow_swappable_v<T2>); // constexpr in C++20 88}; 89 90template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14 91template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14 92template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14 93template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14 94template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14 95template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14 96 97template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14 98template <class T1, class T2> 99void 100swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20 101 102struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; 103inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); 104 105template <class T> struct tuple_size; 106template <size_t I, class T> struct tuple_element; 107 108template <class T1, class T2> struct tuple_size<pair<T1, T2> >; 109template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >; 110template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >; 111 112template<size_t I, class T1, class T2> 113 typename tuple_element<I, pair<T1, T2> >::type& 114 get(pair<T1, T2>&) noexcept; // constexpr in C++14 115 116template<size_t I, class T1, class T2> 117 const typename tuple_element<I, pair<T1, T2> >::type& 118 get(const pair<T1, T2>&) noexcept; // constexpr in C++14 119 120template<size_t I, class T1, class T2> 121 typename tuple_element<I, pair<T1, T2> >::type&& 122 get(pair<T1, T2>&&) noexcept; // constexpr in C++14 123 124template<size_t I, class T1, class T2> 125 const typename tuple_element<I, pair<T1, T2> >::type&& 126 get(const pair<T1, T2>&&) noexcept; // constexpr in C++14 127 128template<class T1, class T2> 129 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14 130 131template<class T1, class T2> 132 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14 133 134template<class T1, class T2> 135 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14 136 137template<class T1, class T2> 138 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14 139 140template<class T1, class T2> 141 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14 142 143template<class T1, class T2> 144 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14 145 146template<class T1, class T2> 147 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14 148 149template<class T1, class T2> 150 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14 151 152// C++14 153 154template<class T, T... I> 155struct integer_sequence 156{ 157 typedef T value_type; 158 159 static constexpr size_t size() noexcept; 160}; 161 162template<size_t... I> 163 using index_sequence = integer_sequence<size_t, I...>; 164 165template<class T, T N> 166 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>; 167template<size_t N> 168 using make_index_sequence = make_integer_sequence<size_t, N>; 169 170template<class... T> 171 using index_sequence_for = make_index_sequence<sizeof...(T)>; 172 173template<class T, class U=T> 174 T exchange(T& obj, U&& new_value); 175 176// 20.2.7, in-place construction // C++17 177struct in_place_t { 178 explicit in_place_t() = default; 179}; 180inline constexpr in_place_t in_place{}; 181template <class T> 182 struct in_place_type_t { 183 explicit in_place_type_t() = default; 184 }; 185template <class T> 186 inline constexpr in_place_type_t<T> in_place_type{}; 187template <size_t I> 188 struct in_place_index_t { 189 explicit in_place_index_t() = default; 190 }; 191template <size_t I> 192 inline constexpr in_place_index_t<I> in_place_index{}; 193 194// [utility.underlying], to_underlying 195template <class T> 196 constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b 197 198} // std 199 200*/ 201 202#include <__config> 203#include <__tuple> 204#include <compare> 205#include <type_traits> 206#include <initializer_list> 207#include <cstddef> 208#include <cstring> 209#include <cstdint> 210#include <version> 211#include <__debug> 212 213#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 214#pragma GCC system_header 215#endif 216 217_LIBCPP_BEGIN_NAMESPACE_STD 218 219namespace rel_ops 220{ 221 222template<class _Tp> 223inline _LIBCPP_INLINE_VISIBILITY 224bool 225operator!=(const _Tp& __x, const _Tp& __y) 226{ 227 return !(__x == __y); 228} 229 230template<class _Tp> 231inline _LIBCPP_INLINE_VISIBILITY 232bool 233operator> (const _Tp& __x, const _Tp& __y) 234{ 235 return __y < __x; 236} 237 238template<class _Tp> 239inline _LIBCPP_INLINE_VISIBILITY 240bool 241operator<=(const _Tp& __x, const _Tp& __y) 242{ 243 return !(__y < __x); 244} 245 246template<class _Tp> 247inline _LIBCPP_INLINE_VISIBILITY 248bool 249operator>=(const _Tp& __x, const _Tp& __y) 250{ 251 return !(__x < __y); 252} 253 254} // rel_ops 255 256// swap_ranges is defined in <type_traits>` 257 258// swap is defined in <type_traits> 259 260// move_if_noexcept 261 262template <class _Tp> 263_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 264#ifndef _LIBCPP_CXX03_LANG 265typename conditional 266< 267 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value, 268 const _Tp&, 269 _Tp&& 270>::type 271#else // _LIBCPP_CXX03_LANG 272const _Tp& 273#endif 274move_if_noexcept(_Tp& __x) _NOEXCEPT 275{ 276 return _VSTD::move(__x); 277} 278 279#if _LIBCPP_STD_VER > 14 280template <class _Tp> 281_LIBCPP_NODISCARD_EXT constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; } 282 283template <class _Tp> 284void as_const(const _Tp&&) = delete; 285#endif 286 287struct _LIBCPP_TEMPLATE_VIS piecewise_construct_t { explicit piecewise_construct_t() = default; }; 288#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY) 289extern _LIBCPP_EXPORTED_FROM_ABI const piecewise_construct_t piecewise_construct;// = piecewise_construct_t(); 290#else 291/* _LIBCPP_INLINE_VAR */ constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); 292#endif 293 294#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR) 295template <class, class> 296struct __non_trivially_copyable_base { 297 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 298 __non_trivially_copyable_base() _NOEXCEPT {} 299 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY 300 __non_trivially_copyable_base(__non_trivially_copyable_base const&) _NOEXCEPT {} 301}; 302#endif 303 304template <class _T1, class _T2> 305struct _LIBCPP_TEMPLATE_VIS pair 306#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR) 307: private __non_trivially_copyable_base<_T1, _T2> 308#endif 309{ 310 typedef _T1 first_type; 311 typedef _T2 second_type; 312 313 _T1 first; 314 _T2 second; 315 316#if !defined(_LIBCPP_CXX03_LANG) 317 pair(pair const&) = default; 318 pair(pair&&) = default; 319#else 320 // Use the implicitly declared copy constructor in C++03 321#endif 322 323#ifdef _LIBCPP_CXX03_LANG 324 _LIBCPP_INLINE_VISIBILITY 325 pair() : first(), second() {} 326 327 _LIBCPP_INLINE_VISIBILITY 328 pair(_T1 const& __t1, _T2 const& __t2) : first(__t1), second(__t2) {} 329 330 template <class _U1, class _U2> 331 _LIBCPP_INLINE_VISIBILITY 332 pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {} 333 334 _LIBCPP_INLINE_VISIBILITY 335 pair& operator=(pair const& __p) { 336 first = __p.first; 337 second = __p.second; 338 return *this; 339 } 340#else 341 template <bool _Val> 342 using _EnableB _LIBCPP_NODEBUG_TYPE = typename enable_if<_Val, bool>::type; 343 344 struct _CheckArgs { 345 template <int&...> 346 static constexpr bool __enable_explicit_default() { 347 return is_default_constructible<_T1>::value 348 && is_default_constructible<_T2>::value 349 && !__enable_implicit_default<>(); 350 } 351 352 template <int&...> 353 static constexpr bool __enable_implicit_default() { 354 return __is_implicitly_default_constructible<_T1>::value 355 && __is_implicitly_default_constructible<_T2>::value; 356 } 357 358 template <class _U1, class _U2> 359 static constexpr bool __enable_explicit() { 360 return is_constructible<first_type, _U1>::value 361 && is_constructible<second_type, _U2>::value 362 && (!is_convertible<_U1, first_type>::value 363 || !is_convertible<_U2, second_type>::value); 364 } 365 366 template <class _U1, class _U2> 367 static constexpr bool __enable_implicit() { 368 return is_constructible<first_type, _U1>::value 369 && is_constructible<second_type, _U2>::value 370 && is_convertible<_U1, first_type>::value 371 && is_convertible<_U2, second_type>::value; 372 } 373 }; 374 375 template <bool _MaybeEnable> 376 using _CheckArgsDep _LIBCPP_NODEBUG_TYPE = typename conditional< 377 _MaybeEnable, _CheckArgs, __check_tuple_constructor_fail>::type; 378 379 struct _CheckTupleLikeConstructor { 380 template <class _Tuple> 381 static constexpr bool __enable_implicit() { 382 return __tuple_convertible<_Tuple, pair>::value; 383 } 384 385 template <class _Tuple> 386 static constexpr bool __enable_explicit() { 387 return __tuple_constructible<_Tuple, pair>::value 388 && !__tuple_convertible<_Tuple, pair>::value; 389 } 390 391 template <class _Tuple> 392 static constexpr bool __enable_assign() { 393 return __tuple_assignable<_Tuple, pair>::value; 394 } 395 }; 396 397 template <class _Tuple> 398 using _CheckTLC _LIBCPP_NODEBUG_TYPE = typename conditional< 399 __tuple_like_with_size<_Tuple, 2>::value 400 && !is_same<typename decay<_Tuple>::type, pair>::value, 401 _CheckTupleLikeConstructor, 402 __check_tuple_constructor_fail 403 >::type; 404 405 template<bool _Dummy = true, _EnableB< 406 _CheckArgsDep<_Dummy>::__enable_explicit_default() 407 > = false> 408 explicit _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 409 pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value && 410 is_nothrow_default_constructible<second_type>::value) 411 : first(), second() {} 412 413 template<bool _Dummy = true, _EnableB< 414 _CheckArgsDep<_Dummy>::__enable_implicit_default() 415 > = false> 416 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 417 pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value && 418 is_nothrow_default_constructible<second_type>::value) 419 : first(), second() {} 420 421 template <bool _Dummy = true, _EnableB< 422 _CheckArgsDep<_Dummy>::template __enable_explicit<_T1 const&, _T2 const&>() 423 > = false> 424 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 425 explicit pair(_T1 const& __t1, _T2 const& __t2) 426 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value && 427 is_nothrow_copy_constructible<second_type>::value) 428 : first(__t1), second(__t2) {} 429 430 template<bool _Dummy = true, _EnableB< 431 _CheckArgsDep<_Dummy>::template __enable_implicit<_T1 const&, _T2 const&>() 432 > = false> 433 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 434 pair(_T1 const& __t1, _T2 const& __t2) 435 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value && 436 is_nothrow_copy_constructible<second_type>::value) 437 : first(__t1), second(__t2) {} 438 439 template<class _U1, class _U2, _EnableB< 440 _CheckArgs::template __enable_explicit<_U1, _U2>() 441 > = false> 442 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 443 explicit pair(_U1&& __u1, _U2&& __u2) 444 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value && 445 is_nothrow_constructible<second_type, _U2>::value)) 446 : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {} 447 448 template<class _U1, class _U2, _EnableB< 449 _CheckArgs::template __enable_implicit<_U1, _U2>() 450 > = false> 451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 452 pair(_U1&& __u1, _U2&& __u2) 453 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value && 454 is_nothrow_constructible<second_type, _U2>::value)) 455 : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {} 456 457 template<class _U1, class _U2, _EnableB< 458 _CheckArgs::template __enable_explicit<_U1 const&, _U2 const&>() 459 > = false> 460 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 461 explicit pair(pair<_U1, _U2> const& __p) 462 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value && 463 is_nothrow_constructible<second_type, _U2 const&>::value)) 464 : first(__p.first), second(__p.second) {} 465 466 template<class _U1, class _U2, _EnableB< 467 _CheckArgs::template __enable_implicit<_U1 const&, _U2 const&>() 468 > = false> 469 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 470 pair(pair<_U1, _U2> const& __p) 471 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value && 472 is_nothrow_constructible<second_type, _U2 const&>::value)) 473 : first(__p.first), second(__p.second) {} 474 475 template<class _U1, class _U2, _EnableB< 476 _CheckArgs::template __enable_explicit<_U1, _U2>() 477 > = false> 478 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 479 explicit pair(pair<_U1, _U2>&&__p) 480 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value && 481 is_nothrow_constructible<second_type, _U2&&>::value)) 482 : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {} 483 484 template<class _U1, class _U2, _EnableB< 485 _CheckArgs::template __enable_implicit<_U1, _U2>() 486 > = false> 487 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 488 pair(pair<_U1, _U2>&& __p) 489 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value && 490 is_nothrow_constructible<second_type, _U2&&>::value)) 491 : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {} 492 493 template<class _Tuple, _EnableB< 494 _CheckTLC<_Tuple>::template __enable_explicit<_Tuple>() 495 > = false> 496 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 497 explicit pair(_Tuple&& __p) 498 : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))), 499 second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {} 500 501 template<class _Tuple, _EnableB< 502 _CheckTLC<_Tuple>::template __enable_implicit<_Tuple>() 503 > = false> 504 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 505 pair(_Tuple&& __p) 506 : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))), 507 second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {} 508 509 template <class... _Args1, class... _Args2> 510 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 511 pair(piecewise_construct_t __pc, 512 tuple<_Args1...> __first_args, tuple<_Args2...> __second_args) 513 _NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value && 514 is_nothrow_constructible<second_type, _Args2...>::value)) 515 : pair(__pc, __first_args, __second_args, 516 typename __make_tuple_indices<sizeof...(_Args1)>::type(), 517 typename __make_tuple_indices<sizeof...(_Args2) >::type()) {} 518 519 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 520 pair& operator=(typename conditional< 521 is_copy_assignable<first_type>::value && 522 is_copy_assignable<second_type>::value, 523 pair, __nat>::type const& __p) 524 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value && 525 is_nothrow_copy_assignable<second_type>::value) 526 { 527 first = __p.first; 528 second = __p.second; 529 return *this; 530 } 531 532 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 533 pair& operator=(typename conditional< 534 is_move_assignable<first_type>::value && 535 is_move_assignable<second_type>::value, 536 pair, __nat>::type&& __p) 537 _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value && 538 is_nothrow_move_assignable<second_type>::value) 539 { 540 first = _VSTD::forward<first_type>(__p.first); 541 second = _VSTD::forward<second_type>(__p.second); 542 return *this; 543 } 544 545 template <class _Tuple, _EnableB< 546 _CheckTLC<_Tuple>::template __enable_assign<_Tuple>() 547 > = false> 548 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 549 pair& operator=(_Tuple&& __p) { 550 first = _VSTD::get<0>(_VSTD::forward<_Tuple>(__p)); 551 second = _VSTD::get<1>(_VSTD::forward<_Tuple>(__p)); 552 return *this; 553 } 554#endif 555 556 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 557 void 558 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value && 559 __is_nothrow_swappable<second_type>::value) 560 { 561 using _VSTD::swap; 562 swap(first, __p.first); 563 swap(second, __p.second); 564 } 565private: 566 567#ifndef _LIBCPP_CXX03_LANG 568 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2> 569 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 570 pair(piecewise_construct_t, 571 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args, 572 __tuple_indices<_I1...>, __tuple_indices<_I2...>); 573#endif 574}; 575 576#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES 577template<class _T1, class _T2> 578pair(_T1, _T2) -> pair<_T1, _T2>; 579#endif // _LIBCPP_HAS_NO_DEDUCTION_GUIDES 580 581template <class _T1, class _T2> 582inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 583bool 584operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) 585{ 586 return __x.first == __y.first && __x.second == __y.second; 587} 588 589template <class _T1, class _T2> 590inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 591bool 592operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) 593{ 594 return !(__x == __y); 595} 596 597template <class _T1, class _T2> 598inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 599bool 600operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) 601{ 602 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second); 603} 604 605template <class _T1, class _T2> 606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 607bool 608operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) 609{ 610 return __y < __x; 611} 612 613template <class _T1, class _T2> 614inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 615bool 616operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) 617{ 618 return !(__x < __y); 619} 620 621template <class _T1, class _T2> 622inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 623bool 624operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) 625{ 626 return !(__y < __x); 627} 628 629template <class _T1, class _T2> 630inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 631typename enable_if 632< 633 __is_swappable<_T1>::value && 634 __is_swappable<_T2>::value, 635 void 636>::type 637swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 638 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value && 639 __is_nothrow_swappable<_T2>::value)) 640{ 641 __x.swap(__y); 642} 643 644template <class _Tp> 645struct __unwrap_reference { typedef _LIBCPP_NODEBUG_TYPE _Tp type; }; 646 647template <class _Tp> 648struct __unwrap_reference<reference_wrapper<_Tp> > { typedef _LIBCPP_NODEBUG_TYPE _Tp& type; }; 649 650#if _LIBCPP_STD_VER > 17 651template <class _Tp> 652struct unwrap_reference : __unwrap_reference<_Tp> { }; 653 654template <class _Tp> 655struct unwrap_ref_decay : unwrap_reference<typename decay<_Tp>::type> { }; 656#endif // > C++17 657 658template <class _Tp> 659struct __unwrap_ref_decay 660#if _LIBCPP_STD_VER > 17 661 : unwrap_ref_decay<_Tp> 662#else 663 : __unwrap_reference<typename decay<_Tp>::type> 664#endif 665{ }; 666 667#ifndef _LIBCPP_CXX03_LANG 668 669template <class _T1, class _T2> 670inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 671pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type> 672make_pair(_T1&& __t1, _T2&& __t2) 673{ 674 return pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type> 675 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2)); 676} 677 678#else // _LIBCPP_CXX03_LANG 679 680template <class _T1, class _T2> 681inline _LIBCPP_INLINE_VISIBILITY 682pair<_T1,_T2> 683make_pair(_T1 __x, _T2 __y) 684{ 685 return pair<_T1, _T2>(__x, __y); 686} 687 688#endif // _LIBCPP_CXX03_LANG 689 690template <class _T1, class _T2> 691 struct _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> > 692 : public integral_constant<size_t, 2> {}; 693 694template <size_t _Ip, class _T1, class _T2> 695struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> > 696{ 697 static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>"); 698}; 699 700template <class _T1, class _T2> 701struct _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> > 702{ 703 typedef _LIBCPP_NODEBUG_TYPE _T1 type; 704}; 705 706template <class _T1, class _T2> 707struct _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> > 708{ 709 typedef _LIBCPP_NODEBUG_TYPE _T2 type; 710}; 711 712template <size_t _Ip> struct __get_pair; 713 714template <> 715struct __get_pair<0> 716{ 717 template <class _T1, class _T2> 718 static 719 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 720 _T1& 721 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;} 722 723 template <class _T1, class _T2> 724 static 725 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 726 const _T1& 727 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;} 728 729#ifndef _LIBCPP_CXX03_LANG 730 template <class _T1, class _T2> 731 static 732 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 733 _T1&& 734 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);} 735 736 template <class _T1, class _T2> 737 static 738 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 739 const _T1&& 740 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T1>(__p.first);} 741#endif // _LIBCPP_CXX03_LANG 742}; 743 744template <> 745struct __get_pair<1> 746{ 747 template <class _T1, class _T2> 748 static 749 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 750 _T2& 751 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;} 752 753 template <class _T1, class _T2> 754 static 755 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 756 const _T2& 757 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;} 758 759#ifndef _LIBCPP_CXX03_LANG 760 template <class _T1, class _T2> 761 static 762 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 763 _T2&& 764 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);} 765 766 template <class _T1, class _T2> 767 static 768 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 769 const _T2&& 770 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T2>(__p.second);} 771#endif // _LIBCPP_CXX03_LANG 772}; 773 774template <size_t _Ip, class _T1, class _T2> 775inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 776typename tuple_element<_Ip, pair<_T1, _T2> >::type& 777get(pair<_T1, _T2>& __p) _NOEXCEPT 778{ 779 return __get_pair<_Ip>::get(__p); 780} 781 782template <size_t _Ip, class _T1, class _T2> 783inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 784const typename tuple_element<_Ip, pair<_T1, _T2> >::type& 785get(const pair<_T1, _T2>& __p) _NOEXCEPT 786{ 787 return __get_pair<_Ip>::get(__p); 788} 789 790#ifndef _LIBCPP_CXX03_LANG 791template <size_t _Ip, class _T1, class _T2> 792inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 793typename tuple_element<_Ip, pair<_T1, _T2> >::type&& 794get(pair<_T1, _T2>&& __p) _NOEXCEPT 795{ 796 return __get_pair<_Ip>::get(_VSTD::move(__p)); 797} 798 799template <size_t _Ip, class _T1, class _T2> 800inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 801const typename tuple_element<_Ip, pair<_T1, _T2> >::type&& 802get(const pair<_T1, _T2>&& __p) _NOEXCEPT 803{ 804 return __get_pair<_Ip>::get(_VSTD::move(__p)); 805} 806#endif // _LIBCPP_CXX03_LANG 807 808#if _LIBCPP_STD_VER > 11 809template <class _T1, class _T2> 810inline _LIBCPP_INLINE_VISIBILITY 811constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT 812{ 813 return __get_pair<0>::get(__p); 814} 815 816template <class _T1, class _T2> 817inline _LIBCPP_INLINE_VISIBILITY 818constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT 819{ 820 return __get_pair<0>::get(__p); 821} 822 823template <class _T1, class _T2> 824inline _LIBCPP_INLINE_VISIBILITY 825constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT 826{ 827 return __get_pair<0>::get(_VSTD::move(__p)); 828} 829 830template <class _T1, class _T2> 831inline _LIBCPP_INLINE_VISIBILITY 832constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT 833{ 834 return __get_pair<0>::get(_VSTD::move(__p)); 835} 836 837template <class _T1, class _T2> 838inline _LIBCPP_INLINE_VISIBILITY 839constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT 840{ 841 return __get_pair<1>::get(__p); 842} 843 844template <class _T1, class _T2> 845inline _LIBCPP_INLINE_VISIBILITY 846constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT 847{ 848 return __get_pair<1>::get(__p); 849} 850 851template <class _T1, class _T2> 852inline _LIBCPP_INLINE_VISIBILITY 853constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT 854{ 855 return __get_pair<1>::get(_VSTD::move(__p)); 856} 857 858template <class _T1, class _T2> 859inline _LIBCPP_INLINE_VISIBILITY 860constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT 861{ 862 return __get_pair<1>::get(_VSTD::move(__p)); 863} 864 865#endif 866 867#if _LIBCPP_STD_VER > 11 868 869template<class _Tp, _Tp... _Ip> 870struct _LIBCPP_TEMPLATE_VIS integer_sequence 871{ 872 typedef _Tp value_type; 873 static_assert( is_integral<_Tp>::value, 874 "std::integer_sequence can only be instantiated with an integral type" ); 875 static 876 _LIBCPP_INLINE_VISIBILITY 877 constexpr 878 size_t 879 size() noexcept { return sizeof...(_Ip); } 880}; 881 882template<size_t... _Ip> 883 using index_sequence = integer_sequence<size_t, _Ip...>; 884 885#if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE) 886 887template <class _Tp, _Tp _Ep> 888using __make_integer_sequence _LIBCPP_NODEBUG_TYPE = __make_integer_seq<integer_sequence, _Tp, _Ep>; 889 890#else 891 892template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked _LIBCPP_NODEBUG_TYPE = 893 typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>; 894 895template <class _Tp, _Tp _Ep> 896struct __make_integer_sequence_checked 897{ 898 static_assert(is_integral<_Tp>::value, 899 "std::make_integer_sequence can only be instantiated with an integral type" ); 900 static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length"); 901 // Workaround GCC bug by preventing bad installations when 0 <= _Ep 902 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929 903 typedef _LIBCPP_NODEBUG_TYPE __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type; 904}; 905 906template <class _Tp, _Tp _Ep> 907using __make_integer_sequence _LIBCPP_NODEBUG_TYPE = typename __make_integer_sequence_checked<_Tp, _Ep>::type; 908 909#endif 910 911template<class _Tp, _Tp _Np> 912 using make_integer_sequence = __make_integer_sequence<_Tp, _Np>; 913 914template<size_t _Np> 915 using make_index_sequence = make_integer_sequence<size_t, _Np>; 916 917template<class... _Tp> 918 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>; 919 920#endif // _LIBCPP_STD_VER > 11 921 922#if _LIBCPP_STD_VER > 11 923template<class _T1, class _T2 = _T1> 924inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 925_T1 exchange(_T1& __obj, _T2 && __new_value) 926{ 927 _T1 __old_value = _VSTD::move(__obj); 928 __obj = _VSTD::forward<_T2>(__new_value); 929 return __old_value; 930} 931#endif // _LIBCPP_STD_VER > 11 932 933#if _LIBCPP_STD_VER > 14 934 935struct _LIBCPP_TYPE_VIS in_place_t { 936 explicit in_place_t() = default; 937}; 938_LIBCPP_INLINE_VAR constexpr in_place_t in_place{}; 939 940template <class _Tp> 941struct _LIBCPP_TEMPLATE_VIS in_place_type_t { 942 explicit in_place_type_t() = default; 943}; 944template <class _Tp> 945_LIBCPP_INLINE_VAR constexpr in_place_type_t<_Tp> in_place_type{}; 946 947template <size_t _Idx> 948struct _LIBCPP_TEMPLATE_VIS in_place_index_t { 949 explicit in_place_index_t() = default; 950}; 951template <size_t _Idx> 952_LIBCPP_INLINE_VAR constexpr in_place_index_t<_Idx> in_place_index{}; 953 954template <class _Tp> struct __is_inplace_type_imp : false_type {}; 955template <class _Tp> struct __is_inplace_type_imp<in_place_type_t<_Tp>> : true_type {}; 956 957template <class _Tp> 958using __is_inplace_type = __is_inplace_type_imp<__uncvref_t<_Tp>>; 959 960template <class _Tp> struct __is_inplace_index_imp : false_type {}; 961template <size_t _Idx> struct __is_inplace_index_imp<in_place_index_t<_Idx>> : true_type {}; 962 963template <class _Tp> 964using __is_inplace_index = __is_inplace_index_imp<__uncvref_t<_Tp>>; 965 966#endif // _LIBCPP_STD_VER > 14 967 968template <class _Arg, class _Result> 969struct _LIBCPP_TEMPLATE_VIS unary_function 970{ 971 typedef _Arg argument_type; 972 typedef _Result result_type; 973}; 974 975template <class _Size> 976inline _LIBCPP_INLINE_VISIBILITY 977_Size 978__loadword(const void* __p) 979{ 980 _Size __r; 981 _VSTD::memcpy(&__r, __p, sizeof(__r)); 982 return __r; 983} 984 985// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t 986// is 64 bits. This is because cityhash64 uses 64bit x 64bit 987// multiplication, which can be very slow on 32-bit systems. 988template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__> 989struct __murmur2_or_cityhash; 990 991template <class _Size> 992struct __murmur2_or_cityhash<_Size, 32> 993{ 994 inline _Size operator()(const void* __key, _Size __len) 995 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK; 996}; 997 998// murmur2 999template <class _Size> 1000_Size 1001__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) 1002{ 1003 const _Size __m = 0x5bd1e995; 1004 const _Size __r = 24; 1005 _Size __h = __len; 1006 const unsigned char* __data = static_cast<const unsigned char*>(__key); 1007 for (; __len >= 4; __data += 4, __len -= 4) 1008 { 1009 _Size __k = __loadword<_Size>(__data); 1010 __k *= __m; 1011 __k ^= __k >> __r; 1012 __k *= __m; 1013 __h *= __m; 1014 __h ^= __k; 1015 } 1016 switch (__len) 1017 { 1018 case 3: 1019 __h ^= __data[2] << 16; 1020 _LIBCPP_FALLTHROUGH(); 1021 case 2: 1022 __h ^= __data[1] << 8; 1023 _LIBCPP_FALLTHROUGH(); 1024 case 1: 1025 __h ^= __data[0]; 1026 __h *= __m; 1027 } 1028 __h ^= __h >> 13; 1029 __h *= __m; 1030 __h ^= __h >> 15; 1031 return __h; 1032} 1033 1034template <class _Size> 1035struct __murmur2_or_cityhash<_Size, 64> 1036{ 1037 inline _Size operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK; 1038 1039 private: 1040 // Some primes between 2^63 and 2^64. 1041 static const _Size __k0 = 0xc3a5c85c97cb3127ULL; 1042 static const _Size __k1 = 0xb492b66fbe98f273ULL; 1043 static const _Size __k2 = 0x9ae16a3b2f90404fULL; 1044 static const _Size __k3 = 0xc949d7c7509e6557ULL; 1045 1046 static _Size __rotate(_Size __val, int __shift) { 1047 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift))); 1048 } 1049 1050 static _Size __rotate_by_at_least_1(_Size __val, int __shift) { 1051 return (__val >> __shift) | (__val << (64 - __shift)); 1052 } 1053 1054 static _Size __shift_mix(_Size __val) { 1055 return __val ^ (__val >> 47); 1056 } 1057 1058 static _Size __hash_len_16(_Size __u, _Size __v) 1059 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 1060 { 1061 const _Size __mul = 0x9ddfea08eb382d69ULL; 1062 _Size __a = (__u ^ __v) * __mul; 1063 __a ^= (__a >> 47); 1064 _Size __b = (__v ^ __a) * __mul; 1065 __b ^= (__b >> 47); 1066 __b *= __mul; 1067 return __b; 1068 } 1069 1070 static _Size __hash_len_0_to_16(const char* __s, _Size __len) 1071 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 1072 { 1073 if (__len > 8) { 1074 const _Size __a = __loadword<_Size>(__s); 1075 const _Size __b = __loadword<_Size>(__s + __len - 8); 1076 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b; 1077 } 1078 if (__len >= 4) { 1079 const uint32_t __a = __loadword<uint32_t>(__s); 1080 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4); 1081 return __hash_len_16(__len + (__a << 3), __b); 1082 } 1083 if (__len > 0) { 1084 const unsigned char __a = __s[0]; 1085 const unsigned char __b = __s[__len >> 1]; 1086 const unsigned char __c = __s[__len - 1]; 1087 const uint32_t __y = static_cast<uint32_t>(__a) + 1088 (static_cast<uint32_t>(__b) << 8); 1089 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2); 1090 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2; 1091 } 1092 return __k2; 1093 } 1094 1095 static _Size __hash_len_17_to_32(const char *__s, _Size __len) 1096 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 1097 { 1098 const _Size __a = __loadword<_Size>(__s) * __k1; 1099 const _Size __b = __loadword<_Size>(__s + 8); 1100 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2; 1101 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0; 1102 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d, 1103 __a + __rotate(__b ^ __k3, 20) - __c + __len); 1104 } 1105 1106 // Return a 16-byte hash for 48 bytes. Quick and dirty. 1107 // Callers do best to use "random-looking" values for a and b. 1108 static pair<_Size, _Size> __weak_hash_len_32_with_seeds( 1109 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) 1110 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 1111 { 1112 __a += __w; 1113 __b = __rotate(__b + __a + __z, 21); 1114 const _Size __c = __a; 1115 __a += __x; 1116 __a += __y; 1117 __b += __rotate(__a, 44); 1118 return pair<_Size, _Size>(__a + __z, __b + __c); 1119 } 1120 1121 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. 1122 static pair<_Size, _Size> __weak_hash_len_32_with_seeds( 1123 const char* __s, _Size __a, _Size __b) 1124 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 1125 { 1126 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s), 1127 __loadword<_Size>(__s + 8), 1128 __loadword<_Size>(__s + 16), 1129 __loadword<_Size>(__s + 24), 1130 __a, 1131 __b); 1132 } 1133 1134 // Return an 8-byte hash for 33 to 64 bytes. 1135 static _Size __hash_len_33_to_64(const char *__s, size_t __len) 1136 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 1137 { 1138 _Size __z = __loadword<_Size>(__s + 24); 1139 _Size __a = __loadword<_Size>(__s) + 1140 (__len + __loadword<_Size>(__s + __len - 16)) * __k0; 1141 _Size __b = __rotate(__a + __z, 52); 1142 _Size __c = __rotate(__a, 37); 1143 __a += __loadword<_Size>(__s + 8); 1144 __c += __rotate(__a, 7); 1145 __a += __loadword<_Size>(__s + 16); 1146 _Size __vf = __a + __z; 1147 _Size __vs = __b + __rotate(__a, 31) + __c; 1148 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32); 1149 __z += __loadword<_Size>(__s + __len - 8); 1150 __b = __rotate(__a + __z, 52); 1151 __c = __rotate(__a, 37); 1152 __a += __loadword<_Size>(__s + __len - 24); 1153 __c += __rotate(__a, 7); 1154 __a += __loadword<_Size>(__s + __len - 16); 1155 _Size __wf = __a + __z; 1156 _Size __ws = __b + __rotate(__a, 31) + __c; 1157 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0); 1158 return __shift_mix(__r * __k0 + __vs) * __k2; 1159 } 1160}; 1161 1162// cityhash64 1163template <class _Size> 1164_Size 1165__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) 1166{ 1167 const char* __s = static_cast<const char*>(__key); 1168 if (__len <= 32) { 1169 if (__len <= 16) { 1170 return __hash_len_0_to_16(__s, __len); 1171 } else { 1172 return __hash_len_17_to_32(__s, __len); 1173 } 1174 } else if (__len <= 64) { 1175 return __hash_len_33_to_64(__s, __len); 1176 } 1177 1178 // For strings over 64 bytes we hash the end first, and then as we 1179 // loop we keep 56 bytes of state: v, w, x, y, and z. 1180 _Size __x = __loadword<_Size>(__s + __len - 40); 1181 _Size __y = __loadword<_Size>(__s + __len - 16) + 1182 __loadword<_Size>(__s + __len - 56); 1183 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len, 1184 __loadword<_Size>(__s + __len - 24)); 1185 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z); 1186 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x); 1187 __x = __x * __k1 + __loadword<_Size>(__s); 1188 1189 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. 1190 __len = (__len - 1) & ~static_cast<_Size>(63); 1191 do { 1192 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1; 1193 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1; 1194 __x ^= __w.second; 1195 __y += __v.first + __loadword<_Size>(__s + 40); 1196 __z = __rotate(__z + __w.first, 33) * __k1; 1197 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first); 1198 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second, 1199 __y + __loadword<_Size>(__s + 16)); 1200 _VSTD::swap(__z, __x); 1201 __s += 64; 1202 __len -= 64; 1203 } while (__len != 0); 1204 return __hash_len_16( 1205 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z, 1206 __hash_len_16(__v.second, __w.second) + __x); 1207} 1208 1209template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)> 1210struct __scalar_hash; 1211 1212template <class _Tp> 1213struct __scalar_hash<_Tp, 0> 1214 : public unary_function<_Tp, size_t> 1215{ 1216 _LIBCPP_INLINE_VISIBILITY 1217 size_t operator()(_Tp __v) const _NOEXCEPT 1218 { 1219 union 1220 { 1221 _Tp __t; 1222 size_t __a; 1223 } __u; 1224 __u.__a = 0; 1225 __u.__t = __v; 1226 return __u.__a; 1227 } 1228}; 1229 1230template <class _Tp> 1231struct __scalar_hash<_Tp, 1> 1232 : public unary_function<_Tp, size_t> 1233{ 1234 _LIBCPP_INLINE_VISIBILITY 1235 size_t operator()(_Tp __v) const _NOEXCEPT 1236 { 1237 union 1238 { 1239 _Tp __t; 1240 size_t __a; 1241 } __u; 1242 __u.__t = __v; 1243 return __u.__a; 1244 } 1245}; 1246 1247template <class _Tp> 1248struct __scalar_hash<_Tp, 2> 1249 : public unary_function<_Tp, size_t> 1250{ 1251 _LIBCPP_INLINE_VISIBILITY 1252 size_t operator()(_Tp __v) const _NOEXCEPT 1253 { 1254 union 1255 { 1256 _Tp __t; 1257 struct 1258 { 1259 size_t __a; 1260 size_t __b; 1261 } __s; 1262 } __u; 1263 __u.__t = __v; 1264 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u)); 1265 } 1266}; 1267 1268template <class _Tp> 1269struct __scalar_hash<_Tp, 3> 1270 : public unary_function<_Tp, size_t> 1271{ 1272 _LIBCPP_INLINE_VISIBILITY 1273 size_t operator()(_Tp __v) const _NOEXCEPT 1274 { 1275 union 1276 { 1277 _Tp __t; 1278 struct 1279 { 1280 size_t __a; 1281 size_t __b; 1282 size_t __c; 1283 } __s; 1284 } __u; 1285 __u.__t = __v; 1286 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u)); 1287 } 1288}; 1289 1290template <class _Tp> 1291struct __scalar_hash<_Tp, 4> 1292 : public unary_function<_Tp, size_t> 1293{ 1294 _LIBCPP_INLINE_VISIBILITY 1295 size_t operator()(_Tp __v) const _NOEXCEPT 1296 { 1297 union 1298 { 1299 _Tp __t; 1300 struct 1301 { 1302 size_t __a; 1303 size_t __b; 1304 size_t __c; 1305 size_t __d; 1306 } __s; 1307 } __u; 1308 __u.__t = __v; 1309 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u)); 1310 } 1311}; 1312 1313struct _PairT { 1314 size_t first; 1315 size_t second; 1316}; 1317 1318_LIBCPP_INLINE_VISIBILITY 1319inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT { 1320 typedef __scalar_hash<_PairT> _HashT; 1321 const _PairT __p = {__lhs, __rhs}; 1322 return _HashT()(__p); 1323} 1324 1325template<class _Tp> 1326struct _LIBCPP_TEMPLATE_VIS hash<_Tp*> 1327 : public unary_function<_Tp*, size_t> 1328{ 1329 _LIBCPP_INLINE_VISIBILITY 1330 size_t operator()(_Tp* __v) const _NOEXCEPT 1331 { 1332 union 1333 { 1334 _Tp* __t; 1335 size_t __a; 1336 } __u; 1337 __u.__t = __v; 1338 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u)); 1339 } 1340}; 1341 1342 1343template <> 1344struct _LIBCPP_TEMPLATE_VIS hash<bool> 1345 : public unary_function<bool, size_t> 1346{ 1347 _LIBCPP_INLINE_VISIBILITY 1348 size_t operator()(bool __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1349}; 1350 1351template <> 1352struct _LIBCPP_TEMPLATE_VIS hash<char> 1353 : public unary_function<char, size_t> 1354{ 1355 _LIBCPP_INLINE_VISIBILITY 1356 size_t operator()(char __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1357}; 1358 1359template <> 1360struct _LIBCPP_TEMPLATE_VIS hash<signed char> 1361 : public unary_function<signed char, size_t> 1362{ 1363 _LIBCPP_INLINE_VISIBILITY 1364 size_t operator()(signed char __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1365}; 1366 1367template <> 1368struct _LIBCPP_TEMPLATE_VIS hash<unsigned char> 1369 : public unary_function<unsigned char, size_t> 1370{ 1371 _LIBCPP_INLINE_VISIBILITY 1372 size_t operator()(unsigned char __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1373}; 1374 1375#ifndef _LIBCPP_NO_HAS_CHAR8_T 1376template <> 1377struct _LIBCPP_TEMPLATE_VIS hash<char8_t> 1378 : public unary_function<char8_t, size_t> 1379{ 1380 _LIBCPP_INLINE_VISIBILITY 1381 size_t operator()(char8_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1382}; 1383#endif // !_LIBCPP_NO_HAS_CHAR8_T 1384 1385#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS 1386 1387template <> 1388struct _LIBCPP_TEMPLATE_VIS hash<char16_t> 1389 : public unary_function<char16_t, size_t> 1390{ 1391 _LIBCPP_INLINE_VISIBILITY 1392 size_t operator()(char16_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1393}; 1394 1395template <> 1396struct _LIBCPP_TEMPLATE_VIS hash<char32_t> 1397 : public unary_function<char32_t, size_t> 1398{ 1399 _LIBCPP_INLINE_VISIBILITY 1400 size_t operator()(char32_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1401}; 1402 1403#endif // _LIBCPP_HAS_NO_UNICODE_CHARS 1404 1405template <> 1406struct _LIBCPP_TEMPLATE_VIS hash<wchar_t> 1407 : public unary_function<wchar_t, size_t> 1408{ 1409 _LIBCPP_INLINE_VISIBILITY 1410 size_t operator()(wchar_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1411}; 1412 1413template <> 1414struct _LIBCPP_TEMPLATE_VIS hash<short> 1415 : public unary_function<short, size_t> 1416{ 1417 _LIBCPP_INLINE_VISIBILITY 1418 size_t operator()(short __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1419}; 1420 1421template <> 1422struct _LIBCPP_TEMPLATE_VIS hash<unsigned short> 1423 : public unary_function<unsigned short, size_t> 1424{ 1425 _LIBCPP_INLINE_VISIBILITY 1426 size_t operator()(unsigned short __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1427}; 1428 1429template <> 1430struct _LIBCPP_TEMPLATE_VIS hash<int> 1431 : public unary_function<int, size_t> 1432{ 1433 _LIBCPP_INLINE_VISIBILITY 1434 size_t operator()(int __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1435}; 1436 1437template <> 1438struct _LIBCPP_TEMPLATE_VIS hash<unsigned int> 1439 : public unary_function<unsigned int, size_t> 1440{ 1441 _LIBCPP_INLINE_VISIBILITY 1442 size_t operator()(unsigned int __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1443}; 1444 1445template <> 1446struct _LIBCPP_TEMPLATE_VIS hash<long> 1447 : public unary_function<long, size_t> 1448{ 1449 _LIBCPP_INLINE_VISIBILITY 1450 size_t operator()(long __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1451}; 1452 1453template <> 1454struct _LIBCPP_TEMPLATE_VIS hash<unsigned long> 1455 : public unary_function<unsigned long, size_t> 1456{ 1457 _LIBCPP_INLINE_VISIBILITY 1458 size_t operator()(unsigned long __v) const _NOEXCEPT {return static_cast<size_t>(__v);} 1459}; 1460 1461template <> 1462struct _LIBCPP_TEMPLATE_VIS hash<long long> 1463 : public __scalar_hash<long long> 1464{ 1465}; 1466 1467template <> 1468struct _LIBCPP_TEMPLATE_VIS hash<unsigned long long> 1469 : public __scalar_hash<unsigned long long> 1470{ 1471}; 1472 1473#ifndef _LIBCPP_HAS_NO_INT128 1474 1475template <> 1476struct _LIBCPP_TEMPLATE_VIS hash<__int128_t> 1477 : public __scalar_hash<__int128_t> 1478{ 1479}; 1480 1481template <> 1482struct _LIBCPP_TEMPLATE_VIS hash<__uint128_t> 1483 : public __scalar_hash<__uint128_t> 1484{ 1485}; 1486 1487#endif 1488 1489template <> 1490struct _LIBCPP_TEMPLATE_VIS hash<float> 1491 : public __scalar_hash<float> 1492{ 1493 _LIBCPP_INLINE_VISIBILITY 1494 size_t operator()(float __v) const _NOEXCEPT 1495 { 1496 // -0.0 and 0.0 should return same hash 1497 if (__v == 0.0f) 1498 return 0; 1499 return __scalar_hash<float>::operator()(__v); 1500 } 1501}; 1502 1503template <> 1504struct _LIBCPP_TEMPLATE_VIS hash<double> 1505 : public __scalar_hash<double> 1506{ 1507 _LIBCPP_INLINE_VISIBILITY 1508 size_t operator()(double __v) const _NOEXCEPT 1509 { 1510 // -0.0 and 0.0 should return same hash 1511 if (__v == 0.0) 1512 return 0; 1513 return __scalar_hash<double>::operator()(__v); 1514 } 1515}; 1516 1517template <> 1518struct _LIBCPP_TEMPLATE_VIS hash<long double> 1519 : public __scalar_hash<long double> 1520{ 1521 _LIBCPP_INLINE_VISIBILITY 1522 size_t operator()(long double __v) const _NOEXCEPT 1523 { 1524 // -0.0 and 0.0 should return same hash 1525 if (__v == 0.0L) 1526 return 0; 1527#if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__)) 1528 // Zero out padding bits 1529 union 1530 { 1531 long double __t; 1532 struct 1533 { 1534 size_t __a; 1535 size_t __b; 1536 size_t __c; 1537 size_t __d; 1538 } __s; 1539 } __u; 1540 __u.__s.__a = 0; 1541 __u.__s.__b = 0; 1542 __u.__s.__c = 0; 1543 __u.__s.__d = 0; 1544 __u.__t = __v; 1545 return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d; 1546#elif defined(__x86_64__) 1547 // Zero out padding bits 1548 union 1549 { 1550 long double __t; 1551 struct 1552 { 1553 size_t __a; 1554 size_t __b; 1555 } __s; 1556 } __u; 1557 __u.__s.__a = 0; 1558 __u.__s.__b = 0; 1559 __u.__t = __v; 1560 return __u.__s.__a ^ __u.__s.__b; 1561#else 1562 return __scalar_hash<long double>::operator()(__v); 1563#endif 1564 } 1565}; 1566 1567#if _LIBCPP_STD_VER > 11 1568 1569template <class _Tp, bool = is_enum<_Tp>::value> 1570struct _LIBCPP_TEMPLATE_VIS __enum_hash 1571 : public unary_function<_Tp, size_t> 1572{ 1573 _LIBCPP_INLINE_VISIBILITY 1574 size_t operator()(_Tp __v) const _NOEXCEPT 1575 { 1576 typedef typename underlying_type<_Tp>::type type; 1577 return hash<type>{}(static_cast<type>(__v)); 1578 } 1579}; 1580template <class _Tp> 1581struct _LIBCPP_TEMPLATE_VIS __enum_hash<_Tp, false> { 1582 __enum_hash() = delete; 1583 __enum_hash(__enum_hash const&) = delete; 1584 __enum_hash& operator=(__enum_hash const&) = delete; 1585}; 1586 1587template <class _Tp> 1588struct _LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp> 1589{ 1590}; 1591#endif 1592 1593#if _LIBCPP_STD_VER > 14 1594 1595template <> 1596struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t> 1597 : public unary_function<nullptr_t, size_t> 1598{ 1599 _LIBCPP_INLINE_VISIBILITY 1600 size_t operator()(nullptr_t) const _NOEXCEPT { 1601 return 662607004ull; 1602 } 1603}; 1604#endif 1605 1606#ifndef _LIBCPP_CXX03_LANG 1607template <class _Key, class _Hash> 1608using __check_hash_requirements _LIBCPP_NODEBUG_TYPE = integral_constant<bool, 1609 is_copy_constructible<_Hash>::value && 1610 is_move_constructible<_Hash>::value && 1611 __invokable_r<size_t, _Hash, _Key const&>::value 1612>; 1613 1614template <class _Key, class _Hash = hash<_Key> > 1615using __has_enabled_hash _LIBCPP_NODEBUG_TYPE = integral_constant<bool, 1616 __check_hash_requirements<_Key, _Hash>::value && 1617 is_default_constructible<_Hash>::value 1618>; 1619 1620#if _LIBCPP_STD_VER > 14 1621template <class _Type, class> 1622using __enable_hash_helper_imp _LIBCPP_NODEBUG_TYPE = _Type; 1623 1624template <class _Type, class ..._Keys> 1625using __enable_hash_helper _LIBCPP_NODEBUG_TYPE = __enable_hash_helper_imp<_Type, 1626 typename enable_if<__all<__has_enabled_hash<_Keys>::value...>::value>::type 1627>; 1628#else 1629template <class _Type, class ...> 1630using __enable_hash_helper _LIBCPP_NODEBUG_TYPE = _Type; 1631#endif 1632 1633template <class _Tp> 1634_LIBCPP_INLINE_VISIBILITY constexpr typename underlying_type<_Tp>::type 1635__to_underlying(_Tp __val) noexcept { 1636 return static_cast<typename underlying_type<_Tp>::type>(__val); 1637} 1638#endif // !_LIBCPP_CXX03_LANG 1639 1640#if _LIBCPP_STD_VER > 20 1641template <class _Tp> 1642_LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY constexpr underlying_type_t<_Tp> 1643to_underlying(_Tp __val) noexcept { 1644 return _VSTD::__to_underlying(__val); 1645} 1646#endif 1647 1648_LIBCPP_END_NAMESPACE_STD 1649 1650#endif // _LIBCPP_UTILITY 1651