OpenWalnut  1.5.0dev
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, std::size_t buckets ):
36  WHistogram( min, max, buckets ),
37  m_bins( buckets, 0 ),
38  m_intervalWidth( std::abs( 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[]( std::size_t index ) const
55 {
56  return m_bins[ index ];
57 }
58 
59 size_t WHistogramBasic::at( std::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( std::size_t /* index */ ) const
70 {
71  return m_intervalWidth;
72 }
73 
74 std::pair< double, double > WHistogramBasic::getIntervalForIndex( std::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 std::size_t 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 0u - 1u;
88  }
89 
90  if( std::abs( m_minimum - m_maximum ) <= 2.0 * wlimits::DBL_EPS )
91  {
92  m_bins.at( m_nbBuckets - 1 )++;
93  return m_nbBuckets - 1;
94  }
95  else if( value >= ( m_maximum - m_intervalWidth ) && value <= m_maximum ) // last bin deserves extra treatment due to possbl out of bounds index
96  {
97  m_bins.at( m_nbBuckets - 1 )++;
98  return m_nbBuckets - 1;
99  }
100 
101  std::size_t bin = static_cast< std::size_t >( ( value - m_minimum ) / std::abs( m_maximum - m_minimum ) * ( m_nbBuckets ) );
102  m_bins.at( bin )++;
103  return bin;
104 }
105 
107 {
108  return std::accumulate( m_bins.begin(), m_bins.end(), 0 );
109 }
110 
Container which associate values with (uniform width) bins (aka intervals or buckets).
double m_intervalWidth
The width of an interval is precomputed to save performance.
virtual std::size_t insert(double value)
Inserts a given value within the given range (min, max) into exactly one bin and increment its size.
~WHistogramBasic()
Default destructor.
virtual std::pair< double, double > getIntervalForIndex(std::size_t index) const
Returns the actual interval associated with the given index.
std::vector< std::size_t > m_bins
Bins to associate with the values.
virtual std::size_t at(std::size_t index) const
Get the count of the specified bucket.
WHistogramBasic(double min, double max, std::size_t buckets=1000)
Default constructor.
virtual double getBucketSize(std::size_t index=0) const
Return the size of one specific bucket.
std::size_t valuesSize() const
Computes the number of inserted values so far.
virtual std::size_t operator[](std::size_t index) const
Get the count of the specified bucket.
Container which associate values with (uniform width) bins (aka intervals or buckets).
Definition: WHistogram.h:36
double m_minimum
The smallest value.
Definition: WHistogram.h:122
double m_nbBuckets
The number of buckets.
Definition: WHistogram.h:132
double m_maximum
The biggest value.
Definition: WHistogram.h:127
const double DBL_EPS
Smallest double such: 1.0 + DBL_EPS == 1.0 is still true.
Definition: WLimits.cpp:46
WStreamedLogger warn(const std::string &source)
Logging a warning message.
Definition: WLogger.h:309
WStreamedLogger error(const std::string &source)
Logging an error message.
Definition: WLogger.h:298