PSCF v1.1
IntDistribution.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "IntDistribution.h"
9#include <util/format/Int.h>
10#include <util/global.h>
11
12namespace Util
13{
14
15 /*
16 * Constructor.
17 */
19 : histogram_(),
20 min_(0),
21 max_(0),
22 nBin_(0),
23 nSample_(0),
24 nReject_(0)
25 { setClassName("IntDistribution"); }
26
27 /*
28 * Copy constructor.
29 */
31 : histogram_(),
32 min_(other.min_),
33 max_(other.max_),
34 nBin_(other.nBin_),
35 nSample_(other.nSample_),
36 nReject_(other.nReject_)
37 {
38 if (nBin_ > 0) {
39 assert(nBin_ == max_ - min_ + 1);
40 assert(other.histogram_.capacity() != 0);
42 for (int i=0; i < nBin_; ++i) {
43 histogram_[i] = other.histogram_[i];
44 }
45 } else {
46 assert(other.histogram_.capacity() == 0);
47 assert(min_ == 0);
48 assert(max_ == 0);
49 assert(nSample_ == 0);
50 assert(nReject_ == 0);
51 }
52 }
53
54 /*
55 * Assignment operator.
56 */
58 {
59 // Check for self assignment
60 if (this == &other) return *this;
61
62 // Check validity of other object
63 if (other.nBin_ > 0) {
64 assert(other.nBin_ == other.max_ - other.min_ + 1);
65 assert(other.histogram_.capacity() != 0);
66 } else {
67 assert(other.nBin_ == 0);
68 assert(other.histogram_.capacity() == 0);
69 assert(other.min_ == 0);
70 assert(other.max_ == 0);
71 assert(other.nSample_ == 0);
72 assert(other.nReject_ == 0);
73 }
74
75 // Assign primitive values
76 min_ = other.min_;
77 max_ = other.max_;
78 nBin_ = other.nBin_;
79 nSample_ = other.nSample_;
80 nReject_ = other.nReject_;
81
82 // Allocate histogram if necessary
83 if (nBin_ > 0) {
85 for (int i=0; i < nBin_; ++i) {
86 histogram_[i] = other.histogram_[i];
87 }
88 }
89
90 return *this;
91 }
92
93 /*
94 * Destructor.
95 */
97 {}
98
99 /*
100 * Read min and max from file.
101 */
102 void IntDistribution::readParameters(std::istream& in)
103 {
104 read<int>(in, "min", min_);
105 read<int>(in, "max", max_);
106 nBin_ = max_ - min_ + 1;
108 clear();
109 }
110
111 /*
112 * Set parameters and initialize.
113 */
114 void IntDistribution::setParam(int min, int max)
115 {
116 min_ = min;
117 max_ = max;
118 nBin_ = max_ - min_ + 1;
120 clear();
121 }
122
123 /*
124 * Load state from an archive.
125 */
127 {
128 loadParameter<int>(ar, "min", min_);
129 loadParameter<int>(ar, "max", max_);
130 ar & nBin_;
131 ar & nSample_;
132 ar & nReject_;
133 ar & histogram_;
134
135 // Validate
136 if (nBin_ != max_ - min_ + 1) {
137 UTIL_THROW("Inconsistent values of nBin");
138 }
139 if (nBin_ != histogram_.capacity()) {
140 UTIL_THROW("Inconsistent histogram capacity");
141 }
142 }
143
144 /*
145 * Save state to an archive.
146 */
148 { ar & *this; }
149
150 /*
151 * Zero all accumulators.
152 */
154 {
155 nSample_ = 0;
156 nReject_ = 0;
157 for (int i=0; i < nBin_; ++i) {
158 histogram_[i] = 0;
159 }
160 }
161
162 /*
163 * Add a value to the histogram
164 */
166 {
167 int i;
168 if (value >= min_ && value <= max_) {
169 i = binIndex(value);
170 histogram_[i] += 1;
171 nSample_ += 1;
172 } else {
173 nReject_ += 1;
174 }
175 }
176
177 /*
178 * Output histogram
179 */
180 void IntDistribution::output(std::ostream& out)
181 {
182 for (int i=0; i < nBin_; ++i) {
183 out << Int(i + min_) << Int(histogram_[i]) << std::endl;
184 }
185 }
186
187}
int capacity() const
Return allocated size.
Definition: Array.h:159
Saving archive for binary istream.
Saving / output archive for binary ostream.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:199
A distribution (or histogram) of values for an int variable.
IntDistribution()
Default constructor.
int nBin_
number of bins.
virtual ~IntDistribution()
Destructor.
void setParam(int min, int max)
Set parameters and initialize.
int nSample_
Number of sampled values in Histogram.
int max() const
Get maximum value in range of histogram.
int min_
minimum value.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
int binIndex(int value)
Return the index of the bin for a value.
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
void sample(int value)
Sample a value.
DArray< long > histogram_
Histogram array.
IntDistribution & operator=(const IntDistribution &other)
Assignment operator.
void readParameters(std::istream &in)
Read parameters from file and initialize.
int min() const
Get minimum value in range of histogram.
void output(std::ostream &out)
Output the distribution to file.
int nReject_
Number of sampled values that were out of range.
void clear()
Clear (i.e., zero) previously allocated histogram.
int max_
maximum value.
Wrapper for an int, for formatted ostream output.
Definition: Int.h:37
void setClassName(const char *className)
Set class name string.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1