diff options
author | Adrian Kummerlaender | 2016-03-29 20:47:27 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2016-03-29 20:47:27 +0200 |
commit | ce9d2ca31e07679292f0a0378792050756d6ab26 (patch) | |
tree | 58e7fc46588e90b0cf30ce367e8470f6eecbdccb /justify.cc | |
parent | 26101aeb8ea28bd180b30fc67cba81baadbba8ee (diff) | |
download | justify-ce9d2ca31e07679292f0a0378792050756d6ab26.tar justify-ce9d2ca31e07679292f0a0378792050756d6ab26.tar.gz justify-ce9d2ca31e07679292f0a0378792050756d6ab26.tar.bz2 justify-ce9d2ca31e07679292f0a0378792050756d6ab26.tar.lz justify-ce9d2ca31e07679292f0a0378792050756d6ab26.tar.xz justify-ce9d2ca31e07679292f0a0378792050756d6ab26.tar.zst justify-ce9d2ca31e07679292f0a0378792050756d6ab26.zip |
Move `LineAccumulator` into separate compilation unit
Diffstat (limited to 'justify.cc')
-rw-r--r-- | justify.cc | 78 |
1 files changed, 1 insertions, 77 deletions
@@ -1,82 +1,6 @@ #include <iostream> -#include <string> -#include <vector> -#include "random.h" - -class LineAccumulator { - public: - LineAccumulator(const std::size_t max_length): - max_length_{max_length}, - random_{}, - length_{0}, - tokens_{}, - spaces_{} { } - - ~LineAccumulator() { - this->discharge(false); - } - - void operator()(const std::string& word) { - if ( ( this->length_ + word.length() + 1 ) > this->max_length_ ) { - this->pop_trailing_token(); - this->discharge(true); - } - - this->add_token(word); - this->add_space(); - } - - private: - const std::size_t max_length_; - - utility::Random random_; - std::size_t length_; - std::vector<std::string> tokens_; - std::vector<std::size_t> spaces_; - - void add_token(const std::string& token) { - this->length_ += token.length(); - this->tokens_.emplace_back(token); - } - - void add_space() { - this->add_token(" "); - this->spaces_.emplace_back(this->tokens_.size() - 1); - } - - void increase_space_at(const std::size_t index) { - this->tokens_.at(this->spaces_[index]).append(" "); - ++this->length_; - } - - void pop_trailing_token() { - this->length_ -= this->tokens_.back().length(); - this->tokens_.pop_back(); - this->spaces_.pop_back(); - } - - void discharge(const bool full) { - if ( full ) { - auto range = this->random_.makeRange(0, this->spaces_.size()-1); - - while ( this->length_ < this->max_length_ ) { - this->increase_space_at(range.get()); - } - } - - for ( const auto& token : this->tokens_ ) { - std::cout << token; - } - - std::cout << '\n'; - - this->length_ = 0; - this->tokens_.clear(); - this->spaces_.clear(); - } - -}; +#include "line_accumulator.h" int main() { std::cout.sync_with_stdio(false); |