[Range] Sliced range adaptor, alternative for non RandomAccessRanges?
Hi, Currently it seems that the sliced range adaptor [1] requires a Random Access Range to do its job. Hence it can't be used for other ranges which dont model this concept. I want to use it with a Forward Range, specifically a std::set iterator range. What I want to do is this: std::set<int> input; input += 1,2,3,4,5,6,7,8,9; boost::copy( input | sliced(2, 5), std::ostream_iterator<int>(std::cout, ",")); (as per the sliced example, but with a std::set instead of a std::vector). I don't know much about the internals of the sliced adapter, but could it be changed to work a similar manner to counting_range, in order to locate the relevant slice of the input range? This would allow it to operate efficiently on Forward Ranges. Failing this, is there an efficient alternative to the above example? [1]: http://www.boost.org/doc/libs/1_45_0/libs/range/doc/html/range/reference/ada...)
Den 25-01-2011 07:22, Alastair Rankine skrev:
Hi,
Currently it seems that the sliced range adaptor [1] requires a Random Access Range to do its job. Hence it can't be used for other ranges which dont model this concept. I want to use it with a Forward Range, specifically a std::set iterator range.
What I want to do is this:
std::set<int> input; input += 1,2,3,4,5,6,7,8,9;
boost::copy( input | sliced(2, 5), std::ostream_iterator<int>(std::cout, ","));
(as per the sliced example, but with a std::set instead of a std::vector).
I don't know much about the internals of the sliced adapter, but could it be changed to work a similar manner to counting_range, in order to locate the relevant slice of the input range? This would allow it to operate efficiently on Forward Ranges.
Failing this, is there an efficient alternative to the above example?
make_iterator_range() have similar functionality. I guess it would make sense to extend sliced() to work in terms of advance() too, so it can work even with input iterators. -Thorsten
What I want to do is this:
std::set<int> input; input += 1,2,3,4,5,6,7,8,9;
boost::copy( input | sliced(2, 5),
make_iterator_range() have similar functionality.
I guess it would make sense to extend sliced() to work in terms of advance() too, so it can work even with input iterators.
Do you really want the slice to positional (i.e., positions 2-5) in that sequence, or do you want the slice to behave more like equal_range and select the range based on the values of the underlying sequence? Andrew
On 2011-01-27 00:35:26 +1100, Thorsten Ottosen said:
Den 25-01-2011 07:22, Alastair Rankine skrev:
I don't know much about the internals of the sliced adapter, but could it be changed to work a similar manner to counting_range, in order to locate the relevant slice of the input range? This would allow it to operate efficiently on Forward Ranges.
Failing this, is there an efficient alternative to the above example?
make_iterator_range() have similar functionality.
I guess it would make sense to extend sliced() to work in terms of advance() too, so it can work even with input iterators.
Yeah I think that would be useful. make_iterator_range is useful but not particularly efficient for the usage I have in mind, which is as follows. Basically I have a set of X and I want to construct a string representation, but truncate it if it gets too long. In other words I really only want the first 20 elements or so (think a log entry for example). The problem with make_iterator_range is that it uses std::advance from the end of the input range to make the new range. So that's basically a walk from the end of the set to (almost) the begining, which is O(n) or so. All this to get the first 20 elements of a set, seems a bit wasteful. BTW there is a (minor) bug in the documentation for sliced() [1]. The example code produces the output "3,4,5," not "3,4,5" as stated. The trailing comma is an artifact of intended behaviour of std::ostream_iterator as I understand it. Thanks, [1]: http://www.boost.org/doc/libs/1_45_0/libs/range/doc/html/range/reference/ada...
participants (3)
-
Alastair Rankine
-
Andrew Sutton
-
Thorsten Ottosen