concepts.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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___ITERATOR_CONCEPTS_H
  10. #define _LIBCPP___ITERATOR_CONCEPTS_H
  11. #include <__concepts/arithmetic.h>
  12. #include <__concepts/assignable.h>
  13. #include <__concepts/common_reference_with.h>
  14. #include <__concepts/constructible.h>
  15. #include <__concepts/copyable.h>
  16. #include <__concepts/derived_from.h>
  17. #include <__concepts/equality_comparable.h>
  18. #include <__concepts/invocable.h>
  19. #include <__concepts/movable.h>
  20. #include <__concepts/predicate.h>
  21. #include <__concepts/regular.h>
  22. #include <__concepts/relation.h>
  23. #include <__concepts/same_as.h>
  24. #include <__concepts/semiregular.h>
  25. #include <__concepts/totally_ordered.h>
  26. #include <__config>
  27. #include <__functional/invoke.h>
  28. #include <__iterator/incrementable_traits.h>
  29. #include <__iterator/iter_move.h>
  30. #include <__iterator/iterator_traits.h>
  31. #include <__iterator/readable_traits.h>
  32. #include <__memory/pointer_traits.h>
  33. #include <__type_traits/add_pointer.h>
  34. #include <__type_traits/common_reference.h>
  35. #include <__type_traits/is_pointer.h>
  36. #include <__type_traits/is_reference.h>
  37. #include <__type_traits/remove_cv.h>
  38. #include <__type_traits/remove_cvref.h>
  39. #include <__utility/forward.h>
  40. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  41. # pragma GCC system_header
  42. #endif
  43. _LIBCPP_BEGIN_NAMESPACE_STD
  44. #if _LIBCPP_STD_VER >= 20
  45. // [iterator.concept.readable]
  46. template<class _In>
  47. concept __indirectly_readable_impl =
  48. requires(const _In __i) {
  49. typename iter_value_t<_In>;
  50. typename iter_reference_t<_In>;
  51. typename iter_rvalue_reference_t<_In>;
  52. { *__i } -> same_as<iter_reference_t<_In>>;
  53. { ranges::iter_move(__i) } -> same_as<iter_rvalue_reference_t<_In>>;
  54. } &&
  55. common_reference_with<iter_reference_t<_In>&&, iter_value_t<_In>&> &&
  56. common_reference_with<iter_reference_t<_In>&&, iter_rvalue_reference_t<_In>&&> &&
  57. common_reference_with<iter_rvalue_reference_t<_In>&&, const iter_value_t<_In>&>;
  58. template<class _In>
  59. concept indirectly_readable = __indirectly_readable_impl<remove_cvref_t<_In>>;
  60. template<indirectly_readable _Tp>
  61. using iter_common_reference_t = common_reference_t<iter_reference_t<_Tp>, iter_value_t<_Tp>&>;
  62. // [iterator.concept.writable]
  63. template<class _Out, class _Tp>
  64. concept indirectly_writable =
  65. requires(_Out&& __o, _Tp&& __t) {
  66. *__o = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
  67. *_VSTD::forward<_Out>(__o) = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
  68. const_cast<const iter_reference_t<_Out>&&>(*__o) = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
  69. const_cast<const iter_reference_t<_Out>&&>(*_VSTD::forward<_Out>(__o)) = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
  70. };
  71. // [iterator.concept.winc]
  72. template<class _Tp>
  73. concept __integer_like = integral<_Tp> && !same_as<_Tp, bool>;
  74. template<class _Tp>
  75. concept __signed_integer_like = signed_integral<_Tp>;
  76. template<class _Ip>
  77. concept weakly_incrementable =
  78. // TODO: remove this once the clang bug is fixed (bugs.llvm.org/PR48173).
  79. !same_as<_Ip, bool> && // Currently, clang does not handle bool correctly.
  80. movable<_Ip> &&
  81. requires(_Ip __i) {
  82. typename iter_difference_t<_Ip>;
  83. requires __signed_integer_like<iter_difference_t<_Ip>>;
  84. { ++__i } -> same_as<_Ip&>; // not required to be equality-preserving
  85. __i++; // not required to be equality-preserving
  86. };
  87. // [iterator.concept.inc]
  88. template<class _Ip>
  89. concept incrementable =
  90. regular<_Ip> &&
  91. weakly_incrementable<_Ip> &&
  92. requires(_Ip __i) {
  93. { __i++ } -> same_as<_Ip>;
  94. };
  95. // [iterator.concept.iterator]
  96. template<class _Ip>
  97. concept input_or_output_iterator =
  98. requires(_Ip __i) {
  99. { *__i } -> __can_reference;
  100. } &&
  101. weakly_incrementable<_Ip>;
  102. // [iterator.concept.sentinel]
  103. template<class _Sp, class _Ip>
  104. concept sentinel_for =
  105. semiregular<_Sp> &&
  106. input_or_output_iterator<_Ip> &&
  107. __weakly_equality_comparable_with<_Sp, _Ip>;
  108. template<class, class>
  109. inline constexpr bool disable_sized_sentinel_for = false;
  110. template<class _Sp, class _Ip>
  111. concept sized_sentinel_for =
  112. sentinel_for<_Sp, _Ip> &&
  113. !disable_sized_sentinel_for<remove_cv_t<_Sp>, remove_cv_t<_Ip>> &&
  114. requires(const _Ip& __i, const _Sp& __s) {
  115. { __s - __i } -> same_as<iter_difference_t<_Ip>>;
  116. { __i - __s } -> same_as<iter_difference_t<_Ip>>;
  117. };
  118. // [iterator.concept.input]
  119. template<class _Ip>
  120. concept input_iterator =
  121. input_or_output_iterator<_Ip> &&
  122. indirectly_readable<_Ip> &&
  123. requires { typename _ITER_CONCEPT<_Ip>; } &&
  124. derived_from<_ITER_CONCEPT<_Ip>, input_iterator_tag>;
  125. // [iterator.concept.output]
  126. template<class _Ip, class _Tp>
  127. concept output_iterator =
  128. input_or_output_iterator<_Ip> &&
  129. indirectly_writable<_Ip, _Tp> &&
  130. requires (_Ip __it, _Tp&& __t) {
  131. *__it++ = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
  132. };
  133. // [iterator.concept.forward]
  134. template<class _Ip>
  135. concept forward_iterator =
  136. input_iterator<_Ip> &&
  137. derived_from<_ITER_CONCEPT<_Ip>, forward_iterator_tag> &&
  138. incrementable<_Ip> &&
  139. sentinel_for<_Ip, _Ip>;
  140. // [iterator.concept.bidir]
  141. template<class _Ip>
  142. concept bidirectional_iterator =
  143. forward_iterator<_Ip> &&
  144. derived_from<_ITER_CONCEPT<_Ip>, bidirectional_iterator_tag> &&
  145. requires(_Ip __i) {
  146. { --__i } -> same_as<_Ip&>;
  147. { __i-- } -> same_as<_Ip>;
  148. };
  149. template<class _Ip>
  150. concept random_access_iterator =
  151. bidirectional_iterator<_Ip> &&
  152. derived_from<_ITER_CONCEPT<_Ip>, random_access_iterator_tag> &&
  153. totally_ordered<_Ip> &&
  154. sized_sentinel_for<_Ip, _Ip> &&
  155. requires(_Ip __i, const _Ip __j, const iter_difference_t<_Ip> __n) {
  156. { __i += __n } -> same_as<_Ip&>;
  157. { __j + __n } -> same_as<_Ip>;
  158. { __n + __j } -> same_as<_Ip>;
  159. { __i -= __n } -> same_as<_Ip&>;
  160. { __j - __n } -> same_as<_Ip>;
  161. { __j[__n] } -> same_as<iter_reference_t<_Ip>>;
  162. };
  163. template<class _Ip>
  164. concept contiguous_iterator =
  165. random_access_iterator<_Ip> &&
  166. derived_from<_ITER_CONCEPT<_Ip>, contiguous_iterator_tag> &&
  167. is_lvalue_reference_v<iter_reference_t<_Ip>> &&
  168. same_as<iter_value_t<_Ip>, remove_cvref_t<iter_reference_t<_Ip>>> &&
  169. requires(const _Ip& __i) {
  170. { _VSTD::to_address(__i) } -> same_as<add_pointer_t<iter_reference_t<_Ip>>>;
  171. };
  172. template<class _Ip>
  173. concept __has_arrow = input_iterator<_Ip> && (is_pointer_v<_Ip> || requires(_Ip __i) { __i.operator->(); });
  174. // [indirectcallable.indirectinvocable]
  175. template<class _Fp, class _It>
  176. concept indirectly_unary_invocable =
  177. indirectly_readable<_It> &&
  178. copy_constructible<_Fp> &&
  179. invocable<_Fp&, iter_value_t<_It>&> &&
  180. invocable<_Fp&, iter_reference_t<_It>> &&
  181. invocable<_Fp&, iter_common_reference_t<_It>> &&
  182. common_reference_with<
  183. invoke_result_t<_Fp&, iter_value_t<_It>&>,
  184. invoke_result_t<_Fp&, iter_reference_t<_It>>>;
  185. template<class _Fp, class _It>
  186. concept indirectly_regular_unary_invocable =
  187. indirectly_readable<_It> &&
  188. copy_constructible<_Fp> &&
  189. regular_invocable<_Fp&, iter_value_t<_It>&> &&
  190. regular_invocable<_Fp&, iter_reference_t<_It>> &&
  191. regular_invocable<_Fp&, iter_common_reference_t<_It>> &&
  192. common_reference_with<
  193. invoke_result_t<_Fp&, iter_value_t<_It>&>,
  194. invoke_result_t<_Fp&, iter_reference_t<_It>>>;
  195. template<class _Fp, class _It>
  196. concept indirect_unary_predicate =
  197. indirectly_readable<_It> &&
  198. copy_constructible<_Fp> &&
  199. predicate<_Fp&, iter_value_t<_It>&> &&
  200. predicate<_Fp&, iter_reference_t<_It>> &&
  201. predicate<_Fp&, iter_common_reference_t<_It>>;
  202. template<class _Fp, class _It1, class _It2>
  203. concept indirect_binary_predicate =
  204. indirectly_readable<_It1> && indirectly_readable<_It2> &&
  205. copy_constructible<_Fp> &&
  206. predicate<_Fp&, iter_value_t<_It1>&, iter_value_t<_It2>&> &&
  207. predicate<_Fp&, iter_value_t<_It1>&, iter_reference_t<_It2>> &&
  208. predicate<_Fp&, iter_reference_t<_It1>, iter_value_t<_It2>&> &&
  209. predicate<_Fp&, iter_reference_t<_It1>, iter_reference_t<_It2>> &&
  210. predicate<_Fp&, iter_common_reference_t<_It1>, iter_common_reference_t<_It2>>;
  211. template<class _Fp, class _It1, class _It2 = _It1>
  212. concept indirect_equivalence_relation =
  213. indirectly_readable<_It1> && indirectly_readable<_It2> &&
  214. copy_constructible<_Fp> &&
  215. equivalence_relation<_Fp&, iter_value_t<_It1>&, iter_value_t<_It2>&> &&
  216. equivalence_relation<_Fp&, iter_value_t<_It1>&, iter_reference_t<_It2>> &&
  217. equivalence_relation<_Fp&, iter_reference_t<_It1>, iter_value_t<_It2>&> &&
  218. equivalence_relation<_Fp&, iter_reference_t<_It1>, iter_reference_t<_It2>> &&
  219. equivalence_relation<_Fp&, iter_common_reference_t<_It1>, iter_common_reference_t<_It2>>;
  220. template<class _Fp, class _It1, class _It2 = _It1>
  221. concept indirect_strict_weak_order =
  222. indirectly_readable<_It1> && indirectly_readable<_It2> &&
  223. copy_constructible<_Fp> &&
  224. strict_weak_order<_Fp&, iter_value_t<_It1>&, iter_value_t<_It2>&> &&
  225. strict_weak_order<_Fp&, iter_value_t<_It1>&, iter_reference_t<_It2>> &&
  226. strict_weak_order<_Fp&, iter_reference_t<_It1>, iter_value_t<_It2>&> &&
  227. strict_weak_order<_Fp&, iter_reference_t<_It1>, iter_reference_t<_It2>> &&
  228. strict_weak_order<_Fp&, iter_common_reference_t<_It1>, iter_common_reference_t<_It2>>;
  229. template<class _Fp, class... _Its>
  230. requires (indirectly_readable<_Its> && ...) && invocable<_Fp, iter_reference_t<_Its>...>
  231. using indirect_result_t = invoke_result_t<_Fp, iter_reference_t<_Its>...>;
  232. template<class _In, class _Out>
  233. concept indirectly_movable =
  234. indirectly_readable<_In> &&
  235. indirectly_writable<_Out, iter_rvalue_reference_t<_In>>;
  236. template<class _In, class _Out>
  237. concept indirectly_movable_storable =
  238. indirectly_movable<_In, _Out> &&
  239. indirectly_writable<_Out, iter_value_t<_In>> &&
  240. movable<iter_value_t<_In>> &&
  241. constructible_from<iter_value_t<_In>, iter_rvalue_reference_t<_In>> &&
  242. assignable_from<iter_value_t<_In>&, iter_rvalue_reference_t<_In>>;
  243. template<class _In, class _Out>
  244. concept indirectly_copyable =
  245. indirectly_readable<_In> &&
  246. indirectly_writable<_Out, iter_reference_t<_In>>;
  247. template<class _In, class _Out>
  248. concept indirectly_copyable_storable =
  249. indirectly_copyable<_In, _Out> &&
  250. indirectly_writable<_Out, iter_value_t<_In>&> &&
  251. indirectly_writable<_Out, const iter_value_t<_In>&> &&
  252. indirectly_writable<_Out, iter_value_t<_In>&&> &&
  253. indirectly_writable<_Out, const iter_value_t<_In>&&> &&
  254. copyable<iter_value_t<_In>> &&
  255. constructible_from<iter_value_t<_In>, iter_reference_t<_In>> &&
  256. assignable_from<iter_value_t<_In>&, iter_reference_t<_In>>;
  257. // Note: indirectly_swappable is located in iter_swap.h to prevent a dependency cycle
  258. // (both iter_swap and indirectly_swappable require indirectly_readable).
  259. #endif // _LIBCPP_STD_VER >= 20
  260. template <class _Tp>
  261. using __has_random_access_iterator_category_or_concept
  262. #if _LIBCPP_STD_VER >= 20
  263. = integral_constant<bool, random_access_iterator<_Tp>>;
  264. #else // _LIBCPP_STD_VER < 20
  265. = __has_random_access_iterator_category<_Tp>;
  266. #endif // _LIBCPP_STD_VER
  267. _LIBCPP_END_NAMESPACE_STD
  268. #endif // _LIBCPP___ITERATOR_CONCEPTS_H