utility 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_UTILITY
  10. #define _LIBCPP_UTILITY
  11. /*
  12. utility synopsis
  13. #include <initializer_list>
  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 <typename T>
  36. [[nodiscard]] constexpr
  37. auto forward_like(auto&& x) noexcept -> see below; // since C++23
  38. template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14
  39. template <class T>
  40. typename conditional
  41. <
  42. !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
  43. const T&,
  44. T&&
  45. >::type
  46. move_if_noexcept(T& x) noexcept; // constexpr in C++14
  47. template <class T> constexpr add_const_t<T>& as_const(T& t) noexcept; // C++17
  48. template <class T> void as_const(const T&&) = delete; // C++17
  49. template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
  50. template<class T, class U> constexpr bool cmp_equal(T t, U u) noexcept; // C++20
  51. template<class T, class U> constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20
  52. template<class T, class U> constexpr bool cmp_less(T t, U u) noexcept; // C++20
  53. template<class T, class U> constexpr bool cmp_greater(T t, U u) noexcept; // C++20
  54. template<class T, class U> constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20
  55. template<class T, class U> constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20
  56. template<class R, class T> constexpr bool in_range(T t) noexcept; // C++20
  57. template <class T1, class T2>
  58. struct pair
  59. {
  60. typedef T1 first_type;
  61. typedef T2 second_type;
  62. T1 first;
  63. T2 second;
  64. pair(const pair&) = default;
  65. pair(pair&&) = default;
  66. explicit(see-below) constexpr pair();
  67. explicit(see-below) pair(const T1& x, const T2& y); // constexpr in C++14
  68. template <class U = T1, class V = T2> explicit(see-below) pair(U&&, V&&); // constexpr in C++14
  69. template <class U, class V> constexpr explicit(see below) pair(pair<U, V>&); // since C++23
  70. template <class U, class V> explicit(see-below) pair(const pair<U, V>& p); // constexpr in C++14
  71. template <class U, class V> explicit(see-below) pair(pair<U, V>&& p); // constexpr in C++14
  72. template <class U, class V>
  73. constexpr explicit(see below) pair(const pair<U, V>&&); // since C++23
  74. template <class... Args1, class... Args2>
  75. pair(piecewise_construct_t, tuple<Args1...> first_args,
  76. tuple<Args2...> second_args); // constexpr in C++20
  77. constexpr const pair& operator=(const pair& p) const; // since C++23
  78. template <class U, class V> pair& operator=(const pair<U, V>& p); // constexpr in C++20
  79. template <class U, class V>
  80. constexpr const pair& operator=(const pair<U, V>& p) const; // since C++23
  81. pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
  82. is_nothrow_move_assignable<T2>::value); // constexpr in C++20
  83. constexpr const pair& operator=(pair&& p) const; // since C++23
  84. template <class U, class V> pair& operator=(pair<U, V>&& p); // constexpr in C++20
  85. template <class U, class V>
  86. constexpr const pair& operator=(pair<U, V>&& p) const; // since C++23
  87. void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
  88. is_nothrow_swappable_v<T2>); // constexpr in C++20
  89. constexpr void swap(const pair& p) const noexcept(see below); // since C++23
  90. };
  91. template<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual>
  92. struct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>; // since C++23
  93. template<class T1, class T2, class U1, class U2>
  94. struct common_type<pair<T1, T2>, pair<U1, U2>>; // since C++23
  95. template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;
  96. template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
  97. template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
  98. template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
  99. template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
  100. template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
  101. template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
  102. template <class T1, class T2>
  103. constexpr common_comparison_type_t<synth-three-way-result<T1>,
  104. synth-three-way-result<T2>>
  105. operator<=>(const pair<T1,T2>&, const pair<T1,T2>&); // C++20
  106. template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
  107. template <class T1, class T2>
  108. void
  109. swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
  110. template<class T1, class T2>
  111. constexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // since C++23
  112. struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
  113. inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
  114. template <class T> struct tuple_size;
  115. template <size_t I, class T> struct tuple_element;
  116. template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
  117. template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
  118. template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
  119. template<size_t I, class T1, class T2>
  120. typename tuple_element<I, pair<T1, T2> >::type&
  121. get(pair<T1, T2>&) noexcept; // constexpr in C++14
  122. template<size_t I, class T1, class T2>
  123. const typename tuple_element<I, pair<T1, T2> >::type&
  124. get(const pair<T1, T2>&) noexcept; // constexpr in C++14
  125. template<size_t I, class T1, class T2>
  126. typename tuple_element<I, pair<T1, T2> >::type&&
  127. get(pair<T1, T2>&&) noexcept; // constexpr in C++14
  128. template<size_t I, class T1, class T2>
  129. const typename tuple_element<I, pair<T1, T2> >::type&&
  130. get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
  131. template<class T1, class T2>
  132. constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
  133. template<class T1, class T2>
  134. constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
  135. template<class T1, class T2>
  136. constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
  137. template<class T1, class T2>
  138. constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
  139. template<class T1, class T2>
  140. constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
  141. template<class T1, class T2>
  142. constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
  143. template<class T1, class T2>
  144. constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
  145. template<class T1, class T2>
  146. constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
  147. // C++14
  148. template<class T, T... I>
  149. struct integer_sequence
  150. {
  151. typedef T value_type;
  152. static constexpr size_t size() noexcept;
  153. };
  154. template<size_t... I>
  155. using index_sequence = integer_sequence<size_t, I...>;
  156. template<class T, T N>
  157. using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
  158. template<size_t N>
  159. using make_index_sequence = make_integer_sequence<size_t, N>;
  160. template<class... T>
  161. using index_sequence_for = make_index_sequence<sizeof...(T)>;
  162. template<class T, class U=T>
  163. constexpr T exchange(T& obj, U&& new_value)
  164. noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_assignable<T&, U>::value); // constexpr in C++17, noexcept in C++23
  165. // 20.2.7, in-place construction // C++17
  166. struct in_place_t {
  167. explicit in_place_t() = default;
  168. };
  169. inline constexpr in_place_t in_place{};
  170. template <class T>
  171. struct in_place_type_t {
  172. explicit in_place_type_t() = default;
  173. };
  174. template <class T>
  175. inline constexpr in_place_type_t<T> in_place_type{};
  176. template <size_t I>
  177. struct in_place_index_t {
  178. explicit in_place_index_t() = default;
  179. };
  180. template <size_t I>
  181. inline constexpr in_place_index_t<I> in_place_index{};
  182. // [utility.underlying], to_underlying
  183. template <class T>
  184. constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
  185. } // std
  186. */
  187. #include <__assert> // all public C++ headers provide the assertion handler
  188. #include <__config>
  189. #include <__utility/as_const.h>
  190. #include <__utility/auto_cast.h>
  191. #include <__utility/cmp.h>
  192. #include <__utility/declval.h>
  193. #include <__utility/exception_guard.h>
  194. #include <__utility/exchange.h>
  195. #include <__utility/forward.h>
  196. #include <__utility/forward_like.h>
  197. #include <__utility/in_place.h>
  198. #include <__utility/integer_sequence.h>
  199. #include <__utility/move.h>
  200. #include <__utility/pair.h>
  201. #include <__utility/piecewise_construct.h>
  202. #include <__utility/priority_tag.h>
  203. #include <__utility/rel_ops.h>
  204. #include <__utility/swap.h>
  205. #include <__utility/to_underlying.h>
  206. #include <__utility/unreachable.h>
  207. #include <version>
  208. // standard-mandated includes
  209. // [utility.syn]
  210. #include <compare>
  211. #include <initializer_list>
  212. // [tuple.helper]
  213. #include <__tuple_dir/tuple_element.h>
  214. #include <__tuple_dir/tuple_size.h>
  215. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  216. # pragma GCC system_header
  217. #endif
  218. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  219. # include <cstdlib>
  220. # include <iosfwd>
  221. # include <type_traits>
  222. #endif
  223. #endif // _LIBCPP_UTILITY