From cfe912a5127b51273d7e3e78c15095ac832f20bd Mon Sep 17 00:00:00 2001 From: Nilay Vaish Date: Fri, 10 Jan 2014 16:19:40 -0600 Subject: [PATCH] stats: add function for adding two histograms This patch adds a function to the HistStor class for adding two histograms. This functionality is required for Ruby. It also adds support for printing histograms in a single line. --- src/base/statistics.cc | 21 +++++++++++++++++++++ src/base/statistics.hh | 11 +++++++++-- src/base/stats/text.cc | 36 +++++++++++++++++++++++++++++------- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/base/statistics.cc b/src/base/statistics.cc index 0b44a4ec7..2bd34d3db 100644 --- a/src/base/statistics.cc +++ b/src/base/statistics.cc @@ -352,6 +352,27 @@ HistStor::grow_up() bucket_size *= 2; } +void +HistStor::add(HistStor *hs) +{ + int b_size = hs->size(); + assert(size() == b_size); + assert(min_bucket == hs->min_bucket); + + sum += hs->sum; + logs += hs->logs; + squares += hs->squares; + samples += hs->samples; + + while(bucket_size > hs->bucket_size) + hs->grow_up(); + while(bucket_size < hs->bucket_size) + grow_up(); + + for (uint32_t i = 0; i < b_size; i++) + cvec[i] += hs->cvec[i]; +} + Formula::Formula() { } diff --git a/src/base/statistics.hh b/src/base/statistics.hh index cc1b59e7f..048c1be86 100644 --- a/src/base/statistics.hh +++ b/src/base/statistics.hh @@ -228,12 +228,12 @@ class DataWrap : public InfoAccess /** * Copy constructor, copies are not allowed. */ - DataWrap(const DataWrap &stat); + DataWrap(const DataWrap &stat) {} /** * Can't copy stats. */ - void operator=(const DataWrap &); + void operator=(const DataWrap &) {} public: DataWrap() @@ -1502,6 +1502,7 @@ class HistStor void grow_up(); void grow_out(); void grow_convert(); + void add(HistStor *); /** * Add a value to the distribution for the given number of times. @@ -1840,6 +1841,12 @@ class DistBase : public DataWrap { data()->reset(this->info()); } + + /** + * Add the argument distribution to the this distibution. + */ + void add(DistBase &d) { data()->add(d.data()); } + }; template diff --git a/src/base/stats/text.cc b/src/base/stats/text.cc index 25b8cb2e7..7d25b94e5 100644 --- a/src/base/stats/text.cc +++ b/src/base/stats/text.cc @@ -210,7 +210,7 @@ ScalarPrint::update(Result val, Result total) void ScalarPrint::operator()(ostream &stream, bool oneLine) const { - if ((flags.isSet(nozero) && value == 0.0) || + if ((flags.isSet(nozero) && (!oneLine) && value == 0.0) || (flags.isSet(nonan) && std::isnan(value))) return; @@ -312,7 +312,6 @@ VectorPrint::operator()(std::ostream &stream) const if (!desc.empty()) ccprintf(stream, " # %s", desc); } - stream << endl; } } @@ -325,10 +324,6 @@ VectorPrint::operator()(std::ostream &stream) const print.value = total; print(stream); } - - if (flags.isSet(oneline) && ((!flags.isSet(nozero)) || (total != 0))) { - stream << endl; - } } struct DistPrint @@ -380,6 +375,7 @@ DistPrint::init(const Text *text, const Info &info) void DistPrint::operator()(ostream &stream) const { + if (flags.isSet(nozero) && data.samples == 0) return; string base = name + separatorString; ScalarPrint print; @@ -390,6 +386,20 @@ DistPrint::operator()(ostream &stream) const print.pdf = NAN; print.cdf = NAN; + if (flags.isSet(oneline)) { + print.name = base + "bucket_size"; + print.value = data.bucket_size; + print(stream); + + print.name = base + "min_bucket"; + print.value = data.min; + print(stream); + + print.name = base + "max_bucket"; + print.value = data.max; + print(stream); + } + print.name = base + "samples"; print.value = data.samples; print(stream); @@ -436,6 +446,10 @@ DistPrint::operator()(ostream &stream) const print(stream); } + if (flags.isSet(oneline)) { + ccprintf(stream, "%-40s", name); + } + for (off_type i = 0; i < size; ++i) { stringstream namestr; namestr << base; @@ -448,7 +462,15 @@ DistPrint::operator()(ostream &stream) const print.name = namestr.str(); print.update(data.cvec[i], total); - print(stream); + print(stream, flags.isSet(oneline)); + } + + if (flags.isSet(oneline)) { + if (descriptions) { + if (!desc.empty()) + ccprintf(stream, " # %s", desc); + } + stream << endl; } if (data.type == Dist && data.overflow != NAN) { -- 2.30.2