32 KiB
[range.slide]
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 Mth element is a view over the Mth through(M+Nâ1)th 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 toslide_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]
25.7.30.2 Class template slide_view [range.slide.view]
namespace std::ranges {templateconcept slide-caches-nothing = random_access_range && sized_range; // exposition onlytemplateconcept slide-caches-last = // exposition only && bidirectional_range && common_range; templateconcept slide-caches-first = // exposition only
&&
; template<forward_range V>requires viewclass slide_view : public view_interface<slide_view> { V base_; // exposition only range_difference_t n_; // exposition only// [range.slide.iterator], class template slide_view::iteratortemplate class iterator; // exposition only// [range.slide.sentinel], class slide_view::sentinelclass sentinel; // exposition onlypublic:constexpr explicit slide_view(V base, range_difference_t n); constexpr V base() const & requires copy_constructible { return base_; }constexpr V base() && { return std::move(base_); }constexpr auto begin()requires (!(simple-view && slide-caches-nothing)); constexpr auto begin() const requires slide-caches-nothing; constexpr auto end()requires (!(simple-view && slide-caches-nothing)); constexpr auto end() const requires slide-caches-nothing; constexpr auto size() requires sized_range; constexpr auto size() const requires sized_range; constexpr auto reserve_hintsize() requires approximately_sized_range; constexpr auto reserve_hintsize() const requires approximately_sized_range; }; template slide_view(R&&, range_difference_t) -> slide_view<views::all_t>;}
constexpr explicit slide_view(V base, range_difference_t<V> n);
Preconditions: n > 0 is true.
Effects: Initializes base_ with std::move(base) andn_ 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(ranges::begin(base_), ranges::next(ranges::begin(base_), n_ - 1, ranges::end(base_)), n_)
Otherwise, iterator(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](#concept:slide-caches-nothing "25.7.30.2 Class template slide_view [range.slide.view]")<const V>;
Returns: iterator(ranges::begin(base_), n_).
constexpr auto end() 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-nothing,iterator(ranges::begin(base_) + range_difference_t(size()), n_)
Otherwise, if V models slide-caches-last,iterator(ranges::prev(ranges::end(base_), n_ - 1, ranges::begin(base_)), n_)
Otherwise, if V models common_range,iterator(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](#concept:slide-caches-nothing "25.7.30.2 Class template slide_view [range.slide.view]")<const V>;
Returns: begin() + range_difference_t(size()).
constexpr auto size() requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<V>; constexpr auto size() const requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<const V>;
Effects: Equivalent to:auto sz = ranges::distance(base_) - n_ + 1;if (sz < 0) sz = 0;return to-unsigned-like(sz);
constexpr auto reserve_hint() requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<V>; constexpr auto reserve_hint() const requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<const V>;
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 viewtemplateclass slide_view::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 onlyconstexpr iterator(iterator_t<Base> current, range_difference_t<Base> n) // exposition onlyrequires (<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 i)requires Const && convertible_to<iterator_t, 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) constrequires 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 Base models random_access_range, then iterator_concept denotes random_access_iterator_tag.
-
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 andn_ 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, andn_ with n.
constexpr iterator(iterator<!Const> i) requires Const && [convertible_to](concept.convertible#concept:convertible_to "18.4.4 Concept convertible_to [concept.convertible]")<iterator_t<V>, iterator_t<Base>>;
Effects: Initializes current_ with std::move(i.current_) andn_ with i.n_.
[Note 1:
iterator 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](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<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](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<Base>;
Effects: Equivalent to:auto tmp = *this;--*this;return tmp;
constexpr iterator& operator+=(difference_type x) requires [random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<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](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<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](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<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](range.refinements#concept:random_access_range "25.4.6 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_access_range "25.4.6 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_access_range "25.4.6 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_access_range "25.4.6 Other range refinements [range.refinements]")<Base>;
Effects: Equivalent to: return !(x < y);
friend constexpr auto operator<=>(const iterator& x, const iterator& y) requires [random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<Base> && [three_way_comparable](cmp.concept#concept:three_way_comparable "17.12.4 Concept three_way_comparable [cmp.concept]")<iterator_t<Base>>;
Returns: x.current_ <=> y.current_.
friend constexpr iterator operator+(const iterator& i, difference_type n) requires [random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<Base>; friend constexpr iterator operator+(difference_type n, const iterator& i) requires [random_access_range](range.refinements#concept:random_access_range "25.4.6 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_access_range "25.4.6 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_sentinel_for "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 viewclass slide_view::sentinel { sentinel_t end_ = sentinel_t(); // exposition onlyconstexpr explicit sentinel(sentinel_t end); // exposition onlypublic:sentinel() = default; friend constexpr bool operator==(const iterator& x, const sentinel& y); friend constexpr range_difference_toperator-(const iterator& x, const sentinel& y)requires sized_sentinel_for<sentinel_t, iterator_t>; friend constexpr range_difference_toperator-(const sentinel& y, const iterator& x)requires sized_sentinel_for<sentinel_t, iterator_t>; };}
[Note 1:
sentinel is used only when slide-caches-first 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_sentinel_for "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_sentinel_for "24.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>;
Returns: y.end_ - x.last_ele_.