utility 6.9 KB

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