]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGSmplhist.cxx
Rework SGAtomic, move more into the implementation.
[simgear.git] / simgear / structure / SGSmplhist.cxx
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 the GNU C++ Library.  This library is free
7 software; you can redistribute it and/or modify it under the terms of
8 the GNU Library General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.  This library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.  See the GNU Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21 #include <iostream>
22 #include <fstream>
23 #include "SGSmplhist.hxx"
24 #include <math.h>
25
26 #ifndef HUGE_VAL
27 #ifdef HUGE
28 #define HUGE_VAL HUGE
29 #else
30 #include <float.h>
31 #define HUGE_VAL DBL_MAX
32 #endif
33 #endif
34
35 const int SampleHistogramMinimum = -2;
36 const int SampleHistogramMaximum = -1;
37
38 SampleHistogram::SampleHistogram (double low, double high, double width)
39 {
40   if (high < low)
41     {
42       double t = high;
43       high = low;
44       low = t;
45     }
46
47   if (width == -1)
48     {
49       width = (high - low) / 10;
50     }
51
52   howManyBuckets = int ((high - low) / width) + 2;
53   bucketCount = new int[howManyBuckets];
54   bucketLimit = new double[howManyBuckets];
55   double lim = low;
56   for (int i = 0; i < howManyBuckets; i++)
57     {
58       bucketCount[i] = 0;
59       bucketLimit[i] = lim;
60       lim += width;
61     }
62   bucketLimit[howManyBuckets - 1] = HUGE_VAL;   /* from math.h */
63 }
64
65 SampleHistogram::~SampleHistogram ()
66 {
67   if (howManyBuckets > 0)
68     {
69       delete[]bucketCount;
70       delete[]bucketLimit;
71     }
72 }
73
74 void SampleHistogram::operator += (double value)
75 {
76   int i;
77   for (i = 0; i < howManyBuckets; i++)
78     {
79       if (value < bucketLimit[i])
80         break;
81     }
82   bucketCount[i]++;
83   this->SampleStatistic::operator += (value);
84 }
85
86 int SampleHistogram::similarSamples (double d)
87 {
88   int i;
89   for (i = 0; i < howManyBuckets; i++)
90     {
91       if (d < bucketLimit[i])
92         return (bucketCount[i]);
93     }
94   return (0);
95 }
96
97 void SampleHistogram::printBuckets (ostream & s)
98 {
99   for (int i = 0; i < howManyBuckets; i++)
100     {
101       if (bucketLimit[i] >= HUGE_VAL)
102         {
103           s << "< max : " << bucketCount[i] << "\n";
104         }
105       else
106         {
107           s << "< " << bucketLimit[i] << " : " << bucketCount[i] << "\n";
108         }
109     }
110 }
111
112 void SampleHistogram::reset ()
113 {
114   this->SampleStatistic::reset ();
115   if (howManyBuckets > 0)
116     {
117       for (register int i = 0; i < howManyBuckets; i++)
118         {
119           bucketCount[i] = 0;
120         }
121     }
122 }