Boost logo

Boost Users :

Subject: Re: [Boost-users] [accumulators] accumulator "adapters"?
From: Eric Niebler (eniebler_at_[hidden])
Date: 2013-01-25 12:42:14


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<typename UnderlyingAccumulator , typename Sample > 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#accumulators.user_s_guide.the_accumulators_framework.extending_the_accumulators_framework

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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net