blob: ccf20a773b0f8e0e09cd31c99144cc4e8cf13cce [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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
32template<class T> void swap(T& a, T& b);
33template <class T, size_t N> void swap(T (&a)[N], T (&b)[N]);
34
35template <class T, class U> T&& forward(U&&);
36template <class T> typename remove_reference<T>::type&& move(T&&);
37
38template <class T>
39 typename conditional
40 <
41 !has_nothrow_move_constructor<T>::value && has_copy_constructor<T>::value,
42 const T&,
43 T&&
44 >::type
45 move_if_noexcept(T& x);
46
47template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
48
49template <class T1, class T2>
50struct pair
51{
52 typedef T1 first_type;
53 typedef T2 second_type;
54
55 T1 first;
56 T2 second;
57
58 pair(const pair&) = default;
59 constexpr pair();
60 pair(const T1& x, const T2& y);
61 template <class U, class V> pair(U&& x, V&& y);
62 template <class U, class V> pair(const pair<U, V>& p);
63 template <class U, class V> pair(pair<U, V>&& p);
64 template <class... Args1, class... Args2>
65 pair(piecewise_construct_t, tuple<Args1...> first_args,
66 tuple<Args2...> second_args);
67
68 template <class U, class V> pair& operator=(const pair<U, V>& p);
69 pair& operator=(pair&& p);
70 template <class U, class V> pair& operator=(pair<U, V>&& p);
71
72 void swap(pair& p);
73};
74
75template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&);
76template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&);
77template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
78template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
79template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&);
80template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&);
81
82template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);
83template <class T1, class T2> void swap(pair<T1, T2>& x, pair<T1, T2>& y);
84
85struct piecewise_construct_t { };
86constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
87
88template <class T> class tuple_size;
89template <size_t I, class T> class tuple_element;
90
91template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >;
92template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >;
93template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
94
95template<size_t I, class T1, class T2>
96 typename tuple_element<I, std::pair<T1, T2> >::type&
97 get(std::pair<T1, T2>&);
98
99template<size_t I, class T1, class T2>
100 const typename const tuple_element<I, std::pair<T1, T2> >::type&
101 get(const std::pair<T1, T2>&);
102
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103} // std
104
105*/
106
107#include <__config>
108#include <__tuple>
109#include <type_traits>
110
111#pragma GCC system_header
112
113_LIBCPP_BEGIN_NAMESPACE_STD
114
115namespace rel_ops
116{
117
118template<class _Tp>
119inline _LIBCPP_INLINE_VISIBILITY
120bool
121operator!=(const _Tp& __x, const _Tp& __y)
122{
123 return !(__x == __y);
124}
125
126template<class _Tp>
127inline _LIBCPP_INLINE_VISIBILITY
128bool
129operator> (const _Tp& __x, const _Tp& __y)
130{
131 return __y < __x;
132}
133
134template<class _Tp>
135inline _LIBCPP_INLINE_VISIBILITY
136bool
137operator<=(const _Tp& __x, const _Tp& __y)
138{
139 return !(__y < __x);
140}
141
142template<class _Tp>
143inline _LIBCPP_INLINE_VISIBILITY
144bool
145operator>=(const _Tp& __x, const _Tp& __y)
146{
147 return !(__x < __y);
148}
149
150} // rel_ops
151
152// swap_ranges
153
154template <class _ForwardIterator1, class _ForwardIterator2>
155inline _LIBCPP_INLINE_VISIBILITY
156_ForwardIterator2
157swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
158{
159 for(; __first1 != __last1; ++__first1, ++__first2)
160 swap(*__first1, *__first2);
161 return __first2;
162}
163
164template<class _Tp, size_t _N>
165inline _LIBCPP_INLINE_VISIBILITY
166void
167swap(_Tp (&__a)[_N], _Tp (&__b)[_N])
168{
169 _STD::swap_ranges(__a, __a + _N, __b);
170}
171
172template <class _Tp>
173inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant73d21a42010-09-04 23:28:19 +0000174#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175typename conditional
176<
177 !has_nothrow_move_constructor<_Tp>::value && has_copy_constructor<_Tp>::value,
178 const _Tp&,
179 _Tp&&
180>::type
Howard Hinnant73d21a42010-09-04 23:28:19 +0000181#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182const _Tp&
183#endif
184move_if_noexcept(_Tp& __x)
185{
186 return _STD::move(__x);
187}
188
189struct piecewise_construct_t { };
190//constexpr
191extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
192
193template <class _T1, class _T2> struct pair;
194template <class _T1, class _T2> void swap(pair<_T1, _T2>&, pair<_T1, _T2>&);
195
196template <class _T1, class _T2>
197struct pair
198{
199 typedef _T1 first_type;
200 typedef _T2 second_type;
201
202 _T1 first;
203 _T2 second;
204
205 _LIBCPP_INLINE_VISIBILITY pair() : first(), second() {}
206
207 _LIBCPP_INLINE_VISIBILITY pair(const _T1& __x, const _T2& __y)
208 : first(__x), second(__y) {}
209
Howard Hinnant73d21a42010-09-04 23:28:19 +0000210#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211
212 template <class _U1, class _U2,
213 class = typename enable_if<is_convertible<_U1, first_type >::value &&
214 is_convertible<_U2, second_type>::value>::type>
215 _LIBCPP_INLINE_VISIBILITY
216 pair(_U1&& __u1, _U2&& __u2)
217 : first(_STD::forward<_U1>(__u1)),
218 second(_STD::forward<_U2>(__u2))
219 {}
220
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000221#ifndef _LIBCPP_HAS_NO_VARIADICS
222
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223 template<class _Tuple,
224 class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
225 _LIBCPP_INLINE_VISIBILITY
226 pair(_Tuple&& __p)
227 : first(_STD::forward<typename tuple_element<0,
228 typename __make_tuple_types<_Tuple>::type>::type>(get<0>(__p))),
229 second(_STD::forward<typename tuple_element<1,
230 typename __make_tuple_types<_Tuple>::type>::type>(get<1>(__p)))
231 {}
232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
234 pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
235 tuple<_Args2...> __second_args)
Howard Hinnant324bb032010-08-22 00:02:43 +0000236 : pair(__pc, __first_args, __second_args,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
238 typename __make_tuple_indices<sizeof...(_Args2) >::type())
239 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240
241 template <class _Tuple,
242 class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type>
243 pair&
244 operator=(_Tuple&& __p)
245 {
246 typedef typename __make_tuple_types<_Tuple>::type _TupleRef;
247 typedef typename tuple_element<0, _TupleRef>::type _U0;
248 typedef typename tuple_element<1, _TupleRef>::type _U1;
249 first = _STD::forward<_U0>(_STD::get<0>(__p));
250 second = _STD::forward<_U1>(_STD::get<1>(__p));
251 return *this;
252 }
253
Howard Hinnant324bb032010-08-22 00:02:43 +0000254#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant60a0a8e2010-08-10 20:48:29 +0000255
Howard Hinnant73d21a42010-09-04 23:28:19 +0000256#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257 template<class _U1, class _U2>
258 _LIBCPP_INLINE_VISIBILITY pair(const pair<_U1, _U2>& __p)
259 : first(__p.first), second(__p.second) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000260#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261 void _LIBCPP_INLINE_VISIBILITY swap(pair& __p) {_STD::swap(*this, __p);}
262private:
Howard Hinnant324bb032010-08-22 00:02:43 +0000263
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264#ifndef _LIBCPP_HAS_NO_VARIADICS
265 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
266 pair(piecewise_construct_t,
267 tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
268 __tuple_indices<_I1...>, __tuple_indices<_I2...>);
Howard Hinnant324bb032010-08-22 00:02:43 +0000269#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270};
271
272template <class _T1, class _T2>
273inline _LIBCPP_INLINE_VISIBILITY
274bool
275operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
276{
277 return __x.first == __y.first && __x.second == __y.second;
278}
279
280template <class _T1, class _T2>
281inline _LIBCPP_INLINE_VISIBILITY
282bool
283operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
284{
285 return !(__x == __y);
286}
287
288template <class _T1, class _T2>
289inline _LIBCPP_INLINE_VISIBILITY
290bool
291operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
292{
293 return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
294}
295
296template <class _T1, class _T2>
297inline _LIBCPP_INLINE_VISIBILITY
298bool
299operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
300{
301 return __y < __x;
302}
303
304template <class _T1, class _T2>
305inline _LIBCPP_INLINE_VISIBILITY
306bool
307operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
308{
309 return !(__x < __y);
310}
311
312template <class _T1, class _T2>
313inline _LIBCPP_INLINE_VISIBILITY
314bool
315operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
316{
317 return !(__y < __x);
318}
319
320template <class _T1, class _T2>
321inline _LIBCPP_INLINE_VISIBILITY
322void
323swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
324{
325 swap(__x.first, __y.first);
326 swap(__x.second, __y.second);
327}
328
Howard Hinnant73d21a42010-09-04 23:28:19 +0000329#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330
331template <class _Tp> class reference_wrapper;
332
333template <class _Tp>
334struct ___make_pair_return
335{
336 typedef _Tp type;
337};
338
339template <class _Tp>
340struct ___make_pair_return<reference_wrapper<_Tp>>
341{
342 typedef _Tp& type;
343};
344
345template <class _Tp>
346struct __make_pair_return
347{
348 typedef typename ___make_pair_return<typename decay<_Tp>::type>::type type;
349};
350
351template <class _T1, class _T2>
352inline
353pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
354make_pair(_T1&& __t1, _T2&& __t2)
355{
356 return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
357 (_STD::forward<_T1>(__t1), _STD::forward<_T2>(__t2));
358}
359
Howard Hinnant73d21a42010-09-04 23:28:19 +0000360#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template <class _T1, class _T2>
363inline _LIBCPP_INLINE_VISIBILITY
364pair<_T1,_T2>
365make_pair(_T1 __x, _T2 __y)
366{
367 return pair<_T1, _T2>(__x, __y);
368}
369
Howard Hinnant73d21a42010-09-04 23:28:19 +0000370#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372#ifndef _LIBCPP_HAS_NO_VARIADICS
373
374template <class _T1, class _T2>
375 class tuple_size<pair<_T1, _T2> > : public integral_constant<size_t, 2> {};
376
377template <class _T1, class _T2>
378 class tuple_size<const pair<_T1, _T2> > : public integral_constant<size_t, 2> {};
379
380template <class _T1, class _T2>
381class tuple_element<0, pair<_T1, _T2> >
382{
383public:
384 typedef _T1 type;
385};
386
387template <class _T1, class _T2>
388class tuple_element<1, pair<_T1, _T2> >
389{
390public:
391 typedef _T2 type;
392};
393
394template <class _T1, class _T2>
395class tuple_element<0, const pair<_T1, _T2> >
396{
397public:
398 typedef const _T1 type;
399};
400
401template <class _T1, class _T2>
402class tuple_element<1, const pair<_T1, _T2> >
403{
404public:
405 typedef const _T2 type;
406};
407
408template <size_t _Ip> struct __get_pair;
409
410template <>
411struct __get_pair<0>
412{
413 template <class _T1, class _T2>
414 static
415 _LIBCPP_INLINE_VISIBILITY
416 _T1&
417 get(pair<_T1, _T2>& __p) {return __p.first;}
418
419 template <class _T1, class _T2>
420 static
421 _LIBCPP_INLINE_VISIBILITY
422 const _T1&
423 get(const pair<_T1, _T2>& __p) {return __p.first;}
424};
425
426template <>
427struct __get_pair<1>
428{
429 template <class _T1, class _T2>
430 static
431 _LIBCPP_INLINE_VISIBILITY
432 _T2&
433 get(pair<_T1, _T2>& __p) {return __p.second;}
434
435 template <class _T1, class _T2>
436 static
437 _LIBCPP_INLINE_VISIBILITY
438 const _T2&
439 get(const pair<_T1, _T2>& __p) {return __p.second;}
440};
441
442template <size_t _Ip, class _T1, class _T2>
443_LIBCPP_INLINE_VISIBILITY inline
444typename tuple_element<_Ip, pair<_T1, _T2> >::type&
445get(pair<_T1, _T2>& __p)
446{
447 return __get_pair<_Ip>::get(__p);
448}
449
450template <size_t _Ip, class _T1, class _T2>
451_LIBCPP_INLINE_VISIBILITY inline
452const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
453get(const pair<_T1, _T2>& __p)
454{
455 return __get_pair<_Ip>::get(__p);
456}
457
Howard Hinnant324bb032010-08-22 00:02:43 +0000458#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460_LIBCPP_END_NAMESPACE_STD
461
462#endif // _LIBCPP_UTILITY