blob: 570ec05ae81bca9771c7cd1b7f45957fb544bf01 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- utility -----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_UTILITY
12#define _LIBCPP_UTILITY
13
14/*
15 utility synopsis
16
Louis Dionneb5769a32018-07-05 16:16:03 +000017#include <initializer_list>
18
Howard Hinnantc51e1022010-05-11 19:42:16 +000019namespace std
20{
21
22template <class T>
23 void
24 swap(T& a, T& b);
25
26namespace rel_ops
27{
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 template<class T> bool operator>=(const T&, const T&);
32}
33
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000034template<class T>
35void
36swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
37 is_nothrow_move_assignable<T>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000038
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000039template <class T, size_t N>
40void
41swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
42
Marshall Clowfe9aa372013-07-15 20:46:11 +000043template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept; // constexpr in C++14
44template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000045
Marshall Clowfe9aa372013-07-15 20:46:11 +000046template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000047
48template <class T>
49 typename conditional
50 <
Howard Hinnanta9a897e2010-11-19 22:17:28 +000051 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +000052 const T&,
53 T&&
54 >::type
Marshall Clowfe9aa372013-07-15 20:46:11 +000055 move_if_noexcept(T& x) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000056
Marshall Clow5d305742018-02-11 21:51:49 +000057template <class T> constexpr add_const_t<T>& as_const(T& t) noexcept; // C++17
Marshall Clow845efe42015-11-17 00:08:08 +000058template <class T> void as_const(const T&&) = delete; // C++17
59
Howard Hinnantc51e1022010-05-11 19:42:16 +000060template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
61
62template <class T1, class T2>
63struct pair
64{
65 typedef T1 first_type;
66 typedef T2 second_type;
67
68 T1 first;
69 T2 second;
70
71 pair(const pair&) = default;
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000072 pair(pair&&) = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +000073 constexpr pair();
Marshall Clowf00c56c2013-07-16 17:45:44 +000074 pair(const T1& x, const T2& y); // constexpr in C++14
75 template <class U, class V> pair(U&& x, V&& y); // constexpr in C++14
76 template <class U, class V> pair(const pair<U, V>& p); // constexpr in C++14
77 template <class U, class V> pair(pair<U, V>&& p); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000078 template <class... Args1, class... Args2>
79 pair(piecewise_construct_t, tuple<Args1...> first_args,
80 tuple<Args2...> second_args);
81
82 template <class U, class V> pair& operator=(const pair<U, V>& p);
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000083 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
84 is_nothrow_move_assignable<T2>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000085 template <class U, class V> pair& operator=(pair<U, V>&& p);
86
Eric Fiselier6bfed252016-04-21 23:38:59 +000087 void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
88 is_nothrow_swappable_v<T2>);
Howard Hinnantc51e1022010-05-11 19:42:16 +000089};
90
Marshall Clowf00c56c2013-07-16 17:45:44 +000091template <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
96template <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 +000097
Marshall Clowf00c56c2013-07-16 17:45:44 +000098template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000099template <class T1, class T2>
100void
101swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102
103struct piecewise_construct_t { };
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000104inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105
106template <class T> class tuple_size;
107template <size_t I, class T> class tuple_element;
108
Marshall Clowf50d66f2014-03-03 06:18:11 +0000109template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
110template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
111template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112
113template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000114 typename tuple_element<I, pair<T1, T2> >::type&
115 get(pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117template<size_t I, class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000118 const typename tuple_element<I, pair<T1, T2> >::type&
Marshall Clowf50d66f2014-03-03 06:18:11 +0000119 get(const pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120
Howard Hinnant22e97242010-11-17 19:52:17 +0000121template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000122 typename tuple_element<I, pair<T1, T2> >::type&&
123 get(pair<T1, T2>&&) noexcept; // constexpr in C++14
Howard Hinnant22e97242010-11-17 19:52:17 +0000124
Eric Fiselier6dea8092015-12-18 00:36:55 +0000125template<size_t I, class T1, class T2>
126 const typename tuple_element<I, pair<T1, T2> >::type&&
127 get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
128
Marshall Clow15b02e02013-07-13 02:54:05 +0000129template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000130 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000131
Eric Fiselier6dea8092015-12-18 00:36:55 +0000132template<class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000133 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000134
Eric Fiselier6dea8092015-12-18 00:36:55 +0000135template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000136 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000137
Eric Fiselier6dea8092015-12-18 00:36:55 +0000138template<class T1, class T2>
139 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
140
141template<class T1, class T2>
142 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
143
144template<class T1, class T2>
145 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
146
147template<class T1, class T2>
148 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
149
150template<class T1, class T2>
151 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
152
Marshall Clow9a970d82013-07-01 16:26:55 +0000153// C++14
154
155template<class T, T... I>
156struct integer_sequence
157{
158 typedef T value_type;
159
160 static constexpr size_t size() noexcept;
161};
162
163template<size_t... I>
164 using index_sequence = integer_sequence<size_t, I...>;
165
166template<class T, T N>
167 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
168template<size_t N>
169 using make_index_sequence = make_integer_sequence<size_t, N>;
170
171template<class... T>
172 using index_sequence_for = make_index_sequence<sizeof...(T)>;
173
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000174template<class T, class U=T>
Marshall Clow867ca362013-07-08 20:54:40 +0000175 T exchange(T& obj, U&& new_value);
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000176
177// 20.2.7, in-place construction // C++17
Eric Fiselier99b81712016-11-17 19:24:04 +0000178struct in_place_t {
179 explicit in_place_t() = default;
180};
181inline constexpr in_place_t in_place{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000182template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000183 struct in_place_type_t {
184 explicit in_place_type_t() = default;
185 };
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000186template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000187 inline constexpr in_place_type_t<T> in_place_type{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000188template <size_t I>
Eric Fiselier99b81712016-11-17 19:24:04 +0000189 struct in_place_index_t {
190 explicit in_place_index_t() = default;
191 };
192template <size_t I>
193 inline constexpr in_place_index_t<I> in_place_index{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000194
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195} // std
196
197*/
198
199#include <__config>
200#include <__tuple>
201#include <type_traits>
Ben Craig23254d22016-04-19 20:13:55 +0000202#include <initializer_list>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000203#include <cstddef>
204#include <cstring>
205#include <cstdint>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000206#include <version>
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000207#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000209#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000211#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213_LIBCPP_BEGIN_NAMESPACE_STD
214
215namespace rel_ops
216{
217
218template<class _Tp>
219inline _LIBCPP_INLINE_VISIBILITY
220bool
221operator!=(const _Tp& __x, const _Tp& __y)
222{
223 return !(__x == __y);
224}
225
226template<class _Tp>
227inline _LIBCPP_INLINE_VISIBILITY
228bool
229operator> (const _Tp& __x, const _Tp& __y)
230{
231 return __y < __x;
232}
233
234template<class _Tp>
235inline _LIBCPP_INLINE_VISIBILITY
236bool
237operator<=(const _Tp& __x, const _Tp& __y)
238{
239 return !(__y < __x);
240}
241
242template<class _Tp>
243inline _LIBCPP_INLINE_VISIBILITY
244bool
245operator>=(const _Tp& __x, const _Tp& __y)
246{
247 return !(__x < __y);
248}
249
250} // rel_ops
251
252// swap_ranges
253
Marshall Clowfd9a8572015-01-06 19:20:49 +0000254
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255template <class _ForwardIterator1, class _ForwardIterator2>
256inline _LIBCPP_INLINE_VISIBILITY
257_ForwardIterator2
258swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
259{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000260 for(; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261 swap(*__first1, *__first2);
262 return __first2;
263}
264
Eric Fiselier6bfed252016-04-21 23:38:59 +0000265// forward declared in <type_traits>
Howard Hinnantc834c512011-11-29 18:15:50 +0000266template<class _Tp, size_t _Np>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +0000268typename enable_if<
269 __is_swappable<_Tp>::value
270>::type
Howard Hinnantc834c512011-11-29 18:15:50 +0000271swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272{
Howard Hinnantc834c512011-11-29 18:15:50 +0000273 _VSTD::swap_ranges(__a, __a + _Np, __b);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274}
275
276template <class _Tp>
Marshall Clowfe9aa372013-07-15 20:46:11 +0000277inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000278#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279typename conditional
280<
Howard Hinnanta9a897e2010-11-19 22:17:28 +0000281 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282 const _Tp&,
283 _Tp&&
284>::type
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000285#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286const _Tp&
287#endif
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000288move_if_noexcept(_Tp& __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000290 return _VSTD::move(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291}
292
Marshall Clow845efe42015-11-17 00:08:08 +0000293#if _LIBCPP_STD_VER > 14
294template <class _Tp> constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
295template <class _Tp> void as_const(const _Tp&&) = delete;
296#endif
297
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000298struct _LIBCPP_TEMPLATE_VIS piecewise_construct_t { };
Louis Dionne5e0eadd2018-08-01 02:08:59 +0000299#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
Howard Hinnant7d7e9582012-04-03 21:09:48 +0000301#else
Marshall Clowbc084382018-01-02 18:57:47 +0000302/* _LIBCPP_INLINE_VAR */ constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
Howard Hinnant7d7e9582012-04-03 21:09:48 +0000303#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000305#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
306struct __non_trivially_copyable_base {
307 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
308 __non_trivially_copyable_base() _NOEXCEPT {}
309 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
310 __non_trivially_copyable_base(__non_trivially_copyable_base const&) _NOEXCEPT {}
311};
312#endif
Eric Fiseliere61db632016-07-25 04:32:07 +0000313
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314template <class _T1, class _T2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000315struct _LIBCPP_TEMPLATE_VIS pair
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000316#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
317: private __non_trivially_copyable_base
318#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319{
320 typedef _T1 first_type;
321 typedef _T2 second_type;
322
323 _T1 first;
324 _T2 second;
325
Eric Fiselierf9618bb2016-10-11 21:22:21 +0000326#if !defined(_LIBCPP_CXX03_LANG)
Eric Fiselier08f7fe82016-07-18 01:58:37 +0000327 pair(pair const&) = default;
328 pair(pair&&) = default;
329#else
330 // Use the implicitly declared copy constructor in C++03
Marshall Clowf00c56c2013-07-16 17:45:44 +0000331#endif
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000332
Eric Fiseliere61db632016-07-25 04:32:07 +0000333#ifdef _LIBCPP_CXX03_LANG
334 _LIBCPP_INLINE_VISIBILITY
335 pair() : first(), second() {}
Eric Fiselier737a16d2016-07-25 02:36:42 +0000336
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000337 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere61db632016-07-25 04:32:07 +0000338 pair(_T1 const& __t1, _T2 const& __t2) : first(__t1), second(__t2) {}
339
340 template <class _U1, class _U2>
341 _LIBCPP_INLINE_VISIBILITY
342 pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
343
344 _LIBCPP_INLINE_VISIBILITY
345 pair& operator=(pair const& __p) {
346 first = __p.first;
347 second = __p.second;
348 return *this;
349 }
350#else
351 template <bool _Val>
352 using _EnableB = typename enable_if<_Val, bool>::type;
353
354 struct _CheckArgs {
355 template <class _U1, class _U2>
356 static constexpr bool __enable_default() {
357 return is_default_constructible<_U1>::value
358 && is_default_constructible<_U2>::value;
359 }
360
361 template <class _U1, class _U2>
362 static constexpr bool __enable_explicit() {
363 return is_constructible<first_type, _U1>::value
364 && is_constructible<second_type, _U2>::value
365 && (!is_convertible<_U1, first_type>::value
366 || !is_convertible<_U2, second_type>::value);
367 }
368
369 template <class _U1, class _U2>
370 static constexpr bool __enable_implicit() {
371 return is_constructible<first_type, _U1>::value
372 && is_constructible<second_type, _U2>::value
373 && is_convertible<_U1, first_type>::value
374 && is_convertible<_U2, second_type>::value;
375 }
376 };
377
378 template <bool _MaybeEnable>
379 using _CheckArgsDep = typename conditional<
380 _MaybeEnable, _CheckArgs, __check_tuple_constructor_fail>::type;
381
382 struct _CheckTupleLikeConstructor {
383 template <class _Tuple>
384 static constexpr bool __enable_implicit() {
385 return __tuple_convertible<_Tuple, pair>::value;
386 }
387
388 template <class _Tuple>
389 static constexpr bool __enable_explicit() {
390 return __tuple_constructible<_Tuple, pair>::value
391 && !__tuple_convertible<_Tuple, pair>::value;
392 }
393
394 template <class _Tuple>
395 static constexpr bool __enable_assign() {
396 return __tuple_assignable<_Tuple, pair>::value;
397 }
398 };
399
400 template <class _Tuple>
401 using _CheckTLC = typename conditional<
402 __tuple_like_with_size<_Tuple, 2>::value
403 && !is_same<typename decay<_Tuple>::type, pair>::value,
404 _CheckTupleLikeConstructor,
405 __check_tuple_constructor_fail
406 >::type;
407
408 template<bool _Dummy = true, _EnableB<
409 _CheckArgsDep<_Dummy>::template __enable_default<_T1, _T2>()
410 > = false>
411 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
412 pair() : first(), second() {}
413
414 template <bool _Dummy = true, _EnableB<
415 _CheckArgsDep<_Dummy>::template __enable_explicit<_T1 const&, _T2 const&>()
416 > = false>
417 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
418 explicit pair(_T1 const& __t1, _T2 const& __t2)
419 : first(__t1), second(__t2) {}
420
421 template<bool _Dummy = true, _EnableB<
422 _CheckArgsDep<_Dummy>::template __enable_implicit<_T1 const&, _T2 const&>()
423 > = false>
424 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
425 pair(_T1 const& __t1, _T2 const& __t2)
426 : first(__t1), second(__t2) {}
427
428 template<class _U1, class _U2, _EnableB<
429 _CheckArgs::template __enable_explicit<_U1, _U2>()
430 > = false>
431 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
432 explicit pair(_U1&& __u1, _U2&& __u2)
433 : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
434
435 template<class _U1, class _U2, _EnableB<
436 _CheckArgs::template __enable_implicit<_U1, _U2>()
437 > = false>
438 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
439 pair(_U1&& __u1, _U2&& __u2)
440 : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {}
441
442 template<class _U1, class _U2, _EnableB<
443 _CheckArgs::template __enable_explicit<_U1 const&, _U2 const&>()
444 > = false>
445 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
446 explicit pair(pair<_U1, _U2> const& __p)
447 : first(__p.first), second(__p.second) {}
448
449 template<class _U1, class _U2, _EnableB<
450 _CheckArgs::template __enable_implicit<_U1 const&, _U2 const&>()
451 > = false>
452 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
453 pair(pair<_U1, _U2> const& __p)
454 : first(__p.first), second(__p.second) {}
455
456 template<class _U1, class _U2, _EnableB<
457 _CheckArgs::template __enable_explicit<_U1, _U2>()
458 > = false>
459 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
460 explicit pair(pair<_U1, _U2>&&__p)
461 : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
462
463 template<class _U1, class _U2, _EnableB<
464 _CheckArgs::template __enable_implicit<_U1, _U2>()
465 > = false>
466 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
467 pair(pair<_U1, _U2>&& __p)
468 : first(_VSTD::forward<_U1>(__p.first)), second(_VSTD::forward<_U2>(__p.second)) {}
469
470 template<class _Tuple, _EnableB<
471 _CheckTLC<_Tuple>::template __enable_explicit<_Tuple>()
472 > = false>
473 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
474 explicit pair(_Tuple&& __p)
475 : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
476 second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
477
478 template<class _Tuple, _EnableB<
479 _CheckTLC<_Tuple>::template __enable_implicit<_Tuple>()
480 > = false>
481 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
482 pair(_Tuple&& __p)
483 : first(_VSTD::get<0>(_VSTD::forward<_Tuple>(__p))),
484 second(_VSTD::get<1>(_VSTD::forward<_Tuple>(__p))) {}
485
486 template <class... _Args1, class... _Args2>
487 _LIBCPP_INLINE_VISIBILITY
488 pair(piecewise_construct_t __pc,
489 tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
490 : pair(__pc, __first_args, __second_args,
491 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
492 typename __make_tuple_indices<sizeof...(_Args2) >::type()) {}
493
494 _LIBCPP_INLINE_VISIBILITY
495 pair& operator=(typename conditional<
496 is_copy_assignable<first_type>::value &&
497 is_copy_assignable<second_type>::value,
498 pair, __nat>::type const& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +0000499 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
500 is_nothrow_copy_assignable<second_type>::value)
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000501 {
502 first = __p.first;
503 second = __p.second;
504 return *this;
505 }
506
Eric Fiselier737a16d2016-07-25 02:36:42 +0000507 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere61db632016-07-25 04:32:07 +0000508 pair& operator=(typename conditional<
509 is_move_assignable<first_type>::value &&
510 is_move_assignable<second_type>::value,
511 pair, __nat>::type&& __p)
Eric Fiselier737a16d2016-07-25 02:36:42 +0000512 _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
513 is_nothrow_move_assignable<second_type>::value)
514 {
515 first = _VSTD::forward<first_type>(__p.first);
516 second = _VSTD::forward<second_type>(__p.second);
517 return *this;
518 }
Eric Fiseliere61db632016-07-25 04:32:07 +0000519
520 template <class _Tuple, _EnableB<
Eric Fiselier9e0256b2016-08-29 01:43:41 +0000521 _CheckTLC<_Tuple>::template __enable_assign<_Tuple>()
Eric Fiseliere61db632016-07-25 04:32:07 +0000522 > = false>
523 _LIBCPP_INLINE_VISIBILITY
524 pair& operator=(_Tuple&& __p) {
525 first = _VSTD::get<0>(_VSTD::forward<_Tuple>(__p));
526 second = _VSTD::get<1>(_VSTD::forward<_Tuple>(__p));
527 return *this;
528 }
Eric Fiselier737a16d2016-07-25 02:36:42 +0000529#endif
530
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000531 _LIBCPP_INLINE_VISIBILITY
532 void
Howard Hinnant89ef1212011-05-27 19:08:18 +0000533 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
534 __is_nothrow_swappable<second_type>::value)
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000535 {
Marshall Clow8b9bd542015-09-22 17:50:11 +0000536 using _VSTD::swap;
537 swap(first, __p.first);
538 swap(second, __p.second);
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000539 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000541
Eric Fiseliere61db632016-07-25 04:32:07 +0000542#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
Howard Hinnantef5aced2011-01-13 20:05:05 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545 pair(piecewise_construct_t,
546 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
547 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
Eric Fiseliere61db632016-07-25 04:32:07 +0000548#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549};
550
Eric Fiselier8df4ac62017-10-04 00:04:26 +0000551#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
552template<class _T1, class _T2>
553pair(_T1, _T2) -> pair<_T1, _T2>;
554#endif // _LIBCPP_HAS_NO_DEDUCTION_GUIDES
555
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000557inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558bool
559operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
560{
561 return __x.first == __y.first && __x.second == __y.second;
562}
563
564template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000565inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566bool
567operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
568{
569 return !(__x == __y);
570}
571
572template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000573inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574bool
575operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
576{
577 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
578}
579
580template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000581inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582bool
583operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
584{
585 return __y < __x;
586}
587
588template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000589inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590bool
591operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
592{
593 return !(__x < __y);
594}
595
596template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000597inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598bool
599operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
600{
601 return !(__y < __x);
602}
603
604template <class _T1, class _T2>
605inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant44267242011-06-01 19:59:32 +0000606typename enable_if
607<
608 __is_swappable<_T1>::value &&
609 __is_swappable<_T2>::value,
610 void
611>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
Howard Hinnant89ef1212011-05-27 19:08:18 +0000613 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
614 __is_nothrow_swappable<_T2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615{
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000616 __x.swap(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617}
618
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000619#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620
621template <class _Tp>
Marshall Clow75533b02014-01-03 22:55:49 +0000622struct __make_pair_return_impl
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623{
624 typedef _Tp type;
625};
626
627template <class _Tp>
Marshall Clow75533b02014-01-03 22:55:49 +0000628struct __make_pair_return_impl<reference_wrapper<_Tp>>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629{
630 typedef _Tp& type;
631};
632
633template <class _Tp>
634struct __make_pair_return
635{
Marshall Clow75533b02014-01-03 22:55:49 +0000636 typedef typename __make_pair_return_impl<typename decay<_Tp>::type>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637};
638
639template <class _T1, class _T2>
Marshall Clowf00c56c2013-07-16 17:45:44 +0000640inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
642make_pair(_T1&& __t1, _T2&& __t2)
643{
644 return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000645 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646}
647
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000648#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649
650template <class _T1, class _T2>
651inline _LIBCPP_INLINE_VISIBILITY
652pair<_T1,_T2>
653make_pair(_T1 __x, _T2 __y)
654{
655 return pair<_T1, _T2>(__x, __y);
656}
657
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000658#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660template <class _T1, class _T2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000661 class _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
Howard Hinnant1c265cd2010-09-23 18:58:28 +0000662 : public integral_constant<size_t, 2> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000663
Marshall Clow185577f2017-06-12 16:13:17 +0000664template <size_t _Ip, class _T1, class _T2>
665class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
666{
667 static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
668};
669
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670template <class _T1, class _T2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000671class _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672{
673public:
674 typedef _T1 type;
675};
676
677template <class _T1, class _T2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000678class _LIBCPP_TEMPLATE_VIS tuple_element<1, pair<_T1, _T2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679{
680public:
681 typedef _T2 type;
682};
683
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684template <size_t _Ip> struct __get_pair;
685
686template <>
687struct __get_pair<0>
688{
689 template <class _T1, class _T2>
690 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 _T1&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000693 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694
695 template <class _T1, class _T2>
696 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000697 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 const _T1&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000699 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnant22e97242010-11-17 19:52:17 +0000700
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000701#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000702 template <class _T1, class _T2>
703 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000704 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000705 _T1&&
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000706 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
Howard Hinnant22e97242010-11-17 19:52:17 +0000707
Eric Fiselier6dea8092015-12-18 00:36:55 +0000708 template <class _T1, class _T2>
709 static
710 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
711 const _T1&&
712 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T1>(__p.first);}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000713#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714};
715
716template <>
717struct __get_pair<1>
718{
719 template <class _T1, class _T2>
720 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000721 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 _T2&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000723 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724
725 template <class _T1, class _T2>
726 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000727 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 const _T2&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000729 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnant22e97242010-11-17 19:52:17 +0000730
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000731#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000732 template <class _T1, class _T2>
733 static
Marshall Clow0abb1042013-07-17 18:25:36 +0000734 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000735 _T2&&
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000736 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
Howard Hinnant22e97242010-11-17 19:52:17 +0000737
Eric Fiselier6dea8092015-12-18 00:36:55 +0000738 template <class _T1, class _T2>
739 static
740 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
741 const _T2&&
742 get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T2>(__p.second);}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000743#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744};
745
746template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000747inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000749get(pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750{
751 return __get_pair<_Ip>::get(__p);
752}
753
754template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000755inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000757get(const pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758{
759 return __get_pair<_Ip>::get(__p);
760}
761
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000762#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000763template <size_t _Ip, class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000764inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000765typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
Howard Hinnant89ef1212011-05-27 19:08:18 +0000766get(pair<_T1, _T2>&& __p) _NOEXCEPT
Howard Hinnant22e97242010-11-17 19:52:17 +0000767{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000768 return __get_pair<_Ip>::get(_VSTD::move(__p));
Howard Hinnant22e97242010-11-17 19:52:17 +0000769}
770
Eric Fiselier6dea8092015-12-18 00:36:55 +0000771template <size_t _Ip, class _T1, class _T2>
772inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
773const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
774get(const pair<_T1, _T2>&& __p) _NOEXCEPT
775{
776 return __get_pair<_Ip>::get(_VSTD::move(__p));
777}
Eric Fiselierffe4aba2017-04-19 01:23:39 +0000778#endif // _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000779
Marshall Clow9a970d82013-07-01 16:26:55 +0000780#if _LIBCPP_STD_VER > 11
Marshall Clow15b02e02013-07-13 02:54:05 +0000781template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000782inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000783constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
784{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000785 return __get_pair<0>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000786}
787
788template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000789inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000790constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
791{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000792 return __get_pair<0>::get(__p);
Marshall Clow15b02e02013-07-13 02:54:05 +0000793}
794
795template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000796inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000797constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
798{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000799 return __get_pair<0>::get(_VSTD::move(__p));
Marshall Clow15b02e02013-07-13 02:54:05 +0000800}
801
802template <class _T1, class _T2>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000803inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6dea8092015-12-18 00:36:55 +0000804constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
805{
806 return __get_pair<0>::get(_VSTD::move(__p));
807}
808
809template <class _T1, class _T2>
810inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow15b02e02013-07-13 02:54:05 +0000811constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
812{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000813 return __get_pair<1>::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<_T2, _T1> const& __p) _NOEXCEPT
819{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000820 return __get_pair<1>::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<_T2, _T1>&& __p) _NOEXCEPT
826{
Marshall Clowf00c56c2013-07-16 17:45:44 +0000827 return __get_pair<1>::get(_VSTD::move(__p));
Marshall Clow15b02e02013-07-13 02:54:05 +0000828}
829
Eric Fiselier6dea8092015-12-18 00:36:55 +0000830template <class _T1, class _T2>
831inline _LIBCPP_INLINE_VISIBILITY
832constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
833{
834 return __get_pair<1>::get(_VSTD::move(__p));
835}
836
Marshall Clow15b02e02013-07-13 02:54:05 +0000837#endif
838
839#if _LIBCPP_STD_VER > 11
Marshall Clow9a970d82013-07-01 16:26:55 +0000840
841template<class _Tp, _Tp... _Ip>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000842struct _LIBCPP_TEMPLATE_VIS integer_sequence
Marshall Clow9a970d82013-07-01 16:26:55 +0000843{
844 typedef _Tp value_type;
845 static_assert( is_integral<_Tp>::value,
846 "std::integer_sequence can only be instantiated with an integral type" );
847 static
848 _LIBCPP_INLINE_VISIBILITY
849 constexpr
850 size_t
851 size() noexcept { return sizeof...(_Ip); }
852};
853
854template<size_t... _Ip>
855 using index_sequence = integer_sequence<size_t, _Ip...>;
856
Eric Fiselierd5746922015-12-09 22:03:06 +0000857#if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
858
859template <class _Tp, _Tp _Ep>
Eric Fiselier2079cc72016-06-30 22:34:43 +0000860using __make_integer_sequence = __make_integer_seq<integer_sequence, _Tp, _Ep>;
Eric Fiselierd5746922015-12-09 22:03:06 +0000861
862#else
863
Marshall Clow2128ea02013-07-03 19:20:30 +0000864template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked =
Eric Fiselier2079cc72016-06-30 22:34:43 +0000865 typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>;
Marshall Clow9a970d82013-07-01 16:26:55 +0000866
867template <class _Tp, _Tp _Ep>
Eric Fiselier2079cc72016-06-30 22:34:43 +0000868struct __make_integer_sequence_checked
Marshall Clow9a970d82013-07-01 16:26:55 +0000869{
870 static_assert(is_integral<_Tp>::value,
871 "std::make_integer_sequence can only be instantiated with an integral type" );
Eric Fiselierd5746922015-12-09 22:03:06 +0000872 static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
Eric Fiselier9b7456e2015-12-16 00:35:45 +0000873 // Workaround GCC bug by preventing bad installations when 0 <= _Ep
874 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
875 typedef __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
Marshall Clow9a970d82013-07-01 16:26:55 +0000876};
877
Eric Fiselier2079cc72016-06-30 22:34:43 +0000878template <class _Tp, _Tp _Ep>
879using __make_integer_sequence = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
880
Eric Fiselierd5746922015-12-09 22:03:06 +0000881#endif
882
Marshall Clow9a970d82013-07-01 16:26:55 +0000883template<class _Tp, _Tp _Np>
Eric Fiselier2079cc72016-06-30 22:34:43 +0000884 using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
Marshall Clow9a970d82013-07-01 16:26:55 +0000885
886template<size_t _Np>
887 using make_index_sequence = make_integer_sequence<size_t, _Np>;
888
889template<class... _Tp>
890 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000891
Marshall Clow9a970d82013-07-01 16:26:55 +0000892#endif // _LIBCPP_STD_VER > 11
893
Marshall Clow867ca362013-07-08 20:54:40 +0000894#if _LIBCPP_STD_VER > 11
895template<class _T1, class _T2 = _T1>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000896inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow867ca362013-07-08 20:54:40 +0000897_T1 exchange(_T1& __obj, _T2 && __new_value)
898{
Howard Hinnantecc7ba92013-07-08 21:06:38 +0000899 _T1 __old_value = _VSTD::move(__obj);
900 __obj = _VSTD::forward<_T2>(__new_value);
901 return __old_value;
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000902}
Marshall Clow867ca362013-07-08 20:54:40 +0000903#endif // _LIBCPP_STD_VER > 11
904
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000905#if _LIBCPP_STD_VER > 14
906
Eric Fiselier99b81712016-11-17 19:24:04 +0000907struct _LIBCPP_TYPE_VIS in_place_t {
908 explicit in_place_t() = default;
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000909};
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000910_LIBCPP_INLINE_VAR constexpr in_place_t in_place{};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000911
912template <class _Tp>
Eric Fiselier2a1b4d92017-04-20 01:45:15 +0000913struct _LIBCPP_TEMPLATE_VIS in_place_type_t {
Eric Fiselier99b81712016-11-17 19:24:04 +0000914 explicit in_place_type_t() = default;
915};
916template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000917_LIBCPP_INLINE_VAR constexpr in_place_type_t<_Tp> in_place_type{};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000918
Eric Fiselier99b81712016-11-17 19:24:04 +0000919template <size_t _Idx>
920struct _LIBCPP_TYPE_VIS in_place_index_t {
921 explicit in_place_index_t() = default;
922};
923template <size_t _Idx>
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000924_LIBCPP_INLINE_VAR constexpr in_place_index_t<_Idx> in_place_index{};
Eric Fiselier99b81712016-11-17 19:24:04 +0000925
926template <class _Tp> struct __is_inplace_type_imp : false_type {};
927template <class _Tp> struct __is_inplace_type_imp<in_place_type_t<_Tp>> : true_type {};
Eric Fiselieraea4e192016-10-16 02:51:50 +0000928
929template <class _Tp>
Eric Fiselier99b81712016-11-17 19:24:04 +0000930using __is_inplace_type = __is_inplace_type_imp<__uncvref_t<_Tp>>;
Eric Fiselier7e0c55e2016-07-24 07:42:13 +0000931
Michael Parka9c61fc2017-06-14 05:51:18 +0000932template <class _Tp> struct __is_inplace_index_imp : false_type {};
933template <size_t _Idx> struct __is_inplace_index_imp<in_place_index_t<_Idx>> : true_type {};
934
935template <class _Tp>
936using __is_inplace_index = __is_inplace_index_imp<__uncvref_t<_Tp>>;
937
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000938#endif // _LIBCPP_STD_VER > 14
939
Eric Fiselier698a97b2017-01-21 00:02:12 +0000940template <class _Arg, class _Result>
941struct _LIBCPP_TEMPLATE_VIS unary_function
942{
943 typedef _Arg argument_type;
944 typedef _Result result_type;
945};
946
947template <class _Size>
948inline _LIBCPP_INLINE_VISIBILITY
949_Size
950__loadword(const void* __p)
951{
952 _Size __r;
953 std::memcpy(&__r, __p, sizeof(__r));
954 return __r;
955}
956
957// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
958// is 64 bits. This is because cityhash64 uses 64bit x 64bit
959// multiplication, which can be very slow on 32-bit systems.
960template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
961struct __murmur2_or_cityhash;
962
963template <class _Size>
964struct __murmur2_or_cityhash<_Size, 32>
965{
Eric Fiselier3308e842017-02-08 00:10:10 +0000966 inline _Size operator()(const void* __key, _Size __len)
967 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
Eric Fiselier698a97b2017-01-21 00:02:12 +0000968};
969
970// murmur2
971template <class _Size>
972_Size
Eric Fiselier3308e842017-02-08 00:10:10 +0000973__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Eric Fiselier698a97b2017-01-21 00:02:12 +0000974{
975 const _Size __m = 0x5bd1e995;
976 const _Size __r = 24;
977 _Size __h = __len;
978 const unsigned char* __data = static_cast<const unsigned char*>(__key);
979 for (; __len >= 4; __data += 4, __len -= 4)
980 {
981 _Size __k = __loadword<_Size>(__data);
982 __k *= __m;
983 __k ^= __k >> __r;
984 __k *= __m;
985 __h *= __m;
986 __h ^= __k;
987 }
988 switch (__len)
989 {
990 case 3:
991 __h ^= __data[2] << 16;
992 case 2:
993 __h ^= __data[1] << 8;
994 case 1:
995 __h ^= __data[0];
996 __h *= __m;
997 }
998 __h ^= __h >> 13;
999 __h *= __m;
1000 __h ^= __h >> 15;
1001 return __h;
1002}
1003
1004template <class _Size>
1005struct __murmur2_or_cityhash<_Size, 64>
1006{
Eric Fiselier3308e842017-02-08 00:10:10 +00001007 inline _Size operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
Eric Fiselier698a97b2017-01-21 00:02:12 +00001008
1009 private:
1010 // Some primes between 2^63 and 2^64.
1011 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
1012 static const _Size __k1 = 0xb492b66fbe98f273ULL;
1013 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
1014 static const _Size __k3 = 0xc949d7c7509e6557ULL;
1015
1016 static _Size __rotate(_Size __val, int __shift) {
1017 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
1018 }
1019
1020 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
1021 return (__val >> __shift) | (__val << (64 - __shift));
1022 }
1023
1024 static _Size __shift_mix(_Size __val) {
1025 return __val ^ (__val >> 47);
1026 }
1027
Eric Fiselier3308e842017-02-08 00:10:10 +00001028 static _Size __hash_len_16(_Size __u, _Size __v)
1029 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1030 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001031 const _Size __mul = 0x9ddfea08eb382d69ULL;
1032 _Size __a = (__u ^ __v) * __mul;
1033 __a ^= (__a >> 47);
1034 _Size __b = (__v ^ __a) * __mul;
1035 __b ^= (__b >> 47);
1036 __b *= __mul;
1037 return __b;
1038 }
1039
Eric Fiselier3308e842017-02-08 00:10:10 +00001040 static _Size __hash_len_0_to_16(const char* __s, _Size __len)
1041 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1042 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001043 if (__len > 8) {
1044 const _Size __a = __loadword<_Size>(__s);
1045 const _Size __b = __loadword<_Size>(__s + __len - 8);
1046 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
1047 }
1048 if (__len >= 4) {
1049 const uint32_t __a = __loadword<uint32_t>(__s);
1050 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
1051 return __hash_len_16(__len + (__a << 3), __b);
1052 }
1053 if (__len > 0) {
1054 const unsigned char __a = __s[0];
1055 const unsigned char __b = __s[__len >> 1];
1056 const unsigned char __c = __s[__len - 1];
1057 const uint32_t __y = static_cast<uint32_t>(__a) +
1058 (static_cast<uint32_t>(__b) << 8);
1059 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
1060 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
1061 }
1062 return __k2;
1063 }
1064
Eric Fiselier3308e842017-02-08 00:10:10 +00001065 static _Size __hash_len_17_to_32(const char *__s, _Size __len)
1066 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1067 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001068 const _Size __a = __loadword<_Size>(__s) * __k1;
1069 const _Size __b = __loadword<_Size>(__s + 8);
1070 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
1071 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
1072 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
1073 __a + __rotate(__b ^ __k3, 20) - __c + __len);
1074 }
1075
1076 // Return a 16-byte hash for 48 bytes. Quick and dirty.
1077 // Callers do best to use "random-looking" values for a and b.
1078 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
Eric Fiselier3308e842017-02-08 00:10:10 +00001079 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b)
1080 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1081 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001082 __a += __w;
1083 __b = __rotate(__b + __a + __z, 21);
1084 const _Size __c = __a;
1085 __a += __x;
1086 __a += __y;
1087 __b += __rotate(__a, 44);
1088 return pair<_Size, _Size>(__a + __z, __b + __c);
1089 }
1090
1091 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
1092 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
Eric Fiselier3308e842017-02-08 00:10:10 +00001093 const char* __s, _Size __a, _Size __b)
1094 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1095 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001096 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
1097 __loadword<_Size>(__s + 8),
1098 __loadword<_Size>(__s + 16),
1099 __loadword<_Size>(__s + 24),
1100 __a,
1101 __b);
1102 }
1103
1104 // Return an 8-byte hash for 33 to 64 bytes.
Eric Fiselier3308e842017-02-08 00:10:10 +00001105 static _Size __hash_len_33_to_64(const char *__s, size_t __len)
1106 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
1107 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001108 _Size __z = __loadword<_Size>(__s + 24);
1109 _Size __a = __loadword<_Size>(__s) +
1110 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
1111 _Size __b = __rotate(__a + __z, 52);
1112 _Size __c = __rotate(__a, 37);
1113 __a += __loadword<_Size>(__s + 8);
1114 __c += __rotate(__a, 7);
1115 __a += __loadword<_Size>(__s + 16);
1116 _Size __vf = __a + __z;
1117 _Size __vs = __b + __rotate(__a, 31) + __c;
1118 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
1119 __z += __loadword<_Size>(__s + __len - 8);
1120 __b = __rotate(__a + __z, 52);
1121 __c = __rotate(__a, 37);
1122 __a += __loadword<_Size>(__s + __len - 24);
1123 __c += __rotate(__a, 7);
1124 __a += __loadword<_Size>(__s + __len - 16);
1125 _Size __wf = __a + __z;
1126 _Size __ws = __b + __rotate(__a, 31) + __c;
1127 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
1128 return __shift_mix(__r * __k0 + __vs) * __k2;
1129 }
1130};
1131
1132// cityhash64
1133template <class _Size>
1134_Size
Eric Fiselier3308e842017-02-08 00:10:10 +00001135__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Eric Fiselier698a97b2017-01-21 00:02:12 +00001136{
1137 const char* __s = static_cast<const char*>(__key);
1138 if (__len <= 32) {
1139 if (__len <= 16) {
1140 return __hash_len_0_to_16(__s, __len);
1141 } else {
1142 return __hash_len_17_to_32(__s, __len);
1143 }
1144 } else if (__len <= 64) {
1145 return __hash_len_33_to_64(__s, __len);
1146 }
1147
1148 // For strings over 64 bytes we hash the end first, and then as we
1149 // loop we keep 56 bytes of state: v, w, x, y, and z.
1150 _Size __x = __loadword<_Size>(__s + __len - 40);
1151 _Size __y = __loadword<_Size>(__s + __len - 16) +
1152 __loadword<_Size>(__s + __len - 56);
1153 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
1154 __loadword<_Size>(__s + __len - 24));
1155 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
1156 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
1157 __x = __x * __k1 + __loadword<_Size>(__s);
1158
1159 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
1160 __len = (__len - 1) & ~static_cast<_Size>(63);
1161 do {
1162 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
1163 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
1164 __x ^= __w.second;
1165 __y += __v.first + __loadword<_Size>(__s + 40);
1166 __z = __rotate(__z + __w.first, 33) * __k1;
1167 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
1168 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
1169 __y + __loadword<_Size>(__s + 16));
1170 std::swap(__z, __x);
1171 __s += 64;
1172 __len -= 64;
1173 } while (__len != 0);
1174 return __hash_len_16(
1175 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
1176 __hash_len_16(__v.second, __w.second) + __x);
1177}
1178
1179template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
1180struct __scalar_hash;
1181
1182template <class _Tp>
1183struct __scalar_hash<_Tp, 0>
1184 : public unary_function<_Tp, size_t>
1185{
1186 _LIBCPP_INLINE_VISIBILITY
1187 size_t operator()(_Tp __v) const _NOEXCEPT
1188 {
1189 union
1190 {
1191 _Tp __t;
1192 size_t __a;
1193 } __u;
1194 __u.__a = 0;
1195 __u.__t = __v;
1196 return __u.__a;
1197 }
1198};
1199
1200template <class _Tp>
1201struct __scalar_hash<_Tp, 1>
1202 : public unary_function<_Tp, size_t>
1203{
1204 _LIBCPP_INLINE_VISIBILITY
1205 size_t operator()(_Tp __v) const _NOEXCEPT
1206 {
1207 union
1208 {
1209 _Tp __t;
1210 size_t __a;
1211 } __u;
1212 __u.__t = __v;
1213 return __u.__a;
1214 }
1215};
1216
1217template <class _Tp>
1218struct __scalar_hash<_Tp, 2>
1219 : public unary_function<_Tp, size_t>
1220{
1221 _LIBCPP_INLINE_VISIBILITY
1222 size_t operator()(_Tp __v) const _NOEXCEPT
1223 {
1224 union
1225 {
1226 _Tp __t;
1227 struct
1228 {
1229 size_t __a;
1230 size_t __b;
1231 } __s;
1232 } __u;
1233 __u.__t = __v;
1234 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1235 }
1236};
1237
1238template <class _Tp>
1239struct __scalar_hash<_Tp, 3>
1240 : public unary_function<_Tp, size_t>
1241{
1242 _LIBCPP_INLINE_VISIBILITY
1243 size_t operator()(_Tp __v) const _NOEXCEPT
1244 {
1245 union
1246 {
1247 _Tp __t;
1248 struct
1249 {
1250 size_t __a;
1251 size_t __b;
1252 size_t __c;
1253 } __s;
1254 } __u;
1255 __u.__t = __v;
1256 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1257 }
1258};
1259
1260template <class _Tp>
1261struct __scalar_hash<_Tp, 4>
1262 : public unary_function<_Tp, size_t>
1263{
1264 _LIBCPP_INLINE_VISIBILITY
1265 size_t operator()(_Tp __v) const _NOEXCEPT
1266 {
1267 union
1268 {
1269 _Tp __t;
1270 struct
1271 {
1272 size_t __a;
1273 size_t __b;
1274 size_t __c;
1275 size_t __d;
1276 } __s;
1277 } __u;
1278 __u.__t = __v;
1279 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1280 }
1281};
1282
1283struct _PairT {
1284 size_t first;
1285 size_t second;
1286};
1287
1288_LIBCPP_INLINE_VISIBILITY
1289inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
1290 typedef __scalar_hash<_PairT> _HashT;
1291 const _PairT __p = {__lhs, __rhs};
1292 return _HashT()(__p);
1293}
1294
1295template<class _Tp>
1296struct _LIBCPP_TEMPLATE_VIS hash<_Tp*>
1297 : public unary_function<_Tp*, size_t>
1298{
1299 _LIBCPP_INLINE_VISIBILITY
1300 size_t operator()(_Tp* __v) const _NOEXCEPT
1301 {
1302 union
1303 {
1304 _Tp* __t;
1305 size_t __a;
1306 } __u;
1307 __u.__t = __v;
1308 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
1309 }
1310};
1311
1312
1313template <>
1314struct _LIBCPP_TEMPLATE_VIS hash<bool>
1315 : public unary_function<bool, size_t>
1316{
1317 _LIBCPP_INLINE_VISIBILITY
1318 size_t operator()(bool __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1319};
1320
1321template <>
1322struct _LIBCPP_TEMPLATE_VIS hash<char>
1323 : public unary_function<char, size_t>
1324{
1325 _LIBCPP_INLINE_VISIBILITY
1326 size_t operator()(char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1327};
1328
1329template <>
1330struct _LIBCPP_TEMPLATE_VIS hash<signed char>
1331 : public unary_function<signed char, size_t>
1332{
1333 _LIBCPP_INLINE_VISIBILITY
1334 size_t operator()(signed char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1335};
1336
1337template <>
1338struct _LIBCPP_TEMPLATE_VIS hash<unsigned char>
1339 : public unary_function<unsigned char, size_t>
1340{
1341 _LIBCPP_INLINE_VISIBILITY
1342 size_t operator()(unsigned char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1343};
1344
1345#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
1346
1347template <>
1348struct _LIBCPP_TEMPLATE_VIS hash<char16_t>
1349 : public unary_function<char16_t, size_t>
1350{
1351 _LIBCPP_INLINE_VISIBILITY
1352 size_t operator()(char16_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1353};
1354
1355template <>
1356struct _LIBCPP_TEMPLATE_VIS hash<char32_t>
1357 : public unary_function<char32_t, size_t>
1358{
1359 _LIBCPP_INLINE_VISIBILITY
1360 size_t operator()(char32_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1361};
1362
1363#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
1364
1365template <>
1366struct _LIBCPP_TEMPLATE_VIS hash<wchar_t>
1367 : public unary_function<wchar_t, size_t>
1368{
1369 _LIBCPP_INLINE_VISIBILITY
1370 size_t operator()(wchar_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1371};
1372
1373template <>
1374struct _LIBCPP_TEMPLATE_VIS hash<short>
1375 : public unary_function<short, size_t>
1376{
1377 _LIBCPP_INLINE_VISIBILITY
1378 size_t operator()(short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1379};
1380
1381template <>
1382struct _LIBCPP_TEMPLATE_VIS hash<unsigned short>
1383 : public unary_function<unsigned short, size_t>
1384{
1385 _LIBCPP_INLINE_VISIBILITY
1386 size_t operator()(unsigned short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1387};
1388
1389template <>
1390struct _LIBCPP_TEMPLATE_VIS hash<int>
1391 : public unary_function<int, size_t>
1392{
1393 _LIBCPP_INLINE_VISIBILITY
1394 size_t operator()(int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1395};
1396
1397template <>
1398struct _LIBCPP_TEMPLATE_VIS hash<unsigned int>
1399 : public unary_function<unsigned int, size_t>
1400{
1401 _LIBCPP_INLINE_VISIBILITY
1402 size_t operator()(unsigned int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1403};
1404
1405template <>
1406struct _LIBCPP_TEMPLATE_VIS hash<long>
1407 : public unary_function<long, size_t>
1408{
1409 _LIBCPP_INLINE_VISIBILITY
1410 size_t operator()(long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1411};
1412
1413template <>
1414struct _LIBCPP_TEMPLATE_VIS hash<unsigned long>
1415 : public unary_function<unsigned long, size_t>
1416{
1417 _LIBCPP_INLINE_VISIBILITY
1418 size_t operator()(unsigned long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
1419};
1420
1421template <>
1422struct _LIBCPP_TEMPLATE_VIS hash<long long>
1423 : public __scalar_hash<long long>
1424{
1425};
1426
1427template <>
1428struct _LIBCPP_TEMPLATE_VIS hash<unsigned long long>
1429 : public __scalar_hash<unsigned long long>
1430{
1431};
1432
1433#ifndef _LIBCPP_HAS_NO_INT128
1434
1435template <>
1436struct _LIBCPP_TEMPLATE_VIS hash<__int128_t>
1437 : public __scalar_hash<__int128_t>
1438{
1439};
1440
1441template <>
1442struct _LIBCPP_TEMPLATE_VIS hash<__uint128_t>
1443 : public __scalar_hash<__uint128_t>
1444{
1445};
1446
1447#endif
1448
1449template <>
1450struct _LIBCPP_TEMPLATE_VIS hash<float>
1451 : public __scalar_hash<float>
1452{
1453 _LIBCPP_INLINE_VISIBILITY
1454 size_t operator()(float __v) const _NOEXCEPT
1455 {
1456 // -0.0 and 0.0 should return same hash
1457 if (__v == 0)
1458 return 0;
1459 return __scalar_hash<float>::operator()(__v);
1460 }
1461};
1462
1463template <>
1464struct _LIBCPP_TEMPLATE_VIS hash<double>
1465 : public __scalar_hash<double>
1466{
1467 _LIBCPP_INLINE_VISIBILITY
1468 size_t operator()(double __v) const _NOEXCEPT
1469 {
1470 // -0.0 and 0.0 should return same hash
1471 if (__v == 0)
1472 return 0;
1473 return __scalar_hash<double>::operator()(__v);
1474 }
1475};
1476
1477template <>
1478struct _LIBCPP_TEMPLATE_VIS hash<long double>
1479 : public __scalar_hash<long double>
1480{
1481 _LIBCPP_INLINE_VISIBILITY
1482 size_t operator()(long double __v) const _NOEXCEPT
1483 {
1484 // -0.0 and 0.0 should return same hash
1485 if (__v == 0)
1486 return 0;
1487#if defined(__i386__)
1488 // Zero out padding bits
1489 union
1490 {
1491 long double __t;
1492 struct
1493 {
1494 size_t __a;
1495 size_t __b;
1496 size_t __c;
1497 size_t __d;
1498 } __s;
1499 } __u;
1500 __u.__s.__a = 0;
1501 __u.__s.__b = 0;
1502 __u.__s.__c = 0;
1503 __u.__s.__d = 0;
1504 __u.__t = __v;
1505 return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
1506#elif defined(__x86_64__)
1507 // Zero out padding bits
1508 union
1509 {
1510 long double __t;
1511 struct
1512 {
1513 size_t __a;
1514 size_t __b;
1515 } __s;
1516 } __u;
1517 __u.__s.__a = 0;
1518 __u.__s.__b = 0;
1519 __u.__t = __v;
1520 return __u.__s.__a ^ __u.__s.__b;
1521#else
1522 return __scalar_hash<long double>::operator()(__v);
1523#endif
1524 }
1525};
1526
1527#if _LIBCPP_STD_VER > 11
1528
1529template <class _Tp, bool = is_enum<_Tp>::value>
1530struct _LIBCPP_TEMPLATE_VIS __enum_hash
1531 : public unary_function<_Tp, size_t>
1532{
1533 _LIBCPP_INLINE_VISIBILITY
1534 size_t operator()(_Tp __v) const _NOEXCEPT
1535 {
1536 typedef typename underlying_type<_Tp>::type type;
1537 return hash<type>{}(static_cast<type>(__v));
1538 }
1539};
1540template <class _Tp>
1541struct _LIBCPP_TEMPLATE_VIS __enum_hash<_Tp, false> {
1542 __enum_hash() = delete;
1543 __enum_hash(__enum_hash const&) = delete;
1544 __enum_hash& operator=(__enum_hash const&) = delete;
1545};
1546
1547template <class _Tp>
1548struct _LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp>
1549{
1550};
1551#endif
1552
1553#if _LIBCPP_STD_VER > 14
1554
1555template <>
1556struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t>
1557 : public unary_function<nullptr_t, size_t>
1558{
1559 _LIBCPP_INLINE_VISIBILITY
1560 size_t operator()(nullptr_t) const _NOEXCEPT {
1561 return 662607004ull;
1562 }
1563};
1564#endif
1565
1566#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierd90b9312017-03-03 22:35:58 +00001567template <class _Key, class _Hash>
1568using __check_hash_requirements = integral_constant<bool,
Eric Fiselier698a97b2017-01-21 00:02:12 +00001569 is_copy_constructible<_Hash>::value &&
1570 is_move_constructible<_Hash>::value &&
1571 __invokable_r<size_t, _Hash, _Key const&>::value
1572>;
1573
Eric Fiselierd90b9312017-03-03 22:35:58 +00001574template <class _Key, class _Hash = std::hash<_Key> >
1575using __has_enabled_hash = integral_constant<bool,
1576 __check_hash_requirements<_Key, _Hash>::value &&
1577 is_default_constructible<_Hash>::value
1578>;
1579
Eric Fiselier698a97b2017-01-21 00:02:12 +00001580#if _LIBCPP_STD_VER > 14
1581template <class _Type, class>
1582using __enable_hash_helper_imp = _Type;
1583
1584template <class _Type, class ..._Keys>
1585using __enable_hash_helper = __enable_hash_helper_imp<_Type,
1586 typename enable_if<__all<__has_enabled_hash<_Keys>::value...>::value>::type
1587>;
1588#else
1589template <class _Type, class ...>
1590using __enable_hash_helper = _Type;
1591#endif
1592
1593#endif // !_LIBCPP_CXX03_LANG
1594
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595_LIBCPP_END_NAMESPACE_STD
1596
1597#endif // _LIBCPP_UTILITY