OpenWalnut  1.4.0
WHistogramBasic.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <algorithm>
00026 #include <iomanip>
00027 #include <numeric>
00028 #include <utility>
00029 
00030 #include "WAssert.h"
00031 #include "WHistogramBasic.h"
00032 #include "WLimits.h"
00033 #include "WLogger.h"
00034 
00035 WHistogramBasic::WHistogramBasic( double min, double max, size_t buckets ):
00036     WHistogram( min, max, buckets ),
00037     m_bins( buckets, 0 ),
00038     m_intervalWidth( std::abs( m_maximum - m_minimum ) / static_cast< double >( m_nbBuckets ) )
00039 {
00040 }
00041 
00042 WHistogramBasic::WHistogramBasic( const WHistogramBasic& hist ):
00043     WHistogram( hist ),
00044     m_bins( hist.m_bins ),
00045     m_intervalWidth( hist.m_intervalWidth )
00046 
00047 {
00048 }
00049 
00050 WHistogramBasic::~WHistogramBasic()
00051 {
00052 }
00053 
00054 size_t WHistogramBasic::operator[]( size_t index ) const
00055 {
00056     return m_bins[ index ];
00057 }
00058 
00059 size_t WHistogramBasic::at( size_t index ) const
00060 {
00061     if( index >= m_bins.size() )
00062     {
00063         wlog::error( "WHistogramBasic" ) << index << "th interval is not available, there are only: " << m_bins.size();
00064         return 0;
00065     }
00066     return m_bins[ index ];
00067 }
00068 
00069 double WHistogramBasic::getBucketSize( size_t /* index */ ) const
00070 {
00071     return m_intervalWidth;
00072 }
00073 
00074 std::pair< double, double > WHistogramBasic::getIntervalForIndex( size_t index ) const
00075 {
00076     double first = m_minimum + m_intervalWidth * index;
00077     double second =  m_minimum + m_intervalWidth * ( index + 1 );
00078     return std::make_pair( first, second );
00079 }
00080 
00081 void WHistogramBasic::insert( double value )
00082 {
00083     if( value > m_maximum || value < m_minimum )
00084     {
00085         wlog::warn( "WHistogramBasic" ) << std::scientific << std::setprecision( 16 ) << "Inserted value out of bounds, thread: "
00086                                         << value << " as min, resp. max: " << m_minimum << "," << m_maximum;
00087         return; // value = ( value > m_maximum ? m_maximum : m_minimum );
00088     }
00089 
00090     if( std::abs( m_minimum - m_maximum ) <= 2.0 * wlimits::DBL_EPS )
00091     {
00092         m_bins.at( m_nbBuckets - 1 )++;
00093     }
00094     else if( value >= ( m_maximum - m_intervalWidth ) && value <= m_maximum ) // last bin deserves extra treatment due to possbl out of bounds index
00095     {
00096         m_bins.at( m_nbBuckets - 1 )++;
00097     }
00098     else
00099     {
00100         m_bins.at( static_cast< size_t >( ( value - m_minimum ) / std::abs( m_maximum - m_minimum ) * ( m_nbBuckets ) ) )++;
00101     }
00102 }
00103 
00104 size_t WHistogramBasic::valuesSize() const
00105 {
00106     return std::accumulate( m_bins.begin(), m_bins.end(), 0 );
00107 }
00108