SIMLIB/C++  3.07
histo.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file histo.cc Histogram implamentation
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // Histogram implementation
11 //
12 
13 ////////////////////////////////////////////////////////////////////////////
14 // interface
15 //
16 
17 #include "simlib.h"
18 #include "internal.h"
19 
20 ////////////////////////////////////////////////////////////////////////////
21 // implementation
22 //
23 
24 namespace simlib3 {
25 
27 
28 // LIMIT: maximum number of histogram items -- implementation dependent
29 // 80x86 => segment 64KB =>
30 const unsigned MAXHISTOCOUNT = 10000;
31 
32 ////////////////////////////////////////////////////////////////////////////
33 // allocate
34 //
35 static unsigned *Alloc(unsigned n)
36 {
37  unsigned *dptr = new unsigned[n];
38 // if (!dptr)
39 // SIMLIB_error(MemoryError); // check (old version)
40  for(unsigned i=0; i<n; i++)
41  dptr[i] = 0; // initialize
42  return dptr;
43 }
44 
45 
46 ////////////////////////////////////////////////////////////////////////////
47 // constructors
48 //
50  low(0),
51  step(1),
52  count(10)
53 {
54  Dprintf(("Histogram::Histogram()"));
55  dptr = Alloc(count+2);
56 }
57 
58 Histogram::Histogram(double l, double s, unsigned c) :
59  low(l), // init low limit
60  step(s), // init step
61  count(c) // init count
62 {
63  Dprintf(("Histogram::Histogram(%g,%g,%u)",l,s,c));
64  if(s<=0) SIMLIB_error(HistoStepError);
65  if(c==0 || c>MAXHISTOCOUNT) SIMLIB_error(HistoCountError);
66  dptr = Alloc(count+2);
67 }
68 
69 Histogram::Histogram(const char *n, double l, double s, unsigned c) :
70  low(l), // init low limit
71  step(s), // init step
72  count(c) // init count
73 {
74  Dprintf(("Histogram::Histogram(\"%s\",%g,%g,%u)",n,l,s,c));
75  SetName(n); // set object name
76  if(s<=0) SIMLIB_error(HistoStepError);
77  if(c==0 || c>MAXHISTOCOUNT) SIMLIB_error(HistoCountError);
78  dptr = Alloc(count+2);
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////
82 // destructor
83 //
85 {
86  Dprintf(("Histogram::~Histogram() // \"%s\" ", Name().c_str()));
87  delete[] dptr;
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////
91 // operator []
92 //
93 unsigned Histogram::operator [] (unsigned i) const
94 {
95  if (i>count) i = count+1;
96  return dptr[i];
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////
100 // operator () - value recording
101 //
103 {
104  stat(x);
105  if(x<low)
106  {
107  dptr[0]++;
108  return;
109  }
110  unsigned ix = unsigned((x-low)/step);
111  if(ix>count)
112  dptr[count+1]++;
113  else
114  dptr[ix+1]++;
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////
118 // Init
119 //
120 void Histogram::Init(double l, double s, unsigned c)
121 {
122  Dprintf(("Histogram::Init(%g,%g,%i)",l,s,c));
123  low = l; // init low limit
124  if(s<=0) SIMLIB_error(HistoStepError);
125  step = s; // init step
126  if(c==0 || c>MAXHISTOCOUNT) SIMLIB_error(HistoCountError);
127  if (dptr && count!=c)
128  {
129  delete[] dptr;
130  dptr = 0;
131  }
132  if (!dptr)
133  {
134  count = c; // init count
135  dptr = Alloc(count+2);
136  }
137  Clear(); // init
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////
141 // Clear
142 //
144 {
145  Dprintf(("Histogram::Clear()"));
146  for(unsigned i=0; i<count+2; i++)
147  dptr[i] = 0;
148  stat.Clear();
149 }
150 
151 }
152 // end
153 
virtual void Clear()
Definition: histo.cc:143
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
static unsigned * Alloc(unsigned n)
Definition: histo.cc:35
virtual void Clear()
initialize
Definition: stat.cc:77
void SetName(const std::string &name)
assign the name
Definition: object.cc:125
const unsigned MAXHISTOCOUNT
Definition: histo.cc:30
void Init(double low, double step, unsigned count)
Definition: histo.cc:120
unsigned count
Definition: simlib.h:729
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
unsigned * dptr
Definition: simlib.h:726
Internal header file for SIMLIB/C++.
Main SIMLIB/C++ interface.
virtual std::string Name() const
get object name
Definition: object.cc:134
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
unsigned operator[](unsigned i) const
Definition: histo.cc:93
#define Dprintf(f)
Definition: internal.h:100
void operator()(double x)
Definition: histo.cc:102