[range.slide] (original) (raw)
25 Ranges library [ranges]
25.7 Range adaptors [range.adaptors]
25.7.30 Slide view [range.slide]
25.7.30.1 Overview [range.slide.overview]
slide_view takes a view and a number N and produces a view whose element is a view over the through elements of the original view.
If the original view has fewer than N elements, the resulting view is empty.
Given subexpressions E and N, the expression views::slide(E, N) is expression-equivalent toslide_view(E, N).
[Example 1: vector v = {1, 2, 3, 4};for (auto i : v | views::slide(2)) { cout << '[' << i[0] << ", " << i[1] << "] "; } — _end example_]
25.7.30.2 Class template slide_view [range.slide.view]
namespace std::ranges { template<class V> concept slide-caches-nothing = random_access_range<V> && sized_range<V>; template<class V> concept slide-caches-last = <V> && bidirectional_range<V> && common_range<V>;template<class V> concept slide-caches-first =
<V> &&
<V>;template<forward_range V> requires view<V> class slide_view : public view_interface<slide_view<V>> { V base_; range_difference_t<V> n_; template<bool> class iterator; class sentinel; public: constexpr explicit slide_view(V base, range_difference_t<V> n);constexpr V base() const & requires copy_constructible<V> { return base_; } constexpr V base() && { return std::move(base_); } constexpr auto begin() requires (!(simple-view<V> && slide-caches-nothing<const V>));constexpr auto begin() const requires slide-caches-nothing<const V>;constexpr auto end() requires (!(simple-view<V> && slide-caches-nothing<const V>));constexpr auto end() const requires slide-caches-nothing<const V>;constexpr auto size() requires sized_range<V>;constexpr auto size() const requires sized_range<const V>;constexpr auto reserve_hintsize() requires approximately_sized_range<V>;constexpr auto reserve_hintsize() const requires approximately_sized_range<const V>;};template<class R> slide_view(R&&, range_difference_t<R>) -> slide_view<views::all_t<R>>;}
constexpr explicit slide_view(V base, range_difference_t<V> n);
Preconditions: n > 0 is true.
Effects: Initializes base_ with std::move(base) and_n__ with n.
constexpr auto begin() requires (!([_simple-view_](range.utility.helpers#concept:simple-view "25.5.2 Helper concepts [range.utility.helpers]")<V> && [_slide-caches-nothing_](#concept:slide-caches-nothing "25.7.30.2 Class template slide_view [range.slide.view]")<const V>));
Returns:
- If V models slide-caches-first,iterator<false>(ranges::begin(base_), ranges::next(ranges::begin(base_), n_ - 1, ranges::end(base_)), n_)
- Otherwise, iterator<false>(ranges::begin(base_), n_).
Remarks: In order to provide the amortized constant-time complexity required by the range concept, this function caches the result within the slide_viewfor use on subsequent calls when V models slide-caches-first.
constexpr auto begin() const requires [_slide-caches-nothing_](#concept:slide-caches-nothing "25.7.30.2 Class template slide_view [range.slide.view]")<const V>;
Returns: iterator<true>(ranges::begin(base_), n_).
Returns:
- If V models slide-caches-nothing,iterator<false>(ranges::begin(base_) + range_difference_t<V>(size()), n_)
- Otherwise, if V models slide-caches-last,iterator<false>(ranges::prev(ranges::end(base_), n_ - 1, ranges::begin(base_)), n_)
- Otherwise, if V models common_range,iterator<false>(ranges::end(base_), ranges::end(base_), n_)
- Otherwise, sentinel(ranges::end(base_)).
Remarks: In order to provide the amortized constant-time complexity required by the range concept, this function caches the result within the slide_viewfor use on subsequent calls when V models slide-caches-last.
constexpr auto end() const requires [_slide-caches-nothing_](#concept:slide-caches-nothing "25.7.30.2 Class template slide_view [range.slide.view]")<const V>;
Returns: begin() + range_difference_t<const V>(size()).
Effects: Equivalent to:auto sz = ranges::distance(base_) - n_ + 1;if (sz < 0) sz = 0;return to-unsigned-like(sz);
Effects: Equivalent to:auto sz = static_cast<range_difference_t<decltype((_base__))>>(ranges::reserve_hint(base_)) - n_ + 1;if (sz < 0) sz = 0;return to-unsigned-like(sz);
25.7.30.3 Class template slide_view::iterator [range.slide.iterator]
namespace std::ranges { template<forward_range V> requires view<V> template<bool Const> class slide_view<V>::iterator { using Base = maybe-const<Const, V>; iterator_t<_Base_> current_ = iterator_t<_Base_>(); iterator_t<_Base_> last_ele_ = iterator_t<_Base_>(); range_difference_t<_Base_> n_ = 0; constexpr iterator(iterator_t<_Base_> current, range_difference_t<_Base_> n) requires (<_Base_>);constexpr iterator(iterator_t<_Base_> current, iterator_t<_Base_> last_ele, range_difference_t<_Base_> n) requires slide-caches-first<_Base_>;public: using iterator_category = input_iterator_tag;using iterator_concept = see below;using value_type = decltype(views::counted(current_, n_));using difference_type = range_difference_t<_Base_>;iterator() = default;constexpr iterator(iterator<!Const> i) requires Const && convertible_to<iterator_t<V>, iterator_t<_Base_>>;constexpr auto operator*() const;constexpr iterator& operator++();constexpr iterator operator++(int);constexpr iterator& operator--() requires bidirectional_range<_Base_>;constexpr iterator operator--(int) requires bidirectional_range<_Base_>;constexpr iterator& operator+=(difference_type x) requires random_access_range<_Base_>;constexpr iterator& operator-=(difference_type x) requires random_access_range<_Base_>;constexpr auto operator[](difference_type n) const requires random_access_range<_Base_>;friend constexpr bool operator==(const iterator& x, const iterator& y);friend constexpr bool operator<(const _iterator_& x, const _iterator_& y) requires random_access_range<_Base_>;friend constexpr bool operator>(const iterator& x, const iterator& y) requires random_access_range<_Base_>;friend constexpr bool operator<=(const _iterator_& x, const _iterator_& y) requires random_access_range<_Base_>;friend constexpr bool operator>=(const iterator& x, const iterator& y) requires random_access_range<_Base_>;friend constexpr auto operator<=>(const iterator& x, const iterator& y) requires random_access_range<_Base_> && three_way_comparable<iterator_t<_Base_>>;friend constexpr iterator operator+(const iterator& i, difference_type n) requires random_access_range<_Base_>;friend constexpr iterator operator+(difference_type n, const iterator& i) requires random_access_range<_Base_>;friend constexpr iterator operator-(const iterator& i, difference_type n) requires random_access_range<_Base_>;friend constexpr difference_type operator-(const iterator& x, const iterator& y) requires sized_sentinel_for<iterator_t<_Base_>, iterator_t<_Base_>>;};}
_iterator_::iterator_concept is defined as follows:
- Otherwise, if Base models bidirectional_range, then iterator_concept denotes bidirectional_iterator_tag.
- Otherwise, iterator_concept denotes forward_iterator_tag.
If the invocation of any non-const member function of _iterator_exits via an exception, the iterator acquires a singular value.
constexpr _iterator_(iterator_t<_Base_> current, range_difference_t<_Base_> n) requires (<_Base_>);
Effects: Initializes current_ with current and_n__ with n.
constexpr _iterator_(iterator_t<_Base_> current, iterator_t<_Base_> last_ele, range_difference_t<_Base_> n) requires [_slide-caches-first_](#concept:slide-caches-first "25.7.30.2 Class template slide_view [range.slide.view]")<_Base_>;
Effects: Initializes current_ with current,last_ele_ with last_ele, and_n__ with n.
constexpr _iterator_(_iterator_<!Const> i) requires Const && [convertible_to](concept.convertible#concept:convertible%5Fto "18.4.4 Concept convertible_to [concept.convertible]")<iterator_t<V>, iterator_t<_Base_>>;
Effects: Initializes current_ with std::move(i.current_) and_n__ with i.n_.
[Note 1:
iterator<true> can only be formed when Base models slide-caches-nothing, in which case last_ele_ is not present.
— _end note_]
constexpr auto operator*() const;
Returns: views::counted(current_, n_).
constexpr _iterator_& operator++();
Preconditions: current_ and last_ele_ (if present) are incrementable.
Postconditions: current_ and last_ele_ (if present) are each equal to ranges::next(i), where i is the value of that data member before the call.
constexpr _iterator_ operator++(int);
Effects: Equivalent to:auto tmp = *this;++*this;return tmp;
Preconditions: current_ and last_ele_ (if present) are decrementable.
Postconditions: current_ and last_ele_ (if present) are each equal to ranges::prev(i), where i is the value of that data member before the call.
Effects: Equivalent to:auto tmp = *this;--*this;return tmp;
Preconditions: current_ + x and last_ele_ + x (if last_ele_ is present) have well-defined behavior.
Postconditions: current_ and last_ele_ (if present) are each equal to i + x, where i is the value of that data member before the call.
Preconditions: current_ - x and last_ele_ - x (if last_ele_ is present) have well-defined behavior.
Postconditions: current_ and last_ele_ (if present) are each equal to i - x, where i is the value of that data member before the call.
Effects: Equivalent to: return views::counted(current_ + n, n_);
friend constexpr bool operator==(const _iterator_& x, const _iterator_& y);
Returns: If last_ele_ is present,x.last_ele_ == y.last_ele_; otherwise, x.current_ == y.current_.
friend constexpr bool operator<(const _iterator_& x, const _iterator_& y) requires [random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.5 Other range refinements [range.refinements]")<_Base_>;
Returns: x.current_ < y.current_.
friend constexpr bool operator>(const _iterator_& x, const _iterator_& y) requires [random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.5 Other range refinements [range.refinements]")<_Base_>;
Effects: Equivalent to: return y < x;
friend constexpr bool operator<=(const _iterator_& x, const _iterator_& y) requires [random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.5 Other range refinements [range.refinements]")<_Base_>;
Effects: Equivalent to: return !(y < x);
friend constexpr bool operator>=(const _iterator_& x, const _iterator_& y) requires [random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.5 Other range refinements [range.refinements]")<_Base_>;
Effects: Equivalent to: return !(x < y);
Returns: x.current_ <=> y.current_.
friend constexpr _iterator_ operator+(const _iterator_& i, difference_type n) requires [random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.5 Other range refinements [range.refinements]")<_Base_>;friend constexpr _iterator_ operator+(difference_type n, const _iterator_& i) requires [random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.5 Other range refinements [range.refinements]")<_Base_>;
Effects: Equivalent to:auto r = i; r += n;return r;
friend constexpr _iterator_ operator-(const _iterator_& i, difference_type n) requires [random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.5 Other range refinements [range.refinements]")<_Base_>;
Effects: Equivalent to:auto r = i; r -= n;return r;
friend constexpr difference_type operator-(const _iterator_& x, const _iterator_& y) requires [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<iterator_t<_Base_>, iterator_t<_Base_>>;
Returns: If last_ele_ is present,x.last_ele_ - y.last_ele_; otherwise, x.current_ - y.current_.
25.7.30.4 Class slide_view::sentinel [range.slide.sentinel]
namespace std::ranges { template<forward_range V> requires view<V> class slide_view<V>::sentinel { sentinel_t<V> end_ = sentinel_t<V>(); constexpr explicit sentinel(sentinel_t<V> end); public: sentinel() = default;friend constexpr bool operator==(const iterator<false>& x, const sentinel& y);friend constexpr range_difference_t<V> operator-(const iterator<false>& x, const sentinel& y) requires sized_sentinel_for<sentinel_t<V>, iterator_t<V>>;friend constexpr range_difference_t<V> operator-(const sentinel& y, const iterator<false>& x) requires sized_sentinel_for<sentinel_t<V>, iterator_t<V>>;};}
[Note 1:
sentinel is used only when slide-caches-first<V> is true.
— _end note_]
constexpr explicit _sentinel_(sentinel_t<V> end);
Effects: Initializes end_ with end.
friend constexpr bool operator==(const _iterator_<false>& x, const _sentinel_& y);
Returns: x.last_ele_ == y.end_.
friend constexpr range_difference_t<V> operator-(const _iterator_<false>& x, const _sentinel_& y) requires [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>;
Returns: x.last_ele_ - y.end_.
friend constexpr range_difference_t<V> operator-(const _sentinel_& y, const _iterator_<false>& x) requires [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>;
Returns: y.end_ - x.last_ele_.