utility 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // <utility> -*- C++ -*-
  2. // Copyright (C) 2001-2018 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 > 201103L
  110. #define __cpp_lib_tuple_element_t 201402
  111. template<std::size_t __i, typename _Tp>
  112. using tuple_element_t = typename tuple_element<__i, _Tp>::type;
  113. #endif
  114. // Various functions which give std::pair a tuple-like interface.
  115. /// Partial specialization for std::pair
  116. template<typename _T1, typename _T2>
  117. struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
  118. { };
  119. /// Partial specialization for std::pair
  120. template<class _Tp1, class _Tp2>
  121. struct tuple_size<std::pair<_Tp1, _Tp2>>
  122. : public integral_constant<std::size_t, 2> { };
  123. /// Partial specialization for std::pair
  124. template<class _Tp1, class _Tp2>
  125. struct tuple_element<0, std::pair<_Tp1, _Tp2>>
  126. { typedef _Tp1 type; };
  127. /// Partial specialization for std::pair
  128. template<class _Tp1, class _Tp2>
  129. struct tuple_element<1, std::pair<_Tp1, _Tp2>>
  130. { typedef _Tp2 type; };
  131. template<std::size_t _Int>
  132. struct __pair_get;
  133. template<>
  134. struct __pair_get<0>
  135. {
  136. template<typename _Tp1, typename _Tp2>
  137. static constexpr _Tp1&
  138. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  139. { return __pair.first; }
  140. template<typename _Tp1, typename _Tp2>
  141. static constexpr _Tp1&&
  142. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  143. { return std::forward<_Tp1>(__pair.first); }
  144. template<typename _Tp1, typename _Tp2>
  145. static constexpr const _Tp1&
  146. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  147. { return __pair.first; }
  148. template<typename _Tp1, typename _Tp2>
  149. static constexpr const _Tp1&&
  150. __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
  151. { return std::forward<const _Tp1>(__pair.first); }
  152. };
  153. template<>
  154. struct __pair_get<1>
  155. {
  156. template<typename _Tp1, typename _Tp2>
  157. static constexpr _Tp2&
  158. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  159. { return __pair.second; }
  160. template<typename _Tp1, typename _Tp2>
  161. static constexpr _Tp2&&
  162. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  163. { return std::forward<_Tp2>(__pair.second); }
  164. template<typename _Tp1, typename _Tp2>
  165. static constexpr const _Tp2&
  166. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  167. { return __pair.second; }
  168. template<typename _Tp1, typename _Tp2>
  169. static constexpr const _Tp2&&
  170. __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
  171. { return std::forward<const _Tp2>(__pair.second); }
  172. };
  173. template<std::size_t _Int, class _Tp1, class _Tp2>
  174. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  175. get(std::pair<_Tp1, _Tp2>& __in) noexcept
  176. { return __pair_get<_Int>::__get(__in); }
  177. template<std::size_t _Int, class _Tp1, class _Tp2>
  178. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  179. get(std::pair<_Tp1, _Tp2>&& __in) noexcept
  180. { return __pair_get<_Int>::__move_get(std::move(__in)); }
  181. template<std::size_t _Int, class _Tp1, class _Tp2>
  182. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  183. get(const std::pair<_Tp1, _Tp2>& __in) noexcept
  184. { return __pair_get<_Int>::__const_get(__in); }
  185. template<std::size_t _Int, class _Tp1, class _Tp2>
  186. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  187. get(const std::pair<_Tp1, _Tp2>&& __in) noexcept
  188. { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
  189. #if __cplusplus > 201103L
  190. #define __cpp_lib_tuples_by_type 201304
  191. template <typename _Tp, typename _Up>
  192. constexpr _Tp&
  193. get(pair<_Tp, _Up>& __p) noexcept
  194. { return __p.first; }
  195. template <typename _Tp, typename _Up>
  196. constexpr const _Tp&
  197. get(const pair<_Tp, _Up>& __p) noexcept
  198. { return __p.first; }
  199. template <typename _Tp, typename _Up>
  200. constexpr _Tp&&
  201. get(pair<_Tp, _Up>&& __p) noexcept
  202. { return std::move(__p.first); }
  203. template <typename _Tp, typename _Up>
  204. constexpr const _Tp&&
  205. get(const pair<_Tp, _Up>&& __p) noexcept
  206. { return std::move(__p.first); }
  207. template <typename _Tp, typename _Up>
  208. constexpr _Tp&
  209. get(pair<_Up, _Tp>& __p) noexcept
  210. { return __p.second; }
  211. template <typename _Tp, typename _Up>
  212. constexpr const _Tp&
  213. get(const pair<_Up, _Tp>& __p) noexcept
  214. { return __p.second; }
  215. template <typename _Tp, typename _Up>
  216. constexpr _Tp&&
  217. get(pair<_Up, _Tp>&& __p) noexcept
  218. { return std::move(__p.second); }
  219. template <typename _Tp, typename _Up>
  220. constexpr const _Tp&&
  221. get(const pair<_Up, _Tp>&& __p) noexcept
  222. { return std::move(__p.second); }
  223. #define __cpp_lib_exchange_function 201304
  224. /// Assign @p __new_val to @p __obj and return its previous value.
  225. template <typename _Tp, typename _Up = _Tp>
  226. inline _Tp
  227. exchange(_Tp& __obj, _Up&& __new_val)
  228. { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
  229. #endif
  230. // Stores a tuple of indices. Used by tuple and pair, and by bind() to
  231. // extract the elements in a tuple.
  232. template<size_t... _Indexes> struct _Index_tuple { };
  233. #ifdef __has_builtin
  234. # if __has_builtin(__make_integer_seq)
  235. # define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1
  236. # endif
  237. #endif
  238. // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
  239. template<size_t _Num>
  240. struct _Build_index_tuple
  241. {
  242. #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
  243. template<typename, size_t... _Indices>
  244. using _IdxTuple = _Index_tuple<_Indices...>;
  245. using __type = __make_integer_seq<_IdxTuple, size_t, _Num>;
  246. #else
  247. using __type = _Index_tuple<__integer_pack(_Num)...>;
  248. #endif
  249. };
  250. #if __cplusplus > 201103L
  251. #define __cpp_lib_integer_sequence 201304
  252. /// Class template integer_sequence
  253. template<typename _Tp, _Tp... _Idx>
  254. struct integer_sequence
  255. {
  256. typedef _Tp value_type;
  257. static constexpr size_t size() noexcept { return sizeof...(_Idx); }
  258. };
  259. /// Alias template make_integer_sequence
  260. template<typename _Tp, _Tp _Num>
  261. using make_integer_sequence
  262. #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
  263. = __make_integer_seq<integer_sequence, _Tp, _Num>;
  264. #else
  265. = integer_sequence<_Tp, __integer_pack(_Num)...>;
  266. #endif
  267. #undef _GLIBCXX_USE_MAKE_INTEGER_SEQ
  268. /// Alias template index_sequence
  269. template<size_t... _Idx>
  270. using index_sequence = integer_sequence<size_t, _Idx...>;
  271. /// Alias template make_index_sequence
  272. template<size_t _Num>
  273. using make_index_sequence = make_integer_sequence<size_t, _Num>;
  274. /// Alias template index_sequence_for
  275. template<typename... _Types>
  276. using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
  277. #endif
  278. #if __cplusplus > 201402L
  279. struct in_place_t {
  280. explicit in_place_t() = default;
  281. };
  282. inline constexpr in_place_t in_place{};
  283. template<typename _Tp> struct in_place_type_t
  284. {
  285. explicit in_place_type_t() = default;
  286. };
  287. template<typename _Tp>
  288. inline constexpr in_place_type_t<_Tp> in_place_type{};
  289. template<size_t _Idx> struct in_place_index_t
  290. {
  291. explicit in_place_index_t() = default;
  292. };
  293. template<size_t _Idx>
  294. inline constexpr in_place_index_t<_Idx> in_place_index{};
  295. template<typename>
  296. struct __is_in_place_type_impl : false_type
  297. { };
  298. template<typename _Tp>
  299. struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
  300. { };
  301. template<typename _Tp>
  302. struct __is_in_place_type
  303. : public __is_in_place_type_impl<_Tp>
  304. { };
  305. #define __cpp_lib_as_const 201510
  306. template<typename _Tp>
  307. constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
  308. template<typename _Tp>
  309. void as_const(const _Tp&&) = delete;
  310. #endif // C++17
  311. _GLIBCXX_END_NAMESPACE_VERSION
  312. } // namespace
  313. #endif
  314. #endif /* _GLIBCXX_UTILITY */