join_view.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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_JOIN_VIEW_H
  10. #define _LIBCPP___RANGES_JOIN_VIEW_H
  11. #include <__concepts/constructible.h>
  12. #include <__concepts/convertible_to.h>
  13. #include <__concepts/copyable.h>
  14. #include <__concepts/derived_from.h>
  15. #include <__concepts/equality_comparable.h>
  16. #include <__config>
  17. #include <__iterator/concepts.h>
  18. #include <__iterator/iter_move.h>
  19. #include <__iterator/iter_swap.h>
  20. #include <__iterator/iterator_traits.h>
  21. #include <__iterator/iterator_with_data.h>
  22. #include <__iterator/segmented_iterator.h>
  23. #include <__memory/addressof.h>
  24. #include <__ranges/access.h>
  25. #include <__ranges/all.h>
  26. #include <__ranges/concepts.h>
  27. #include <__ranges/empty.h>
  28. #include <__ranges/non_propagating_cache.h>
  29. #include <__ranges/range_adaptor.h>
  30. #include <__ranges/view_interface.h>
  31. #include <__type_traits/common_type.h>
  32. #include <__type_traits/maybe_const.h>
  33. #include <__utility/forward.h>
  34. #include <optional>
  35. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  36. # pragma GCC system_header
  37. #endif
  38. _LIBCPP_BEGIN_NAMESPACE_STD
  39. // Note: `join_view` is still marked experimental because there is an ABI-breaking change that affects `join_view` in
  40. // the pipeline (https://isocpp.org/files/papers/D2770R0.html).
  41. // TODO: make `join_view` non-experimental once D2770 is implemented.
  42. #if _LIBCPP_STD_VER >= 20
  43. namespace ranges {
  44. template<class>
  45. struct __join_view_iterator_category {};
  46. template<class _View>
  47. requires is_reference_v<range_reference_t<_View>> &&
  48. forward_range<_View> &&
  49. forward_range<range_reference_t<_View>>
  50. struct __join_view_iterator_category<_View> {
  51. using _OuterC = typename iterator_traits<iterator_t<_View>>::iterator_category;
  52. using _InnerC = typename iterator_traits<iterator_t<range_reference_t<_View>>>::iterator_category;
  53. using iterator_category = _If<
  54. derived_from<_OuterC, bidirectional_iterator_tag> && derived_from<_InnerC, bidirectional_iterator_tag> &&
  55. common_range<range_reference_t<_View>>,
  56. bidirectional_iterator_tag,
  57. _If<
  58. derived_from<_OuterC, forward_iterator_tag> && derived_from<_InnerC, forward_iterator_tag>,
  59. forward_iterator_tag,
  60. input_iterator_tag
  61. >
  62. >;
  63. };
  64. template<input_range _View>
  65. requires view<_View> && input_range<range_reference_t<_View>>
  66. class join_view
  67. : public view_interface<join_view<_View>> {
  68. private:
  69. using _InnerRange = range_reference_t<_View>;
  70. template<bool> struct __iterator;
  71. template<bool> struct __sentinel;
  72. template <class>
  73. friend struct std::__segmented_iterator_traits;
  74. static constexpr bool _UseCache = !is_reference_v<_InnerRange>;
  75. using _Cache = _If<_UseCache, __non_propagating_cache<remove_cvref_t<_InnerRange>>, __empty_cache>;
  76. _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cache_;
  77. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  78. public:
  79. _LIBCPP_HIDE_FROM_ABI
  80. join_view() requires default_initializable<_View> = default;
  81. _LIBCPP_HIDE_FROM_ABI
  82. constexpr explicit join_view(_View __base)
  83. : __base_(std::move(__base)) {}
  84. _LIBCPP_HIDE_FROM_ABI
  85. constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
  86. _LIBCPP_HIDE_FROM_ABI
  87. constexpr _View base() && { return std::move(__base_); }
  88. _LIBCPP_HIDE_FROM_ABI
  89. constexpr auto begin() {
  90. constexpr bool __use_const = __simple_view<_View> &&
  91. is_reference_v<range_reference_t<_View>>;
  92. return __iterator<__use_const>{*this, ranges::begin(__base_)};
  93. }
  94. template<class _V2 = _View>
  95. _LIBCPP_HIDE_FROM_ABI
  96. constexpr auto begin() const
  97. requires input_range<const _V2> &&
  98. is_reference_v<range_reference_t<const _V2>>
  99. {
  100. return __iterator<true>{*this, ranges::begin(__base_)};
  101. }
  102. _LIBCPP_HIDE_FROM_ABI
  103. constexpr auto end() {
  104. if constexpr (forward_range<_View> &&
  105. is_reference_v<_InnerRange> &&
  106. forward_range<_InnerRange> &&
  107. common_range<_View> &&
  108. common_range<_InnerRange>)
  109. return __iterator<__simple_view<_View>>{*this, ranges::end(__base_)};
  110. else
  111. return __sentinel<__simple_view<_View>>{*this};
  112. }
  113. template<class _V2 = _View>
  114. _LIBCPP_HIDE_FROM_ABI
  115. constexpr auto end() const
  116. requires input_range<const _V2> &&
  117. is_reference_v<range_reference_t<const _V2>>
  118. {
  119. using _ConstInnerRange = range_reference_t<const _View>;
  120. if constexpr (forward_range<const _View> &&
  121. is_reference_v<_ConstInnerRange> &&
  122. forward_range<_ConstInnerRange> &&
  123. common_range<const _View> &&
  124. common_range<_ConstInnerRange>) {
  125. return __iterator<true>{*this, ranges::end(__base_)};
  126. } else {
  127. return __sentinel<true>{*this};
  128. }
  129. }
  130. };
  131. template<input_range _View>
  132. requires view<_View> && input_range<range_reference_t<_View>>
  133. template<bool _Const>
  134. struct join_view<_View>::__sentinel {
  135. template<bool>
  136. friend struct __sentinel;
  137. private:
  138. using _Parent = __maybe_const<_Const, join_view<_View>>;
  139. using _Base = __maybe_const<_Const, _View>;
  140. sentinel_t<_Base> __end_ = sentinel_t<_Base>();
  141. public:
  142. _LIBCPP_HIDE_FROM_ABI
  143. __sentinel() = default;
  144. _LIBCPP_HIDE_FROM_ABI
  145. constexpr explicit __sentinel(_Parent& __parent)
  146. : __end_(ranges::end(__parent.__base_)) {}
  147. _LIBCPP_HIDE_FROM_ABI
  148. constexpr __sentinel(__sentinel<!_Const> __s)
  149. requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
  150. : __end_(std::move(__s.__end_)) {}
  151. template<bool _OtherConst>
  152. requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  153. _LIBCPP_HIDE_FROM_ABI
  154. friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
  155. return __x.__outer_ == __y.__end_;
  156. }
  157. };
  158. // https://reviews.llvm.org/D142811#inline-1383022
  159. // To simplify the segmented iterator traits specialization,
  160. // make the iterator `final`
  161. template<input_range _View>
  162. requires view<_View> && input_range<range_reference_t<_View>>
  163. template<bool _Const>
  164. struct join_view<_View>::__iterator final
  165. : public __join_view_iterator_category<__maybe_const<_Const, _View>> {
  166. template<bool>
  167. friend struct __iterator;
  168. template <class>
  169. friend struct std::__segmented_iterator_traits;
  170. static constexpr bool __is_join_view_iterator = true;
  171. private:
  172. using _Parent = __maybe_const<_Const, join_view<_View>>;
  173. using _Base = __maybe_const<_Const, _View>;
  174. using _Outer = iterator_t<_Base>;
  175. using _Inner = iterator_t<range_reference_t<_Base>>;
  176. using _InnerRange = range_reference_t<_View>;
  177. static constexpr bool __ref_is_glvalue = is_reference_v<range_reference_t<_Base>>;
  178. public:
  179. _Outer __outer_ = _Outer();
  180. private:
  181. optional<_Inner> __inner_;
  182. _Parent *__parent_ = nullptr;
  183. _LIBCPP_HIDE_FROM_ABI
  184. constexpr void __satisfy() {
  185. for (; __outer_ != ranges::end(__parent_->__base_); ++__outer_) {
  186. auto&& __inner = [&]() -> auto&& {
  187. if constexpr (__ref_is_glvalue)
  188. return *__outer_;
  189. else
  190. return __parent_->__cache_.__emplace_from([&]() -> decltype(auto) { return *__outer_; });
  191. }();
  192. __inner_ = ranges::begin(__inner);
  193. if (*__inner_ != ranges::end(__inner))
  194. return;
  195. }
  196. if constexpr (__ref_is_glvalue)
  197. __inner_.reset();
  198. }
  199. _LIBCPP_HIDE_FROM_ABI constexpr __iterator(_Parent* __parent, _Outer __outer, _Inner __inner)
  200. : __outer_(std::move(__outer)), __inner_(std::move(__inner)), __parent_(__parent) {}
  201. public:
  202. using iterator_concept = _If<
  203. __ref_is_glvalue && bidirectional_range<_Base> && bidirectional_range<range_reference_t<_Base>> &&
  204. common_range<range_reference_t<_Base>>,
  205. bidirectional_iterator_tag,
  206. _If<
  207. __ref_is_glvalue && forward_range<_Base> && forward_range<range_reference_t<_Base>>,
  208. forward_iterator_tag,
  209. input_iterator_tag
  210. >
  211. >;
  212. using value_type = range_value_t<range_reference_t<_Base>>;
  213. using difference_type = common_type_t<
  214. range_difference_t<_Base>, range_difference_t<range_reference_t<_Base>>>;
  215. _LIBCPP_HIDE_FROM_ABI
  216. __iterator() requires default_initializable<_Outer> = default;
  217. _LIBCPP_HIDE_FROM_ABI
  218. constexpr __iterator(_Parent& __parent, _Outer __outer)
  219. : __outer_(std::move(__outer))
  220. , __parent_(std::addressof(__parent)) {
  221. __satisfy();
  222. }
  223. _LIBCPP_HIDE_FROM_ABI
  224. constexpr __iterator(__iterator<!_Const> __i)
  225. requires _Const &&
  226. convertible_to<iterator_t<_View>, _Outer> &&
  227. convertible_to<iterator_t<_InnerRange>, _Inner>
  228. : __outer_(std::move(__i.__outer_))
  229. , __inner_(std::move(__i.__inner_))
  230. , __parent_(__i.__parent_) {}
  231. _LIBCPP_HIDE_FROM_ABI
  232. constexpr decltype(auto) operator*() const {
  233. return **__inner_;
  234. }
  235. _LIBCPP_HIDE_FROM_ABI
  236. constexpr _Inner operator->() const
  237. requires __has_arrow<_Inner> && copyable<_Inner>
  238. {
  239. return *__inner_;
  240. }
  241. _LIBCPP_HIDE_FROM_ABI
  242. constexpr __iterator& operator++() {
  243. auto&& __inner = [&]() -> auto&& {
  244. if constexpr (__ref_is_glvalue)
  245. return *__outer_;
  246. else
  247. return *__parent_->__cache_;
  248. }();
  249. if (++*__inner_ == ranges::end(__inner)) {
  250. ++__outer_;
  251. __satisfy();
  252. }
  253. return *this;
  254. }
  255. _LIBCPP_HIDE_FROM_ABI
  256. constexpr void operator++(int) {
  257. ++*this;
  258. }
  259. _LIBCPP_HIDE_FROM_ABI
  260. constexpr __iterator operator++(int)
  261. requires __ref_is_glvalue &&
  262. forward_range<_Base> &&
  263. forward_range<range_reference_t<_Base>>
  264. {
  265. auto __tmp = *this;
  266. ++*this;
  267. return __tmp;
  268. }
  269. _LIBCPP_HIDE_FROM_ABI
  270. constexpr __iterator& operator--()
  271. requires __ref_is_glvalue &&
  272. bidirectional_range<_Base> &&
  273. bidirectional_range<range_reference_t<_Base>> &&
  274. common_range<range_reference_t<_Base>>
  275. {
  276. if (__outer_ == ranges::end(__parent_->__base_))
  277. __inner_ = ranges::end(*--__outer_);
  278. // Skip empty inner ranges when going backwards.
  279. while (*__inner_ == ranges::begin(*__outer_)) {
  280. __inner_ = ranges::end(*--__outer_);
  281. }
  282. --*__inner_;
  283. return *this;
  284. }
  285. _LIBCPP_HIDE_FROM_ABI
  286. constexpr __iterator operator--(int)
  287. requires __ref_is_glvalue &&
  288. bidirectional_range<_Base> &&
  289. bidirectional_range<range_reference_t<_Base>> &&
  290. common_range<range_reference_t<_Base>>
  291. {
  292. auto __tmp = *this;
  293. --*this;
  294. return __tmp;
  295. }
  296. _LIBCPP_HIDE_FROM_ABI
  297. friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
  298. requires __ref_is_glvalue &&
  299. equality_comparable<iterator_t<_Base>> &&
  300. equality_comparable<iterator_t<range_reference_t<_Base>>>
  301. {
  302. return __x.__outer_ == __y.__outer_ && __x.__inner_ == __y.__inner_;
  303. }
  304. _LIBCPP_HIDE_FROM_ABI
  305. friend constexpr decltype(auto) iter_move(const __iterator& __i)
  306. noexcept(noexcept(ranges::iter_move(*__i.__inner_)))
  307. {
  308. return ranges::iter_move(*__i.__inner_);
  309. }
  310. _LIBCPP_HIDE_FROM_ABI
  311. friend constexpr void iter_swap(const __iterator& __x, const __iterator& __y)
  312. noexcept(noexcept(ranges::iter_swap(*__x.__inner_, *__y.__inner_)))
  313. requires indirectly_swappable<_Inner>
  314. {
  315. return ranges::iter_swap(*__x.__inner_, *__y.__inner_);
  316. }
  317. };
  318. template<class _Range>
  319. explicit join_view(_Range&&) -> join_view<views::all_t<_Range>>;
  320. namespace views {
  321. namespace __join_view {
  322. struct __fn : __range_adaptor_closure<__fn> {
  323. template<class _Range>
  324. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  325. constexpr auto operator()(_Range&& __range) const
  326. noexcept(noexcept(join_view<all_t<_Range&&>>(std::forward<_Range>(__range))))
  327. -> decltype( join_view<all_t<_Range&&>>(std::forward<_Range>(__range)))
  328. { return join_view<all_t<_Range&&>>(std::forward<_Range>(__range)); }
  329. };
  330. } // namespace __join_view
  331. inline namespace __cpo {
  332. inline constexpr auto join = __join_view::__fn{};
  333. } // namespace __cpo
  334. } // namespace views
  335. } // namespace ranges
  336. template <class _JoinViewIterator>
  337. requires(_JoinViewIterator::__is_join_view_iterator &&
  338. ranges::common_range<typename _JoinViewIterator::_Parent> &&
  339. __has_random_access_iterator_category<typename _JoinViewIterator::_Outer>::value &&
  340. __has_random_access_iterator_category<typename _JoinViewIterator::_Inner>::value)
  341. struct __segmented_iterator_traits<_JoinViewIterator> {
  342. using __segment_iterator =
  343. _LIBCPP_NODEBUG __iterator_with_data<typename _JoinViewIterator::_Outer, typename _JoinViewIterator::_Parent*>;
  344. using __local_iterator = typename _JoinViewIterator::_Inner;
  345. // TODO: Would it make sense to enable the optimization for other iterator types?
  346. static constexpr _LIBCPP_HIDE_FROM_ABI __segment_iterator __segment(_JoinViewIterator __iter) {
  347. if (ranges::empty(__iter.__parent_->__base_))
  348. return {};
  349. if (!__iter.__inner_.has_value())
  350. return __segment_iterator(--__iter.__outer_, __iter.__parent_);
  351. return __segment_iterator(__iter.__outer_, __iter.__parent_);
  352. }
  353. static constexpr _LIBCPP_HIDE_FROM_ABI __local_iterator __local(_JoinViewIterator __iter) {
  354. if (ranges::empty(__iter.__parent_->__base_))
  355. return {};
  356. if (!__iter.__inner_.has_value())
  357. return ranges::end(*--__iter.__outer_);
  358. return *__iter.__inner_;
  359. }
  360. static constexpr _LIBCPP_HIDE_FROM_ABI __local_iterator __begin(__segment_iterator __iter) {
  361. return ranges::begin(*__iter.__get_iter());
  362. }
  363. static constexpr _LIBCPP_HIDE_FROM_ABI __local_iterator __end(__segment_iterator __iter) {
  364. return ranges::end(*__iter.__get_iter());
  365. }
  366. static constexpr _LIBCPP_HIDE_FROM_ABI _JoinViewIterator
  367. __compose(__segment_iterator __seg_iter, __local_iterator __local_iter) {
  368. return _JoinViewIterator(
  369. std::move(__seg_iter).__get_data(), std::move(__seg_iter).__get_iter(), std::move(__local_iter));
  370. }
  371. };
  372. #endif // #if _LIBCPP_STD_VER >= 20 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
  373. _LIBCPP_END_NAMESPACE_STD
  374. #endif // _LIBCPP___RANGES_JOIN_VIEW_H