00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #include <Basics.H>
00024 #include "Histogram.H"
00025 
00026 using std::ostream;
00027 using std::endl;
00028 
00029 void Histogram::AddVal(int t) throw ()
00030 {
00031     int b = t / this->gran;         
00032     int bx = b - this->firstBucket; 
00033     if (this->numBuckets == 0) {
00034         this->numBuckets = 1;
00035         this->bucket = NEW_PTRFREE_ARRAY(int, 1);
00036         this->firstBucket = b;
00037         bx = 0;
00038     } else if (bx < 0) {
00039         int growBy = -bx;
00040         int *newBucket = NEW_PTRFREE_ARRAY(int, this->numBuckets + growBy);
00041         int i;
00042         for (i = 0; i < growBy; i++) newBucket[i] = 0;
00043         for (i = 0; i < this->numBuckets; i++)
00044             newBucket[i + growBy] = this->bucket[i];
00045         this->numBuckets += growBy;
00046         this->bucket = newBucket;
00047         this->firstBucket = b;
00048         bx = 0;
00049     } else if (numBuckets <= bx) {
00050         int growBy = 1 + bx - this->numBuckets;
00051         int *newBucket = NEW_PTRFREE_ARRAY(int, this->numBuckets + growBy);
00052         int i;
00053         for (i = 0; i < this->numBuckets; i++)
00054             newBucket[i] = this->bucket[i];
00055         for (; i < this->numBuckets + growBy; i++) newBucket[i] = 0;
00056         this->numBuckets += growBy;
00057         this->bucket = newBucket;
00058     }
00059     this->bucket[bx]++;
00060     this->numVals++;
00061 }
00062 
00063 void Histogram::Print(ostream &os, int width) const throw ()
00064 {
00065     
00066     int maxVal = 0;
00067     int i;
00068     for (i = 0; i < this->numBuckets; i++) {
00069         maxVal = max(maxVal, this->bucket[i]);
00070     }
00071 
00072     
00073     float starVal = ((float)maxVal) / ((float)(width - 12));
00074 
00075     
00076     int bVal = this->firstBucket * this->gran;
00077     for (i = 0; i < this->numBuckets; i++) {
00078         char buff[8];
00079         int printLen = sprintf(buff, "%7d", bVal);
00080         assert(printLen == 7);
00081         bVal += this->gran;
00082         float sz = (float)(this->bucket[i]);
00083         for (float j = 0.0; j < sz; j += starVal) os << '*';
00084         if (sz > 0.0) os << ' ';
00085         os << this->bucket[i] << endl;
00086     }
00087 
00088     
00089     os << "TOTAL = " << this->numVals << endl;
00090 }