|
libstdc++
|
00001 // Pair implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996,1997 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file bits/stl_pair.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{utility} 00054 */ 00055 00056 #ifndef _STL_PAIR_H 00057 #define _STL_PAIR_H 1 00058 00059 #include <bits/move.h> // for std::move / std::forward, and std::swap 00060 00061 #if __cplusplus >= 201103L 00062 #include <type_traits> // for std::__decay_and_strip too 00063 #endif 00064 00065 namespace std _GLIBCXX_VISIBILITY(default) 00066 { 00067 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00068 00069 /** 00070 * @addtogroup utilities 00071 * @{ 00072 */ 00073 00074 #if __cplusplus >= 201103L 00075 /// piecewise_construct_t 00076 struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; 00077 00078 /// piecewise_construct 00079 constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); 00080 00081 // Forward declarations. 00082 template<typename...> 00083 class tuple; 00084 00085 template<std::size_t...> 00086 struct _Index_tuple; 00087 00088 // Concept utility functions, reused in conditionally-explicit 00089 // constructors. 00090 // See PR 70437, don't look at is_constructible or 00091 // is_convertible if the types are the same to 00092 // avoid querying those properties for incomplete types. 00093 template <bool, typename _T1, typename _T2> 00094 struct _PCC 00095 { 00096 template <typename _U1, typename _U2> 00097 static constexpr bool _ConstructiblePair() 00098 { 00099 return __and_<is_constructible<_T1, const _U1&>, 00100 is_constructible<_T2, const _U2&>>::value; 00101 } 00102 00103 template <typename _U1, typename _U2> 00104 static constexpr bool _ImplicitlyConvertiblePair() 00105 { 00106 return __and_<is_convertible<const _U1&, _T1>, 00107 is_convertible<const _U2&, _T2>>::value; 00108 } 00109 00110 template <typename _U1, typename _U2> 00111 static constexpr bool _MoveConstructiblePair() 00112 { 00113 return __and_<is_constructible<_T1, _U1&&>, 00114 is_constructible<_T2, _U2&&>>::value; 00115 } 00116 00117 template <typename _U1, typename _U2> 00118 static constexpr bool _ImplicitlyMoveConvertiblePair() 00119 { 00120 return __and_<is_convertible<_U1&&, _T1>, 00121 is_convertible<_U2&&, _T2>>::value; 00122 } 00123 00124 template <bool __implicit, typename _U1, typename _U2> 00125 static constexpr bool _CopyMovePair() 00126 { 00127 using __do_converts = __and_<is_convertible<const _U1&, _T1>, 00128 is_convertible<_U2&&, _T2>>; 00129 using __converts = typename conditional<__implicit, 00130 __do_converts, 00131 __not_<__do_converts>>::type; 00132 return __and_<is_constructible<_T1, const _U1&>, 00133 is_constructible<_T2, _U2&&>, 00134 __converts 00135 >::value; 00136 } 00137 00138 template <bool __implicit, typename _U1, typename _U2> 00139 static constexpr bool _MoveCopyPair() 00140 { 00141 using __do_converts = __and_<is_convertible<_U1&&, _T1>, 00142 is_convertible<const _U2&, _T2>>; 00143 using __converts = typename conditional<__implicit, 00144 __do_converts, 00145 __not_<__do_converts>>::type; 00146 return __and_<is_constructible<_T1, _U1&&>, 00147 is_constructible<_T2, const _U2&&>, 00148 __converts 00149 >::value; 00150 } 00151 }; 00152 00153 template <typename _T1, typename _T2> 00154 struct _PCC<false, _T1, _T2> 00155 { 00156 template <typename _U1, typename _U2> 00157 static constexpr bool _ConstructiblePair() 00158 { 00159 return false; 00160 } 00161 00162 template <typename _U1, typename _U2> 00163 static constexpr bool _ImplicitlyConvertiblePair() 00164 { 00165 return false; 00166 } 00167 00168 template <typename _U1, typename _U2> 00169 static constexpr bool _MoveConstructiblePair() 00170 { 00171 return false; 00172 } 00173 00174 template <typename _U1, typename _U2> 00175 static constexpr bool _ImplicitlyMoveConvertiblePair() 00176 { 00177 return false; 00178 } 00179 }; 00180 00181 struct __wrap_nonesuch : std::__nonesuch { 00182 explicit __wrap_nonesuch(const __nonesuch&) = delete; 00183 }; 00184 00185 #endif 00186 00187 /** 00188 * @brief Struct holding two objects of arbitrary type. 00189 * 00190 * @tparam _T1 Type of first object. 00191 * @tparam _T2 Type of second object. 00192 */ 00193 template<typename _T1, typename _T2> 00194 struct pair 00195 { 00196 typedef _T1 first_type; /// @c first_type is the first bound type 00197 typedef _T2 second_type; /// @c second_type is the second bound type 00198 00199 _T1 first; /// @c first is a copy of the first object 00200 _T2 second; /// @c second is a copy of the second object 00201 00202 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00203 // 265. std::pair::pair() effects overly restrictive 00204 /** The default constructor creates @c first and @c second using their 00205 * respective default constructors. */ 00206 #if __cplusplus >= 201103L 00207 template <typename _U1 = _T1, 00208 typename _U2 = _T2, 00209 typename enable_if<__and_< 00210 __is_implicitly_default_constructible<_U1>, 00211 __is_implicitly_default_constructible<_U2>> 00212 ::value, bool>::type = true> 00213 #endif 00214 _GLIBCXX_CONSTEXPR pair() 00215 : first(), second() { } 00216 00217 #if __cplusplus >= 201103L 00218 template <typename _U1 = _T1, 00219 typename _U2 = _T2, 00220 typename enable_if<__and_< 00221 is_default_constructible<_U1>, 00222 is_default_constructible<_U2>, 00223 __not_< 00224 __and_<__is_implicitly_default_constructible<_U1>, 00225 __is_implicitly_default_constructible<_U2>>>> 00226 ::value, bool>::type = false> 00227 explicit constexpr pair() 00228 : first(), second() { } 00229 #endif 00230 00231 /** Two objects may be passed to a @c pair constructor to be copied. */ 00232 #if __cplusplus < 201103L 00233 pair(const _T1& __a, const _T2& __b) 00234 : first(__a), second(__b) { } 00235 #else 00236 // Shortcut for constraining the templates that don't take pairs. 00237 using _PCCP = _PCC<true, _T1, _T2>; 00238 00239 template<typename _U1 = _T1, typename _U2=_T2, typename 00240 enable_if<_PCCP::template 00241 _ConstructiblePair<_U1, _U2>() 00242 && _PCCP::template 00243 _ImplicitlyConvertiblePair<_U1, _U2>(), 00244 bool>::type=true> 00245 constexpr pair(const _T1& __a, const _T2& __b) 00246 : first(__a), second(__b) { } 00247 00248 template<typename _U1 = _T1, typename _U2=_T2, typename 00249 enable_if<_PCCP::template 00250 _ConstructiblePair<_U1, _U2>() 00251 && !_PCCP::template 00252 _ImplicitlyConvertiblePair<_U1, _U2>(), 00253 bool>::type=false> 00254 explicit constexpr pair(const _T1& __a, const _T2& __b) 00255 : first(__a), second(__b) { } 00256 #endif 00257 00258 /** There is also a templated copy ctor for the @c pair class itself. */ 00259 #if __cplusplus < 201103L 00260 template<typename _U1, typename _U2> 00261 pair(const pair<_U1, _U2>& __p) 00262 : first(__p.first), second(__p.second) { } 00263 #else 00264 // Shortcut for constraining the templates that take pairs. 00265 template <typename _U1, typename _U2> 00266 using _PCCFP = _PCC<!is_same<_T1, _U1>::value 00267 || !is_same<_T2, _U2>::value, 00268 _T1, _T2>; 00269 00270 template<typename _U1, typename _U2, typename 00271 enable_if<_PCCFP<_U1, _U2>::template 00272 _ConstructiblePair<_U1, _U2>() 00273 && _PCCFP<_U1, _U2>::template 00274 _ImplicitlyConvertiblePair<_U1, _U2>(), 00275 bool>::type=true> 00276 constexpr pair(const pair<_U1, _U2>& __p) 00277 : first(__p.first), second(__p.second) { } 00278 00279 template<typename _U1, typename _U2, typename 00280 enable_if<_PCCFP<_U1, _U2>::template 00281 _ConstructiblePair<_U1, _U2>() 00282 && !_PCCFP<_U1, _U2>::template 00283 _ImplicitlyConvertiblePair<_U1, _U2>(), 00284 bool>::type=false> 00285 explicit constexpr pair(const pair<_U1, _U2>& __p) 00286 : first(__p.first), second(__p.second) { } 00287 00288 constexpr pair(const pair&) = default; 00289 constexpr pair(pair&&) = default; 00290 00291 // DR 811. 00292 template<typename _U1, typename 00293 enable_if<_PCCP::template 00294 _MoveCopyPair<true, _U1, _T2>(), 00295 bool>::type=true> 00296 constexpr pair(_U1&& __x, const _T2& __y) 00297 : first(std::forward<_U1>(__x)), second(__y) { } 00298 00299 template<typename _U1, typename 00300 enable_if<_PCCP::template 00301 _MoveCopyPair<false, _U1, _T2>(), 00302 bool>::type=false> 00303 explicit constexpr pair(_U1&& __x, const _T2& __y) 00304 : first(std::forward<_U1>(__x)), second(__y) { } 00305 00306 template<typename _U2, typename 00307 enable_if<_PCCP::template 00308 _CopyMovePair<true, _T1, _U2>(), 00309 bool>::type=true> 00310 constexpr pair(const _T1& __x, _U2&& __y) 00311 : first(__x), second(std::forward<_U2>(__y)) { } 00312 00313 template<typename _U2, typename 00314 enable_if<_PCCP::template 00315 _CopyMovePair<false, _T1, _U2>(), 00316 bool>::type=false> 00317 explicit pair(const _T1& __x, _U2&& __y) 00318 : first(__x), second(std::forward<_U2>(__y)) { } 00319 00320 template<typename _U1, typename _U2, typename 00321 enable_if<_PCCP::template 00322 _MoveConstructiblePair<_U1, _U2>() 00323 && _PCCP::template 00324 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00325 bool>::type=true> 00326 constexpr pair(_U1&& __x, _U2&& __y) 00327 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } 00328 00329 template<typename _U1, typename _U2, typename 00330 enable_if<_PCCP::template 00331 _MoveConstructiblePair<_U1, _U2>() 00332 && !_PCCP::template 00333 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00334 bool>::type=false> 00335 explicit constexpr pair(_U1&& __x, _U2&& __y) 00336 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } 00337 00338 00339 template<typename _U1, typename _U2, typename 00340 enable_if<_PCCFP<_U1, _U2>::template 00341 _MoveConstructiblePair<_U1, _U2>() 00342 && _PCCFP<_U1, _U2>::template 00343 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00344 bool>::type=true> 00345 constexpr pair(pair<_U1, _U2>&& __p) 00346 : first(std::forward<_U1>(__p.first)), 00347 second(std::forward<_U2>(__p.second)) { } 00348 00349 template<typename _U1, typename _U2, typename 00350 enable_if<_PCCFP<_U1, _U2>::template 00351 _MoveConstructiblePair<_U1, _U2>() 00352 && !_PCCFP<_U1, _U2>::template 00353 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00354 bool>::type=false> 00355 explicit constexpr pair(pair<_U1, _U2>&& __p) 00356 : first(std::forward<_U1>(__p.first)), 00357 second(std::forward<_U2>(__p.second)) { } 00358 00359 template<typename... _Args1, typename... _Args2> 00360 pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>); 00361 00362 pair& 00363 operator=(typename conditional< 00364 __and_<is_copy_assignable<_T1>, 00365 is_copy_assignable<_T2>>::value, 00366 const pair&, const __wrap_nonesuch&>::type __p) 00367 { 00368 first = __p.first; 00369 second = __p.second; 00370 return *this; 00371 } 00372 00373 pair& 00374 operator=(typename conditional< 00375 __not_<__and_<is_copy_assignable<_T1>, 00376 is_copy_assignable<_T2>>>::value, 00377 const pair&, const __wrap_nonesuch&>::type __p) = delete; 00378 00379 pair& 00380 operator=(typename conditional< 00381 __and_<is_move_assignable<_T1>, 00382 is_move_assignable<_T2>>::value, 00383 pair&&, __wrap_nonesuch&&>::type __p) 00384 noexcept(__and_<is_nothrow_move_assignable<_T1>, 00385 is_nothrow_move_assignable<_T2>>::value) 00386 { 00387 first = std::forward<first_type>(__p.first); 00388 second = std::forward<second_type>(__p.second); 00389 return *this; 00390 } 00391 00392 template<typename _U1, typename _U2> 00393 typename enable_if<__and_<is_assignable<_T1&, const _U1&>, 00394 is_assignable<_T2&, const _U2&>>::value, 00395 pair&>::type 00396 operator=(const pair<_U1, _U2>& __p) 00397 { 00398 first = __p.first; 00399 second = __p.second; 00400 return *this; 00401 } 00402 00403 template<typename _U1, typename _U2> 00404 typename enable_if<__and_<is_assignable<_T1&, _U1&&>, 00405 is_assignable<_T2&, _U2&&>>::value, 00406 pair&>::type 00407 operator=(pair<_U1, _U2>&& __p) 00408 { 00409 first = std::forward<_U1>(__p.first); 00410 second = std::forward<_U2>(__p.second); 00411 return *this; 00412 } 00413 00414 void 00415 swap(pair& __p) 00416 noexcept(__is_nothrow_swappable<_T1>::value 00417 && __is_nothrow_swappable<_T2>::value) 00418 { 00419 using std::swap; 00420 swap(first, __p.first); 00421 swap(second, __p.second); 00422 } 00423 00424 private: 00425 template<typename... _Args1, std::size_t... _Indexes1, 00426 typename... _Args2, std::size_t... _Indexes2> 00427 pair(tuple<_Args1...>&, tuple<_Args2...>&, 00428 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>); 00429 #endif 00430 }; 00431 00432 /// Two pairs of the same type are equal iff their members are equal. 00433 template<typename _T1, typename _T2> 00434 inline _GLIBCXX_CONSTEXPR bool 00435 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00436 { return __x.first == __y.first && __x.second == __y.second; } 00437 00438 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html> 00439 template<typename _T1, typename _T2> 00440 inline _GLIBCXX_CONSTEXPR bool 00441 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00442 { return __x.first < __y.first 00443 || (!(__y.first < __x.first) && __x.second < __y.second); } 00444 00445 /// Uses @c operator== to find the result. 00446 template<typename _T1, typename _T2> 00447 inline _GLIBCXX_CONSTEXPR bool 00448 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00449 { return !(__x == __y); } 00450 00451 /// Uses @c operator< to find the result. 00452 template<typename _T1, typename _T2> 00453 inline _GLIBCXX_CONSTEXPR bool 00454 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00455 { return __y < __x; } 00456 00457 /// Uses @c operator< to find the result. 00458 template<typename _T1, typename _T2> 00459 inline _GLIBCXX_CONSTEXPR bool 00460 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00461 { return !(__y < __x); } 00462 00463 /// Uses @c operator< to find the result. 00464 template<typename _T1, typename _T2> 00465 inline _GLIBCXX_CONSTEXPR bool 00466 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00467 { return !(__x < __y); } 00468 00469 #if __cplusplus >= 201103L 00470 /// See std::pair::swap(). 00471 // Note: no std::swap overloads in C++03 mode, this has performance 00472 // implications, see, eg, libstdc++/38466. 00473 template<typename _T1, typename _T2> 00474 inline void 00475 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 00476 noexcept(noexcept(__x.swap(__y))) 00477 { __x.swap(__y); } 00478 #endif 00479 00480 /** 00481 * @brief A convenience wrapper for creating a pair from two objects. 00482 * @param __x The first object. 00483 * @param __y The second object. 00484 * @return A newly-constructed pair<> object of the appropriate type. 00485 * 00486 * The standard requires that the objects be passed by reference-to-const, 00487 * but LWG issue #181 says they should be passed by const value. We follow 00488 * the LWG by default. 00489 */ 00490 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00491 // 181. make_pair() unintended behavior 00492 #if __cplusplus >= 201103L 00493 // NB: DR 706. 00494 template<typename _T1, typename _T2> 00495 constexpr pair<typename __decay_and_strip<_T1>::__type, 00496 typename __decay_and_strip<_T2>::__type> 00497 make_pair(_T1&& __x, _T2&& __y) 00498 { 00499 typedef typename __decay_and_strip<_T1>::__type __ds_type1; 00500 typedef typename __decay_and_strip<_T2>::__type __ds_type2; 00501 typedef pair<__ds_type1, __ds_type2> __pair_type; 00502 return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y)); 00503 } 00504 #else 00505 template<typename _T1, typename _T2> 00506 inline pair<_T1, _T2> 00507 make_pair(_T1 __x, _T2 __y) 00508 { return pair<_T1, _T2>(__x, __y); } 00509 #endif 00510 00511 /// @} 00512 00513 _GLIBCXX_END_NAMESPACE_VERSION 00514 } // namespace std 00515 00516 #endif /* _STL_PAIR_H */