-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.cpp
More file actions
53 lines (47 loc) · 1.46 KB
/
histogram.cpp
File metadata and controls
53 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "histogram.h"
#include <algorithm>
#include <cassert>
#include <sstream>
HistogramCategory::HistogramCategory(const double min, const double max, const int cnt)
: m_min{min}, m_max{max}, m_cnt{cnt}
{
assert(m_min < m_max);
}
Histogram::Histogram(
const std::vector<double>& v,
const double lowest, const double highest, const int n
)
: m_v{}
{
//const int sz{static_cast<int>(v.size())};
//const int n{1+static_cast<int>(std::sqrt(static_cast<double>(sz)))};
//const double lowest{*std::min_element(std::begin(v),std::end(v))};
//const double highest{*std::max_element(std::begin(v),std::end(v))};
const double total_range{highest-lowest};
const double category_width{total_range/static_cast<double>(n)};
for (int i=0; i!=n; ++i)
{
const double min{lowest + (static_cast<double>(i + 0) * category_width)};
const double max{lowest + (static_cast<double>(i + 1) * category_width)};
const int n_values{
static_cast<int>(
std::count_if(std::begin(v),std::end(v),
[min,max](const double x) { return x >= min && x < max; }
)
)
};
m_v.push_back(HistogramCategory(min,max,n_values));
}
}
std::ostream& operator<<(std::ostream& os, const Histogram& h)
{
std::stringstream s;
for (const HistogramCategory& c: h.GetHistogram())
{
s << "[" << c.GetMin() << "," << c.GetMax() << "): " << c.GetCnt() << '\n';
}
std::string t{s.str()};
if (!t.empty()) t.pop_back();
os << t;
return os;
}