Replace OutputIterator and Extend Range
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Replace OutputIterator and Extend Range

  • 2,011 views
Uploaded on

C++Now! 2012, Library in a Week presentation.

C++Now! 2012, Library in a Week presentation.

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • manuscript:

    I have 2 idea for algorithm.
    replace Output Iterator and Extended Range.

    My profile...

    Caution.
    I don't can speak very little English.
    I may not answer the question immediately.

    first idea is 'OutputIteraotors Must Go'.

    This idea overview
    Now, we don't need Output Iterator.
    Because we have lambda.
    Some STL algorithm can replace from Output Iterator to UnaryFunction.

    Basic example. std::copy can replace std::for_each.
    Before, and After.
    This replacement is failry useful.


    More useful example: STL set algorithm.
    set_union, set_intersection, and set_difference.
    This code is now standard algorithm using OutputIterator.
    But, Insert Iterator Adaptor is not useful.
    Output Iterator can't customize operation easily.
    If, std::set_union's parameter is UnaryFunction...

    UnaryFunction version is very useful.
    We can customize operation easily.
    I think need add UnaryFunction version algorithm.
    UnaryFunction version implementation is here.


    ==

    2nd idea is Boost.Range extension. OvenToBoost project.

    What is OvenToBoost?
    Oven is Range Library in P-Stade C++ Libraries.
    I'm this library's committer.
    Oven is more useful than Boost Range Library.
    Now, I'm working on porting from Oven to Boost as extended Boost.Range.
    I'm working in GitHub this URL.

    Boost.Range's issue.
    Boost.Range is very small support Range Adaptors.
    and Boost.Range's Range adaptors can't use Lambda.
    Oven has solution for these issues.
    Oven provide many Range adaptors, and utilities.

    taken Range adaptor is make n length prefix range.

    dropped Range adaptor is reverse of taken.
    dropped Range adaptor make suffix Range.

    elements Range adaptor is extract elements from Range object as tuple.
    elements can applied to user defined type using Boost.Fusion.

    elements_key Range adaptor is extract elements by tag type.
    This adaptor implementation Boost.Fusion assosiative sequence.
    This adaptors is very useful for extract member variable.

    iteration function makes lazy infinite Range.
    iteration function's 2nd argument is next value calculation function.
    sequence of number of Mathematics often need infinite Range.

    regular function.
    regular function is function object conversion to Regular concept.
    Regular concept is DefaultConstructible and CopyAssignable.
    For example, Boost.Lambda and C++11's lambda is no DefaultConstructible and no CopyAssignable.
    This means, iterator of Range Adaptor with lambda is no match requirements InputIterator.
    regular function is lambda conversion to Regular Concept.

    and regular function is verbose.
    I provide regular operator 'or plus (|+)' as regular function's syntax sugar.
    regular operator is compound operator. 'or' and 'plus'.
    This design rationable.
    I need regular operator, but same primary operator of 'or' is nothing.
    So, I must makes new operator.

    Finally, Combination examle. prime list.
    This code make infinite prime list using Oven range adaptor and utilities.
    This code algorithm is エラトステネス's sieve.

    Now OvenToBoost project's status.
    Primary implementation has been complete.
    and Test has been complete.
    But documentation is not complete. So, English is difficult.
    If documentation complete, I will submit to Boost.

    Thanks!
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
2,011
On Slideshare
1,971
From Embeds
40
Number of Embeds
5

Actions

Shares
Downloads
7
Comments
1
Likes
4

Embeds 40

https://twimg0-a.akamaihd.net 19
https://si0.twimg.com 14
http://a0.twimg.com 5
http://us-w1.rockmelt.com 1
https://twitter.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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…
  • 4. 1stOutputIterators Must Go
  • 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::copystd::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_differenceSTL set algorithms using Output Iterator arent 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_differenceSTL 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. Its accutually useful, easily to customize operation. This implementation is here: https://github.com/faithandbrave/Set-Algorithm
  • 9. 2nd OvenToBoost project
  • 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.Ranges Range adaptors cant use lambda• Oven has solution for these issues
  • 12. taken Range Adaptorconst std::vector<int> v = {3, 1, 4, 2, 5};boost::for_each(v | taken(2), print);31
  • 13. dropped Range Adaptorconst std::vector<int> v = {3, 1, 4, 2, 5};boost::for_each(v | dropped(2), print);425
  • 14. elements Range Adaptorstruct 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 Adaptorstruct 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 functionint next(int x) { return x * 2; }boost::for_each(iteration(1, next) | taken(5), print);124816
  • 17. regular functiontemplate <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 listrange 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.