Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===-------------------------- utility -----------------------------------===// |
| 3 | // |
| 4 | // ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_UTILITY |
| 12 | #define _LIBCPP_UTILITY |
| 13 | |
| 14 | /* |
| 15 | utility synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class T> |
| 21 | void |
| 22 | swap(T& a, T& b); |
| 23 | |
| 24 | namespace rel_ops |
| 25 | { |
| 26 | template<class T> bool operator!=(const T&, const T&); |
| 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 | } |
| 31 | |
| 32 | template<class T> void swap(T& a, T& b); |
| 33 | template <class T, size_t N> void swap(T (&a)[N], T (&b)[N]); |
| 34 | |
| 35 | template <class T, class U> T&& forward(U&&); |
| 36 | template <class T> typename remove_reference<T>::type&& move(T&&); |
| 37 | |
| 38 | template <class T> |
| 39 | typename conditional |
| 40 | < |
| 41 | !has_nothrow_move_constructor<T>::value && has_copy_constructor<T>::value, |
| 42 | const T&, |
| 43 | T&& |
| 44 | >::type |
| 45 | move_if_noexcept(T& x); |
| 46 | |
| 47 | template <class T> typename add_rvalue_reference<T>::type declval() noexcept; |
| 48 | |
| 49 | template <class T1, class T2> |
| 50 | struct pair |
| 51 | { |
| 52 | typedef T1 first_type; |
| 53 | typedef T2 second_type; |
| 54 | |
| 55 | T1 first; |
| 56 | T2 second; |
| 57 | |
| 58 | pair(const pair&) = default; |
| 59 | constexpr pair(); |
| 60 | pair(const T1& x, const T2& y); |
| 61 | template <class U, class V> pair(U&& x, V&& y); |
| 62 | template <class U, class V> pair(const pair<U, V>& p); |
| 63 | template <class U, class V> pair(pair<U, V>&& p); |
| 64 | template <class... Args1, class... Args2> |
| 65 | pair(piecewise_construct_t, tuple<Args1...> first_args, |
| 66 | tuple<Args2...> second_args); |
| 67 | |
| 68 | template <class U, class V> pair& operator=(const pair<U, V>& p); |
| 69 | pair& operator=(pair&& p); |
| 70 | template <class U, class V> pair& operator=(pair<U, V>&& p); |
| 71 | |
| 72 | void swap(pair& p); |
| 73 | }; |
| 74 | |
| 75 | template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); |
| 76 | template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); |
| 77 | template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); |
| 78 | template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); |
| 79 | template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); |
| 80 | template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); |
| 81 | |
| 82 | template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); |
| 83 | template <class T1, class T2> void swap(pair<T1, T2>& x, pair<T1, T2>& y); |
| 84 | |
| 85 | struct piecewise_construct_t { }; |
| 86 | constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); |
| 87 | |
| 88 | template <class T> class tuple_size; |
| 89 | template <size_t I, class T> class tuple_element; |
| 90 | |
| 91 | template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >; |
| 92 | template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >; |
| 93 | template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >; |
| 94 | |
| 95 | template<size_t I, class T1, class T2> |
| 96 | typename tuple_element<I, std::pair<T1, T2> >::type& |
| 97 | get(std::pair<T1, T2>&); |
| 98 | |
| 99 | template<size_t I, class T1, class T2> |
| 100 | const typename const tuple_element<I, std::pair<T1, T2> >::type& |
| 101 | get(const std::pair<T1, T2>&); |
| 102 | |
| 103 | template <class InputIterator> |
| 104 | InputIterator begin(const std::pair<InputIterator, InputIterator>& p); |
| 105 | template <class InputIterator> |
| 106 | InputIterator end(const std::pair<InputIterator, InputIterator>& p); |
| 107 | |
| 108 | } // std |
| 109 | |
| 110 | */ |
| 111 | |
| 112 | #include <__config> |
| 113 | #include <__tuple> |
| 114 | #include <type_traits> |
| 115 | |
| 116 | #pragma GCC system_header |
| 117 | |
| 118 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 119 | |
| 120 | namespace rel_ops |
| 121 | { |
| 122 | |
| 123 | template<class _Tp> |
| 124 | inline _LIBCPP_INLINE_VISIBILITY |
| 125 | bool |
| 126 | operator!=(const _Tp& __x, const _Tp& __y) |
| 127 | { |
| 128 | return !(__x == __y); |
| 129 | } |
| 130 | |
| 131 | template<class _Tp> |
| 132 | inline _LIBCPP_INLINE_VISIBILITY |
| 133 | bool |
| 134 | operator> (const _Tp& __x, const _Tp& __y) |
| 135 | { |
| 136 | return __y < __x; |
| 137 | } |
| 138 | |
| 139 | template<class _Tp> |
| 140 | inline _LIBCPP_INLINE_VISIBILITY |
| 141 | bool |
| 142 | operator<=(const _Tp& __x, const _Tp& __y) |
| 143 | { |
| 144 | return !(__y < __x); |
| 145 | } |
| 146 | |
| 147 | template<class _Tp> |
| 148 | inline _LIBCPP_INLINE_VISIBILITY |
| 149 | bool |
| 150 | operator>=(const _Tp& __x, const _Tp& __y) |
| 151 | { |
| 152 | return !(__x < __y); |
| 153 | } |
| 154 | |
| 155 | } // rel_ops |
| 156 | |
| 157 | // swap_ranges |
| 158 | |
| 159 | template <class _ForwardIterator1, class _ForwardIterator2> |
| 160 | inline _LIBCPP_INLINE_VISIBILITY |
| 161 | _ForwardIterator2 |
| 162 | swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) |
| 163 | { |
| 164 | for(; __first1 != __last1; ++__first1, ++__first2) |
| 165 | swap(*__first1, *__first2); |
| 166 | return __first2; |
| 167 | } |
| 168 | |
| 169 | template<class _Tp, size_t _N> |
| 170 | inline _LIBCPP_INLINE_VISIBILITY |
| 171 | void |
| 172 | swap(_Tp (&__a)[_N], _Tp (&__b)[_N]) |
| 173 | { |
| 174 | _STD::swap_ranges(__a, __a + _N, __b); |
| 175 | } |
| 176 | |
| 177 | template <class _Tp> |
| 178 | inline _LIBCPP_INLINE_VISIBILITY |
| 179 | #ifdef _LIBCPP_MOVE |
| 180 | typename conditional |
| 181 | < |
| 182 | !has_nothrow_move_constructor<_Tp>::value && has_copy_constructor<_Tp>::value, |
| 183 | const _Tp&, |
| 184 | _Tp&& |
| 185 | >::type |
| 186 | #else |
| 187 | const _Tp& |
| 188 | #endif |
| 189 | move_if_noexcept(_Tp& __x) |
| 190 | { |
| 191 | return _STD::move(__x); |
| 192 | } |
| 193 | |
| 194 | struct piecewise_construct_t { }; |
| 195 | //constexpr |
| 196 | extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t(); |
| 197 | |
| 198 | template <class _T1, class _T2> struct pair; |
| 199 | template <class _T1, class _T2> void swap(pair<_T1, _T2>&, pair<_T1, _T2>&); |
| 200 | |
| 201 | template <class _T1, class _T2> |
| 202 | struct pair |
| 203 | { |
| 204 | typedef _T1 first_type; |
| 205 | typedef _T2 second_type; |
| 206 | |
| 207 | _T1 first; |
| 208 | _T2 second; |
| 209 | |
| 210 | _LIBCPP_INLINE_VISIBILITY pair() : first(), second() {} |
| 211 | |
| 212 | _LIBCPP_INLINE_VISIBILITY pair(const _T1& __x, const _T2& __y) |
| 213 | : first(__x), second(__y) {} |
| 214 | |
| 215 | #ifdef _LIBCPP_MOVE |
| 216 | |
| 217 | template <class _U1, class _U2, |
| 218 | class = typename enable_if<is_convertible<_U1, first_type >::value && |
| 219 | is_convertible<_U2, second_type>::value>::type> |
| 220 | _LIBCPP_INLINE_VISIBILITY |
| 221 | pair(_U1&& __u1, _U2&& __u2) |
| 222 | : first(_STD::forward<_U1>(__u1)), |
| 223 | second(_STD::forward<_U2>(__u2)) |
| 224 | {} |
| 225 | |
| 226 | template<class _Tuple, |
| 227 | class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type> |
| 228 | _LIBCPP_INLINE_VISIBILITY |
| 229 | pair(_Tuple&& __p) |
| 230 | : first(_STD::forward<typename tuple_element<0, |
| 231 | typename __make_tuple_types<_Tuple>::type>::type>(get<0>(__p))), |
| 232 | second(_STD::forward<typename tuple_element<1, |
| 233 | typename __make_tuple_types<_Tuple>::type>::type>(get<1>(__p))) |
| 234 | {} |
| 235 | |
| 236 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 237 | template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2> |
| 238 | pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args, |
| 239 | tuple<_Args2...> __second_args) |
| 240 | : pair(__pc, __first_args, __second_args, |
| 241 | typename __make_tuple_indices<sizeof...(_Args1)>::type(), |
| 242 | typename __make_tuple_indices<sizeof...(_Args2) >::type()) |
| 243 | {} |
| 244 | #endif |
| 245 | |
| 246 | template <class _Tuple, |
| 247 | class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type> |
| 248 | pair& |
| 249 | operator=(_Tuple&& __p) |
| 250 | { |
| 251 | typedef typename __make_tuple_types<_Tuple>::type _TupleRef; |
| 252 | typedef typename tuple_element<0, _TupleRef>::type _U0; |
| 253 | typedef typename tuple_element<1, _TupleRef>::type _U1; |
| 254 | first = _STD::forward<_U0>(_STD::get<0>(__p)); |
| 255 | second = _STD::forward<_U1>(_STD::get<1>(__p)); |
| 256 | return *this; |
| 257 | } |
| 258 | |
| 259 | #else |
| 260 | template<class _U1, class _U2> |
| 261 | _LIBCPP_INLINE_VISIBILITY pair(const pair<_U1, _U2>& __p) |
| 262 | : first(__p.first), second(__p.second) {} |
| 263 | #endif |
| 264 | void _LIBCPP_INLINE_VISIBILITY swap(pair& __p) {_STD::swap(*this, __p);} |
| 265 | private: |
| 266 | |
| 267 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 268 | template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2> |
| 269 | pair(piecewise_construct_t, |
| 270 | tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args, |
| 271 | __tuple_indices<_I1...>, __tuple_indices<_I2...>); |
| 272 | #endif |
| 273 | }; |
| 274 | |
| 275 | template <class _T1, class _T2> |
| 276 | inline _LIBCPP_INLINE_VISIBILITY |
| 277 | bool |
| 278 | operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) |
| 279 | { |
| 280 | return __x.first == __y.first && __x.second == __y.second; |
| 281 | } |
| 282 | |
| 283 | template <class _T1, class _T2> |
| 284 | inline _LIBCPP_INLINE_VISIBILITY |
| 285 | bool |
| 286 | operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) |
| 287 | { |
| 288 | return !(__x == __y); |
| 289 | } |
| 290 | |
| 291 | template <class _T1, class _T2> |
| 292 | inline _LIBCPP_INLINE_VISIBILITY |
| 293 | bool |
| 294 | operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) |
| 295 | { |
| 296 | return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second); |
| 297 | } |
| 298 | |
| 299 | template <class _T1, class _T2> |
| 300 | inline _LIBCPP_INLINE_VISIBILITY |
| 301 | bool |
| 302 | operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) |
| 303 | { |
| 304 | return __y < __x; |
| 305 | } |
| 306 | |
| 307 | template <class _T1, class _T2> |
| 308 | inline _LIBCPP_INLINE_VISIBILITY |
| 309 | bool |
| 310 | operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) |
| 311 | { |
| 312 | return !(__x < __y); |
| 313 | } |
| 314 | |
| 315 | template <class _T1, class _T2> |
| 316 | inline _LIBCPP_INLINE_VISIBILITY |
| 317 | bool |
| 318 | operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y) |
| 319 | { |
| 320 | return !(__y < __x); |
| 321 | } |
| 322 | |
| 323 | template <class _T1, class _T2> |
| 324 | inline _LIBCPP_INLINE_VISIBILITY |
| 325 | void |
| 326 | swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) |
| 327 | { |
| 328 | swap(__x.first, __y.first); |
| 329 | swap(__x.second, __y.second); |
| 330 | } |
| 331 | |
| 332 | #ifdef _LIBCPP_MOVE |
| 333 | |
| 334 | template <class _Tp> class reference_wrapper; |
| 335 | |
| 336 | template <class _Tp> |
| 337 | struct ___make_pair_return |
| 338 | { |
| 339 | typedef _Tp type; |
| 340 | }; |
| 341 | |
| 342 | template <class _Tp> |
| 343 | struct ___make_pair_return<reference_wrapper<_Tp>> |
| 344 | { |
| 345 | typedef _Tp& type; |
| 346 | }; |
| 347 | |
| 348 | template <class _Tp> |
| 349 | struct __make_pair_return |
| 350 | { |
| 351 | typedef typename ___make_pair_return<typename decay<_Tp>::type>::type type; |
| 352 | }; |
| 353 | |
| 354 | template <class _T1, class _T2> |
| 355 | inline |
| 356 | pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type> |
| 357 | make_pair(_T1&& __t1, _T2&& __t2) |
| 358 | { |
| 359 | return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type> |
| 360 | (_STD::forward<_T1>(__t1), _STD::forward<_T2>(__t2)); |
| 361 | } |
| 362 | |
| 363 | #else |
| 364 | |
| 365 | template <class _T1, class _T2> |
| 366 | inline _LIBCPP_INLINE_VISIBILITY |
| 367 | pair<_T1,_T2> |
| 368 | make_pair(_T1 __x, _T2 __y) |
| 369 | { |
| 370 | return pair<_T1, _T2>(__x, __y); |
| 371 | } |
| 372 | |
| 373 | #endif |
| 374 | |
| 375 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 376 | |
| 377 | template <class _T1, class _T2> |
| 378 | class tuple_size<pair<_T1, _T2> > : public integral_constant<size_t, 2> {}; |
| 379 | |
| 380 | template <class _T1, class _T2> |
| 381 | class tuple_size<const pair<_T1, _T2> > : public integral_constant<size_t, 2> {}; |
| 382 | |
| 383 | template <class _T1, class _T2> |
| 384 | class tuple_element<0, pair<_T1, _T2> > |
| 385 | { |
| 386 | public: |
| 387 | typedef _T1 type; |
| 388 | }; |
| 389 | |
| 390 | template <class _T1, class _T2> |
| 391 | class tuple_element<1, pair<_T1, _T2> > |
| 392 | { |
| 393 | public: |
| 394 | typedef _T2 type; |
| 395 | }; |
| 396 | |
| 397 | template <class _T1, class _T2> |
| 398 | class tuple_element<0, const pair<_T1, _T2> > |
| 399 | { |
| 400 | public: |
| 401 | typedef const _T1 type; |
| 402 | }; |
| 403 | |
| 404 | template <class _T1, class _T2> |
| 405 | class tuple_element<1, const pair<_T1, _T2> > |
| 406 | { |
| 407 | public: |
| 408 | typedef const _T2 type; |
| 409 | }; |
| 410 | |
| 411 | template <size_t _Ip> struct __get_pair; |
| 412 | |
| 413 | template <> |
| 414 | struct __get_pair<0> |
| 415 | { |
| 416 | template <class _T1, class _T2> |
| 417 | static |
| 418 | _LIBCPP_INLINE_VISIBILITY |
| 419 | _T1& |
| 420 | get(pair<_T1, _T2>& __p) {return __p.first;} |
| 421 | |
| 422 | template <class _T1, class _T2> |
| 423 | static |
| 424 | _LIBCPP_INLINE_VISIBILITY |
| 425 | const _T1& |
| 426 | get(const pair<_T1, _T2>& __p) {return __p.first;} |
| 427 | }; |
| 428 | |
| 429 | template <> |
| 430 | struct __get_pair<1> |
| 431 | { |
| 432 | template <class _T1, class _T2> |
| 433 | static |
| 434 | _LIBCPP_INLINE_VISIBILITY |
| 435 | _T2& |
| 436 | get(pair<_T1, _T2>& __p) {return __p.second;} |
| 437 | |
| 438 | template <class _T1, class _T2> |
| 439 | static |
| 440 | _LIBCPP_INLINE_VISIBILITY |
| 441 | const _T2& |
| 442 | get(const pair<_T1, _T2>& __p) {return __p.second;} |
| 443 | }; |
| 444 | |
| 445 | template <size_t _Ip, class _T1, class _T2> |
| 446 | _LIBCPP_INLINE_VISIBILITY inline |
| 447 | typename tuple_element<_Ip, pair<_T1, _T2> >::type& |
| 448 | get(pair<_T1, _T2>& __p) |
| 449 | { |
| 450 | return __get_pair<_Ip>::get(__p); |
| 451 | } |
| 452 | |
| 453 | template <size_t _Ip, class _T1, class _T2> |
| 454 | _LIBCPP_INLINE_VISIBILITY inline |
| 455 | const typename tuple_element<_Ip, pair<_T1, _T2> >::type& |
| 456 | get(const pair<_T1, _T2>& __p) |
| 457 | { |
| 458 | return __get_pair<_Ip>::get(__p); |
| 459 | } |
| 460 | |
| 461 | #endif |
| 462 | |
| 463 | template <class _InputIterator> |
| 464 | _LIBCPP_INLINE_VISIBILITY inline |
| 465 | _InputIterator |
| 466 | begin(const pair<_InputIterator, _InputIterator>& __p) |
| 467 | { |
| 468 | return __p.first; |
| 469 | } |
| 470 | |
| 471 | template <class _InputIterator> |
| 472 | _LIBCPP_INLINE_VISIBILITY inline |
| 473 | _InputIterator |
| 474 | end(const pair<_InputIterator, _InputIterator>& __p) |
| 475 | { |
| 476 | return __p.second; |
| 477 | } |
| 478 | |
| 479 | _LIBCPP_END_NAMESPACE_STD |
| 480 | |
| 481 | #endif // _LIBCPP_UTILITY |