]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Moved some more typedefs to StatHist.h
authorFrancesco Chemolli <kinkie@squid-cache.org>
Thu, 15 Dec 2011 23:14:48 +0000 (00:14 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Thu, 15 Dec 2011 23:14:48 +0000 (00:14 +0100)
Improved documentation
Removed squid.h include
Changed StatHist::operator= to almost be an actual assignment operator
Fixed findBin corner-case

src/StatHist.cc
src/StatHist.h
src/typedefs.h

index c857de3b73ff623ce178c16156c3ba0e6592da69..395708c77abb1787a39602e8bf8f91501e903dd5 100644 (file)
@@ -1,7 +1,5 @@
 
 /*
- * $Id$
- *
  * DEBUG: section 62    Generic Histogram
  * AUTHOR: Duane Wessels
  *
  *
  */
 
-/*
- * Important restrictions on val_in and val_out functions:
- *
- *   - val_in:  ascending, defined on [0, oo), val_in(0) == 0;
- *   - val_out: x == val_out(val_in(x)) where val_in(x) is defined
- *
- *  In practice, the requirements are less strict,
- *  but then it gets hard to define them without math notation.
- *  val_in is applied after offseting the value but before scaling
- *  See log and linear based histograms for examples
- */
-
-#include "squid.h"
+#include "config.h"
 #include "StatHist.h"
-#include "Store.h"
 
 /* Local functions */
 static StatHistBinDumper statHistBinDumper;
@@ -72,7 +57,7 @@ StatHist::init(int capacity_, hbase_f * val_in_, hbase_f * val_out_, double min_
     capacity = capacity_;
     val_in = val_in_;
     val_out = val_out_;
-    bins = (int *)xcalloc(capacity, sizeof(int));
+    bins = static_cast<int *>(xcalloc(capacity, sizeof(int)));
     scale = capacity / val_in(max - min);
 
     /* check that functions are valid */
@@ -99,18 +84,22 @@ StatHist::~StatHist()
     }
 }
 
-/* assumes that somebody already called init for Dest */
-/* TODO: remove assumption */
 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);
+    assert(src.bins != NULL); // TODO: remove after initializing bins at construction time
+    if (capacity != src.capacity) {
+        // need to resize.
+        xfree(bins);
+        bins = static_cast<int *>(xcalloc(src.capacity, sizeof(int)));
+        capacity=src.capacity;
+
+    }
+    min=src.min;
+    max=src.max;
+    scale=src.scale;
+    val_in=src.val_in;
+    val_out=src.val_out;
     memcpy(bins,src.bins,capacity*sizeof(*bins));
     return *this;
 }
@@ -137,7 +126,7 @@ StatHist::findBin(double v)
     bin = (int) floor(scale * val_in(v) + 0.5);
 
     if (bin < 0)               /* should not happen */
-        bin = 0;
+        return 0;
 
     if (bin >= capacity)       /* too big */
         bin = capacity - 1;
@@ -154,13 +143,13 @@ StatHist::val(int bin) const
 double
 statHistDeltaMedian(const StatHist & A, const StatHist & B)
 {
-    return statHistDeltaPctile(A,B, 0.5);
+    return statHistDeltaPctile(A, B, 0.5);
 }
 
 double
 statHistDeltaPctile(const StatHist & A, const StatHist & B, double pctile)
 {
-    return A.deltaPctile(B,pctile);
+    return A.deltaPctile(B, pctile);
 }
 
 double
@@ -178,7 +167,7 @@ StatHist::deltaPctile(const StatHist & B, double pctile) const
 
     assert(capacity == B.capacity);
 
-    int *D = (int *)xcalloc(capacity, sizeof(int));
+    int *D = static_cast<int *>(xcalloc(capacity, sizeof(int)));
 
     for (i = 0; i < capacity; ++i) {
         D[i] = B.bins[i] - bins[i];
@@ -279,7 +268,7 @@ Math::Null(double x)
 void
 StatHist::enumInit(int last_enum)
 {
-    init(last_enum + 3, Math::Null, Math::Null, (double) -1, (double) (last_enum + 1 + 1));
+    init(last_enum + 3, Math::Null, Math::Null, -1.0, (2.0 + last_enum));
 }
 
 void
@@ -296,4 +285,3 @@ statHistIntDumper(StoreEntry * sentry, int idx, double val, double size, int cou
     if (count)
         storeAppendPrintf(sentry, "%9d\t%9d\n", (int) val, count);
 }
-
index 0b6ce1d05a4e51376294c895e436882302ad6c25..4660958c7bf12edbd729f124e667669e2fa51f20 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * AUTHOR: Francesco Chemolli
+ *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
  * ----------------------------------------------------------
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
- *
- *  AUTHOR: Francesco Chemolli
  */
 
 #ifndef STATHIST_H_
 #define STATHIST_H_
 
-#include "typedefs.h"
+/* for StoreEntry */
+#include "Store.h"
+
+
+/// function signature for in/out StatHist adaptation
+typedef double hbase_f(double);
+
+/// function signature for StatHist dumping functions
+typedef void StatHistBinDumper(StoreEntry *, int idx, double val, double size, int count);
 
 /** Generic histogram class
  *
- * see important comments on hbase_f restrictions in StatHist.c
+ * see important comments on hbase_f restrictions in StatHist.cc
  */
 class StatHist {
 public:
-    /** Default constructor
-     *
+    /**
      * \note the default constructor doesn't fully initialize.
      *       you have to call one of the *init functions to specialize the
      *       histogram
+     * \todo merge functionality from the *init functions to the constructor and
+     *       drop these
      * \todo specialize the class in a small hierarchy so that all
      *       relevant initializations are done at build-time
      */
@@ -82,7 +91,17 @@ public:
      */
     void enumInit(int last_enum);
 protected:
-    /// low-level initialize function. called by *Init high-level functions
+    /** low-level initialize function. called by *Init high-level functions
+     * \note Important restrictions on val_in and val_out functions:
+     *
+     *   - val_in:  ascending, defined on [0, oo), val_in(0) == 0;
+     *   - val_out: x == val_out(val_in(x)) where val_in(x) is defined
+     *
+     *  In practice, the requirements are less strict,
+     *  but then it gets hard to define them without math notation.
+     *  val_in is applied after offseting the value but before scaling
+     *  See log and linear based histograms for examples
+     */
     void init(int capacity, hbase_f * val_in, hbase_f * val_out, double min, double max);
     /// find what entry in the histogram corresponds to v, by applying
     /// the preset input transformation function
@@ -100,7 +119,6 @@ protected:
     hbase_f *val_out;       /* e.g., exp() for log based histogram */
 };
 
-/* StatHist */
 double statHistDeltaMedian(const StatHist & A, const StatHist & B);
 double statHistDeltaPctile(const StatHist & A, const StatHist & B, double pctile);
 StatHistBinDumper statHistEnumDumper;
index 6b539ffcf20a16be6badab684023b39027d4e907..438ffc85f9725253f2a33e92325c1c2abf81583d 100644 (file)
@@ -142,9 +142,6 @@ typedef void HLPSONEQ(void *);
 typedef void HLPCMDOPTS(int *argc, char **argv);
 typedef void IDNSCB(void *, rfc1035_rr *, int, const char *);
 
-typedef double hbase_f(double);
-typedef void StatHistBinDumper(StoreEntry *, int idx, double val, double size, int count);
-
 /* MD5 cache keys */
 typedef unsigned char cache_key;