utility 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. // -*- C++ -*-
  2. //===-------------------------- utility -----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_UTILITY
  11. #define _LIBCPP_UTILITY
  12. /*
  13. utility synopsis
  14. namespace std
  15. {
  16. template <class T>
  17. void
  18. swap(T& a, T& b);
  19. namespace rel_ops
  20. {
  21. template<class T> bool operator!=(const T&, const T&);
  22. template<class T> bool operator> (const T&, const T&);
  23. template<class T> bool operator<=(const T&, const T&);
  24. template<class T> bool operator>=(const T&, const T&);
  25. }
  26. template<class T>
  27. void
  28. swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
  29. is_nothrow_move_assignable<T>::value);
  30. template <class T, size_t N>
  31. void
  32. swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
  33. template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept; // constexpr in C++14
  34. template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
  35. template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14
  36. template <class T>
  37. typename conditional
  38. <
  39. !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
  40. const T&,
  41. T&&
  42. >::type
  43. move_if_noexcept(T& x) noexcept; // constexpr in C++14
  44. template <class T> constexpr add_const<T>_t& as_const(T& t) noexcept; // C++17
  45. template <class T> void as_const(const T&&) = delete; // C++17
  46. template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
  47. template <class T1, class T2>
  48. struct pair
  49. {
  50. typedef T1 first_type;
  51. typedef T2 second_type;
  52. T1 first;
  53. T2 second;
  54. pair(const pair&) = default;
  55. pair(pair&&) = default;
  56. constexpr pair();
  57. pair(const T1& x, const T2& y); // constexpr in C++14
  58. template <class U, class V> pair(U&& x, V&& y); // constexpr in C++14
  59. template <class U, class V> pair(const pair<U, V>& p); // constexpr in C++14
  60. template <class U, class V> pair(pair<U, V>&& p); // constexpr in C++14
  61. template <class... Args1, class... Args2>
  62. pair(piecewise_construct_t, tuple<Args1...> first_args,
  63. tuple<Args2...> second_args);
  64. template <class U, class V> pair& operator=(const pair<U, V>& p);
  65. pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
  66. is_nothrow_move_assignable<T2>::value);
  67. template <class U, class V> pair& operator=(pair<U, V>&& p);
  68. void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
  69. is_nothrow_swappable_v<T2>);
  70. };
  71. template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
  72. template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
  73. template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
  74. template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
  75. template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
  76. template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
  77. template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
  78. template <class T1, class T2>
  79. void
  80. swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
  81. struct piecewise_construct_t { };
  82. constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
  83. template <class T> class tuple_size;
  84. template <size_t I, class T> class tuple_element;
  85. template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
  86. template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
  87. template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
  88. template<size_t I, class T1, class T2>
  89. typename tuple_element<I, pair<T1, T2> >::type&
  90. get(pair<T1, T2>&) noexcept; // constexpr in C++14
  91. template<size_t I, class T1, class T2>
  92. const typename tuple_element<I, pair<T1, T2> >::type&
  93. get(const pair<T1, T2>&) noexcept; // constexpr in C++14
  94. template<size_t I, class T1, class T2>
  95. typename tuple_element<I, pair<T1, T2> >::type&&
  96. get(pair<T1, T2>&&) noexcept; // constexpr in C++14
  97. template<size_t I, class T1, class T2>
  98. const typename tuple_element<I, pair<T1, T2> >::type&&
  99. get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
  100. template<class T1, class T2>
  101. constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
  102. template<class T1, class T2>
  103. constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
  104. template<class T1, class T2>
  105. constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
  106. template<class T1, class T2>
  107. constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
  108. template<class T1, class T2>
  109. constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
  110. template<class T1, class T2>
  111. constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
  112. template<class T1, class T2>
  113. constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
  114. template<class T1, class T2>
  115. constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
  116. // C++14
  117. template<class T, T... I>
  118. struct integer_sequence
  119. {
  120. typedef T value_type;
  121. static constexpr size_t size() noexcept;
  122. };
  123. template<size_t... I>
  124. using index_sequence = integer_sequence<size_t, I...>;
  125. template<class T, T N>
  126. using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
  127. template<size_t N>
  128. using make_index_sequence = make_integer_sequence<size_t, N>;
  129. template<class... T>
  130. using index_sequence_for = make_index_sequence<sizeof...(T)>;
  131. template<class T, class U=T>
  132. T exchange(T& obj, U&& new_value);
  133. } // std
  134. */
  135. #include <__config>
  136. #include <__tuple>
  137. #include <type_traits>
  138. #include <initializer_list>
  139. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  140. #pragma GCC system_header
  141. #endif
  142. _LIBCPP_BEGIN_NAMESPACE_STD
  143. namespace rel_ops
  144. {
  145. template<class _Tp>
  146. inline _LIBCPP_INLINE_VISIBILITY
  147. bool
  148. operator!=(const _Tp& __x, const _Tp& __y)
  149. {
  150. return !(__x == __y);
  151. }
  152. template<class _Tp>
  153. inline _LIBCPP_INLINE_VISIBILITY
  154. bool
  155. operator> (const _Tp& __x, const _Tp& __y)
  156. {
  157. return __y < __x;
  158. }
  159. template<class _Tp>
  160. inline _LIBCPP_INLINE_VISIBILITY
  161. bool
  162. operator<=(const _Tp& __x, const _Tp& __y)
  163. {
  164. return !(__y < __x);
  165. }
  166. template<class _Tp>
  167. inline _LIBCPP_INLINE_VISIBILITY
  168. bool
  169. operator>=(const _Tp& __x, const _Tp& __y)
  170. {
  171. return !(__x < __y);
  172. }
  173. } // rel_ops
  174. // swap_ranges
  175. template <class _ForwardIterator1, class _ForwardIterator2>
  176. inline _LIBCPP_INLINE_VISIBILITY
  177. _ForwardIterator2
  178. swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2)
  179. {
  180. for(; __first1 != __last1; ++__first1, (void) ++__first2)
  181. swap(*__first1, *__first2);
  182. return __first2;
  183. }
  184. // forward declared in <type_traits>
  185. template<class _Tp, size_t _Np>
  186. inline _LIBCPP_INLINE_VISIBILITY
  187. typename enable_if<
  188. __is_swappable<_Tp>::value
  189. >::type
  190. swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
  191. {
  192. _VSTD::swap_ranges(__a, __a + _Np, __b);
  193. }
  194. template <class _Tp>
  195. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  196. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  197. typename conditional
  198. <
  199. !is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value,
  200. const _Tp&,
  201. _Tp&&
  202. >::type
  203. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  204. const _Tp&
  205. #endif
  206. move_if_noexcept(_Tp& __x) _NOEXCEPT
  207. {
  208. return _VSTD::move(__x);
  209. }
  210. #if _LIBCPP_STD_VER > 14
  211. template <class _Tp> constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
  212. template <class _Tp> void as_const(const _Tp&&) = delete;
  213. #endif
  214. struct _LIBCPP_TYPE_VIS_ONLY piecewise_construct_t { };
  215. #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_UTILITY)
  216. extern const piecewise_construct_t piecewise_construct;// = piecewise_construct_t();
  217. #else
  218. constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
  219. #endif
  220. template <class _T1, class _T2>
  221. struct _LIBCPP_TYPE_VIS_ONLY pair
  222. {
  223. typedef _T1 first_type;
  224. typedef _T2 second_type;
  225. _T1 first;
  226. _T2 second;
  227. #ifndef _LIBCPP_HAS_NO_DEFAULT_FUNCTION_TEMPLATE_ARGS
  228. template <bool _Dummy = true, class = typename enable_if<
  229. __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
  230. __dependent_type<is_default_constructible<_T2>, _Dummy>::value
  231. >::type>
  232. #endif
  233. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR pair() : first(), second() {}
  234. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  235. pair(const _T1& __x, const _T2& __y)
  236. : first(__x), second(__y) {}
  237. template<class _U1, class _U2>
  238. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  239. pair(const pair<_U1, _U2>& __p
  240. #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
  241. ,typename enable_if<is_convertible<const _U1&, _T1>::value &&
  242. is_convertible<const _U2&, _T2>::value>::type* = 0
  243. #endif
  244. )
  245. : first(__p.first), second(__p.second) {}
  246. #if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
  247. _LIBCPP_INLINE_VISIBILITY
  248. pair(const pair& __p)
  249. _NOEXCEPT_(is_nothrow_copy_constructible<first_type>::value &&
  250. is_nothrow_copy_constructible<second_type>::value)
  251. : first(__p.first),
  252. second(__p.second)
  253. {
  254. }
  255. # ifndef _LIBCPP_CXX03_LANG
  256. _LIBCPP_INLINE_VISIBILITY
  257. pair(pair&& __p) _NOEXCEPT_(is_nothrow_move_constructible<first_type>::value &&
  258. is_nothrow_move_constructible<second_type>::value)
  259. : first(_VSTD::forward<first_type>(__p.first)),
  260. second(_VSTD::forward<second_type>(__p.second))
  261. {
  262. }
  263. # endif
  264. #elif !defined(_LIBCPP_CXX03_LANG)
  265. pair(pair const&) = default;
  266. pair(pair&&) = default;
  267. #else
  268. // Use the implicitly declared copy constructor in C++03
  269. #endif
  270. _LIBCPP_INLINE_VISIBILITY
  271. pair& operator=(const pair& __p)
  272. _NOEXCEPT_(is_nothrow_copy_assignable<first_type>::value &&
  273. is_nothrow_copy_assignable<second_type>::value)
  274. {
  275. first = __p.first;
  276. second = __p.second;
  277. return *this;
  278. }
  279. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  280. template <class _U1, class _U2,
  281. class = typename enable_if<is_convertible<_U1, first_type>::value &&
  282. is_convertible<_U2, second_type>::value>::type>
  283. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  284. pair(_U1&& __u1, _U2&& __u2)
  285. : first(_VSTD::forward<_U1>(__u1)),
  286. second(_VSTD::forward<_U2>(__u2))
  287. {}
  288. template<class _U1, class _U2>
  289. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  290. pair(pair<_U1, _U2>&& __p,
  291. typename enable_if<is_convertible<_U1, _T1>::value &&
  292. is_convertible<_U2, _T2>::value>::type* = 0)
  293. : first(_VSTD::forward<_U1>(__p.first)),
  294. second(_VSTD::forward<_U2>(__p.second)) {}
  295. _LIBCPP_INLINE_VISIBILITY
  296. pair&
  297. operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&
  298. is_nothrow_move_assignable<second_type>::value)
  299. {
  300. first = _VSTD::forward<first_type>(__p.first);
  301. second = _VSTD::forward<second_type>(__p.second);
  302. return *this;
  303. }
  304. #ifndef _LIBCPP_HAS_NO_VARIADICS
  305. template<class _Tuple,
  306. class = typename enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
  307. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  308. pair(_Tuple&& __p)
  309. : first(_VSTD::forward<typename tuple_element<0,
  310. typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<0>(__p))),
  311. second(_VSTD::forward<typename tuple_element<1,
  312. typename __make_tuple_types<_Tuple>::type>::type>(_VSTD::get<1>(__p)))
  313. {}
  314. template <class... _Args1, class... _Args2>
  315. _LIBCPP_INLINE_VISIBILITY
  316. pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
  317. tuple<_Args2...> __second_args)
  318. : pair(__pc, __first_args, __second_args,
  319. typename __make_tuple_indices<sizeof...(_Args1)>::type(),
  320. typename __make_tuple_indices<sizeof...(_Args2) >::type())
  321. {}
  322. template <class _Tuple,
  323. class = typename enable_if<__tuple_assignable<_Tuple, pair>::value>::type>
  324. _LIBCPP_INLINE_VISIBILITY
  325. pair&
  326. operator=(_Tuple&& __p)
  327. {
  328. typedef typename __make_tuple_types<_Tuple>::type _TupleRef;
  329. typedef typename tuple_element<0, _TupleRef>::type _U0;
  330. typedef typename tuple_element<1, _TupleRef>::type _U1;
  331. first = _VSTD::forward<_U0>(_VSTD::get<0>(__p));
  332. second = _VSTD::forward<_U1>(_VSTD::get<1>(__p));
  333. return *this;
  334. }
  335. #endif // _LIBCPP_HAS_NO_VARIADICS
  336. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  337. _LIBCPP_INLINE_VISIBILITY
  338. void
  339. swap(pair& __p) _NOEXCEPT_(__is_nothrow_swappable<first_type>::value &&
  340. __is_nothrow_swappable<second_type>::value)
  341. {
  342. using _VSTD::swap;
  343. swap(first, __p.first);
  344. swap(second, __p.second);
  345. }
  346. private:
  347. #ifndef _LIBCPP_HAS_NO_VARIADICS
  348. template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
  349. _LIBCPP_INLINE_VISIBILITY
  350. pair(piecewise_construct_t,
  351. tuple<_Args1...>& __first_args, tuple<_Args2...>& __second_args,
  352. __tuple_indices<_I1...>, __tuple_indices<_I2...>);
  353. #endif // _LIBCPP_HAS_NO_VARIADICS
  354. };
  355. template <class _T1, class _T2>
  356. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  357. bool
  358. operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  359. {
  360. return __x.first == __y.first && __x.second == __y.second;
  361. }
  362. template <class _T1, class _T2>
  363. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  364. bool
  365. operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  366. {
  367. return !(__x == __y);
  368. }
  369. template <class _T1, class _T2>
  370. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  371. bool
  372. operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  373. {
  374. return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
  375. }
  376. template <class _T1, class _T2>
  377. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  378. bool
  379. operator> (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  380. {
  381. return __y < __x;
  382. }
  383. template <class _T1, class _T2>
  384. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  385. bool
  386. operator>=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  387. {
  388. return !(__x < __y);
  389. }
  390. template <class _T1, class _T2>
  391. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  392. bool
  393. operator<=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
  394. {
  395. return !(__y < __x);
  396. }
  397. template <class _T1, class _T2>
  398. inline _LIBCPP_INLINE_VISIBILITY
  399. typename enable_if
  400. <
  401. __is_swappable<_T1>::value &&
  402. __is_swappable<_T2>::value,
  403. void
  404. >::type
  405. swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
  406. _NOEXCEPT_((__is_nothrow_swappable<_T1>::value &&
  407. __is_nothrow_swappable<_T2>::value))
  408. {
  409. __x.swap(__y);
  410. }
  411. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  412. template <class _Tp>
  413. struct __make_pair_return_impl
  414. {
  415. typedef _Tp type;
  416. };
  417. template <class _Tp>
  418. struct __make_pair_return_impl<reference_wrapper<_Tp>>
  419. {
  420. typedef _Tp& type;
  421. };
  422. template <class _Tp>
  423. struct __make_pair_return
  424. {
  425. typedef typename __make_pair_return_impl<typename decay<_Tp>::type>::type type;
  426. };
  427. template <class _T1, class _T2>
  428. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  429. pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
  430. make_pair(_T1&& __t1, _T2&& __t2)
  431. {
  432. return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
  433. (_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
  434. }
  435. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  436. template <class _T1, class _T2>
  437. inline _LIBCPP_INLINE_VISIBILITY
  438. pair<_T1,_T2>
  439. make_pair(_T1 __x, _T2 __y)
  440. {
  441. return pair<_T1, _T2>(__x, __y);
  442. }
  443. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  444. template <class _T1, class _T2>
  445. class _LIBCPP_TYPE_VIS_ONLY tuple_size<pair<_T1, _T2> >
  446. : public integral_constant<size_t, 2> {};
  447. template <class _T1, class _T2>
  448. class _LIBCPP_TYPE_VIS_ONLY tuple_element<0, pair<_T1, _T2> >
  449. {
  450. public:
  451. typedef _T1 type;
  452. };
  453. template <class _T1, class _T2>
  454. class _LIBCPP_TYPE_VIS_ONLY tuple_element<1, pair<_T1, _T2> >
  455. {
  456. public:
  457. typedef _T2 type;
  458. };
  459. template <size_t _Ip> struct __get_pair;
  460. template <>
  461. struct __get_pair<0>
  462. {
  463. template <class _T1, class _T2>
  464. static
  465. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  466. _T1&
  467. get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
  468. template <class _T1, class _T2>
  469. static
  470. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  471. const _T1&
  472. get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.first;}
  473. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  474. template <class _T1, class _T2>
  475. static
  476. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  477. _T1&&
  478. get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T1>(__p.first);}
  479. template <class _T1, class _T2>
  480. static
  481. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  482. const _T1&&
  483. get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T1>(__p.first);}
  484. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  485. };
  486. template <>
  487. struct __get_pair<1>
  488. {
  489. template <class _T1, class _T2>
  490. static
  491. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  492. _T2&
  493. get(pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
  494. template <class _T1, class _T2>
  495. static
  496. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  497. const _T2&
  498. get(const pair<_T1, _T2>& __p) _NOEXCEPT {return __p.second;}
  499. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  500. template <class _T1, class _T2>
  501. static
  502. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  503. _T2&&
  504. get(pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<_T2>(__p.second);}
  505. template <class _T1, class _T2>
  506. static
  507. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  508. const _T2&&
  509. get(const pair<_T1, _T2>&& __p) _NOEXCEPT {return _VSTD::forward<const _T2>(__p.second);}
  510. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  511. };
  512. template <size_t _Ip, class _T1, class _T2>
  513. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  514. typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  515. get(pair<_T1, _T2>& __p) _NOEXCEPT
  516. {
  517. return __get_pair<_Ip>::get(__p);
  518. }
  519. template <size_t _Ip, class _T1, class _T2>
  520. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  521. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  522. get(const pair<_T1, _T2>& __p) _NOEXCEPT
  523. {
  524. return __get_pair<_Ip>::get(__p);
  525. }
  526. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  527. template <size_t _Ip, class _T1, class _T2>
  528. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  529. typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  530. get(pair<_T1, _T2>&& __p) _NOEXCEPT
  531. {
  532. return __get_pair<_Ip>::get(_VSTD::move(__p));
  533. }
  534. template <size_t _Ip, class _T1, class _T2>
  535. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  536. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  537. get(const pair<_T1, _T2>&& __p) _NOEXCEPT
  538. {
  539. return __get_pair<_Ip>::get(_VSTD::move(__p));
  540. }
  541. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  542. #if _LIBCPP_STD_VER > 11
  543. template <class _T1, class _T2>
  544. inline _LIBCPP_INLINE_VISIBILITY
  545. constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
  546. {
  547. return __get_pair<0>::get(__p);
  548. }
  549. template <class _T1, class _T2>
  550. inline _LIBCPP_INLINE_VISIBILITY
  551. constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
  552. {
  553. return __get_pair<0>::get(__p);
  554. }
  555. template <class _T1, class _T2>
  556. inline _LIBCPP_INLINE_VISIBILITY
  557. constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
  558. {
  559. return __get_pair<0>::get(_VSTD::move(__p));
  560. }
  561. template <class _T1, class _T2>
  562. inline _LIBCPP_INLINE_VISIBILITY
  563. constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
  564. {
  565. return __get_pair<0>::get(_VSTD::move(__p));
  566. }
  567. template <class _T1, class _T2>
  568. inline _LIBCPP_INLINE_VISIBILITY
  569. constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
  570. {
  571. return __get_pair<1>::get(__p);
  572. }
  573. template <class _T1, class _T2>
  574. inline _LIBCPP_INLINE_VISIBILITY
  575. constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
  576. {
  577. return __get_pair<1>::get(__p);
  578. }
  579. template <class _T1, class _T2>
  580. inline _LIBCPP_INLINE_VISIBILITY
  581. constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
  582. {
  583. return __get_pair<1>::get(_VSTD::move(__p));
  584. }
  585. template <class _T1, class _T2>
  586. inline _LIBCPP_INLINE_VISIBILITY
  587. constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
  588. {
  589. return __get_pair<1>::get(_VSTD::move(__p));
  590. }
  591. #endif
  592. #if _LIBCPP_STD_VER > 11
  593. template<class _Tp, _Tp... _Ip>
  594. struct _LIBCPP_TYPE_VIS_ONLY integer_sequence
  595. {
  596. typedef _Tp value_type;
  597. static_assert( is_integral<_Tp>::value,
  598. "std::integer_sequence can only be instantiated with an integral type" );
  599. static
  600. _LIBCPP_INLINE_VISIBILITY
  601. constexpr
  602. size_t
  603. size() noexcept { return sizeof...(_Ip); }
  604. };
  605. template<size_t... _Ip>
  606. using index_sequence = integer_sequence<size_t, _Ip...>;
  607. #if __has_builtin(__make_integer_seq) && !defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
  608. template <class _Tp, _Tp _Ep>
  609. using __make_integer_sequence = __make_integer_seq<integer_sequence, _Tp, _Ep>;
  610. #else
  611. template<typename _Tp, _Tp _Np> using __make_integer_sequence_unchecked =
  612. typename __detail::__make<_Np>::type::template __convert<integer_sequence, _Tp>;
  613. template <class _Tp, _Tp _Ep>
  614. struct __make_integer_sequence_checked
  615. {
  616. static_assert(is_integral<_Tp>::value,
  617. "std::make_integer_sequence can only be instantiated with an integral type" );
  618. static_assert(0 <= _Ep, "std::make_integer_sequence must have a non-negative sequence length");
  619. // Workaround GCC bug by preventing bad installations when 0 <= _Ep
  620. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68929
  621. typedef __make_integer_sequence_unchecked<_Tp, 0 <= _Ep ? _Ep : 0> type;
  622. };
  623. template <class _Tp, _Tp _Ep>
  624. using __make_integer_sequence = typename __make_integer_sequence_checked<_Tp, _Ep>::type;
  625. #endif
  626. template<class _Tp, _Tp _Np>
  627. using make_integer_sequence = __make_integer_sequence<_Tp, _Np>;
  628. template<size_t _Np>
  629. using make_index_sequence = make_integer_sequence<size_t, _Np>;
  630. template<class... _Tp>
  631. using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;
  632. #endif // _LIBCPP_STD_VER > 11
  633. #if _LIBCPP_STD_VER > 11
  634. template<class _T1, class _T2 = _T1>
  635. inline _LIBCPP_INLINE_VISIBILITY
  636. _T1 exchange(_T1& __obj, _T2 && __new_value)
  637. {
  638. _T1 __old_value = _VSTD::move(__obj);
  639. __obj = _VSTD::forward<_T2>(__new_value);
  640. return __old_value;
  641. }
  642. #endif // _LIBCPP_STD_VER > 11
  643. _LIBCPP_END_NAMESPACE_STD
  644. #endif // _LIBCPP_UTILITY