SlideShare a Scribd company logo
Replace OutputIterator and Extended Range



                                     Akira Takahashi (Japan)
                                          LongGate CO.,LTD.
Site: https://sites.google.com/site/faithandbrave/about/en
                                         Twitter: @cpp_akira
                                  C++Now! 2012 Library in a Week
Akira Takahashi profile
• C++ Standard Committee, Japan Expert Member
• P-Stade C++ Libraries committer
• Japanese C++ Community Manager
   – boostjp : Boost Japanese Information Site
      https://sites.google.com/site/boostjp/
   – cpprefjp: C++11 Library Reference Site
      https://sites.google.com/site/cpprefjp/
   – Blog : Japanese C++ Programmers Activity
      http://cppjp.blogspot.com/
   – Boost.StudyMeeting (so to say, Japanese BoostCon/C++Now!)
       • participation person is 100+
• My Book : C++ Template Techniques
  http://www.amazon.co.jp/dp/4797354534/
• My Magazine: Programmers' Grimoire
  http://longgate.co.jp/products.html
note:
        I can speak very little English!
I may not answer your question immediately…
1st

OutputIterators Must Go
This Idea Overview
• Output Iterators are now unnecessary
  because C++11 is there.
• Some STL algorithm can replace
  from Output Iterator to UnaryFunction.
Basic Example: std::copy
std::copy can replace std::for_each with lambda.
 Before:
 std::vector<int> v = {1, 2, 3};
 std::vector<int> result;

 std::copy(v.begin(), v.end(), std::back_inserter(result));
 After:
 std::vector<int> v = {1, 2, 3};
 std::vector<int> result;

 std::for_each(v.begin(), v.end(),
                   [&](int x) { result.push_back(x); });

            This replacement is failry useful.
More Useful Example:
   set_union, set_intersection, set_difference
STL set algorithms using Output Iterator aren't useful.

 Now STL Algorithm
 std::set<int> a = {1, 2, 3};
 std::set<int> b = {4, 5, 6};
 std::set<int> result;

 std::set_union(a.begin(), a.end(),
                 b.begin(), b.end(),
                 std::inserter(result, result.end()));


               Insert Iterator Adaptor is not useful!
                  Custom operation is not easy.
More Useful Example:
   set_union, set_intersection, set_difference
STL set algorithm using Output Iterator. Not useful.

 New STL Algorithm
 std::set<int> a = {1, 2, 3};
 std::set<int> b = {4, 5, 6};
 std::set<int> result;

 make_union(a.begin(), a.end(),
             b.begin(), b.end(),
             [](int x) { result.insert(x); });

       Output Iterator canreplace to UnaryFunction.
   It's accutually useful, easily to customize operation.
                This implementation is here:
     https://github.com/faithandbrave/Set-Algorithm
2nd

 OvenToBoost project
OvenToBoost project overview
• Oven is Range Library in P-Stade C++ Libraries
• Oven is more useful than Boost.Range
• OvenToBoost project is porting from Oven To Boost
  as extended Boost.Range
• https://github.com/faithandbrave/OvenToBoost
Boost.Range issues
• There are not many Range adaptors.
  – nothing "taken"
  – nothing "dropped"
  – nothing Infinite Range
  – etc…
• Boost.Range's Range adaptors can't use
  lambda
• Oven has solution for these issues
taken Range Adaptor

const std::vector<int> v = {3, 1, 4, 2, 5};

boost::for_each(v | taken(2), print);

3
1
dropped Range Adaptor

const std::vector<int> v = {3, 1, 4, 2, 5};

boost::for_each(v | dropped(2), print);

4
2
5
elements Range Adaptor
struct Person {
  int id;
  std::string name;
  …
};
BOOST_FUSION_ADAPT_STRUCT(…)

const std::vector<Person> v = {
    {1, "Alice"}
    {2, "Carol"}
    {3, "Bob"}
 };

boost::for_each(v | elements<1>(), print);

Alice,Carol,Bob
elements_key Range Adaptor
struct id_tag {}; struct name_tag {};

struct Person {
  int id;
  std::string name;
  …
};
BOOST_FUSION_ADAPT_ASSOC_STRUCT(…)

const std::vector<Person> v = {
    {1, "Alice"}
    {2, "Carol"}
    {3, "Bob"}
 };

boost::for_each(v | elements_key<name_tag>(), print);

Alice,Carol,Bob
iteration function

int next(int x) { return x * 2; }

boost::for_each(iteration(1, next) | taken(5), print);

1
2
4
8
16
regular function
template <class InputIterator, class F>
F for_each_(InputIterator first, InputIterator last, F f) {
  InputIterator it; // default construct
  it = first; // copy assign

    while (it != last) { f(*it); ++i; }
    return f;
}

template <class Range, class F>
F for_each_(const Range& r, F f)
{ return for_each(boost::begin(r), boost::end(r), f); }

using boost::lambda::_1;
for_each_(r | filtered(_1 % 2 == 0), f);          // Error!
for_each_(r | filtered(regular(_1 % 2 == 0)), f); // OK
regular operator|+()
template <class InputIterator, class F>
F for_each_(InputIterator first, InputIterator last, F f) {
  InputIterator it; // default construct
  it = first; // copy assign

    while (it != last) { f(*it); ++i; }
    return f;
}

template <class Range, class F>
F for_each_(const Range& r, F f)
{ return for_each(boost::begin(r), boost::end(r), f); }

using boost::lambda::_1;
for_each_(r | filtered(_1 % 2 == 0), f); // Error!
for_each_(r |+ filtered(_1 % 2 == 0), f); // OK
Combination Example: Prime list
range sieve(range r)
{
  return r | dropped(1) |+ filtered(_1 % value_front(r) != 0);
}

range primes =
    iteration(range(
      iteration(2, regular(_1 + 1))), sieve) | transformed(value_front);


for_each(primes, print);


2 3 5 7 11 …
OvenToBoost now status
•   Primary implementation has been complete.
•   Test has been complete.
•   But documentation is late…
•   I would like to submit a review request to Boost.

More Related Content

PDF
C++ references
PDF
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
PPTX
Idiomatic C++
PPT
STL ALGORITHMS
PDF
Generic programming and concepts that should be in C++
PPTX
Summary of C++17 features
PPT
Function overloading(C++)
PDF
Ds lab handouts
C++ references
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
Idiomatic C++
STL ALGORITHMS
Generic programming and concepts that should be in C++
Summary of C++17 features
Function overloading(C++)
Ds lab handouts

What's hot (20)

PDF
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
PDF
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
PDF
Virtual Functions
PDF
46630497 fun-pointer-1
PPT
Pointers+(2)
PPT
Pointers
PDF
Java Script Workshop
PDF
C++ idioms by example (Nov 2008)
PPTX
Functions (Computer programming and utilization)
PPTX
C++ Presentation
ODP
OpenGurukul : Language : C++ Programming
PPT
Pointers in C
PDF
03 function overloading
PPT
C tutorial
PDF
Solid C++ by Example
PPTX
C++ programming function
PPTX
The Style of C++ 11
PPTX
Function C++
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
Virtual Functions
46630497 fun-pointer-1
Pointers+(2)
Pointers
Java Script Workshop
C++ idioms by example (Nov 2008)
Functions (Computer programming and utilization)
C++ Presentation
OpenGurukul : Language : C++ Programming
Pointers in C
03 function overloading
C tutorial
Solid C++ by Example
C++ programming function
The Style of C++ 11
Function C++
Ad

Similar to Replace OutputIterator and Extend Range (20)

PPT
10.ppt
PDF
Functions And Header Files In C++ | Bjarne stroustrup
PPT
What's New in C++ 11?
PDF
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
PPT
PPT
Csdfsadf
PPTX
Return of c++
PDF
Writing DSL with Applicative Functors
PDF
Memory Management with Java and C++
PPTX
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
PDF
A Recovering Java Developer Learns to Go
PPTX
C Programming Homework Help
PPTX
NetPonto - The Future Of C# - NetConf Edition
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPT
C++: Constructor, Copy Constructor and Assignment operator
PPTX
C to perl binding
PPTX
C++ Overview PPT
PPT
C++ Advanced
PPT
02basics
10.ppt
Functions And Header Files In C++ | Bjarne stroustrup
What's New in C++ 11?
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Csdfsadf
Return of c++
Writing DSL with Applicative Functors
Memory Management with Java and C++
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
A Recovering Java Developer Learns to Go
C Programming Homework Help
NetPonto - The Future Of C# - NetConf Edition
Chp8_C++_Functions_Part2_User-defined functions.pptx
C++: Constructor, Copy Constructor and Assignment operator
C to perl binding
C++ Overview PPT
C++ Advanced
02basics
Ad

More from Akira Takahashi (20)

PPTX
Cpp20 overview language features
PDF
Cppmix 02
PPTX
Cppmix 01
PDF
Modern C++ Learning
PDF
cpprefjp documentation
PDF
C++1z draft
PDF
Boost tour 1_61_0 merge
PDF
Boost tour 1_61_0
PDF
error handling using expected
PDF
Boost tour 1.60.0 merge
PDF
Boost tour 1.60.0
PDF
Boost container feature
PDF
Boost Tour 1_58_0 merge
PDF
Boost Tour 1_58_0
PDF
C++14 solve explicit_default_constructor
PDF
C++14 enum hash
PDF
Multi paradigm design
PDF
Start Concurrent
PDF
Programmer mind
PDF
Boost.Study 14 Opening
Cpp20 overview language features
Cppmix 02
Cppmix 01
Modern C++ Learning
cpprefjp documentation
C++1z draft
Boost tour 1_61_0 merge
Boost tour 1_61_0
error handling using expected
Boost tour 1.60.0 merge
Boost tour 1.60.0
Boost container feature
Boost Tour 1_58_0 merge
Boost Tour 1_58_0
C++14 solve explicit_default_constructor
C++14 enum hash
Multi paradigm design
Start Concurrent
Programmer mind
Boost.Study 14 Opening

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.
Sensors and Actuators in IoT Systems using pdf
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
cuic standard and advanced reporting.pdf
Advanced Soft Computing BINUS July 2025.pdf
Electronic commerce courselecture one. Pdf
Chapter 3 Spatial Domain Image Processing.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Advanced methodologies resolving dimensionality complications for autism neur...
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx

Replace OutputIterator and Extend Range

  • 1. Replace OutputIterator and Extended Range Akira Takahashi (Japan) LongGate CO.,LTD. Site: https://sites.google.com/site/faithandbrave/about/en Twitter: @cpp_akira C++Now! 2012 Library in a Week
  • 2. Akira Takahashi profile • C++ Standard Committee, Japan Expert Member • P-Stade C++ Libraries committer • Japanese C++ Community Manager – boostjp : Boost Japanese Information Site https://sites.google.com/site/boostjp/ – cpprefjp: C++11 Library Reference Site https://sites.google.com/site/cpprefjp/ – Blog : Japanese C++ Programmers Activity http://cppjp.blogspot.com/ – Boost.StudyMeeting (so to say, Japanese BoostCon/C++Now!) • participation person is 100+ • My Book : C++ Template Techniques http://www.amazon.co.jp/dp/4797354534/ • My Magazine: Programmers' Grimoire http://longgate.co.jp/products.html
  • 3. note: I can speak very little English! I may not answer your question immediately…
  • 5. This Idea Overview • Output Iterators are now unnecessary because C++11 is there. • Some STL algorithm can replace from Output Iterator to UnaryFunction.
  • 6. Basic Example: std::copy std::copy can replace std::for_each with lambda. Before: std::vector<int> v = {1, 2, 3}; std::vector<int> result; std::copy(v.begin(), v.end(), std::back_inserter(result)); After: std::vector<int> v = {1, 2, 3}; std::vector<int> result; std::for_each(v.begin(), v.end(), [&](int x) { result.push_back(x); }); This replacement is failry useful.
  • 7. More Useful Example: set_union, set_intersection, set_difference STL set algorithms using Output Iterator aren't useful. Now STL Algorithm std::set<int> a = {1, 2, 3}; std::set<int> b = {4, 5, 6}; std::set<int> result; std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::inserter(result, result.end())); Insert Iterator Adaptor is not useful! Custom operation is not easy.
  • 8. More Useful Example: set_union, set_intersection, set_difference STL set algorithm using Output Iterator. Not useful. New STL Algorithm std::set<int> a = {1, 2, 3}; std::set<int> b = {4, 5, 6}; std::set<int> result; make_union(a.begin(), a.end(), b.begin(), b.end(), [](int x) { result.insert(x); }); Output Iterator canreplace to UnaryFunction. It's accutually useful, easily to customize operation. This implementation is here: https://github.com/faithandbrave/Set-Algorithm
  • 10. OvenToBoost project overview • Oven is Range Library in P-Stade C++ Libraries • Oven is more useful than Boost.Range • OvenToBoost project is porting from Oven To Boost as extended Boost.Range • https://github.com/faithandbrave/OvenToBoost
  • 11. Boost.Range issues • There are not many Range adaptors. – nothing "taken" – nothing "dropped" – nothing Infinite Range – etc… • Boost.Range's Range adaptors can't use lambda • Oven has solution for these issues
  • 12. taken Range Adaptor const std::vector<int> v = {3, 1, 4, 2, 5}; boost::for_each(v | taken(2), print); 3 1
  • 13. dropped Range Adaptor const std::vector<int> v = {3, 1, 4, 2, 5}; boost::for_each(v | dropped(2), print); 4 2 5
  • 14. elements Range Adaptor struct Person { int id; std::string name; … }; BOOST_FUSION_ADAPT_STRUCT(…) const std::vector<Person> v = { {1, "Alice"} {2, "Carol"} {3, "Bob"} }; boost::for_each(v | elements<1>(), print); Alice,Carol,Bob
  • 15. elements_key Range Adaptor struct id_tag {}; struct name_tag {}; struct Person { int id; std::string name; … }; BOOST_FUSION_ADAPT_ASSOC_STRUCT(…) const std::vector<Person> v = { {1, "Alice"} {2, "Carol"} {3, "Bob"} }; boost::for_each(v | elements_key<name_tag>(), print); Alice,Carol,Bob
  • 16. iteration function int next(int x) { return x * 2; } boost::for_each(iteration(1, next) | taken(5), print); 1 2 4 8 16
  • 17. regular function template <class InputIterator, class F> F for_each_(InputIterator first, InputIterator last, F f) { InputIterator it; // default construct it = first; // copy assign while (it != last) { f(*it); ++i; } return f; } template <class Range, class F> F for_each_(const Range& r, F f) { return for_each(boost::begin(r), boost::end(r), f); } using boost::lambda::_1; for_each_(r | filtered(_1 % 2 == 0), f); // Error! for_each_(r | filtered(regular(_1 % 2 == 0)), f); // OK
  • 18. regular operator|+() template <class InputIterator, class F> F for_each_(InputIterator first, InputIterator last, F f) { InputIterator it; // default construct it = first; // copy assign while (it != last) { f(*it); ++i; } return f; } template <class Range, class F> F for_each_(const Range& r, F f) { return for_each(boost::begin(r), boost::end(r), f); } using boost::lambda::_1; for_each_(r | filtered(_1 % 2 == 0), f); // Error! for_each_(r |+ filtered(_1 % 2 == 0), f); // OK
  • 19. Combination Example: Prime list range sieve(range r) { return r | dropped(1) |+ filtered(_1 % value_front(r) != 0); } range primes = iteration(range( iteration(2, regular(_1 + 1))), sieve) | transformed(value_front); for_each(primes, print); 2 3 5 7 11 …
  • 20. OvenToBoost now status • Primary implementation has been complete. • Test has been complete. • But documentation is late… • I would like to submit a review request to Boost.