utility 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // <utility> -*- C++ -*-
  2. // Copyright (C) 2001-2016 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. template<typename _Tp>
  74. struct tuple_size<const _Tp>
  75. : integral_constant<size_t, tuple_size<_Tp>::value> { };
  76. template<typename _Tp>
  77. struct tuple_size<volatile _Tp>
  78. : integral_constant<size_t, tuple_size<_Tp>::value> { };
  79. template<typename _Tp>
  80. struct tuple_size<const volatile _Tp>
  81. : integral_constant<size_t, tuple_size<_Tp>::value> { };
  82. /// Gives the type of the ith element of a given tuple type.
  83. template<std::size_t __i, typename _Tp>
  84. struct tuple_element;
  85. // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
  86. template<std::size_t __i, typename _Tp>
  87. using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
  88. template<std::size_t __i, typename _Tp>
  89. struct tuple_element<__i, const _Tp>
  90. {
  91. typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
  92. };
  93. template<std::size_t __i, typename _Tp>
  94. struct tuple_element<__i, volatile _Tp>
  95. {
  96. typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
  97. };
  98. template<std::size_t __i, typename _Tp>
  99. struct tuple_element<__i, const volatile _Tp>
  100. {
  101. typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
  102. };
  103. #if __cplusplus > 201103L
  104. #define __cpp_lib_tuple_element_t 201402
  105. template<std::size_t __i, typename _Tp>
  106. using tuple_element_t = typename tuple_element<__i, _Tp>::type;
  107. #endif
  108. template<typename>
  109. struct __is_tuple_like_impl : false_type
  110. { };
  111. // Various functions which give std::pair a tuple-like interface.
  112. /// Partial specialization for std::pair
  113. template<typename _T1, typename _T2>
  114. struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
  115. { };
  116. /// Partial specialization for std::pair
  117. template<class _Tp1, class _Tp2>
  118. struct tuple_size<std::pair<_Tp1, _Tp2>>
  119. : public integral_constant<std::size_t, 2> { };
  120. /// Partial specialization for std::pair
  121. template<class _Tp1, class _Tp2>
  122. struct tuple_element<0, std::pair<_Tp1, _Tp2>>
  123. { typedef _Tp1 type; };
  124. /// Partial specialization for std::pair
  125. template<class _Tp1, class _Tp2>
  126. struct tuple_element<1, std::pair<_Tp1, _Tp2>>
  127. { typedef _Tp2 type; };
  128. template<std::size_t _Int>
  129. struct __pair_get;
  130. template<>
  131. struct __pair_get<0>
  132. {
  133. template<typename _Tp1, typename _Tp2>
  134. static constexpr _Tp1&
  135. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  136. { return __pair.first; }
  137. template<typename _Tp1, typename _Tp2>
  138. static constexpr _Tp1&&
  139. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  140. { return std::forward<_Tp1>(__pair.first); }
  141. template<typename _Tp1, typename _Tp2>
  142. static constexpr const _Tp1&
  143. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  144. { return __pair.first; }
  145. };
  146. template<>
  147. struct __pair_get<1>
  148. {
  149. template<typename _Tp1, typename _Tp2>
  150. static constexpr _Tp2&
  151. __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
  152. { return __pair.second; }
  153. template<typename _Tp1, typename _Tp2>
  154. static constexpr _Tp2&&
  155. __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
  156. { return std::forward<_Tp2>(__pair.second); }
  157. template<typename _Tp1, typename _Tp2>
  158. static constexpr const _Tp2&
  159. __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
  160. { return __pair.second; }
  161. };
  162. template<std::size_t _Int, class _Tp1, class _Tp2>
  163. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  164. get(std::pair<_Tp1, _Tp2>& __in) noexcept
  165. { return __pair_get<_Int>::__get(__in); }
  166. template<std::size_t _Int, class _Tp1, class _Tp2>
  167. constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
  168. get(std::pair<_Tp1, _Tp2>&& __in) noexcept
  169. { return __pair_get<_Int>::__move_get(std::move(__in)); }
  170. template<std::size_t _Int, class _Tp1, class _Tp2>
  171. constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
  172. get(const std::pair<_Tp1, _Tp2>& __in) noexcept
  173. { return __pair_get<_Int>::__const_get(__in); }
  174. #if __cplusplus > 201103L
  175. #define __cpp_lib_tuples_by_type 201304
  176. template <typename _Tp, typename _Up>
  177. constexpr _Tp&
  178. get(pair<_Tp, _Up>& __p) noexcept
  179. { return __p.first; }
  180. template <typename _Tp, typename _Up>
  181. constexpr const _Tp&
  182. get(const pair<_Tp, _Up>& __p) noexcept
  183. { return __p.first; }
  184. template <typename _Tp, typename _Up>
  185. constexpr _Tp&&
  186. get(pair<_Tp, _Up>&& __p) noexcept
  187. { return std::move(__p.first); }
  188. template <typename _Tp, typename _Up>
  189. constexpr _Tp&
  190. get(pair<_Up, _Tp>& __p) noexcept
  191. { return __p.second; }
  192. template <typename _Tp, typename _Up>
  193. constexpr const _Tp&
  194. get(const pair<_Up, _Tp>& __p) noexcept
  195. { return __p.second; }
  196. template <typename _Tp, typename _Up>
  197. constexpr _Tp&&
  198. get(pair<_Up, _Tp>&& __p) noexcept
  199. { return std::move(__p.second); }
  200. #define __cpp_lib_exchange_function 201304
  201. /// Assign @p __new_val to @p __obj and return its previous value.
  202. template <typename _Tp, typename _Up = _Tp>
  203. inline _Tp
  204. exchange(_Tp& __obj, _Up&& __new_val)
  205. { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
  206. #endif
  207. // Stores a tuple of indices. Used by tuple and pair, and by bind() to
  208. // extract the elements in a tuple.
  209. template<size_t... _Indexes> struct _Index_tuple { };
  210. // Concatenates two _Index_tuples.
  211. template<typename _Itup1, typename _Itup2> struct _Itup_cat;
  212. template<size_t... _Ind1, size_t... _Ind2>
  213. struct _Itup_cat<_Index_tuple<_Ind1...>, _Index_tuple<_Ind2...>>
  214. {
  215. using __type = _Index_tuple<_Ind1..., (_Ind2 + sizeof...(_Ind1))...>;
  216. };
  217. // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
  218. template<size_t _Num>
  219. struct _Build_index_tuple
  220. : _Itup_cat<typename _Build_index_tuple<_Num / 2>::__type,
  221. typename _Build_index_tuple<_Num - _Num / 2>::__type>
  222. { };
  223. template<>
  224. struct _Build_index_tuple<1>
  225. {
  226. typedef _Index_tuple<0> __type;
  227. };
  228. template<>
  229. struct _Build_index_tuple<0>
  230. {
  231. typedef _Index_tuple<> __type;
  232. };
  233. #if __cplusplus > 201103L
  234. #define __cpp_lib_integer_sequence 201304
  235. /// Class template integer_sequence
  236. template<typename _Tp, _Tp... _Idx>
  237. struct integer_sequence
  238. {
  239. typedef _Tp value_type;
  240. static constexpr size_t size() { return sizeof...(_Idx); }
  241. };
  242. template<typename _Tp, _Tp _Num,
  243. typename _ISeq = typename _Build_index_tuple<_Num>::__type>
  244. struct _Make_integer_sequence;
  245. template<typename _Tp, _Tp _Num, size_t... _Idx>
  246. struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>>
  247. {
  248. static_assert( _Num >= 0,
  249. "Cannot make integer sequence of negative length" );
  250. typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type;
  251. };
  252. /// Alias template make_integer_sequence
  253. template<typename _Tp, _Tp _Num>
  254. using make_integer_sequence
  255. = typename _Make_integer_sequence<_Tp, _Num>::__type;
  256. /// Alias template index_sequence
  257. template<size_t... _Idx>
  258. using index_sequence = integer_sequence<size_t, _Idx...>;
  259. /// Alias template make_index_sequence
  260. template<size_t _Num>
  261. using make_index_sequence = make_integer_sequence<size_t, _Num>;
  262. /// Alias template index_sequence_for
  263. template<typename... _Types>
  264. using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
  265. #endif
  266. _GLIBCXX_END_NAMESPACE_VERSION
  267. } // namespace
  268. #endif
  269. #endif /* _GLIBCXX_UTILITY */