From 9ba6b069d58dd9891ed9bc8d55f52d0c5a0e7b5f Mon Sep 17 00:00:00 2001 From: Franck Dary <franck.dary@lis-lab.fr> Date: Mon, 16 Dec 2019 13:45:38 +0100 Subject: [PATCH] Update of SubConfig now related to wordIndex --- dev/src/dev.cpp | 8 +++++ reading_machine/include/Config.hpp | 3 ++ reading_machine/include/SubConfig.hpp | 2 +- reading_machine/src/SubConfig.cpp | 45 ++++++++++++++++++--------- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/dev/src/dev.cpp b/dev/src/dev.cpp index 9eba067..3a07a91 100644 --- a/dev/src/dev.cpp +++ b/dev/src/dev.cpp @@ -16,6 +16,14 @@ int main(int argc, char * argv[]) for (int i = 0; i < 2; i++) configs.emplace_back(config); + configs[0].wordIndex = 2000; + + configs[0].update(); + configs[0].wordIndex = 0; + + configs[0].update(); + configs[0].update(); + configs[0].update(); configs[0].print(stdout); fmt::print(stderr, "ok\n"); diff --git a/reading_machine/include/Config.hpp b/reading_machine/include/Config.hpp index c4f7607..dae3e6c 100644 --- a/reading_machine/include/Config.hpp +++ b/reading_machine/include/Config.hpp @@ -25,6 +25,9 @@ class Config using ConstValueIterator = std::vector<String>::const_iterator; std::vector<String> lines; + + public : + std::size_t wordIndex{0}; std::size_t characterIndex{0}; String state{"NONE"}; diff --git a/reading_machine/include/SubConfig.hpp b/reading_machine/include/SubConfig.hpp index 18fd0d3..60faa69 100644 --- a/reading_machine/include/SubConfig.hpp +++ b/reading_machine/include/SubConfig.hpp @@ -13,7 +13,7 @@ class SubConfig : public Config private : const BaseConfig & model; - int firstLineIndex{0}; + std::size_t firstLineIndex{0}; private : diff --git a/reading_machine/src/SubConfig.cpp b/reading_machine/src/SubConfig.cpp index c93d145..eeb284a 100644 --- a/reading_machine/src/SubConfig.cpp +++ b/reading_machine/src/SubConfig.cpp @@ -2,6 +2,10 @@ SubConfig::SubConfig(BaseConfig & model) : model(model) { + wordIndex = model.wordIndex; + characterIndex = model.characterIndex; + state = model.state; + history = model.history; update(); } @@ -12,36 +16,47 @@ bool SubConfig::update() if (currentLastLineIndex >= model.getNbLines()-1) return false; - unsigned int newFirstLineIndex = firstLineIndex + 0.8*(currentLastLineIndex-firstLineIndex); - unsigned int newLastLineIndex = std::min(newFirstLineIndex + spanSize, model.getNbLines()); + std::size_t newFirstLineIndex = spanSize/2 >= wordIndex ? 0 : wordIndex - spanSize/2; + std::size_t newLastLineIndex = std::min(newFirstLineIndex + spanSize, model.getNbLines()); unsigned int newLineNumber = newLastLineIndex - newFirstLineIndex; if (getNbLines() < newLineNumber) resizeLines(newLineNumber); + // The two windows are disjoint, we must copy every value from model + if (currentLastLineIndex <= newFirstLineIndex or newLastLineIndex <= firstLineIndex) + { + auto linesBegin = getIterator(0, firstLineIndex, 0); + auto firstToSave = model.getConstIterator(0, newFirstLineIndex, 0); + auto lastToSave = model.getConstIterator(0, newLastLineIndex, 0); + + while (firstToSave != lastToSave) + *linesBegin++ = *firstToSave++; + } // The new window is a bit shifted to the write, we will keep the lasts elements from old window + else if (currentLastLineIndex > newFirstLineIndex and currentLastLineIndex < newLastLineIndex) { auto linesBegin = getIterator(0, firstLineIndex, 0); auto firstToSave = getConstIterator(0, newFirstLineIndex, 0); auto lastToSave = getConstIterator(0, currentLastLineIndex, 0); while (firstToSave != lastToSave) - (*linesBegin++) = (*firstToSave++); - } + *linesBegin++ = *firstToSave++; - if (getNbLines() > newLineNumber) - resizeLines(newLineNumber); - - { - unsigned int nbLinesCopied = currentLastLineIndex - newFirstLineIndex; - auto newlinesBegin = getIterator(0, firstLineIndex+nbLinesCopied, 0); - auto firstToSave = model.getConstIterator(0, currentLastLineIndex, 0); - auto lastToSave = model.getConstIterator(0, newLastLineIndex, 0); + firstToSave = model.getConstIterator(0, currentLastLineIndex, 0); + lastToSave = model.getConstIterator(0, newLastLineIndex, 0); while (firstToSave != lastToSave) - (*newlinesBegin++) = (*firstToSave++); - - firstLineIndex = newFirstLineIndex; + *linesBegin++ = *firstToSave++; + } // If the two windows are the same, do nothing. + else if (firstLineIndex == newFirstLineIndex and currentLastLineIndex == newLastLineIndex) + { } + else + { + util::myThrow("Update after a regression of wordIndex is not yet supported !"); + } + + firstLineIndex = newFirstLineIndex; return true; } -- GitLab