Boost logo

Boost :

Subject: Re: [boost] [algorithm] adjacent_for_each interest?
From: Jeff Flinn (Jeffrey.Flinn_at_[hidden])
Date: 2013-02-12 08:01:46


On 2/11/2013 5:09 AM, Evgeny Panasyuk wrote:
> 11.02.2013 2:17, Nathan Ridge:
>
>>>
>>> P.S. Consider implementation of companion - transform_adjacent.
>>> Maybe even consider version which accepts "count of adjacent elements
>>> within each iteration" as template non-type parameter.
>>> Or maybe choose some another approach involving Boost.Range?
>>
>> I think a more general approach would be a Boost.Range-style range
>> adaptor [1] that presents a view of the range where elements in
>> the view are pairs of (references to, if desired) elements in the
>> underlying range. You can then use standard algorithms like for_each
>> and transform on the adapted range.
>>
>> I have written such an adaptor in the past and called it
>> 'adjacent_paired'. I've also seen (can't remember where) the name
>> 'windowed' used for the more general version which presents a view
>> of tuples of N adjacent elements.
>
> Yes, I was thinking about something like that - use tuple views.
>
> Just for example(of tuple views):
>
> int arr[] = {0,1,2,3,4,5};
>
> for_each
> (
> make_zip_iterator( make_tuple(arr+0, arr+1) ),
> make_zip_iterator( make_tuple(end(arr)-1, end(arr)) ),
> cout << lambda::_1 << " "
> );
>
> or
>
> boost::for_each
> (
> combine(arr | sliced(0,size(arr)-1), arr | sliced(1,size(arr))),
> cout << lambda::_1 << " "
> );
>
> output: (0 1) (1 2) (2 3) (3 4) (4 5)
>
> By the way, is combine in boost/range/combine.hpp in public api? I don't
> see it on website.

Nice find! I've been missing a zip_range! A few useful additions would
be a corresponding adaptor and exposed range type(s) that are currently
in the detail namespace.

Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk