OGS
Histogram.h
Go to the documentation of this file.
1
13#pragma once
14
15#include <algorithm>
16#include <cstddef>
17#include <iosfwd>
18#include <iterator>
19#include <string>
20#include <utility>
21#include <vector>
22
23namespace BaseLib
24{
29template <typename T>
31{
32public:
33 using Data =
34 typename std::vector<double>;
35
36public:
47 template <typename InputIterator>
48 Histogram(InputIterator first, InputIterator last, const int nr_bins = 16,
49 const bool computeHistogram = true)
50 : data_(first, last), nr_bins_(nr_bins), dirty_(true)
51 {
52 init(computeHistogram);
53 }
54
61 explicit Histogram(std::vector<T> data, const unsigned int nr_bins = 16,
62 const bool computeHistogram = true)
63 : data_(std::move(data)), nr_bins_(nr_bins), dirty_(true)
64 {
65 init(computeHistogram);
66 }
67
78 void update()
79 {
80 if (!dirty_)
81 {
82 return;
83 }
84
86
87 auto it = data_.begin();
88 for (unsigned int bin = 0; bin < nr_bins_; bin++)
89 {
90 auto itEnd = std::upper_bound(it, data_.end(),
91 min_ + (bin + 1) * bin_width_);
92 histogram_[bin] = std::distance(it, itEnd);
93 it = itEnd;
94 }
95 dirty_ = false;
96 }
97
98 void setMinimum(const T& minimum)
99 {
100 min_ = minimum;
101 dirty_ = true;
102 }
103 void setMaximum(const T& maximum)
104 {
105 max_ = maximum;
106 dirty_ = true;
107 }
108
109 const Data& getSortedData() const { return data_; }
110 const std::vector<std::size_t>& getBinCounts() const { return histogram_; }
111 const unsigned int& getNumberOfBins() const { return nr_bins_; }
112 const T& getMinimum() const { return min_; }
113 const T& getMaximum() const { return max_; }
114 const T& getBinWidth() const { return bin_width_; }
115
116 void prettyPrint(std::ostream& os,
117 const unsigned int line_width = 16) const;
118
119 int write(std::string const& file_name, std::string const& data_set_name,
120 std::string const& param_name) const;
121
122protected:
125 void init(const bool computeHistogram = true)
126 {
127 std::sort(data_.begin(), data_.end());
128 histogram_.resize(nr_bins_);
129 min_ = data_.front();
130 max_ = data_.back();
132
133 dirty_ = true;
134 if (computeHistogram)
135 {
136 update();
137 }
138 }
139
141 const unsigned int nr_bins_;
142 std::vector<std::size_t> histogram_;
145
146private:
147 bool dirty_;
148};
149
155template <typename T>
156std::ostream& operator<<(std::ostream& os, const Histogram<T>& h);
157
158extern template class Histogram<double>;
159extern template std::ostream& operator<<(std::ostream& os,
160 const Histogram<double>& h);
161} // namespace BaseLib
const T & getBinWidth() const
Definition Histogram.h:114
const T & getMinimum() const
Definition Histogram.h:112
bool dirty_
When set update() will recompute histogram.
Definition Histogram.h:147
void setMaximum(const T &maximum)
Definition Histogram.h:103
T max_
Minimum and maximum input data values.
Definition Histogram.h:143
const Data & getSortedData() const
Definition Histogram.h:109
const unsigned int & getNumberOfBins() const
Definition Histogram.h:111
const std::vector< std::size_t > & getBinCounts() const
Definition Histogram.h:110
typename std::vector< double > Data
Definition Histogram.h:33
void init(const bool computeHistogram=true)
Definition Histogram.h:125
void prettyPrint(std::ostream &os, const unsigned int line_width=16) const
Definition Histogram.cpp:55
Histogram(std::vector< T > data, const unsigned int nr_bins=16, const bool computeHistogram=true)
Definition Histogram.h:61
const T & getMaximum() const
Definition Histogram.h:113
std::vector< std::size_t > histogram_
Definition Histogram.h:142
Histogram(InputIterator first, InputIterator last, const int nr_bins=16, const bool computeHistogram=true)
Underlying input data vector type.
Definition Histogram.h:48
void setMinimum(const T &minimum)
Definition Histogram.h:98
const unsigned int nr_bins_
Definition Histogram.h:141
int write(std::string const &file_name, std::string const &data_set_name, std::string const &param_name) const
Definition Histogram.cpp:22
std::ostream & operator<<(std::ostream &os, const Histogram< T > &h)
Definition Histogram.cpp:77