Namespaces
Variants
Views
Actions

std::ranges::views::chunk, std::ranges::chunk_view

From cppreference.com
< cpp‎ | ranges
 
 
Ranges library
Range access
Range conversions
(C++23)
Range primitives



Dangling iterator handling
Range concepts
Views

Factories




Adaptors
Range adaptor objects
Range adaptor closure objects
Helper items
 
 
Defined in header <ranges>
template< ranges::view V >

    requires ranges::input_range<V>
class chunk_view :

    public ranges::view_interface<chunk_view<V>>
(1) (since C++23)
template< ranges::view V >

    requires ranges::forward_range<V>
class chunk_view<V> :

    public ranges::view_interface<chunk_view<V>>
(2) (since C++23)
namespace views {

    inline constexpr /* unspecified */ chunk = /* unspecified */;

}
(3) (since C++23)
Call signature
template< ranges::viewable_range R >
constexpr ranges::view auto chunk( R&& r, ranges::range_difference_t<V> n );
(since C++23)
template< ranges::input_range V >

    requires ranges::view<V>

constexpr __range_adaptor_closure chunk( ranges::range_difference_t<V> n );
(since C++23)
Helper templates
template< class I >
constexpr I __div_ceil( I num, I denom );    // exposition only
(4) (since C++23)

chunk_view takes a view and a number n and produces a range of views (the chunks) of the original view, such that each chunk, except maybe the last one, has the size n. These chunks are non-overlapping, successive sub-ranges of the elements of the original view, in order.

Let s be the size of the original view. If s is not the multiple of of n, the size of the last produced view is exactly s % n (the remainder). Otherwise, the size of each chunk, including the last one, is n.

The size of produced view is __div_ceil(s).

If the n is not greater than 0 the behavior is undefined.

1) An implementation that supports the underlying view V that models only input_range.
2) An implementation that supports the underlying view V that models forward_range or stronger. Models common_range if the underlying view V is forward_range, common_range, and either sized_range or non bidirectional_range.
3) The name views::chunk denotes a RangeAdaptorObject. Given subexpressions e and n, the expression views::chunk(e, n) is expression-equivalent to chunk_view(e, n).
4) Computes the smallest integer value that is not less than the quotient of dividing num by denom. Equivalent to:
I r = num / denom;
if (num % denom)
    ++r;
return r;

Contents

[edit] Expression-equivalent

Expression e is expression-equivalent to expression f, if

  • e and f have the same effects, and
  • either both are constant subexpressions or else neither is a constant subexpression, and
  • either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).

[edit] Data members

Typical implementations of chunk_view (1,2) hold two non-static data members:

  • the underlying view of type V (shown here as base_ for exposition only),
  • the "chunk size" of type ranges::range_difference_t<V> (shown here as n_ for exposition only).

In addition, if V models exactly the input_range, chunk_view (1) typically holds two more non-static data members:

  • the number of elements left in the current chunk ranges::range_difference_t<V> (shown here as remainder_ for exposition only),
  • the current cached underlying iterator __non_propagating_cache<ranges::iterator_t<V>> (shown here as current_ for exposition only).

[edit] Member functions

constructs a chunk_view
(public member function) [edit]
(C++23)
returns a copy of the underlying (adapted) view
(public member function) [edit]
(C++23)
returns an iterator to the beginning
(public member function) [edit]
(C++23)
returns an iterator or a sentinel to the end
(public member function) [edit]
(C++23)
returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range.
(public member function) [edit]
Inherited from std::ranges::view_interface
(C++20)
Returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
Returns whether the derived view is not empty. Provided if ranges::empty is applicable to it.
(public member function of std::ranges::view_interface<D>) [edit]
(C++20)
Returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
(C++20)
Returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>) [edit]
Returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>) [edit]

[edit] Deduction guides

[edit] Nested classes

the output iterator type when V models input_range (1)
(exposition-only member class template)
the inner iterator type when V models input_range (1)
(exposition-only member class template)
(C++23)
the iterator type when V models forward_range (2)
(exposition-only member class template)

[edit] Helper templates

template< class V >

    inline constexpr bool ranges::enable_borrowed_range<chunk_view<V>> =

        ranges::forward_range<V> && ranges::enable_borrowed_range<V>;
(since C++23)

This specialization of ranges::enable_borrowed_range makes chunk_view satisfy borrowed_range when the underlying view V satisfies both, the forward_range and the borrowed_range.

[edit] Notes

If V models input_range (1), chunk_view's iterator has a dedicated type: outer_iterator::value_type that is itself an input view.

If V models forward_range or stronger (2), chunk_view defers to views::take for its value_type.

If V models bidirectional_range or stronger ranges (2), the need to calculate size the last chunk correctly (from the end iterator) requires the underlying range type V to be sized_range.

Feature-test macro Value Std Comment
__cpp_lib_ranges_chunk 202202L (C++23) std::ranges::chunk_view

[edit] Example

A link to evaluate online: Compiler Explorer.

#include <algorithm>
#include <iostream>
#include <ranges>
 
auto print_subrange = [](std::ranges::viewable_range auto&& r)
{
    std::cout << "[";
    for (int pos{}; auto elem : r)
        std::cout << (pos++ ? " " : "") << elem;
    std::cout << "] ";
};
 
int main()
{
    const auto v = {1, 2, 3, 4, 5, 6};
 
    for (const unsigned width : std::views::iota(1U, 2U + v.size()))
    {
        auto const chunks = v | std::views::chunk(width);
        std::cout << "chunk(" << width << "): ";
        std::ranges::for_each(chunks, print_subrange);
        std::cout << '\n';
    }
}

Output:

chunk(1): [1] [2] [3] [4] [5] [6]
chunk(2): [1 2] [3 4] [5 6]
chunk(3): [1 2 3] [4 5 6]
chunk(4): [1 2 3 4] [5 6]
chunk(5): [1 2 3 4 5] [6]
chunk(6): [1 2 3 4 5 6]
chunk(7): [1 2 3 4 5 6]

[edit] References

  • C++23 standard (ISO/IEC 14882:2023):
  • 26.7.27 Chunk view [range.chunk]

[edit] See also

splits the view into subranges between each pair of adjacent elements for which the given predicate returns false
(class template) (range adaptor object) [edit]
a view consisting of tuples of references to adjacent elements of the adapted view
(class template) (range adaptor object) [edit]
a view whose Mth element is a view over the Mth through (M + N - 1)th elements of another view
(class template) (range adaptor object) [edit]