Dear all,
The accumulators library is a very nice way to get a set of statistics for a stream of numbers, doubles, say.
Now let’s suppose I’m receiving a stream of boost::optional<double>s – e.g. let’s suppose I’m receiving simulation results and some simulations fail.
It would be neat if I could somehow write and “adaptor accumulator” that took this stream of optionals and then, if they are not empty, pass them to an underlying accumulator.
In rough terms:
template
On Fri, Jan 25, 2013 at 11:05 AM, Pete Bartlett
It would be neat if I could somehow write and “adaptor accumulator” that took this stream of optionals and then, if they are not empty, pass them to an underlying accumulator.
My impulse is to suggest implementing an iterator (or range) adapter instead, to skip the items you don't have and dereference the ones you do.
(we might also define another accumulator that just counts the number of “bad” samples.
I admit I don't know how to do that in a single pass using iterator/range adapters instead of what you propose.
On 01/25/2013 08:05 AM, Pete Bartlett wrote:
Dear all,
The accumulators library is a very nice way to get a set of statistics for a stream of numbers, doubles, say. Now let’s suppose I’m receiving a stream of boost::optional<double>s – e.g. let’s suppose I’m receiving simulation results and some simulations fail. It would be neat if I could somehow write and “adaptor accumulator” that took this stream of optionals and then, if they are not empty, pass them to an underlying accumulator.
In rough terms:
template
struct accumulator_adaptor { template<typename Args> void operator()( Args const& args) { Sample const& s = args[sample]; if( s ) { m_underlying_accumulator( *s ) ; //doesn’t compile UnderlyingAccumulator expects an Args, not a Sample } } private: UnderlyingAccumulator m_underlying_accumulator; };
(we might also define another accumulator that just counts the number of “bad” samples. Then by adding an appropriate feature adaptor, one could write something like
accumulator_set< boost::optional<double> , tag::accumulator_adaptor< mean > , tag::bad_samples_count >
For the data 1.0,2.0,boost::optional<double>(),4.0
This would return a mean of 7/3 and a bad_samples_count of 1.
Is it possible for me to implement such an adaptor that would work for an arbitrary underlying accumulator?
Sure. I don't anticipate problems. Have a look at the docs for extending the accumulators framework: http://www.boost.org/doc/libs/1_52_0/doc/html/accumulators/user_s_guide.html... Then give it a shot and report back. If you run into problems, look at the implementation of some of the simpler accumulators, like sum, count, and mean. That should give you some ideas. HTH, Eric
participants (3)
-
Eric Niebler
-
Nat Linden
-
Pete Bartlett