aboutsummaryrefslogtreecommitdiff
path: root/src/line_accumulator.cc
blob: 9922f04cdb61de2f372dd77808c9903add7cd0f4 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "line_accumulator.h"

#include <cassert>
#include <iostream>
#include <algorithm>

namespace {

static std::mt19937 generator;

std::uint8_t getRandomIndex(
	const int          seed,
	const std::uint8_t n
) {
	generator.seed(seed);

	return std::uniform_int_distribution<std::uint8_t>{0, n}(generator);
}

std::vector<std::uint8_t> getRandomIndizes(
	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(),
		generator
	);

	return indizes;
}

}

namespace justify {

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

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

std::uint8_t LineAccumulator::getMissing() const {
	return this->max_length_ - this->length_;
}

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

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

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

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:
		case 1: {
			this->length_ = this->max_length_;

			break;
		}
		// i.e. left align the first token and right align the second token
		case 2: {
			this->tokens_[0].second += this->getMissing();
			this->length_            = this->max_length_;

			break;
		}
		// most common branch for normal texts, add the maximum equal amount of
		// spaces to all but the last token
		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;
		}
	}

	assert(this->getMissing() < this->tokens_.size());

	switch ( this->getMissing() ) {
		case 0: {
			break;
		}
		// randomly distribute missing spaces
		case 1: {
			this->tokens_[
				getRandomIndex(seed, this->tokens_.size() - 2)
			].second += 1;

			break;
		}
		default: {
			const auto indizes = getRandomIndizes(seed, this->tokens_.size() - 2);

			std::for_each(
				indizes.begin(),
				indizes.begin() + this->getMissing(),
				[&](std::uint8_t x) {
					this->tokens_[x].second += 1;
					this->length_           += 1;
				}
			);

			break;
		}
	}
}

void LineAccumulator::discharge(const bool full) {
	this->length_               -= this->tokens_.back().second;
	this->tokens_.back().second  = 0;

	if ( full ) {
		this->justify();
	}

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

	std::cout << '\n';

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

}