[Boost.Iterator] transform_iterator equivalent for output iterators
Here is my problem: for any given algorithm that writes its output into an output iterator, I'd like to apply a transformation *before* writing to the output iterator. e.g. inside the algorithm the output iterator is dereferenced and incremented to write each result, typically like this: *output++ = ith_result; I'd like to wrap the output iterator with a unary function func such that the effect is equivalent to: *output++ = func( ith_result ); I'm looking at the adaptors in Boost.Iterator but I cannot find a suitable one. Am I missing anything? Is there a better approach? Regards
On 7/30/2015 7:06 AM, dariomt@gmail.com wrote:
Here is my problem: for any given algorithm that writes its output into an output iterator, I'd like to apply a transformation *before* writing to the output iterator.
e.g. inside the algorithm the output iterator is dereferenced and incremented to write each result, typically like this:
*output++ = ith_result;
I'd like to wrap the output iterator with a unary function func such that the effect is equivalent to:
*output++ = func( ith_result );
I'm looking at the adaptors in Boost.Iterator but I cannot find a suitable one.
Am I missing anything? Is there a better approach?
Can you not use the function_output_iterator to accomplish what you want to do ?
On Thu, Jul 30, 2015 at 5:08 PM, Edward Diener
On 7/30/2015 7:06 AM, dariomt@gmail.com wrote:
Here is my problem: for any given algorithm that writes its output into an output iterator, I'd like to apply a transformation *before* writing to the output iterator.
Can you not use the function_output_iterator to accomplish what you want to do ?
Please point me to it? I can't seem to find any such thing in Boost 1.58.
On Fri, Jul 31, 2015 at 8:14 AM, Nat Goodspeed
On Thu, Jul 30, 2015 at 5:08 PM, Edward Diener
wrote:
Can you not use the function_output_iterator to accomplish what you want to do ?
Please point me to it? I can't seem to find any such thing in Boost 1.58.
Never mind. Silly me. I was looking under the iterator library.
Edward Diener
On 7/30/2015 7:06 AM, dariomt <at> gmail.com wrote:
Here is my problem: for any given algorithm that writes its output
into
an output iterator, I'd like to apply a transformation *before* writing to the output iterator.
e.g. inside the algorithm the output iterator is dereferenced and incremented to write each result, typically like this:
*output++ = ith_result;
I'd like to wrap the output iterator with a unary function func such that the effect is equivalent to:
*output++ = func( ith_result );
I'm looking at the adaptors in Boost.Iterator but I cannot find a suitable one.
Am I missing anything? Is there a better approach?
Can you not use the function_output_iterator to accomplish what you want to do ?
IIUC the function_output_iterator "fakes" an output iterator with a function object that gets called for each element written to the iterator. I need to wrap *both* an actual output iterator and a function object, and I need to apply the function *before* writing to the actual output iterator. Given that the output iterator is written by dereference and assignment, I think I would need some kind of proxy reference, that when assigned to applies the function before doing the actual assignment. That looks ugly, so I'm asking here for a more elegant solution :)
On 7/31/15 8:40 AM, dariomt wrote:
Edward Diener
writes: On 7/30/2015 7:06 AM, dariomt <at> gmail.com wrote:
Here is my problem: for any given algorithm that writes its output
into
an output iterator, I'd like to apply a transformation *before* writing to the output iterator.
e.g. inside the algorithm the output iterator is dereferenced and incremented to write each result, typically like this:
*output++ = ith_result;
I'd like to wrap the output iterator with a unary function func such that the effect is equivalent to:
*output++ = func( ith_result );
I'm looking at the adaptors in Boost.Iterator but I cannot find a suitable one.
Am I missing anything? Is there a better approach?
Can you not use the function_output_iterator to accomplish what you want to do ?
IIUC the function_output_iterator "fakes" an output iterator with a function object that gets called for each element written to the iterator.
I need to wrap *both* an actual output iterator and a function object, and I need to apply the function *before* writing to the actual output iterator.
Given that the output iterator is written by dereference and assignment, I think I would need some kind of proxy reference, that when assigned to applies the function before doing the actual assignment.
That looks ugly, so I'm asking here for a more elegant solution :)
how is what you want to do different from: boost::copy( myrange | transformed(func), outputitr ); Jeff
Jeff Flinn
On 7/31/15 8:40 AM, dariomt wrote:
Edward Diener
writes: On 7/30/2015 7:06 AM, dariomt <at> gmail.com wrote:
Here is my problem: for any given algorithm that writes its output
into
an output iterator, I'd like to apply a transformation *before* writing to the output iterator.
e.g. inside the algorithm the output iterator is dereferenced and incremented to write each result, typically like this:
*output++ = ith_result;
I'd like to wrap the output iterator with a unary function func
such
that the effect is equivalent to:
*output++ = func( ith_result );
I'm looking at the adaptors in Boost.Iterator but I cannot find a suitable one.
Am I missing anything? Is there a better approach?
Can you not use the function_output_iterator to accomplish what you want to do ?
IIUC the function_output_iterator "fakes" an output iterator with a function object that gets called for each element written to the iterator.
I need to wrap *both* an actual output iterator and a function object, and I need to apply the function *before* writing to the actual output iterator.
Given that the output iterator is written by dereference and assignment, I think I would need some kind of proxy reference, that when assigned to applies the function before doing the actual assignment.
That looks ugly, so I'm asking here for a more elegant solution :)
how is what you want to do different from:
boost::copy( myrange | transformed(func), outputitr );
Jeff
What if myrange is the output of an algorithm, but I don't want to copy
it into an intermediate range?
e.g.
template
The only way I could see this different from Jeff proposal is if somehow
yous functor to transform the result is dependant on the traversal of the
sequence or the order of the element.
Other I don't see why you couldn't do my_algo(input_range |
trasnformed(func), out).
Cheers,
Stephane
On Mon, Aug 3, 2015 at 12:03 PM dariomt
Jeff Flinn
writes: On 7/31/15 8:40 AM, dariomt wrote:
Edward Diener
writes: On 7/30/2015 7:06 AM, dariomt <at> gmail.com wrote:
Here is my problem: for any given algorithm that writes its output
into
an output iterator, I'd like to apply a transformation *before* writing to the output iterator.
e.g. inside the algorithm the output iterator is dereferenced and incremented to write each result, typically like this:
*output++ = ith_result;
I'd like to wrap the output iterator with a unary function func
such
that the effect is equivalent to:
*output++ = func( ith_result );
I'm looking at the adaptors in Boost.Iterator but I cannot find a suitable one.
Am I missing anything? Is there a better approach?
Can you not use the function_output_iterator to accomplish what you want to do ?
IIUC the function_output_iterator "fakes" an output iterator with a function object that gets called for each element written to the iterator.
I need to wrap *both* an actual output iterator and a function object, and I need to apply the function *before* writing to the actual output iterator.
Given that the output iterator is written by dereference and assignment, I think I would need some kind of proxy reference, that when assigned to applies the function before doing the actual assignment.
That looks ugly, so I'm asking here for a more elegant solution :)
how is what you want to do different from:
boost::copy( myrange | transformed(func), outputitr );
Jeff
What if myrange is the output of an algorithm, but I don't want to copy it into an intermediate range?
e.g. template
void my_algo(InputIter first, InputIter last, OutputIter out) { // complex loop traversing input sequence ... // some complicated code to compute each result ... // write each result to output iterator *out++ = ith_result; } I know I could do it easily in two steps storing the intermediate results.
intermediate_storage tmp; my_algo(input.begin(), input.end(), back_inserter(tmp)); transform(tmp.begin(), tmp.end(), output);
But how to do it in one go, without requiring storage for intermediate results?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I am not 100% sure, since I only looked at the code today for other reasons, but I think what you are looking for is: boost/interprocess/detail/transform_iterator.hpp -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-Iterator-transform-iterator-equival... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (7)
-
dariomt
-
dariomt@gmail.com
-
Edward Diener
-
Jeff Flinn
-
Malko
-
Nat Goodspeed
-
stephane GULLAUD