C++ Iterator Pipelining Designs

Posted by Kirakun on Stack Overflow See other posts from Stack Overflow or by Kirakun
Published on 2011-02-25T23:06:44Z Indexed on 2011/02/25 23:25 UTC
Read the original article Hit count: 189

Filed under:
|
|

Suppose we want to apply a series of transformations, int f1(int), int f2(int), int f3(int), to a list of objects. A naive way would be

SourceContainer source;

TempContainer1 temp1;
transform(source.begin(), source.end(), back_inserter(temp1), f1);
TempContainer2 temp2;
transform(temp1.begin(), temp1.end(), back_inserter(temp2), f2);

TargetContainer target;
transform(temp2.begin(), temp2.end(), back_inserter(target), f3);

This first solution is not optimal because of the extra space requirement with temp1 and temp2. So, let's get smarter with this:

int f123(int n) { return f3(f2(f1(n))); }
...
SourceContainer source;
TargetContainer target;
transform(source.begin(), source.end(), back_inserter(target), f123);

This second solution is much better because not only the code is simpler but more importantly there is less space requirement without the intermediate calculations.

However, the composition f123 must be determined at compile time and thus is fixed at run time.

How would I try to do this efficiently if the composition is to be determined at run time? For example, if this code was in a RPC service and the actual composition--which can be any permutation of f1, f2, and f3--is based on arguments from the RPC call.

© Stack Overflow or respective owner

Related posts about c++

Related posts about design-patterns