Commit 2f85c644 authored by Robert Frohl's avatar Robert Frohl
Browse files

ADD WHistogram

parent 32cc9e51
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2010 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/>.
//
//---------------------------------------------------------------------------
#include <iostream> // test() -> std::cout
#include <cmath> // test() -> log()
#include "../common/WAssert.h"
#include "WHistogram.h"
WHistogram::WHistogram() : uniformInterval(0)
{
mappingPointer.first = mapping.begin();
mappingPointer.second = 0;
}
WHistogram::WHistogram( unsigned int interval ) : uniformInterval(interval)
{
mappingPointer.first = mapping.begin();
mappingPointer.second = 0;
}
WHistogram::~WHistogram()
{
}
void WHistogram::add( double value )
{
// IF(MIN || MAX needed) : here!
elements[value]++;
}
void WHistogram::setUniformInterval( unsigned int value )
{
// negativ ?
//WAssert( elements.size > 0, "No elements in WHistogram." );
uniformInterval = value;
calculateMapping();
}
void WHistogram::calculateMapping()
{
if( mapping.size() != 0 ) // is there already a mapping ?
{
mapping.clear();
}
unsigned int count = 0;
unsigned int value = 0;
for( std::map< double, WBar >::const_iterator iter = elements.begin(); iter != elements.end(); ++iter )
{
if( count == uniformInterval )
{
mapping.push_back( std::pair< const WBar*, unsigned int >( &iter->second, value ) );
count = 0;
value = iter->second.getValue();
}
else
{
value += iter->second.getValue();
count++;
}
}
}
unsigned int WHistogram::operator[]( unsigned int position )
{
if(mappingPointer.second == 0 || mappingPointer.second > position )
{
mappingPointer.first = mapping.begin();
mappingPointer.second = 0;
}
for( unsigned int t = mappingPointer.second; t != position; ++t )
{
mappingPointer.first++;
}
mappingPointer.second = position; // == mappingPointer.second++
return mappingPointer.first->second;
}
void WHistogram::setInterval( const std::list<unsigned int>& rangeList )
{
if( mapping.size() != 0 ) // is there already a mapping ?
{
mapping.clear();
}
std::list<unsigned int>::const_iterator iter;
for( iter = rangeList.begin(); iter != rangeList.end(); ++iter )
{
std::map< double, WBar >::const_iterator elementsIter = elements.begin();
unsigned int barHeight = elementsIter->second.getValue();
for( unsigned int i = 0; i != *iter; ++i )
{
elementsIter++;
barHeight += elementsIter->second.getValue();
}
// elementsIter has to point at the first element of the next bucket
elementsIter++;
mapping.push_back( std::pair< const WBar*, unsigned int >( &elementsIter->second, barHeight ) );
}
}
void WHistogram::test()
{
calculateMapping();
/*unsigned int rangeStart= 0;
std::list< std:pair< WBar*, unsigned int > >::iterator iter;
for(iter = mapping.begin(); iter != mapping.end(); iter++ )
{
std::cout << iter->second << ": " << rangeStart << " - " << rangeStart+uniformInterval;
rangeStart += uniformInterval;
}*/
std::cout << mapping.size() << std::endl;
std::list< std::pair< const WBar*, unsigned int > >::iterator it = mapping.begin();
for(it = mapping.begin(); it != mapping.end(); ++it)
{
std::cout << it->second << " ";
}
std::cout << std::endl;
for( unsigned int y = 10; y != 0; --y )
{
std::cout << "|";
std::list< std::pair< const WBar*, unsigned int > >::iterator iter = mapping.begin();
for(unsigned int x = 0; x != mapping.size(); ++x)
{
if( log((double)iter->second) > (double)(y-1)*2 )
{
std::cout << " #";
} else {
std::cout << " ";
}
iter++;
}
std::cout << " |\n";
}
std::cout << "-" ;
for(unsigned int i = 0; i != mapping.size(); ++i)
{
std::cout << "--";
}
std::cout << "--\n";
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2010 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>
class WHistogram
{
/**
* To compare the elements of the std::map used in WHistogram.
**/
class cmp
{
public:
bool operator()( double const& p1, double const& p2) const
{
return p1 < p2;
}
};
/**
* WBar is an element of WHistogram, where the range of monitored values equals 1.
* Meaning WBar represents the amount of each specific value in the value set.
**/
class WBar
{
private:
unsigned int value;
//unsigned int distance;
//std::pair<int, int> range;
public:
/**
* Creates empty bar.
**/
WBar() : value(0)
{
}
/**
* Creates bar.
**/
WBar(WBar const& other) : value(other.value)
{
}
/**
* Destroys this Bar instance.
**/
~WBar()
{
}
/**
* Postfix inkrement (x++) the bar by one.
**/
WBar& operator++(int) // x++
{
WBar* tmp = this;
tmp->value++;
return *tmp;
}
/**
* Prefix inkrement (++x) the bar by one.
**/
WBar& operator++() // ++x
{
WBar* tmp = this;
tmp->value++;
return *tmp;
}
/**
* Returns the value of the bar.
**/
unsigned int getValue() const
{
return value;
}
};
private:
unsigned int uniformInterval;
std::pair< std::list<std::pair< const WBar*, unsigned int > >::iterator, unsigned int > mappingPointer;
/**
* Orderd sequence of WBar, which only account for specific values(the
* double value).
**/
std::map< double, WBar, cmp > elements;
/**
* The Mapping, to gain bar's with a range greater than 1, is stored as
* std::pair<WBar, unsigned int>.
* Where the unsigned int is the the height of the specific bar.
* The *WBar is the initial point (related to the std::map<> elements)
* of the next bar.
**/
std::list< std::pair< const WBar*, unsigned int > > mapping;
/**
* Creates the mapping from the given range.
**/
void calculateMapping(); // for uniformInterval only
public:
/**
* Creates empty histogram.
**/
WHistogram();
/**
* Create a histogram with a given uniform intervall.
**/
WHistogram( unsigned int );
/**
* Destroys this Histogram instance.
**/
~WHistogram();
/**
* Add an element to one of the WBar.
**/
void add( double );
/**
* Set uniform interval.
**/
void setUniformInterval( unsigned int );
/**
* Seting Intervals that are not uniform.
**/
void setInterval( const std::list<unsigned int>& );
/**
* Get the height of a bar.
**/
unsigned int operator[]( unsigned int );// const;
// To test the functionality
void test();
};
#endif // WHISTOGRAM_H
......@@ -46,14 +46,17 @@ WDataSetScalar::WDataSetScalar( boost::shared_ptr< WValueSetBase > newValueSet,
double max = wlimits::MIN_DOUBLE;
double min = wlimits::MAX_DOUBLE;
histogram.setUniformInterval(8);
for( size_t i = 0; i < newValueSet->size(); ++i )
{
double tmp = newValueSet->getScalarDouble( i );
max = max < tmp ? tmp : max;
min = min > tmp ? tmp : min;
histogram.add(tmp);
}
m_maximum = max;
m_minimum = min;
histogram.test();
}
WDataSetScalar::WDataSetScalar()
......
......@@ -25,6 +25,8 @@
#ifndef WDATASETSCALAR_H
#define WDATASETSCALAR_H
#include "../common/WHistogram.h"
#include "WDataSetSingle.h"
/**
......@@ -127,6 +129,11 @@ protected:
private:
double m_maximum; //!< Largest scalar of data set.
double m_minimum; //!< Smallest scalar of data set.
/**
* The histogram for later use.
**/
WHistogram histogram;
};
template< typename T > T WDataSetScalar::getValueAt( int x, int y, int z ) const
......
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