OpenWalnut  1.3.1
WHistogramBasic.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 #include <iomanip>
27 #include <numeric>
28 #include <utility>
29 
30 #include "WAssert.h"
31 #include "WHistogramBasic.h"
32 #include "WLimits.h"
33 #include "WLogger.h"
34 
35 WHistogramBasic::WHistogramBasic( double min, double max, size_t buckets ):
36  WHistogram( min, max, buckets ),
37  m_bins( buckets, 0 ),
38  m_intervalWidth( ( m_maximum - m_minimum ) / static_cast< double >( m_nbBuckets ) )
39 {
40 }
41 
43  WHistogram( hist ),
44  m_bins( hist.m_bins ),
45  m_intervalWidth( hist.m_intervalWidth )
46 
47 {
48 }
49 
51 {
52 }
53 
54 size_t WHistogramBasic::operator[]( size_t index ) const
55 {
56  return m_bins[ index ];
57 }
58 
59 size_t WHistogramBasic::at( size_t index ) const
60 {
61  if( index >= m_bins.size() )
62  {
63  wlog::error( "WHistogramBasic" ) << index << "th interval is not available, there are only: " << m_bins.size();
64  return 0;
65  }
66  return m_bins[ index ];
67 }
68 
69 double WHistogramBasic::getBucketSize( size_t /* index */ ) const
70 {
71  return m_intervalWidth;
72 }
73 
74 std::pair< double, double > WHistogramBasic::getIntervalForIndex( size_t index ) const
75 {
76  double first = m_minimum + m_intervalWidth * index;
77  double second = m_minimum + m_intervalWidth * ( index + 1 );
78  return std::make_pair( first, second );
79 }
80 
81 void WHistogramBasic::insert( double value )
82 {
83  if( value > m_maximum || value < m_minimum )
84  {
85  wlog::warn( "WHistogramBasic" ) << std::scientific << std::setprecision( 16 ) << "Inserted value out of bounds, thread: "
86  << value << " as min, resp. max: " << m_minimum << "," << m_maximum;
87  return; // value = ( value > m_maximum ? m_maximum : m_minimum );
88  }
89 
90  if( std::abs( m_minimum - m_maximum ) <= 2.0 * wlimits::DBL_EPS )
91  {
92  m_bins.at( m_nbBuckets - 1 )++;
93  }
94  else
95  {
96  m_bins.at( static_cast< size_t >( ( value - m_minimum ) / std::abs( m_maximum - m_minimum ) * ( m_nbBuckets - 1 ) ) )++;
97  }
98 }
99 
101 {
102  return std::accumulate( m_bins.begin(), m_bins.end(), 0 );
103 }
104