26 Ranges library [ranges]

26.7 Range adaptors [range.adaptors]

26.7.25 Slide view [range.slide]

26.7.25.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.
The name views​::​slide denotes a range adaptor object ([range.adaptor.object]).
Given subexpressions E and N, the expression views​::​slide(E, N) is expression-equivalent to slide_­view(E, N).
[Example 1: vector v = {1, 2, 3, 4}; for (auto i : v | views::slide(2)) { cout << '[' << i[0] << ", " << i[1] << "] "; // prints [1, 2] [2, 3] [3, 4] } — end example]

26.7.25.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>; // exposition only template<class V> concept slide-caches-last = // exposition only !slide-caches-nothing<V> && bidirectional_­range<V> && common_­range<V>; template<class V> concept slide-caches-first = // exposition only !slide-caches-nothing<V> && !slide-caches-last<V>; template<forward_­range V> requires view<V> class slide_view : public view_interface<slide_view<V>> { V base_ = V(); // exposition only range_difference_t<V> n_ = 0; // exposition only // [range.slide.iterator], class template slide_­view​::​iterator template<bool> class iterator; // exposition only // [range.slide.sentinel], class slide_­view​::​sentinel class sentinel; // exposition only public: slide_view() requires default_­initializable<V> = default; constexpr explicit slide_view(V base, range_difference_t<V> n); 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>; }; template<class R> slide_view(R&& r, range_difference_t<R>) -> slide_view<views::all_t<R>>; }
constexpr explicit slide_view(V base, range_difference_t<V> n);
Effects: Initializes base_­ with std​::​move(base) and n_­ with n.
constexpr auto begin() requires (!(simple-view<V> && slide-caches-nothing<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_­view for use on subsequent calls when V models slide-caches-first.
constexpr auto begin() const requires slide-caches-nothing<const V>;
Returns: iterator<true>(ranges​::​begin(base_­), n_­).
constexpr auto end() requires (!(simple-view<V> && slide-caches-nothing<const V>));
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_­view for use on subsequent calls when V models slide-caches-last.
constexpr auto end() const requires slide-caches-nothing<const V>;
Returns: begin() + range_­difference_­t<const V>(size()).
constexpr auto size() requires sized_­range<V>; constexpr auto size() const requires sized_­range<const V>;
Effects: Equivalent to: auto sz = ranges::distance(base_) - n_ + 1; if (sz < 0) sz = 0; return to-unsigned-like(sz);

26.7.25.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>; // exposition only iterator_t<Base> current_ = iterator_t<Base>(); // exposition only iterator_t<Base> last_ele_ = iterator_t<Base>(); // exposition only, // present only if Base models slide-caches-first range_difference_t<Base> n_ = 0; // exposition only constexpr iterator(iterator_t<Base> current, range_difference_t<Base> n) // exposition only requires (!slide-caches-first<Base>); constexpr iterator(iterator_t<Base> current, iterator_t<Base> last_ele, // exposition only 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:
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 (!slide-caches-first<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<Base>;
Effects: Initializes current_­ with current, last_­ele_­ with last_­ele, and n_­ with n.
constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<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.
Returns: *this.
constexpr iterator operator++(int);
Effects: Equivalent to: auto tmp = *this; ++*this; return tmp;
constexpr iterator& operator--() requires bidirectional_­range<Base>;
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.
Returns: *this.
constexpr iterator operator--(int) requires bidirectional_­range<Base>;
Effects: Equivalent to: auto tmp = *this; --*this; return tmp;
constexpr iterator& operator+=(difference_type x) requires random_­access_­range<Base>;
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.
Returns: *this.
constexpr iterator& operator-=(difference_type x) requires random_­access_­range<Base>;
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.
Returns: *this.
constexpr auto operator[](difference_type n) const requires random_­access_­range<Base>;
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<Base>;
Returns: x.current_­ < y.current_­.
friend constexpr bool operator>(const iterator& x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return y < x;
friend constexpr bool operator<=(const iterator& x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return !(y < x);
friend constexpr bool operator>=(const iterator& x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return !(x < y);
friend constexpr auto operator<=>(const iterator& x, const iterator& y) requires random_­access_­range<Base> && three_­way_­comparable<iterator_t<Base>>;
Returns: x.current_­ <=> y.current_­.
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>;
Effects: Equivalent to: auto r = i; r += n; return r;
friend constexpr iterator operator-(const iterator& i, difference_type n) requires random_­access_­range<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_t<Base>, iterator_t<Base>>;
Returns: If last_­ele_­ is present, x.last_­ele_­ - y.last_­ele_­; otherwise, x.current_­ - y.current_­.

26.7.25.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>(); // exposition only constexpr explicit sentinel(sentinel_t<V> end); // exposition only 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<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<sentinel_t<V>, iterator_t<V>>;
Returns: y.end_­ - x.last_­ele_­.