From 76bc3b3677062c0c1a6b9fa08ff20d12e470159c Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 4 Nov 2024 02:19:09 -0500 Subject: Some old refactoring + some new refactoring --- designer.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 designer.cpp (limited to 'designer.cpp') diff --git a/designer.cpp b/designer.cpp new file mode 100644 index 0000000..024796f --- /dev/null +++ b/designer.cpp @@ -0,0 +1,69 @@ +#include "designer.h" + +std::list designer::generate(std::mt19937& rng) const { + std::list result; + size_t cur = 0; + + while (cur < text_.length()) { + const solution& curSol = get(cur); + const std::vector& posLens = curSol.lengths; + + std::uniform_int_distribution lenDist(0, posLens.size() - 1); + size_t len = posLens.at(lenDist(rng)); + + const ps_type& prefix = curSol.prefix; + std::uniform_int_distribution cardDist(0, prefix.getCount() - 1); + size_t cardIndex = cardDist(rng); + std::tuple pd = prefix.at(cardIndex); + + result.emplace_back(std::get<0>(pd), std::get<1>(pd), len); + + cur += len; + } + + return result; +} + +solution designer::calculate(size_t i) const { + if (i == text_.length()) { + return {titles_, {}, 0}; + } + + const ps_type& prefix = titles_.find(text_, i); + + bool foundScore = false; + size_t bestScore; + std::vector bestLens; + + for (int j = 1; (j <= prefix.getDepth()) && (i + j <= text_.length()); j++) { + const solution& subSol = get(i + j); + + if (subSol.score > 0 || (i + j == text_.length())) { + size_t tempScore = subSol.score + 1; + + if (!foundScore || tempScore < bestScore) { + foundScore = true; + bestScore = tempScore; + + bestLens.clear(); + bestLens.push_back(j); + } else if (tempScore == bestScore) { + bestLens.push_back(j); + } + } + } + + if (!foundScore) { + return {titles_, {}, 0}; + } else { + return {prefix, std::move(bestLens), bestScore}; + } +} + +const solution& designer::get(size_t i) const { + if (!solutions_.at(i)) { + solutions_[i] = std::make_unique(calculate(i)); + } + + return *solutions_.at(i); +} -- cgit 1.4.1