aboutsummaryrefslogtreecommitdiff
path: root/src/line_accumulator.cc
blob: b2facabfd5b6800ec13f407a821828e233b6cbdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "line_accumulator.h"

#include <iostream>
#include <algorithm>

LineAccumulator::LineAccumulator(const std::size_t max_length):
	max_length_{max_length},
	random_{},
	length_{0},
	tokens_{} { }

LineAccumulator::~LineAccumulator() {
	this->discharge(false);
}

void LineAccumulator::operator()(const std::string& word) {
	if ( ( this->length_ + word.length() ) > this->max_length_ ) {
		this->discharge(true);
	}

	this->tokens_.emplace_back(word, 0);
	this->length_ += word.length();

	if ( this->length_ < this->max_length_ ) {
		this->tokens_.back().second += 1;
		this->length_               += 1;
	}
}

void LineAccumulator::discharge(const bool full) {
	if ( 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);

		std::for_each(
			this->tokens_.begin(),
			this->tokens_.end()-2,
			[base, this](auto& token) {
				token.second  += base;
				this->length_ += base;
			}
		);

		auto range = this->random_.makeRange(0, this->tokens_.size()-2);

		while ( this->length_ < this->max_length_ ) {
			this->tokens_[range.get()].second += 1;
			this->length_                     += 1;
		}
	}

	for ( const auto& token : this->tokens_ ) {
		std::cout << token.first
		          << std::string(token.second, ' ');
	}

	std::cout << '\n';

	this->length_ = 0;
	this->tokens_.clear();
}