/* * Copyright (c) 1998 by the University of Washington. All rights reserved. * * This is a proprietary and confidential document. Under no circumstances * may this document be used, modified, copied, distributed, or sold * without the express written permission of the copyright holder. */ #include #include const MAXKEYS = 150; //+------------------------------------- // Class: Counters // Description: keeps a set of counters that you can increment and // print out. This makes keeping stats fairly easy. // class Counters { public: Counters() : nkeys(0) {}; // increment the count of a handle void inc(int index) { assert(index < nkeys); counts[index]++; }; // register a key, returns a handle int RegisterKey(const char * const key) { keys[nkeys] = key; counts[nkeys] = 0; return nkeys++; } // dump the statistics virtual void DumpCounts(FILE *f) { for (int i = 0; i < nkeys; i++) { fprintf(f,"%s -> %I64d\n",getkey(i), getcount(i)); } } int getnkeys() { return nkeys; } // how many keys there are __int64 getcount(int index) { return counts[index]; } // get the count of // a handle const char * getkey(int index) { return keys[index]; } // get the name of an // handle private: int nkeys; const char *keys[MAXKEYS]; __int64 counts[MAXKEYS]; }; // like counters except keeps track of means and stddev class StatCounters : public Counters { public: StatCounters():Counters() {}; double getmean(int i) { return sum[i]/getcount(i); } double getstddev(int i) { return sqrt( (getcount(i)*sumsquares[i] - sum[i]*sum[i])/(getcount(i)*(getcount(i)-1)) ); } // adds num to the i-th counter void add(int i, int num) { sum[i] += num; sumsquares[i] += num*num; } virtual void DumpCounts(FILE *f) { for (int i = 0; i < getnkeys(); i++) { fprintf(f,"%s -> %I64d -> %0.2f -> %0.2f\n", getkey(i), getcount(i), getmean(i), getstddev(i) ); } } protected: __int64 sum[MAXKEYS]; __int64 sumsquares[MAXKEYS]; };