diff options
author | Adrian Kummerlaender | 2016-04-02 21:25:06 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2016-04-02 21:25:06 +0200 |
commit | 78b045924a0b589f599546de7d96b7154f76983e (patch) | |
tree | 9c8a51903b84e98003bcfe04d457464dd62cfc64 | |
parent | 1fbedc31a590116e1f49ed6420960f890a44c467 (diff) | |
download | justify-78b045924a0b589f599546de7d96b7154f76983e.tar justify-78b045924a0b589f599546de7d96b7154f76983e.tar.gz justify-78b045924a0b589f599546de7d96b7154f76983e.tar.bz2 justify-78b045924a0b589f599546de7d96b7154f76983e.tar.lz justify-78b045924a0b589f599546de7d96b7154f76983e.tar.xz justify-78b045924a0b589f599546de7d96b7154f76983e.tar.zst justify-78b045924a0b589f599546de7d96b7154f76983e.zip |
Improve algorithm support for corner cases
i.e. over-long lines and minimal token counts
-rw-r--r-- | src/line_accumulator.cc | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/src/line_accumulator.cc b/src/line_accumulator.cc index 8865851..36cc370 100644 --- a/src/line_accumulator.cc +++ b/src/line_accumulator.cc @@ -59,17 +59,35 @@ void LineAccumulator::operator()(const std::string& word) { } void LineAccumulator::justify() { - const std::uint8_t base = this->getMissing() - / (this->tokens_.size() - 1); - - std::for_each( - this->tokens_.begin(), - this->tokens_.end()-2, - [&, base](auto& token) { - token.second += base; - this->length_ += base; + switch ( this->tokens_.size() ) { + case 0: + case 1: { + this->length_ = this->max_length_; + + break; } - ); + case 2: { + this->tokens_[0].second += this->getMissing(); + this->length_ = this->max_length_; + + break; + } + default: { + const std::uint8_t base = this->getMissing() + / (this->tokens_.size() - 2); + + std::for_each( + this->tokens_.begin(), + this->tokens_.end() - 2, + [&, base](auto& token) { + token.second += base; + this->length_ += base; + } + ); + + break; + } + } switch ( this->getMissing() ) { case 0: { @@ -77,7 +95,7 @@ void LineAccumulator::justify() { } case 1: { this->tokens_[ - getRandomIndex(this->random_, this->tokens_.size() - 1) + getRandomIndex(this->random_, this->tokens_.size() - 2) ].second += 1; break; @@ -85,12 +103,12 @@ void LineAccumulator::justify() { default: { const auto indizes = getRandomIndizes( this->random_, - this->tokens_.size() - 1 + this->tokens_.size() - 2 ); std::for_each( indizes.begin(), - indizes.begin() + (this->max_length_ - this->length_), + indizes.begin() + this->getMissing(), [&](std::uint8_t x) { this->tokens_[x].second += 1; this->length_ += 1; |