Difference between revisions of "cpp/ranges/lazy split view"
D41D8CD98F (Talk | contribs) |
|||
Line 37: | Line 37: | ||
* The view is an {{lconcept|input_range}}, the delimiter is a single element (wrapped in a {{rlpt|single_view}}). | * The view is an {{lconcept|input_range}}, the delimiter is a single element (wrapped in a {{rlpt|single_view}}). | ||
* The view is a {{lconcept|forward_range}}, the delimiter is a {{lconcept|view}} of elements. | * The view is a {{lconcept|forward_range}}, the delimiter is a {{lconcept|view}} of elements. | ||
− | |||
@2@ A [[cpp/ranges#Range adaptor objects|range adaptor object]]. The expression {{c|views::lazy_split(e, f)}} is ''expression-equivalent'' to {{c|lazy_split_view(e, f)}}<!-- LWG 3524 -->. | @2@ A [[cpp/ranges#Range adaptor objects|range adaptor object]]. The expression {{c|views::lazy_split(e, f)}} is ''expression-equivalent'' to {{c|lazy_split_view(e, f)}}<!-- LWG 3524 -->. | ||
Line 44: | Line 43: | ||
@4@ The exposition-only concept {{c|/*tiny_range*/<Pattern>}} is satisfied if {{c|Pattern}} satisfies {{lconcept|sized_range}}, {{c|Pattern::size()}} is a constant expression, and the value of {{c|Pattern::size()}} is less than or equal to {{tt|1}}.<!-- The standard uses `std::remove_reference_t<R>::size()`, but since `Pattern` is a `view`, it cannot be a reference. --> Notably, {{rlpt|empty_view}} and {{rlpt|single_view}} satisfy this concept. | @4@ The exposition-only concept {{c|/*tiny_range*/<Pattern>}} is satisfied if {{c|Pattern}} satisfies {{lconcept|sized_range}}, {{c|Pattern::size()}} is a constant expression, and the value of {{c|Pattern::size()}} is less than or equal to {{tt|1}}.<!-- The standard uses `std::remove_reference_t<R>::size()`, but since `Pattern` is a `view`, it cannot be a reference. --> Notably, {{rlpt|empty_view}} and {{rlpt|single_view}} satisfy this concept. | ||
+ | |||
+ | {{tt|lazy_split_view}} models the concepts {{lconcept|forward_range}} and {{lconcept|input_range}} when the underlying {{lconcept|view}} {{tt|V}} models respective concepts, and models {{lconcept|common_range}} when {{tt|V}} models both {{lconcept|forward_range}} and {{lconcept|common_range}}. | ||
+ | |||
+ | The inner range ({{c|ranges::range_reference_t<lazy_split_view>}}) models the concepts {{lconcept|forward_range}} and {{lconcept|input_range}} when the underlying {{lconcept|view}} {{tt|V}} models respective concepts. It does not model {{lconcept|common_range}}, and cannot be used with algorithms that expect a {{lconcept|bidirectional_range}} or higher. | ||
===Data members=== | ===Data members=== | ||
Line 97: | Line 100: | ||
constexpr static auto source = { 0, 1,0, 2,3,0, 4,5,6,0, 7,8,9 }; | constexpr static auto source = { 0, 1,0, 2,3,0, 4,5,6,0, 7,8,9 }; | ||
constexpr int delimiter {0}; | constexpr int delimiter {0}; | ||
− | constexpr std::ranges::lazy_split_view | + | constexpr std::ranges::lazy_split_view outer_view{source, delimiter}; |
− | std::cout << "splits[" << std::ranges::distance( | + | std::cout << "splits[" << std::ranges::distance(outer_view) << "]: "; |
− | for (auto const& | + | for (auto const& inner_view: outer_view) |
− | print( | + | print(inner_view); |
constexpr std::string_view hello { "Hello C++ 20 !" }; | constexpr std::string_view hello { "Hello C++ 20 !" }; |
Revision as of 02:06, 13 October 2021
Defined in header <ranges>
|
||
template< ranges::input_range V, ranges::forward_range Pattern > requires ranges::view<V> && ranges::view<Pattern> && |
(1) | (since C++20) |
namespace views { inline constexpr /*unspecified*/ lazy_split = /*unspecified*/; |
(2) | (since C++20) |
Helper concepts |
||
namespace ranges { template< auto > struct /*require_constant*/; // exposition only |
(3) | (since C++20) |
template< class R > concept /*tiny_range*/ = // exposition only |
(4) | (since C++20) |
lazy_split_view
takes a view
and a delimiter, and splits the view
into subranges on the delimiter.
Two major scenarios are supported:
- The view is an
input_range
, the delimiter is a single element (wrapped in asingle_view
). - The view is a
forward_range
, the delimiter is aview
of elements.
require_constant
, such that the requirement requires { typename /*require_constant*/<E>; } is satisfied if and only if E
is constexpr
qualified.sized_range
, Pattern::size() is a constant expression, and the value of Pattern::size() is less than or equal to 1
. Notably, empty_view
and single_view
satisfy this concept.lazy_split_view
models the concepts forward_range
and input_range
when the underlying view
V
models respective concepts, and models common_range
when V
models both forward_range
and common_range
.
The inner range (ranges::range_reference_t<lazy_split_view>) models the concepts forward_range
and input_range
when the underlying view
V
models respective concepts. It does not model common_range
, and cannot be used with algorithms that expect a bidirectional_range
or higher.
Contents |
Data members
Typical implementations of lazy_split_view
hold two or three non-static data members:
- the underlying
view
of typeV
(shown here asbase_
for exposition only), - the pattern (shown here as
pattern_
for exposition only) that is used as a delimiter to split the underlyingview
, and - the caching object (shown here as
current_
for exposition only) of the std::optional-like exposition-only type /*non_propagating_cache*/<ranges::iterator_t<V>>. Thecurrent_
caches the result of calls tobegin()
. Present only if the expression ranges::forward_range<V> evaluates to false.
Member functions
constructs a lazy_split_view (public member function) | |
returns a copy of the underlying (adapted) view (public member function) | |
returns an iterator to the beginning (public member function) | |
returns an iterator or a sentinel to the end (public 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> )
|
Nested classes
(C++20) |
the iterator type (exposition-only member class template*) |
(C++20) |
the iterator type of the inner range (exposition-only member class template*) |
Deduction guides
Notes
This section is incomplete Reason: R2210R2 changes to be reflected. |
Example
#include <algorithm> #include <iostream> #include <ranges> #include <string_view> // P2210R2: a temporary patch until online g++ >= 12 #define lazy_split_view split_view #define lazy_split split auto print = [](auto const& view) { // `view` is a std::views::lazy_split_view::/*outer_iterator*/::value_type for (std::cout << "{ "; const auto element : view) std::cout << element << ' '; std::cout << "} "; }; int main() { constexpr static auto source = { 0, 1,0, 2,3,0, 4,5,6,0, 7,8,9 }; constexpr int delimiter {0}; constexpr std::ranges::lazy_split_view outer_view{source, delimiter}; std::cout << "splits[" << std::ranges::distance(outer_view) << "]: "; for (auto const& inner_view: outer_view) print(inner_view); constexpr std::string_view hello { "Hello C++ 20 !" }; std::cout << "\n" "substrings: "; std::ranges::for_each(hello | std::views::lazy_split(' '), print); constexpr std::string_view text { "Hello-+-C++-+-20-+-!" }; constexpr std::string_view delim { "-+-" }; std::cout << "\n" "substrings: "; std::ranges::for_each(text | std::views::lazy_split(delim), print); }
Output:
splits[5]: { } { 1 } { 2 3 } { 4 5 6 } { 7 8 9 } substrings: { H e l l o } { C + + } { 2 0 } { ! } substrings: { H e l l o } { C + + } { 2 0 } { ! }
See also
a view over the subranges obtained from splitting another view using a delimiter(class template) (range adaptor object) | |
(C++20) |
a view consisting of the sequence obtained from flattening a view of range s(class template) (range adaptor object) |