1 | // Pair implementation -*- C++ -*- |
2 | |
3 | // Copyright (C) 2001-2016 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /* |
26 | * |
27 | * Copyright (c) 1994 |
28 | * Hewlett-Packard Company |
29 | * |
30 | * Permission to use, copy, modify, distribute and sell this software |
31 | * and its documentation for any purpose is hereby granted without fee, |
32 | * provided that the above copyright notice appear in all copies and |
33 | * that both that copyright notice and this permission notice appear |
34 | * in supporting documentation. Hewlett-Packard Company makes no |
35 | * representations about the suitability of this software for any |
36 | * purpose. It is provided "as is" without express or implied warranty. |
37 | * |
38 | * |
39 | * Copyright (c) 1996,1997 |
40 | * Silicon Graphics Computer Systems, Inc. |
41 | * |
42 | * Permission to use, copy, modify, distribute and sell this software |
43 | * and its documentation for any purpose is hereby granted without fee, |
44 | * provided that the above copyright notice appear in all copies and |
45 | * that both that copyright notice and this permission notice appear |
46 | * in supporting documentation. Silicon Graphics makes no |
47 | * representations about the suitability of this software for any |
48 | * purpose. It is provided "as is" without express or implied warranty. |
49 | */ |
50 | |
51 | /** @file bits/stl_pair.h |
52 | * This is an internal header file, included by other library headers. |
53 | * Do not attempt to use it directly. @headername{utility} |
54 | */ |
55 | |
56 | #ifndef _STL_PAIR_H |
57 | #define _STL_PAIR_H 1 |
58 | |
59 | #include <bits/move.h> // for std::move / std::forward, and std::swap |
60 | |
61 | #if __cplusplus >= 201103L |
62 | #include <type_traits> // for std::__decay_and_strip too |
63 | #endif |
64 | |
65 | namespace std _GLIBCXX_VISIBILITY(default) |
66 | { |
67 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
68 | |
69 | /** |
70 | * @addtogroup utilities |
71 | * @{ |
72 | */ |
73 | |
74 | #if __cplusplus >= 201103L |
75 | /// piecewise_construct_t |
76 | struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; |
77 | |
78 | /// piecewise_construct |
79 | constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); |
80 | |
81 | // Forward declarations. |
82 | template<typename...> |
83 | class tuple; |
84 | |
85 | template<std::size_t...> |
86 | struct _Index_tuple; |
87 | |
88 | // Concept utility functions, reused in conditionally-explicit |
89 | // constructors. |
90 | // See PR 70437, don't look at is_constructible or |
91 | // is_convertible if the decayed types are the same to |
92 | // avoid querying those properties for incomplete types. |
93 | template <typename _T1, typename _T2, typename _U1, typename _U2> |
94 | constexpr bool _ConstructiblePair() |
95 | { |
96 | return __and_<__or_<is_same<typename decay<_T1>::type, |
97 | typename decay<_U1>::type>, |
98 | is_constructible<_T1, const _U1&>>, |
99 | __or_<is_same<typename decay<_T2>::type, |
100 | typename decay<_U2>::type>, |
101 | is_constructible<_T2, const _U2&>>>::value; |
102 | } |
103 | |
104 | template <typename _T1, typename _T2, typename _U1, typename _U2> |
105 | constexpr bool _ImplicitlyConvertiblePair() |
106 | { |
107 | return __and_<__or_<is_same<typename decay<_T1>::type, |
108 | typename decay<_U1>::type>, |
109 | is_convertible<const _U1&, _T1>>, |
110 | __or_<is_same<typename decay<_T2>::type, |
111 | typename decay<_U2>::type>, |
112 | is_convertible<const _U2&, _T2>>>::value; |
113 | } |
114 | |
115 | template <typename _T1, typename _T2, typename _U1, typename _U2> |
116 | constexpr bool _MoveConstructiblePair() |
117 | { |
118 | return __and_<__or_<is_same<typename decay<_T1>::type, |
119 | typename decay<_U1>::type>, |
120 | is_constructible<_T1, _U1&&>>, |
121 | __or_<is_same<typename decay<_T2>::type, |
122 | typename decay<_U2>::type>, |
123 | is_constructible<_T2, _U2&&>>>::value; |
124 | } |
125 | |
126 | template <typename _T1, typename _T2, typename _U1, typename _U2> |
127 | constexpr bool _ImplicitlyMoveConvertiblePair() |
128 | { |
129 | return __and_<__or_<is_same<typename decay<_T1>::type, |
130 | typename decay<_U1>::type>, |
131 | is_convertible<_U1&&, _T1>>, |
132 | __or_<is_same<typename decay<_T2>::type, |
133 | typename decay<_U2>::type>, |
134 | is_convertible<_U2&&, _T2>>>::value; |
135 | } |
136 | |
137 | |
138 | #endif |
139 | |
140 | /** |
141 | * @brief Struct holding two objects of arbitrary type. |
142 | * |
143 | * @tparam _T1 Type of first object. |
144 | * @tparam _T2 Type of second object. |
145 | */ |
146 | template<typename _T1, typename _T2> |
147 | struct pair |
148 | { |
149 | typedef _T1 first_type; /// @c first_type is the first bound type |
150 | typedef _T2 second_type; /// @c second_type is the second bound type |
151 | |
152 | _T1 first; /// @c first is a copy of the first object |
153 | _T2 second; /// @c second is a copy of the second object |
154 | |
155 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
156 | // 265. std::pair::pair() effects overly restrictive |
157 | /** The default constructor creates @c first and @c second using their |
158 | * respective default constructors. */ |
159 | #if __cplusplus >= 201103L |
160 | template <typename _U1 = _T1, |
161 | typename _U2 = _T2, |
162 | typename enable_if<__and_< |
163 | __is_implicitly_default_constructible<_U1>, |
164 | __is_implicitly_default_constructible<_U2>> |
165 | ::value, bool>::type = true> |
166 | #endif |
167 | _GLIBCXX_CONSTEXPR pair() |
168 | : first(), second() { } |
169 | |
170 | #if __cplusplus >= 201103L |
171 | template <typename _U1 = _T1, |
172 | typename _U2 = _T2, |
173 | typename enable_if<__and_< |
174 | is_default_constructible<_U1>, |
175 | is_default_constructible<_U2>, |
176 | __not_< |
177 | __and_<__is_implicitly_default_constructible<_U1>, |
178 | __is_implicitly_default_constructible<_U2>>>> |
179 | ::value, bool>::type = false> |
180 | explicit constexpr pair() |
181 | : first(), second() { } |
182 | #endif |
183 | |
184 | /** Two objects may be passed to a @c pair constructor to be copied. */ |
185 | #if __cplusplus < 201103L |
186 | pair(const _T1& __a, const _T2& __b) |
187 | : first(__a), second(__b) { } |
188 | #else |
189 | template<typename _U1 = _T1, typename _U2=_T2, typename |
190 | enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>() |
191 | && _ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), |
192 | bool>::type=true> |
193 | constexpr pair(const _T1& __a, const _T2& __b) |
194 | : first(__a), second(__b) { } |
195 | |
196 | template<typename _U1 = _T1, typename _U2=_T2, typename |
197 | enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>() |
198 | && !_ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), |
199 | bool>::type=false> |
200 | explicit constexpr pair(const _T1& __a, const _T2& __b) |
201 | : first(__a), second(__b) { } |
202 | #endif |
203 | |
204 | /** There is also a templated copy ctor for the @c pair class itself. */ |
205 | #if __cplusplus < 201103L |
206 | template<typename _U1, typename _U2> |
207 | pair(const pair<_U1, _U2>& __p) |
208 | : first(__p.first), second(__p.second) { } |
209 | #else |
210 | template<typename _U1, typename _U2, typename |
211 | enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>() |
212 | && _ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), |
213 | bool>::type=true> |
214 | constexpr pair(const pair<_U1, _U2>& __p) |
215 | : first(__p.first), second(__p.second) { } |
216 | |
217 | template<typename _U1, typename _U2, typename |
218 | enable_if<_ConstructiblePair<_T1, _T2, _U1, _U2>() |
219 | && !_ImplicitlyConvertiblePair<_T1, _T2, _U1, _U2>(), |
220 | bool>::type=false> |
221 | explicit constexpr pair(const pair<_U1, _U2>& __p) |
222 | : first(__p.first), second(__p.second) { } |
223 | |
224 | constexpr pair(const pair&) = default; |
225 | constexpr pair(pair&&) = default; |
226 | |
227 | // DR 811. |
228 | template<typename _U1, typename |
229 | enable_if<_ConstructiblePair<_T2, _T2, _T2, _T2>() |
230 | && _MoveConstructiblePair<_T1, _T2, _U1, _T2>() |
231 | && _ImplicitlyConvertiblePair<_T2, _T2, _T2, _T2>() |
232 | && _ImplicitlyMoveConvertiblePair<_T1, _T2, |
233 | _U1, _T2>(), |
234 | bool>::type=true> |
235 | constexpr pair(_U1&& __x, const _T2& __y) |
236 | : first(std::forward<_U1>(__x)), second(__y) { } |
237 | |
238 | template<typename _U1, typename |
239 | enable_if<_ConstructiblePair<_T2, _T2, _T2, _T2>() |
240 | && _MoveConstructiblePair<_T1, _T2, _U1, _T2>() |
241 | && (!_ImplicitlyConvertiblePair<_T2, _T2, _T2, _T2>() |
242 | || !_ImplicitlyMoveConvertiblePair<_T1, _T2, |
243 | _U1, _T2>()), |
244 | bool>::type=false> |
245 | explicit constexpr pair(_U1&& __x, const _T2& __y) |
246 | : first(std::forward<_U1>(__x)), second(__y) { } |
247 | |
248 | template<typename _U2, typename |
249 | enable_if<_ConstructiblePair<_T1, _T1, _T1, _T1>() |
250 | && _MoveConstructiblePair<_T1, _T2, _T1, _U2>() |
251 | && _ImplicitlyConvertiblePair<_T1, _T1, _T1, _T1>() |
252 | && _ImplicitlyMoveConvertiblePair<_T1, _T2, |
253 | _T1, _U2>(), |
254 | bool>::type=true> |
255 | constexpr pair(const _T1& __x, _U2&& __y) |
256 | : first(__x), second(std::forward<_U2>(__y)) { } |
257 | |
258 | template<typename _U2, typename |
259 | enable_if<_ConstructiblePair<_T1, _T1, _T1, _T1>() |
260 | && _MoveConstructiblePair<_T1, _T2, _T1, _U2>() |
261 | && (!_ImplicitlyConvertiblePair<_T1, _T1, _T1, _T1>() |
262 | || !_ImplicitlyMoveConvertiblePair<_T1, _T2, |
263 | _T1, _U2>()), |
264 | bool>::type=false> |
265 | explicit pair(const _T1& __x, _U2&& __y) |
266 | : first(__x), second(std::forward<_U2>(__y)) { } |
267 | |
268 | template<typename _U1, typename _U2, typename |
269 | enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>() |
270 | && _ImplicitlyMoveConvertiblePair<_T1, _T2, |
271 | _U1, _U2>(), |
272 | bool>::type=true> |
273 | constexpr pair(_U1&& __x, _U2&& __y) |
274 | : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } |
275 | |
276 | template<typename _U1, typename _U2, typename |
277 | enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>() |
278 | && !_ImplicitlyMoveConvertiblePair<_T1, _T2, |
279 | _U1, _U2>(), |
280 | bool>::type=false> |
281 | explicit constexpr pair(_U1&& __x, _U2&& __y) |
282 | : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } |
283 | |
284 | |
285 | template<typename _U1, typename _U2, typename |
286 | enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>() |
287 | && _ImplicitlyMoveConvertiblePair<_T1, _T2, |
288 | _U1, _U2>(), |
289 | bool>::type=true> |
290 | constexpr pair(pair<_U1, _U2>&& __p) |
291 | : first(std::forward<_U1>(__p.first)), |
292 | second(std::forward<_U2>(__p.second)) { } |
293 | |
294 | template<typename _U1, typename _U2, typename |
295 | enable_if<_MoveConstructiblePair<_T1, _T2, _U1, _U2>() |
296 | && !_ImplicitlyMoveConvertiblePair<_T1, _T2, |
297 | _U1, _U2>(), |
298 | bool>::type=false> |
299 | explicit constexpr pair(pair<_U1, _U2>&& __p) |
300 | : first(std::forward<_U1>(__p.first)), |
301 | second(std::forward<_U2>(__p.second)) { } |
302 | |
303 | template<typename... _Args1, typename... _Args2> |
304 | pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>); |
305 | |
306 | pair& |
307 | operator=(const pair& __p) |
308 | { |
309 | first = __p.first; |
310 | second = __p.second; |
311 | return *this; |
312 | } |
313 | |
314 | pair& |
315 | operator=(pair&& __p) |
316 | noexcept(__and_<is_nothrow_move_assignable<_T1>, |
317 | is_nothrow_move_assignable<_T2>>::value) |
318 | { |
319 | first = std::forward<first_type>(__p.first); |
320 | second = std::forward<second_type>(__p.second); |
321 | return *this; |
322 | } |
323 | |
324 | template<typename _U1, typename _U2> |
325 | pair& |
326 | operator=(const pair<_U1, _U2>& __p) |
327 | { |
328 | first = __p.first; |
329 | second = __p.second; |
330 | return *this; |
331 | } |
332 | |
333 | template<typename _U1, typename _U2> |
334 | pair& |
335 | operator=(pair<_U1, _U2>&& __p) |
336 | { |
337 | first = std::forward<_U1>(__p.first); |
338 | second = std::forward<_U2>(__p.second); |
339 | return *this; |
340 | } |
341 | |
342 | void |
343 | swap(pair& __p) |
344 | noexcept(__is_nothrow_swappable<_T1>::value |
345 | && __is_nothrow_swappable<_T2>::value) |
346 | { |
347 | using std::swap; |
348 | swap(first, __p.first); |
349 | swap(second, __p.second); |
350 | } |
351 | |
352 | private: |
353 | template<typename... _Args1, std::size_t... _Indexes1, |
354 | typename... _Args2, std::size_t... _Indexes2> |
355 | pair(tuple<_Args1...>&, tuple<_Args2...>&, |
356 | _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>); |
357 | #endif |
358 | }; |
359 | |
360 | /// Two pairs of the same type are equal iff their members are equal. |
361 | template<typename _T1, typename _T2> |
362 | inline _GLIBCXX_CONSTEXPR bool |
363 | operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
364 | { return __x.first == __y.first && __x.second == __y.second; } |
365 | |
366 | /// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html> |
367 | template<typename _T1, typename _T2> |
368 | inline _GLIBCXX_CONSTEXPR bool |
369 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
370 | { return __x.first < __y.first |
371 | || (!(__y.first < __x.first) && __x.second < __y.second); } |
372 | |
373 | /// Uses @c operator== to find the result. |
374 | template<typename _T1, typename _T2> |
375 | inline _GLIBCXX_CONSTEXPR bool |
376 | operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
377 | { return !(__x == __y); } |
378 | |
379 | /// Uses @c operator< to find the result. |
380 | template<typename _T1, typename _T2> |
381 | inline _GLIBCXX_CONSTEXPR bool |
382 | operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
383 | { return __y < __x; } |
384 | |
385 | /// Uses @c operator< to find the result. |
386 | template<typename _T1, typename _T2> |
387 | inline _GLIBCXX_CONSTEXPR bool |
388 | operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
389 | { return !(__y < __x); } |
390 | |
391 | /// Uses @c operator< to find the result. |
392 | template<typename _T1, typename _T2> |
393 | inline _GLIBCXX_CONSTEXPR bool |
394 | operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) |
395 | { return !(__x < __y); } |
396 | |
397 | #if __cplusplus >= 201103L |
398 | /// See std::pair::swap(). |
399 | // Note: no std::swap overloads in C++03 mode, this has performance |
400 | // implications, see, eg, libstdc++/38466. |
401 | template<typename _T1, typename _T2> |
402 | inline void |
403 | swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y) |
404 | noexcept(noexcept(__x.swap(__y))) |
405 | { __x.swap(__y); } |
406 | #endif |
407 | |
408 | /** |
409 | * @brief A convenience wrapper for creating a pair from two objects. |
410 | * @param __x The first object. |
411 | * @param __y The second object. |
412 | * @return A newly-constructed pair<> object of the appropriate type. |
413 | * |
414 | * The standard requires that the objects be passed by reference-to-const, |
415 | * but LWG issue #181 says they should be passed by const value. We follow |
416 | * the LWG by default. |
417 | */ |
418 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
419 | // 181. make_pair() unintended behavior |
420 | #if __cplusplus >= 201103L |
421 | // NB: DR 706. |
422 | template<typename _T1, typename _T2> |
423 | constexpr pair<typename __decay_and_strip<_T1>::__type, |
424 | typename __decay_and_strip<_T2>::__type> |
425 | make_pair(_T1&& __x, _T2&& __y) |
426 | { |
427 | typedef typename __decay_and_strip<_T1>::__type __ds_type1; |
428 | typedef typename __decay_and_strip<_T2>::__type __ds_type2; |
429 | typedef pair<__ds_type1, __ds_type2> __pair_type; |
430 | return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y)); |
431 | } |
432 | #else |
433 | template<typename _T1, typename _T2> |
434 | inline pair<_T1, _T2> |
435 | make_pair(_T1 __x, _T2 __y) |
436 | { return pair<_T1, _T2>(__x, __y); } |
437 | #endif |
438 | |
439 | /// @} |
440 | |
441 | _GLIBCXX_END_NAMESPACE_VERSION |
442 | } // namespace std |
443 | |
444 | #endif /* _STL_PAIR_H */ |
445 | |