lazy_split_view.h 15 KB

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