library accumulator, example sum_accumulator
Hi all, i ' ve written a piece of code to get the class sum_accumulator working which is described in the user's guide http://boost-sandbox.sourceforge.net/libs/accumulators/doc/html/accumulators.... The function result must be provided with a struct dont_care. And the constructor of dont_care must be provided with a value to get an instanciation. In my opinion a default constructor taking no arguments for the struct dont_care must exists so the user can write code like dont_care(). To get the example working i must write code like sumacc.result(*dont_care(0.0)*) to get the result from my accumulator. And this is a little bit unnecessary. with regards, Kim Tang the code below is tested with visual studio 2008. #include <iostream> #include <boost/accumulators/framework/accumulator_base.hpp> #include <boost/accumulators/framework/parameters/sample.hpp> using namespace boost::accumulators; namespace boost { // Putting your accumulators in the namespace accumulators { // impl namespace has some namespace impl { // advantages. See below. template<typename Sample> struct sum_accumulator // All accumulators should inherit from : accumulator_base // accumulator_base. { typedef Sample result_type; // The type returned by result() below. template<typename Args> // The constructor takes an argument pack. sum_accumulator(Args const & args) : sum(args[sample | Sample()]) // Maybe there is an initial value in the { // argument pack. ('sample' is defined in } // sample.hpp, included above.) template<typename Args> // The accumulate function is the function void operator ()(Args const & args) // call operator, and it also accepts an { // argument pack. this->sum += args[sample]; } result_type result(dont_care) const // The result function will also be passed { // an argument pack, but we don't use it here, return this->sum; // so we use "dont_care" as the argument type. } private: Sample sum; }; }}} int main() { try { boost::accumulators::impl::sum_accumulator<double> sumacc(sample=0.0); sumacc(sample=1.2); std::cout<<sumacc.result(*dont_care(0.0)*)<<"\n"; return EXIT_SUCCESS; } catch(std::exception& e) { std::cout<<"Errors with message: "<<e.what()<<"\n"; return EXIT_FAILURE; } catch(...) { std::cout<<"Unknown Errors \n"; return EXIT_FAILURE; } }
Kim Kuen Tang wrote:
Hi all,
i ' ve written a piece of code to get the class sum_accumulator working which is described in the user's guide http://boost-sandbox.sourceforge.net/libs/accumulators/doc/html/accumulators....
The function result must be provided with a struct dont_care. And the constructor of dont_care must be provided with a value to get an instanciation. In my opinion a default constructor taking no arguments for the struct dont_care must exists so the user can write code like dont_care().
To get the example working i must write code like sumacc.result(*dont_care(0.0)*) to get the result from my accumulator. And this is a little bit unnecessary. <snip>
The interface you're describing is an internal interface, consumed by the accumulator_set<>. End-user code should never be directly calling the result() member function of an accumulator implementation. Accumulators are not supposed to be used stand-alone ... only as part of an accumulator_set<>. HTH, -- Eric Niebler BoostPro Computing http://www.boostpro.com
Hi Eric, thank you for your reply. After studying the interface i see that this is an internal interface. And this interface should be used internally. The reason why i ask is that i want to implement an general static class in QuantLib, which should accumulate an vector, valarray or scoped_array. I noticed that the class accumulator_set<> can not take a vector as sample. Is this true? And do you plan to extend it to accumulate vector? with regards, Kim Tang Eric Niebler schrieb:
Kim Kuen Tang wrote:
Hi all,
i ' ve written a piece of code to get the class sum_accumulator working which is described in the user's guide http://boost-sandbox.sourceforge.net/libs/accumulators/doc/html/accumulators....
The function result must be provided with a struct dont_care. And the constructor of dont_care must be provided with a value to get an instanciation. In my opinion a default constructor taking no arguments for the struct dont_care must exists so the user can write code like dont_care().
To get the example working i must write code like sumacc.result(*dont_care(0.0)*) to get the result from my accumulator. And this is a little bit unnecessary. <snip>
The interface you're describing is an internal interface, consumed by the accumulator_set<>. End-user code should never be directly calling the result() member function of an accumulator implementation. Accumulators are not supposed to be used stand-alone ... only as part of an accumulator_set<>.
HTH,
Kim Kuen Tang wrote:
I noticed that the class accumulator_set<> can not take a vector as sample. Is this true?
It's not true. But you must #include <boost/accumulators/numeric/functional/vector.hpp> (before any other accumulators header), or else compile with BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT defined. BTW, I'm currently on vacation and my not reply to any further messages immediately. -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (2)
-
Eric Niebler
-
Kim Kuen Tang