aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-04-03 12:53:41 +0200
committerAdrian Kummerlaender2016-04-03 12:53:41 +0200
commit0f6f4c9db81ce100baa2502075dc611fa50aa116 (patch)
tree85933484775102fca53af98fe13902017eaa1e52
parent182ee7769f167f54b4952177d9226c4941e67275 (diff)
downloadjustify-0f6f4c9db81ce100baa2502075dc611fa50aa116.tar
justify-0f6f4c9db81ce100baa2502075dc611fa50aa116.tar.gz
justify-0f6f4c9db81ce100baa2502075dc611fa50aa116.tar.bz2
justify-0f6f4c9db81ce100baa2502075dc611fa50aa116.tar.lz
justify-0f6f4c9db81ce100baa2502075dc611fa50aa116.tar.xz
justify-0f6f4c9db81ce100baa2502075dc611fa50aa116.tar.zst
justify-0f6f4c9db81ce100baa2502075dc611fa50aa116.zip
Implement deterministic justification
i.e. the mersenne twister seed now depends on the data.
-rw-r--r--src/line_accumulator.cc26
-rw-r--r--src/line_accumulator.h4
2 files changed, 16 insertions, 14 deletions
diff --git a/src/line_accumulator.cc b/src/line_accumulator.cc
index 0abe1a4..9922f04 100644
--- a/src/line_accumulator.cc
+++ b/src/line_accumulator.cc
@@ -6,23 +6,29 @@
namespace {
+static std::mt19937 generator;
+
std::uint8_t getRandomIndex(
- std::mt19937& random,
+ const int seed,
const std::uint8_t n
) {
- return std::uniform_int_distribution<std::uint8_t>{0, n}(random);
+ generator.seed(seed);
+
+ return std::uniform_int_distribution<std::uint8_t>{0, n}(generator);
}
std::vector<std::uint8_t> getRandomIndizes(
- std::mt19937& random,
+ const int seed,
const std::uint8_t n
) {
+ generator.seed(seed);
+
std::vector<std::uint8_t> indizes(n);
std::iota(indizes.begin(), indizes.end(), 0);
std::shuffle(
indizes.begin(),
indizes.end(),
- random
+ generator
);
return indizes;
@@ -34,8 +40,6 @@ namespace justify {
LineAccumulator::LineAccumulator(const std::uint8_t max_length):
max_length_{max_length},
- device_{},
- random_{device_()},
length_{0},
tokens_{} { }
@@ -62,6 +66,9 @@ void LineAccumulator::operator()(const std::string& token) {
}
void LineAccumulator::justify() {
+ const int seed = this->tokens_.size()
+ + this->getMissing();
+
switch ( this->tokens_.size() ) {
// There is no sensible block justification of null or any single token
case 0:
@@ -105,16 +112,13 @@ void LineAccumulator::justify() {
// randomly distribute missing spaces
case 1: {
this->tokens_[
- getRandomIndex(this->random_, this->tokens_.size() - 2)
+ getRandomIndex(seed, this->tokens_.size() - 2)
].second += 1;
break;
}
default: {
- const auto indizes = getRandomIndizes(
- this->random_,
- this->tokens_.size() - 2
- );
+ const auto indizes = getRandomIndizes(seed, this->tokens_.size() - 2);
std::for_each(
indizes.begin(),
diff --git a/src/line_accumulator.h b/src/line_accumulator.h
index cb07210..2ca271c 100644
--- a/src/line_accumulator.h
+++ b/src/line_accumulator.h
@@ -19,9 +19,7 @@ class LineAccumulator {
private:
const std::uint8_t max_length_;
- std::random_device device_;
- std::mt19937 random_;
- std::uint8_t length_;
+ std::uint8_t length_;
std::vector<
std::pair<std::string, std::uint8_t>