chunk_by_view.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_CHUNK_BY_VIEW_H
  10. #define _LIBCPP___RANGES_CHUNK_BY_VIEW_H
  11. #include <__algorithm/ranges_adjacent_find.h>
  12. #include <__assert>
  13. #include <__concepts/constructible.h>
  14. #include <__config>
  15. #include <__functional/bind_back.h>
  16. #include <__functional/invoke.h>
  17. #include <__functional/not_fn.h>
  18. #include <__functional/reference_wrapper.h>
  19. #include <__iterator/concepts.h>
  20. #include <__iterator/default_sentinel.h>
  21. #include <__iterator/iterator_traits.h>
  22. #include <__iterator/next.h>
  23. #include <__iterator/prev.h>
  24. #include <__memory/addressof.h>
  25. #include <__ranges/access.h>
  26. #include <__ranges/all.h>
  27. #include <__ranges/concepts.h>
  28. #include <__ranges/movable_box.h>
  29. #include <__ranges/non_propagating_cache.h>
  30. #include <__ranges/range_adaptor.h>
  31. #include <__ranges/reverse_view.h>
  32. #include <__ranges/subrange.h>
  33. #include <__ranges/view_interface.h>
  34. #include <__type_traits/conditional.h>
  35. #include <__type_traits/decay.h>
  36. #include <__type_traits/is_nothrow_constructible.h>
  37. #include <__type_traits/is_object.h>
  38. #include <__utility/forward.h>
  39. #include <__utility/in_place.h>
  40. #include <__utility/move.h>
  41. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  42. # pragma GCC system_header
  43. #endif
  44. _LIBCPP_PUSH_MACROS
  45. #include <__undef_macros>
  46. _LIBCPP_BEGIN_NAMESPACE_STD
  47. #if _LIBCPP_STD_VER >= 23
  48. namespace ranges {
  49. template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred>
  50. requires view<_View> && is_object_v<_Pred>
  51. class chunk_by_view : public view_interface<chunk_by_view<_View, _Pred>> {
  52. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  53. _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_;
  54. // We cache the result of begin() to allow providing an amortized O(1).
  55. using _Cache = __non_propagating_cache<iterator_t<_View>>;
  56. _Cache __cached_begin_;
  57. class __iterator;
  58. _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_next(iterator_t<_View> __current) {
  59. _LIBCPP_ASSERT_UNCATEGORIZED(
  60. __pred_.__has_value(), "Trying to call __find_next() on a chunk_by_view that does not have a valid predicate.");
  61. return ranges::next(ranges::adjacent_find(__current, ranges::end(__base_), std::not_fn(std::ref(*__pred_))),
  62. 1,
  63. ranges::end(__base_));
  64. }
  65. _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_prev(iterator_t<_View> __current)
  66. requires bidirectional_range<_View>
  67. {
  68. _LIBCPP_ASSERT_UNCATEGORIZED(
  69. __current != ranges::begin(__base_), "Trying to call __find_prev() on a begin iterator.");
  70. _LIBCPP_ASSERT_UNCATEGORIZED(
  71. __pred_.__has_value(), "Trying to call __find_prev() on a chunk_by_view that does not have a valid predicate.");
  72. auto __first = ranges::begin(__base_);
  73. reverse_view __reversed{subrange{__first, __current}};
  74. auto __reversed_pred = [this]<class _Tp, class _Up>(_Tp&& __x, _Up&& __y) {
  75. return !std::invoke(*__pred_, std::forward<_Up>(__y), std::forward<_Tp>(__x));
  76. };
  77. return ranges::prev(ranges::adjacent_find(__reversed, __reversed_pred).base(), 1, std::move(__first));
  78. }
  79. public:
  80. _LIBCPP_HIDE_FROM_ABI chunk_by_view()
  81. requires default_initializable<_View> && default_initializable<_Pred>
  82. = default;
  83. _LIBCPP_HIDE_FROM_ABI constexpr explicit chunk_by_view(_View __base, _Pred __pred)
  84. : __base_(std::move(__base)), __pred_(in_place, std::move(__pred)) {}
  85. _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
  86. requires copy_constructible<_View>
  87. {
  88. return __base_;
  89. }
  90. _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
  91. _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; }
  92. _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() {
  93. _LIBCPP_ASSERT_UNCATEGORIZED(
  94. __pred_.__has_value(), "Trying to call begin() on a chunk_by_view that does not have a valid predicate.");
  95. auto __first = ranges::begin(__base_);
  96. if (!__cached_begin_.__has_value()) {
  97. __cached_begin_.__emplace(__find_next(__first));
  98. }
  99. return {*this, std::move(__first), *__cached_begin_};
  100. }
  101. _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
  102. if constexpr (common_range<_View>) {
  103. return __iterator{*this, ranges::end(__base_), ranges::end(__base_)};
  104. } else {
  105. return default_sentinel;
  106. }
  107. }
  108. };
  109. template <class _Range, class _Pred>
  110. chunk_by_view(_Range&&, _Pred) -> chunk_by_view<views::all_t<_Range>, _Pred>;
  111. template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred>
  112. requires view<_View> && is_object_v<_Pred>
  113. class chunk_by_view<_View, _Pred>::__iterator {
  114. friend chunk_by_view;
  115. chunk_by_view* __parent_ = nullptr;
  116. _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>();
  117. _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __next_ = iterator_t<_View>();
  118. _LIBCPP_HIDE_FROM_ABI constexpr __iterator(
  119. chunk_by_view& __parent, iterator_t<_View> __current, iterator_t<_View> __next)
  120. : __parent_(std::addressof(__parent)), __current_(__current), __next_(__next) {}
  121. public:
  122. using value_type = subrange<iterator_t<_View>>;
  123. using difference_type = range_difference_t<_View>;
  124. using iterator_category = input_iterator_tag;
  125. using iterator_concept = conditional_t<bidirectional_range<_View>, bidirectional_iterator_tag, forward_iterator_tag>;
  126. _LIBCPP_HIDE_FROM_ABI __iterator() = default;
  127. _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const {
  128. _LIBCPP_ASSERT_UNCATEGORIZED(__current_ != __next_, "Trying to dereference past-the-end chunk_by_view iterator.");
  129. return {__current_, __next_};
  130. }
  131. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
  132. _LIBCPP_ASSERT_UNCATEGORIZED(__current_ != __next_, "Trying to increment past end chunk_by_view iterator.");
  133. __current_ = __next_;
  134. __next_ = __parent_->__find_next(__current_);
  135. return *this;
  136. }
  137. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {
  138. auto __tmp = *this;
  139. ++*this;
  140. return __tmp;
  141. }
  142. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
  143. requires bidirectional_range<_View>
  144. {
  145. __next_ = __current_;
  146. __current_ = __parent_->__find_prev(__next_);
  147. return *this;
  148. }
  149. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
  150. requires bidirectional_range<_View>
  151. {
  152. auto __tmp = *this;
  153. --*this;
  154. return __tmp;
  155. }
  156. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {
  157. return __x.__current_ == __y.__current_;
  158. }
  159. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, default_sentinel_t) {
  160. return __x.__current_ == __x.__next_;
  161. }
  162. };
  163. namespace views {
  164. namespace __chunk_by {
  165. struct __fn {
  166. template <class _Range, class _Pred>
  167. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
  168. noexcept(noexcept(/**/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
  169. -> decltype(/*--*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {
  170. return /*-------------*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));
  171. }
  172. template <class _Pred>
  173. requires constructible_from<decay_t<_Pred>, _Pred>
  174. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const
  175. noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {
  176. return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred)));
  177. }
  178. };
  179. } // namespace __chunk_by
  180. inline namespace __cpo {
  181. inline constexpr auto chunk_by = __chunk_by::__fn{};
  182. } // namespace __cpo
  183. } // namespace views
  184. } // namespace ranges
  185. #endif // _LIBCPP_STD_VER >= 23
  186. _LIBCPP_END_NAMESPACE_STD
  187. _LIBCPP_POP_MACROS
  188. #endif // _LIBCPP___RANGES_CHUNK_BY_VIEW_H