|
Boost : |
Subject: Re: [boost] [algorithm] adjacent_for_each interest?
From: Evgeny Panasyuk (evgeny.panasyuk_at_[hidden])
Date: 2013-02-10 10:06:18
10.02.2013 17:10, Ian Hobson:
> Is there any interest in a version of for_each that operates on each adjacent pair of elements in a range?
[...]
> Initial implementation available at http://github.com/irh/adjacent_for_each
I have implemented for_each_adjacent on project I worked on (sorry,
can't share source).
I had two versions: for InputIterator and ForwardIterator. Selection was
implemented as tag dispatching of iterator_category.
You need special version for InputIterator which maintains temporary
copy of value_type, due to 24.1.1/3:
[Note: For input iterators, a == b does not imply ++a == ++b. (Equality
does not guarantee the substitution property or referential
transparency.) Algorithms on input iterators should never attempt to
pass through the same iterator twice. They should be single pass
algorithms.]
In this regard your version at
https://github.com/irh/adjacent_for_each/blob/master/boost/algorithm/adjacent_for_each.hpp
is broken.
Because it advertises to work on InputIterator, but it dereferences
iterator at same position several times. As the result current
implementation is for ForwardIterator.
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?
-- Evgeny Panasyuk
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk