Commit 0b4af10e authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - removed some if's for better performance

parent 699888bd
......@@ -29,44 +29,33 @@
#include "WValueSetHistogram.h"
WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet )
WValueSetHistogram::WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet ):
m_minimum( valueSet->getMinimumValue() ),
m_maximum( valueSet->getMaximumValue() )
{
// calculate min max
m_minimum = wlimits::MAX_DOUBLE;
m_maximum = wlimits::MIN_DOUBLE;
double minDistance = wlimits::MAX_DOUBLE;
for( size_t i = 0; i != valueSet->size(); ++i )
{
double tmp = valueSet->getScalarDouble( i );
m_maximum = m_maximum < tmp ? tmp : m_maximum;
m_minimum = m_minimum > tmp ? tmp : m_minimum;
// create base histogram
m_nInitialBuckets = 100;
m_initialBucketSize = ( m_maximum - m_minimum ) / static_cast< double >( m_nInitialBuckets );
WAssert( m_initialBucketSize > 0.0, "WValueSetHistogram::WValueSetHistogram() : m_initialBucketSize to small." );
if( m_minimum != tmp && m_minimum != wlimits::MAX_DOUBLE )
{
minDistance = tmp - m_minimum < minDistance ? tmp - m_minimum : minDistance;
}
if( m_maximum != tmp && m_maximum != wlimits::MIN_DOUBLE )
{
minDistance = m_maximum - tmp < minDistance ? m_maximum - tmp : minDistance;
}
}
// create and initialize array to zero which finally contains the counts
size_t* initialBuckets = new size_t[ m_nInitialBuckets ];
memset( initialBuckets, 0, m_nInitialBuckets * sizeof( size_t ) );
// *initialBuckets = { 0 }; // this should works with C++0x (instead memset), TEST IT!
// create base histogram
m_nInitialBuckets = ( ( m_maximum - m_minimum ) / minDistance ) + 1;
std::cout << "m_nInitialBuckets" <<m_nInitialBuckets << std::endl;
m_bucketSize = minDistance;
unsigned int* initialBuckets = new unsigned int[m_nInitialBuckets];
// initialize array to zero
memset( initialBuckets, 0, m_nInitialBuckets * sizeof( unsigned int ) );
//initialBuckets = { 0 }; // this should works with C++0x (instead memset), TEST IT!
m_initialBuckets = boost::shared_array< unsigned int >( initialBuckets );
// the array can be shared among several instances of WValueSetHistogram.
m_initialBuckets = boost::shared_array< size_t >( initialBuckets );
m_nMappedBuckets = 0;
// no mapping applied yet. Initial and mapped are equal
m_nMappedBuckets = m_nInitialBuckets;
m_mappedBuckets = m_initialBuckets;
m_mappedBucketSize = m_initialBucketSize;
// and finally create the histogram
for( size_t i = 0; i < valueSet->size(); ++i )
{
double tmp = valueSet->getScalarDouble( i );
// increment( tmp );
insert( tmp );
}
}
/*
......@@ -95,11 +84,11 @@ WValueSetHistogram::WValueSetHistogram( const WValueSetBase& valueSet )
// create base histogram
m_nInitialBuckets = ( ( m_maximum - m_minimum ) / minDistance ) + 1;
m_bucketSize = minDistance;
unsigned int* initialBuckets = new unsigned int[m_nInitialBuckets];
size_t* initialBuckets = new size_t[m_nInitialBuckets];
// initialize array to zero
memset( initialBuckets, 0, m_nInitialBuckets * sizeof( unsigned int ) );
memset( initialBuckets, 0, m_nInitialBuckets * sizeof( size_t ) );
//*initialBuckets = { 0 }; // this should works with C++0x (instead memset), TEST IT!
m_initialBuckets = boost::shared_array< unsigned int >( initialBuckets );
m_initialBuckets = boost::shared_array< size_t >( initialBuckets );
m_nMappedBuckets = 0;
......@@ -114,7 +103,7 @@ WValueSetHistogram::WValueSetHistogram( const WValueSetHistogram& histogram, dou
{
// copy constructor
m_nInitialBuckets = histogram.getNInitialBuckets();
m_initialBuckets = boost::shared_array< unsigned int >( histogram.getInitialBuckets() );
m_initialBuckets = boost::shared_array< size_t >( histogram.getInitialBuckets() );
m_bucketSize = histogram.getBucketSize();
m_nMappedBuckets = 0;
......@@ -133,48 +122,36 @@ WValueSetHistogram::~WValueSetHistogram()
{
}
boost::shared_array< unsigned int > WValueSetHistogram::getInitialBuckets() const
boost::shared_array< size_t > WValueSetHistogram::getInitialBuckets() const
{
return m_initialBuckets;
}
unsigned int WValueSetHistogram::getNInitialBuckets() const
size_t WValueSetHistogram::getNInitialBuckets() const
{
return m_nInitialBuckets;
}
double WValueSetHistogram::getBucketSize() const
double WValueSetHistogram::getInitialBucketSize() const
{
return m_bucketSize;
return m_initialBucketSize;
}
void WValueSetHistogram::increment( double value )
double WValueSetHistogram::getBucketSize() const
{
WAssert( m_bucketSize > 0.0, "WValueSetHistogram::increment() : m_bucketSize to small." );
unsigned int index = static_cast<unsigned int>( value / m_bucketSize );
m_initialBuckets[index]++;
return m_mappedBucketSize;
}
unsigned int WValueSetHistogram::setInterval( double intervalSize )
void WValueSetHistogram::insert( double value )
{
if( m_bucketSize == intervalSize )
{
if( m_mappedBuckets )
{
m_mappedBuckets.reset();
m_nMappedBuckets = 0;
}
}
else
{
calculateMapping( intervalSize );
}
return m_nMappedBuckets;
size_t index = static_cast< size_t >( ( value - m_minimum ) / static_cast< double >( m_mappedBucketSize ) );
m_mappedBuckets[ index ]++;
}
void WValueSetHistogram::calculateMapping( double intervalSize )
{
unsigned int ratio = static_cast<unsigned int>( intervalSize / m_bucketSize );
/*
size_t ratio = static_cast<size_t>( intervalSize / m_bucketSize );
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 );
......@@ -184,63 +161,47 @@ void WValueSetHistogram::calculateMapping( double intervalSize )
m_mappedBuckets.reset();
}
unsigned int* mappedBuckets = new unsigned int[m_nMappedBuckets];
memset( mappedBuckets, 0, m_nMappedBuckets * sizeof( unsigned int ) );
//*mappedBuckets = { 0 }; // works with C++0x
boost::scoped_array< unsigned int > scoped( mappedBuckets );
size_t* mappedBuckets = new size_t[m_nMappedBuckets];
memset( mappedBuckets, 0, m_nMappedBuckets * sizeof( size_t ) );
// *mappedBuckets = { 0 }; // works with C++0x
boost::scoped_array< size_t > scoped( mappedBuckets );
m_mappedBuckets.swap( scoped );
unsigned int index = 0;
for( unsigned int i = 0; i != m_nInitialBuckets; ++i )
size_t index = 0;
for( size_t i = 0; i != m_nInitialBuckets; ++i )
{
if( i % ratio == 0 && i != 0 )
{
index++;
}
m_mappedBuckets[index] += m_initialBuckets[i];
}
}*/
}
unsigned int WValueSetHistogram::operator[]( unsigned int index )
size_t WValueSetHistogram::operator[]( size_t index )
{
unsigned int value = 0;
if( m_mappedBuckets )
{
index = m_mappedBuckets[index];
}
else
{
value = m_initialBuckets[index];
}
return value;
// if no mapping has been applied, mappedBuckets points to the initial bucket
return m_mappedBuckets[ index ];
}
unsigned int WValueSetHistogram::at( unsigned int index )
size_t WValueSetHistogram::at( size_t index )
{
unsigned int value = 0;
if( m_mappedBuckets )
{
WAssert( index < m_nMappedBuckets, "WValueSetHistogram::at() : index out of range." );
value = m_mappedBuckets[index];
}
else
{
WAssert( index < m_nInitialBuckets, "WValueSetHistogram::at() : index out of range." );
value = m_initialBuckets[index];
}
return value;
WAssert( index < m_nMappedBuckets, "WValueSetHistogram::at() : index out of range." );
// if no mapping has been applied, mappedBuckets points to the initial bucket
return m_mappedBuckets[ index ];
}
unsigned int WValueSetHistogram::size() const
size_t WValueSetHistogram::size() const
{
return (m_mappedBuckets ? m_nMappedBuckets : m_nInitialBuckets);
return m_nMappedBuckets;
}
double WValueSetHistogram::getMin() const
double WValueSetHistogram::getMinimum() const
{
return m_minimum;
}
double WValueSetHistogram::getMax() const
double WValueSetHistogram::getMaximum() const
{
return m_maximum;
}
......
......@@ -60,8 +60,7 @@ public:
//explicit WValueSetHistogram( const WValueSetBase& valueSet );
/**
* Copy constructor. If another interval size is given setInterval() is called and
* the mapped histogram is calculated.
* Copy constructor. If another interval size is given the histogram gets matched to it using the initial bucket data.
*
* \param histogram another WValueSetHistogram
* \param intervalSize the size of one bucket in the mapped histogram
......@@ -73,15 +72,6 @@ public:
*/
~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.
*
......@@ -90,7 +80,7 @@ public:
*
* \return elements in the bucket.
*/
unsigned int operator[]( unsigned int index );
size_t operator[]( size_t index );
/**
* Get the size of the bucket. Testing if the position is valid.
......@@ -100,28 +90,35 @@ public:
*
* \return elements in the bucket
*/
unsigned int at( unsigned int index );
size_t at( size_t index );
/**
* Returns the number of bars in the histogram with the actual mapping.
* Returns the number of buckets in the histogram with the actual mapping.
*
* \return number of buckets
*/
unsigned int size() const;
size_t size() const;
/**
* Returns the minimum value in the value set.
*
* \return minimum
*/
double getMin() const;
double getMinimum() const;
/**
* Returns the maximum value in the value set.
*
* \return maximum
*/
double getMax() const;
double getMaximum() const;
/**
* Return the size of one bucket.
*
* \return the size of a bucket.
*/
double getBucketSize() const;
protected:
/**
......@@ -129,21 +126,21 @@ protected:
*
* \return m_initialBuckets
*/
boost::shared_array< unsigned int > getInitialBuckets() const;
boost::shared_array< size_t > getInitialBuckets() const;
/**
* Return the number of initial buckets.
*
* \return m_nInitialBuckets
*/
unsigned int getNInitialBuckets() const;
size_t getNInitialBuckets() const;
/**
* Return the size of one initial bucket.
*
* \return m_bucketSize
*/
double getBucketSize() const;
double getInitialBucketSize() const;
private:
/**
......@@ -159,27 +156,32 @@ private:
/**
* Size of one bucket in the initial histogram.
*/
double m_bucketSize;
double m_initialBucketSize;
/**
* Pointer to all initial buckets of the histogram.
*/
boost::shared_array< unsigned int > m_initialBuckets;
boost::shared_array< size_t > m_initialBuckets;
/**
* Number of buckets in the initial histogram.
*/
unsigned int m_nInitialBuckets;
size_t m_nInitialBuckets;
/**
* Pointer to all the buckets in the mapped histogram.
* Pointer to all initial buckets of the histogram.
*/
boost::scoped_array< unsigned int > m_mappedBuckets;
boost::shared_array< size_t > m_mappedBuckets;
/**
* Tracks the number of a buckets in the mapped histogram.
*/
unsigned int m_nMappedBuckets;
size_t m_nMappedBuckets;
/**
* Size of one bucket in the mapped histogram.
*/
double m_mappedBucketSize;
/**
* To calculate the new buckets
......@@ -194,7 +196,7 @@ private:
*
* \param value value to increment
*/
void increment( double value );
void insert( 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