blob: bfde01c587e227ae056359cfde098e43b1f83f61 [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>
Marshall Clowfe9aa372013-07-15 20:46:11 +0000263inline _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
280template <class _Tp> constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
281template <class _Tp> void as_const(const _Tp&&) = delete;
282#endif
283
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000284struct _LIBCPP_TEMPLATE_VIS piecewise_construct_t { explicit piecewise_construct_t() = default; };
Louis Dionne5e0eadd2018-08-01 02:08:59 +0000285#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Louis Dionne5254b372018-10-25 12:13:43 +0000286extern _LIBCPP_EXPORTED_FROM_ABI const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
Howard Hinnant7d7e9582012-04-03 21:09:48 +0000287#else
Marshall Clowbc084382018-01-02 18:57:47 +0000288/* _LIBCPP_INLINE_VAR */ constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
Howard Hinnant7d7e9582012-04-03 21:09:48 +0000289#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000291#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
Eric Fiselierbfb285c2019-01-16 01:54:34 +0000292template <class, class>
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000293struct __non_trivially_copyable_base {
294 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
295 __non_trivially_copyable_base() _NOEXCEPT {}
296 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
297 __non_trivially_copyable_base(__non_trivially_copyable_base const&) _NOEXCEPT {}
298};
299#endif
Eric Fiseliere61db632016-07-25 04:32:07 +0000300
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301template <class _T1, class _T2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000302struct _LIBCPP_TEMPLATE_VIS pair
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000303#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
Eric Fiselierbfb285c2019-01-16 01:54:34 +0000304: private __non_trivially_copyable_base<_T1, _T2>
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000305#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306{
307 typedef _T1 first_type;
308 typedef _T2 second_type;
309
310 _T1 first;
311 _T2 second;
312
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000313#if !defined(_LIBCPP_CXX03_LANG)
Eric Fiselier08f7fe82016-07-18 01:58:37 +0000314 pair(pair const&) = default;
315 pair(pair&&) = default;
316#else
317 // Use the implicitly declared copy constructor in C++03
Marshall Clowf00c56c2013-07-16 17:45:44 +0000318#endif
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000319
Eric Fiseliere61db632016-07-25 04:32:07 +0000320#ifdef _LIBCPP_CXX03_LANG
321 _LIBCPP_INLINE_VISIBILITY
322 pair() : first(), second() {}
Eric Fiselier737a16d2016-07-25 02:36:42 +0000323
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000324 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere61db632016-07-25 04:32:07 +0000325 pair(_T1 const& __t1, _T2 const& __t2) : first(__t1), second(__t2) {}
326
327 template <class _U1, class _U2>
328 _LIBCPP_INLINE_VISIBILITY
329 pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
330
331 _LIBCPP_INLINE_VISIBILITY
332 pair& operator=(pair const& __p) {
333 first = __p.first;
334 second = __p.second;
335 return *this;
336 }
337#else
338 template <bool _Val>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000339 using _EnableB _LIBCPP_NODEBUG_TYPE = typename enable_if<_Val, bool>::type;
Eric Fiseliere61db632016-07-25 04:32:07 +0000340
341 struct _CheckArgs {
Eric Fiselier18d72a02019-09-30 20:55:30 +0000342 template <int&...>
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000343 static constexpr bool __enable_explicit_default() {
Eric Fiselier18d72a02019-09-30 20:55:30 +0000344 return is_default_constructible<_T1>::value
345 && is_default_constructible<_T2>::value
346 && !__enable_implicit_default<>();
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000347 }
348
Eric Fiselier18d72a02019-09-30 20:55:30 +0000349 template <int&...>
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000350 static constexpr bool __enable_implicit_default() {
Eric Fiselier18d72a02019-09-30 20:55:30 +0000351 return __is_implicitly_default_constructible<_T1>::value
352 && __is_implicitly_default_constructible<_T2>::value;
Eric Fiseliere61db632016-07-25 04:32:07 +0000353 }
354
355 template <class _U1, class _U2>
356 static constexpr bool __enable_explicit() {
357 return is_constructible<first_type, _U1>::value
358 && is_constructible<second_type, _U2>::value
359 && (!is_convertible<_U1, first_type>::value
360 || !is_convertible<_U2, second_type>::value);
361 }
362
363 template <class _U1, class _U2>
364 static constexpr bool __enable_implicit() {
365 return is_constructible<first_type, _U1>::value
366 && is_constructible<second_type, _U2>::value
367 && is_convertible<_U1, first_type>::value
368 && is_convertible<_U2, second_type>::value;
369 }
370 };
371
372 template <bool _MaybeEnable>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000373 using _CheckArgsDep _LIBCPP_NODEBUG_TYPE = typename conditional<
Eric Fiseliere61db632016-07-25 04:32:07 +0000374 _MaybeEnable, _CheckArgs, __check_tuple_constructor_fail>::type;
375
376 struct _CheckTupleLikeConstructor {
377 template <class _Tuple>
378 static constexpr bool __enable_implicit() {
379 return __tuple_convertible<_Tuple, pair>::value;
380 }
381
382 template <class _Tuple>
383 static constexpr bool __enable_explicit() {
384 return __tuple_constructible<_Tuple, pair>::value
385 && !__tuple_convertible<_Tuple, pair>::value;
386 }
387
388 template <class _Tuple>
389 static constexpr bool __enable_assign() {
390 return __tuple_assignable<_Tuple, pair>::value;
391 }
392 };
393
394 template <class _Tuple>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000395 using _CheckTLC _LIBCPP_NODEBUG_TYPE = typename conditional<
Eric Fiseliere61db632016-07-25 04:32:07 +0000396 __tuple_like_with_size<_Tuple, 2>::value
397 && !is_same<typename decay<_Tuple>::type, pair>::value,
398 _CheckTupleLikeConstructor,
399 __check_tuple_constructor_fail
400 >::type;
401
402 template<bool _Dummy = true, _EnableB<
Eric Fiselier18d72a02019-09-30 20:55:30 +0000403 _CheckArgsDep<_Dummy>::__enable_explicit_default()
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000404 > = false>
405 explicit _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
406 pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
407 is_nothrow_default_constructible<second_type>::value)
408 : first(), second() {}
409
410 template<bool _Dummy = true, _EnableB<
Eric Fiselier18d72a02019-09-30 20:55:30 +0000411 _CheckArgsDep<_Dummy>::__enable_implicit_default()
Eric Fiseliere61db632016-07-25 04:32:07 +0000412 > = false>
413 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Louis Dionnee7e477a2018-12-11 14:22:28 +0000414 pair() _NOEXCEPT_(is_nothrow_default_constructible<first_type>::value &&
415 is_nothrow_default_constructible<second_type>::value)
416 : first(), second() {}
Eric Fiseliere61db632016-07-25 04:32:07 +0000417
418 template <bool _Dummy = true, _EnableB<
419 _CheckArgsDep<_Dummy>::template __enable_explicit<_T1 const&, _T2 const&>()
420 > = false>
421 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
422 explicit pair(_T1 const& __t1, _T2 const& __t2)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000423 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
424 is_nothrow_copy_constructible<second_type>::value)
Eric Fiseliere61db632016-07-25 04:32:07 +0000425 : first(__t1), second(__t2) {}
426
427 template<bool _Dummy = true, _EnableB<
428 _CheckArgsDep<_Dummy>::template __enable_implicit<_T1 const&, _T2 const&>()
429 > = false>
430 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
431 pair(_T1 const& __t1, _T2 const& __t2)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000432 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
433 is_nothrow_copy_constructible<second_type>::value)
Eric Fiseliere61db632016-07-25 04:32:07 +0000434 : first(__t1), second(__t2) {}
435
436 template<class _U1, class _U2, _EnableB<
437 _CheckArgs::template __enable_explicit<_U1, _U2>()
438 > = false>
439 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
440 explicit pair(_U1&& __u1, _U2&& __u2)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000441 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
442 is_nothrow_constructible<second_type, _U2>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000443 : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
444
445 template<class _U1, class _U2, _EnableB<
446 _CheckArgs::template __enable_implicit<_U1, _U2>()
447 > = false>
448 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
449 pair(_U1&& __u1, _U2&& __u2)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000450 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1>::value &&
451 is_nothrow_constructible<second_type, _U2>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000452 : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
453
454 template<class _U1, class _U2, _EnableB<
455 _CheckArgs::template __enable_explicit<_U1 const&, _U2 const&>()
456 > = false>
457 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
458 explicit pair(pair<_U1, _U2> const& __p)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000459 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
460 is_nothrow_constructible<second_type, _U2 const&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000461 : first(__p.first), second(__p.second) {}
462
463 template<class _U1, class _U2, _EnableB<
464 _CheckArgs::template __enable_implicit<_U1 const&, _U2 const&>()
465 > = false>
466 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
467 pair(pair<_U1, _U2> const& __p)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000468 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1 const&>::value &&
469 is_nothrow_constructible<second_type, _U2 const&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000470 : first(__p.first), second(__p.second) {}
471
472 template<class _U1, class _U2, _EnableB<
473 _CheckArgs::template __enable_explicit<_U1, _U2>()
474 > = false>
475 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
476 explicit pair(pair<_U1, _U2>&&__p)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000477 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
478 is_nothrow_constructible<second_type, _U2&&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000479 : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
480
481 template<class _U1, class _U2, _EnableB<
482 _CheckArgs::template __enable_implicit<_U1, _U2>()
483 > = false>
484 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
485 pair(pair<_U1, _U2>&& __p)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000486 _NOEXCEPT_((is_nothrow_constructible<first_type, _U1&&>::value &&
487 is_nothrow_constructible<second_type, _U2&&>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000488 : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
489
490 template<class _Tuple, _EnableB<
491 _CheckTLC<_Tuple>::template __enable_explicit<_Tuple>()
492 > = false>
493 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
494 explicit pair(_Tuple&& __p)
495 : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
496 second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
497
498 template<class _Tuple, _EnableB<
499 _CheckTLC<_Tuple>::template __enable_implicit<_Tuple>()
500 > = false>
501 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
502 pair(_Tuple&& __p)
503 : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
504 second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
505
506 template <class... _Args1, class... _Args2>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200507 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000508 pair(piecewise_construct_t __pc,
509 tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
Louis Dionnee7e477a2018-12-11 14:22:28 +0000510 _NOEXCEPT_((is_nothrow_constructible<first_type, _Args1...>::value &&
511 is_nothrow_constructible<second_type, _Args2...>::value))
Eric Fiseliere61db632016-07-25 04:32:07 +0000512 : pair(__pc, __first_args, __second_args,
513 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
514 typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
515
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200516 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000517 pair& operator=(typename conditional<
518 is_copy_assignable<first_type>::value &&
519 is_copy_assignable<second_type>::value,
520 pair, __nat>::type const& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000521 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
522 is_nothrow_copy_assignable<second_type>::value)
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000523 {
524 first = __p.first;
525 second = __p.second;
526 return *this;
527 }
528
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200529 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000530 pair& operator=(typename conditional<
531 is_move_assignable<first_type>::value &&
532 is_move_assignable<second_type>::value,
533 pair, __nat>::type&& __p)
Eric Fiselier737a16d2016-07-25 02:36:42 +0000534 _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
535 is_nothrow_move_assignable<second_type>::value)
536 {
537 first = _VSTD::forward<first_type>(__p.first);
538 second = _VSTD::forward<second_type>(__p.second);
539 return *this;
540 }
Eric Fiseliere61db632016-07-25 04:32:07 +0000541
542 template <class _Tuple, _EnableB<
Eric Fiselier9e0256b2016-08-29 01:43:41 +0000543 _CheckTLC<_Tuple>::template __enable_assign<_Tuple>()
Eric Fiseliere61db632016-07-25 04:32:07 +0000544 > = false>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200545 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere61db632016-07-25 04:32:07 +0000546 pair& operator=(_Tuple&& __p) {
547 first = _VSTD::get<0>(_VSTD::forward<_Tuple>(__p));
548 second = _VSTD::get<1>(_VSTD::forward<_Tuple>(__p));
549 return *this;
550 }
Eric Fiselier737a16d2016-07-25 02:36:42 +0000551#endif
552
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200553 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000554 void
Howard Hinnant89ef1212011-05-27 19:08:18 +0000555 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
556 __is_nothrow_swappable<second_type>::value)
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000557 {
Marshall Clow8b9bd542015-09-22 17:50:11 +0000558 using _VSTD::swap;
559 swap(first, __p.first);
560 swap(second, __p.second);
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000561 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000563
Eric Fiseliere61db632016-07-25 04:32:07 +0000564#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200566 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
567 pair(piecewise_construct_t,
568 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
569 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
Eric Fiseliere61db632016-07-25 04:32:07 +0000570#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571};
572
Eric Fiselier8df4ac62017-10-04 00:04:26 +0000573#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
574template<class _T1, class _T2>
575pair(_T1, _T2) -> pair<_T1, _T2>;
576#endif // _LIBCPP_HAS_NO_DEDUCTION_GUIDES
577
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000579inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580bool
581operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
582{
583 return __x.first == __y.first && __x.second == __y.second;
584}
585
586template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000587inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588bool
589operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
590{
591 return !(__x == __y);
592}
593
594template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000595inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596bool
597operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
598{
599 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
600}
601
602template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000603inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604bool
605operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
606{
607 return __y < __x;
608}
609
610template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000611inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612bool
613operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
614{
615 return !(__x < __y);
616}
617
618template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000619inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620bool
621operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
622{
623 return !(__y < __x);
624}
625
626template <class _T1, class _T2>
Michael Schellenberger Costad41ace62020-09-02 21:20:33 +0200627inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant44267242011-06-01 19:59:32 +0000628typename enable_if
629<
630 __is_swappable<_T1>::value &&
631 __is_swappable<_T2>::value,
632 void
633>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
Howard Hinnant89ef1212011-05-27 19:08:18 +0000635 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
636 __is_nothrow_swappable<_T2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637{
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000638 __x.swap(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639}
640
Louis Dionnedb1892a2018-12-03 14:03:27 +0000641template <class _Tp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000642struct __unwrap_reference { typedef _LIBCPP_NODEBUG_TYPE _Tp type; };
Louis Dionnedb1892a2018-12-03 14:03:27 +0000643
644template <class _Tp>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000645struct __unwrap_reference<reference_wrapper<_Tp> > { typedef _LIBCPP_NODEBUG_TYPE _Tp& type; };
Louis Dionnedb1892a2018-12-03 14:03:27 +0000646
647#if _LIBCPP_STD_VER > 17
648template <class _Tp>
649struct unwrap_reference : __unwrap_reference<_Tp> { };
650
651template <class _Tp>
652struct unwrap_ref_decay : unwrap_reference<typename decay<_Tp>::type> { };
653#endif // > C++17
654
655template <class _Tp>
656struct __unwrap_ref_decay
657#if _LIBCPP_STD_VER > 17
658 : unwrap_ref_decay<_Tp>
659#else
660 : __unwrap_reference<typename decay<_Tp>::type>
661#endif
662{ };
663
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000664#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000667inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Louis Dionnedb1892a2018-12-03 14:03:27 +0000668pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669make_pair(_T1&& __t1, _T2&& __t2)
670{
Louis Dionnedb1892a2018-12-03 14:03:27 +0000671 return pair<typename __unwrap_ref_decay<_T1>::type, typename __unwrap_ref_decay<_T2>::type>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000672 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673}
674
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000675#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676
677template <class _T1, class _T2>
678inline _LIBCPP_INLINE_VISIBILITY
679pair<_T1,_T2>
680make_pair(_T1 __x, _T2 __y)
681{
682 return pair<_T1, _T2>(__x, __y);
683}
684
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000685#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687template <class _T1, class _T2>
Marshall Clowce62b4d2019-01-11 21:57:12 +0000688 struct _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000689 : public integral_constant<size_t, 2> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690
Marshall Clow185577f2017-06-12 16:13:17 +0000691template <size_t _Ip, class _T1, class _T2>
Louis Dionnee684aa62019-04-01 16:39:34 +0000692struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
Marshall Clow185577f2017-06-12 16:13:17 +0000693{
694 static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
695};
696
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697template <class _T1, class _T2>
Louis Dionnee684aa62019-04-01 16:39:34 +0000698struct _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000700 typedef _LIBCPP_NODEBUG_TYPE _T1 type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701};
702
703template <class _T1, class _T2>
Louis Dionnee684aa62019-04-01 16:39:34 +0000704struct _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705{
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000706 typedef _LIBCPP_NODEBUG_TYPE _T2 type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707};
708
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709template <size_t _Ip> struct __get_pair;
710
711template <>
712struct __get_pair<0>
713{
714 template <class _T1, class _T2>
715 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000716 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000717 _T1&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000718 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719
720 template <class _T1, class _T2>
721 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000722 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 const _T1&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000724 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnant22e97242010-11-17 19:52:17 +0000725
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000726#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000727 template <class _T1, class _T2>
728 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000729 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000730 _T1&&
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000731 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
Howard Hinnant22e97242010-11-17 19:52:17 +0000732
Eric Fiselier6dea8092015-12-18 00:36:55 +0000733 template <class _T1, class _T2>
734 static
735 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
736 const _T1&&
737 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T1>(__p.first);}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000738#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739};
740
741template <>
742struct __get_pair<1>
743{
744 template <class _T1, class _T2>
745 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000746 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 _T2&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000748 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749
750 template <class _T1, class _T2>
751 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000752 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753 const _T2&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000754 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnant22e97242010-11-17 19:52:17 +0000755
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000756#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000757 template <class _T1, class _T2>
758 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000759 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000760 _T2&&
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000761 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
Howard Hinnant22e97242010-11-17 19:52:17 +0000762
Eric Fiselier6dea8092015-12-18 00:36:55 +0000763 template <class _T1, class _T2>
764 static
765 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
766 const _T2&&
767 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T2>(__p.second);}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000768#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769};
770
771template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000772inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000774get(pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775{
776 return __get_pair<_Ip>::get(__p);
777}
778
779template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000780inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000782get(const pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783{
784 return __get_pair<_Ip>::get(__p);
785}
786
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000787#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000788template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000789inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000790typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000791get(pair<_T1, _T2>&& __p) _NOEXCEPT
Howard Hinnant22e97242010-11-17 19:52:17 +0000792{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000793 return __get_pair<_Ip>::get(_VSTD::move(__p));
Howard Hinnant22e97242010-11-17 19:52:17 +0000794}
795
Eric Fiselier6dea8092015-12-18 00:36:55 +0000796template <size_t _Ip, class _T1, class _T2>
797inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
798const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
799get(const pair<_T1, _T2>&& __p) _NOEXCEPT
800{
801 return __get_pair<_Ip>::get(_VSTD::move(__p));
802}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000803#endif // _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000804
Marshall Clow9a970d82013-07-01 16:26:55 +0000805#if _LIBCPP_STD_VER > 11
Marshall Clow15b02e02013-07-13 02:54:05 +0000806template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000807inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000808constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
809{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000810 return __get_pair<0>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000811}
812
813template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000814inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000815constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
816{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000817 return __get_pair<0>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000818}
819
820template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000821inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000822constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
823{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000824 return __get_pair<0>::get(_VSTD::move(__p));
Marshall Clow15b02e02013-07-13 02:54:05 +0000825}
826
827template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000828inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6dea8092015-12-18 00:36:55 +0000829constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
830{
831 return __get_pair<0>::get(_VSTD::move(__p));
832}
833
834template <class _T1, class _T2>
835inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000836constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
837{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000838 return __get_pair<1>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000839}
840
841template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000842inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000843constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
844{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000845 return __get_pair<1>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000846}
847
848template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000849inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000850constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
851{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000852 return __get_pair<1>::get(_VSTD::move(__p));
Marshall Clow15b02e02013-07-13 02:54:05 +0000853}
854
Eric Fiselier6dea8092015-12-18 00:36:55 +0000855template <class _T1, class _T2>
856inline _LIBCPP_INLINE_VISIBILITY
857constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
858{
859 return __get_pair<1>::get(_VSTD::move(__p));
860}
861
Marshall Clow15b02e02013-07-13 02:54:05 +0000862#endif
863
864#if _LIBCPP_STD_VER > 11
Marshall Clow9a970d82013-07-01 16:26:55 +0000865
866template<class _Tp, _Tp... _Ip>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000867struct _LIBCPP_TEMPLATE_VIS integer_sequence
Marshall Clow9a970d82013-07-01 16:26:55 +0000868{
869 typedef _Tp value_type;
870 static_assert( is_integral<_Tp>::value,
871 "std::integer_sequence can only be instantiated with an integral type" );
872 static
873 _LIBCPP_INLINE_VISIBILITY
874 constexpr
875 size_t
876 size() noexcept { return sizeof...(_Ip); }
877};
878
879template<size_t... _Ip>
880 using index_sequence = integer_sequence<size_t, _Ip...>;
881
Eric Fiselierd5746922015-12-09 22:03:06 +0000882#if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
883
884template <class _Tp, _Tp _Ep>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000885using __make_integer_sequence _LIBCPP_NODEBUG_TYPE = __make_integer_seq<integer_sequence, _Tp, _Ep>;
Eric Fiselierd5746922015-12-09 22:03:06 +0000886
887#else
888
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000889template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked _LIBCPP_NODEBUG_TYPE =
Eric Fiselier2079cc72016-06-30 22:34:43 +0000890 typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>;
Marshall Clow9a970d82013-07-01 16:26:55 +0000891
892template <class _Tp, _Tp _Ep>
Eric Fiselier2079cc72016-06-30 22:34:43 +0000893struct __make_integer_sequence_checked
Marshall Clow9a970d82013-07-01 16:26:55 +0000894{
895 static_assert(is_integral<_Tp>::value,
896 "std::make_integer_sequence can only be instantiated with an integral type" );
Eric Fiselierd5746922015-12-09 22:03:06 +0000897 static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
Eric Fiselier9b7456e2015-12-16 00:35:45 +0000898 // Workaround GCC bug by preventing bad installations when 0 <= _Ep
899 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000900 typedef _LIBCPP_NODEBUG_TYPE __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
Marshall Clow9a970d82013-07-01 16:26:55 +0000901};
902
Eric Fiselier2079cc72016-06-30 22:34:43 +0000903template <class _Tp, _Tp _Ep>
Eric Fiselier4fc82a22019-06-12 02:03:31 +0000904using __make_integer_sequence _LIBCPP_NODEBUG_TYPE = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
Eric Fiselier2079cc72016-06-30 22:34:43 +0000905
Eric Fiselierd5746922015-12-09 22:03:06 +0000906#endif
907
Marshall Clow9a970d82013-07-01 16:26:55 +0000908template<class _Tp, _Tp _Np>
Eric Fiselier2079cc72016-06-30 22:34:43 +0000909 using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
Marshall Clow9a970d82013-07-01 16:26:55 +0000910
911template<size_t _Np>
912 using make_index_sequence = make_integer_sequence<size_t, _Np>;
913
914template<class... _Tp>
915 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000916
Marshall Clow9a970d82013-07-01 16:26:55 +0000917#endif // _LIBCPP_STD_VER > 11
918
Marshall Clow867ca362013-07-08 20:54:40 +0000919#if _LIBCPP_STD_VER > 11
920template<class _T1, class _T2 = _T1>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000921inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow867ca362013-07-08 20:54:40 +0000922_T1 exchange(_T1& __obj, _T2 && __new_value)
923{
Howard Hinnantecc7ba92013-07-08 21:06:38 +0000924 _T1 __old_value = _VSTD::move(__obj);
925 __obj = _VSTD::forward<_T2>(__new_value);
926 return __old_value;
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000927}
Marshall Clow867ca362013-07-08 20:54:40 +0000928#endif // _LIBCPP_STD_VER > 11
929
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000930#if _LIBCPP_STD_VER > 14
931
Eric Fiselier99b81712016-11-17 19:24:04 +0000932struct _LIBCPP_TYPE_VIS in_place_t {
933 explicit in_place_t() = default;
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000934};
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000935_LIBCPP_INLINE_VAR constexpr in_place_t in_place{};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000936
937template <class _Tp>
Eric Fiselier2a1b4d92017-04-20 01:45:15 +0000938struct _LIBCPP_TEMPLATE_VIS in_place_type_t {
Eric Fiselier99b81712016-11-17 19:24:04 +0000939 explicit in_place_type_t() = default;
940};
941template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000942_LIBCPP_INLINE_VAR constexpr in_place_type_t<_Tp> in_place_type{};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000943
Eric Fiselier99b81712016-11-17 19:24:04 +0000944template <size_t _Idx>
Martin Storsjö4d6f2212021-04-06 10:55:33 +0300945struct _LIBCPP_TEMPLATE_VIS in_place_index_t {
Eric Fiselier99b81712016-11-17 19:24:04 +0000946 explicit in_place_index_t() = default;
947};
948template <size_t _Idx>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000949_LIBCPP_INLINE_VAR constexpr in_place_index_t<_Idx> in_place_index{};
Eric Fiselier99b81712016-11-17 19:24:04 +0000950
951template <class _Tp> struct __is_inplace_type_imp : false_type {};
952template <class _Tp> struct __is_inplace_type_imp<in_place_type_t<_Tp>> : true_type {};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000953
954template <class _Tp>
Eric Fiselier99b81712016-11-17 19:24:04 +0000955using __is_inplace_type = __is_inplace_type_imp<__uncvref_t<_Tp>>;
Eric Fiselier7e0c55e2016-07-24 07:42:13 +0000956
Michael Parka9c61fc2017-06-14 05:51:18 +0000957template <class _Tp> struct __is_inplace_index_imp : false_type {};
958template <size_t _Idx> struct __is_inplace_index_imp<in_place_index_t<_Idx>> : true_type {};
959
960template <class _Tp>
961using __is_inplace_index = __is_inplace_index_imp<__uncvref_t<_Tp>>;
962
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000963#endif // _LIBCPP_STD_VER > 14
964
Eric Fiselier698a97b2017-01-21 00:02:12 +0000965template <class _Arg, class _Result>
966struct _LIBCPP_TEMPLATE_VIS unary_function
967{
968 typedef _Arg argument_type;
969 typedef _Result result_type;
970};
971
972template <class _Size>
973inline _LIBCPP_INLINE_VISIBILITY
974_Size
975__loadword(const void* __p)
976{
977 _Size __r;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500978 _VSTD::memcpy(&__r, __p, sizeof(__r));
Eric Fiselier698a97b2017-01-21 00:02:12 +0000979 return __r;
980}
981
982// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
983// is 64 bits. This is because cityhash64 uses 64bit x 64bit
984// multiplication, which can be very slow on 32-bit systems.
985template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
986struct __murmur2_or_cityhash;
987
988template <class _Size>
989struct __murmur2_or_cityhash<_Size, 32>
990{
Eric Fiselier3308e842017-02-08 00:10:10 +0000991 inline _Size operator()(const void* __key, _Size __len)
992 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
Eric Fiselier698a97b2017-01-21 00:02:12 +0000993};
994
995// murmur2
996template <class _Size>
997_Size
Eric Fiselier3308e842017-02-08 00:10:10 +0000998__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Eric Fiselier698a97b2017-01-21 00:02:12 +0000999{
1000 const _Size __m = 0x5bd1e995;
1001 const _Size __r = 24;
1002 _Size __h = __len;
1003 const unsigned char* __data = static_cast<const unsigned char*>(__key);
1004 for (; __len >= 4; __data += 4, __len -= 4)
1005 {
1006 _Size __k = __loadword<_Size>(__data);
1007 __k *= __m;
1008 __k ^= __k >> __r;
1009 __k *= __m;
1010 __h *= __m;
1011 __h ^= __k;
1012 }
1013 switch (__len)
1014 {
1015 case 3:
1016 __h ^= __data[2] << 16;
Fangrui Songcb135b32018-11-07 23:51:13 +00001017 _LIBCPP_FALLTHROUGH();
Eric Fiselier698a97b2017-01-21 00:02:12 +00001018 case 2:
1019 __h ^= __data[1] << 8;
Fangrui Songcb135b32018-11-07 23:51:13 +00001020 _LIBCPP_FALLTHROUGH();
Eric Fiselier698a97b2017-01-21 00:02:12 +00001021 case 1:
1022 __h ^= __data[0];
1023 __h *= __m;
1024 }
1025 __h ^= __h >> 13;
1026 __h *= __m;
1027 __h ^= __h >> 15;
1028 return __h;
1029}
1030
1031template <class _Size>
1032struct __murmur2_or_cityhash<_Size, 64>
1033{
Eric Fiselier3308e842017-02-08 00:10:10 +00001034 inline _Size operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001035
1036 private:
1037 // Some primes between 2^63 and 2^64.
1038 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
1039 static const _Size __k1 = 0xb492b66fbe98f273ULL;
1040 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
1041 static const _Size __k3 = 0xc949d7c7509e6557ULL;
1042
1043 static _Size __rotate(_Size __val, int __shift) {
1044 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
1045 }
1046
1047 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
1048 return (__val >> __shift) | (__val << (64 - __shift));
1049 }
1050
1051 static _Size __shift_mix(_Size __val) {
1052 return __val ^ (__val >> 47);
1053 }
1054
Eric Fiselier3308e842017-02-08 00:10:10 +00001055 static _Size __hash_len_16(_Size __u, _Size __v)
1056 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1057 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001058 const _Size __mul = 0x9ddfea08eb382d69ULL;
1059 _Size __a = (__u ^ __v) * __mul;
1060 __a ^= (__a >> 47);
1061 _Size __b = (__v ^ __a) * __mul;
1062 __b ^= (__b >> 47);
1063 __b *= __mul;
1064 return __b;
1065 }
1066
Eric Fiselier3308e842017-02-08 00:10:10 +00001067 static _Size __hash_len_0_to_16(const char* __s, _Size __len)
1068 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1069 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001070 if (__len > 8) {
1071 const _Size __a = __loadword<_Size>(__s);
1072 const _Size __b = __loadword<_Size>(__s + __len - 8);
1073 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
1074 }
1075 if (__len >= 4) {
1076 const uint32_t __a = __loadword<uint32_t>(__s);
1077 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
1078 return __hash_len_16(__len + (__a << 3), __b);
1079 }
1080 if (__len > 0) {
1081 const unsigned char __a = __s[0];
1082 const unsigned char __b = __s[__len >> 1];
1083 const unsigned char __c = __s[__len - 1];
1084 const uint32_t __y = static_cast<uint32_t>(__a) +
1085 (static_cast<uint32_t>(__b) << 8);
1086 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
1087 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
1088 }
1089 return __k2;
1090 }
1091
Eric Fiselier3308e842017-02-08 00:10:10 +00001092 static _Size __hash_len_17_to_32(const char *__s, _Size __len)
1093 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1094 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001095 const _Size __a = __loadword<_Size>(__s) * __k1;
1096 const _Size __b = __loadword<_Size>(__s + 8);
1097 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
1098 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
1099 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
1100 __a + __rotate(__b ^ __k3, 20) - __c + __len);
1101 }
1102
1103 // Return a 16-byte hash for 48 bytes. Quick and dirty.
1104 // Callers do best to use "random-looking" values for a and b.
1105 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
Eric Fiselier3308e842017-02-08 00:10:10 +00001106 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b)
1107 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1108 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001109 __a += __w;
1110 __b = __rotate(__b + __a + __z, 21);
1111 const _Size __c = __a;
1112 __a += __x;
1113 __a += __y;
1114 __b += __rotate(__a, 44);
1115 return pair<_Size, _Size>(__a + __z, __b + __c);
1116 }
1117
1118 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
1119 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
Eric Fiselier3308e842017-02-08 00:10:10 +00001120 const char* __s, _Size __a, _Size __b)
1121 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1122 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001123 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
1124 __loadword<_Size>(__s + 8),
1125 __loadword<_Size>(__s + 16),
1126 __loadword<_Size>(__s + 24),
1127 __a,
1128 __b);
1129 }
1130
1131 // Return an 8-byte hash for 33 to 64 bytes.
Eric Fiselier3308e842017-02-08 00:10:10 +00001132 static _Size __hash_len_33_to_64(const char *__s, size_t __len)
1133 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1134 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001135 _Size __z = __loadword<_Size>(__s + 24);
1136 _Size __a = __loadword<_Size>(__s) +
1137 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
1138 _Size __b = __rotate(__a + __z, 52);
1139 _Size __c = __rotate(__a, 37);
1140 __a += __loadword<_Size>(__s + 8);
1141 __c += __rotate(__a, 7);
1142 __a += __loadword<_Size>(__s + 16);
1143 _Size __vf = __a + __z;
1144 _Size __vs = __b + __rotate(__a, 31) + __c;
1145 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
1146 __z += __loadword<_Size>(__s + __len - 8);
1147 __b = __rotate(__a + __z, 52);
1148 __c = __rotate(__a, 37);
1149 __a += __loadword<_Size>(__s + __len - 24);
1150 __c += __rotate(__a, 7);
1151 __a += __loadword<_Size>(__s + __len - 16);
1152 _Size __wf = __a + __z;
1153 _Size __ws = __b + __rotate(__a, 31) + __c;
1154 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
1155 return __shift_mix(__r * __k0 + __vs) * __k2;
1156 }
1157};
1158
1159// cityhash64
1160template <class _Size>
1161_Size
Eric Fiselier3308e842017-02-08 00:10:10 +00001162__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001163{
1164 const char* __s = static_cast<const char*>(__key);
1165 if (__len <= 32) {
1166 if (__len <= 16) {
1167 return __hash_len_0_to_16(__s, __len);
1168 } else {
1169 return __hash_len_17_to_32(__s, __len);
1170 }
1171 } else if (__len <= 64) {
1172 return __hash_len_33_to_64(__s, __len);
1173 }
1174
1175 // For strings over 64 bytes we hash the end first, and then as we
1176 // loop we keep 56 bytes of state: v, w, x, y, and z.
1177 _Size __x = __loadword<_Size>(__s + __len - 40);
1178 _Size __y = __loadword<_Size>(__s + __len - 16) +
1179 __loadword<_Size>(__s + __len - 56);
1180 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
1181 __loadword<_Size>(__s + __len - 24));
1182 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
1183 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
1184 __x = __x * __k1 + __loadword<_Size>(__s);
1185
1186 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
1187 __len = (__len - 1) & ~static_cast<_Size>(63);
1188 do {
1189 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
1190 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
1191 __x ^= __w.second;
1192 __y += __v.first + __loadword<_Size>(__s + 40);
1193 __z = __rotate(__z + __w.first, 33) * __k1;
1194 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
1195 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
1196 __y + __loadword<_Size>(__s + 16));
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001197 _VSTD::swap(__z, __x);
Eric Fiselier698a97b2017-01-21 00:02:12 +00001198 __s += 64;
1199 __len -= 64;
1200 } while (__len != 0);
1201 return __hash_len_16(
1202 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
1203 __hash_len_16(__v.second, __w.second) + __x);
1204}
1205
1206template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
1207struct __scalar_hash;
1208
1209template <class _Tp>
1210struct __scalar_hash<_Tp, 0>
1211 : public unary_function<_Tp, size_t>
1212{
1213 _LIBCPP_INLINE_VISIBILITY
1214 size_t operator()(_Tp __v) const _NOEXCEPT
1215 {
1216 union
1217 {
1218 _Tp __t;
1219 size_t __a;
1220 } __u;
1221 __u.__a = 0;
1222 __u.__t = __v;
1223 return __u.__a;
1224 }
1225};
1226
1227template <class _Tp>
1228struct __scalar_hash<_Tp, 1>
1229 : public unary_function<_Tp, size_t>
1230{
1231 _LIBCPP_INLINE_VISIBILITY
1232 size_t operator()(_Tp __v) const _NOEXCEPT
1233 {
1234 union
1235 {
1236 _Tp __t;
1237 size_t __a;
1238 } __u;
1239 __u.__t = __v;
1240 return __u.__a;
1241 }
1242};
1243
1244template <class _Tp>
1245struct __scalar_hash<_Tp, 2>
1246 : public unary_function<_Tp, size_t>
1247{
1248 _LIBCPP_INLINE_VISIBILITY
1249 size_t operator()(_Tp __v) const _NOEXCEPT
1250 {
1251 union
1252 {
1253 _Tp __t;
1254 struct
1255 {
1256 size_t __a;
1257 size_t __b;
1258 } __s;
1259 } __u;
1260 __u.__t = __v;
1261 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1262 }
1263};
1264
1265template <class _Tp>
1266struct __scalar_hash<_Tp, 3>
1267 : public unary_function<_Tp, size_t>
1268{
1269 _LIBCPP_INLINE_VISIBILITY
1270 size_t operator()(_Tp __v) const _NOEXCEPT
1271 {
1272 union
1273 {
1274 _Tp __t;
1275 struct
1276 {
1277 size_t __a;
1278 size_t __b;
1279 size_t __c;
1280 } __s;
1281 } __u;
1282 __u.__t = __v;
1283 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1284 }
1285};
1286
1287template <class _Tp>
1288struct __scalar_hash<_Tp, 4>
1289 : public unary_function<_Tp, size_t>
1290{
1291 _LIBCPP_INLINE_VISIBILITY
1292 size_t operator()(_Tp __v) const _NOEXCEPT
1293 {
1294 union
1295 {
1296 _Tp __t;
1297 struct
1298 {
1299 size_t __a;
1300 size_t __b;
1301 size_t __c;
1302 size_t __d;
1303 } __s;
1304 } __u;
1305 __u.__t = __v;
1306 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1307 }
1308};
1309
1310struct _PairT {
1311 size_t first;
1312 size_t second;
1313};
1314
1315_LIBCPP_INLINE_VISIBILITY
1316inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
1317 typedef __scalar_hash<_PairT> _HashT;
1318 const _PairT __p = {__lhs, __rhs};
1319 return _HashT()(__p);
1320}
1321
1322template<class _Tp>
1323struct _LIBCPP_TEMPLATE_VIS hash<_Tp*>
1324 : public unary_function<_Tp*, size_t>
1325{
1326 _LIBCPP_INLINE_VISIBILITY
1327 size_t operator()(_Tp* __v) const _NOEXCEPT
1328 {
1329 union
1330 {
1331 _Tp* __t;
1332 size_t __a;
1333 } __u;
1334 __u.__t = __v;
1335 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1336 }
1337};
1338
1339
1340template <>
1341struct _LIBCPP_TEMPLATE_VIS hash<bool>
1342 : public unary_function<bool, size_t>
1343{
1344 _LIBCPP_INLINE_VISIBILITY
1345 size_t operator()(bool __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1346};
1347
1348template <>
1349struct _LIBCPP_TEMPLATE_VIS hash<char>
1350 : public unary_function<char, size_t>
1351{
1352 _LIBCPP_INLINE_VISIBILITY
1353 size_t operator()(char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1354};
1355
1356template <>
1357struct _LIBCPP_TEMPLATE_VIS hash<signed char>
1358 : public unary_function<signed char, size_t>
1359{
1360 _LIBCPP_INLINE_VISIBILITY
1361 size_t operator()(signed char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1362};
1363
1364template <>
1365struct _LIBCPP_TEMPLATE_VIS hash<unsigned char>
1366 : public unary_function<unsigned char, size_t>
1367{
1368 _LIBCPP_INLINE_VISIBILITY
1369 size_t operator()(unsigned char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1370};
1371
Yuriy Chernyshovfbb87fa2020-12-08 13:39:56 -05001372#ifndef _LIBCPP_NO_HAS_CHAR8_T
1373template <>
1374struct _LIBCPP_TEMPLATE_VIS hash<char8_t>
1375 : public unary_function<char8_t, size_t>
1376{
1377 _LIBCPP_INLINE_VISIBILITY
1378 size_t operator()(char8_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1379};
1380#endif // !_LIBCPP_NO_HAS_CHAR8_T
1381
Eric Fiselier698a97b2017-01-21 00:02:12 +00001382#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1383
1384template <>
1385struct _LIBCPP_TEMPLATE_VIS hash<char16_t>
1386 : public unary_function<char16_t, size_t>
1387{
1388 _LIBCPP_INLINE_VISIBILITY
1389 size_t operator()(char16_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1390};
1391
1392template <>
1393struct _LIBCPP_TEMPLATE_VIS hash<char32_t>
1394 : public unary_function<char32_t, size_t>
1395{
1396 _LIBCPP_INLINE_VISIBILITY
1397 size_t operator()(char32_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1398};
1399
1400#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
1401
1402template <>
1403struct _LIBCPP_TEMPLATE_VIS hash<wchar_t>
1404 : public unary_function<wchar_t, size_t>
1405{
1406 _LIBCPP_INLINE_VISIBILITY
1407 size_t operator()(wchar_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1408};
1409
1410template <>
1411struct _LIBCPP_TEMPLATE_VIS hash<short>
1412 : public unary_function<short, size_t>
1413{
1414 _LIBCPP_INLINE_VISIBILITY
1415 size_t operator()(short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1416};
1417
1418template <>
1419struct _LIBCPP_TEMPLATE_VIS hash<unsigned short>
1420 : public unary_function<unsigned short, size_t>
1421{
1422 _LIBCPP_INLINE_VISIBILITY
1423 size_t operator()(unsigned short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1424};
1425
1426template <>
1427struct _LIBCPP_TEMPLATE_VIS hash<int>
1428 : public unary_function<int, size_t>
1429{
1430 _LIBCPP_INLINE_VISIBILITY
1431 size_t operator()(int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1432};
1433
1434template <>
1435struct _LIBCPP_TEMPLATE_VIS hash<unsigned int>
1436 : public unary_function<unsigned int, size_t>
1437{
1438 _LIBCPP_INLINE_VISIBILITY
1439 size_t operator()(unsigned int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1440};
1441
1442template <>
1443struct _LIBCPP_TEMPLATE_VIS hash<long>
1444 : public unary_function<long, size_t>
1445{
1446 _LIBCPP_INLINE_VISIBILITY
1447 size_t operator()(long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1448};
1449
1450template <>
1451struct _LIBCPP_TEMPLATE_VIS hash<unsigned long>
1452 : public unary_function<unsigned long, size_t>
1453{
1454 _LIBCPP_INLINE_VISIBILITY
1455 size_t operator()(unsigned long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1456};
1457
1458template <>
1459struct _LIBCPP_TEMPLATE_VIS hash<long long>
1460 : public __scalar_hash<long long>
1461{
1462};
1463
1464template <>
1465struct _LIBCPP_TEMPLATE_VIS hash<unsigned long long>
1466 : public __scalar_hash<unsigned long long>
1467{
1468};
1469
1470#ifndef _LIBCPP_HAS_NO_INT128
1471
1472template <>
1473struct _LIBCPP_TEMPLATE_VIS hash<__int128_t>
1474 : public __scalar_hash<__int128_t>
1475{
1476};
1477
1478template <>
1479struct _LIBCPP_TEMPLATE_VIS hash<__uint128_t>
1480 : public __scalar_hash<__uint128_t>
1481{
1482};
1483
1484#endif
1485
1486template <>
1487struct _LIBCPP_TEMPLATE_VIS hash<float>
1488 : public __scalar_hash<float>
1489{
1490 _LIBCPP_INLINE_VISIBILITY
1491 size_t operator()(float __v) const _NOEXCEPT
1492 {
1493 // -0.0 and 0.0 should return same hash
Bruce Mitchenerb5da8e92019-07-01 16:13:31 +00001494 if (__v == 0.0f)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001495 return 0;
1496 return __scalar_hash<float>::operator()(__v);
1497 }
1498};
1499
1500template <>
1501struct _LIBCPP_TEMPLATE_VIS hash<double>
1502 : public __scalar_hash<double>
1503{
1504 _LIBCPP_INLINE_VISIBILITY
1505 size_t operator()(double __v) const _NOEXCEPT
1506 {
1507 // -0.0 and 0.0 should return same hash
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001508 if (__v == 0.0)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001509 return 0;
1510 return __scalar_hash<double>::operator()(__v);
1511 }
1512};
1513
1514template <>
1515struct _LIBCPP_TEMPLATE_VIS hash<long double>
1516 : public __scalar_hash<long double>
1517{
1518 _LIBCPP_INLINE_VISIBILITY
1519 size_t operator()(long double __v) const _NOEXCEPT
1520 {
1521 // -0.0 and 0.0 should return same hash
Bruce Mitchenerb5da8e92019-07-01 16:13:31 +00001522 if (__v == 0.0L)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001523 return 0;
Harald van Dijk6de97772020-11-29 13:52:28 +00001524#if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
Eric Fiselier698a97b2017-01-21 00:02:12 +00001525 // Zero out padding bits
1526 union
1527 {
1528 long double __t;
1529 struct
1530 {
1531 size_t __a;
1532 size_t __b;
1533 size_t __c;
1534 size_t __d;
1535 } __s;
1536 } __u;
1537 __u.__s.__a = 0;
1538 __u.__s.__b = 0;
1539 __u.__s.__c = 0;
1540 __u.__s.__d = 0;
1541 __u.__t = __v;
1542 return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
1543#elif defined(__x86_64__)
1544 // Zero out padding bits
1545 union
1546 {
1547 long double __t;
1548 struct
1549 {
1550 size_t __a;
1551 size_t __b;
1552 } __s;
1553 } __u;
1554 __u.__s.__a = 0;
1555 __u.__s.__b = 0;
1556 __u.__t = __v;
1557 return __u.__s.__a ^ __u.__s.__b;
1558#else
1559 return __scalar_hash<long double>::operator()(__v);
1560#endif
1561 }
1562};
1563
1564#if _LIBCPP_STD_VER > 11
1565
1566template <class _Tp, bool = is_enum<_Tp>::value>
1567struct _LIBCPP_TEMPLATE_VIS __enum_hash
1568 : public unary_function<_Tp, size_t>
1569{
1570 _LIBCPP_INLINE_VISIBILITY
1571 size_t operator()(_Tp __v) const _NOEXCEPT
1572 {
1573 typedef typename underlying_type<_Tp>::type type;
1574 return hash<type>{}(static_cast<type>(__v));
1575 }
1576};
1577template <class _Tp>
1578struct _LIBCPP_TEMPLATE_VIS __enum_hash<_Tp, false> {
1579 __enum_hash() = delete;
1580 __enum_hash(__enum_hash const&) = delete;
1581 __enum_hash& operator=(__enum_hash const&) = delete;
1582};
1583
1584template <class _Tp>
1585struct _LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp>
1586{
1587};
1588#endif
1589
1590#if _LIBCPP_STD_VER > 14
1591
1592template <>
1593struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t>
1594 : public unary_function<nullptr_t, size_t>
1595{
1596 _LIBCPP_INLINE_VISIBILITY
1597 size_t operator()(nullptr_t) const _NOEXCEPT {
1598 return 662607004ull;
1599 }
1600};
1601#endif
1602
1603#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierd90b9312017-03-03 22:35:58 +00001604template <class _Key, class _Hash>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001605using __check_hash_requirements _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Eric Fiselier698a97b2017-01-21 00:02:12 +00001606 is_copy_constructible<_Hash>::value &&
1607 is_move_constructible<_Hash>::value &&
1608 __invokable_r<size_t, _Hash, _Key const&>::value
1609>;
1610
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001611template <class _Key, class _Hash = hash<_Key> >
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001612using __has_enabled_hash _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Eric Fiselierd90b9312017-03-03 22:35:58 +00001613 __check_hash_requirements<_Key, _Hash>::value &&
1614 is_default_constructible<_Hash>::value
1615>;
1616
Eric Fiselier698a97b2017-01-21 00:02:12 +00001617#if _LIBCPP_STD_VER > 14
1618template <class _Type, class>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001619using __enable_hash_helper_imp _LIBCPP_NODEBUG_TYPE = _Type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001620
1621template <class _Type, class ..._Keys>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001622using __enable_hash_helper _LIBCPP_NODEBUG_TYPE = __enable_hash_helper_imp<_Type,
Eric Fiselier698a97b2017-01-21 00:02:12 +00001623 typename enable_if<__all<__has_enabled_hash<_Keys>::value...>::value>::type
1624>;
1625#else
1626template <class _Type, class ...>
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001627using __enable_hash_helper _LIBCPP_NODEBUG_TYPE = _Type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001628#endif
1629
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +01001630template <class _Tp>
1631_LIBCPP_INLINE_VISIBILITY constexpr typename underlying_type<_Tp>::type
1632__to_underlying(_Tp __val) noexcept {
1633 return static_cast<typename underlying_type<_Tp>::type>(__val);
1634}
Eric Fiselier698a97b2017-01-21 00:02:12 +00001635#endif // !_LIBCPP_CXX03_LANG
1636
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +01001637#if _LIBCPP_STD_VER > 20
1638template <class _Tp>
1639_LIBCPP_INLINE_VISIBILITY constexpr underlying_type_t<_Tp>
1640to_underlying(_Tp __val) noexcept {
1641 return _VSTD::__to_underlying(__val);
1642}
1643#endif
1644
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645_LIBCPP_END_NAMESPACE_STD
1646
1647#endif // _LIBCPP_UTILITY