SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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
26
SIMLIB_IMPLEMENTATION
;
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
//
49
Histogram::Histogram
() :
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
//
84
Histogram::~Histogram
()
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
//
102
void
Histogram::operator ()
(
double
x)
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
//
143
void
Histogram::Clear
()
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
simlib3::Histogram::operator[]
unsigned operator[](unsigned i) const
Definition
histo.cc:93
simlib3::Histogram::step
double step
Definition
simlib.h:733
simlib3::Histogram::Histogram
Histogram()
Definition
histo.cc:49
simlib3::Histogram::dptr
unsigned * dptr
Definition
simlib.h:731
simlib3::Histogram::operator()
void operator()(double x)
Definition
histo.cc:102
simlib3::Histogram::stat
Stat stat
Definition
simlib.h:736
simlib3::Histogram::Clear
virtual void Clear()
Definition
histo.cc:143
simlib3::Histogram::Init
void Init(double low, double step, unsigned count)
Definition
histo.cc:120
simlib3::Histogram::low
double low
Definition
simlib.h:732
simlib3::Histogram::~Histogram
~Histogram()
Definition
histo.cc:84
simlib3::Histogram::count
unsigned count
Definition
simlib.h:734
simlib3::SimObject::SetName
void SetName(const std::string &name)
assign the name
Definition
object.cc:125
simlib3::SimObject::Name
virtual std::string Name() const
get object name
Definition
object.cc:134
simlib3::Stat::Clear
virtual void Clear()
initialize
Definition
stat.cc:77
internal.h
Internal header file for SIMLIB/C++.
Dprintf
#define Dprintf(f)
Definition
internal.h:100
simlib3
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition
algloop.cc:32
simlib3::SIMLIB_error
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition
error.cc:38
simlib3::SIMLIB_IMPLEMENTATION
SIMLIB_IMPLEMENTATION
Definition
algloop.cc:34
simlib3::Alloc
static unsigned * Alloc(unsigned n)
Definition
histo.cc:35
simlib3::MAXHISTOCOUNT
const unsigned MAXHISTOCOUNT
Definition
histo.cc:30
simlib3::HistoCountError
@ HistoCountError
Definition
errors.cc:34
simlib3::HistoStepError
@ HistoStepError
Definition
errors.cc:33
simlib.h
Main SIMLIB/C++ interface.
Generated on
for SIMLIB/C++ by
1.18.0