Subject: [boost] Interest in bounded (Top-Bounded) Range adaptor.
From: Fernando Pelliccioni (fpelliccioni_at_[hidden])
Date: 2011-10-27 11:15:42


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,

The above example is similar in behavior to ...

  boost::copy_n( input, 0, std::ostream_iterator<int>(std::cout, ","));
  boost::copy_n( input, 1, std::ostream_iterator<int>(std::cout, ","));
  boost::copy_n( input, 2, std::ostream_iterator<int>(std::cout, ","));
  boost::copy_n( input, 8, std::ostream_iterator<int>(std::cout, ","));

The difference is that *copy_n* algorithm doesn't check for
   begin(range) != end(range)

In the following line, an error occurs at runtime...

  boost::copy_n( input, 9, std::ostream_iterator<int>(std::cout, ","));

Unlike the following that runs ok...

  boost::copy( input | bounded(9), std::ostream_iterator<int>(std::cout,
",")); // prints 1,2,3,4,5,7,8,9,

This behavior could be equated using *sliced(0, X)*, but sliced requires
*RandomAccessRange*, unlike bounded that requires *SinglePassRange*.
*bounded* code is based on *indexed* adaptor code, I think it is more
efficient than *sliced* ( that uses *make_iterator_range* --> std::advance
).

I don't know if this behavior can be achieved using other adapters, if so,
please let me know.

Is there interest in this for Boost?

Regards,
Fernando.