utility 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // <utility> -*- C++ -*-
  2. // Copyright (C) 2001-2023 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. * Basic function and class templates used with the rest of the library.
  55. * Includes pair, swap, forward/move helpers, declval, integer_sequence.
  56. */
  57. #include <bits/c++config.h>
  58. #include <bits/stl_relops.h>
  59. #include <bits/stl_pair.h>
  60. #if __cplusplus >= 201103L
  61. #include <initializer_list>
  62. #include <type_traits>
  63. #include <bits/move.h>
  64. #include <bits/utility.h>
  65. #if __cplusplus >= 202002L
  66. #include <ext/numeric_traits.h> // __is_standard_integer, __int_traits
  67. #endif
  68. namespace std _GLIBCXX_VISIBILITY(default)
  69. {
  70. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  71. #if __cplusplus >= 201402L
  72. #define __cpp_lib_exchange_function 201304L
  73. #if __cplusplus > 201703L
  74. # define __cpp_lib_constexpr_algorithms 201806L
  75. #endif
  76. /// Assign @p __new_val to @p __obj and return its previous value.
  77. template <typename _Tp, typename _Up = _Tp>
  78. _GLIBCXX20_CONSTEXPR
  79. inline _Tp
  80. exchange(_Tp& __obj, _Up&& __new_val)
  81. noexcept(__and_<is_nothrow_move_constructible<_Tp>,
  82. is_nothrow_assignable<_Tp&, _Up>>::value)
  83. { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
  84. #if __cplusplus >= 201703L
  85. #define __cpp_lib_as_const 201510L
  86. template<typename _Tp>
  87. [[nodiscard]]
  88. constexpr add_const_t<_Tp>&
  89. as_const(_Tp& __t) noexcept
  90. { return __t; }
  91. template<typename _Tp>
  92. void as_const(const _Tp&&) = delete;
  93. #if __cplusplus > 201703L
  94. #define __cpp_lib_integer_comparison_functions 202002L
  95. template<typename _Tp, typename _Up>
  96. constexpr bool
  97. cmp_equal(_Tp __t, _Up __u) noexcept
  98. {
  99. static_assert(__is_standard_integer<_Tp>::value);
  100. static_assert(__is_standard_integer<_Up>::value);
  101. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  102. return __t == __u;
  103. else if constexpr (is_signed_v<_Tp>)
  104. return __t >= 0 && make_unsigned_t<_Tp>(__t) == __u;
  105. else
  106. return __u >= 0 && __t == make_unsigned_t<_Up>(__u);
  107. }
  108. template<typename _Tp, typename _Up>
  109. constexpr bool
  110. cmp_not_equal(_Tp __t, _Up __u) noexcept
  111. { return !std::cmp_equal(__t, __u); }
  112. template<typename _Tp, typename _Up>
  113. constexpr bool
  114. cmp_less(_Tp __t, _Up __u) noexcept
  115. {
  116. static_assert(__is_standard_integer<_Tp>::value);
  117. static_assert(__is_standard_integer<_Up>::value);
  118. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  119. return __t < __u;
  120. else if constexpr (is_signed_v<_Tp>)
  121. return __t < 0 || make_unsigned_t<_Tp>(__t) < __u;
  122. else
  123. return __u >= 0 && __t < make_unsigned_t<_Up>(__u);
  124. }
  125. template<typename _Tp, typename _Up>
  126. constexpr bool
  127. cmp_greater(_Tp __t, _Up __u) noexcept
  128. { return std::cmp_less(__u, __t); }
  129. template<typename _Tp, typename _Up>
  130. constexpr bool
  131. cmp_less_equal(_Tp __t, _Up __u) noexcept
  132. { return !std::cmp_less(__u, __t); }
  133. template<typename _Tp, typename _Up>
  134. constexpr bool
  135. cmp_greater_equal(_Tp __t, _Up __u) noexcept
  136. { return !std::cmp_less(__t, __u); }
  137. template<typename _Res, typename _Tp>
  138. constexpr bool
  139. in_range(_Tp __t) noexcept
  140. {
  141. static_assert(__is_standard_integer<_Res>::value);
  142. static_assert(__is_standard_integer<_Tp>::value);
  143. using __gnu_cxx::__int_traits;
  144. if constexpr (is_signed_v<_Tp> == is_signed_v<_Res>)
  145. return __int_traits<_Res>::__min <= __t
  146. && __t <= __int_traits<_Res>::__max;
  147. else if constexpr (is_signed_v<_Tp>)
  148. return __t >= 0
  149. && make_unsigned_t<_Tp>(__t) <= __int_traits<_Res>::__max;
  150. else
  151. return __t <= make_unsigned_t<_Res>(__int_traits<_Res>::__max);
  152. }
  153. #if __cplusplus > 202002L
  154. #define __cpp_lib_to_underlying 202102L
  155. /// Convert an object of enumeration type to its underlying type.
  156. template<typename _Tp>
  157. [[nodiscard]]
  158. constexpr underlying_type_t<_Tp>
  159. to_underlying(_Tp __value) noexcept
  160. { return static_cast<underlying_type_t<_Tp>>(__value); }
  161. #define __cpp_lib_unreachable 202202L
  162. /// Informs the compiler that program control flow never reaches this point.
  163. /**
  164. * Evaluating a call to this function results in undefined behaviour.
  165. * This can be used as an assertion informing the compiler that certain
  166. * conditions are impossible, for when the compiler is unable to determine
  167. * that by itself.
  168. *
  169. * For example, it can be used to prevent warnings about reaching the
  170. * end of a non-void function without returning.
  171. *
  172. * @since C++23
  173. */
  174. [[noreturn,__gnu__::__always_inline__]]
  175. inline void
  176. unreachable()
  177. {
  178. #ifdef _GLIBCXX_DEBUG
  179. std::__glibcxx_assert_fail(nullptr, 0, "std::unreachable()", nullptr);
  180. #elif defined _GLIBCXX_ASSERTIONS
  181. __builtin_trap();
  182. #else
  183. __builtin_unreachable();
  184. #endif
  185. }
  186. #endif // C++23
  187. #endif // C++20
  188. #endif // C++17
  189. #endif // C++14
  190. _GLIBCXX_END_NAMESPACE_VERSION
  191. } // namespace
  192. #endif
  193. #endif /* _GLIBCXX_UTILITY */