Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/ranges/lazy split view"

From cppreference.com
< cpp‎ | ranges
m (Member functions: + view_interface: inherited member functions (not much.))
m (Nested classes: rm rev-tags.)
 
(26 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/ranges/view title | lazy_split}}
+
{{cpp/ranges/view title|lazy_split}}
 
{{cpp/ranges/lazy_split_view/navbar}}
 
{{cpp/ranges/lazy_split_view/navbar}}
  
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | ranges}}
+
{{dcl header|ranges}}
{{dcl | num=1 | since=c++20 |1=
+
{{dcl|num=1|since=c++20|1=
 
template< ranges::input_range V, ranges::forward_range Pattern >
 
template< ranges::input_range V, ranges::forward_range Pattern >
requires ranges::view<V> && ranges::view<Pattern> &&
+
requires ranges::view<V> &&
  std::indirectly_comparable<ranges::iterator_t<V>, ranges::iterator_t<Pattern>,
+
        ranges::view<Pattern> &&
                            ranges::equal_to> &&
+
        std::indirectly_comparable<ranges::iterator_t<V>,
  (ranges::forward_range<V> {{!!}} /*tiny-range*/<Pattern>)
+
                                    ranges::iterator_t<Pattern>,
class lazy_split_view : public ranges::view_interface<lazy_split_view<V, Pattern>>
+
                                    ranges::equal_to> &&
 +
        (ranges::forward_range<V> {{!!}} /*tiny-range*/<Pattern>)
 +
class lazy_split_view
 +
    : public ranges::view_interface<lazy_split_view<V, Pattern>>
 
}}
 
}}
{{dcl | num=2 | since=c++20 |1=
+
{{dcl|num=2|since=c++20|1=
 
namespace views {
 
namespace views {
     inline constexpr /*unspecified*/ lazy_split = /*unspecified*/;
+
     inline constexpr /* unspecified */ lazy_split = /* unspecified */;
 
}
 
}
 +
}}
 +
{{dcl h|Call signature}}
 +
{{dcl|since=c++20|1=
 +
template< ranges::viewable_range R, class Pattern >
 +
    requires /* see below */
 +
constexpr ranges::view auto lazy_split( R&& r, Pattern&& pattern );
 +
}}
 +
{{dcl|since=c++20|1=
 +
template< class Pattern >
 +
constexpr /* range adaptor closure */ lazy_split( Pattern&& pattern );
 +
}}
 +
{{dcl h|Helper concepts}}
 +
{{dcl|num=3|notes={{mark expos}}|1=
 +
template< class R >
 +
concept /*tiny-range*/ =
 +
    ranges::sized_range<R> &&
 +
    requires { /* is-statically-constexpr-sized */<R>; } &&
 +
    (std::remove_reference_t<R>::size() <= 1);
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 22: Line 43:
 
<!---->
 
<!---->
 
Two major scenarios are supported:
 
Two major scenarios are supported:
* The view is a {{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.
<!---->
 
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.
 
  
@2@ The expression {{c|views::lazy_split(e,f)}} is ''expression-equivalent'' to {{c|lazy_split_view{e,f} }}.
+
@2@ A {{named req|RangeAdaptorObject}}. The expression {{c|views::lazy_split(e, f)}} is [[cpp/language/expressions#Expression-equivalence|expression-equivalent]] to {{c|lazy_split_view(e, f)}}<!-- LWG 3524 -->.
 +
 
 +
@3@ The exposition-only concept {{c|/*tiny-range*/<Pattern>}} is satisfied if {{tt|Pattern}} satisfies {{lconcept|sized_range}}, {{c|Pattern::size()}} is a constant expression and suitable as a template non-type argument, 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/core|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===
 +
{{dsc begin}}
 +
{{dsc hitem|Member name|Definition}}
 +
{{dsc expos mem obj|base_|private=yes|The underlying {{lconcept|view}} of type {{tt|V}}.}}
 +
{{dsc expos mem obj|pattern_|private=yes|The pattern that is used as a delimiter to split the underlying {{lconcept|view}}.}}
 +
{{dsc expos mem obj|current_|private=yes|maybe=yes|An [[cpp/ranges#Non-propagating cache|''optional-like'']] caching object of type {{c/core|1=/*non-propagating-cache*/<ranges::iterator_t<V>>}}. Caches the result of calls to {{rltf|begin}}. Present only if {{tt|V}} does not satisfy {{lconcept|forward_range}}.}}
 +
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/adaptor/dsc constructor | lazy_split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc constructor|lazy_split_view}}
{{dsc inc | cpp/ranges/adaptor/dsc base | lazy_split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc base|lazy_split_view}}
{{dsc inc | cpp/ranges/adaptor/dsc begin | lazy_split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc begin|lazy_split_view}}
{{dsc inc | cpp/ranges/adaptor/dsc end | lazy_split_view }}
+
{{dsc inc|cpp/ranges/adaptor/dsc end|lazy_split_view}}
  
 
{{cpp/ranges/view_interface/inherit|embedded=yes|data=invalid|size=invalid|back=invalid|operator[]=invalid}}
 
{{cpp/ranges/view_interface/inherit|embedded=yes|data=invalid|size=invalid|back=invalid|operator[]=invalid}}
Line 41: Line 74:
 
===Nested classes===
 
===Nested classes===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem class| cpp/ranges/lazy_split_view/outer-iterator | title=''outer-iterator'' | the iterator type}}
+
{{dsc expos mem tclass|cpp/ranges/lazy_split_view/outer_iterator|the iterator type}}
{{dsc mem class| cpp/ranges/lazy_split_view/inner-iterator | title=''inner-iterator'' | the iterator type of the inner range}}  
+
{{dsc expos mem tclass|cpp/ranges/lazy_split_view/inner_iterator|the iterator type of the inner range}}
 
{{dsc end}}
 
{{dsc end}}
  
 
==={{rl|deduction guides|Deduction guides}}===
 
==={{rl|deduction guides|Deduction guides}}===
 +
 +
===Notes===
 +
The name {{tt|lazy_split_view}} is introduced by the post-C++20 defect report {{wg21|P2210R2}}. It has the same lazy mechanism as that of the old {{tt|split_view}} before change.
  
 
===Example===
 
===Example===
Line 54: Line 90:
 
#include <ranges>
 
#include <ranges>
 
#include <string_view>
 
#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)
 
auto print = [](auto const& view)
 
{
 
{
     // `view` is a std::views::lazy_split_view::/*outer-iterator*/::value_type
+
     // `view` is of std::views::lazy_split_view::__outer_iterator::value_type
  
 
     for (std::cout << "{ "; const auto element : view)
 
     for (std::cout << "{ "; const auto element : view)
Line 70: Line 102:
 
int main()
 
int main()
 
{
 
{
     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 lazy_split_view {source, delimiter};
+
     constexpr std::ranges::lazy_split_view outer_view{source, delimiter};
     std::cout << "splits[" << std::ranges::distance(lazy_split_view) << "]:  ";
+
     std::cout << "splits[" << std::ranges::distance(outer_view) << "]:  ";
     for (auto const& lazy_split: lazy_split_view)
+
     for (auto const& inner_view: outer_view)
         print(lazy_split);
+
         print(inner_view);
  
     constexpr std::string_view hello { "Hello C++ 20 !" };
+
     constexpr std::string_view hello{"Hello C++ 20 !"};
 
     std::cout << "\n" "substrings: ";
 
     std::cout << "\n" "substrings: ";
 
     std::ranges::for_each(hello {{!}} std::views::lazy_split(' '), print);
 
     std::ranges::for_each(hello {{!}} std::views::lazy_split(' '), print);
+
 
     constexpr std::string_view text { "Hello-+-C++-+-20-+-!" };
+
     constexpr std::string_view text{"Hello-+-C++-+-20-+-!"};
     constexpr std::string_view delim { "-+-" };
+
     constexpr std::string_view delim{"-+-"};
 
     std::cout << "\n" "substrings: ";
 
     std::cout << "\n" "substrings: ";
 
     std::ranges::for_each(text {{!}} std::views::lazy_split(delim), print);
 
     std::ranges::for_each(text {{!}} std::views::lazy_split(delim), print);
 
}
 
}
| output=
+
|output=
splits[5]:  { } { 1 } { 2 3 } { 4 5 6 } { 7 8 9 }  
+
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 } { ! }
 
substrings: { H e l l o } { C + + } { 2 0 } { ! }
 
substrings: { H e l l o } { C + + } { 2 0 } { ! }
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|paper=P2210R2|std=C++20|before=the old {{tt|split_view}} was too lazy to be easily used|after=moves its functionality to {{tt|lazy_split_view}}}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/dsc split_view}}
+
{{dsc inc|cpp/ranges/dsc split_view}}
{{dsc inc | cpp/ranges/dsc join_view}}
+
{{dsc inc|cpp/ranges/dsc join_view}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|es|ja|ru|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 21:58, 20 June 2024

 
 
Ranges library
Range adaptors
 
 
Defined in header <ranges>
template< ranges::input_range V, ranges::forward_range Pattern >

requires ranges::view<V> &&
         ranges::view<Pattern> &&
         std::indirectly_comparable<ranges::iterator_t<V>,
                                    ranges::iterator_t<Pattern>,
                                    ranges::equal_to> &&
         (ranges::forward_range<V> || /*tiny-range*/<Pattern>)
class lazy_split_view

    : public ranges::view_interface<lazy_split_view<V, Pattern>>
(1) (since C++20)
namespace views {

    inline constexpr /* unspecified */ lazy_split = /* unspecified */;

}
(2) (since C++20)
Call signature
template< ranges::viewable_range R, class Pattern >

    requires /* see below */

constexpr ranges::view auto lazy_split( R&& r, Pattern&& pattern );
(since C++20)
template< class Pattern >
constexpr /* range adaptor closure */ lazy_split( Pattern&& pattern );
(since C++20)
Helper concepts
template< class R >

concept /*tiny-range*/ =
    ranges::sized_range<R> &&
    requires { /* is-statically-constexpr-sized */<R>; } &&

    (std::remove_reference_t<R>::size() <= 1);
(3) (exposition only*)
1) lazy_split_view takes a view and a delimiter, and splits the view into subranges on the delimiter.

Two major scenarios are supported:

2) A RangeAdaptorObject. The expression views::lazy_split(e, f) is expression-equivalent to lazy_split_view(e, f).
3) The exposition-only concept /*tiny-range*/<Pattern> is satisfied if Pattern satisfies sized_range, Pattern::size() is a constant expression and suitable as a template non-type argument, 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

[edit] Data members

Member name Definition
base_ (private) The underlying view of type V.
(exposition-only member object*)
pattern_ (private) The pattern that is used as a delimiter to split the underlying view.
(exposition-only member object*)
current_ (private)
(conditionally present)
An optional-like caching object of type /*non-propagating-cache*/<ranges::iterator_t<V>>. Caches the result of calls to begin(). Present only if V does not satisfy forward_range.
(exposition-only member object*)

[edit] Member functions

constructs a lazy_split_view
(public member function) [edit]
returns a copy of the underlying (adapted) view
(public member function) [edit]
returns an iterator to the beginning
(public member function) [edit]
returns an iterator or a sentinel to the end
(public member function) [edit]
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>) [edit]
(C++23)
returns a constant iterator to the beginning of the range.
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a sentinel for the constant iterator of the 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]
returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]

[edit] Nested classes

the iterator type
(exposition-only member class template*)
the iterator type of the inner range
(exposition-only member class template*)

[edit] Deduction guides

[edit] Notes

The name lazy_split_view is introduced by the post-C++20 defect report P2210R2. It has the same lazy mechanism as that of the old split_view before change.

[edit] Example

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
 
auto print = [](auto const& view)
{
    // `view` is of 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 } { ! }

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
P2210R2 C++20 the old split_view was too lazy to be easily used moves its functionality to lazy_split_view

[edit] See also

a view over the subranges obtained from splitting another view using a delimiter
(class template) (range adaptor object)[edit]
a view consisting of the sequence obtained from flattening a view of ranges
(class template) (range adaptor object)[edit]