From 01fcbeb60da0bff33d5d9f5b870d444cc418a01d Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 28 Feb 2019 20:12:45 -0500 Subject: Converted to C++ style randomization The logic in rawr::randomSentence with the cuts might be slightly different now but who even knows what's going on there. --- histogram.h | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'histogram.h') diff --git a/histogram.h b/histogram.h index 76d8f1b..c7e051b 100644 --- a/histogram.h +++ b/histogram.h @@ -3,18 +3,53 @@ #include #include +#include +#include template class histogram { - public: - void add(const T& inst); - void compile(); - const T& next() const; - void print() const; - - private: - std::map freqtable; - std::map distribution; +public: + + void add(const T& inst) + { + freqtable_[inst]++; + } + + void compile() + { + distribution_.clear(); + + int max = 0; + for (auto& it : freqtable_) + { + max += it.second; + distribution_.emplace(max, it.first); + } + + freqtable_.clear(); + } + + const T& next(std::mt19937& rng) const + { + int max = distribution_.rbegin()->first; + std::uniform_int_distribution randDist(0, max - 1); + int r = randDist(rng); + + return distribution_.upper_bound(r)->second; + } + + void print() const + { + for (auto& freqpair : freqtable_) + { + std::cout << freqpair.first << ": " << freqpair.second << std::endl; + } + } + +private: + + std::map freqtable_; + std::map distribution_; }; #endif /* end of include guard: HISTOGRAM_H_24094D97 */ -- cgit 1.4.1