split_view.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_SPLIT_VIEW_H
  10. #define _LIBCPP___RANGES_SPLIT_VIEW_H
  11. #include <__algorithm/ranges_search.h>
  12. #include <__concepts/constructible.h>
  13. #include <__config>
  14. #include <__functional/bind_back.h>
  15. #include <__functional/ranges_operations.h>
  16. #include <__iterator/indirectly_comparable.h>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__memory/addressof.h>
  19. #include <__ranges/access.h>
  20. #include <__ranges/all.h>
  21. #include <__ranges/concepts.h>
  22. #include <__ranges/empty.h>
  23. #include <__ranges/non_propagating_cache.h>
  24. #include <__ranges/range_adaptor.h>
  25. #include <__ranges/single_view.h>
  26. #include <__ranges/subrange.h>
  27. #include <__ranges/view_interface.h>
  28. #include <__type_traits/decay.h>
  29. #include <__type_traits/is_nothrow_constructible.h>
  30. #include <__utility/forward.h>
  31. #include <__utility/move.h>
  32. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  33. # pragma GCC system_header
  34. #endif
  35. _LIBCPP_BEGIN_NAMESPACE_STD
  36. #if _LIBCPP_STD_VER >= 20
  37. namespace ranges {
  38. template <forward_range _View, forward_range _Pattern>
  39. requires view<_View> && view<_Pattern> &&
  40. indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to>
  41. class split_view : public view_interface<split_view<_View, _Pattern>> {
  42. private:
  43. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  44. _LIBCPP_NO_UNIQUE_ADDRESS _Pattern __pattern_ = _Pattern();
  45. using _Cache = __non_propagating_cache<subrange<iterator_t<_View>>>;
  46. _Cache __cached_begin_ = _Cache();
  47. template <class, class>
  48. friend struct __iterator;
  49. template <class, class>
  50. friend struct __sentinel;
  51. struct __iterator;
  52. struct __sentinel;
  53. _LIBCPP_HIDE_FROM_ABI constexpr subrange<iterator_t<_View>> __find_next(iterator_t<_View> __it) {
  54. auto [__begin, __end] = ranges::search(subrange(__it, ranges::end(__base_)), __pattern_);
  55. if (__begin != ranges::end(__base_) && ranges::empty(__pattern_)) {
  56. ++__begin;
  57. ++__end;
  58. }
  59. return {__begin, __end};
  60. }
  61. public:
  62. _LIBCPP_HIDE_FROM_ABI split_view()
  63. requires default_initializable<_View> && default_initializable<_Pattern>
  64. = default;
  65. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 split_view(_View __base, _Pattern __pattern)
  66. : __base_(std::move(__base)), __pattern_(std::move((__pattern))) {}
  67. template <forward_range _Range>
  68. requires constructible_from<_View, views::all_t<_Range>> &&
  69. constructible_from<_Pattern, single_view<range_value_t<_Range>>>
  70. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23
  71. split_view(_Range&& __range, range_value_t<_Range> __elem)
  72. : __base_(views::all(std::forward<_Range>(__range))), __pattern_(views::single(std::move(__elem))) {}
  73. _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
  74. requires copy_constructible<_View>
  75. {
  76. return __base_;
  77. }
  78. _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
  79. _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() {
  80. if (!__cached_begin_.__has_value()) {
  81. __cached_begin_.__emplace(__find_next(ranges::begin(__base_)));
  82. }
  83. return {*this, ranges::begin(__base_), *__cached_begin_};
  84. }
  85. _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
  86. if constexpr (common_range<_View>) {
  87. return __iterator{*this, ranges::end(__base_), {}};
  88. } else {
  89. return __sentinel{*this};
  90. }
  91. }
  92. };
  93. template <class _Range, class _Pattern>
  94. split_view(_Range&&, _Pattern&&) -> split_view<views::all_t<_Range>, views::all_t<_Pattern>>;
  95. template <forward_range _Range>
  96. split_view(_Range&&, range_value_t<_Range>) -> split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
  97. template <forward_range _View, forward_range _Pattern>
  98. requires view<_View> && view<_Pattern> &&
  99. indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to>
  100. struct split_view<_View, _Pattern>::__iterator {
  101. private:
  102. split_view* __parent_ = nullptr;
  103. _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __cur_ = iterator_t<_View>();
  104. _LIBCPP_NO_UNIQUE_ADDRESS subrange<iterator_t<_View>> __next_ = subrange<iterator_t<_View>>();
  105. bool __trailing_empty_ = false;
  106. friend struct __sentinel;
  107. public:
  108. using iterator_concept = forward_iterator_tag;
  109. using iterator_category = input_iterator_tag;
  110. using value_type = subrange<iterator_t<_View>>;
  111. using difference_type = range_difference_t<_View>;
  112. _LIBCPP_HIDE_FROM_ABI __iterator() = default;
  113. _LIBCPP_HIDE_FROM_ABI constexpr __iterator(
  114. split_view<_View, _Pattern>& __parent, iterator_t<_View> __current, subrange<iterator_t<_View>> __next)
  115. : __parent_(std::addressof(__parent)), __cur_(std::move(__current)), __next_(std::move(__next)) {}
  116. _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() const { return __cur_; }
  117. _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return {__cur_, __next_.begin()}; }
  118. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
  119. __cur_ = __next_.begin();
  120. if (__cur_ != ranges::end(__parent_->__base_)) {
  121. __cur_ = __next_.end();
  122. if (__cur_ == ranges::end(__parent_->__base_)) {
  123. __trailing_empty_ = true;
  124. __next_ = {__cur_, __cur_};
  125. } else {
  126. __next_ = __parent_->__find_next(__cur_);
  127. }
  128. } else {
  129. __trailing_empty_ = false;
  130. }
  131. return *this;
  132. }
  133. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {
  134. auto __tmp = *this;
  135. ++*this;
  136. return __tmp;
  137. }
  138. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {
  139. return __x.__cur_ == __y.__cur_ && __x.__trailing_empty_ == __y.__trailing_empty_;
  140. }
  141. };
  142. template <forward_range _View, forward_range _Pattern>
  143. requires view<_View> && view<_Pattern> &&
  144. indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to>
  145. struct split_view<_View, _Pattern>::__sentinel {
  146. private:
  147. _LIBCPP_NO_UNIQUE_ADDRESS sentinel_t<_View> __end_ = sentinel_t<_View>();
  148. _LIBCPP_HIDE_FROM_ABI static constexpr bool __equals(const __iterator& __x, const __sentinel& __y) {
  149. return __x.__cur_ == __y.__end_ && !__x.__trailing_empty_;
  150. }
  151. public:
  152. _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
  153. _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(split_view<_View, _Pattern>& __parent)
  154. : __end_(ranges::end(__parent.__base_)) {}
  155. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __sentinel& __y) {
  156. return __equals(__x, __y);
  157. }
  158. };
  159. namespace views {
  160. namespace __split_view {
  161. struct __fn : __range_adaptor_closure<__fn> {
  162. // clang-format off
  163. template <class _Range, class _Pattern>
  164. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
  165. constexpr auto operator()(_Range&& __range, _Pattern&& __pattern) const
  166. noexcept(noexcept(split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))))
  167. -> decltype( split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern)))
  168. { return split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern)); }
  169. // clang-format on
  170. template <class _Pattern>
  171. requires constructible_from<decay_t<_Pattern>, _Pattern>
  172. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pattern&& __pattern) const
  173. noexcept(is_nothrow_constructible_v<decay_t<_Pattern>, _Pattern>) {
  174. return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pattern>(__pattern)));
  175. }
  176. };
  177. } // namespace __split_view
  178. inline namespace __cpo {
  179. inline constexpr auto split = __split_view::__fn{};
  180. } // namespace __cpo
  181. } // namespace views
  182. } // namespace ranges
  183. #endif // _LIBCPP_STD_VER >= 20
  184. _LIBCPP_END_NAMESPACE_STD
  185. #endif // _LIBCPP___RANGES_SPLIT_VIEW_H