[foreach] reverse foreach?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[foreach] reverse foreach?

Andy Little
FWIW Heres some sample code. Its seems a bit lopsided!

 std::ofstream out("toolpath_out.txt");

    BOOST_FOREACH(boost::shared_ptr<section> sp,vect){

        if (sp->m_action == section::reverse){
           for( std::vector<std::string>::reverse_iterator iter
                = sp->m_vector.rbegin();
                    iter != sp->m_vector.rend();
                        ++iter){
                out << *iter << '\n';
            }
        }
        else {
            BOOST_FOREACH(std::string str,sp->m_vector){
                out << str << '\n';
            }
        }
    }

Are there any plans for a BOOST_REVERSE_FOREACH?

regards
Andy Little





_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [foreach] reverse foreach?

Shunsuke Sogame
Andy Little wrote:

> FWIW Heres some sample code. Its seems a bit lopsided!
>
>  std::ofstream out("toolpath_out.txt");
>
>     BOOST_FOREACH(boost::shared_ptr<section> sp,vect){
>
>         if (sp->m_action == section::reverse){
>            for( std::vector<std::string>::reverse_iterator iter
>                 = sp->m_vector.rbegin();
>                     iter != sp->m_vector.rend();
>                         ++iter){
>                 out << *iter << '\n';
>             }
>         }
>         else {
>             BOOST_FOREACH(std::string str,sp->m_vector){
>                 out << str << '\n';
>             }
>         }
>     }
>
> Are there any plans for a BOOST_REVERSE_FOREACH?

I hear the new range library is under construction,
which will enable:

     BOOST_FOREACH (int x, vect|reversed) {

     }


A current workaround is maybe something like:

     template< class Range >
     boost::iterator_range<
         boost::reverse_iterator<
             typename boost::range_result_iterator<Range>::type
         >
     >
     make_reverse_range(Range& rng)
     {
         return boost::make_iterator_range(
             boost::make_reverse_iterator(boost::end(rng)),
             boost::make_reverse_iterator(boost::begin(rng))
         );
     }

     BOOST_FOREACH (int x, ::make_reverse_range(vect)) {

     }

--
Regards,
Shunsuke Sogame

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [foreach] reverse foreach?

Andy Little

"Shunsuke Sogame"  wrote
> Andy Little wrote:

>> Are there any plans for a BOOST_REVERSE_FOREACH?
>
> I hear the new range library is under construction,
> which will enable:
>
>     BOOST_FOREACH (int x, vect|reversed) {
>
>     }

That looks neat!

>     BOOST_FOREACH (int x, ::make_reverse_range(vect)) {

That looks quite neat too... :-)


Thanks!

regards
Andy Little




_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [foreach] reverse foreach?

David Abrahams
In reply to this post by Andy Little
"Andy Little" <[hidden email]> writes:

> FWIW Heres some sample code. Its seems a bit lopsided!
>
>  std::ofstream out("toolpath_out.txt");
>
>     BOOST_FOREACH(boost::shared_ptr<section> sp,vect){
>
>         if (sp->m_action == section::reverse){
>            for( std::vector<std::string>::reverse_iterator iter
>                 = sp->m_vector.rbegin();
>                     iter != sp->m_vector.rend();
>                         ++iter){
>                 out << *iter << '\n';
>             }
>         }
>         else {
>             BOOST_FOREACH(std::string str,sp->m_vector){
>                 out << str << '\n';
>             }
>         }
>     }
>
> Are there any plans for a BOOST_REVERSE_FOREACH?

I suggest you just make a nice reverse adaptor that models Range,
wrapping the underlying iterators in boost::reverse_iterator, and an
object generator that builds instances.  Then you can

   BOOST_FOREACH( whatever, reverse_view( the_sequence ) )
      ...

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [foreach] reverse foreach?

Andy Little

"David Abrahams"
> "Andy Little"  writes:

>> Are there any plans for a BOOST_REVERSE_FOREACH?
>
> I suggest you just make a nice reverse adaptor that models Range,
> wrapping the underlying iterators in boost::reverse_iterator, and an
> object generator that builds instances.  Then you can
>
>   BOOST_FOREACH( whatever, reverse_view( the_sequence ) )


Ok Thanks!

regards
Andy Little




_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users