diff options
Diffstat (limited to 'histogram.cpp')
-rw-r--r-- | histogram.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..38fca45 --- /dev/null +++ b/histogram.cpp | |||
@@ -0,0 +1,44 @@ | |||
1 | #include "histogram.h" | ||
2 | #include <cstdlib> | ||
3 | #include <iostream> | ||
4 | |||
5 | template <class T> | ||
6 | void histogram<T>::add(const T& inst) | ||
7 | { | ||
8 | freqtable[inst]++; | ||
9 | } | ||
10 | |||
11 | template <class T> | ||
12 | void histogram<T>::compile() | ||
13 | { | ||
14 | distribution.clear(); | ||
15 | |||
16 | int max = 0; | ||
17 | for (auto& it : freqtable) | ||
18 | { | ||
19 | max += it.second; | ||
20 | distribution.emplace(max, it.first); | ||
21 | } | ||
22 | |||
23 | freqtable.clear(); | ||
24 | } | ||
25 | |||
26 | template <class T> | ||
27 | const T& histogram<T>::next() const | ||
28 | { | ||
29 | int max = distribution.rbegin()->first; | ||
30 | int r = rand() % max; | ||
31 | |||
32 | return distribution.upper_bound(r)->second; | ||
33 | } | ||
34 | |||
35 | template <class T> | ||
36 | void histogram<T>::print() const | ||
37 | { | ||
38 | for (auto& freqpair : freqtable) | ||
39 | { | ||
40 | std::cout << freqpair.first << ": " << freqpair.second << std::endl; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | template class histogram <unsigned long>; | ||