Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Histogram.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Fri Apr 22 22:32:29 EDT 2005 by ken@xorian.net  
00020 //      modified on Sat Feb 12 17:33:34 PST 2000 by mann  
00021 //      modified on Mon Mar 30 13:12:58 PST 1998 by heydon
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;         // compute bucket index
00032     int bx = b - this->firstBucket; // adjust for current array
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 (/*SKIP*/; 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     // compute maximum bucket value
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     // compute value for each '*'
00073     float starVal = ((float)maxVal) / ((float)(width - 12));
00074 
00075     // print histogram bars
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     // print total
00089     os << "TOTAL = " << this->numVals << endl;
00090 }

Generated on Mon May 8 00:48:30 2006 for Vesta by  doxygen 1.4.2