blob: 925b9521a5cbebc67e2e1bfcc6f0c46286651d39 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- utility -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_UTILITY
11#define _LIBCPP_UTILITY
12
13/*
14 utility synopsis
15
Louis Dionneb5769a32018-07-05 16:16:03 +000016#include <initializer_list>
17
Howard Hinnantc51e1022010-05-11 19:42:16 +000018namespace 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
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000033template<class T>
34void
35swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
36 is_nothrow_move_assignable<T>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000037
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000038template <class T, size_t N>
39void
40swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
41
Marshall Clowfe9aa372013-07-15 20:46:11 +000042template <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
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000044
Marshall Clowfe9aa372013-07-15 20:46:11 +000045template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000046
47template <class T>
48 typename conditional
49 <
Howard Hinnanta9a897e2010-11-19 22:17:28 +000050 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +000051 const T&,
52 T&&
53 >::type
Marshall Clowfe9aa372013-07-15 20:46:11 +000054 move_if_noexcept(T& x) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
Marshall Clow5d305742018-02-11 21:51:49 +000056template <class T> constexpr add_const_t<T>& as_const(T& t) noexcept; // C++17
Marshall Clow845efe42015-11-17 00:08:08 +000057template <class T> void as_const(const T&&) = delete; // C++17
58
Howard Hinnantc51e1022010-05-11 19:42:16 +000059template <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;
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000071 pair(pair&&) = default;
Louis Dionnea7a2beb2019-09-26 14:51:10 +000072 explicit(see-below) constexpr pair();
Louis Dionne1afad1d2019-07-22 20:45:23 +000073 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
Howard Hinnantc51e1022010-05-11 19:42:16 +000077 template <class... Args1, class... Args2>
78 pair(piecewise_construct_t, tuple<Args1...> first_args,
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050079 tuple<Args2...> second_args); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050081 template <class U, class V> pair& operator=(const pair<U, V>& p); // constexpr in C++20
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000082 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050083 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
Howard Hinnantc51e1022010-05-11 19:42:16 +000085
Eric Fiselier6bfed252016-04-21 23:38:59 +000086 void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050087 is_nothrow_swappable_v<T2>); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088};
89
Marshall Clowf00c56c2013-07-16 17:45:44 +000090template <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
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050097template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000098template <class T1, class T2>
99void
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500100swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000102struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000103inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104
Marshall Clowce62b4d2019-01-11 21:57:12 +0000105template <class T> struct tuple_size;
Louis Dionnee684aa62019-04-01 16:39:34 +0000106template <size_t I, class T> struct tuple_element;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000107
Marshall Clowf50d66f2014-03-03 06:18:11 +0000108template <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> >;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111
112template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000113 typename tuple_element<I, pair<T1, T2> >::type&
114 get(pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115
116template<size_t I, class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000117 const typename tuple_element<I, pair<T1, T2> >::type&
Marshall Clowf50d66f2014-03-03 06:18:11 +0000118 get(const pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119
Howard Hinnant22e97242010-11-17 19:52:17 +0000120template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000121 typename tuple_element<I, pair<T1, T2> >::type&&
122 get(pair<T1, T2>&&) noexcept; // constexpr in C++14
Howard Hinnant22e97242010-11-17 19:52:17 +0000123
Eric Fiselier6dea8092015-12-18 00:36:55 +0000124template<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
Marshall Clow15b02e02013-07-13 02:54:05 +0000128template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000129 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000130
Eric Fiselier6dea8092015-12-18 00:36:55 +0000131template<class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000132 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000133
Eric Fiselier6dea8092015-12-18 00:36:55 +0000134template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000135 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000136
Eric Fiselier6dea8092015-12-18 00:36:55 +0000137template<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
Marshall Clow9a970d82013-07-01 16:26:55 +0000152// 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
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000173template<class T, class U=T>
Marshall Clow867ca362013-07-08 20:54:40 +0000174 T exchange(T& obj, U&& new_value);
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000175
176// 20.2.7, in-place construction // C++17
Eric Fiselier99b81712016-11-17 19:24:04 +0000177struct in_place_t {
178 explicit in_place_t() = default;
179};
180inline constexpr in_place_t in_place{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000181template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000182 struct in_place_type_t {
183 explicit in_place_type_t() = default;
184 };
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000185template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000186 inline constexpr in_place_type_t<T> in_place_type{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000187template <size_t I>
Eric Fiselier99b81712016-11-17 19:24:04 +0000188 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{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000193
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +0100194// [utility.underlying], to_underlying
195template <class T>
196 constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
197
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198} // std
199
200*/
201
202#include <__config>
203#include <__tuple>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400204#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205#include <type_traits>
Ben Craig23254d22016-04-19 20:13:55 +0000206#include <initializer_list>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000207#include <cstddef>
208#include <cstring>
209#include <cstdint>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000210#include <version>
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000211#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000213#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
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
Zoe Carver72cf75a2019-09-11 17:39:24 +0000256// swap_ranges is defined in <type_traits>`
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Zoe Carver72cf75a2019-09-11 17:39:24 +0000258// swap is defined in <type_traits>
Marshall Clowfd9a8572015-01-06 19:20:49 +0000259
Zoe Carver72cf75a2019-09-11 17:39:24 +0000260// move_if_noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
262template <class _Tp>
Arthur O'Dwyer108facb2021-04-05 14:56:03 -0400263_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000264#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265typename conditional
266<
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000267 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268 const _Tp&,
269 _Tp&&
270>::type
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000271#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272const _Tp&
273#endif
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000274move_if_noexcept(_Tp& __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000276 return _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277}
278
Marshall Clow845efe42015-11-17 00:08:08 +0000279#if _LIBCPP_STD_VER > 14
Arthur O'Dwyer108facb2021-04-05 14:56:03 -0400280template <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;
Marshall Clow845efe42015-11-17 00:08:08 +0000285#endif
286
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000287struct _LIBCPP_TEMPLATE_VIS piecewise_construct_t { explicit piecewise_construct_t() = default; };
Louis Dionne5e0eadd2018-08-01 02:08:59 +0000288#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Louis Dionne5254b372018-10-25 12:13:43 +0000289extern _LIBCPP_EXPORTED_FROM_ABI const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
Howard Hinnant7d7e9582012-04-03 21:09:48 +0000290#else
Marshall Clowbc084382018-01-02 18:57:47 +0000291/* _LIBCPP_INLINE_VAR */ constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
Howard Hinnant7d7e9582012-04-03 21:09:48 +0000292#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000294#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
Eric Fiselierbfb285c2019-01-16 01:54:34 +0000295template <class, class>
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000296struct __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
Eric Fiseliere61db632016-07-25 04:32:07 +0000303
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304template <class _T1, class _T2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000305struct _LIBCPP_TEMPLATE_VIS pair
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000306#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
Eric Fiselierbfb285c2019-01-16 01:54:34 +0000307: private __non_trivially_copyable_base<_T1, _T2>
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000308#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309{
310 typedef _T1 first_type;
311 typedef _T2 second_type;
312
313 _T1 first;
314 _T2 second;
315
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000316#if !defined(_LIBCPP_CXX03_LANG)
Eric Fiselier08f7fe82016-07-18 01:58:37 +0000317 pair(pair const&) = default;
318 pair(pair&&) = default;
319#else
320 // Use the implicitly declared copy constructor in C++03
Marshall Clowf00c56c2013-07-16 17:45:44 +0000321#endif
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000322
Eric Fiseliere61db632016-07-25 04:32:07 +0000323#ifdef _LIBCPP_CXX03_LANG
324 _LIBCPP_INLINE_VISIBILITY
325 pair() : first(), second() {}
Eric Fiselier737a16d2016-07-25 02:36:42 +0000326
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000327 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere61db632016-07-25 04:32:07 +0000328 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>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000342 using _EnableB _LIBCPP_NODEBUG_TYPE = typename enable_if<_Val, bool>::type;
Eric Fiseliere61db632016-07-25 04:32:07 +0000343
344 struct _CheckArgs {
Eric Fiselier18d72a02019-09-30 20:55:30 +0000345 template <int&...>
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000346 static constexpr bool __enable_explicit_default() {
Eric Fiselier18d72a02019-09-30 20:55:30 +0000347 return is_default_constructible<_T1>::value
348 && is_default_constructible<_T2>::value
349 && !__enable_implicit_default<>();
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000350 }
351
Eric Fiselier18d72a02019-09-30 20:55:30 +0000352 template <int&...>
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000353 static constexpr bool __enable_implicit_default() {
Eric Fiselier18d72a02019-09-30 20:55:30 +0000354 return __is_implicitly_default_constructible<_T1>::value
355 && __is_implicitly_default_constructible<_T2>::value;
Eric Fiseliere61db632016-07-25 04:32:07 +0000356 }
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>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000376 using _CheckArgsDep _LIBCPP_NODEBUG_TYPE = typename conditional<
Eric Fiseliere61db632016-07-25 04:32:07 +0000377 _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>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000398 using _CheckTLC _LIBCPP_NODEBUG_TYPE = typename conditional<
Eric Fiseliere61db632016-07-25 04:32:07 +0000399 __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<
Eric Fiselier18d72a02019-09-30 20:55:30 +0000406 _CheckArgsDep<_Dummy>::__enable_explicit_default()
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000407 > = 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<
Eric Fiselier18d72a02019-09-30 20:55:30 +0000414 _CheckArgsDep<_Dummy>::__enable_implicit_default()
Eric Fiseliere61db632016-07-25 04:32:07 +0000415 > = false>
416 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Louis Dionnee7e477a2018-12-11 14:22:28 +0000417 pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
418 is_nothrow_default_constructible<second_type>::value)
419 : first(), second() {}
Eric Fiseliere61db632016-07-25 04:32:07 +0000420
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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000426 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
427 is_nothrow_copy_constructible<second_type>::value)
Eric Fiseliere61db632016-07-25 04:32:07 +0000428 : 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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000435 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
436 is_nothrow_copy_constructible<second_type>::value)
Eric Fiseliere61db632016-07-25 04:32:07 +0000437 : 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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000444 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
445 is_nothrow_constructible<second_type, _U2>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000446 : 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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000453 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
454 is_nothrow_constructible<second_type, _U2>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000455 : 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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000462 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
463 is_nothrow_constructible<second_type, _U2 const&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000464 : 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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000471 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
472 is_nothrow_constructible<second_type, _U2 const&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000473 : 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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000480 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
481 is_nothrow_constructible<second_type, _U2&&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000482 : 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)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000489 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
490 is_nothrow_constructible<second_type, _U2&&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000491 : 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>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200510 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000511 pair(piecewise_construct_t __pc,
512 tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000513 _NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value &&
514 is_nothrow_constructible<second_type, _Args2...>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000515 : pair(__pc, __first_args, __second_args,
516 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
517 typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
518
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200519 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000520 pair& operator=(typename conditional<
521 is_copy_assignable<first_type>::value &&
522 is_copy_assignable<second_type>::value,
523 pair, __nat>::type const& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000524 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
525 is_nothrow_copy_assignable<second_type>::value)
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000526 {
527 first = __p.first;
528 second = __p.second;
529 return *this;
530 }
531
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200532 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000533 pair& operator=(typename conditional<
534 is_move_assignable<first_type>::value &&
535 is_move_assignable<second_type>::value,
536 pair, __nat>::type&& __p)
Eric Fiselier737a16d2016-07-25 02:36:42 +0000537 _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 }
Eric Fiseliere61db632016-07-25 04:32:07 +0000544
545 template <class _Tuple, _EnableB<
Eric Fiselier9e0256b2016-08-29 01:43:41 +0000546 _CheckTLC<_Tuple>::template __enable_assign<_Tuple>()
Eric Fiseliere61db632016-07-25 04:32:07 +0000547 > = false>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200548 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000549 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 }
Eric Fiselier737a16d2016-07-25 02:36:42 +0000554#endif
555
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200556 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000557 void
Howard Hinnant89ef1212011-05-27 19:08:18 +0000558 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
559 __is_nothrow_swappable<second_type>::value)
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000560 {
Marshall Clow8b9bd542015-09-22 17:50:11 +0000561 using _VSTD::swap;
562 swap(first, __p.first);
563 swap(second, __p.second);
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000564 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000566
Eric Fiseliere61db632016-07-25 04:32:07 +0000567#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200569 _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...>);
Eric Fiseliere61db632016-07-25 04:32:07 +0000573#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574};
575
Eric Fiselier8df4ac62017-10-04 00:04:26 +0000576#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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000582inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583bool
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>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000590inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591bool
592operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
593{
594 return !(__x == __y);
595}
596
597template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000598inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599bool
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>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607bool
608operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
609{
610 return __y < __x;
611}
612
613template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000614inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615bool
616operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
617{
618 return !(__x < __y);
619}
620
621template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000622inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623bool
624operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
625{
626 return !(__y < __x);
627}
628
629template <class _T1, class _T2>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200630inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant44267242011-06-01 19:59:32 +0000631typename enable_if
632<
633 __is_swappable<_T1>::value &&
634 __is_swappable<_T2>::value,
635 void
636>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
Howard Hinnant89ef1212011-05-27 19:08:18 +0000638 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
639 __is_nothrow_swappable<_T2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640{
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000641 __x.swap(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642}
643
Louis Dionnedb1892a2018-12-03 14:03:27 +0000644template <class _Tp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000645struct __unwrap_reference { typedef _LIBCPP_NODEBUG_TYPE _Tp type; };
Louis Dionnedb1892a2018-12-03 14:03:27 +0000646
647template <class _Tp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000648struct __unwrap_reference<reference_wrapper<_Tp> > { typedef _LIBCPP_NODEBUG_TYPE _Tp& type; };
Louis Dionnedb1892a2018-12-03 14:03:27 +0000649
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
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000667#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000670inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Louis Dionnedb1892a2018-12-03 14:03:27 +0000671pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672make_pair(_T1&& __t1, _T2&& __t2)
673{
Louis Dionnedb1892a2018-12-03 14:03:27 +0000674 return pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000675 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676}
677
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000678#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679
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
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000688#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690template <class _T1, class _T2>
Marshall Clowce62b4d2019-01-11 21:57:12 +0000691 struct _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000692 : public integral_constant<size_t, 2> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693
Marshall Clow185577f2017-06-12 16:13:17 +0000694template <size_t _Ip, class _T1, class _T2>
Louis Dionnee684aa62019-04-01 16:39:34 +0000695struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
Marshall Clow185577f2017-06-12 16:13:17 +0000696{
697 static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
698};
699
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700template <class _T1, class _T2>
Louis Dionnee684aa62019-04-01 16:39:34 +0000701struct _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000703 typedef _LIBCPP_NODEBUG_TYPE _T1 type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704};
705
706template <class _T1, class _T2>
Louis Dionnee684aa62019-04-01 16:39:34 +0000707struct _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000709 typedef _LIBCPP_NODEBUG_TYPE _T2 type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710};
711
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712template <size_t _Ip> struct __get_pair;
713
714template <>
715struct __get_pair<0>
716{
717 template <class _T1, class _T2>
718 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000719 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 _T1&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000721 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722
723 template <class _T1, class _T2>
724 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000725 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 const _T1&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000727 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnant22e97242010-11-17 19:52:17 +0000728
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000729#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000730 template <class _T1, class _T2>
731 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000732 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000733 _T1&&
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000734 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
Howard Hinnant22e97242010-11-17 19:52:17 +0000735
Eric Fiselier6dea8092015-12-18 00:36:55 +0000736 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);}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000741#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742};
743
744template <>
745struct __get_pair<1>
746{
747 template <class _T1, class _T2>
748 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000749 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 _T2&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000751 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752
753 template <class _T1, class _T2>
754 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000755 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756 const _T2&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000757 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnant22e97242010-11-17 19:52:17 +0000758
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000759#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000760 template <class _T1, class _T2>
761 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000762 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000763 _T2&&
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000764 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
Howard Hinnant22e97242010-11-17 19:52:17 +0000765
Eric Fiselier6dea8092015-12-18 00:36:55 +0000766 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);}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000771#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772};
773
774template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000775inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000777get(pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778{
779 return __get_pair<_Ip>::get(__p);
780}
781
782template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000783inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000785get(const pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786{
787 return __get_pair<_Ip>::get(__p);
788}
789
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000790#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000791template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000792inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000793typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000794get(pair<_T1, _T2>&& __p) _NOEXCEPT
Howard Hinnant22e97242010-11-17 19:52:17 +0000795{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000796 return __get_pair<_Ip>::get(_VSTD::move(__p));
Howard Hinnant22e97242010-11-17 19:52:17 +0000797}
798
Eric Fiselier6dea8092015-12-18 00:36:55 +0000799template <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}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000806#endif // _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000807
Marshall Clow9a970d82013-07-01 16:26:55 +0000808#if _LIBCPP_STD_VER > 11
Marshall Clow15b02e02013-07-13 02:54:05 +0000809template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000810inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000811constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
812{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000813 return __get_pair<0>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000814}
815
816template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000817inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000818constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
819{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000820 return __get_pair<0>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000821}
822
823template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000824inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000825constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
826{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000827 return __get_pair<0>::get(_VSTD::move(__p));
Marshall Clow15b02e02013-07-13 02:54:05 +0000828}
829
830template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000831inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6dea8092015-12-18 00:36:55 +0000832constexpr _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
Marshall Clow15b02e02013-07-13 02:54:05 +0000839constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
840{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000841 return __get_pair<1>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000842}
843
844template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000845inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000846constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
847{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000848 return __get_pair<1>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000849}
850
851template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000852inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000853constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
854{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000855 return __get_pair<1>::get(_VSTD::move(__p));
Marshall Clow15b02e02013-07-13 02:54:05 +0000856}
857
Eric Fiselier6dea8092015-12-18 00:36:55 +0000858template <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
Marshall Clow15b02e02013-07-13 02:54:05 +0000865#endif
866
867#if _LIBCPP_STD_VER > 11
Marshall Clow9a970d82013-07-01 16:26:55 +0000868
869template<class _Tp, _Tp... _Ip>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000870struct _LIBCPP_TEMPLATE_VIS integer_sequence
Marshall Clow9a970d82013-07-01 16:26:55 +0000871{
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
Eric Fiselierd5746922015-12-09 22:03:06 +0000885#if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
886
887template <class _Tp, _Tp _Ep>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000888using __make_integer_sequence _LIBCPP_NODEBUG_TYPE = __make_integer_seq<integer_sequence, _Tp, _Ep>;
Eric Fiselierd5746922015-12-09 22:03:06 +0000889
890#else
891
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000892template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked _LIBCPP_NODEBUG_TYPE =
Eric Fiselier2079cc72016-06-30 22:34:43 +0000893 typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>;
Marshall Clow9a970d82013-07-01 16:26:55 +0000894
895template <class _Tp, _Tp _Ep>
Eric Fiselier2079cc72016-06-30 22:34:43 +0000896struct __make_integer_sequence_checked
Marshall Clow9a970d82013-07-01 16:26:55 +0000897{
898 static_assert(is_integral<_Tp>::value,
899 "std::make_integer_sequence can only be instantiated with an integral type" );
Eric Fiselierd5746922015-12-09 22:03:06 +0000900 static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
Eric Fiselier9b7456e2015-12-16 00:35:45 +0000901 // Workaround GCC bug by preventing bad installations when 0 <= _Ep
902 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000903 typedef _LIBCPP_NODEBUG_TYPE __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
Marshall Clow9a970d82013-07-01 16:26:55 +0000904};
905
Eric Fiselier2079cc72016-06-30 22:34:43 +0000906template <class _Tp, _Tp _Ep>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000907using __make_integer_sequence _LIBCPP_NODEBUG_TYPE = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
Eric Fiselier2079cc72016-06-30 22:34:43 +0000908
Eric Fiselierd5746922015-12-09 22:03:06 +0000909#endif
910
Marshall Clow9a970d82013-07-01 16:26:55 +0000911template<class _Tp, _Tp _Np>
Eric Fiselier2079cc72016-06-30 22:34:43 +0000912 using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
Marshall Clow9a970d82013-07-01 16:26:55 +0000913
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)>;
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000919
Marshall Clow9a970d82013-07-01 16:26:55 +0000920#endif // _LIBCPP_STD_VER > 11
921
Marshall Clow867ca362013-07-08 20:54:40 +0000922#if _LIBCPP_STD_VER > 11
923template<class _T1, class _T2 = _T1>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000924inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow867ca362013-07-08 20:54:40 +0000925_T1 exchange(_T1& __obj, _T2 && __new_value)
926{
Howard Hinnantecc7ba92013-07-08 21:06:38 +0000927 _T1 __old_value = _VSTD::move(__obj);
928 __obj = _VSTD::forward<_T2>(__new_value);
929 return __old_value;
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000930}
Marshall Clow867ca362013-07-08 20:54:40 +0000931#endif // _LIBCPP_STD_VER > 11
932
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000933#if _LIBCPP_STD_VER > 14
934
Eric Fiselier99b81712016-11-17 19:24:04 +0000935struct _LIBCPP_TYPE_VIS in_place_t {
936 explicit in_place_t() = default;
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000937};
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000938_LIBCPP_INLINE_VAR constexpr in_place_t in_place{};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000939
940template <class _Tp>
Eric Fiselier2a1b4d92017-04-20 01:45:15 +0000941struct _LIBCPP_TEMPLATE_VIS in_place_type_t {
Eric Fiselier99b81712016-11-17 19:24:04 +0000942 explicit in_place_type_t() = default;
943};
944template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000945_LIBCPP_INLINE_VAR constexpr in_place_type_t<_Tp> in_place_type{};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000946
Eric Fiselier99b81712016-11-17 19:24:04 +0000947template <size_t _Idx>
Martin Storsjö4d6f2212021-04-06 10:55:33 +0300948struct _LIBCPP_TEMPLATE_VIS in_place_index_t {
Eric Fiselier99b81712016-11-17 19:24:04 +0000949 explicit in_place_index_t() = default;
950};
951template <size_t _Idx>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000952_LIBCPP_INLINE_VAR constexpr in_place_index_t<_Idx> in_place_index{};
Eric Fiselier99b81712016-11-17 19:24:04 +0000953
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 {};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000956
957template <class _Tp>
Eric Fiselier99b81712016-11-17 19:24:04 +0000958using __is_inplace_type = __is_inplace_type_imp<__uncvref_t<_Tp>>;
Eric Fiselier7e0c55e2016-07-24 07:42:13 +0000959
Michael Parka9c61fc2017-06-14 05:51:18 +0000960template <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
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000966#endif // _LIBCPP_STD_VER > 14
967
Eric Fiselier698a97b2017-01-21 00:02:12 +0000968template <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;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500981 _VSTD::memcpy(&__r, __p, sizeof(__r));
Eric Fiselier698a97b2017-01-21 00:02:12 +0000982 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{
Eric Fiselier3308e842017-02-08 00:10:10 +0000994 inline _Size operator()(const void* __key, _Size __len)
995 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
Eric Fiselier698a97b2017-01-21 00:02:12 +0000996};
997
998// murmur2
999template <class _Size>
1000_Size
Eric Fiselier3308e842017-02-08 00:10:10 +00001001__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001002{
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;
Fangrui Songcb135b32018-11-07 23:51:13 +00001020 _LIBCPP_FALLTHROUGH();
Eric Fiselier698a97b2017-01-21 00:02:12 +00001021 case 2:
1022 __h ^= __data[1] << 8;
Fangrui Songcb135b32018-11-07 23:51:13 +00001023 _LIBCPP_FALLTHROUGH();
Eric Fiselier698a97b2017-01-21 00:02:12 +00001024 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{
Eric Fiselier3308e842017-02-08 00:10:10 +00001037 inline _Size operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001038
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
Eric Fiselier3308e842017-02-08 00:10:10 +00001058 static _Size __hash_len_16(_Size __u, _Size __v)
1059 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1060 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001061 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
Eric Fiselier3308e842017-02-08 00:10:10 +00001070 static _Size __hash_len_0_to_16(const char* __s, _Size __len)
1071 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1072 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001073 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
Eric Fiselier3308e842017-02-08 00:10:10 +00001095 static _Size __hash_len_17_to_32(const char *__s, _Size __len)
1096 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1097 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001098 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(
Eric Fiselier3308e842017-02-08 00:10:10 +00001109 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b)
1110 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1111 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001112 __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(
Eric Fiselier3308e842017-02-08 00:10:10 +00001123 const char* __s, _Size __a, _Size __b)
1124 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1125 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001126 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.
Eric Fiselier3308e842017-02-08 00:10:10 +00001135 static _Size __hash_len_33_to_64(const char *__s, size_t __len)
1136 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1137 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001138 _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
Eric Fiselier3308e842017-02-08 00:10:10 +00001165__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001166{
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));
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001200 _VSTD::swap(__z, __x);
Eric Fiselier698a97b2017-01-21 00:02:12 +00001201 __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
Yuriy Chernyshovfbb87fa2020-12-08 13:39:56 -05001375#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
Eric Fiselier698a97b2017-01-21 00:02:12 +00001385#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
Bruce Mitchenerb5da8e92019-07-01 16:13:31 +00001497 if (__v == 0.0f)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001498 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
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001511 if (__v == 0.0)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001512 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
Bruce Mitchenerb5da8e92019-07-01 16:13:31 +00001525 if (__v == 0.0L)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001526 return 0;
Harald van Dijk6de97772020-11-29 13:52:28 +00001527#if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
Eric Fiselier698a97b2017-01-21 00:02:12 +00001528 // 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
Eric Fiselierd90b9312017-03-03 22:35:58 +00001607template <class _Key, class _Hash>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001608using __check_hash_requirements _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Eric Fiselier698a97b2017-01-21 00:02:12 +00001609 is_copy_constructible<_Hash>::value &&
1610 is_move_constructible<_Hash>::value &&
1611 __invokable_r<size_t, _Hash, _Key const&>::value
1612>;
1613
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001614template <class _Key, class _Hash = hash<_Key> >
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001615using __has_enabled_hash _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Eric Fiselierd90b9312017-03-03 22:35:58 +00001616 __check_hash_requirements<_Key, _Hash>::value &&
1617 is_default_constructible<_Hash>::value
1618>;
1619
Eric Fiselier698a97b2017-01-21 00:02:12 +00001620#if _LIBCPP_STD_VER > 14
1621template <class _Type, class>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001622using __enable_hash_helper_imp _LIBCPP_NODEBUG_TYPE = _Type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001623
1624template <class _Type, class ..._Keys>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001625using __enable_hash_helper _LIBCPP_NODEBUG_TYPE = __enable_hash_helper_imp<_Type,
Eric Fiselier698a97b2017-01-21 00:02:12 +00001626 typename enable_if<__all<__has_enabled_hash<_Keys>::value...>::value>::type
1627>;
1628#else
1629template <class _Type, class ...>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001630using __enable_hash_helper _LIBCPP_NODEBUG_TYPE = _Type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001631#endif
1632
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +01001633template <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}
Eric Fiselier698a97b2017-01-21 00:02:12 +00001638#endif // !_LIBCPP_CXX03_LANG
1639
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +01001640#if _LIBCPP_STD_VER > 20
1641template <class _Tp>
Arthur O'Dwyer108facb2021-04-05 14:56:03 -04001642_LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY constexpr underlying_type_t<_Tp>
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +01001643to_underlying(_Tp __val) noexcept {
1644 return _VSTD::__to_underlying(__val);
1645}
1646#endif
1647
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648_LIBCPP_END_NAMESPACE_STD
1649
1650#endif // _LIBCPP_UTILITY