xref: /386bsd/usr/src/lib/libg++/libg++/SmplHist.cc (revision a2142627)
1 // This may look like C code, but it is really -*- C++ -*-
2 /*
3 Copyright (C) 1988 Free Software Foundation
4     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
5 
6 This file is part of GNU CC.
7 
8 GNU CC is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY.  No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing.  Refer to the GNU CC General Public
13 License for full details.
14 
15 Everyone is granted permission to copy, modify and redistribute
16 GNU CC, but only under the conditions described in the
17 GNU CC General Public License.   A copy of this license is
18 supposed to have been given to you along with GNU CC so you
19 can know your rights and responsibilities.  It should be in a
20 file named COPYING.  Among other things, the copyright notice
21 and this notice must be preserved on all copies.
22 */
23 #ifdef __GNUG__
24 #pragma implementation
25 #endif
26 #include <stream.h>
27 #include <SmplHist.h>
28 #include <math.h>
29 
30 
31 const int SampleHistogramMinimum = -2;
32 const int SampleHistogramMaximum = -1;
33 
SampleHistogram(double low,double high,double width)34 SampleHistogram::SampleHistogram(double low, double high, double width)
35 {
36     if (high < low) {
37 	double t = high;
38 	high = low;
39 	low = t;
40     }
41 
42     if (width == -1) {
43 	width = (high - low) / 10;
44     }
45 
46     howManyBuckets = int((high - low) / width) + 2;
47     bucketCount = new int[howManyBuckets];
48     bucketLimit = new double[howManyBuckets];
49     double lim = low;
50     for (int i = 0; i < howManyBuckets; i++) {
51 	bucketCount[i] = 0;
52 	bucketLimit[i] = lim;
53 	lim += width;
54     }
55     bucketLimit[howManyBuckets-1] = HUGE;	/* from math.h */
56 }
57 
~SampleHistogram()58 SampleHistogram::~SampleHistogram()
59 {
60     if (howManyBuckets > 0) {
61 	delete bucketCount;
62 	delete bucketLimit;
63     }
64 }
65 
66 void
operator +=(double value)67 SampleHistogram::operator+=(double value)
68 {
69     int i;
70     for (i = 0; i < howManyBuckets; i++) {
71 	if (value < bucketLimit[i]) break;
72     }
73     bucketCount[i]++;
74     this->SampleStatistic::operator+=(value);
75 }
76 
77 int
similarSamples(double d)78 SampleHistogram::similarSamples(double d)
79 {
80     int i;
81     for (i = 0; i < howManyBuckets; i++) {
82 	if (d < bucketLimit[i]) return(bucketCount[i]);
83     }
84     return(0);
85 }
86 
87 void
printBuckets(ostream & s)88 SampleHistogram::printBuckets(ostream& s)
89 {
90     for(int i = 0; i < howManyBuckets; i++) {
91 	if (bucketLimit[i] >= HUGE) {
92 	    s << "< max : " << bucketCount[i] << "\n";
93 	} else {
94 	    s << "< " << bucketLimit[i] << " : " << bucketCount[i] << "\n";
95 	}
96     }
97 }
98 
99 void
reset()100 SampleHistogram::reset()
101 {
102     this->SampleStatistic::reset();
103     if (howManyBuckets > 0) {
104 	for (register int i = 0; i < howManyBuckets; i++) {
105 	    bucketCount[i] = 0;
106 	}
107     }
108 }
109 
110