blob: ba656493c509579de34d3b866249ee276249f020 [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 Clow7ec46bc2013-07-01 16:26:55 +0000120// C++14
121
122template<class T, T... I>
123struct integer_sequence
124{
125 typedef T value_type;
126
127 static constexpr size_t size() noexcept;
128};
129
130template<size_t... I>
131 using index_sequence = integer_sequence<size_t, I...>;
132
133template<class T, T N>
134 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
135template<size_t N>
136 using make_index_sequence = make_integer_sequence<size_t, N>;
137
138template<class... T>
139 using index_sequence_for = make_index_sequence<sizeof...(T)>;
140
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141} // std
142
143*/
144
145#include <__config>
146#include <__tuple>
147#include <type_traits>
148
Howard Hinnant08e17472011-10-17 20:05:10 +0000149#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000151#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152
153_LIBCPP_BEGIN_NAMESPACE_STD
154
155namespace rel_ops
156{
157
158template<class _Tp>
159inline _LIBCPP_INLINE_VISIBILITY
160bool
161operator!=(const _Tp& __x, const _Tp& __y)
162{
163 return !(__x == __y);
164}
165
166template<class _Tp>
167inline _LIBCPP_INLINE_VISIBILITY
168bool
169operator> (const _Tp& __x, const _Tp& __y)
170{
171 return __y < __x;
172}
173
174template<class _Tp>
175inline _LIBCPP_INLINE_VISIBILITY
176bool
177operator<=(const _Tp& __x, const _Tp& __y)
178{
179 return !(__y < __x);
180}
181
182template<class _Tp>
183inline _LIBCPP_INLINE_VISIBILITY
184bool
185operator>=(const _Tp& __x, const _Tp& __y)
186{
187 return !(__x < __y);
188}
189
190} // rel_ops
191
192// swap_ranges
193
194template <class _ForwardIterator1, class _ForwardIterator2>
195inline _LIBCPP_INLINE_VISIBILITY
196_ForwardIterator2
197swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
198{
199 for(; __first1 != __last1; ++__first1, ++__first2)
200 swap(*__first1, *__first2);
201 return __first2;
202}
203
Howard Hinnant99968442011-11-29 18:15:50 +0000204template<class _Tp, size_t _Np>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205inline _LIBCPP_INLINE_VISIBILITY
206void
Howard Hinnant99968442011-11-29 18:15:50 +0000207swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208{
Howard Hinnant99968442011-11-29 18:15:50 +0000209 _VSTD::swap_ranges(__a, __a + _Np, __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210}
211
212template <class _Tp>
213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant73d21a42010-09-04 23:28:19 +0000214#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215typename conditional
216<
Howard Hinnant1468b662010-11-19 22:17:28 +0000217 !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000218 const _Tp&,
219 _Tp&&
220>::type
Howard Hinnant73d21a42010-09-04 23:28:19 +0000221#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222const _Tp&
223#endif
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000224move_if_noexcept(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000226 return _VSTD::move(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227}
228
Howard Hinnant83eade62013-03-06 23:30:19 +0000229struct _LIBCPP_TYPE_VIS piecewise_construct_t { };
Howard Hinnant616e92d2012-04-03 23:45:46 +0000230#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
Howard Hinnant2a5349b2012-04-03 21:09:48 +0000232#else
233constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
234#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000237struct _LIBCPP_TYPE_VIS pair
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238{
239 typedef _T1 first_type;
240 typedef _T2 second_type;
241
242 _T1 first;
243 _T2 second;
244
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000245 // pair(const pair&) = default;
246 // pair(pair&&) = default;
247
Howard Hinnant2a5349b2012-04-03 21:09:48 +0000248 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR pair() : first(), second() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249
250 _LIBCPP_INLINE_VISIBILITY pair(const _T1& __x, const _T2& __y)
251 : first(__x), second(__y) {}
252
Howard Hinnant469d4192011-04-29 18:10:55 +0000253 template<class _U1, class _U2>
254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c59d382011-07-27 19:25:28 +0000255 pair(const pair<_U1, _U2>& __p
256#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantfe592762012-06-09 20:01:23 +0000257 ,typename enable_if<is_convertible<const _U1&, _T1>::value &&
258 is_convertible<const _U2&, _T2>::value>::type* = 0
Howard Hinnant9c59d382011-07-27 19:25:28 +0000259#endif
260 )
Howard Hinnant469d4192011-04-29 18:10:55 +0000261 : first(__p.first), second(__p.second) {}
262
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant61aa6012011-07-01 19:24:36 +0000264 pair(const pair& __p)
265 _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
266 is_nothrow_copy_constructible<second_type>::value)
267 : first(__p.first),
268 second(__p.second)
269 {
270 }
271
272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000273 pair& operator=(const pair& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +0000274 _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
275 is_nothrow_copy_assignable<second_type>::value)
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000276 {
277 first = __p.first;
278 second = __p.second;
279 return *this;
280 }
281
Howard Hinnant73d21a42010-09-04 23:28:19 +0000282#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283
284 template <class _U1, class _U2,
Howard Hinnantfe592762012-06-09 20:01:23 +0000285 class = typename enable_if<is_convertible<_U1, first_type>::value &&
286 is_convertible<_U2, second_type>::value>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287 _LIBCPP_INLINE_VISIBILITY
288 pair(_U1&& __u1, _U2&& __u2)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000289 : first(_VSTD::forward<_U1>(__u1)),
290 second(_VSTD::forward<_U2>(__u2))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291 {}
292
Howard Hinnant469d4192011-04-29 18:10:55 +0000293 template<class _U1, class _U2>
294 _LIBCPP_INLINE_VISIBILITY
295 pair(pair<_U1, _U2>&& __p,
Howard Hinnantfe592762012-06-09 20:01:23 +0000296 typename enable_if<is_convertible<_U1, _T1>::value &&
297 is_convertible<_U2, _T2>::value>::type* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000298 : first(_VSTD::forward<_U1>(__p.first)),
299 second(_VSTD::forward<_U2>(__p.second)) {}
Howard Hinnant469d4192011-04-29 18:10:55 +0000300
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant61aa6012011-07-01 19:24:36 +0000302 pair(pair&& __p) _NOEXCEPT_(is_nothrow_move_constructible<first_type>::value &&
303 is_nothrow_move_constructible<second_type>::value)
304 : first(_VSTD::forward<first_type>(__p.first)),
305 second(_VSTD::forward<second_type>(__p.second))
306 {
307 }
308
309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000310 pair&
311 operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
312 is_nothrow_move_assignable<second_type>::value)
313 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000314 first = _VSTD::forward<first_type>(__p.first);
315 second = _VSTD::forward<second_type>(__p.second);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000316 return *this;
317 }
318
Michael J. Spencer626916f2010-12-10 19:47:54 +0000319#ifndef _LIBCPP_HAS_NO_VARIADICS
320
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321 template<class _Tuple,
322 class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
323 _LIBCPP_INLINE_VISIBILITY
324 pair(_Tuple&& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000325 : first(_VSTD::forward<typename tuple_element<0,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326 typename __make_tuple_types<_Tuple>::type>::type>(get<0>(__p))),
Howard Hinnant0949eed2011-06-30 21:18:19 +0000327 second(_VSTD::forward<typename tuple_element<1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328 typename __make_tuple_types<_Tuple>::type>::type>(get<1>(__p)))
329 {}
330
Michael J. Spencer626916f2010-12-10 19:47:54 +0000331
Howard Hinnant726a76f2010-11-16 21:10:23 +0000332
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000333 template <class... _Args1, class... _Args2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
336 tuple<_Args2...> __second_args)
Howard Hinnant324bb032010-08-22 00:02:43 +0000337 : pair(__pc, __first_args, __second_args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000338 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
339 typename __make_tuple_indices<sizeof...(_Args2) >::type())
340 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341
342 template <class _Tuple,
343 class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345 pair&
346 operator=(_Tuple&& __p)
347 {
348 typedef typename __make_tuple_types<_Tuple>::type _TupleRef;
349 typedef typename tuple_element<0, _TupleRef>::type _U0;
350 typedef typename tuple_element<1, _TupleRef>::type _U1;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000351 first = _VSTD::forward<_U0>(_VSTD::get<0>(__p));
352 second = _VSTD::forward<_U1>(_VSTD::get<1>(__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353 return *this;
354 }
355
Michael J. Spencer626916f2010-12-10 19:47:54 +0000356#endif // _LIBCPP_HAS_NO_VARIADICS
357
Howard Hinnant73d21a42010-09-04 23:28:19 +0000358#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000359 _LIBCPP_INLINE_VISIBILITY
360 void
Howard Hinnanta5e01212011-05-27 19:08:18 +0000361 swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
362 __is_nothrow_swappable<second_type>::value)
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000363 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000364 _VSTD::iter_swap(&first, &__p.first);
365 _VSTD::iter_swap(&second, &__p.second);
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000366 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367private:
Howard Hinnant324bb032010-08-22 00:02:43 +0000368
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369#ifndef _LIBCPP_HAS_NO_VARIADICS
370 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
Howard Hinnant5f5859c2011-01-13 20:05:05 +0000371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372 pair(piecewise_construct_t,
373 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
374 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
Howard Hinnant324bb032010-08-22 00:02:43 +0000375#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376};
377
378template <class _T1, class _T2>
379inline _LIBCPP_INLINE_VISIBILITY
380bool
381operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
382{
383 return __x.first == __y.first && __x.second == __y.second;
384}
385
386template <class _T1, class _T2>
387inline _LIBCPP_INLINE_VISIBILITY
388bool
389operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
390{
391 return !(__x == __y);
392}
393
394template <class _T1, class _T2>
395inline _LIBCPP_INLINE_VISIBILITY
396bool
397operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
398{
399 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
400}
401
402template <class _T1, class _T2>
403inline _LIBCPP_INLINE_VISIBILITY
404bool
405operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
406{
407 return __y < __x;
408}
409
410template <class _T1, class _T2>
411inline _LIBCPP_INLINE_VISIBILITY
412bool
413operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
414{
415 return !(__x < __y);
416}
417
418template <class _T1, class _T2>
419inline _LIBCPP_INLINE_VISIBILITY
420bool
421operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
422{
423 return !(__y < __x);
424}
425
426template <class _T1, class _T2>
427inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaabf2872011-06-01 19:59:32 +0000428typename enable_if
429<
430 __is_swappable<_T1>::value &&
431 __is_swappable<_T2>::value,
432 void
433>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
Howard Hinnanta5e01212011-05-27 19:08:18 +0000435 _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
436 __is_nothrow_swappable<_T2>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437{
Howard Hinnante9b2c2d2011-05-27 15:04:19 +0000438 __x.swap(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439}
440
Howard Hinnant73d21a42010-09-04 23:28:19 +0000441#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442
Howard Hinnant83eade62013-03-06 23:30:19 +0000443template <class _Tp> class _LIBCPP_TYPE_VIS reference_wrapper;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444
445template <class _Tp>
446struct ___make_pair_return
447{
448 typedef _Tp type;
449};
450
451template <class _Tp>
452struct ___make_pair_return<reference_wrapper<_Tp>>
453{
454 typedef _Tp& type;
455};
456
457template <class _Tp>
458struct __make_pair_return
459{
460 typedef typename ___make_pair_return<typename decay<_Tp>::type>::type type;
461};
462
463template <class _T1, class _T2>
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000464inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
466make_pair(_T1&& __t1, _T2&& __t2)
467{
468 return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
Howard Hinnant0949eed2011-06-30 21:18:19 +0000469 (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470}
471
Howard Hinnant73d21a42010-09-04 23:28:19 +0000472#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473
474template <class _T1, class _T2>
475inline _LIBCPP_INLINE_VISIBILITY
476pair<_T1,_T2>
477make_pair(_T1 __x, _T2 __y)
478{
479 return pair<_T1, _T2>(__x, __y);
480}
481
Howard Hinnant73d21a42010-09-04 23:28:19 +0000482#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000484template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000485 class _LIBCPP_TYPE_VIS tuple_size<pair<_T1, _T2> >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000486 : public integral_constant<size_t, 2> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487
488template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000489 class _LIBCPP_TYPE_VIS tuple_size<const pair<_T1, _T2> >
Howard Hinnantee6ccd02010-09-23 18:58:28 +0000490 : public integral_constant<size_t, 2> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491
492template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000493class _LIBCPP_TYPE_VIS tuple_element<0, pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494{
495public:
496 typedef _T1 type;
497};
498
499template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000500class _LIBCPP_TYPE_VIS tuple_element<1, pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501{
502public:
503 typedef _T2 type;
504};
505
506template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000507class _LIBCPP_TYPE_VIS tuple_element<0, const pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508{
509public:
510 typedef const _T1 type;
511};
512
513template <class _T1, class _T2>
Howard Hinnant83eade62013-03-06 23:30:19 +0000514class _LIBCPP_TYPE_VIS tuple_element<1, const pair<_T1, _T2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515{
516public:
517 typedef const _T2 type;
518};
519
520template <size_t _Ip> struct __get_pair;
521
522template <>
523struct __get_pair<0>
524{
525 template <class _T1, class _T2>
526 static
527 _LIBCPP_INLINE_VISIBILITY
528 _T1&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000529 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530
531 template <class _T1, class _T2>
532 static
533 _LIBCPP_INLINE_VISIBILITY
534 const _T1&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000535 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000536
537#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
538
539 template <class _T1, class _T2>
540 static
541 _LIBCPP_INLINE_VISIBILITY
542 _T1&&
Howard Hinnant0949eed2011-06-30 21:18:19 +0000543 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000544
545#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546};
547
548template <>
549struct __get_pair<1>
550{
551 template <class _T1, class _T2>
552 static
553 _LIBCPP_INLINE_VISIBILITY
554 _T2&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000555 get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
557 template <class _T1, class _T2>
558 static
559 _LIBCPP_INLINE_VISIBILITY
560 const _T2&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000561 get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000562
563#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
564
565 template <class _T1, class _T2>
566 static
567 _LIBCPP_INLINE_VISIBILITY
568 _T2&&
Howard Hinnant0949eed2011-06-30 21:18:19 +0000569 get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000570
571#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572};
573
574template <size_t _Ip, class _T1, class _T2>
575_LIBCPP_INLINE_VISIBILITY inline
576typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000577get(pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000578{
579 return __get_pair<_Ip>::get(__p);
580}
581
582template <size_t _Ip, class _T1, class _T2>
583_LIBCPP_INLINE_VISIBILITY inline
584const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000585get(const pair<_T1, _T2>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586{
587 return __get_pair<_Ip>::get(__p);
588}
589
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000590#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
591
592template <size_t _Ip, class _T1, class _T2>
593_LIBCPP_INLINE_VISIBILITY inline
594typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000595get(pair<_T1, _T2>&& __p) _NOEXCEPT
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000596{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000597 return __get_pair<_Ip>::get(_VSTD::move(__p));
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000598}
599
600#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
601
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000602#if _LIBCPP_STD_VER > 11
603
604template<class _Tp, _Tp... _Ip>
Marshall Clow42e55e92013-07-03 19:20:30 +0000605struct _LIBCPP_TYPE_VIS integer_sequence
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000606{
607 typedef _Tp value_type;
608 static_assert( is_integral<_Tp>::value,
609 "std::integer_sequence can only be instantiated with an integral type" );
610 static
611 _LIBCPP_INLINE_VISIBILITY
612 constexpr
613 size_t
614 size() noexcept { return sizeof...(_Ip); }
615};
616
617template<size_t... _Ip>
618 using index_sequence = integer_sequence<size_t, _Ip...>;
619
Marshall Clow42e55e92013-07-03 19:20:30 +0000620namespace __detail {
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000621
Marshall Clow42e55e92013-07-03 19:20:30 +0000622template<typename _Tp, size_t ..._Extra> struct __repeat;
623template<typename _Tp, _Tp ..._Np, size_t ..._Extra> struct __repeat<integer_sequence<_Tp, _Np...>, _Extra...> {
624 typedef integer_sequence<_Tp,
625 _Np...,
626 sizeof...(_Np) + _Np...,
627 2 * sizeof...(_Np) + _Np...,
628 3 * sizeof...(_Np) + _Np...,
629 4 * sizeof...(_Np) + _Np...,
630 5 * sizeof...(_Np) + _Np...,
631 6 * sizeof...(_Np) + _Np...,
632 7 * sizeof...(_Np) + _Np...,
633 _Extra...> type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000634};
635
Marshall Clow42e55e92013-07-03 19:20:30 +0000636template<size_t _Np> struct __parity;
637template<size_t _Np> struct __make : __parity<_Np % 8>::template __pmake<_Np> {};
638
639template<> struct __make<0> { typedef integer_sequence<size_t> type; };
640template<> struct __make<1> { typedef integer_sequence<size_t, 0> type; };
641template<> struct __make<2> { typedef integer_sequence<size_t, 0, 1> type; };
642template<> struct __make<3> { typedef integer_sequence<size_t, 0, 1, 2> type; };
643template<> struct __make<4> { typedef integer_sequence<size_t, 0, 1, 2, 3> type; };
644template<> struct __make<5> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4> type; };
645template<> struct __make<6> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5> type; };
646template<> struct __make<7> { typedef integer_sequence<size_t, 0, 1, 2, 3, 4, 5, 6> type; };
647
648template<> struct __parity<0> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type> {}; };
649template<> struct __parity<1> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 1> {}; };
650template<> struct __parity<2> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 2, _Np - 1> {}; };
651template<> struct __parity<3> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 3, _Np - 2, _Np - 1> {}; };
652template<> struct __parity<4> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
653template<> 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> {}; };
654template<> 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> {}; };
655template<> 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> {}; };
656
657template<typename _Tp, typename _Up> struct __convert {
658 template<typename> struct __result;
659 template<_Tp ..._Np> struct __result<integer_sequence<_Tp, _Np...> > { typedef integer_sequence<_Up, _Np...> type; };
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000660};
Marshall Clow42e55e92013-07-03 19:20:30 +0000661template<typename _Tp> struct __convert<_Tp, _Tp> { template<typename _Up> struct __result { typedef _Up type; }; };
662
663}
664
665template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked =
666 typename __detail::__convert<size_t, _Tp>::template __result<typename __detail::__make<_Np>::type>::type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000667
668template <class _Tp, _Tp _Ep>
669struct __make_integer_sequence
670{
671 static_assert(is_integral<_Tp>::value,
672 "std::make_integer_sequence can only be instantiated with an integral type" );
673 static_assert(0 <= _Ep, "std::make_integer_sequence input shall not be negative");
Marshall Clow42e55e92013-07-03 19:20:30 +0000674 typedef __make_integer_sequence_unchecked<_Tp, _Ep> type;
Marshall Clow7ec46bc2013-07-01 16:26:55 +0000675};
676
677template<class _Tp, _Tp _Np>
678 using make_integer_sequence = typename __make_integer_sequence<_Tp, _Np>::type;
679
680template<size_t _Np>
681 using make_index_sequence = make_integer_sequence<size_t, _Np>;
682
683template<class... _Tp>
684 using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
685
686#endif // _LIBCPP_STD_VER > 11
687
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688_LIBCPP_END_NAMESPACE_STD
689
690#endif // _LIBCPP_UTILITY