chunk_by_view.h 8.8 KB

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