utility 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // <utility> -*- C++ -*-
  2. // Copyright (C) 2001-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file include/utility
  46. * This is a Standard C++ Library header.
  47. */
  48. #ifndef _GLIBCXX_UTILITY
  49. #define _GLIBCXX_UTILITY 1
  50. #pragma GCC system_header
  51. /**
  52. * @defgroup utilities Utilities
  53. *
  54. * Components deemed generally useful. Includes pair, tuple,
  55. * forward/move helpers, ratio, function object, metaprogramming and
  56. * type traits, time, date, and memory functions.
  57. */
  58. #include <bits/c++config.h>
  59. #include <bits/stl_relops.h>
  60. #include <bits/stl_pair.h>
  61. #if __cplusplus >= 201103L
  62. #include <type_traits>
  63. #include <bits/move.h>
  64. #include <initializer_list>
  65. namespace std _GLIBCXX_VISIBILITY(default)
  66. {
  67. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  68. /// Finds the size of a given tuple type.
  69. template<typename _Tp>
  70. struct tuple_size;
  71. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  72. // 2313. tuple_size should always derive from integral_constant<size_t, N>
  73. // 2770. tuple_size<const T> specialization is not SFINAE compatible
  74. template<typename _Tp,
  75. typename _Up = typename remove_cv<_Tp>::type,
  76. typename = typename enable_if<is_same<_Tp, _Up>::value>::type,
  77. size_t = tuple_size<_Tp>::value>
  78. using __enable_if_has_tuple_size = _Tp;
  79. template<typename _Tp>
  80. struct tuple_size<const __enable_if_has_tuple_size<_Tp>>
  81. : public tuple_size<_Tp> { };
  82. template<typename _Tp>
  83. struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>>
  84. : public tuple_size<_Tp> { };
  85. template<typename _Tp>
  86. struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>>
  87. : public tuple_size<_Tp> { };
  88. /// Gives the type of the ith element of a given tuple type.
  89. template<std::size_t __i, typename _Tp>
  90. struct tuple_element;
  91. // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
  92. template<std::size_t __i, typename _Tp>
  93. using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
  94. template<std::size_t __i, typename _Tp>
  95. struct tuple_element<__i, const _Tp>
  96. {
  97. typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
  98. };
  99. template<std::size_t __i, typename _Tp>
  100. struct tuple_element<__i, volatile _Tp>
  101. {
  102. typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
  103. };
  104. template<std::size_t __i, typename _Tp>
  105. struct tuple_element<__i, const volatile _Tp>
  106. {
  107. typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
  108. };
  109. #if __cplusplus >= 201402L
  110. // The standard says this macro and alias template should be in <tuple>
  111. // but we define them here, to be available when the partial specializations
  112. // of tuple_element<pair<T,U>> and tuple_element<array<T,N>> are defined.
  113. #define __cpp_lib_tuple_element_t 201402L
  114. template<std::size_t __i, typename _Tp>
  115. using tuple_element_t = typename tuple_element<__i, _Tp>::type;
  116. #endif
  117. // Various functions which give std::pair a tuple-like interface.
  118. /// Partial specialization for std::pair
  119. template<typename _T1, typename _T2>
  120. struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
  121. { };
  122. /// Partial specialization for std::pair
  123. template<class _Tp1, class _Tp2>
  124. struct tuple_size<std::pair<_Tp1, _Tp2>>
  125. : public integral_constant<std::size_t, 2> { };
  126. /// Partial specialization for std::pair
  127. template<class _Tp1, class _Tp2>
  128. struct tuple_element<0, std::pair<_Tp1, _Tp2>>
  129. { typedef _Tp1 type; };
  130. /// Partial specialization for std::pair
  131. template<class _Tp1, class _Tp2>
  132. struct tuple_element<1, std::pair<_Tp1, _Tp2>>
  133. { typedef _Tp2 type; };
  134. template<std::size_t _Int>
  135. struct __pair_get;
  136. template<>
  137. struct __pair_get<0>
  138. {
  139. template<typename _Tp1, typename _Tp2>
  140. static constexpr _Tp1&
  141. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  142. { return __pair.first; }
  143. template<typename _Tp1, typename _Tp2>
  144. static constexpr _Tp1&&
  145. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  146. { return std::forward<_Tp1>(__pair.first); }
  147. template<typename _Tp1, typename _Tp2>
  148. static constexpr const _Tp1&
  149. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  150. { return __pair.first; }
  151. template<typename _Tp1, typename _Tp2>
  152. static constexpr const _Tp1&&
  153. __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
  154. { return std::forward<const _Tp1>(__pair.first); }
  155. };
  156. template<>
  157. struct __pair_get<1>
  158. {
  159. template<typename _Tp1, typename _Tp2>
  160. static constexpr _Tp2&
  161. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  162. { return __pair.second; }
  163. template<typename _Tp1, typename _Tp2>
  164. static constexpr _Tp2&&
  165. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  166. { return std::forward<_Tp2>(__pair.second); }
  167. template<typename _Tp1, typename _Tp2>
  168. static constexpr const _Tp2&
  169. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  170. { return __pair.second; }
  171. template<typename _Tp1, typename _Tp2>
  172. static constexpr const _Tp2&&
  173. __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
  174. { return std::forward<const _Tp2>(__pair.second); }
  175. };
  176. template<std::size_t _Int, class _Tp1, class _Tp2>
  177. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  178. get(std::pair<_Tp1, _Tp2>& __in) noexcept
  179. { return __pair_get<_Int>::__get(__in); }
  180. template<std::size_t _Int, class _Tp1, class _Tp2>
  181. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  182. get(std::pair<_Tp1, _Tp2>&& __in) noexcept
  183. { return __pair_get<_Int>::__move_get(std::move(__in)); }
  184. template<std::size_t _Int, class _Tp1, class _Tp2>
  185. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  186. get(const std::pair<_Tp1, _Tp2>& __in) noexcept
  187. { return __pair_get<_Int>::__const_get(__in); }
  188. template<std::size_t _Int, class _Tp1, class _Tp2>
  189. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  190. get(const std::pair<_Tp1, _Tp2>&& __in) noexcept
  191. { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
  192. #if __cplusplus > 201103L
  193. #define __cpp_lib_tuples_by_type 201304
  194. template <typename _Tp, typename _Up>
  195. constexpr _Tp&
  196. get(pair<_Tp, _Up>& __p) noexcept
  197. { return __p.first; }
  198. template <typename _Tp, typename _Up>
  199. constexpr const _Tp&
  200. get(const pair<_Tp, _Up>& __p) noexcept
  201. { return __p.first; }
  202. template <typename _Tp, typename _Up>
  203. constexpr _Tp&&
  204. get(pair<_Tp, _Up>&& __p) noexcept
  205. { return std::move(__p.first); }
  206. template <typename _Tp, typename _Up>
  207. constexpr const _Tp&&
  208. get(const pair<_Tp, _Up>&& __p) noexcept
  209. { return std::move(__p.first); }
  210. template <typename _Tp, typename _Up>
  211. constexpr _Tp&
  212. get(pair<_Up, _Tp>& __p) noexcept
  213. { return __p.second; }
  214. template <typename _Tp, typename _Up>
  215. constexpr const _Tp&
  216. get(const pair<_Up, _Tp>& __p) noexcept
  217. { return __p.second; }
  218. template <typename _Tp, typename _Up>
  219. constexpr _Tp&&
  220. get(pair<_Up, _Tp>&& __p) noexcept
  221. { return std::move(__p.second); }
  222. template <typename _Tp, typename _Up>
  223. constexpr const _Tp&&
  224. get(const pair<_Up, _Tp>&& __p) noexcept
  225. { return std::move(__p.second); }
  226. #define __cpp_lib_exchange_function 201304
  227. /// Assign @p __new_val to @p __obj and return its previous value.
  228. template <typename _Tp, typename _Up = _Tp>
  229. inline _Tp
  230. exchange(_Tp& __obj, _Up&& __new_val)
  231. { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
  232. #endif
  233. // Stores a tuple of indices. Used by tuple and pair, and by bind() to
  234. // extract the elements in a tuple.
  235. template<size_t... _Indexes> struct _Index_tuple { };
  236. #ifdef __has_builtin
  237. # if __has_builtin(__make_integer_seq)
  238. # define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1
  239. # endif
  240. #endif
  241. // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
  242. template<size_t _Num>
  243. struct _Build_index_tuple
  244. {
  245. #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
  246. template<typename, size_t... _Indices>
  247. using _IdxTuple = _Index_tuple<_Indices...>;
  248. using __type = __make_integer_seq<_IdxTuple, size_t, _Num>;
  249. #else
  250. using __type = _Index_tuple<__integer_pack(_Num)...>;
  251. #endif
  252. };
  253. #if __cplusplus > 201103L
  254. #define __cpp_lib_integer_sequence 201304
  255. /// Class template integer_sequence
  256. template<typename _Tp, _Tp... _Idx>
  257. struct integer_sequence
  258. {
  259. typedef _Tp value_type;
  260. static constexpr size_t size() noexcept { return sizeof...(_Idx); }
  261. };
  262. /// Alias template make_integer_sequence
  263. template<typename _Tp, _Tp _Num>
  264. using make_integer_sequence
  265. #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
  266. = __make_integer_seq<integer_sequence, _Tp, _Num>;
  267. #else
  268. = integer_sequence<_Tp, __integer_pack(_Num)...>;
  269. #endif
  270. #undef _GLIBCXX_USE_MAKE_INTEGER_SEQ
  271. /// Alias template index_sequence
  272. template<size_t... _Idx>
  273. using index_sequence = integer_sequence<size_t, _Idx...>;
  274. /// Alias template make_index_sequence
  275. template<size_t _Num>
  276. using make_index_sequence = make_integer_sequence<size_t, _Num>;
  277. /// Alias template index_sequence_for
  278. template<typename... _Types>
  279. using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
  280. #endif
  281. #if __cplusplus > 201402L
  282. struct in_place_t {
  283. explicit in_place_t() = default;
  284. };
  285. inline constexpr in_place_t in_place{};
  286. template<typename _Tp> struct in_place_type_t
  287. {
  288. explicit in_place_type_t() = default;
  289. };
  290. template<typename _Tp>
  291. inline constexpr in_place_type_t<_Tp> in_place_type{};
  292. template<size_t _Idx> struct in_place_index_t
  293. {
  294. explicit in_place_index_t() = default;
  295. };
  296. template<size_t _Idx>
  297. inline constexpr in_place_index_t<_Idx> in_place_index{};
  298. template<typename>
  299. struct __is_in_place_type_impl : false_type
  300. { };
  301. template<typename _Tp>
  302. struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
  303. { };
  304. template<typename _Tp>
  305. struct __is_in_place_type
  306. : public __is_in_place_type_impl<_Tp>
  307. { };
  308. #define __cpp_lib_as_const 201510
  309. template<typename _Tp>
  310. constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
  311. template<typename _Tp>
  312. void as_const(const _Tp&&) = delete;
  313. #endif // C++17
  314. _GLIBCXX_END_NAMESPACE_VERSION
  315. } // namespace
  316. #endif
  317. #endif /* _GLIBCXX_UTILITY */