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