[range.chunk.by] (original) (raw)

25 Ranges library [ranges]

25.7 Range adaptors [range.adaptors]

25.7.31 Chunk by view [range.chunk.by]

25.7.31.1 Overview [range.chunk.by.overview]

chunk_by_view takes a view and a predicate, and splits the view into subranges between each pair of adjacent elements for which the predicate returns false.

Given subexpressions E and F, the expression views​::​chunk_by(E, F) is expression-equivalent tochunk_by_view(E, F).

[Example 1: vector v = {1, 2, 2, 3, 0, 4, 5, 2};for (auto r : v | views::chunk_by(ranges::less_equal{})) { cout << '[';auto sep = "";for (auto i : r) { cout << sep << i; sep = ", ";} cout << "] ";} — _end example_]

25.7.31.2 Class template chunk_by_view [range.chunk.by.view]

namespace std::ranges { template<forward_range V, indirect_binary_predicate<iterator_t<V>, iterator_t<V>> Pred> requires view<V> && is_object_v<Pred> class chunk_by_view : public view_interface<chunk_by_view<V, Pred>> { V base_ = V(); movable-box<Pred> pred_; class iterator; public: chunk_by_view() requires default_initializable<V> && default_initializable<Pred> = default;constexpr explicit chunk_by_view(V base, Pred pred);constexpr V base() const & requires copy_constructible<V> { return base_; } constexpr V base() && { return std::move(base_); } constexpr const Pred& pred() const;constexpr iterator begin();constexpr auto end();constexpr iterator_t<V> find-next(iterator_t<V>); constexpr iterator_t<V> find-prev(iterator_t<V>) requires bidirectional_range<V>;};template<class R, class Pred> chunk_by_view(R&&, Pred) -> chunk_by_view<views::all_t<R>, Pred>;}

constexpr explicit chunk_by_view(V base, Pred pred);

Effects: Initializes base_ with std​::​move(base) and_pred__ with std​::​move(pred).

constexpr const Pred& pred() const;

Effects: Equivalent to: return *pred_;

constexpr _iterator_ begin();

Preconditions: pred_.has_value() is true.

Returns: iterator(*this, ranges​::​begin(base_), find-next(ranges​::​begin(base_))).

Remarks: In order to provide the amortized constant-time complexity required by the range concept, this function caches the result within the chunk_by_viewfor use on subsequent calls.

Effects: Equivalent to:if constexpr (common_range<V>) { return iterator(*this, ranges::end(base_), ranges::end(base_));} else { return default_sentinel;}

constexpr iterator_t<V> _find-next_(iterator_t<V> current);

Preconditions: pred_.has_value() is true.

Returns: ranges::next(ranges::adjacent_find(current, ranges::end(base_), not_fn(ref(*pred_))),1, ranges::end(base_))

Preconditions:

Returns: An iterator iin the range [ranges​::​begin(base_), current) such that:

25.7.31.3 Class chunk_by_view​::​iterator [range.chunk.by.iter]

namespace std::ranges { template<forward_range V, indirect_binary_predicate<iterator_t<V>, iterator_t<V>> Pred> requires view<V> && is_object_v<Pred> class chunk_by_view<V, Pred>::iterator { chunk_by_view* parent_ = nullptr; iterator_t<V> current_ = iterator_t<V>(); iterator_t<V> next_ = iterator_t<V>(); constexpr iterator(chunk_by_view& parent, iterator_t<V> current, iterator_t<V> next);public: using value_type = subrange<iterator_t<V>>;using difference_type = range_difference_t<V>;using iterator_category = input_iterator_tag;using iterator_concept = see below;iterator() = default;constexpr value_type operator*() const;constexpr iterator& operator++();constexpr iterator operator++(int);constexpr iterator& operator--() requires bidirectional_range<V>;constexpr iterator operator--(int) requires bidirectional_range<V>;friend constexpr bool operator==(const iterator& x, const iterator& y);friend constexpr bool operator==(const iterator& x, default_sentinel_t);};}

_iterator_​::​iterator_concept is defined as follows:

constexpr _iterator_(chunk_by_view& parent, iterator_t<V> current, iterator_t<V> next);

Effects: Initializes parent_ with addressof(parent),current_ with current, and_next__ with next.

constexpr value_type operator*() const;

Preconditions: current_ is not equal to next_.

Returns: subrange(current_, next_).

constexpr _iterator_& operator++();

Preconditions: current_ is not equal to next_.

Effects: Equivalent to:current_ = next_;next_ = _parent__->find-next(current_);return *this;

constexpr _iterator_ operator++(int);

Effects: Equivalent to:auto tmp = *this;++*this;return tmp;

Effects: Equivalent to:next_ = current_;current_ = _parent__->find-prev(next_);return *this;

Effects: Equivalent to:auto tmp = *this;--*this;return tmp;

friend constexpr bool operator==(const _iterator_& x, const _iterator_& y);

Returns: x.current_ == y.current_.

friend constexpr bool operator==(const _iterator_& x, default_sentinel_t);

Returns: x.current_ == x.next_.