00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef __GNUG__
00019 #pragma implementation
00020 #endif
00021 #include <iostream>
00022 #include <fstream>
00023 #include "SGSmplhist.hxx"
00024 #include <math.h>
00025
00026 #ifndef HUGE_VAL
00027 #ifdef HUGE
00028 #define HUGE_VAL HUGE
00029 #else
00030 #include <float.h>
00031 #define HUGE_VAL DBL_MAX
00032 #endif
00033 #endif
00034
00035 const int SampleHistogramMinimum = -2;
00036 const int SampleHistogramMaximum = -1;
00037
00038 SampleHistogram::SampleHistogram (double low, double high, double width)
00039 {
00040 if (high < low)
00041 {
00042 double t = high;
00043 high = low;
00044 low = t;
00045 }
00046
00047 if (width == -1)
00048 {
00049 width = (high - low) / 10;
00050 }
00051
00052 howManyBuckets = int ((high - low) / width) + 2;
00053 bucketCount = new int[howManyBuckets];
00054 bucketLimit = new double[howManyBuckets];
00055 double lim = low;
00056 for (int i = 0; i < howManyBuckets; i++)
00057 {
00058 bucketCount[i] = 0;
00059 bucketLimit[i] = lim;
00060 lim += width;
00061 }
00062 bucketLimit[howManyBuckets - 1] = HUGE_VAL;
00063 }
00064
00065 SampleHistogram::~SampleHistogram ()
00066 {
00067 if (howManyBuckets > 0)
00068 {
00069 delete[]bucketCount;
00070 delete[]bucketLimit;
00071 }
00072 }
00073
00074 void SampleHistogram::operator += (double value)
00075 {
00076 int i;
00077 for (i = 0; i < howManyBuckets; i++)
00078 {
00079 if (value < bucketLimit[i])
00080 break;
00081 }
00082 bucketCount[i]++;
00083 this->SampleStatistic::operator += (value);
00084 }
00085
00086 int SampleHistogram::similarSamples (double d)
00087 {
00088 int i;
00089 for (i = 0; i < howManyBuckets; i++)
00090 {
00091 if (d < bucketLimit[i])
00092 return (bucketCount[i]);
00093 }
00094 return (0);
00095 }
00096
00097 void SampleHistogram::printBuckets (ostream & s)
00098 {
00099 for (int i = 0; i < howManyBuckets; i++)
00100 {
00101 if (bucketLimit[i] >= HUGE_VAL)
00102 {
00103 s << "< max : " << bucketCount[i] << "\n";
00104 }
00105 else
00106 {
00107 s << "< " << bucketLimit[i] << " : " << bucketCount[i] << "\n";
00108 }
00109 }
00110 }
00111
00112 void SampleHistogram::reset ()
00113 {
00114 this->SampleStatistic::reset ();
00115 if (howManyBuckets > 0)
00116 {
00117 for (register int i = 0; i < howManyBuckets; i++)
00118 {
00119 bucketCount[i] = 0;
00120 }
00121 }
00122 }