From: Francesco Chemolli Date: Tue, 6 Dec 2011 12:21:20 +0000 (+0100) Subject: Implemented StatHist default constructor X-Git-Tag: BumpSslServerFirst.take05~12^2~120^2~24 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=406c0a08bf4a45299a75fa17937044bff39db368;p=thirdparty%2Fsquid.git Implemented StatHist default constructor Refactored StatHist::init --- diff --git a/src/StatHist.cc b/src/StatHist.cc index 62d0027cec..80acea0f30 100644 --- a/src/StatHist.cc +++ b/src/StatHist.cc @@ -50,7 +50,6 @@ #include "Store.h" /* Local functions */ -static void statHistInit(StatHist * H, int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max); static int statHistBin(const StatHist * H, double v); static StatHistBinDumper statHistBinDumper; @@ -62,34 +61,28 @@ hbase_f Null; }; /* low level init, higher level functions has less params */ -static void -statHistInit(StatHist * H, int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max) +void +StatHist::init(int capacity_, hbase_f * val_in_, hbase_f * val_out_, double min_, double max_) { - assert(H); - assert(capacity > 0); - assert(val_in && val_out); + assert(capacity_ > 0); + assert(val_in_ && val_out_); /* check before we divide to get scale */ - assert(val_in(max - min) > 0); - H->bins = (int *)xcalloc(capacity, sizeof(int)); - H->min = min; - H->max = max; - H->capacity = capacity; - H->scale = capacity / val_in(max - min); - H->val_in = val_in; - H->val_out = val_out; - - /* HPUX users: If you get one of the assertions below, please send - * [at least] the values of all variables involved in the assertions - * when reporting a bug! - */ + assert(val_in_(max_ - min_) > 0); + min = min_; + max = max_; + capacity = capacity_; + val_in = val_in_; + val_out = val_out_; + bins = (int *)xcalloc(capacity, sizeof(int)); + scale = capacity / val_in(max - min); /* check that functions are valid */ /* a min value should go into bin[0] */ - assert(statHistBin(H, min) == 0); + assert(statHistBin(this, min) == 0); /* a max value should go into the last bin */ - assert(statHistBin(H, max) == H->capacity - 1); + assert(statHistBin(this, max) == capacity - 1); /* it is hard to test val_out, here is a crude test */ - assert(((int) floor(0.99 + H->val(0) - min)) == 0); + assert(((int) floor(0.99 + val(0) - min)) == 0); } void @@ -126,6 +119,20 @@ statHistCopy(StatHist * Dest, const StatHist * Orig) memcpy(Dest->bins, Orig->bins, Dest->capacity * sizeof(*Dest->bins)); } +StatHist& +StatHist::operator =(const StatHist & src) +{ + assert(src.bins != NULL); + assert(capacity==src.capacity); + assert(min==src.min); + assert(max==src.max); + assert(fabs(scale - src.scale) < 0.0000001); + assert(val_in==src.val_in); + assert(val_out==src.val_out); + memcpy(bins,src.bins,capacity*sizeof(*bins)); + return *this; +} + /* * same as statHistCopy but will do nothing if capacities do not match; the * latter happens, for example, when #peers changes during reconfiguration; @@ -290,7 +297,7 @@ Math::Exp(double x) void statHistLogInit(StatHist * H, int capacity, double min, double max) { - statHistInit(H, capacity, Math::Log, Math::Exp, min, max); + H->init(capacity, Math::Log, Math::Exp, min, max); } /* linear histogram for enums */ @@ -304,7 +311,7 @@ Math::Null(double x) void statHistEnumInit(StatHist * H, int last_enum) { - statHistInit(H, last_enum + 3, Math::Null, Math::Null, (double) -1, (double) (last_enum + 1 + 1)); + H->init(last_enum + 3, Math::Null, Math::Null, (double) -1, (double) (last_enum + 1 + 1)); } void @@ -318,7 +325,7 @@ statHistEnumDumper(StoreEntry * sentry, int idx, double val, double size, int co void statHistIntInit(StatHist * H, int n) { - statHistInit(H, n, Math::Null, Math::Null, (double) 0, (double) n - 1); + H->init(n, Math::Null, Math::Null, (double) 0, (double) n - 1); } void diff --git a/src/StatHist.h b/src/StatHist.h index ca06db2022..e853d42556 100644 --- a/src/StatHist.h +++ b/src/StatHist.h @@ -52,13 +52,15 @@ public: double val(int bin) const; //todo: make private void count(double val) const; StatHist &operator=(const StatHist &); - StatHist(); + StatHist() : bins(NULL), capacity(0), min(0), max(0), scale(1.0), + val_in(NULL), val_out(NULL) {}; StatHist(const StatHist&); double deltaMedian(const StatHist &B, double pctile) const; void dump(StoreEntry *sentry, StatHistBinDumper * bd); void logInit(int capacity, double min, double max); void enumInit(int last_enum); void intInit(int n); + void init(int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max); private: };