Files
2025-10-25 03:02:53 +03:00

32 KiB
Raw Permalink Blame History

[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]

1

#

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.

2

#

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 onlyslide-caches-nothing && bidirectional_range && common_range; templateconcept slide-caches-first = // exposition onlyslide-caches-nothing && slide-caches-last; 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);

1

#

Preconditions: n > 0 is true.

2

#

Effects: Initializes base_ with std::move(base) andn_ with n.

🔗

constexpr auto begin() requires (!([simple-view](range.utility.helpers#concept:simple-view "25.5.2Helper concepts[range.utility.helpers]")<V> && [slide-caches-nothing](#concept:slide-caches-nothing "25.7.30.2Class template slide_­view[range.slide.view]")<const V>));

3

#

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_).

4

#

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.2Class template slide_­view[range.slide.view]")<const V>;

5

#

Returns: iterator(ranges::begin(base_), n_).

🔗

constexpr auto end() requires (!([simple-view](range.utility.helpers#concept:simple-view "25.5.2Helper concepts[range.utility.helpers]")<V> && [slide-caches-nothing](#concept:slide-caches-nothing "25.7.30.2Class template slide_­view[range.slide.view]")<const V>));

6

#

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_)).

7

#

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.2Class template slide_­view[range.slide.view]")<const V>;

8

#

Returns: begin() + range_difference_t(size()).

🔗

constexpr auto size() requires [sized_range](range.sized#concept:sized_range "25.4.4Sized ranges[range.sized]")<V>; constexpr auto size() const requires [sized_range](range.sized#concept:sized_range "25.4.4Sized ranges[range.sized]")<const V>;

9

#

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.3Approximately sized ranges[range.approximately.sized]")<V>; constexpr auto reserve_hint() const requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3Approximately sized ranges[range.approximately.sized]")<const V>;

10

#

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 (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 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>>; };}

1

#

iterator::iterator_concept is defined as follows:

2

#

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](#concept:slide-caches-first "25.7.30.2Class template slide_­view[range.slide.view]")<Base>);

3

#

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.2Class template slide_­view[range.slide.view]")<Base>;

4

#

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.4Concept convertible_­to[concept.convertible]")<iterator_t<V>, iterator_t<Base>>;

5

#

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;

6

#

Returns: views::counted(current_, n_).

🔗

constexpr iterator& operator++();

7

#

Preconditions: current_ and last_ele_ (if present) are incrementable.

8

#

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.

9

#

Returns: *this.

🔗

constexpr iterator operator++(int);

10

#

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

🔗

constexpr iterator& operator--() requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6Other range refinements[range.refinements]")<Base>;

11

#

Preconditions: current_ and last_ele_ (if present) are decrementable.

12

#

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.

13

#

Returns: *this.

🔗

constexpr iterator operator--(int) requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6Other range refinements[range.refinements]")<Base>;

14

#

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.6Other range refinements[range.refinements]")<Base>;

15

#

Preconditions: current_ + x and last_ele_ + x (if last_ele_ is present) have well-defined behavior.

16

#

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.

17

#

Returns: *this.

🔗

constexpr iterator& operator-=(difference_type x) requires [random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]")<Base>;

18

#

Preconditions: current_ - x and last_ele_ - x (if last_ele_ is present) have well-defined behavior.

19

#

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.

20

#

Returns: *this.

🔗

constexpr auto operator[](difference_type n) const requires [random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]")<Base>;

21

#

Effects: Equivalent to: return views::counted(current_ + n, n_);

🔗

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

22

#

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.6Other range refinements[range.refinements]")<Base>;

23

#

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.6Other range refinements[range.refinements]")<Base>;

24

#

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.6Other range refinements[range.refinements]")<Base>;

25

#

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.6Other range refinements[range.refinements]")<Base>;

26

#

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.6Other range refinements[range.refinements]")<Base> && [three_way_comparable](cmp.concept#concept:three_way_comparable "17.12.4Concept three_­way_­comparable[cmp.concept]")<iterator_t<Base>>;

27

#

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.6Other 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.6Other range refinements[range.refinements]")<Base>;

28

#

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.6Other range refinements[range.refinements]")<Base>;

29

#

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.8Concept sized_­sentinel_­for[iterator.concept.sizedsentinel]")<iterator_t<Base>, iterator_t<Base>>;

30

#

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>; };}

1

#

[Note 1:

sentinel is used only when slide-caches-first is true.

— end note]

🔗

constexpr explicit sentinel(sentinel_t<V> end);

2

#

Effects: Initializes end_ with end.

🔗

friend constexpr bool operator==(const iterator<false>& x, const sentinel& y);

3

#

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.8Concept sized_­sentinel_­for[iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>;

4

#

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.8Concept sized_­sentinel_­for[iterator.concept.sizedsentinel]")<sentinel_t<V>, iterator_t<V>>;

5

#

Returns: y.end_ - x.last_ele_.