subrange.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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___RANGES_SUBRANGE_H
  10. #define _LIBCPP___RANGES_SUBRANGE_H
  11. #include <__assert>
  12. #include <__concepts/constructible.h>
  13. #include <__concepts/convertible_to.h>
  14. #include <__concepts/copyable.h>
  15. #include <__concepts/derived_from.h>
  16. #include <__concepts/different_from.h>
  17. #include <__config>
  18. #include <__fwd/get.h>
  19. #include <__fwd/subrange.h>
  20. #include <__iterator/advance.h>
  21. #include <__iterator/concepts.h>
  22. #include <__iterator/incrementable_traits.h>
  23. #include <__iterator/iterator_traits.h>
  24. #include <__ranges/access.h>
  25. #include <__ranges/concepts.h>
  26. #include <__ranges/dangling.h>
  27. #include <__ranges/enable_borrowed_range.h>
  28. #include <__ranges/size.h>
  29. #include <__ranges/view_interface.h>
  30. #include <__tuple/pair_like.h>
  31. #include <__tuple/tuple_element.h>
  32. #include <__tuple/tuple_size.h>
  33. #include <__type_traits/conditional.h>
  34. #include <__type_traits/decay.h>
  35. #include <__type_traits/is_pointer.h>
  36. #include <__type_traits/is_reference.h>
  37. #include <__type_traits/make_unsigned.h>
  38. #include <__type_traits/remove_const.h>
  39. #include <__type_traits/remove_pointer.h>
  40. #include <__utility/move.h>
  41. #include <cstddef>
  42. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  43. # pragma GCC system_header
  44. #endif
  45. _LIBCPP_PUSH_MACROS
  46. #include <__undef_macros>
  47. _LIBCPP_BEGIN_NAMESPACE_STD
  48. #if _LIBCPP_STD_VER >= 20
  49. namespace ranges {
  50. template<class _From, class _To>
  51. concept __uses_nonqualification_pointer_conversion =
  52. is_pointer_v<_From> && is_pointer_v<_To> &&
  53. !convertible_to<remove_pointer_t<_From>(*)[], remove_pointer_t<_To>(*)[]>;
  54. template<class _From, class _To>
  55. concept __convertible_to_non_slicing =
  56. convertible_to<_From, _To> &&
  57. !__uses_nonqualification_pointer_conversion<decay_t<_From>, decay_t<_To>>;
  58. template<class _Pair, class _Iter, class _Sent>
  59. concept __pair_like_convertible_from =
  60. !range<_Pair> && __pair_like<_Pair> &&
  61. constructible_from<_Pair, _Iter, _Sent> &&
  62. __convertible_to_non_slicing<_Iter, tuple_element_t<0, _Pair>> &&
  63. convertible_to<_Sent, tuple_element_t<1, _Pair>>;
  64. template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent = _Iter,
  65. subrange_kind _Kind = sized_sentinel_for<_Sent, _Iter>
  66. ? subrange_kind::sized
  67. : subrange_kind::unsized>
  68. requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _Iter>)
  69. class _LIBCPP_TEMPLATE_VIS subrange
  70. : public view_interface<subrange<_Iter, _Sent, _Kind>>
  71. {
  72. public:
  73. // Note: this is an internal implementation detail that is public only for internal usage.
  74. static constexpr bool _StoreSize = (_Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _Iter>);
  75. private:
  76. static constexpr bool _MustProvideSizeAtConstruction = !_StoreSize; // just to improve compiler diagnostics
  77. struct _Empty { _LIBCPP_HIDE_FROM_ABI constexpr _Empty(auto) noexcept { } };
  78. using _Size = conditional_t<_StoreSize, make_unsigned_t<iter_difference_t<_Iter>>, _Empty>;
  79. _LIBCPP_NO_UNIQUE_ADDRESS _Iter __begin_ = _Iter();
  80. _LIBCPP_NO_UNIQUE_ADDRESS _Sent __end_ = _Sent();
  81. _LIBCPP_NO_UNIQUE_ADDRESS _Size __size_ = 0;
  82. public:
  83. _LIBCPP_HIDE_FROM_ABI
  84. subrange() requires default_initializable<_Iter> = default;
  85. _LIBCPP_HIDE_FROM_ABI
  86. constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent)
  87. requires _MustProvideSizeAtConstruction
  88. : __begin_(std::move(__iter)), __end_(std::move(__sent))
  89. { }
  90. _LIBCPP_HIDE_FROM_ABI
  91. constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent,
  92. make_unsigned_t<iter_difference_t<_Iter>> __n)
  93. requires (_Kind == subrange_kind::sized)
  94. : __begin_(std::move(__iter)), __end_(std::move(__sent)), __size_(__n)
  95. {
  96. if constexpr (sized_sentinel_for<_Sent, _Iter>)
  97. _LIBCPP_ASSERT_UNCATEGORIZED((__end_ - __begin_) == static_cast<iter_difference_t<_Iter>>(__n),
  98. "std::ranges::subrange was passed an invalid size hint");
  99. }
  100. template<__different_from<subrange> _Range>
  101. requires borrowed_range<_Range> &&
  102. __convertible_to_non_slicing<iterator_t<_Range>, _Iter> &&
  103. convertible_to<sentinel_t<_Range>, _Sent>
  104. _LIBCPP_HIDE_FROM_ABI
  105. constexpr subrange(_Range&& __range)
  106. requires (!_StoreSize)
  107. : subrange(ranges::begin(__range), ranges::end(__range))
  108. { }
  109. template<__different_from<subrange> _Range>
  110. requires borrowed_range<_Range> &&
  111. __convertible_to_non_slicing<iterator_t<_Range>, _Iter> &&
  112. convertible_to<sentinel_t<_Range>, _Sent>
  113. _LIBCPP_HIDE_FROM_ABI
  114. constexpr subrange(_Range&& __range)
  115. requires _StoreSize && sized_range<_Range>
  116. : subrange(__range, ranges::size(__range))
  117. { }
  118. template<borrowed_range _Range>
  119. requires __convertible_to_non_slicing<iterator_t<_Range>, _Iter> &&
  120. convertible_to<sentinel_t<_Range>, _Sent>
  121. _LIBCPP_HIDE_FROM_ABI
  122. constexpr subrange(_Range&& __range, make_unsigned_t<iter_difference_t<_Iter>> __n)
  123. requires (_Kind == subrange_kind::sized)
  124. : subrange(ranges::begin(__range), ranges::end(__range), __n)
  125. { }
  126. template<__different_from<subrange> _Pair>
  127. requires __pair_like_convertible_from<_Pair, const _Iter&, const _Sent&>
  128. _LIBCPP_HIDE_FROM_ABI
  129. constexpr operator _Pair() const {
  130. return _Pair(__begin_, __end_);
  131. }
  132. _LIBCPP_HIDE_FROM_ABI
  133. constexpr _Iter begin() const requires copyable<_Iter> {
  134. return __begin_;
  135. }
  136. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Iter begin() requires (!copyable<_Iter>) {
  137. return std::move(__begin_);
  138. }
  139. _LIBCPP_HIDE_FROM_ABI
  140. constexpr _Sent end() const {
  141. return __end_;
  142. }
  143. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const {
  144. return __begin_ == __end_;
  145. }
  146. _LIBCPP_HIDE_FROM_ABI
  147. constexpr make_unsigned_t<iter_difference_t<_Iter>> size() const
  148. requires (_Kind == subrange_kind::sized)
  149. {
  150. if constexpr (_StoreSize)
  151. return __size_;
  152. else
  153. return std::__to_unsigned_like(__end_ - __begin_);
  154. }
  155. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange next(iter_difference_t<_Iter> __n = 1) const&
  156. requires forward_iterator<_Iter>
  157. {
  158. auto __tmp = *this;
  159. __tmp.advance(__n);
  160. return __tmp;
  161. }
  162. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange next(iter_difference_t<_Iter> __n = 1) && {
  163. advance(__n);
  164. return std::move(*this);
  165. }
  166. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange prev(iter_difference_t<_Iter> __n = 1) const
  167. requires bidirectional_iterator<_Iter>
  168. {
  169. auto __tmp = *this;
  170. __tmp.advance(-__n);
  171. return __tmp;
  172. }
  173. _LIBCPP_HIDE_FROM_ABI
  174. constexpr subrange& advance(iter_difference_t<_Iter> __n) {
  175. if constexpr (bidirectional_iterator<_Iter>) {
  176. if (__n < 0) {
  177. ranges::advance(__begin_, __n);
  178. if constexpr (_StoreSize)
  179. __size_ += std::__to_unsigned_like(-__n);
  180. return *this;
  181. }
  182. }
  183. auto __d = __n - ranges::advance(__begin_, __n, __end_);
  184. if constexpr (_StoreSize)
  185. __size_ -= std::__to_unsigned_like(__d);
  186. return *this;
  187. }
  188. };
  189. template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent>
  190. subrange(_Iter, _Sent) -> subrange<_Iter, _Sent>;
  191. template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent>
  192. subrange(_Iter, _Sent, make_unsigned_t<iter_difference_t<_Iter>>)
  193. -> subrange<_Iter, _Sent, subrange_kind::sized>;
  194. template<borrowed_range _Range>
  195. subrange(_Range&&) -> subrange<iterator_t<_Range>, sentinel_t<_Range>,
  196. (sized_range<_Range> || sized_sentinel_for<sentinel_t<_Range>, iterator_t<_Range>>)
  197. ? subrange_kind::sized : subrange_kind::unsized>;
  198. template<borrowed_range _Range>
  199. subrange(_Range&&, make_unsigned_t<range_difference_t<_Range>>)
  200. -> subrange<iterator_t<_Range>, sentinel_t<_Range>, subrange_kind::sized>;
  201. template<size_t _Index, class _Iter, class _Sent, subrange_kind _Kind>
  202. requires ((_Index == 0 && copyable<_Iter>) || _Index == 1)
  203. _LIBCPP_HIDE_FROM_ABI
  204. constexpr auto get(const subrange<_Iter, _Sent, _Kind>& __subrange) {
  205. if constexpr (_Index == 0)
  206. return __subrange.begin();
  207. else
  208. return __subrange.end();
  209. }
  210. template<size_t _Index, class _Iter, class _Sent, subrange_kind _Kind>
  211. requires (_Index < 2)
  212. _LIBCPP_HIDE_FROM_ABI
  213. constexpr auto get(subrange<_Iter, _Sent, _Kind>&& __subrange) {
  214. if constexpr (_Index == 0)
  215. return __subrange.begin();
  216. else
  217. return __subrange.end();
  218. }
  219. template<class _Ip, class _Sp, subrange_kind _Kp>
  220. inline constexpr bool enable_borrowed_range<subrange<_Ip, _Sp, _Kp>> = true;
  221. template<range _Rp>
  222. using borrowed_subrange_t = _If<borrowed_range<_Rp>, subrange<iterator_t<_Rp>>, dangling>;
  223. } // namespace ranges
  224. // [range.subrange.general]
  225. using ranges::get;
  226. // [ranges.syn]
  227. template<class _Ip, class _Sp, ranges::subrange_kind _Kp>
  228. struct tuple_size<ranges::subrange<_Ip, _Sp, _Kp>> : integral_constant<size_t, 2> {};
  229. template<class _Ip, class _Sp, ranges::subrange_kind _Kp>
  230. struct tuple_element<0, ranges::subrange<_Ip, _Sp, _Kp>> {
  231. using type = _Ip;
  232. };
  233. template<class _Ip, class _Sp, ranges::subrange_kind _Kp>
  234. struct tuple_element<1, ranges::subrange<_Ip, _Sp, _Kp>> {
  235. using type = _Sp;
  236. };
  237. template<class _Ip, class _Sp, ranges::subrange_kind _Kp>
  238. struct tuple_element<0, const ranges::subrange<_Ip, _Sp, _Kp>> {
  239. using type = _Ip;
  240. };
  241. template<class _Ip, class _Sp, ranges::subrange_kind _Kp>
  242. struct tuple_element<1, const ranges::subrange<_Ip, _Sp, _Kp>> {
  243. using type = _Sp;
  244. };
  245. #endif // _LIBCPP_STD_VER >= 20
  246. _LIBCPP_END_NAMESPACE_STD
  247. _LIBCPP_POP_MACROS
  248. #endif // _LIBCPP___RANGES_SUBRANGE_H