26 Ranges library [ranges]

26.7 Range adaptors [range.adaptors]

26.7.30 Stride view [range.stride]

26.7.30.1 Overview [range.stride.overview]

stride_­view presents a view of an underlying sequence, advancing over n elements at a time, as opposed to the usual single-step succession.
The name views​::​stride denotes a range adaptor object ([range.adaptor.object]).
Given subexpressions E and N, the expression views​::​stride(E, N) is expression-equivalent to stride_­view(E, N).
[Example 1: auto input = views::iota(0, 12) | views::stride(3); ranges::copy(input, ostream_iterator<int>(cout, " ")); // prints 0 3 6 9 ranges::copy(input | views::reverse, ostream_iterator<int>(cout, " ")); // prints 9 6 3 0 — end example]

26.7.30.2 Class template stride_­view [range.stride.view]

namespace std::ranges { template<input_­range V> requires view<V> class stride_view : public view_interface<stride_view<V>> { V base_; // exposition only range_difference_t<V> stride_; // exposition only // [range.stride.iterator], class template stride_­view​::​iterator template<bool> class iterator; // exposition only public: constexpr explicit stride_view(V base, range_difference_t<V> stride); constexpr V base() const & requires copy_­constructible<V> { return base_; } constexpr V base() && { return std::move(base_); } constexpr range_difference_t<V> stride() const noexcept; constexpr auto begin() requires (!simple-view<V>) { return iterator<false>(this, ranges::begin(base_)); } constexpr auto begin() const requires range<const V> { return iterator<true>(this, ranges::begin(base_)); } constexpr auto end() requires (!simple-view<V>) { if constexpr (common_­range<V> && sized_­range<V> && forward_­range<V>) { auto missing = (stride_ - ranges::distance(base_) % stride_) % stride_; return iterator<false>(this, ranges::end(base_), missing); } else if constexpr (common_­range<V> && !bidirectional_­range<V>) { return iterator<false>(this, ranges::end(base_)); } else { return default_sentinel; } } constexpr auto end() const requires range<const V> { if constexpr (common_­range<const V> && sized_­range<const V> && forward_­range<const V>) { auto missing = (stride_ - ranges::distance(base_) % stride_) % stride_; return iterator<true>(this, ranges::end(base_), missing); } else if constexpr (common_­range<const V> && !bidirectional_­range<const V>) { return iterator<true>(this, ranges::end(base_)); } else { return default_sentinel; } } constexpr auto size() requires sized_­range<V>; constexpr auto size() const requires sized_­range<const V>; }; template<class R> stride_view(R&&, range_difference_t<R>) -> stride_view<views::all_t<R>>; }
constexpr stride_view(V base, range_difference_t<V> stride);
Preconditions: stride > 0 is true.
Effects: Initializes base_­ with std​::​move(base) and stride_­ with stride.
constexpr range_difference_t<V> stride() const noexcept;
Returns: stride_­.
constexpr auto size() requires sized_­range<V>; constexpr auto size() const requires sized_­range<const V>;
Effects: Equivalent to: return to-unsigned-like(div-ceil(ranges::distance(base_), stride_));

26.7.30.3 Class template stride_­view​::​iterator [range.stride.iterator]

namespace std::ranges { template<input_­range V> requires view<V> template<bool Const> class stride_view<V>::iterator { using Parent = maybe-const<Const, stride_view>; // exposition only using Base = maybe-const<Const, V>; // exposition only iterator_t<Base> current_ = iterator_t<Base>(); // exposition only sentinel_t<Base> end_ = sentinel_t<Base>(); // exposition only range_difference_t<Base> stride_ = 0; // exposition only range_difference_t<Base> missing_ = 0; // exposition only constexpr iterator(Parent* parent, iterator_t<Base> current, // exposition only range_difference_t<Base> missing = 0); public: using difference_type = range_difference_t<Base>; using value_type = range_value_t<Base>; using iterator_concept = see below; using iterator_category = see below; // not always present iterator() requires default_­initializable<iterator_t<Base>> = default; constexpr iterator(iterator<!Const> other) requires Const && convertible_­to<iterator_t<V>, iterator_t<Base>> && convertible_­to<sentinel_t<V>, sentinel_t<Base>>; constexpr iterator_t<Base> base() &&; constexpr const iterator_t<Base>& base() const & noexcept; constexpr decltype(auto) operator*() const { return *current_; } constexpr iterator& operator++(); constexpr void operator++(int); constexpr iterator operator++(int) requires forward_­range<Base>; constexpr iterator& operator--() requires bidirectional_­range<Base>; constexpr iterator operator--(int) requires bidirectional_­range<Base>; constexpr iterator& operator+=(difference_type n) requires random_­access_­range<Base>; constexpr iterator& operator-=(difference_type n) requires random_­access_­range<Base>; constexpr decltype(auto) operator[](difference_type n) const requires random_­access_­range<Base> { return *(*this + n); } friend constexpr bool operator==(const iterator& x, default_sentinel_t); friend constexpr bool operator==(const iterator& x, const iterator& y) requires equality_­comparable<iterator_t<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 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& x, difference_type n) requires random_­access_­range<Base>; friend constexpr iterator operator+(difference_type n, const iterator& x) requires random_­access_­range<Base>; friend constexpr iterator operator-(const iterator& x, 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>>; friend constexpr difference_type operator-(default_sentinel_t y, const iterator& x) requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<Base>>; friend constexpr difference_type operator-(const iterator& x, default_sentinel_t y) requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<Base>>; friend constexpr range_rvalue_reference_t<Base> iter_move(const iterator& i) noexcept(noexcept(ranges::iter_move(i.current_))); friend constexpr void iter_swap(const iterator& x, const iterator& y) noexcept(noexcept(ranges::iter_swap(x.current_, y.current_))) requires indirectly_­swappable<iterator_t<Base>>; }; }
iterator​::​iterator_­concept is defined as follows:
The member typedef-name iterator_­category is defined if and only if Base models forward_­range.
In that case, iterator​::​iterator_­category is defined as follows:
  • Let C denote the type iterator_­traits<iterator_­t<Base>>​::​iterator_­category.
  • If C models derived_­from<random_­access_­iterator_­tag>, then iterator_­category denotes random_­access_­iterator_­tag.
  • Otherwise, iterator_­category denotes C.
constexpr iterator(Parent* parent, iterator_t<Base> current, range_difference_t<Base> missing = 0);
Effects: Initializes current_­ with std​::​move(current), end_­ with ranges​::​end(parent->base_­), stride_­ with parent->stride_­, and missing_­ with missing.
constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<iterator_t<V>, iterator_t<Base>> && convertible_­to<sentinel_t<V>, sentinel_t<Base>>;
Effects: Initializes current_­ with std​::​move(i.current_­), end_­ with std​::​move(i.end_­), stride_­ with i.stride_­, and missing_­ with i.missing_­.
constexpr iterator_t<Base> base() &&;
Returns: std​::​move(current_­).
constexpr const iterator_t<Base>& base() const & noexcept;
Returns: current_­.
constexpr iterator& operator++();
Preconditions: current_­ != end_­ is true.
Effects: Equivalent to: missing_ = ranges::advance(current_, stride_, end_); return *this;
constexpr void operator++(int);
Effects: Equivalent to: ++*this;
constexpr iterator operator++(int) requires forward_­range<Base>;
Effects: Equivalent to: auto tmp = *this; ++*this; return tmp;
constexpr iterator& operator--() requires bidirectional_­range<Base>;
Effects: Equivalent to: ranges::advance(current_, missing_ - stride_); missing_ = 0; return *this;
constexpr iterator operator--(int) requires bidirectional_­range<Base>;
Effects: Equivalent to: auto tmp = *this; --*this; return tmp;
constexpr iterator& operator+=(difference_type n) requires random_­access_­range<Base>;
Preconditions: If n is positive, ranges​::​distance(current_­, end_­) > stride_­ * (n - 1) is true.
[Note 1:
If n is negative, the Effects paragraph implies a precondition.
— end note]
Effects: Equivalent to: if (n > 0) { missing_ = ranges::advance(current_, stride_ * n, end_); } else if (n < 0) { ranges::advance(current_, stride_ * n + missing_); missing_ = 0; } return *this;
constexpr iterator& operator-=(difference_type x) requires random_­access_­range<Base>;
Effects: Equivalent to: return *this += -x;
friend constexpr bool operator==(const iterator& x, default_sentinel_t);
Returns: x.current_­ == x.end_­.
friend constexpr bool operator==(const iterator& x, const iterator& y) requires equality_­comparable<iterator_t<Base>>;
Returns: 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: Let N be (x.current_­ - y.current_­).
  • If Base models forward_­range, (N + x.missing_­ - y.missing_­) / x.stride_­.
  • Otherwise, if N is negative, -div-ceil(-N, x.stride_­).
  • Otherwise, div-ceil(N, x.stride_­).
friend constexpr difference_type operator-(default_sentinel_t y, const iterator& x) requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<Base>>;
Returns: div-ceil(x.end_­ - x.current_­, x.stride_­).
friend constexpr difference_type operator-(const iterator& x, default_sentinel_t y) requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<Base>>;
Effects: Equivalent to: return -(y - x);
friend constexpr range_rvalue_reference_t<Base> iter_move(const iterator& i) noexcept(noexcept(ranges::iter_move(i.current_­)));
Effects: Equivalent to: return ranges​::​iter_­move(i.current_­);
friend constexpr void iter_swap(const iterator& x, const iterator& y) noexcept(noexcept(ranges::iter_swap(x.current_­, y.current_­))) requires indirectly_­swappable<iterator_t<Base>>;
Effects: Equivalent to: ranges​::​iter_­swap(x.current_­, y.current_­);