66// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
77
88#include < iostream>
9+ #include < fstream> // needed for de/serialization example (4)
910#include < algorithm>
1011#include < boost/ref.hpp>
1112#include < boost/bind.hpp>
1213#include < boost/array.hpp>
1314#include < boost/foreach.hpp>
1415#include < boost/accumulators/accumulators.hpp>
1516#include < boost/accumulators/statistics.hpp>
17+ // needed for de/serialization example (4)
18+ #include < boost/archive/text_oarchive.hpp>
19+ #include < boost/archive/text_iarchive.hpp>
1620
1721using namespace boost ;
1822using namespace boost ::accumulators;
@@ -159,6 +163,85 @@ void example3()
159163 }
160164}
161165
166+ // /////////////////////////////////////////////////////////////////////////////
167+ // example4
168+ //
169+ // Show how accumulators could be persisted into a file, and then continued
170+ // from where they were left of
171+ //
172+ void example4 ()
173+ {
174+ accumulator_set<
175+ double
176+ , stats<tag::min, tag::mean (immediate), tag::sum, tag::moment<2 >, tag::p_square_quantile, tag::kurtosis >
177+ > acc (quantile_probability = 0.9 );
178+
179+ {
180+ // accumulate values from array
181+ boost::array<double , 10 > data = {-10 ., -8 ., -7 ., -6 ., -5 ., -4 ., -3 ., -2 ., -1 ., 0 .};
182+
183+ acc = std::for_each (data.begin (), data.end (), acc);
184+ }
185+
186+ std::cout << " min = " << (min)(acc) << std::endl;
187+ std::cout << " mean = " << mean (acc) << std::endl;
188+ std::cout << " count = " << count (acc) << std::endl;
189+ std::cout << " sum = " << sum (acc) << std::endl;
190+ std::cout << " moment<2> = " << accumulators::moment<2 >(acc) << std::endl;
191+ std::cout << " p_square_quantile = " << accumulators::p_square_quantile (acc) << std::endl;
192+ std::cout << " kurtosis = " << accumulators::kurtosis (acc) << std::endl;
193+
194+ // save accumulator list into a file called "saved-stats"
195+ const unsigned ACC_VER = 0 ;
196+ const char * file_name = " saved-stats" ;
197+ {
198+ std::ofstream ofs (file_name);
199+ boost::archive::text_oarchive oa (ofs);
200+ acc.serialize (oa, ACC_VER);
201+ }
202+
203+ // create a new accumulator set and initialize from data saved into the file
204+ accumulator_set<
205+ double
206+ , stats<tag::min, tag::mean (immediate), tag::sum, tag::moment<2 >, tag::p_square_quantile, tag::kurtosis >
207+ > restored_acc (quantile_probability = 0.9 );
208+
209+ {
210+ std::ifstream ifs (file_name);
211+ boost::archive::text_iarchive ia (ifs);
212+ restored_acc.serialize (ia, ACC_VER);
213+ }
214+
215+ // continue accumulating into both sets
216+ {
217+ // accumulate values from array
218+ boost::array<double , 10 > data = {10 ., 8 ., 7 ., 6 ., 5 ., 4 ., 3 ., 2 ., 1 ., 0 .};
219+
220+ acc = std::for_each (data.begin (), data.end (), acc);
221+ restored_acc = std::for_each (data.begin (), data.end (), restored_acc);
222+ }
223+
224+ // validate that both return th same values
225+ std::cout << std::endl << " Values in original set:" << std::endl;
226+ std::cout << " min" " (acc) = " << (min)(acc) << std::endl;
227+ std::cout << " mean(acc) = " << mean (acc) << std::endl;
228+ std::cout << " count(acc) = " << count (acc) << std::endl;
229+ std::cout << " sum(acc) = " << sum (acc) << std::endl;
230+ std::cout << " moment<2>(acc) = " << accumulators::moment<2 >(acc) << std::endl;
231+ std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile (acc) << std::endl;
232+ std::cout << " kurtosis(acc) = " << accumulators::kurtosis (acc) << std::endl;
233+
234+ std::cout << std::endl << " Values in restored set:" << std::endl;
235+ std::cout << " min" " (acc) = " << (min)(restored_acc) << std::endl;
236+ std::cout << " mean(acc) = " << mean (restored_acc) << std::endl;
237+ std::cout << " count(acc) = " << count (restored_acc) << std::endl;
238+ std::cout << " sum(acc) = " << sum (restored_acc) << std::endl;
239+ std::cout << " moment<2>(acc) = " << accumulators::moment<2 >(restored_acc) << std::endl;
240+ std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile (restored_acc) << std::endl;
241+ std::cout << " kurtosis(acc) = " << accumulators::kurtosis (restored_acc) << std::endl;
242+
243+ }
244+
162245// /////////////////////////////////////////////////////////////////////////////
163246// main
164247int main ()
@@ -172,5 +255,8 @@ int main()
172255 std::cout << " \n Example 3:\n " ;
173256 example3 ();
174257
258+ std::cout << " \n Example 4:\n " ;
259+ example4 ();
260+
175261 return 0 ;
176262}
0 commit comments