summary refs log tree commit diff stats
path: root/histogram.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'histogram.cpp')
-rw-r--r--histogram.cpp44
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
5template <class T>
6void histogram<T>::add(const T& inst)
7{
8 freqtable[inst]++;
9}
10
11template <class T>
12void 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
26template <class T>
27const 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
35template <class T>
36void histogram<T>::print() const
37{
38 for (auto& freqpair : freqtable)
39 {
40 std::cout << freqpair.first << ": " << freqpair.second << std::endl;
41 }
42}
43
44template class histogram <unsigned long>;