blob: 3e4b401f2d9ac11c9f04becb0232f28134c0ba79 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- utility -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_UTILITY
12#define _LIBCPP_UTILITY
13
14/*
15 utility synopsis
16
17namespace std
18{
19
20template <class T>
21 void
22 swap(T& a, T& b);
23
24namespace 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
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000032template<class T>
33void
34swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
35 is_nothrow_move_assignable<T>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000036
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000037template <class T, size_t N>
38void
39swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
40
41template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;
42template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept;
43
44template <class T> typename remove_reference<T>::type&& move(T&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045
46template <class T>
47 typename conditional
48 <
Howard Hinnant1468b662010-11-19 22:17:28 +000049 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050 const T&,
51 T&&
52 >::type
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000053 move_if_noexcept(T& x) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054
55template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
56
57template <class T1, class T2>
58struct pair
59{
60 typedef T1 first_type;
61 typedef T2 second_type;
62
63 T1 first;
64 T2 second;
65
66 pair(const pair&) = default;
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000067 pair(pair&&) = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068 constexpr pair();
69 pair(const T1& x, const T2& y);
70 template <class U, class V> pair(U&& x, V&& y);
71 template <class U, class V> pair(const pair<U, V>& p);
72 template <class U, class V> pair(pair<U, V>&& p);
73 template <class... Args1, class... Args2>
74 pair(piecewise_construct_t, tuple<Args1...> first_args,
75 tuple<Args2...> second_args);
76
77 template <class U, class V> pair& operator=(const pair<U, V>& p);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000078 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
79 is_nothrow_move_assignable<T2>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080 template <class U, class V> pair& operator=(pair<U, V>&& p);
81
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000082 void swap(pair& p) noexcept(noexcept(swap(first, p.first)) &&
83 noexcept(swap(second, p.second)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084};
85
86template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&);
87template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&);
88template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
89template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
90template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&);
91template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&);
92
93template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +000094template <class T1, class T2>
95void
96swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097
98struct piecewise_construct_t { };
99constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
100
101template <class T> class tuple_size;
102template <size_t I, class T> class tuple_element;
103
104template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >;
105template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >;
106template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
107
108template<size_t I, class T1, class T2>
109 typename tuple_element<I, std::pair<T1, T2> >::type&
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000110 get(std::pair<T1, T2>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111
112template<size_t I, class T1, class T2>
113 const typename const tuple_element<I, std::pair<T1, T2> >::type&
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000114 get(const std::pair<T1, T2>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000116template<size_t I, class T1, class T2>
117 typename tuple_element<I, std::pair<T1, T2> >::type&&
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000118 get(std::pair<T1, T2>&&) noexcept;
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000119
Marshall Clowe8029e52013-07-13 02:54:05 +0000120template<class T1, class T2>
121 constexpr T1& get(std::pair<T1, T2>&) noexcept; // C++14
122
123template<size_t I, class T1, class T2>
124 constexpr T1 const& get(std::pair<T1, T2> const &) noexcept; // C++14
125
126template<size_t I, class T1, class T2>
127 constexpr T1&& get(std::pair<T1, T2>&&) noexcept; // C++14
128
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000129// C++14
130
131template<class T, T... I>
132struct integer_sequence
133{
134 typedef T value_type;
135
136 static constexpr size_t size() noexcept;
137};
138
139template<size_t... I>
140 using index_sequence = integer_sequence<size_t, I...>;
141
142template<class T, T N>
143 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
144template<size_t N>
145 using make_index_sequence = make_integer_sequence<size_t, N>;
146
147template<class... T>
148 using index_sequence_for = make_index_sequence<sizeof...(T)>;
149
Marshall Clowe2735d12013-07-08 20:54:40 +0000150template<class T, class U=T>
151 T exchange(T& obj, U&& new_value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152} // std
153
154*/
155
156#include <__config>
157#include <__tuple>
158#include <type_traits>
159
Howard Hinnant08e17472011-10-17 20:05:10 +0000160#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000162#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163
164_LIBCPP_BEGIN_NAMESPACE_STD
165
166namespace rel_ops
167{
168
169template<class _Tp>
170inline _LIBCPP_INLINE_VISIBILITY
171bool
172operator!=(const _Tp& __x, const _Tp& __y)
173{
174 return !(__x == __y);
175}
176
177template<class _Tp>
178inline _LIBCPP_INLINE_VISIBILITY
179bool
180operator> (const _Tp& __x, const _Tp& __y)
181{
182 return __y < __x;
183}
184
185template<class _Tp>
186inline _LIBCPP_INLINE_VISIBILITY
187bool
188operator<=(const _Tp& __x, const _Tp& __y)
189{
190 return !(__y < __x);
191}
192
193template<class _Tp>
194inline _LIBCPP_INLINE_VISIBILITY
195bool
196operator>=(const _Tp& __x, const _Tp& __y)
197{
198 return !(__x < __y);
199}
200
201} // rel_ops
202
203// swap_ranges
204
205template <class _ForwardIterator1, class _ForwardIterator2>
206inline _LIBCPP_INLINE_VISIBILITY
207_ForwardIterator2
208swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
209{
210 for(; __first1 != __last1; ++__first1, ++__first2)
211 swap(*__first1, *__first2);
212 return __first2;
213}
214
Howard Hinnant99968442011-11-29 18:15:50 +0000215template<class _Tp, size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216inline _LIBCPP_INLINE_VISIBILITY
217void
Howard Hinnant99968442011-11-29 18:15:50 +0000218swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219{
Howard Hinnant99968442011-11-29 18:15:50 +0000220 _VSTD::swap_ranges(__a, __a + _Np, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221}
222
223template <class _Tp>
224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant73d21a42010-09-04 23:28:19 +0000225#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000226typename conditional
227<
Howard Hinnant1468b662010-11-19 22:17:28 +0000228 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229 const _Tp&,
230 _Tp&&
231>::type
Howard Hinnant73d21a42010-09-04 23:28:19 +0000232#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233const _Tp&
234#endif
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000235move_if_noexcept(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000237 return _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238}
239
Howard Hinnant83eade62013-03-06 23:30:19 +0000240struct _LIBCPP_TYPE_VIS piecewise_construct_t { };
Howard Hinnant616e92d2012-04-03 23:45:46 +0000241#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
Howard Hinnant2a5349b2012-04-03 21:09:48 +0000243#else
244constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
245#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000248struct _LIBCPP_TYPE_VIS pair
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249{
250 typedef _T1 first_type;
251 typedef _T2 second_type;
252
253 _T1 first;
254 _T2 second;
255
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000256 // pair(const pair&) = default;
257 // pair(pair&&) = default;
258
Howard Hinnant2a5349b2012-04-03 21:09:48 +0000259 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR pair() : first(), second() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
261 _LIBCPP_INLINE_VISIBILITY pair(const _T1& __x, const _T2& __y)
262 : first(__x), second(__y) {}
263
Howard Hinnant469d4192011-04-29 18:10:55 +0000264 template<class _U1, class _U2>
265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c59d382011-07-27 19:25:28 +0000266 pair(const pair<_U1, _U2>& __p
267#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantfe592762012-06-09 20:01:23 +0000268 ,typename enable_if<is_convertible<const _U1&, _T1>::value &&
269 is_convertible<const _U2&, _T2>::value>::type* = 0
Howard Hinnant9c59d382011-07-27 19:25:28 +0000270#endif
271 )
Howard Hinnant469d4192011-04-29 18:10:55 +0000272 : first(__p.first), second(__p.second) {}
273
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant61aa6012011-07-01 19:24:36 +0000275 pair(const pair& __p)
276 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
277 is_nothrow_copy_constructible<second_type>::value)
278 : first(__p.first),
279 second(__p.second)
280 {
281 }
282
283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000284 pair& operator=(const pair& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +0000285 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
286 is_nothrow_copy_assignable<second_type>::value)
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000287 {
288 first = __p.first;
289 second = __p.second;
290 return *this;
291 }
292
Howard Hinnant73d21a42010-09-04 23:28:19 +0000293#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294
295 template <class _U1, class _U2,
Howard Hinnantfe592762012-06-09 20:01:23 +0000296 class = typename enable_if<is_convertible<_U1, first_type>::value &&
297 is_convertible<_U2, second_type>::value>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 _LIBCPP_INLINE_VISIBILITY
299 pair(_U1&& __u1, _U2&& __u2)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000300 : first(_VSTD::forward<_U1>(__u1)),
301 second(_VSTD::forward<_U2>(__u2))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302 {}
303
Howard Hinnant469d4192011-04-29 18:10:55 +0000304 template<class _U1, class _U2>
305 _LIBCPP_INLINE_VISIBILITY
306 pair(pair<_U1, _U2>&& __p,
Howard Hinnantfe592762012-06-09 20:01:23 +0000307 typename enable_if<is_convertible<_U1, _T1>::value &&
308 is_convertible<_U2, _T2>::value>::type* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000309 : first(_VSTD::forward<_U1>(__p.first)),
310 second(_VSTD::forward<_U2>(__p.second)) {}
Howard Hinnant469d4192011-04-29 18:10:55 +0000311
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant61aa6012011-07-01 19:24:36 +0000313 pair(pair&& __p) _NOEXCEPT_(is_nothrow_move_constructible<first_type>::value &&
314 is_nothrow_move_constructible<second_type>::value)
315 : first(_VSTD::forward<first_type>(__p.first)),
316 second(_VSTD::forward<second_type>(__p.second))
317 {
318 }
319
320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000321 pair&
322 operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
323 is_nothrow_move_assignable<second_type>::value)
324 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000325 first = _VSTD::forward<first_type>(__p.first);
326 second = _VSTD::forward<second_type>(__p.second);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000327 return *this;
328 }
329
Michael J. Spencer626916f2010-12-10 19:47:54 +0000330#ifndef _LIBCPP_HAS_NO_VARIADICS
331
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332 template<class _Tuple,
333 class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
334 _LIBCPP_INLINE_VISIBILITY
335 pair(_Tuple&& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000336 : first(_VSTD::forward<typename tuple_element<0,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 typename __make_tuple_types<_Tuple>::type>::type>(get<0>(__p))),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000338 second(_VSTD::forward<typename tuple_element<1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339 typename __make_tuple_types<_Tuple>::type>::type>(get<1>(__p)))
340 {}
341
Michael J. Spencer626916f2010-12-10 19:47:54 +0000342
Howard Hinnant726a76f2010-11-16 21:10:23 +0000343
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000344 template <class... _Args1, class... _Args2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346 pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
347 tuple<_Args2...> __second_args)
Howard Hinnant324bb032010-08-22 00:02:43 +0000348 : pair(__pc, __first_args, __second_args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
350 typename __make_tuple_indices<sizeof...(_Args2) >::type())
351 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352
353 template <class _Tuple,
354 class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356 pair&
357 operator=(_Tuple&& __p)
358 {
359 typedef typename __make_tuple_types<_Tuple>::type _TupleRef;
360 typedef typename tuple_element<0, _TupleRef>::type _U0;
361 typedef typename tuple_element<1, _TupleRef>::type _U1;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000362 first = _VSTD::forward<_U0>(_VSTD::get<0>(__p));
363 second = _VSTD::forward<_U1>(_VSTD::get<1>(__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364 return *this;
365 }
366
Michael J. Spencer626916f2010-12-10 19:47:54 +0000367#endif // _LIBCPP_HAS_NO_VARIADICS
368
Howard Hinnant73d21a42010-09-04 23:28:19 +0000369#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000370 _LIBCPP_INLINE_VISIBILITY
371 void
Howard Hinnanta5e01212011-05-27 19:08:18 +0000372 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
373 __is_nothrow_swappable<second_type>::value)
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000374 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000375 _VSTD::iter_swap(&first, &__p.first);
376 _VSTD::iter_swap(&second, &__p.second);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000377 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378private:
Howard Hinnant324bb032010-08-22 00:02:43 +0000379
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380#ifndef _LIBCPP_HAS_NO_VARIADICS
381 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
Howard Hinnant5f5859c2011-01-13 20:05:05 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 pair(piecewise_construct_t,
384 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
385 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
Howard Hinnant324bb032010-08-22 00:02:43 +0000386#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387};
388
389template <class _T1, class _T2>
390inline _LIBCPP_INLINE_VISIBILITY
391bool
392operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
393{
394 return __x.first == __y.first && __x.second == __y.second;
395}
396
397template <class _T1, class _T2>
398inline _LIBCPP_INLINE_VISIBILITY
399bool
400operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
401{
402 return !(__x == __y);
403}
404
405template <class _T1, class _T2>
406inline _LIBCPP_INLINE_VISIBILITY
407bool
408operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
409{
410 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
411}
412
413template <class _T1, class _T2>
414inline _LIBCPP_INLINE_VISIBILITY
415bool
416operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
417{
418 return __y < __x;
419}
420
421template <class _T1, class _T2>
422inline _LIBCPP_INLINE_VISIBILITY
423bool
424operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
425{
426 return !(__x < __y);
427}
428
429template <class _T1, class _T2>
430inline _LIBCPP_INLINE_VISIBILITY
431bool
432operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
433{
434 return !(__y < __x);
435}
436
437template <class _T1, class _T2>
438inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaabf2872011-06-01 19:59:32 +0000439typename enable_if
440<
441 __is_swappable<_T1>::value &&
442 __is_swappable<_T2>::value,
443 void
444>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
Howard Hinnanta5e01212011-05-27 19:08:18 +0000446 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
447 __is_nothrow_swappable<_T2>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448{
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000449 __x.swap(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450}
451
Howard Hinnant73d21a42010-09-04 23:28:19 +0000452#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453
Howard Hinnant83eade62013-03-06 23:30:19 +0000454template <class _Tp> class _LIBCPP_TYPE_VIS reference_wrapper;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455
456template <class _Tp>
457struct ___make_pair_return
458{
459 typedef _Tp type;
460};
461
462template <class _Tp>
463struct ___make_pair_return<reference_wrapper<_Tp>>
464{
465 typedef _Tp& type;
466};
467
468template <class _Tp>
469struct __make_pair_return
470{
471 typedef typename ___make_pair_return<typename decay<_Tp>::type>::type type;
472};
473
474template <class _T1, class _T2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
477make_pair(_T1&& __t1, _T2&& __t2)
478{
479 return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
Howard Hinnant0949eed2011-06-30 21:18:19 +0000480 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481}
482
Howard Hinnant73d21a42010-09-04 23:28:19 +0000483#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484
485template <class _T1, class _T2>
486inline _LIBCPP_INLINE_VISIBILITY
487pair<_T1,_T2>
488make_pair(_T1 __x, _T2 __y)
489{
490 return pair<_T1, _T2>(__x, __y);
491}
492
Howard Hinnant73d21a42010-09-04 23:28:19 +0000493#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000495template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000496 class _LIBCPP_TYPE_VIS tuple_size<pair<_T1, _T2> >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000497 : public integral_constant<size_t, 2> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498
499template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000500 class _LIBCPP_TYPE_VIS tuple_size<const pair<_T1, _T2> >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000501 : public integral_constant<size_t, 2> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502
503template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000504class _LIBCPP_TYPE_VIS tuple_element<0, pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000505{
506public:
507 typedef _T1 type;
508};
509
510template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000511class _LIBCPP_TYPE_VIS tuple_element<1, pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000512{
513public:
514 typedef _T2 type;
515};
516
517template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000518class _LIBCPP_TYPE_VIS tuple_element<0, const pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519{
520public:
521 typedef const _T1 type;
522};
523
524template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000525class _LIBCPP_TYPE_VIS tuple_element<1, const pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526{
527public:
528 typedef const _T2 type;
529};
530
531template <size_t _Ip> struct __get_pair;
532
533template <>
534struct __get_pair<0>
535{
536 template <class _T1, class _T2>
537 static
538 _LIBCPP_INLINE_VISIBILITY
539 _T1&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000540 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541
542 template <class _T1, class _T2>
543 static
544 _LIBCPP_INLINE_VISIBILITY
545 const _T1&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000546 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000547
548#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
549
550 template <class _T1, class _T2>
551 static
552 _LIBCPP_INLINE_VISIBILITY
553 _T1&&
Howard Hinnant0949eed2011-06-30 21:18:19 +0000554 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000555
556#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557};
558
559template <>
560struct __get_pair<1>
561{
562 template <class _T1, class _T2>
563 static
564 _LIBCPP_INLINE_VISIBILITY
565 _T2&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000566 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567
568 template <class _T1, class _T2>
569 static
570 _LIBCPP_INLINE_VISIBILITY
571 const _T2&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000572 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000573
574#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
575
576 template <class _T1, class _T2>
577 static
578 _LIBCPP_INLINE_VISIBILITY
579 _T2&&
Howard Hinnant0949eed2011-06-30 21:18:19 +0000580 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000581
582#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583};
584
585template <size_t _Ip, class _T1, class _T2>
586_LIBCPP_INLINE_VISIBILITY inline
587typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000588get(pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589{
590 return __get_pair<_Ip>::get(__p);
591}
592
593template <size_t _Ip, class _T1, class _T2>
594_LIBCPP_INLINE_VISIBILITY inline
595const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000596get(const pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597{
598 return __get_pair<_Ip>::get(__p);
599}
600
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000601#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
602
603template <size_t _Ip, class _T1, class _T2>
604_LIBCPP_INLINE_VISIBILITY inline
605typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000606get(pair<_T1, _T2>&& __p) _NOEXCEPT
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000607{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000608 return __get_pair<_Ip>::get(_VSTD::move(__p));
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000609}
610
611#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
612
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000613#if _LIBCPP_STD_VER > 11
Marshall Clowe8029e52013-07-13 02:54:05 +0000614template <class _T1, class _T2>
615_LIBCPP_INLINE_VISIBILITY inline
616constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
617{
618 return __get_pair<0>::get(__p);
619}
620
621template <class _T1, class _T2>
622_LIBCPP_INLINE_VISIBILITY inline
623constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
624{
625 return __get_pair<0>::get(__p);
626}
627
628template <class _T1, class _T2>
629_LIBCPP_INLINE_VISIBILITY inline
630constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
631{
632 return __get_pair<0>::get(_VSTD::move(__p));
633}
634
635template <class _T1, class _T2>
636_LIBCPP_INLINE_VISIBILITY inline
637constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
638{
639 return __get_pair<1>::get(__p);
640}
641
642template <class _T1, class _T2>
643_LIBCPP_INLINE_VISIBILITY inline
644constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
645{
646 return __get_pair<1>::get(__p);
647}
648
649template <class _T1, class _T2>
650_LIBCPP_INLINE_VISIBILITY inline
651constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
652{
653 return __get_pair<1>::get(_VSTD::move(__p));
654}
655
656#endif
657
658#if _LIBCPP_STD_VER > 11
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000659
660template<class _Tp, _Tp... _Ip>
Marshall Clow42e55e92013-07-03 19:20:30 +0000661struct _LIBCPP_TYPE_VIS integer_sequence
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000662{
663 typedef _Tp value_type;
664 static_assert( is_integral<_Tp>::value,
665 "std::integer_sequence can only be instantiated with an integral type" );
666 static
667 _LIBCPP_INLINE_VISIBILITY
668 constexpr
669 size_t
670 size() noexcept { return sizeof...(_Ip); }
671};
672
673template<size_t... _Ip>
674 using index_sequence = integer_sequence<size_t, _Ip...>;
675
Marshall Clow42e55e92013-07-03 19:20:30 +0000676namespace __detail {
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000677
Marshall Clow42e55e92013-07-03 19:20:30 +0000678template<typename _Tp, size_t ..._Extra> struct __repeat;
679template<typename _Tp, _Tp ..._Np, size_t ..._Extra> struct __repeat<integer_sequence<_Tp, _Np...>, _Extra...> {
680 typedef integer_sequence<_Tp,
681 _Np...,
682 sizeof...(_Np) + _Np...,
683 2 * sizeof...(_Np) + _Np...,
684 3 * sizeof...(_Np) + _Np...,
685 4 * sizeof...(_Np) + _Np...,
686 5 * sizeof...(_Np) + _Np...,
687 6 * sizeof...(_Np) + _Np...,
688 7 * sizeof...(_Np) + _Np...,
689 _Extra...> type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000690};
691
Marshall Clow42e55e92013-07-03 19:20:30 +0000692template<size_t _Np> struct __parity;
693template<size_t _Np> struct __make : __parity<_Np % 8>::template __pmake<_Np> {};
694
695template<> struct __make<0> { typedef integer_sequence<size_t> type; };
696template<> struct __make<1> { typedef integer_sequence<size_t, 0> type; };
697template<> struct __make<2> { typedef integer_sequence<size_t, 0, 1> type; };
698template<> struct __make<3> { typedef integer_sequence<size_t, 0, 1, 2> type; };
699template<> struct __make<4> { typedef integer_sequence<size_t, 0, 1, 2, 3> type; };
700template<> struct __make<5> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4> type; };
701template<> struct __make<6> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5> type; };
702template<> struct __make<7> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5, 6> type; };
703
704template<> struct __parity<0> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type> {}; };
705template<> struct __parity<1> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 1> {}; };
706template<> struct __parity<2> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 2, _Np - 1> {}; };
707template<> struct __parity<3> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 3, _Np - 2, _Np - 1> {}; };
708template<> struct __parity<4> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
709template<> struct __parity<5> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
710template<> struct __parity<6> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
711template<> struct __parity<7> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 7, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
712
713template<typename _Tp, typename _Up> struct __convert {
714 template<typename> struct __result;
715 template<_Tp ..._Np> struct __result<integer_sequence<_Tp, _Np...> > { typedef integer_sequence<_Up, _Np...> type; };
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000716};
Marshall Clow42e55e92013-07-03 19:20:30 +0000717template<typename _Tp> struct __convert<_Tp, _Tp> { template<typename _Up> struct __result { typedef _Up type; }; };
718
719}
720
721template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked =
722 typename __detail::__convert<size_t, _Tp>::template __result<typename __detail::__make<_Np>::type>::type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000723
724template <class _Tp, _Tp _Ep>
725struct __make_integer_sequence
726{
727 static_assert(is_integral<_Tp>::value,
728 "std::make_integer_sequence can only be instantiated with an integral type" );
729 static_assert(0 <= _Ep, "std::make_integer_sequence input shall not be negative");
Marshall Clow42e55e92013-07-03 19:20:30 +0000730 typedef __make_integer_sequence_unchecked<_Tp, _Ep> type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000731};
732
733template<class _Tp, _Tp _Np>
734 using make_integer_sequence = typename __make_integer_sequence<_Tp, _Np>::type;
735
736template<size_t _Np>
737 using make_index_sequence = make_integer_sequence<size_t, _Np>;
738
739template<class... _Tp>
740 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
741
742#endif // _LIBCPP_STD_VER > 11
743
Marshall Clowe2735d12013-07-08 20:54:40 +0000744#if _LIBCPP_STD_VER > 11
745template<class _T1, class _T2 = _T1>
746_LIBCPP_INLINE_VISIBILITY inline
747_T1 exchange(_T1& __obj, _T2 && __new_value)
748{
Howard Hinnant171771a2013-07-08 21:06:38 +0000749 _T1 __old_value = _VSTD::move(__obj);
750 __obj = _VSTD::forward<_T2>(__new_value);
751 return __old_value;
752}
Marshall Clowe2735d12013-07-08 20:54:40 +0000753#endif // _LIBCPP_STD_VER > 11
754
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755_LIBCPP_END_NAMESPACE_STD
756
757#endif // _LIBCPP_UTILITY