diff options
Diffstat (limited to 'histogram.cpp')
-rw-r--r-- | histogram.cpp | 34 |
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 | |||
4 | template <class T> | ||
5 | void histogram<T>::add(const T& inst) | ||
6 | { | ||
7 | freqtable[inst]++; | ||
8 | } | ||
9 | |||
10 | template <class T> | ||
11 | void 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 | |||
25 | template <class T> | ||
26 | const 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 | |||
34 | template class histogram <std::string>; | ||