A library for working with phylogenetic and population genetic data.
v0.27.0
histogram.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_MATH_HISTOGRAM_H_
2 #define GENESIS_UTILS_MATH_HISTOGRAM_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2021 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
34 #include <cstddef>
35 #include <vector>
36 
37 namespace genesis {
38 namespace utils {
39 
40 // =================================================================================================
41 // Forward Declarations
42 // =================================================================================================
43 
44 class Histogram;
45 bool equal_ranges( Histogram const& lhs, Histogram const& rhs );
46 void swap( Histogram& lhs, Histogram& rhs );
47 
48 // =================================================================================================
49 // Histogram
50 // =================================================================================================
51 
68 class Histogram
69 {
70  // -------------------------------------------------------------------------
71  // Typedefs and Enums
72  // -------------------------------------------------------------------------
73 
74 public:
75 
76  enum class OutOfRangeBehaviour {
77  kSqueeze,
78  kIgnore,
79  kThrow
80  };
81 
82  typedef std::vector<double>::iterator iterator;
83  typedef std::vector<double>::const_iterator const_iterator;
84 
85  // -------------------------------------------------------------------------
86  // Constructors and Rule of Five
87  // -------------------------------------------------------------------------
88 
89  Histogram() = default;
90 
91  explicit Histogram(
92  size_t num_bins
93  );
94 
95  Histogram(
96  size_t num_bins,
97  double range_min,
98  double range_max
99  );
100 
101  explicit Histogram(
102  const std::vector<double>& ranges
103  );
104 
105  ~Histogram() = default;
106 
107  Histogram(Histogram const&) = default;
108  Histogram(Histogram&&) = default;
109 
110  Histogram& operator= (Histogram const&) = default;
111  Histogram& operator= (Histogram&&) = default;
112 
113  friend void swap( Histogram& lhs, Histogram& rhs );
114 
115  // -------------------------------------------------------------------------
116  // General Methods
117  // -------------------------------------------------------------------------
118 
119  void set_ranges( const std::vector<double>& ranges );
120 
121  void set_uniform_ranges( const double min, const double max );
122 
123  void clear();
124 
126 
128 
129  // -------------------------------------------------------------------------
130  // Comparison
131  // -------------------------------------------------------------------------
132 
133  bool operator== ( Histogram const& rhs );
134 
135  friend bool equal_ranges (Histogram const& lhs, Histogram const& rhs);
136 
137  // -------------------------------------------------------------------------
138  // Bin Access
139  // -------------------------------------------------------------------------
140 
141  double& at( size_t bin_num );
142 
143  double at( size_t bin_num ) const;
144 
145  double& operator [] ( size_t bin_num );
146 
147  double operator [] ( size_t bin_num ) const;
148 
149  // -------------------------------------------------------------------------
150  // Bin Iterators
151  // -------------------------------------------------------------------------
152 
153  iterator begin();
154 
155  iterator end();
156 
157  const_iterator begin() const;
158 
159  const_iterator end() const;
160 
161  const_iterator cbegin() const;
162 
163  const_iterator cend() const;
164 
165  // -------------------------------------------------------------------------
166  // Properties
167  // -------------------------------------------------------------------------
168 
169  size_t bins() const;
170 
171  std::pair<double, double> bin_range( size_t bin_num ) const;
172 
173  double bin_midpoint( size_t bin_num ) const;
174 
175  double bin_width( size_t bin_num ) const;
176 
177  long find_bin( double x ) const;
178 
179  double range_min() const;
180 
181  double range_max() const;
182 
183  bool check_range( double x ) const;
184 
185  // -------------------------------------------------------------------------
186  // Modifiers
187  // -------------------------------------------------------------------------
188 
189  long increment( double x );
190 
191  long accumulate( double x, double weight );
192 
193  void increment_bin( size_t bin );
194 
195  void accumulate_bin( size_t bin, double weight );
196 
197  // -------------------------------------------------------------------------
198  // Data Members
199  // -------------------------------------------------------------------------
200 
201 private:
202 
203  std::vector<double> bins_;
204  std::vector<double> ranges_;
205 
206  OutOfRangeBehaviour out_of_range_behaviour_ = OutOfRangeBehaviour::kThrow;
207 };
208 
209 } // namespace utils
210 } // namespace genesis
211 
212 #endif // include guard
genesis::utils::Histogram::bin_midpoint
double bin_midpoint(size_t bin_num) const
Definition: histogram.cpp:254
genesis::utils::Histogram::cend
const_iterator cend() const
Definition: histogram.cpp:235
genesis::utils::Histogram::const_iterator
std::vector< double >::const_iterator const_iterator
Definition: histogram.hpp:83
genesis::utils::equal_ranges
bool equal_ranges(Histogram const &lhs, Histogram const &rhs)
Definition: histogram.cpp:55
genesis::utils::Histogram::out_of_range_behaviour
OutOfRangeBehaviour out_of_range_behaviour() const
Definition: histogram.cpp:157
genesis::utils::Histogram::increment_bin
void increment_bin(size_t bin)
Definition: histogram.cpp:349
genesis::utils::Histogram::check_range
bool check_range(double x) const
Definition: histogram.cpp:306
genesis::utils::Histogram::OutOfRangeBehaviour::kThrow
@ kThrow
genesis::utils::Histogram::range_max
double range_max() const
Definition: histogram.cpp:301
genesis::utils::Histogram::operator==
bool operator==(Histogram const &rhs)
Definition: histogram.cpp:171
genesis::utils::Histogram::end
iterator end()
Definition: histogram.cpp:215
genesis::utils::Histogram::operator[]
double & operator[](size_t bin_num)
Definition: histogram.cpp:196
genesis::utils::Histogram::equal_ranges
friend bool equal_ranges(Histogram const &lhs, Histogram const &rhs)
Definition: histogram.cpp:55
genesis::utils::Histogram::bin_width
double bin_width(size_t bin_num) const
Definition: histogram.cpp:259
genesis::utils::Histogram::OutOfRangeBehaviour::kSqueeze
@ kSqueeze
genesis::utils::Histogram::set_uniform_ranges
void set_uniform_ranges(const double min, const double max)
Definition: histogram.cpp:120
genesis::utils::Histogram::cbegin
const_iterator cbegin() const
Definition: histogram.cpp:230
genesis::utils::Histogram::OutOfRangeBehaviour
OutOfRangeBehaviour
Definition: histogram.hpp:76
genesis::utils::Histogram::accumulate_bin
void accumulate_bin(size_t bin, double weight)
Definition: histogram.cpp:354
genesis::utils::Histogram::bin_range
std::pair< double, double > bin_range(size_t bin_num) const
Definition: histogram.cpp:249
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::Histogram::bins
size_t bins() const
Definition: histogram.cpp:244
genesis::utils::Histogram::iterator
std::vector< double >::iterator iterator
Definition: histogram.hpp:82
genesis::utils::Histogram::begin
iterator begin()
Definition: histogram.cpp:210
genesis::utils::Histogram::OutOfRangeBehaviour::kIgnore
@ kIgnore
genesis::utils::Histogram::at
double & at(size_t bin_num)
Definition: histogram.cpp:180
genesis::utils::Histogram::find_bin
long find_bin(double x) const
Definition: histogram.cpp:264
genesis::utils::Histogram::Histogram
Histogram()=default
genesis::utils::Histogram::~Histogram
~Histogram()=default
genesis::utils::swap
void swap(Optional< T > &x, Optional< T > &y)
Definition: optional.hpp:566
genesis::utils::Histogram::range_min
double range_min() const
Definition: histogram.cpp:296
genesis::utils::Histogram::accumulate
long accumulate(double x, double weight)
Definition: histogram.cpp:320
genesis::utils::Histogram::clear
void clear()
Definition: histogram.cpp:150
genesis::utils::Histogram::increment
long increment(double x)
Definition: histogram.cpp:315
genesis::utils::Histogram::set_ranges
void set_ranges(const std::vector< double > &ranges)
Definition: histogram.cpp:107
genesis::utils::Histogram::operator=
Histogram & operator=(Histogram const &)=default
genesis::utils::Histogram::swap
friend void swap(Histogram &lhs, Histogram &rhs)
Definition: histogram.cpp:47
genesis::utils::Histogram
Histogram class for accumulating and summarizing data.
Definition: histogram.hpp:68