Another recurring issue; How to effectively create a histogram of some data? A histogram is also called a frequency distribution.
I have created a small class in C++ that provides a histogram and can count the frequency of data values within a range. The code is supplied below for your amusement. If you have any idea on how to improve on the code, please let me know in a comment. Of course, the standard template library could be used, e.g. std::vector<unsigned int>, if you like to have an even more simple class. But I like code that have no includes :-)
// A histogram class.
// The Histogram object can keep a tally of values
// within a range, the range is arranged into some
// number of bins specified during construction.
// Any allocation of a Histogram object may throw
// a bad_alloc exception.
class Histogram
{
public:
// Construct a histogram that can count
// within a range of values.
// All bins of the histogram are set to zero.
Histogram(
const double& Start,
const double& End,
const unsigned int& nBins):
Start(Start),
nBins_by_interval(nBins/(End-Start)),
nBins(nBins),
freq( new unsigned int[nBins] )
{
for(unsigned int i(0); i < nBins; ++i)
freq[i] = 0U;
}
// Construct a histogram by copying another one.
Histogram(const Histogram& other):
Start(other.Start),
nBins_by_interval(other.nBins_by_interval),
nBins(other.nBins),
freq( new unsigned int[nBins] )
{
for(unsigned int i(0); i < nBins; ++i)
freq[i] = other.freq[i];
}
// Deallocate the memory that was allocated for
// the tallied counts.
~Histogram() {delete[] freq;}
// Set this histogram equal to another.
Histogram& operator=(const Histogram& other)
{
if( this != &other )
{
Start = other.Start;
nBins_by_interval = other.nBins_by_interval;
if( nBins != other.nBins )
{
nBins = other.nBins;
delete[] freq;
freq = new unsigned int[nBins];
}
for(unsigned int i(0); i < nBins; ++i)
freq[i] = other.freq[i];
}
return *this;
}
// Increase the count for the bin that holds a
// value that is in range for this histogram.
void Add(const double& x)
{
const unsigned int i(
static_cast<unsigned int>(
(x-Start)*nBins_by_interval) );
if( i < nBins ) freq[i]++;
}
// Get the sum of all counts in the histogram.
const unsigned int GetTotalCount() const
{
unsigned int c(0U);
for( unsigned int i(0); i < nBins; ++i )
c += freq[i];
return c;
}
private:
double Start,nBins_by_interval;
unsigned int nBins;
unsigned int* freq;
};
This blog is all about issues related to the business of the company