aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-04-01 21:31:28 +0200
committerAdrian Kummerlaender2016-04-01 21:31:28 +0200
commitae7ea39966236965500faa96c42986bf35cc9af3 (patch)
tree0d81cbcc1062ef7ade18f3979aca0db82c407242
parent1d6e1df13b1b27bcf7144e03137b9a4f6acbcdcd (diff)
downloadjustify-ae7ea39966236965500faa96c42986bf35cc9af3.tar
justify-ae7ea39966236965500faa96c42986bf35cc9af3.tar.gz
justify-ae7ea39966236965500faa96c42986bf35cc9af3.tar.bz2
justify-ae7ea39966236965500faa96c42986bf35cc9af3.tar.lz
justify-ae7ea39966236965500faa96c42986bf35cc9af3.tar.xz
justify-ae7ea39966236965500faa96c42986bf35cc9af3.tar.zst
justify-ae7ea39966236965500faa96c42986bf35cc9af3.zip
Improve distribution of remaining spaces after base addition
Removed random utility class as it was over-designed.
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/line_accumulator.cc39
-rw-r--r--src/line_accumulator.h11
-rw-r--r--src/random.cc29
-rw-r--r--src/random.h34
5 files changed, 35 insertions, 79 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b24d21d..2a7b6a4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,5 +14,4 @@ add_executable(
justify
justify.cc
src/line_accumulator.cc
- src/random.cc
)
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 <iostream>
#include <algorithm>
-LineAccumulator::LineAccumulator(const std::size_t max_length):
+namespace {
+
+std::vector<std::uint8_t> getRandomIndizes(
+ std::random_device& device,
+ const std::uint8_t n) {
+ std::vector<std::uint8_t> 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 <tuple>
#include <string>
#include <vector>
-
-#include "random.h"
+#include <random>
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<std::string, std::uint8_t>
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 <random>
-
-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<std::size_t> distribution_;
- };
-
- Random();
-
- Range makeRange(const std::size_t a, const std::size_t b);
-
- private:
- std::random_device device_;
- std::mt19937 generator_;
-
-};
-
-
-}