From edad461db539701b0eeb1407a54129de063b6d72 Mon Sep 17 00:00:00 2001 From: Franck Dary <franck.dary@lis-lab.fr> Date: Sat, 14 Dec 2019 18:23:39 +0100 Subject: [PATCH] Added optimized SubConfig class, but the update function is still not working --- CMakeLists.txt | 8 +- common/include/util.hpp | 15 +++ common/src/util.cpp | 10 ++ dev/src/dev.cpp | 12 +- reading_machine/include/BaseConfig.hpp | 54 ++++++++ reading_machine/include/Config.hpp | 61 ++++----- reading_machine/include/SubConfig.hpp | 31 +++++ reading_machine/src/BaseConfig.cpp | 140 +++++++++++++++++++++ reading_machine/src/Config.cpp | 163 ++++++------------------- reading_machine/src/SubConfig.cpp | 87 +++++++++++++ 10 files changed, 410 insertions(+), 171 deletions(-) create mode 100644 reading_machine/include/BaseConfig.hpp create mode 100644 reading_machine/include/SubConfig.hpp create mode 100644 reading_machine/src/BaseConfig.cpp create mode 100644 reading_machine/src/SubConfig.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 17dc5f0..e305461 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,14 +12,14 @@ add_library(Torch SHARED IMPORTED) set_target_properties(Torch PROPERTIES IMPORTED_LOCATION ${TORCH_LIBRARIES}) set(CMAKE_VERBOSE_MAKEFILE 0) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) if(NOT CMAKE_BUILD_TYPE) -# set(CMAKE_BUILD_TYPE Debug) - set(CMAKE_BUILD_TYPE Release) + set(CMAKE_BUILD_TYPE Debug) +# set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_CXX_FLAGS "-Wall -Wextra -std=c++17") +set(CMAKE_CXX_FLAGS "-Wall -Wextra") set(CMAKE_CXX_FLAGS_DEBUG "-g3 -Ofast") set(CMAKE_CXX_FLAGS_RELEASE "-Ofast") diff --git a/common/include/util.hpp b/common/include/util.hpp index 5f2d520..3351b45 100644 --- a/common/include/util.hpp +++ b/common/include/util.hpp @@ -20,6 +20,7 @@ #include <unordered_map> #include <fmt/core.h> #include <experimental/source_location> +#include <boost/flyweight.hpp> #include "utf8.hpp" namespace util @@ -38,6 +39,8 @@ void error(const std::exception & e, const std::experimental::source_location & void myThrow(std::string_view message, const std::experimental::source_location & location = std::experimental::source_location::current()); std::string int2HumanStr(int number); +bool isEmpty(const std::string & s); +bool isEmpty(const boost::flyweight<std::string> & s); }; @@ -66,6 +69,18 @@ struct fmt::formatter<util::utf8char> } }; +template <typename T> +struct fmt::formatter<boost::flyweight<T>> +{ + constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); } + + template <typename FormatContext> + auto format(const boost::flyweight<T> & s, FormatContext & ctx) + { + return format_to(ctx.out(), "{}", s.get()); + } +}; + std::string_view operator+(std::string_view a, std::string_view b); void operator+=(std::string_view & a, std::string_view b); diff --git a/common/src/util.cpp b/common/src/util.cpp index eed75ff..0c53e98 100644 --- a/common/src/util.cpp +++ b/common/src/util.cpp @@ -98,6 +98,16 @@ std::string int2HumanStr(int number) return result; } +bool isEmpty(const std::string & s) +{ + return s.empty(); +} + +bool isEmpty(const boost::flyweight<std::string> & s) +{ + return s.get().empty(); +} + }; std::string_view operator+(std::string_view a, std::string_view b) diff --git a/dev/src/dev.cpp b/dev/src/dev.cpp index 0aa2137..5ebd435 100644 --- a/dev/src/dev.cpp +++ b/dev/src/dev.cpp @@ -1,16 +1,22 @@ #include <cstdio> #include <fmt/core.h> #include "util.hpp" -#include "Config.hpp" +#include "BaseConfig.hpp" +#include "SubConfig.hpp" int main(int argc, char * argv[]) { if (argc != 4) util::error("3 arguments expected"); - Config config(argv[3], argv[1], argv[2]); + BaseConfig config(argv[3], argv[1], argv[2]); + + std::vector<SubConfig> configs; + + for (int i = 0; i < 1; i++) + configs.emplace_back(config); + - config.print(stdout); fmt::print(stderr, "ok\n"); std::scanf("%*c"); diff --git a/reading_machine/include/BaseConfig.hpp b/reading_machine/include/BaseConfig.hpp new file mode 100644 index 0000000..c38f926 --- /dev/null +++ b/reading_machine/include/BaseConfig.hpp @@ -0,0 +1,54 @@ +/*Copyright (c) 2019 Alexis Nasr && Franck Dary + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:i + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ +/// \file BaseConfig.hpp +/// \brief Configuration of the ReadingMachine. +/// \author Franck Dary +/// @version 2.0 +/// @date 2019-12-10 + +#ifndef BASECONFIG__H +#define BASECONFIG__H + +#include <string> +#include <vector> +#include <unordered_map> +#include <boost/flyweight.hpp> +#include "util.hpp" +#include "Config.hpp" + +class SubConfig; + +class BaseConfig : public Config +{ + private : + + std::vector<std::string> colIndex2Name; + std::unordered_map<std::string, int> colName2Index; + + std::string rawInput; + util::utf8string rawInputUtf8; + + private : + + void readMCD(std::string_view mcdFilename); + void readRawInput(std::string_view rawFilename); + void readTSVInput(std::string_view tsvFilename); + + std::size_t getNbColumns() const override; + std::size_t getFirstLineIndex() const override; + std::size_t getColIndex(const std::string & colName) const override; + const std::string & getColName(int colIndex) const override; + + public : + + BaseConfig(std::string_view mcdFilename, std::string_view tsvFilename, std::string_view rawFilename); + + friend SubConfig; +}; + +#endif diff --git a/reading_machine/include/Config.hpp b/reading_machine/include/Config.hpp index 7185c8e..a834496 100644 --- a/reading_machine/include/Config.hpp +++ b/reading_machine/include/Config.hpp @@ -1,64 +1,53 @@ -/*Copyright (c) 2019 Alexis Nasr && Franck Dary - - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:i - - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ -/// \file Config.hpp -/// \brief Configuration of the ReadingMachine. -/// \author Franck Dary -/// @version 2.0 -/// @date 2019-12-10 - #ifndef CONFIG__H #define CONFIG__H +#include <memory> #include <string> #include <vector> -#include <unordered_map> -#include "util.hpp" #include <boost/flyweight.hpp> -class Config; - class Config { - public : + protected : static constexpr const char * EOSColName = "EOS"; static constexpr const char * EOSSymbol1 = "1"; static constexpr const char * EOSSymbol0 = "0"; - static constexpr int nbHypothesesMax = 1; private : - std::vector<std::string> colIndex2Name; - std::unordered_map<std::string, int> colName2Index; + //using String = boost::flyweight<std::string>; + using String = std::string; + std::vector<String> lines; + using ValueIterator = std::vector<String>::iterator; + using ConstValueIterator = std::vector<String>::const_iterator; - std::string rawInput; - util::utf8string rawInputUtf8; + protected : - int nbColumns; - std::vector<boost::flyweight<std::string>> lines; + virtual std::size_t getNbColumns() const = 0; + virtual std::size_t getColIndex(const std::string & colName) const = 0; + virtual std::size_t getFirstLineIndex() const = 0; + virtual const std::string & getColName(int colIndex) const = 0; - private : + protected : - void readMCD(std::string_view mcdFilename); - void readRawInput(std::string_view rawFilename); - void readTSVInput(std::string_view tsvFilename); + std::size_t getIndexOfLine(int lineIndex) const; + std::size_t getIndexOfCol(int colIndex) const; + std::size_t getNbLines() const; + void addLines(unsigned int nbLines); + void resizeLines(unsigned int nbLines); + String & get(const std::string & colName, int lineIndex, int hypothesisIndex); + String & get(int colIndex, int lineIndex, int hypothesisIndex); + String & getLastNotEmpty(const std::string & colName, int lineIndex); + String & getLastNotEmpty(int colIndex, int lineIndex); + ValueIterator getIterator(int colIndex, int lineIndex, int hypothesisIndex); + ConstValueIterator getConstIterator(int colIndex, int lineIndex, int hypothesisIndex) const; public : - Config(std::string_view mcdFilename, std::string_view tsvFilename, std::string_view rawFilename); + virtual ~Config() {} void print(FILE * dest); - void addLine(); - boost::flyweight<std::string> & get(const std::string & colName, int lineIndex, int hypothesisIndex); - boost::flyweight<std::string> & get(int colIndex, int lineIndex, int hypothesisIndex); - boost::flyweight<std::string> & getLastNotEmpty(const std::string & colName, int lineIndex); - boost::flyweight<std::string> & getLastNotEmpty(int colIndex, int lineIndex); - std::size_t getNbLines() const; }; #endif diff --git a/reading_machine/include/SubConfig.hpp b/reading_machine/include/SubConfig.hpp new file mode 100644 index 0000000..daaa0cc --- /dev/null +++ b/reading_machine/include/SubConfig.hpp @@ -0,0 +1,31 @@ +#ifndef SUBCONFIG__H +#define SUBCONFIG__H + +#include "Config.hpp" +#include "BaseConfig.hpp" + +class SubConfig : public Config +{ + private : + + static constexpr std::size_t spanSize = 100; + + private : + + const BaseConfig & model; + int firstLineIndex; + + private : + + std::size_t getNbColumns() const override; + std::size_t getColIndex(const std::string & colName) const override; + const std::string & getColName(int colIndex) const override; + std::size_t getFirstLineIndex() const override; + + public : + + SubConfig(BaseConfig & model); + void update(); +}; + +#endif diff --git a/reading_machine/src/BaseConfig.cpp b/reading_machine/src/BaseConfig.cpp new file mode 100644 index 0000000..0a33c51 --- /dev/null +++ b/reading_machine/src/BaseConfig.cpp @@ -0,0 +1,140 @@ +/*Copyright (c) 2019 Alexis Nasr && Franck Dary + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:i + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ + +#include "BaseConfig.hpp" +#include "util.hpp" + +void BaseConfig::readMCD(std::string_view mcdFilename) +{ + if (!colIndex2Name.empty()) + util::myThrow("a mcd has already been read for this BaseConfig"); + + std::FILE * file = std::fopen(mcdFilename.data(), "r"); + + if (not file) + util::myThrow(fmt::format("Cannot open file '{}'", mcdFilename)); + + char lineBuffer[1024]; + while (std::fscanf(file, "%1023[^\n]\n", lineBuffer) == 1) + { + colIndex2Name.emplace_back(lineBuffer); + colName2Index.emplace(lineBuffer, colIndex2Name.size()-1); + } + + std::fclose(file); + + if (colName2Index.count(EOSColName)) + util::myThrow(fmt::format("mcd '{}' must not contain column '{}'", mcdFilename, EOSColName)); + colIndex2Name.emplace_back(EOSColName); + colName2Index.emplace(EOSColName, colIndex2Name.size()-1); +} + +void BaseConfig::readRawInput(std::string_view rawFilename) +{ + std::FILE * file = std::fopen(rawFilename.data(), "r"); + + if (not file) + util::myThrow(fmt::format("Cannot open file '{}'", rawFilename)); + + while (not std::feof(file)) + rawInput.push_back(std::fgetc(file)); + + std::fclose(file); + + rawInputUtf8 = util::splitAsUtf8(rawInput); +} + +void BaseConfig::readTSVInput(std::string_view tsvFilename) +{ + std::FILE * file = std::fopen(tsvFilename.data(), "r"); + + if (not file) + util::myThrow(fmt::format("Cannot open file '{}'", tsvFilename)); + + char lineBuffer[100000]; + int inputLineIndex = 0; + bool inputHasBeenRead = false; + int usualNbCol = -1; + while (!std::feof(file)) + { + if (lineBuffer != std::fgets(lineBuffer, 100000, file)) + break; + + std::string_view line(lineBuffer); + inputLineIndex++; + + if (line.size() < 3) + { + if (!inputHasBeenRead) + continue; + + get(EOSColName, getNbLines()-1, 0) = EOSSymbol1; + + continue; + } + else if (line[0] == '#') + continue; + + if (line.back() == '\n') + line.remove_suffix(1); + + inputHasBeenRead = true; + + auto splited = util::split(line, '\t'); + if (usualNbCol == -1) + usualNbCol = splited.size(); + if ((int)splited.size() != usualNbCol) + util::myThrow(fmt::format("in file {} line {} is invalid, it shoud have {} columns", tsvFilename, line, usualNbCol)); + + addLines(1); + get(EOSColName, getNbLines()-1, 0) = EOSSymbol0; + + for (unsigned int i = 0; i < splited.size(); i++) + if (i < colIndex2Name.size()) + get(i, getNbLines()-1, 0) = std::string(splited[i]); + } + + std::fclose(file); +} + +BaseConfig::BaseConfig(std::string_view mcdFilename, std::string_view tsvFilename, std::string_view rawFilename) +{ + if (tsvFilename.empty() and rawFilename.empty()) + util::myThrow("tsvFilename and rawFilenames can't be both empty"); + if (mcdFilename.empty()) + util::myThrow("mcdFilename can't be empty"); + + readMCD(mcdFilename); + + if (not rawFilename.empty()) + readRawInput(rawFilename); + + if (not tsvFilename.empty()) + readTSVInput(tsvFilename); +} + +std::size_t BaseConfig::getNbColumns() const +{ + return colIndex2Name.size(); +} + +std::size_t BaseConfig::getColIndex(const std::string & colName) const +{ + return colName2Index.at(colName); +} + +const std::string & BaseConfig::getColName(int colIndex) const +{ + return colIndex2Name[colIndex]; +} + +std::size_t BaseConfig::getFirstLineIndex() const +{ + return 0; +} + diff --git a/reading_machine/src/Config.cpp b/reading_machine/src/Config.cpp index 0f8fcb8..cddcdfe 100644 --- a/reading_machine/src/Config.cpp +++ b/reading_machine/src/Config.cpp @@ -1,168 +1,75 @@ -/*Copyright (c) 2019 Alexis Nasr && Franck Dary - - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:i - - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ - +#include <fmt/core.h> #include "Config.hpp" #include "util.hpp" -void Config::readMCD(std::string_view mcdFilename) +std::size_t Config::getIndexOfLine(int lineIndex) const { - if (!colIndex2Name.empty()) - util::myThrow("a mcd has already been read for this Config"); - - std::FILE * file = std::fopen(mcdFilename.data(), "r"); - - if (not file) - util::myThrow(fmt::format("Cannot open file '{}'", mcdFilename)); - - char lineBuffer[1024]; - while (std::fscanf(file, "%1023[^\n]\n", lineBuffer) == 1) - { - colIndex2Name.emplace_back(lineBuffer); - colName2Index.emplace(lineBuffer, colIndex2Name.size()-1); - } - - std::fclose(file); - - if (colName2Index.count(EOSColName)) - util::myThrow(fmt::format("mcd '{}' must not contain column '{}'", mcdFilename, EOSColName)); - colIndex2Name.emplace_back(EOSColName); - colName2Index.emplace(EOSColName, colIndex2Name.size()-1); - - nbColumns = colIndex2Name.size(); + return lineIndex * getNbColumns() * (nbHypothesesMax+1); } -void Config::readRawInput(std::string_view rawFilename) +std::size_t Config::getIndexOfCol(int colIndex) const { - std::FILE * file = std::fopen(rawFilename.data(), "r"); - - if (not file) - util::myThrow(fmt::format("Cannot open file '{}'", rawFilename)); - - while (not std::feof(file)) - rawInput.push_back(std::fgetc(file)); - - std::fclose(file); - - rawInputUtf8 = util::splitAsUtf8(rawInput); + return colIndex * (nbHypothesesMax+1); } -void Config::readTSVInput(std::string_view tsvFilename) +void Config::addLines(unsigned int nbLines) { - std::FILE * file = std::fopen(tsvFilename.data(), "r"); - - if (not file) - util::myThrow(fmt::format("Cannot open file '{}'", tsvFilename)); - - char lineBuffer[100000]; - int inputLineIndex = 0; - bool inputHasBeenRead = false; - int usualNbCol = -1; - while (!std::feof(file)) - { - if (lineBuffer != std::fgets(lineBuffer, 100000, file)) - break; - - std::string_view line(lineBuffer); - inputLineIndex++; - - if (line.size() < 3) - { - if (!inputHasBeenRead) - continue; - - get(EOSColName, getNbLines()-1, 0) = EOSSymbol1; - - continue; - } - else if (line[0] == '#') - continue; - - if (line.back() == '\n') - line.remove_suffix(1); - - inputHasBeenRead = true; - - auto splited = util::split(line, '\t'); - if (usualNbCol == -1) - usualNbCol = splited.size(); - if ((int)splited.size() != usualNbCol) - util::myThrow(fmt::format("in file {} line {} is invalid, it shoud have {} columns", tsvFilename, line, usualNbCol)); - - addLine(); - get(EOSColName, getNbLines()-1, 0) = EOSSymbol0; - - for (unsigned int i = 0; i < splited.size(); i++) - if (i < colIndex2Name.size()) - get(i, getNbLines()-1, 0) = std::string(splited[i]); - } - - std::fclose(file); + lines.resize(lines.size() + nbLines*getNbColumns()*(nbHypothesesMax+1)); } -Config::Config(std::string_view mcdFilename, std::string_view tsvFilename, std::string_view rawFilename) +void Config::resizeLines(unsigned int nbLines) { - if (tsvFilename.empty() and rawFilename.empty()) - util::myThrow("tsvFilename and rawFilenames can't be both empty"); - if (mcdFilename.empty()) - util::myThrow("mcdFilename can't be empty"); - - readMCD(mcdFilename); - - if (not rawFilename.empty()) - readRawInput(rawFilename); - - if (not tsvFilename.empty()) - readTSVInput(tsvFilename); + lines.resize(nbLines*getNbColumns()*(nbHypothesesMax+1)); } -void Config::print(FILE * dest) +Config::String & Config::get(const std::string & colName, int lineIndex, int hypothesisIndex) { - for (unsigned int line = 0; line < getNbLines(); line++) - { - for (int i = 0; i < nbColumns-1; i++) - fmt::print(dest, "{}{}", getLastNotEmpty(i, line).get(), i < nbColumns-2 ? "\t" : "\n"); - if (getLastNotEmpty(EOSColName, line) == EOSSymbol1) - fmt::print(dest, "\n"); - } + return get(getColIndex(colName), lineIndex, hypothesisIndex); } -void Config::addLine() +Config::String & Config::get(int colIndex, int lineIndex, int hypothesisIndex) { - lines.resize(lines.size() + nbColumns*(nbHypothesesMax+1)); + return *getIterator(colIndex, lineIndex, hypothesisIndex); } -boost::flyweight<std::string> & Config::get(const std::string & colName, int lineIndex, int hypothesisIndex) +std::size_t Config::getNbLines() const { - return get(colName2Index[colName], lineIndex, hypothesisIndex); + return lines.size() / getIndexOfCol(getNbColumns()); } -boost::flyweight<std::string> & Config::get(int colIndex, int lineIndex, int hypothesisIndex) +void Config::print(FILE * dest) { - return lines[lineIndex * nbColumns * (nbHypothesesMax+1) + colIndex * (nbHypothesesMax+1) + hypothesisIndex]; + for (unsigned int line = 0; line < getNbLines(); line++) + { + for (unsigned int i = 0; i < getNbColumns()-1; i++) + fmt::print(dest, "{}{}", getLastNotEmpty(i, line), i < getNbColumns()-2 ? "\t" : "\n"); + if (getLastNotEmpty(EOSColName, line) == EOSSymbol1) + fmt::print(dest, "\n"); + } } -boost::flyweight<std::string> & Config::getLastNotEmpty(int colIndex, int lineIndex) +Config::String & Config::getLastNotEmpty(int colIndex, int lineIndex) { - int baseIndex = lineIndex * nbColumns * (nbHypothesesMax+1) + colIndex * (nbHypothesesMax+1); + int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex); for (int i = nbHypothesesMax; i > 0; --i) - if (!lines[baseIndex+i].get().empty()) + if (!util::isEmpty(lines[baseIndex+i])) return lines[baseIndex+i]; return lines[baseIndex]; } -boost::flyweight<std::string> & Config::getLastNotEmpty(const std::string & colName, int lineIndex) +Config::String & Config::getLastNotEmpty(const std::string & colName, int lineIndex) { - return getLastNotEmpty(colName2Index[colName], lineIndex); + return getLastNotEmpty(getColIndex(colName), lineIndex); } -std::size_t Config::getNbLines() const +Config::ValueIterator Config::getIterator(int colIndex, int lineIndex, int hypothesisIndex) +{ + return lines.begin() + getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex) + hypothesisIndex; +} + +Config::ConstValueIterator Config::getConstIterator(int colIndex, int lineIndex, int hypothesisIndex) const { - return lines.size() / (nbColumns * (nbHypothesesMax+1)); + return lines.begin() + getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex) + hypothesisIndex; } diff --git a/reading_machine/src/SubConfig.cpp b/reading_machine/src/SubConfig.cpp new file mode 100644 index 0000000..75c6de6 --- /dev/null +++ b/reading_machine/src/SubConfig.cpp @@ -0,0 +1,87 @@ +#include "SubConfig.hpp" + +SubConfig::SubConfig(BaseConfig & model) : model(model) +{ + firstLineIndex = 0; + update(); + update(); + print(stdout); +} + +void SubConfig::update() +{ + fmt::print(stderr, "Begin\n"); + unsigned int currentLastLineIndex = firstLineIndex + getNbLines(); + + if (currentLastLineIndex >= model.getNbLines()-1) + return; + + unsigned int newFirstLineIndex = 0.8*currentLastLineIndex; + unsigned int newLastLineIndex = std::min(newFirstLineIndex + spanSize, model.getNbLines()); + unsigned int newLineNumber = newLastLineIndex - newFirstLineIndex; + + fmt::print(stderr, "FirstlineIndex = {}\n", firstLineIndex); + fmt::print(stderr, "newFirstlineIndex = {}\n", newFirstLineIndex); + + if (getNbLines() < newLineNumber) + { + fmt::print(stderr, "Resizing because {} < {}\n", getNbLines(), newLineNumber); + resizeLines(newLineNumber); + } + + { + auto linesBegin = getIterator(0, firstLineIndex, 0); + auto firstToSave = getConstIterator(0, newFirstLineIndex, 0); + auto lastToSave = getConstIterator(0, currentLastLineIndex, 0); + + fmt::print(stderr, "Copying from {} to {} into {}\n", newFirstLineIndex, currentLastLineIndex, firstLineIndex); + + while (firstToSave != lastToSave) + (*linesBegin++) = (*firstToSave++); + } + + if (getNbLines() > newLineNumber) + { + fmt::print(stderr, "Resizing because {} > {}\n", 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); + + fmt::print(stderr, "Copying from {} to {} into {}\n", currentLastLineIndex, newLastLineIndex, firstLineIndex+nbLinesCopied); + if (newlinesBegin < getIterator(0, 0, 0)) + fmt::print(stderr, "Bug\n"); + + while (firstToSave != lastToSave) + (*newlinesBegin++) = (*firstToSave++); + + firstLineIndex = newFirstLineIndex; + } + + fmt::print(stderr, "End\n"); +} + +std::size_t SubConfig::getNbColumns() const +{ + return model.getNbColumns(); +} + +std::size_t SubConfig::getColIndex(const std::string & colName) const +{ + return model.getColIndex(colName); +} + +const std::string & SubConfig::getColName(int colIndex) const +{ + return model.getColName(colIndex); +} + +std::size_t SubConfig::getFirstLineIndex() const +{ + return firstLineIndex; +} + -- GitLab