From 2b152d09881559a0330b3ff923e03e715777c6c3 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 27 Feb 2019 20:45:17 -0500 Subject: Initial commit (by Pink!) --- histogram.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 histogram.cpp (limited to 'histogram.cpp') diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..38fca45 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,44 @@ +#include "histogram.h" +#include +#include + +template +void histogram::add(const T& inst) +{ + freqtable[inst]++; +} + +template +void histogram::compile() +{ + distribution.clear(); + + int max = 0; + for (auto& it : freqtable) + { + max += it.second; + distribution.emplace(max, it.first); + } + + freqtable.clear(); +} + +template +const T& histogram::next() const +{ + int max = distribution.rbegin()->first; + int r = rand() % max; + + return distribution.upper_bound(r)->second; +} + +template +void histogram::print() const +{ + for (auto& freqpair : freqtable) + { + std::cout << freqpair.first << ": " << freqpair.second << std::endl; + } +} + +template class histogram ; -- cgit 1.4.1