Difference between revisions of "cpp/ranges/chunk by view"
m (→Notes: ~FTM: tt→ttt, i.e. make local link.) |
m (→Data members: fmt using {box}.) |
||
Line 37: | Line 37: | ||
Typical implementations of {{tt|chunk_by_view}} hold three non-static data members: | Typical implementations of {{tt|chunk_by_view}} hold three non-static data members: | ||
* the underlying {{lconcept|view}} of type {{tt|V}} (shown here as {{tt|''base_''}} for exposition only), | * the underlying {{lconcept|view}} of type {{tt|V}} (shown here as {{tt|''base_''}} for exposition only), | ||
− | * a wrapper that wraps the predicate used to split the elements of {{tt|''base_''}} of type {{ | + | * a wrapper that wraps the predicate used to split the elements of {{tt|''base_''}} of type {{box|{{ltt|cpp/ranges/copyable_wrapper|''movable-box''}}{{tt|''<Pred>''}}}} (shown here as {{tt|''pred_''}} for exposition only), where {{ltt|cpp/ranges/copyable_wrapper|''movable-box''}} is a wrapper class template that always satisfies {{lconcept|movable}}, <!-- P2325R3 --> <!-- "copyable" => "movable" --> |
* an object of {{lc|std::optional}}-like type (shown here as {{tt|''begin_''}} for exposition only) that caches an iterator to the first element. | * an object of {{lc|std::optional}}-like type (shown here as {{tt|''begin_''}} for exposition only) that caches an iterator to the first element. | ||
Revision as of 01:54, 12 February 2023
Defined in header <ranges>
|
||
template< ranges::forward_range V, std::indirect_binary_predicate<iterator_t<V>, ranges::iterator_t<V>> Pred > |
(1) | (since C++23) |
namespace views { inline constexpr /* unspecified */ chunk_by = /* unspecified */ ; |
(2) | (since C++23) |
Call signature |
||
template< ranges::viewable_range R, class Pred > requires /* see below */ |
(since C++23) | |
template< class Pred > constexpr /*range adaptor closure*/ chunk_by( Pred&& pred ); |
(since C++23) | |
chunk_by_view
is a range adaptor that takes a view
and an invocable object pred (the binary predicate), and produces a view
of subranges (chunks), by splitting the underlying view between each pair of adjacent elements for which pred returns false. The first element of each such pair belongs to the previous chunk, and the second element belongs to the next chunk.chunk_by_view
always models forward_range
, and models bidirectional_range
and/or common_range
, if adapted view
type models the corresponding concepts.
chunk_by_view
never models borrowed_range
or sized_range
.
Contents |
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)).
Data members
Typical implementations of chunk_by_view
hold three non-static data members:
- the underlying
view
of typeV
(shown here asbase_
for exposition only), - a wrapper that wraps the predicate used to split the elements of
base_
of type movable-box<Pred>
(shown here aspred_
for exposition only), where movable-box is a wrapper class template that always satisfiesmovable
, - an object of std::optional-like type (shown here as
begin_
for exposition only) that caches an iterator to the first element.
Member functions
constructs a chunk_by_view (public member function) | |
returns a copy of the underlying (adapted) view (public member function) | |
returns a reference to the stored predicate (public member function) | |
returns an iterator to the beginning (public member function) | |
returns an iterator or a sentinel to the end (public member function) | |
(C++23) |
returns an iterator to the begin of the next subrange (the name is for exposition only) (exposition-only member function*) |
(C++23) |
returns an iterator to the begin of the previous subrange (the name is for exposition only) (exposition-only member function*) |
Inherited from std::ranges::view_interface | |
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> )
| |
(C++23) |
returns a constant iterator to the beginning of the range. (public member function of std::ranges::view_interface<D> )
|
(C++23) |
returns a sentinel for the constant iterator of the range. (public member function of std::ranges::view_interface<D> )
|
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> )
| |
returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> )
| |
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> )
|
Deduction guides
Nested classes
(C++23) |
the iterator type (exposition-only member class template*) |
Notes
In order to provide the amortized constant time complexity required by the range
concept, the result of begin
is cached within the chunk_by_view
object. If the underlying range is modified after the first call to begin(), subsequent uses of the chunk_by_view
object might have unintuitive behavior.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_chunk_by |
202202L | (C++23) | std::ranges::chunk_by_view
|
Example
A link to test: Compiler Explorer
#include <functional> #include <iostream> #include <ranges> #include <string_view> #include <vector> void print_chunks(auto view, std::string_view separator = " ") { for (auto const subrange : view) { std::cout << "["; for (std::string_view prefix = ""; auto const &elem : subrange) { std::cout << prefix << elem; prefix = separator; } std::cout << "] "; } std::cout << '\n'; } int main() { { auto v = std::vector{1, 2, 3, 1, 2, 3, 3, 3, 1, 2, 3}; auto fun = std::ranges::less{}; auto view = v | std::views::chunk_by(fun); print_chunks(view); } { auto v = std::vector{1, 2, 3, 4, 4, 0, 2, 3, 3, 3, 2, 1}; auto fun = std::not_fn(std::ranges::equal_to{}); // or ranges::not_equal_to auto view = v | std::views::chunk_by(fun); print_chunks(view); } { std::string v = "__cpp_lib_ranges_chunk_by"; auto fun = [](char x, char y) { return not(x == '_' or y == '_'); }; auto view = v | std::views::chunk_by(fun); print_chunks(view, ""); } }
Output:
[1 2 3] [1 2 3] [3] [3] [1 2 3] [1 2 3 4] [4 0 2 3] [3] [3 2 1] [_] [_] [cpp] [_] [lib] [_] [ranges] [_] [chunk] [_] [by]
References
- C++23 standard (ISO/IEC 14882:2024):
- 26.7.29 Chunk by view [range.chunk.by]
See also
a range of view s that are N -sized non-overlapping successive chunks of the elements of another view (class template) (range adaptor object) | |
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) | |
a view consisting of elements of another view , advancing over N elements at a time(class template) (range adaptor object) |