1 |
35 |
ultra_embe |
// Pair implementation -*- C++ -*-
|
2 |
|
|
|
3 |
|
|
// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
4 |
|
|
// 2010, 2011, 2012
|
5 |
|
|
// Free Software Foundation, Inc.
|
6 |
|
|
//
|
7 |
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
8 |
|
|
// software; you can redistribute it and/or modify it under the
|
9 |
|
|
// terms of the GNU General Public License as published by the
|
10 |
|
|
// Free Software Foundation; either version 3, or (at your option)
|
11 |
|
|
// any later version.
|
12 |
|
|
|
13 |
|
|
// This library is distributed in the hope that it will be useful,
|
14 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
// GNU General Public License for more details.
|
17 |
|
|
|
18 |
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
19 |
|
|
// permissions described in the GCC Runtime Library Exception, version
|
20 |
|
|
// 3.1, as published by the Free Software Foundation.
|
21 |
|
|
|
22 |
|
|
// You should have received a copy of the GNU General Public License and
|
23 |
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
24 |
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
25 |
|
|
// <http://www.gnu.org/licenses/>.
|
26 |
|
|
|
27 |
|
|
/*
|
28 |
|
|
*
|
29 |
|
|
* Copyright (c) 1994
|
30 |
|
|
* Hewlett-Packard Company
|
31 |
|
|
*
|
32 |
|
|
* Permission to use, copy, modify, distribute and sell this software
|
33 |
|
|
* and its documentation for any purpose is hereby granted without fee,
|
34 |
|
|
* provided that the above copyright notice appear in all copies and
|
35 |
|
|
* that both that copyright notice and this permission notice appear
|
36 |
|
|
* in supporting documentation. Hewlett-Packard Company makes no
|
37 |
|
|
* representations about the suitability of this software for any
|
38 |
|
|
* purpose. It is provided "as is" without express or implied warranty.
|
39 |
|
|
*
|
40 |
|
|
*
|
41 |
|
|
* Copyright (c) 1996,1997
|
42 |
|
|
* Silicon Graphics Computer Systems, Inc.
|
43 |
|
|
*
|
44 |
|
|
* Permission to use, copy, modify, distribute and sell this software
|
45 |
|
|
* and its documentation for any purpose is hereby granted without fee,
|
46 |
|
|
* provided that the above copyright notice appear in all copies and
|
47 |
|
|
* that both that copyright notice and this permission notice appear
|
48 |
|
|
* in supporting documentation. Silicon Graphics makes no
|
49 |
|
|
* representations about the suitability of this software for any
|
50 |
|
|
* purpose. It is provided "as is" without express or implied warranty.
|
51 |
|
|
*/
|
52 |
|
|
|
53 |
|
|
/** @file bits/stl_pair.h
|
54 |
|
|
* This is an internal header file, included by other library headers.
|
55 |
|
|
* Do not attempt to use it directly. @headername{utility}
|
56 |
|
|
*/
|
57 |
|
|
|
58 |
|
|
#ifndef _STL_PAIR_H
|
59 |
|
|
#define _STL_PAIR_H 1
|
60 |
|
|
|
61 |
|
|
#include <bits/move.h> // for std::move / std::forward, and std::swap
|
62 |
|
|
|
63 |
|
|
#if __cplusplus >= 201103L
|
64 |
|
|
#include <type_traits> // for std::__decay_and_strip too
|
65 |
|
|
#endif
|
66 |
|
|
|
67 |
|
|
namespace std _GLIBCXX_VISIBILITY(default)
|
68 |
|
|
{
|
69 |
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
70 |
|
|
|
71 |
|
|
#if __cplusplus >= 201103L
|
72 |
|
|
/// piecewise_construct_t
|
73 |
|
|
struct piecewise_construct_t { };
|
74 |
|
|
|
75 |
|
|
/// piecewise_construct
|
76 |
|
|
constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
|
77 |
|
|
|
78 |
|
|
// Forward declarations.
|
79 |
|
|
template<typename...>
|
80 |
|
|
class tuple;
|
81 |
|
|
|
82 |
|
|
template<std::size_t...>
|
83 |
|
|
struct _Index_tuple;
|
84 |
|
|
#endif
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
* @brief Struct holding two objects of arbitrary type.
|
88 |
|
|
*
|
89 |
|
|
* @tparam _T1 Type of first object.
|
90 |
|
|
* @tparam _T2 Type of second object.
|
91 |
|
|
*/
|
92 |
|
|
template<class _T1, class _T2>
|
93 |
|
|
struct pair
|
94 |
|
|
{
|
95 |
|
|
typedef _T1 first_type; /// @c first_type is the first bound type
|
96 |
|
|
typedef _T2 second_type; /// @c second_type is the second bound type
|
97 |
|
|
|
98 |
|
|
_T1 first; /// @c first is a copy of the first object
|
99 |
|
|
_T2 second; /// @c second is a copy of the second object
|
100 |
|
|
|
101 |
|
|
// _GLIBCXX_RESOLVE_LIB_DEFECTS
|
102 |
|
|
// 265. std::pair::pair() effects overly restrictive
|
103 |
|
|
/** The default constructor creates @c first and @c second using their
|
104 |
|
|
* respective default constructors. */
|
105 |
|
|
_GLIBCXX_CONSTEXPR pair()
|
106 |
|
|
: first(), second() { }
|
107 |
|
|
|
108 |
|
|
/** Two objects may be passed to a @c pair constructor to be copied. */
|
109 |
|
|
_GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)
|
110 |
|
|
: first(__a), second(__b) { }
|
111 |
|
|
|
112 |
|
|
/** There is also a templated copy ctor for the @c pair class itself. */
|
113 |
|
|
#if __cplusplus < 201103L
|
114 |
|
|
template<class _U1, class _U2>
|
115 |
|
|
pair(const pair<_U1, _U2>& __p)
|
116 |
|
|
: first(__p.first), second(__p.second) { }
|
117 |
|
|
#else
|
118 |
|
|
template<class _U1, class _U2, class = typename
|
119 |
|
|
enable_if<__and_<is_convertible<const _U1&, _T1>,
|
120 |
|
|
is_convertible<const _U2&, _T2>>::value>::type>
|
121 |
|
|
constexpr pair(const pair<_U1, _U2>& __p)
|
122 |
|
|
: first(__p.first), second(__p.second) { }
|
123 |
|
|
|
124 |
|
|
constexpr pair(const pair&) = default;
|
125 |
|
|
constexpr pair(pair&&) = default;
|
126 |
|
|
|
127 |
|
|
// DR 811.
|
128 |
|
|
template<class _U1, class = typename
|
129 |
|
|
enable_if<is_convertible<_U1, _T1>::value>::type>
|
130 |
|
|
constexpr pair(_U1&& __x, const _T2& __y)
|
131 |
|
|
: first(std::forward<_U1>(__x)), second(__y) { }
|
132 |
|
|
|
133 |
|
|
template<class _U2, class = typename
|
134 |
|
|
enable_if<is_convertible<_U2, _T2>::value>::type>
|
135 |
|
|
constexpr pair(const _T1& __x, _U2&& __y)
|
136 |
|
|
: first(__x), second(std::forward<_U2>(__y)) { }
|
137 |
|
|
|
138 |
|
|
template<class _U1, class _U2, class = typename
|
139 |
|
|
enable_if<__and_<is_convertible<_U1, _T1>,
|
140 |
|
|
is_convertible<_U2, _T2>>::value>::type>
|
141 |
|
|
constexpr pair(_U1&& __x, _U2&& __y)
|
142 |
|
|
: first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
|
143 |
|
|
|
144 |
|
|
template<class _U1, class _U2, class = typename
|
145 |
|
|
enable_if<__and_<is_convertible<_U1, _T1>,
|
146 |
|
|
is_convertible<_U2, _T2>>::value>::type>
|
147 |
|
|
constexpr pair(pair<_U1, _U2>&& __p)
|
148 |
|
|
: first(std::forward<_U1>(__p.first)),
|
149 |
|
|
second(std::forward<_U2>(__p.second)) { }
|
150 |
|
|
|
151 |
|
|
template<typename... _Args1, typename... _Args2>
|
152 |
|
|
pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
|
153 |
|
|
|
154 |
|
|
pair&
|
155 |
|
|
operator=(const pair& __p)
|
156 |
|
|
{
|
157 |
|
|
first = __p.first;
|
158 |
|
|
second = __p.second;
|
159 |
|
|
return *this;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
pair&
|
163 |
|
|
operator=(pair&& __p)
|
164 |
|
|
noexcept(__and_<is_nothrow_move_assignable<_T1>,
|
165 |
|
|
is_nothrow_move_assignable<_T2>>::value)
|
166 |
|
|
{
|
167 |
|
|
first = std::forward<first_type>(__p.first);
|
168 |
|
|
second = std::forward<second_type>(__p.second);
|
169 |
|
|
return *this;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
template<class _U1, class _U2>
|
173 |
|
|
pair&
|
174 |
|
|
operator=(const pair<_U1, _U2>& __p)
|
175 |
|
|
{
|
176 |
|
|
first = __p.first;
|
177 |
|
|
second = __p.second;
|
178 |
|
|
return *this;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
template<class _U1, class _U2>
|
182 |
|
|
pair&
|
183 |
|
|
operator=(pair<_U1, _U2>&& __p)
|
184 |
|
|
{
|
185 |
|
|
first = std::forward<_U1>(__p.first);
|
186 |
|
|
second = std::forward<_U2>(__p.second);
|
187 |
|
|
return *this;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
void
|
191 |
|
|
swap(pair& __p)
|
192 |
|
|
noexcept(noexcept(swap(first, __p.first))
|
193 |
|
|
&& noexcept(swap(second, __p.second)))
|
194 |
|
|
{
|
195 |
|
|
using std::swap;
|
196 |
|
|
swap(first, __p.first);
|
197 |
|
|
swap(second, __p.second);
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
private:
|
201 |
|
|
template<typename... _Args1, std::size_t... _Indexes1,
|
202 |
|
|
typename... _Args2, std::size_t... _Indexes2>
|
203 |
|
|
pair(tuple<_Args1...>&, tuple<_Args2...>&,
|
204 |
|
|
_Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>);
|
205 |
|
|
#endif
|
206 |
|
|
};
|
207 |
|
|
|
208 |
|
|
/// Two pairs of the same type are equal iff their members are equal.
|
209 |
|
|
template<class _T1, class _T2>
|
210 |
|
|
inline _GLIBCXX_CONSTEXPR bool
|
211 |
|
|
operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
|
212 |
|
|
{ return __x.first == __y.first && __x.second == __y.second; }
|
213 |
|
|
|
214 |
|
|
/// <http://gcc.gnu.org/onlinedocs/libstdc++/manual/utilities.html>
|
215 |
|
|
template<class _T1, class _T2>
|
216 |
|
|
inline _GLIBCXX_CONSTEXPR bool
|
217 |
|
|
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
|
218 |
|
|
{ return __x.first < __y.first
|
219 |
|
|
|| (!(__y.first < __x.first) && __x.second < __y.second); }
|
220 |
|
|
|
221 |
|
|
/// Uses @c operator== to find the result.
|
222 |
|
|
template<class _T1, class _T2>
|
223 |
|
|
inline _GLIBCXX_CONSTEXPR bool
|
224 |
|
|
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
|
225 |
|
|
{ return !(__x == __y); }
|
226 |
|
|
|
227 |
|
|
/// Uses @c operator< to find the result.
|
228 |
|
|
template<class _T1, class _T2>
|
229 |
|
|
inline _GLIBCXX_CONSTEXPR bool
|
230 |
|
|
operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
|
231 |
|
|
{ return __y < __x; }
|
232 |
|
|
|
233 |
|
|
/// Uses @c operator< to find the result.
|
234 |
|
|
template<class _T1, class _T2>
|
235 |
|
|
inline _GLIBCXX_CONSTEXPR bool
|
236 |
|
|
operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
|
237 |
|
|
{ return !(__y < __x); }
|
238 |
|
|
|
239 |
|
|
/// Uses @c operator< to find the result.
|
240 |
|
|
template<class _T1, class _T2>
|
241 |
|
|
inline _GLIBCXX_CONSTEXPR bool
|
242 |
|
|
operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
|
243 |
|
|
{ return !(__x < __y); }
|
244 |
|
|
|
245 |
|
|
#if __cplusplus >= 201103L
|
246 |
|
|
/// See std::pair::swap().
|
247 |
|
|
// Note: no std::swap overloads in C++03 mode, this has performance
|
248 |
|
|
// implications, see, eg, libstdc++/38466.
|
249 |
|
|
template<class _T1, class _T2>
|
250 |
|
|
inline void
|
251 |
|
|
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
|
252 |
|
|
noexcept(noexcept(__x.swap(__y)))
|
253 |
|
|
{ __x.swap(__y); }
|
254 |
|
|
#endif
|
255 |
|
|
|
256 |
|
|
/**
|
257 |
|
|
* @brief A convenience wrapper for creating a pair from two objects.
|
258 |
|
|
* @param __x The first object.
|
259 |
|
|
* @param __y The second object.
|
260 |
|
|
* @return A newly-constructed pair<> object of the appropriate type.
|
261 |
|
|
*
|
262 |
|
|
* The standard requires that the objects be passed by reference-to-const,
|
263 |
|
|
* but LWG issue #181 says they should be passed by const value. We follow
|
264 |
|
|
* the LWG by default.
|
265 |
|
|
*/
|
266 |
|
|
// _GLIBCXX_RESOLVE_LIB_DEFECTS
|
267 |
|
|
// 181. make_pair() unintended behavior
|
268 |
|
|
#if __cplusplus >= 201103L
|
269 |
|
|
// NB: DR 706.
|
270 |
|
|
template<class _T1, class _T2>
|
271 |
|
|
constexpr pair<typename __decay_and_strip<_T1>::__type,
|
272 |
|
|
typename __decay_and_strip<_T2>::__type>
|
273 |
|
|
make_pair(_T1&& __x, _T2&& __y)
|
274 |
|
|
{
|
275 |
|
|
typedef typename __decay_and_strip<_T1>::__type __ds_type1;
|
276 |
|
|
typedef typename __decay_and_strip<_T2>::__type __ds_type2;
|
277 |
|
|
typedef pair<__ds_type1, __ds_type2> __pair_type;
|
278 |
|
|
return __pair_type(std::forward<_T1>(__x), std::forward<_T2>(__y));
|
279 |
|
|
}
|
280 |
|
|
#else
|
281 |
|
|
template<class _T1, class _T2>
|
282 |
|
|
inline pair<_T1, _T2>
|
283 |
|
|
make_pair(_T1 __x, _T2 __y)
|
284 |
|
|
{ return pair<_T1, _T2>(__x, __y); }
|
285 |
|
|
#endif
|
286 |
|
|
|
287 |
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
288 |
|
|
} // namespace
|
289 |
|
|
|
290 |
|
|
#endif /* _STL_PAIR_H */
|