Commit 767467ae authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - renamed WHistogram in dataStructures to better match its real purpose.

[STYLE] - improved style and spelling
parent df9d1a6f
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WHISTOGRAM_H
#define WHISTOGRAM_H
#include <map>
#include <list>
#include <utility>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/shared_array.hpp>
#include "../WValueSet.h"
/**
* Used to track (later: display) the occurrence frequencies of values in a value set.
**/
class WHistogram
{
private:
/**
* The smalest value in the ValueSet
**/
double m_minimum;
/**
* The biggest value in the ValueSet
**/
double m_maximum;
/**
* Size of one bucket in the initial histogram.
**/
double m_bucketSize;
/**
* Pointer to all initial buckets of the histogram.
**/
boost::shared_array< unsigned int > m_initialBuckets;
/**
* Number of buckets in the initial histogram.
**/
unsigned int m_nInitialBuckets;
/**
* Pointer to all the buckets in the mapped histogram.
**/
boost::scoped_array< unsigned int > m_mappedBuckets;
/**
* Tracks the number of a buckets in the mapped histogram.
**/
unsigned int m_nMappedBuckets;
/**
* To calculate the new buckets
*
* \param intervalSize the size of one bucket
**/
void calculateMapping( double intervalSize );
/**
* increment the value by one, contains the logic to find the element place in the array.
* Should only be used in the constructor ie. while iterating over WValueSet.
*
* \param value value to increment
**/
void increment( double value );
protected:
/**
* Return the initial buckets.
*
* \return m_initialBuckets
**/
boost::shared_array< unsigned int > getInitialBuckets() const;
/**
* Return the number of initial buckets.
*
* \return m_nInitialBuckets
**/
unsigned int getNInitialBuckets() const;
/**
* Return the size of one initial bucket.
*
* \return m_bucketSize
**/
double getBucketSize() const;
public:
/**
* Constructor.
*
* \param valueSet source of the data for the histogram
**/
explicit WHistogram( boost::shared_ptr< WValueSetBase > valueSet );
/**
* Constructor.
*
* \param valueSet source of the data for the histogram
**/
explicit WHistogram( const WValueSetBase& valueSet );
/**
* Copy constructor. If another interval size is given setInterval() is called and a
* the mapped histogram is calculated.
*
* \param histogram another WHisogram
* \param intervalSize the size of one bucket in the mapped histogram
**/
explicit WHistogram( const WHistogram& histogram, double intervalSize = 0.0 );
/**
* Destructor.
**/
~WHistogram();
/**
* Set the new intervall size.
*
* \param intervalSize size of the interval for each mapped bucket.
*
* \return size of the new (mapped) histogram.
**/
unsigned int setInterval( double intervalSize );
/**
* Get the size of the bucket.
*
* \param index which buckets size is to be returned, starts with 0 which is the bucket
* containing the smalest values.
*
* \return elements in the bucket.
**/
unsigned int operator[]( unsigned int index );
/**
* Get the size of the bucket. Testing if the position is valid.
*
* \param index which buckets size is to be returned, starts with 0 which is the bar with
* the smalest values
*
* \return elements in the bucket
**/
unsigned int at( unsigned int index );
/**
* Returns the number of bars in the histogram with the actual mapping.
*
* \return number of buckets
**/
unsigned int size() const;
/**
* Retruns the minimum value in the value set.
*
* \return minimum
**/
double getMin() const;
/**
* Retruns the maximum value in the value set.
*
* \return maximum
**/
double getMax() const;
/**
* to test if the histogram works
**/
void test();
};
#endif // WHISTOGRAM_H
......@@ -22,16 +22,14 @@
//
//---------------------------------------------------------------------------
#include <cmath> // test() -> log()
#include <cstring> // memset()
#include <iostream> // test() -> std::cout
#include "../../common/WAssert.h"
#include "../../common/WLimits.h"
#include "WHistogram.h"
#include "WValueSetHistogram.h"
WHistogram::WHistogram( boost::shared_ptr< WValueSetBase > valueSet )
WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet )
{
// calculate min max
m_minimum = wlimits::MAX_DOUBLE;
......@@ -71,7 +69,7 @@ WHistogram::WHistogram( boost::shared_ptr< WValueSetBase > valueSet )
}
}
WHistogram::WHistogram( const WValueSetBase& valueSet )
WValueSetHistogram::WValueSetHistogram( const WValueSetBase& valueSet )
{
// calculate min max
m_minimum = wlimits::MAX_DOUBLE;
......@@ -111,7 +109,7 @@ WHistogram::WHistogram( const WValueSetBase& valueSet )
}
}
WHistogram::WHistogram( const WHistogram& histogram, double intervalSize )
WValueSetHistogram::WValueSetHistogram( const WValueSetHistogram& histogram, double intervalSize )
{
// copy constructor
m_nInitialBuckets = histogram.getNInitialBuckets();
......@@ -125,38 +123,38 @@ WHistogram::WHistogram( const WHistogram& histogram, double intervalSize )
if( intervalSize != 0.0 )
{
WAssert( intervalSize > 0.0, "WHistogram::WHistogram : intervalSize has to be greater then zero." );
WAssert( intervalSize > 0.0, "WValueSetHistogram::WValueSetHistogram : intervalSize has to be greater then zero." );
calculateMapping( intervalSize );
}
}
WHistogram::~WHistogram()
WValueSetHistogram::~WValueSetHistogram()
{
}
boost::shared_array< unsigned int > WHistogram::getInitialBuckets() const
boost::shared_array< unsigned int > WValueSetHistogram::getInitialBuckets() const
{
return m_initialBuckets;
}
unsigned int WHistogram::getNInitialBuckets() const
unsigned int WValueSetHistogram::getNInitialBuckets() const
{
return m_nInitialBuckets;
}
double WHistogram::getBucketSize() const
double WValueSetHistogram::getBucketSize() const
{
return m_bucketSize;
}
void WHistogram::increment( double value )
void WValueSetHistogram::increment( double value )
{
WAssert( m_bucketSize > 0.0, "WHistogram::increment() : m_bucketSize to small." );
WAssert( m_bucketSize > 0.0, "WValueSetHistogram::increment() : m_bucketSize to small." );
unsigned int index = static_cast<unsigned int>( value / m_bucketSize );
m_initialBuckets[index]++;
}
unsigned int WHistogram::setInterval( double intervalSize )
unsigned int WValueSetHistogram::setInterval( double intervalSize )
{
if( m_bucketSize == intervalSize )
{
......@@ -173,10 +171,10 @@ unsigned int WHistogram::setInterval( double intervalSize )
return m_nMappedBuckets;
}
void WHistogram::calculateMapping( double intervalSize )
void WValueSetHistogram::calculateMapping( double intervalSize )
{
unsigned int ratio = static_cast<unsigned int>( intervalSize / m_bucketSize );
WAssert( ratio > 1, "WHistogram::calculateMapping() : intervalSize has to be greater then the original size." );
WAssert( ratio > 1, "WValueSetHistogram::calculateMapping() : intervalSize has to be greater then the original size." );
// number of elements in the new mapped histogram = division + (round up)
m_nMappedBuckets = m_nInitialBuckets / ratio + ( m_nInitialBuckets % ratio > 0 ? 1 : 0 );
......@@ -201,7 +199,7 @@ void WHistogram::calculateMapping( double intervalSize )
}
}
unsigned int WHistogram::operator[]( unsigned int index )
unsigned int WValueSetHistogram::operator[]( unsigned int index )
{
unsigned int value = 0;
if( m_mappedBuckets )
......@@ -215,75 +213,34 @@ unsigned int WHistogram::operator[]( unsigned int index )
return value;
}
unsigned int WHistogram::at( unsigned int index )
unsigned int WValueSetHistogram::at( unsigned int index )
{
unsigned int value = 0;
if( m_mappedBuckets )
{
WAssert( index < m_nMappedBuckets, "WHistogram::at() : index out of range." );
WAssert( index < m_nMappedBuckets, "WValueSetHistogram::at() : index out of range." );
value = m_mappedBuckets[index];
}
else
{
WAssert( index < m_nInitialBuckets, "WHistogram::at() : index out of range." );
WAssert( index < m_nInitialBuckets, "WValueSetHistogram::at() : index out of range." );
value = m_initialBuckets[index];
}
return value;
}
unsigned int WHistogram::size() const
unsigned int WValueSetHistogram::size() const
{
return (m_mappedBuckets ? m_nMappedBuckets : m_nInitialBuckets);
}
double WHistogram::getMin() const
double WValueSetHistogram::getMin() const
{
return m_minimum;
}
double WHistogram::getMax() const
double WValueSetHistogram::getMax() const
{
return m_maximum;
}
void WHistogram::test()
{
unsigned int* ptr;
unsigned int histSize;
if( m_mappedBuckets )
{
ptr = m_mappedBuckets.get();
histSize = m_nMappedBuckets;
}
else
{
ptr = m_initialBuckets.get();
histSize = m_nInitialBuckets;
}
for( unsigned int y = 10; y != 0; --y )
{
std::cout << "|";
unsigned int* h_ptr = ptr;
for( unsigned int x = 0; x != histSize; ++x )
{
if( log( static_cast<double>( *h_ptr ) ) > static_cast<double>( (y-1)*2 ) )
{
std::cout << " #";
}
else
{
std::cout << " ";
}
h_ptr++;
}
std::cout << " |\n";
}
std::cout << "-";
for(unsigned int i = 0; i != histSize; ++i)
{
std::cout << "--";
}
std::cout << "--\n";
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WVALUESETHISTOGRAM_H
#define WVALUESETHISTOGRAM_H
#include <map>
#include <list>
#include <utility>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/shared_array.hpp>
#include "../WValueSet.h"
/**
* Used to find the occurrence frequencies of values in a value set. It implements a classical histogram but allows easy modification of bucket
* sizes without unnecessary recalculation of the whole histogram.
*
* \note This histogram is different from from WValueSetHistogram which is a generic histogram class.
*/
class WValueSetHistogram
{
public:
/**
* Constructor. Creates the histogram for the specified value set.
*
* \param valueSet source of the data for the histogram
*/
explicit WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet );
/**
* Constructor. Creates the histogram for the specified value set.
*
* \param valueSet source of the data for the histogram
*/
explicit WValueSetHistogram( const WValueSetBase& valueSet );
/**
* Copy constructor. If another interval size is given setInterval() is called and
* the mapped histogram is calculated.
*
* \param histogram another WValueSetHistogram
* \param intervalSize the size of one bucket in the mapped histogram
*/
explicit WValueSetHistogram( const WValueSetHistogram& histogram, double intervalSize = 0.0 );
/**
* Destructor.
*/
~WValueSetHistogram();
/**
* Set the new interval size.
*
* \param intervalSize size of the interval for each mapped bucket.
*
* \return size of the new (mapped) histogram.
*/
unsigned int setInterval( double intervalSize );
/**
* Get the size of the bucket.
*
* \param index which buckets size is to be returned, starts with 0 which is the bucket
* containing the smallest values.
*
* \return elements in the bucket.
*/
unsigned int operator[]( unsigned int index );
/**
* Get the size of the bucket. Testing if the position is valid.
*
* \param index which buckets size is to be returned, starts with 0 which is the bar with
* the smallest values
*
* \return elements in the bucket
*/
unsigned int at( unsigned int index );
/**
* Returns the number of bars in the histogram with the actual mapping.
*
* \return number of buckets
*/
unsigned int size() const;
/**
* Returns the minimum value in the value set.
*
* \return minimum
*/
double getMin() const;
/**
* Returns the maximum value in the value set.
*
* \return maximum
*/
double getMax() const;
protected:
/**
* Return the initial buckets.
*
* \return m_initialBuckets
*/
boost::shared_array< unsigned int > getInitialBuckets() const;
/**
* Return the number of initial buckets.
*
* \return m_nInitialBuckets
*/
unsigned int getNInitialBuckets() const;
/**
* Return the size of one initial bucket.
*
* \return m_bucketSize
*/
double getBucketSize() const;
private:
/**
* The smallest value in the ValueSet
*/
double m_minimum;
/**
* The biggest value in the ValueSet
*/
double m_maximum;
/**
* Size of one bucket in the initial histogram.
*/
double m_bucketSize;
/**
* Pointer to all initial buckets of the histogram.
*/
boost::shared_array< unsigned int > m_initialBuckets;
/**
* Number of buckets in the initial histogram.
*/
unsigned int m_nInitialBuckets;
/**
* Pointer to all the buckets in the mapped histogram.
*/
boost::scoped_array< unsigned int > m_mappedBuckets;
/**
* Tracks the number of a buckets in the mapped histogram.
*/
unsigned int m_nMappedBuckets;
/**
* To calculate the new buckets
*
* \param intervalSize the size of one bucket
*/
void calculateMapping( double intervalSize );
/**
* increment the value by one, contains the logic to find the element place in the array.
* Should only be used in the constructor i.e. while iterating over WValueSet.
*
* \param value value to increment
*/
void increment( double value );
};
#endif // WVALUESETHISTOGRAM_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment