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