You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

479 lines
14KB

  1. // <utility> -*- C++ -*-
  2. // Copyright (C) 2001-2020 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. #if __cplusplus > 201703L
  66. #include <ext/numeric_traits.h>
  67. #endif
  68. namespace std _GLIBCXX_VISIBILITY(default)
  69. {
  70. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  71. /// Finds the size of a given tuple type.
  72. template<typename _Tp>
  73. struct tuple_size;
  74. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  75. // 2313. tuple_size should always derive from integral_constant<size_t, N>
  76. // 2770. tuple_size<const T> specialization is not SFINAE compatible
  77. template<typename _Tp,
  78. typename _Up = typename remove_cv<_Tp>::type,
  79. typename = typename enable_if<is_same<_Tp, _Up>::value>::type,
  80. size_t = tuple_size<_Tp>::value>
  81. using __enable_if_has_tuple_size = _Tp;
  82. template<typename _Tp>
  83. struct tuple_size<const __enable_if_has_tuple_size<_Tp>>
  84. : public tuple_size<_Tp> { };
  85. template<typename _Tp>
  86. struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>>
  87. : public tuple_size<_Tp> { };
  88. template<typename _Tp>
  89. struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>>
  90. : public tuple_size<_Tp> { };
  91. /// Gives the type of the ith element of a given tuple type.
  92. template<std::size_t __i, typename _Tp>
  93. struct tuple_element;
  94. // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
  95. template<std::size_t __i, typename _Tp>
  96. using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
  97. template<std::size_t __i, typename _Tp>
  98. struct tuple_element<__i, const _Tp>
  99. {
  100. typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
  101. };
  102. template<std::size_t __i, typename _Tp>
  103. struct tuple_element<__i, volatile _Tp>
  104. {
  105. typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
  106. };
  107. template<std::size_t __i, typename _Tp>
  108. struct tuple_element<__i, const volatile _Tp>
  109. {
  110. typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
  111. };
  112. #if __cplusplus >= 201402L
  113. // The standard says this macro and alias template should be in <tuple>
  114. // but we define them here, to be available when the partial specializations
  115. // of tuple_element<pair<T,U>> and tuple_element<array<T,N>> are defined.
  116. #define __cpp_lib_tuple_element_t 201402L
  117. template<std::size_t __i, typename _Tp>
  118. using tuple_element_t = typename tuple_element<__i, _Tp>::type;
  119. #endif
  120. // Various functions which give std::pair a tuple-like interface.
  121. /// Partial specialization for std::pair
  122. template<typename _T1, typename _T2>
  123. struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
  124. { };
  125. /// Partial specialization for std::pair
  126. template<class _Tp1, class _Tp2>
  127. struct tuple_size<std::pair<_Tp1, _Tp2>>
  128. : public integral_constant<std::size_t, 2> { };
  129. /// Partial specialization for std::pair
  130. template<class _Tp1, class _Tp2>
  131. struct tuple_element<0, std::pair<_Tp1, _Tp2>>
  132. { typedef _Tp1 type; };
  133. /// Partial specialization for std::pair
  134. template<class _Tp1, class _Tp2>
  135. struct tuple_element<1, std::pair<_Tp1, _Tp2>>
  136. { typedef _Tp2 type; };
  137. template<std::size_t _Int>
  138. struct __pair_get;
  139. template<>
  140. struct __pair_get<0>
  141. {
  142. template<typename _Tp1, typename _Tp2>
  143. static constexpr _Tp1&
  144. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  145. { return __pair.first; }
  146. template<typename _Tp1, typename _Tp2>
  147. static constexpr _Tp1&&
  148. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  149. { return std::forward<_Tp1>(__pair.first); }
  150. template<typename _Tp1, typename _Tp2>
  151. static constexpr const _Tp1&
  152. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  153. { return __pair.first; }
  154. template<typename _Tp1, typename _Tp2>
  155. static constexpr const _Tp1&&
  156. __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
  157. { return std::forward<const _Tp1>(__pair.first); }
  158. };
  159. template<>
  160. struct __pair_get<1>
  161. {
  162. template<typename _Tp1, typename _Tp2>
  163. static constexpr _Tp2&
  164. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  165. { return __pair.second; }
  166. template<typename _Tp1, typename _Tp2>
  167. static constexpr _Tp2&&
  168. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  169. { return std::forward<_Tp2>(__pair.second); }
  170. template<typename _Tp1, typename _Tp2>
  171. static constexpr const _Tp2&
  172. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  173. { return __pair.second; }
  174. template<typename _Tp1, typename _Tp2>
  175. static constexpr const _Tp2&&
  176. __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
  177. { return std::forward<const _Tp2>(__pair.second); }
  178. };
  179. template<std::size_t _Int, class _Tp1, class _Tp2>
  180. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  181. get(std::pair<_Tp1, _Tp2>& __in) noexcept
  182. { return __pair_get<_Int>::__get(__in); }
  183. template<std::size_t _Int, class _Tp1, class _Tp2>
  184. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  185. get(std::pair<_Tp1, _Tp2>&& __in) noexcept
  186. { return __pair_get<_Int>::__move_get(std::move(__in)); }
  187. template<std::size_t _Int, class _Tp1, class _Tp2>
  188. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  189. get(const std::pair<_Tp1, _Tp2>& __in) noexcept
  190. { return __pair_get<_Int>::__const_get(__in); }
  191. template<std::size_t _Int, class _Tp1, class _Tp2>
  192. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  193. get(const std::pair<_Tp1, _Tp2>&& __in) noexcept
  194. { return __pair_get<_Int>::__const_move_get(std::move(__in)); }
  195. #if __cplusplus >= 201402L
  196. #define __cpp_lib_tuples_by_type 201304
  197. template <typename _Tp, typename _Up>
  198. constexpr _Tp&
  199. get(pair<_Tp, _Up>& __p) noexcept
  200. { return __p.first; }
  201. template <typename _Tp, typename _Up>
  202. constexpr const _Tp&
  203. get(const pair<_Tp, _Up>& __p) noexcept
  204. { return __p.first; }
  205. template <typename _Tp, typename _Up>
  206. constexpr _Tp&&
  207. get(pair<_Tp, _Up>&& __p) noexcept
  208. { return std::move(__p.first); }
  209. template <typename _Tp, typename _Up>
  210. constexpr const _Tp&&
  211. get(const pair<_Tp, _Up>&& __p) noexcept
  212. { return std::move(__p.first); }
  213. template <typename _Tp, typename _Up>
  214. constexpr _Tp&
  215. get(pair<_Up, _Tp>& __p) noexcept
  216. { return __p.second; }
  217. template <typename _Tp, typename _Up>
  218. constexpr const _Tp&
  219. get(const pair<_Up, _Tp>& __p) noexcept
  220. { return __p.second; }
  221. template <typename _Tp, typename _Up>
  222. constexpr _Tp&&
  223. get(pair<_Up, _Tp>&& __p) noexcept
  224. { return std::move(__p.second); }
  225. template <typename _Tp, typename _Up>
  226. constexpr const _Tp&&
  227. get(const pair<_Up, _Tp>&& __p) noexcept
  228. { return std::move(__p.second); }
  229. #define __cpp_lib_exchange_function 201304
  230. /// Assign @p __new_val to @p __obj and return its previous value.
  231. template <typename _Tp, typename _Up = _Tp>
  232. _GLIBCXX20_CONSTEXPR
  233. inline _Tp
  234. exchange(_Tp& __obj, _Up&& __new_val)
  235. { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
  236. #endif // C++14
  237. // Stores a tuple of indices. Used by tuple and pair, and by bind() to
  238. // extract the elements in a tuple.
  239. template<size_t... _Indexes> struct _Index_tuple { };
  240. #ifdef __has_builtin
  241. # if __has_builtin(__make_integer_seq)
  242. # define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1
  243. # endif
  244. #endif
  245. // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
  246. template<size_t _Num>
  247. struct _Build_index_tuple
  248. {
  249. #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
  250. template<typename, size_t... _Indices>
  251. using _IdxTuple = _Index_tuple<_Indices...>;
  252. using __type = __make_integer_seq<_IdxTuple, size_t, _Num>;
  253. #else
  254. using __type = _Index_tuple<__integer_pack(_Num)...>;
  255. #endif
  256. };
  257. #if __cplusplus > 201103L
  258. #define __cpp_lib_integer_sequence 201304
  259. /// Class template integer_sequence
  260. template<typename _Tp, _Tp... _Idx>
  261. struct integer_sequence
  262. {
  263. typedef _Tp value_type;
  264. static constexpr size_t size() noexcept { return sizeof...(_Idx); }
  265. };
  266. /// Alias template make_integer_sequence
  267. template<typename _Tp, _Tp _Num>
  268. using make_integer_sequence
  269. #if _GLIBCXX_USE_MAKE_INTEGER_SEQ
  270. = __make_integer_seq<integer_sequence, _Tp, _Num>;
  271. #else
  272. = integer_sequence<_Tp, __integer_pack(_Num)...>;
  273. #endif
  274. #undef _GLIBCXX_USE_MAKE_INTEGER_SEQ
  275. /// Alias template index_sequence
  276. template<size_t... _Idx>
  277. using index_sequence = integer_sequence<size_t, _Idx...>;
  278. /// Alias template make_index_sequence
  279. template<size_t _Num>
  280. using make_index_sequence = make_integer_sequence<size_t, _Num>;
  281. /// Alias template index_sequence_for
  282. template<typename... _Types>
  283. using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
  284. #endif
  285. #if __cplusplus > 201402L
  286. struct in_place_t {
  287. explicit in_place_t() = default;
  288. };
  289. inline constexpr in_place_t in_place{};
  290. template<typename _Tp> struct in_place_type_t
  291. {
  292. explicit in_place_type_t() = default;
  293. };
  294. template<typename _Tp>
  295. inline constexpr in_place_type_t<_Tp> in_place_type{};
  296. template<size_t _Idx> struct in_place_index_t
  297. {
  298. explicit in_place_index_t() = default;
  299. };
  300. template<size_t _Idx>
  301. inline constexpr in_place_index_t<_Idx> in_place_index{};
  302. template<typename>
  303. struct __is_in_place_type_impl : false_type
  304. { };
  305. template<typename _Tp>
  306. struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
  307. { };
  308. template<typename _Tp>
  309. struct __is_in_place_type
  310. : public __is_in_place_type_impl<_Tp>
  311. { };
  312. #define __cpp_lib_as_const 201510
  313. template<typename _Tp>
  314. constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
  315. template<typename _Tp>
  316. void as_const(const _Tp&&) = delete;
  317. #if __cplusplus > 201703L
  318. #define __cpp_lib_integer_comparison_functions 202002L
  319. template<typename _Tp, typename _Up>
  320. constexpr bool
  321. cmp_equal(_Tp __t, _Up __u) noexcept
  322. {
  323. static_assert(__is_standard_integer<_Tp>::value);
  324. static_assert(__is_standard_integer<_Up>::value);
  325. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  326. return __t == __u;
  327. else if constexpr (is_signed_v<_Tp>)
  328. return __t >= 0 && make_unsigned_t<_Tp>(__t) == __u;
  329. else
  330. return __u >= 0 && __t == make_unsigned_t<_Up>(__u);
  331. }
  332. template<typename _Tp, typename _Up>
  333. constexpr bool
  334. cmp_not_equal(_Tp __t, _Up __u) noexcept
  335. { return !std::cmp_equal(__t, __u); }
  336. template<typename _Tp, typename _Up>
  337. constexpr bool
  338. cmp_less(_Tp __t, _Up __u) noexcept
  339. {
  340. static_assert(__is_standard_integer<_Tp>::value);
  341. static_assert(__is_standard_integer<_Up>::value);
  342. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  343. return __t < __u;
  344. else if constexpr (is_signed_v<_Tp>)
  345. return __t < 0 || make_unsigned_t<_Tp>(__t) < __u;
  346. else
  347. return __u >= 0 && __t < make_unsigned_t<_Up>(__u);
  348. }
  349. template<typename _Tp, typename _Up>
  350. constexpr bool
  351. cmp_greater(_Tp __t, _Up __u) noexcept
  352. { return std::cmp_less(__u, __t); }
  353. template<typename _Tp, typename _Up>
  354. constexpr bool
  355. cmp_less_equal(_Tp __t, _Up __u) noexcept
  356. { return !std::cmp_less(__u, __t); }
  357. template<typename _Tp, typename _Up>
  358. constexpr bool
  359. cmp_greater_equal(_Tp __t, _Up __u) noexcept
  360. { return !std::cmp_less(__t, __u); }
  361. template<typename _Up, typename _Tp>
  362. constexpr bool
  363. in_range(_Tp __t) noexcept
  364. {
  365. static_assert(__is_standard_integer<_Up>::value);
  366. static_assert(__is_standard_integer<_Tp>::value);
  367. using __gnu_cxx::__int_traits;
  368. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  369. return __int_traits<_Up>::__min <= __t
  370. && __t <= __int_traits<_Up>::__max;
  371. else if constexpr (is_signed_v<_Tp>)
  372. return __t >= 0
  373. && make_unsigned_t<_Tp>(__t) <= __int_traits<_Up>::__max;
  374. else
  375. return __t <= make_unsigned_t<_Up>(__int_traits<_Up>::__max);
  376. }
  377. #endif // C++20
  378. #endif // C++17
  379. _GLIBCXX_END_NAMESPACE_VERSION
  380. } // namespace
  381. #endif
  382. #endif /* _GLIBCXX_UTILITY */