From a5a81425f4e656ecf0e867e114863b1b208e90b0 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 2 Apr 2016 21:51:57 +0200 Subject: Move `LineAccumulator` closure into `justify` project namespace --- justify.cc | 8 ++++---- src/line_accumulator.cc | 16 ++++++++++------ src/line_accumulator.h | 5 ++++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/justify.cc b/justify.cc index 870c815..73a3a3b 100644 --- a/justify.cc +++ b/justify.cc @@ -6,10 +6,10 @@ int main() { std::cout.sync_with_stdio(false); std::cin.sync_with_stdio(false); - LineAccumulator acc{60}; - std::string word; + justify::LineAccumulator acc{60}; + std::string token; - while ( std::cin >> word ) { - acc(word); + while ( std::cin >> token ) { + acc(token); } } diff --git a/src/line_accumulator.cc b/src/line_accumulator.cc index 95c2acd..0abe1a4 100644 --- a/src/line_accumulator.cc +++ b/src/line_accumulator.cc @@ -30,6 +30,8 @@ std::vector getRandomIndizes( } +namespace justify { + LineAccumulator::LineAccumulator(const std::uint8_t max_length): max_length_{max_length}, device_{}, @@ -45,13 +47,13 @@ std::uint8_t LineAccumulator::getMissing() const { return this->max_length_ - this->length_; } -void LineAccumulator::operator()(const std::string& word) { - if ( ( this->length_ + word.length() ) > this->max_length_ ) { +void LineAccumulator::operator()(const std::string& token) { + if ( ( this->length_ + token.length() ) > this->max_length_ ) { this->discharge(true); } - this->tokens_.emplace_back(word, 0); - this->length_ += word.length(); + this->tokens_.emplace_back(token, 0); + this->length_ += token.length(); if ( this->length_ < this->max_length_ ) { this->tokens_.back().second += 1; @@ -129,8 +131,8 @@ void LineAccumulator::justify() { } void LineAccumulator::discharge(const bool full) { - this->length_ -= this->tokens_.back().second; - this->tokens_.back().second = 0; + this->length_ -= this->tokens_.back().second; + this->tokens_.back().second = 0; if ( full ) { this->justify(); @@ -146,3 +148,5 @@ void LineAccumulator::discharge(const bool full) { this->length_ = 0; this->tokens_.clear(); } + +} diff --git a/src/line_accumulator.h b/src/line_accumulator.h index 373cb1b..cb07210 100644 --- a/src/line_accumulator.h +++ b/src/line_accumulator.h @@ -5,6 +5,8 @@ #include #include +namespace justify { + class LineAccumulator { public: LineAccumulator(const std::uint8_t max_length); @@ -12,7 +14,7 @@ class LineAccumulator { std::uint8_t getMissing() const; - void operator()(const std::string& word); + void operator()(const std::string& token); private: const std::uint8_t max_length_; @@ -30,3 +32,4 @@ class LineAccumulator { }; +} -- cgit v1.2.3