From caaca1a793c64d2ea551a84ce55ef2bcb15f56b8 Mon Sep 17 00:00:00 2001 From: Franck Dary <franck.dary@lis-lab.fr> Date: Sun, 17 May 2020 17:08:42 +0200 Subject: [PATCH] AddToHypothesis is now sorted --- common/include/util.hpp | 11 +++++++++++ reading_machine/src/Action.cpp | 23 ++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/common/include/util.hpp b/common/include/util.hpp index b257180..ac4ffdc 100644 --- a/common/include/util.hpp +++ b/common/include/util.hpp @@ -79,6 +79,17 @@ bool doIfNameMatch(const std::regex & reg, std::string_view name, const std::fun bool choiceWithProbability(float probability); +template <typename T> +std::string join(const std::string & delim, const std::vector<T> elems) +{ + std::string result; + + for (unsigned int i = 0; i < elems.size(); i++) + result = fmt::format("{}{}{}", result, elems[i], i == elems.size()-1 ? "" : delim); + + return result; +} + }; template <> diff --git a/reading_machine/src/Action.cpp b/reading_machine/src/Action.cpp index 7c496ee..92b44a3 100644 --- a/reading_machine/src/Action.cpp +++ b/reading_machine/src/Action.cpp @@ -147,18 +147,23 @@ Action Action::addToHypothesis(const std::string & colName, std::size_t lineInde { auto apply = [colName, lineIndex, addition](Config & config, Action &) { - auto & current = config.getLastNotEmptyHyp(colName, lineIndex); - current = util::isEmpty(current) ? addition : current.get() + '|' + addition; + auto currentElems = util::split(config.getLastNotEmptyHypConst(colName, lineIndex).get(), '|'); + currentElems.emplace_back(addition); + + std::sort(currentElems.begin(), currentElems.end()); + + config.getLastNotEmptyHyp(colName, lineIndex) = util::join("|", currentElems); }; - auto undo = [colName, lineIndex](Config & config, Action &) + auto undo = [colName, lineIndex, addition](Config & config, Action &) { - std::string newValue = config.getLastNotEmpty(colName, lineIndex); - while (!newValue.empty() and newValue.back() == '|') - newValue.pop_back(); - if (!newValue.empty()) - newValue.pop_back(); - config.getLastNotEmpty(colName, lineIndex) = newValue; + auto curElems = util::split(config.getLastNotEmptyHypConst(colName, lineIndex).get(), '|'); + std::vector<std::string> newElems; + for (auto & elem : curElems) + if (elem != addition) + newElems.emplace_back(elem); + + config.getLastNotEmptyHyp(colName, lineIndex) = util::join("|", newElems); }; auto appliable = [colName, lineIndex, addition](const Config & config, const Action &) -- GitLab