From ae7ea39966236965500faa96c42986bf35cc9af3 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 1 Apr 2016 21:31:28 +0200 Subject: Improve distribution of remaining spaces after base addition Removed random utility class as it was over-designed. --- src/line_accumulator.cc | 39 ++++++++++++++++++++++++++++++--------- src/line_accumulator.h | 11 +++++------ src/random.cc | 29 ----------------------------- src/random.h | 34 ---------------------------------- 4 files changed, 35 insertions(+), 78 deletions(-) delete mode 100644 src/random.cc delete mode 100644 src/random.h (limited to 'src') diff --git a/src/line_accumulator.cc b/src/line_accumulator.cc index b2facab..bc85e6c 100644 --- a/src/line_accumulator.cc +++ b/src/line_accumulator.cc @@ -3,9 +3,27 @@ #include #include -LineAccumulator::LineAccumulator(const std::size_t max_length): +namespace { + +std::vector getRandomIndizes( + std::random_device& device, + const std::uint8_t n) { + std::vector indizes(n); + std::iota(indizes.begin(), indizes.end(), 0); + std::shuffle( + indizes.begin(), + indizes.end(), + std::mt19937{device()} + ); + + return indizes; +} + +} + +LineAccumulator::LineAccumulator(const std::uint8_t max_length): max_length_{max_length}, - random_{}, + device_{}, length_{0}, tokens_{} { } @@ -32,8 +50,8 @@ void LineAccumulator::discharge(const bool full) { this->length_ -= this->tokens_.back().second; this->tokens_.back().second = 0; - const std::size_t missing = this->max_length_ - this->length_; - const std::size_t base = missing / (this->tokens_.size()-1); + const std::uint8_t missing = this->max_length_ - this->length_; + const std::uint8_t base = missing / (this->tokens_.size()-1); std::for_each( this->tokens_.begin(), @@ -44,12 +62,15 @@ void LineAccumulator::discharge(const bool full) { } ); - auto range = this->random_.makeRange(0, this->tokens_.size()-2); + auto indizes = getRandomIndizes(this->device_, this->tokens_.size()-1); - while ( this->length_ < this->max_length_ ) { - this->tokens_[range.get()].second += 1; - this->length_ += 1; - } + std::for_each( + indizes.begin(), + indizes.begin()+(this->max_length_ - this->length_), + [this](std::uint8_t x) { + this->tokens_[x].second += 1; + } + ); } for ( const auto& token : this->tokens_ ) { diff --git a/src/line_accumulator.h b/src/line_accumulator.h index b3e78de..93ebfc0 100644 --- a/src/line_accumulator.h +++ b/src/line_accumulator.h @@ -3,21 +3,20 @@ #include #include #include - -#include "random.h" +#include class LineAccumulator { public: - LineAccumulator(const std::size_t max_length); + LineAccumulator(const std::uint8_t max_length); ~LineAccumulator(); void operator()(const std::string& word); private: - const std::size_t max_length_; + const std::uint8_t max_length_; - utility::Random random_; - std::size_t length_; + std::random_device device_; + std::uint8_t length_; std::vector< std::pair diff --git a/src/random.cc b/src/random.cc deleted file mode 100644 index dba3647..0000000 --- a/src/random.cc +++ /dev/null @@ -1,29 +0,0 @@ -#include "random.h" - -namespace utility { - -std::size_t Random::Range::get() { - return this->distribution_(*(this->generator_)); -} - -Random::Range::Range( - std::mt19937* const generator, - const std::size_t a, - const std::size_t b -): - generator_(generator), - distribution_(a, b) { } - -Random::Random(): - device_{}, - generator_{device_()} { } - -Random::Range Random::makeRange( - const std::size_t a, - const std::size_t b) { - this->generator_.seed(this->device_()); - - return Range{&(this->generator_), a, b}; -} - -} diff --git a/src/random.h b/src/random.h deleted file mode 100644 index 73f3215..0000000 --- a/src/random.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include - -namespace utility { - -class Random { - public: - class Range { - public: - std::size_t get(); - - protected: - friend class Random; - - Range(std::mt19937* const, const std::size_t, const std::size_t); - - private: - std::mt19937* const generator_; - std::uniform_int_distribution distribution_; - }; - - Random(); - - Range makeRange(const std::size_t a, const std::size_t b); - - private: - std::random_device device_; - std::mt19937 generator_; - -}; - - -} -- cgit v1.2.3