summary refs log tree commit diff stats
path: root/designer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'designer.cpp')
-rw-r--r--designer.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/designer.cpp b/designer.cpp new file mode 100644 index 0000000..024796f --- /dev/null +++ b/designer.cpp
@@ -0,0 +1,69 @@
1#include "designer.h"
2
3std::list<usage> designer::generate(std::mt19937& rng) const {
4 std::list<usage> result;
5 size_t cur = 0;
6
7 while (cur < text_.length()) {
8 const solution& curSol = get(cur);
9 const std::vector<size_t>& posLens = curSol.lengths;
10
11 std::uniform_int_distribution<size_t> lenDist(0, posLens.size() - 1);
12 size_t len = posLens.at(lenDist(rng));
13
14 const ps_type& prefix = curSol.prefix;
15 std::uniform_int_distribution<size_t> cardDist(0, prefix.getCount() - 1);
16 size_t cardIndex = cardDist(rng);
17 std::tuple<size_t, size_t> pd = prefix.at(cardIndex);
18
19 result.emplace_back(std::get<0>(pd), std::get<1>(pd), len);
20
21 cur += len;
22 }
23
24 return result;
25}
26
27solution designer::calculate(size_t i) const {
28 if (i == text_.length()) {
29 return {titles_, {}, 0};
30 }
31
32 const ps_type& prefix = titles_.find(text_, i);
33
34 bool foundScore = false;
35 size_t bestScore;
36 std::vector<size_t> bestLens;
37
38 for (int j = 1; (j <= prefix.getDepth()) && (i + j <= text_.length()); j++) {
39 const solution& subSol = get(i + j);
40
41 if (subSol.score > 0 || (i + j == text_.length())) {
42 size_t tempScore = subSol.score + 1;
43
44 if (!foundScore || tempScore < bestScore) {
45 foundScore = true;
46 bestScore = tempScore;
47
48 bestLens.clear();
49 bestLens.push_back(j);
50 } else if (tempScore == bestScore) {
51 bestLens.push_back(j);
52 }
53 }
54 }
55
56 if (!foundScore) {
57 return {titles_, {}, 0};
58 } else {
59 return {prefix, std::move(bestLens), bestScore};
60 }
61}
62
63const solution& designer::get(size_t i) const {
64 if (!solutions_.at(i)) {
65 solutions_[i] = std::make_unique<solution>(calculate(i));
66 }
67
68 return *solutions_.at(i);
69}