lazy_split_view.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_LAZY_SPLIT_VIEW_H
  10. #define _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H
  11. #include <__algorithm/ranges_find.h>
  12. #include <__algorithm/ranges_mismatch.h>
  13. #include <__assert>
  14. #include <__concepts/constructible.h>
  15. #include <__concepts/convertible_to.h>
  16. #include <__concepts/derived_from.h>
  17. #include <__config>
  18. #include <__functional/bind_back.h>
  19. #include <__functional/ranges_operations.h>
  20. #include <__iterator/concepts.h>
  21. #include <__iterator/default_sentinel.h>
  22. #include <__iterator/incrementable_traits.h>
  23. #include <__iterator/indirectly_comparable.h>
  24. #include <__iterator/iter_move.h>
  25. #include <__iterator/iter_swap.h>
  26. #include <__iterator/iterator_traits.h>
  27. #include <__memory/addressof.h>
  28. #include <__ranges/access.h>
  29. #include <__ranges/all.h>
  30. #include <__ranges/concepts.h>
  31. #include <__ranges/non_propagating_cache.h>
  32. #include <__ranges/range_adaptor.h>
  33. #include <__ranges/single_view.h>
  34. #include <__ranges/subrange.h>
  35. #include <__ranges/view_interface.h>
  36. #include <__type_traits/conditional.h>
  37. #include <__type_traits/decay.h>
  38. #include <__type_traits/is_nothrow_constructible.h>
  39. #include <__type_traits/maybe_const.h>
  40. #include <__type_traits/remove_reference.h>
  41. #include <__utility/forward.h>
  42. #include <__utility/move.h>
  43. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  44. # pragma GCC system_header
  45. #endif
  46. _LIBCPP_PUSH_MACROS
  47. #include <__undef_macros>
  48. _LIBCPP_BEGIN_NAMESPACE_STD
  49. #if _LIBCPP_STD_VER >= 20
  50. namespace ranges {
  51. template <auto>
  52. struct __require_constant;
  53. template <class _Range>
  54. concept __tiny_range = sized_range<_Range> && requires {
  55. typename __require_constant<remove_reference_t<_Range>::size()>;
  56. } && (remove_reference_t<_Range>::size() <= 1);
  57. template <input_range _View, forward_range _Pattern>
  58. requires view<_View> && view<_Pattern> &&
  59. indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to> &&
  60. (forward_range<_View> || __tiny_range<_Pattern>)
  61. class lazy_split_view : public view_interface<lazy_split_view<_View, _Pattern>> {
  62. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  63. _LIBCPP_NO_UNIQUE_ADDRESS _Pattern __pattern_ = _Pattern();
  64. using _MaybeCurrent = _If<!forward_range<_View>, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
  65. _LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();
  66. template <bool>
  67. struct __outer_iterator;
  68. template <bool>
  69. struct __inner_iterator;
  70. public:
  71. _LIBCPP_HIDE_FROM_ABI lazy_split_view()
  72. requires default_initializable<_View> && default_initializable<_Pattern>
  73. = default;
  74. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 lazy_split_view(_View __base, _Pattern __pattern)
  75. : __base_(std::move(__base)), __pattern_(std::move(__pattern)) {}
  76. template <input_range _Range>
  77. requires constructible_from<_View, views::all_t<_Range>> &&
  78. constructible_from<_Pattern, single_view<range_value_t<_Range>>>
  79. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 lazy_split_view(_Range&& __r, range_value_t<_Range> __e)
  80. : __base_(views::all(std::forward<_Range>(__r))), __pattern_(views::single(std::move(__e))) {}
  81. _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
  82. requires copy_constructible<_View>
  83. {
  84. return __base_;
  85. }
  86. _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
  87. _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
  88. if constexpr (forward_range<_View>) {
  89. return __outer_iterator < __simple_view<_View> && __simple_view < _Pattern >> {*this, ranges::begin(__base_)};
  90. } else {
  91. __current_.__emplace(ranges::begin(__base_));
  92. return __outer_iterator<false>{*this};
  93. }
  94. }
  95. _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
  96. requires forward_range<_View> && forward_range<const _View>
  97. {
  98. return __outer_iterator<true>{*this, ranges::begin(__base_)};
  99. }
  100. _LIBCPP_HIDE_FROM_ABI constexpr auto end()
  101. requires forward_range<_View> && common_range<_View>
  102. {
  103. return __outer_iterator < __simple_view<_View> && __simple_view < _Pattern >> {*this, ranges::end(__base_)};
  104. }
  105. _LIBCPP_HIDE_FROM_ABI constexpr auto end() const {
  106. if constexpr (forward_range<_View> && forward_range<const _View> && common_range<const _View>) {
  107. return __outer_iterator<true>{*this, ranges::end(__base_)};
  108. } else {
  109. return default_sentinel;
  110. }
  111. }
  112. private:
  113. template <class>
  114. struct __outer_iterator_category {};
  115. template <forward_range _Tp>
  116. struct __outer_iterator_category<_Tp> {
  117. using iterator_category = input_iterator_tag;
  118. };
  119. template <bool _Const>
  120. struct __outer_iterator : __outer_iterator_category<__maybe_const<_Const, _View>> {
  121. private:
  122. template <bool>
  123. friend struct __inner_iterator;
  124. friend __outer_iterator<true>;
  125. using _Parent = __maybe_const<_Const, lazy_split_view>;
  126. using _Base = __maybe_const<_Const, _View>;
  127. _Parent* __parent_ = nullptr;
  128. using _MaybeCurrent = _If<forward_range<_View>, iterator_t<_Base>, __empty_cache>;
  129. _LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();
  130. bool __trailing_empty_ = false;
  131. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __current() noexcept {
  132. if constexpr (forward_range<_View>) {
  133. return __current_;
  134. } else {
  135. return *__parent_->__current_;
  136. }
  137. }
  138. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __current() const noexcept {
  139. if constexpr (forward_range<_View>) {
  140. return __current_;
  141. } else {
  142. return *__parent_->__current_;
  143. }
  144. }
  145. // Workaround for the GCC issue that doesn't allow calling `__parent_->__base_` from friend functions (because
  146. // `__base_` is private).
  147. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __parent_base() const noexcept { return __parent_->__base_; }
  148. public:
  149. // using iterator_category = inherited;
  150. using iterator_concept = conditional_t<forward_range<_Base>, forward_iterator_tag, input_iterator_tag>;
  151. using difference_type = range_difference_t<_Base>;
  152. struct value_type : view_interface<value_type> {
  153. private:
  154. __outer_iterator __i_ = __outer_iterator();
  155. public:
  156. _LIBCPP_HIDE_FROM_ABI value_type() = default;
  157. _LIBCPP_HIDE_FROM_ABI constexpr explicit value_type(__outer_iterator __i) : __i_(std::move(__i)) {}
  158. _LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator<_Const> begin() const { return __inner_iterator<_Const>{__i_}; }
  159. _LIBCPP_HIDE_FROM_ABI constexpr default_sentinel_t end() const noexcept { return default_sentinel; }
  160. };
  161. _LIBCPP_HIDE_FROM_ABI __outer_iterator() = default;
  162. _LIBCPP_HIDE_FROM_ABI constexpr explicit __outer_iterator(_Parent& __parent)
  163. requires(!forward_range<_Base>)
  164. : __parent_(std::addressof(__parent)) {}
  165. _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(_Parent& __parent, iterator_t<_Base> __current)
  166. requires forward_range<_Base>
  167. : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}
  168. _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(__outer_iterator<!_Const> __i)
  169. requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
  170. : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
  171. _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return value_type{*this}; }
  172. _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator& operator++() {
  173. const auto __end = ranges::end(__parent_->__base_);
  174. if (__current() == __end) {
  175. __trailing_empty_ = false;
  176. return *this;
  177. }
  178. const auto [__pbegin, __pend] = ranges::subrange{__parent_->__pattern_};
  179. if (__pbegin == __pend) {
  180. // Empty pattern: split on every element in the input range
  181. ++__current();
  182. } else if constexpr (__tiny_range<_Pattern>) {
  183. // One-element pattern: we can use `ranges::find`.
  184. __current() = ranges::find(std::move(__current()), __end, *__pbegin);
  185. if (__current() != __end) {
  186. // Make sure we point to after the separator we just found.
  187. ++__current();
  188. if (__current() == __end)
  189. __trailing_empty_ = true;
  190. }
  191. } else {
  192. // General case for n-element pattern.
  193. do {
  194. const auto [__b, __p] = ranges::mismatch(__current(), __end, __pbegin, __pend);
  195. if (__p == __pend) {
  196. __current() = __b;
  197. if (__current() == __end) {
  198. __trailing_empty_ = true;
  199. }
  200. break; // The pattern matched; skip it.
  201. }
  202. } while (++__current() != __end);
  203. }
  204. return *this;
  205. }
  206. _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {
  207. if constexpr (forward_range<_Base>) {
  208. auto __tmp = *this;
  209. ++*this;
  210. return __tmp;
  211. } else {
  212. ++*this;
  213. }
  214. }
  215. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, const __outer_iterator& __y)
  216. requires forward_range<_Base>
  217. {
  218. return __x.__current_ == __y.__current_ && __x.__trailing_empty_ == __y.__trailing_empty_;
  219. }
  220. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, default_sentinel_t) {
  221. _LIBCPP_ASSERT_NON_NULL(__x.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");
  222. return __x.__current() == ranges::end(__x.__parent_base()) && !__x.__trailing_empty_;
  223. }
  224. };
  225. template <class>
  226. struct __inner_iterator_category {};
  227. template <forward_range _Tp>
  228. struct __inner_iterator_category<_Tp> {
  229. using iterator_category =
  230. _If< derived_from<typename iterator_traits<iterator_t<_Tp>>::iterator_category, forward_iterator_tag>,
  231. forward_iterator_tag,
  232. typename iterator_traits<iterator_t<_Tp>>::iterator_category >;
  233. };
  234. template <bool _Const>
  235. struct __inner_iterator : __inner_iterator_category<__maybe_const<_Const, _View>> {
  236. private:
  237. using _Base = __maybe_const<_Const, _View>;
  238. // Workaround for a GCC issue.
  239. static constexpr bool _OuterConst = _Const;
  240. __outer_iterator<_Const> __i_ = __outer_iterator<_OuterConst>();
  241. bool __incremented_ = false;
  242. // Note: these private functions are necessary because GCC doesn't allow calls to private members of `__i_` from
  243. // free functions that are friends of `inner-iterator`.
  244. _LIBCPP_HIDE_FROM_ABI constexpr bool __is_done() const {
  245. _LIBCPP_ASSERT_NON_NULL(__i_.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");
  246. auto [__pcur, __pend] = ranges::subrange{__i_.__parent_->__pattern_};
  247. auto __end = ranges::end(__i_.__parent_->__base_);
  248. if constexpr (__tiny_range<_Pattern>) {
  249. const auto& __cur = __i_.__current();
  250. if (__cur == __end)
  251. return true;
  252. if (__pcur == __pend)
  253. return __incremented_;
  254. return *__cur == *__pcur;
  255. } else {
  256. auto __cur = __i_.__current();
  257. if (__cur == __end)
  258. return true;
  259. if (__pcur == __pend)
  260. return __incremented_;
  261. do {
  262. if (*__cur != *__pcur)
  263. return false;
  264. if (++__pcur == __pend)
  265. return true;
  266. } while (++__cur != __end);
  267. return false;
  268. }
  269. }
  270. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __outer_current() noexcept { return __i_.__current(); }
  271. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __outer_current() const noexcept {
  272. return __i_.__current();
  273. }
  274. public:
  275. // using iterator_category = inherited;
  276. using iterator_concept = typename __outer_iterator<_Const>::iterator_concept;
  277. using value_type = range_value_t<_Base>;
  278. using difference_type = range_difference_t<_Base>;
  279. _LIBCPP_HIDE_FROM_ABI __inner_iterator() = default;
  280. _LIBCPP_HIDE_FROM_ABI constexpr explicit __inner_iterator(__outer_iterator<_Const> __i) : __i_(std::move(__i)) {}
  281. _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __i_.__current(); }
  282. _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() &&
  283. requires forward_range<_View>
  284. {
  285. return std::move(__i_.__current());
  286. }
  287. _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const { return *__i_.__current(); }
  288. _LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator& operator++() {
  289. __incremented_ = true;
  290. if constexpr (!forward_range<_Base>) {
  291. if constexpr (_Pattern::size() == 0) {
  292. return *this;
  293. }
  294. }
  295. ++__i_.__current();
  296. return *this;
  297. }
  298. _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {
  299. if constexpr (forward_range<_Base>) {
  300. auto __tmp = *this;
  301. ++*this;
  302. return __tmp;
  303. } else {
  304. ++*this;
  305. }
  306. }
  307. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, const __inner_iterator& __y)
  308. requires forward_range<_Base>
  309. {
  310. return __x.__outer_current() == __y.__outer_current();
  311. }
  312. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, default_sentinel_t) {
  313. return __x.__is_done();
  314. }
  315. _LIBCPP_HIDE_FROM_ABI friend constexpr decltype(auto)
  316. iter_move(const __inner_iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__outer_current()))) {
  317. return ranges::iter_move(__i.__outer_current());
  318. }
  319. _LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(
  320. const __inner_iterator& __x,
  321. const __inner_iterator& __y) noexcept(noexcept(ranges::iter_swap(__x.__outer_current(), __y.__outer_current())))
  322. requires indirectly_swappable<iterator_t<_Base>>
  323. {
  324. ranges::iter_swap(__x.__outer_current(), __y.__outer_current());
  325. }
  326. };
  327. };
  328. template <class _Range, class _Pattern>
  329. lazy_split_view(_Range&&, _Pattern&&) -> lazy_split_view<views::all_t<_Range>, views::all_t<_Pattern>>;
  330. template <input_range _Range>
  331. lazy_split_view(_Range&&, range_value_t<_Range>)
  332. -> lazy_split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
  333. namespace views {
  334. namespace __lazy_split_view {
  335. struct __fn {
  336. template <class _Range, class _Pattern>
  337. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pattern&& __pattern) const
  338. noexcept(noexcept(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))))
  339. -> decltype(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))) {
  340. return lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern));
  341. }
  342. template <class _Pattern>
  343. requires constructible_from<decay_t<_Pattern>, _Pattern>
  344. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pattern&& __pattern) const
  345. noexcept(is_nothrow_constructible_v<decay_t<_Pattern>, _Pattern>) {
  346. return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pattern>(__pattern)));
  347. }
  348. };
  349. } // namespace __lazy_split_view
  350. inline namespace __cpo {
  351. inline constexpr auto lazy_split = __lazy_split_view::__fn{};
  352. } // namespace __cpo
  353. } // namespace views
  354. } // namespace ranges
  355. #endif // _LIBCPP_STD_VER >= 20
  356. _LIBCPP_END_NAMESPACE_STD
  357. _LIBCPP_POP_MACROS
  358. #endif // _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H