Subject: Re: [boost] Interest in bounded (Top-Bounded) Range adaptor.
From: Fernando Pelliccioni (fpelliccioni_at_[hidden])
Date: 2011-10-31 08:17:38


On Fri, Oct 28, 2011 at 9:28 PM, Dave Abrahams <dave_at_[hidden]> wrote:

>
> on Thu Oct 27 2011, Fernando Pelliccioni <fpelliccioni-AT-gmail.com> wrote:
>
> > Is there interest in adding a Top-Bounded adaptor into Boost.Range ?
> >
> > Example:
> >
> > using namespace boost::adaptors;
> > using namespace boost::assign;
> >
> > std::vector<int> input;
> > input += 1,2,3,4,5,6,7,8,9;
> >
> > boost::copy( input | bounded(0), std::ostream_iterator<int>(std::cout,
> > ",")); // prints nothing
> > boost::copy( input | bounded(1), std::ostream_iterator<int>(std::cout,
> > ",")); // prints 1,
> > boost::copy( input | bounded(2), std::ostream_iterator<int>(std::cout,
> > ",")); // prints 1,2,
> > boost::copy( input | bounded(8), std::ostream_iterator<int>(std::cout,
> > ",")); // prints 1,2,3,4,5,7,8,
> >
> > Is there interest in this for Boost?
>
> There is an obvious generalization that stops the range when a
> predicate is satisfied, e.g.
>
> boost::copy( input | until(_1 == 8),
> std::ostream_iterator<int>(std::cout, ","));
>
> That leads me to believe that the proper behavior of the
> less-generalized component, which you call "bounded," is to stop the
> range when the element reaches the bound, rather than including it.
>
>

I think I have explained incorrectly. The above example is not appropriate.
What I want to achieve ( using ranges ) is the following...

//-------------------------------------------------------
#include <iostream>
#include <vector>
#include <boost/assign.hpp>
#include <boost/range/adaptors.hpp>

int main()
{
    using namespace boost::adaptors;
    using namespace boost::assign;

    std::vector<int> input;
    input += 0,1,1,2,3,5,8,13,21,34,55;

    auto it = boost::begin(input);
    auto end = boost::end(input);
    const int N = 9; // top bound

    for ( int i = 0; it != end && i < N; ++it, ++i )
    {
        std::cout << *it << ','; // or whatever ...
    }
}
//-------------------------------------------------------

// prints: 0,1,1,2,3,5,8,13,21,
// ( 9 elements )

Similar to the algorithm *copy_n*, with the difference that *copy_n*doesn't
check the end of the iterator.
Perhaps "bound" isn't the best name, there should be a better name, maybe
"top" ... I dont know...

Could this behavior be replicated with other adaptors?
Is the "until" adaptor part of Boost?

Regards,
Fernando.