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 #endif 00182 00183 /** 00184 * @brief Struct holding two objects of arbitrary type. 00185 * 00186 * @tparam _T1 Type of first object. 00187 * @tparam _T2 Type of second object. 00188 */ 00189 template<typename _T1, typename _T2> 00190 struct pair 00191 { 00192 typedef _T1 first_type; /// @c first_type is the first bound type 00193 typedef _T2 second_type; /// @c second_type is the second bound type 00194 00195 _T1 first; /// @c first is a copy of the first object 00196 _T2 second; /// @c second is a copy of the second object 00197 00198 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00199 // 265. std::pair::pair() effects overly restrictive 00200 /** The default constructor creates @c first and @c second using their 00201 * respective default constructors. */ 00202 #if __cplusplus >= 201103L 00203 template <typename _U1 = _T1, 00204 typename _U2 = _T2, 00205 typename enable_if<__and_< 00206 __is_implicitly_default_constructible<_U1>, 00207 __is_implicitly_default_constructible<_U2>> 00208 ::value, bool>::type = true> 00209 #endif 00210 _GLIBCXX_CONSTEXPR pair() 00211 : first(), second() { } 00212 00213 #if __cplusplus >= 201103L 00214 template <typename _U1 = _T1, 00215 typename _U2 = _T2, 00216 typename enable_if<__and_< 00217 is_default_constructible<_U1>, 00218 is_default_constructible<_U2>, 00219 __not_< 00220 __and_<__is_implicitly_default_constructible<_U1>, 00221 __is_implicitly_default_constructible<_U2>>>> 00222 ::value, bool>::type = false> 00223 explicit constexpr pair() 00224 : first(), second() { } 00225 #endif 00226 00227 /** Two objects may be passed to a @c pair constructor to be copied. */ 00228 #if __cplusplus < 201103L 00229 pair(const _T1& __a, const _T2& __b) 00230 : first(__a), second(__b) { } 00231 #else 00232 // Shortcut for constraining the templates that don't take pairs. 00233 using _PCCP = _PCC<true, _T1, _T2>; 00234 00235 template<typename _U1 = _T1, typename _U2=_T2, typename 00236 enable_if<_PCCP::template 00237 _ConstructiblePair<_U1, _U2>() 00238 && _PCCP::template 00239 _ImplicitlyConvertiblePair<_U1, _U2>(), 00240 bool>::type=true> 00241 constexpr pair(const _T1& __a, const _T2& __b) 00242 : first(__a), second(__b) { } 00243 00244 template<typename _U1 = _T1, typename _U2=_T2, typename 00245 enable_if<_PCCP::template 00246 _ConstructiblePair<_U1, _U2>() 00247 && !_PCCP::template 00248 _ImplicitlyConvertiblePair<_U1, _U2>(), 00249 bool>::type=false> 00250 explicit constexpr pair(const _T1& __a, const _T2& __b) 00251 : first(__a), second(__b) { } 00252 #endif 00253 00254 /** There is also a templated copy ctor for the @c pair class itself. */ 00255 #if __cplusplus < 201103L 00256 template<typename _U1, typename _U2> 00257 pair(const pair<_U1, _U2>& __p) 00258 : first(__p.first), second(__p.second) { } 00259 #else 00260 // Shortcut for constraining the templates that take pairs. 00261 template <typename _U1, typename _U2> 00262 using _PCCFP = _PCC<!is_same<_T1, _U1>::value 00263 || !is_same<_T2, _U2>::value, 00264 _T1, _T2>; 00265 00266 template<typename _U1, typename _U2, typename 00267 enable_if<_PCCFP<_U1, _U2>::template 00268 _ConstructiblePair<_U1, _U2>() 00269 && _PCCFP<_U1, _U2>::template 00270 _ImplicitlyConvertiblePair<_U1, _U2>(), 00271 bool>::type=true> 00272 constexpr pair(const pair<_U1, _U2>& __p) 00273 : first(__p.first), second(__p.second) { } 00274 00275 template<typename _U1, typename _U2, typename 00276 enable_if<_PCCFP<_U1, _U2>::template 00277 _ConstructiblePair<_U1, _U2>() 00278 && !_PCCFP<_U1, _U2>::template 00279 _ImplicitlyConvertiblePair<_U1, _U2>(), 00280 bool>::type=false> 00281 explicit constexpr pair(const pair<_U1, _U2>& __p) 00282 : first(__p.first), second(__p.second) { } 00283 00284 constexpr pair(const pair&) = default; 00285 constexpr pair(pair&&) = default; 00286 00287 // DR 811. 00288 template<typename _U1, typename 00289 enable_if<_PCCP::template 00290 _MoveCopyPair<true, _U1, _T2>(), 00291 bool>::type=true> 00292 constexpr pair(_U1&& __x, const _T2& __y) 00293 : first(std::forward<_U1>(__x)), second(__y) { } 00294 00295 template<typename _U1, typename 00296 enable_if<_PCCP::template 00297 _MoveCopyPair<false, _U1, _T2>(), 00298 bool>::type=false> 00299 explicit constexpr pair(_U1&& __x, const _T2& __y) 00300 : first(std::forward<_U1>(__x)), second(__y) { } 00301 00302 template<typename _U2, typename 00303 enable_if<_PCCP::template 00304 _CopyMovePair<true, _T1, _U2>(), 00305 bool>::type=true> 00306 constexpr pair(const _T1& __x, _U2&& __y) 00307 : first(__x), second(std::forward<_U2>(__y)) { } 00308 00309 template<typename _U2, typename 00310 enable_if<_PCCP::template 00311 _CopyMovePair<false, _T1, _U2>(), 00312 bool>::type=false> 00313 explicit pair(const _T1& __x, _U2&& __y) 00314 : first(__x), second(std::forward<_U2>(__y)) { } 00315 00316 template<typename _U1, typename _U2, typename 00317 enable_if<_PCCP::template 00318 _MoveConstructiblePair<_U1, _U2>() 00319 && _PCCP::template 00320 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00321 bool>::type=true> 00322 constexpr pair(_U1&& __x, _U2&& __y) 00323 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } 00324 00325 template<typename _U1, typename _U2, typename 00326 enable_if<_PCCP::template 00327 _MoveConstructiblePair<_U1, _U2>() 00328 && !_PCCP::template 00329 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00330 bool>::type=false> 00331 explicit constexpr pair(_U1&& __x, _U2&& __y) 00332 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } 00333 00334 00335 template<typename _U1, typename _U2, typename 00336 enable_if<_PCCFP<_U1, _U2>::template 00337 _MoveConstructiblePair<_U1, _U2>() 00338 && _PCCFP<_U1, _U2>::template 00339 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00340 bool>::type=true> 00341 constexpr pair(pair<_U1, _U2>&& __p) 00342 : first(std::forward<_U1>(__p.first)), 00343 second(std::forward<_U2>(__p.second)) { } 00344 00345 template<typename _U1, typename _U2, typename 00346 enable_if<_PCCFP<_U1, _U2>::template 00347 _MoveConstructiblePair<_U1, _U2>() 00348 && !_PCCFP<_U1, _U2>::template 00349 _ImplicitlyMoveConvertiblePair<_U1, _U2>(), 00350 bool>::type=false> 00351 explicit constexpr pair(pair<_U1, _U2>&& __p) 00352 : first(std::forward<_U1>(__p.first)), 00353 second(std::forward<_U2>(__p.second)) { } 00354 00355 template<typename... _Args1, typename... _Args2> 00356 pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>); 00357 00358 pair& 00359 operator=(typename conditional< 00360 __and_<is_copy_assignable<_T1>, 00361 is_copy_assignable<_T2>>::value, 00362 const pair&, const __nonesuch&>::type __p) 00363 { 00364 first = __p.first; 00365 second = __p.second; 00366 return *this; 00367 } 00368 00369 pair& 00370 operator=(typename conditional< 00371 __not_<__and_<is_copy_assignable<_T1>, 00372 is_copy_assignable<_T2>>>::value, 00373 const pair&, const __nonesuch&>::type __p) = delete; 00374 00375 pair& 00376 operator=(typename conditional< 00377 __and_<is_move_assignable<_T1>, 00378 is_move_assignable<_T2>>::value, 00379 pair&&, __nonesuch&&>::type __p) 00380 noexcept(__and_<is_nothrow_move_assignable<_T1>, 00381 is_nothrow_move_assignable<_T2>>::value) 00382 { 00383 first = std::forward<first_type>(__p.first); 00384 second = std::forward<second_type>(__p.second); 00385 return *this; 00386 } 00387 00388 template<typename _U1, typename _U2> 00389 typename enable_if<__and_<is_assignable<_T1&, const _U1&>, 00390 is_assignable<_T2&, const _U2&>>::value, 00391 pair&>::type 00392 operator=(const pair<_U1, _U2>& __p) 00393 { 00394 first = __p.first; 00395 second = __p.second; 00396 return *this; 00397 } 00398 00399 template<typename _U1, typename _U2> 00400 typename enable_if<__and_<is_assignable<_T1&, _U1&&>, 00401 is_assignable<_T2&, _U2&&>>::value, 00402 pair&>::type 00403 operator=(pair<_U1, _U2>&& __p) 00404 { 00405 first = std::forward<_U1>(__p.first); 00406 second = std::forward<_U2>(__p.second); 00407 return *this; 00408 } 00409 00410 void 00411 swap(pair& __p) 00412 noexcept(__is_nothrow_swappable<_T1>::value 00413 && __is_nothrow_swappable<_T2>::value) 00414 { 00415 using std::swap; 00416 swap(first, __p.first); 00417 swap(second, __p.second); 00418 } 00419 00420 private: 00421 template<typename... _Args1, std::size_t... _Indexes1, 00422 typename... _Args2, std::size_t... _Indexes2> 00423 pair(tuple<_Args1...>&, tuple<_Args2...>&, 00424 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>); 00425 #endif 00426 }; 00427 00428 /// Two pairs of the same type are equal iff their members are equal. 00429 template<typename _T1, typename _T2> 00430 inline _GLIBCXX_CONSTEXPR bool 00431 operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00432 { return __x.first == __y.first && __x.second == __y.second; } 00433 00434 /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html> 00435 template<typename _T1, typename _T2> 00436 inline _GLIBCXX_CONSTEXPR bool 00437 operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00438 { return __x.first < __y.first 00439 || (!(__y.first < __x.first) && __x.second < __y.second); } 00440 00441 /// Uses @c operator== to find the result. 00442 template<typename _T1, typename _T2> 00443 inline _GLIBCXX_CONSTEXPR bool 00444 operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00445 { return !(__x == __y); } 00446 00447 /// Uses @c operator< to find the result. 00448 template<typename _T1, typename _T2> 00449 inline _GLIBCXX_CONSTEXPR bool 00450 operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00451 { return __y < __x; } 00452 00453 /// Uses @c operator< to find the result. 00454 template<typename _T1, typename _T2> 00455 inline _GLIBCXX_CONSTEXPR bool 00456 operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00457 { return !(__y < __x); } 00458 00459 /// Uses @c operator< to find the result. 00460 template<typename _T1, typename _T2> 00461 inline _GLIBCXX_CONSTEXPR bool 00462 operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) 00463 { return !(__x < __y); } 00464 00465 #if __cplusplus >= 201103L 00466 /// See std::pair::swap(). 00467 // Note: no std::swap overloads in C++03 mode, this has performance 00468 // implications, see, eg, libstdc++/38466. 00469 template<typename _T1, typename _T2> 00470 inline void 00471 swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) 00472 noexcept(noexcept(__x.swap(__y))) 00473 { __x.swap(__y); } 00474 #endif 00475 00476 /** 00477 * @brief A convenience wrapper for creating a pair from two objects. 00478 * @param __x The first object. 00479 * @param __y The second object. 00480 * @return A newly-constructed pair<> object of the appropriate type. 00481 * 00482 * The standard requires that the objects be passed by reference-to-const, 00483 * but LWG issue #181 says they should be passed by const value. We follow 00484 * the LWG by default. 00485 */ 00486 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00487 // 181. make_pair() unintended behavior 00488 #if __cplusplus >= 201103L 00489 // NB: DR 706. 00490 template<typename _T1, typename _T2> 00491 constexpr pair<typename __decay_and_strip<_T1>::__type, 00492 typename __decay_and_strip<_T2>::__type> 00493 make_pair(_T1&& __x, _T2&& __y) 00494 { 00495 typedef typename __decay_and_strip<_T1>::__type __ds_type1; 00496 typedef typename __decay_and_strip<_T2>::__type __ds_type2; 00497 typedef pair<__ds_type1, __ds_type2> __pair_type; 00498 return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y)); 00499 } 00500 #else 00501 template<typename _T1, typename _T2> 00502 inline pair<_T1, _T2> 00503 make_pair(_T1 __x, _T2 __y) 00504 { return pair<_T1, _T2>(__x, __y); } 00505 #endif 00506 00507 /// @} 00508 00509 _GLIBCXX_END_NAMESPACE_VERSION 00510 } // namespace std 00511 00512 #endif /* _STL_PAIR_H */