about 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.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..6896146 --- /dev/null +++ b/histogram.cpp
@@ -0,0 +1,34 @@
1#include "histogram.h"
2#include <cstdlib>
3
4template <class T>
5void histogram<T>::add(const T& inst)
6{
7 freqtable[inst]++;
8}
9
10template <class T>
11void histogram<T>::compile()
12{
13 distribution.clear();
14
15 int max = 0;
16 for (auto& it : freqtable)
17 {
18 max += it.second;
19 distribution.emplace(max, it.first);
20 }
21
22 freqtable.clear();
23}
24
25template <class T>
26const T& histogram<T>::next() const
27{
28 int max = distribution.rbegin()->first;
29 int r = rand() % max;
30
31 return distribution.upper_bound(r)->second;
32}
33
34template class histogram <std::string>;