A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2018 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 <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
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(
90  size_t num_bins
91  );
92 
93  Histogram(
94  size_t num_bins,
95  double range_min,
96  double range_max
97  );
98 
99  Histogram(
100  const std::vector<double>& ranges
101  );
102 
103  ~Histogram() = default;
104 
105  Histogram(Histogram const&) = default;
106  Histogram(Histogram&&) = default;
107 
108  Histogram& operator= (Histogram const&) = default;
109  Histogram& operator= (Histogram&&) = default;
110 
111  friend void swap( Histogram& lhs, Histogram& rhs );
112 
113  // -------------------------------------------------------------------------
114  // General Methods
115  // -------------------------------------------------------------------------
116 
117  void set_ranges( const std::vector<double>& ranges );
118 
119  void set_uniform_ranges( const double min, const double max );
120 
121  void clear();
122 
124 
126 
127  // -------------------------------------------------------------------------
128  // Comparison
129  // -------------------------------------------------------------------------
130 
131  bool operator== ( Histogram const& rhs );
132 
133  friend bool equal_ranges (Histogram const& lhs, Histogram const& rhs);
134 
135  // -------------------------------------------------------------------------
136  // Bin Access
137  // -------------------------------------------------------------------------
138 
139  double& at( size_t bin_num );
140 
141  double at( size_t bin_num ) const;
142 
143  double& operator [] ( size_t bin_num );
144 
145  double operator [] ( size_t bin_num ) const;
146 
147  // -------------------------------------------------------------------------
148  // Bin Iterators
149  // -------------------------------------------------------------------------
150 
151  iterator begin();
152 
153  iterator end();
154 
155  const_iterator begin() const;
156 
157  const_iterator end() const;
158 
159  const_iterator cbegin() const;
160 
161  const_iterator cend() const;
162 
163  // -------------------------------------------------------------------------
164  // Properties
165  // -------------------------------------------------------------------------
166 
167  size_t bins() const;
168 
169  std::pair<double, double> bin_range( size_t bin_num ) const;
170 
171  double bin_midpoint( size_t bin_num ) const;
172 
173  double bin_width( size_t bin_num ) const;
174 
175  int find_bin( double x ) const;
176 
177  double range_min() const;
178 
179  double range_max() const;
180 
181  bool check_range( double x ) const;
182 
183  // -------------------------------------------------------------------------
184  // Modifiers
185  // -------------------------------------------------------------------------
186 
187  int increment( double x );
188 
189  int accumulate( double x, double weight );
190 
191  void increment_bin( size_t bin );
192 
193  void accumulate_bin( size_t bin, double weight );
194 
195  // -------------------------------------------------------------------------
196  // Data Members
197  // -------------------------------------------------------------------------
198 
199 private:
200 
201  std::vector<double> bins_;
202  std::vector<double> ranges_;
203 
204  OutOfRangeBehaviour out_of_range_behaviour_;
205 };
206 
207 } // namespace utils
208 } // namespace genesis
209 
210 #endif // include guard
size_t bins() const
Definition: histogram.cpp:244
std::vector< double >::iterator iterator
Definition: histogram.hpp:82
std::vector< double >::const_iterator const_iterator
Definition: histogram.hpp:83
void increment_bin(size_t bin)
Definition: histogram.cpp:346
double range_max() const
Definition: histogram.cpp:298
bool operator==(Histogram const &rhs)
Definition: histogram.cpp:171
friend bool equal_ranges(Histogram const &lhs, Histogram const &rhs)
Definition: histogram.cpp:55
Histogram class for accumulating and summarizing data.
Definition: histogram.hpp:68
int find_bin(double x) const
Definition: histogram.cpp:264
bool equal_ranges(Histogram const &lhs, Histogram const &rhs)
Definition: histogram.cpp:55
double bin_width(size_t bin_num) const
Definition: histogram.cpp:259
double & operator[](size_t bin_num)
Definition: histogram.cpp:196
double bin_midpoint(size_t bin_num) const
Definition: histogram.cpp:254
bool check_range(double x) const
Definition: histogram.cpp:303
void swap(NexusTaxa &lhs, NexusTaxa &rhs)
Definition: taxa.hpp:207
void accumulate_bin(size_t bin, double weight)
Definition: histogram.cpp:351
OutOfRangeBehaviour out_of_range_behaviour() const
Definition: histogram.cpp:157
int accumulate(double x, double weight)
Definition: histogram.cpp:317
std::pair< double, double > bin_range(size_t bin_num) const
Definition: histogram.cpp:249
double & at(size_t bin_num)
Definition: histogram.cpp:180
Histogram(size_t num_bins)
Definition: histogram.cpp:64
const_iterator cbegin() const
Definition: histogram.cpp:230
void set_uniform_ranges(const double min, const double max)
Definition: histogram.cpp:120
friend void swap(Histogram &lhs, Histogram &rhs)
Definition: histogram.cpp:47
double range_min() const
Definition: histogram.cpp:293
Histogram & operator=(Histogram const &)=default
void set_ranges(const std::vector< double > &ranges)
Definition: histogram.cpp:107
const_iterator cend() const
Definition: histogram.cpp:235