#include <fmt/core.h> #include "Config.hpp" #include "util.hpp" std::size_t Config::getIndexOfLine(int lineIndex) const { return lineIndex * getNbColumns() * (nbHypothesesMax+1); } std::size_t Config::getIndexOfCol(int colIndex) const { return colIndex * (nbHypothesesMax+1); } void Config::addLines(unsigned int nbLines) { lines.resize(lines.size() + nbLines*getNbColumns()*(nbHypothesesMax+1)); } void Config::resizeLines(unsigned int nbLines) { lines.resize(nbLines*getNbColumns()*(nbHypothesesMax+1)); } Config::String & Config::get(const std::string & colName, int lineIndex, int hypothesisIndex) { return get(getColIndex(colName), lineIndex, hypothesisIndex); } Config::String & Config::get(int colIndex, int lineIndex, int hypothesisIndex) { return *getIterator(colIndex, lineIndex, hypothesisIndex); } std::size_t Config::getNbLines() const { return lines.size() / getIndexOfCol(getNbColumns()); } void Config::print(FILE * dest) { 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"); } } Config::String & Config::getLastNotEmpty(int colIndex, int lineIndex) { int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex); for (int i = nbHypothesesMax; i > 0; --i) if (!util::isEmpty(lines[baseIndex+i])) return lines[baseIndex+i]; return lines[baseIndex]; } Config::String & Config::getLastNotEmpty(const std::string & colName, int lineIndex) { return getLastNotEmpty(getColIndex(colName), lineIndex); } 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.begin() + getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex) + hypothesisIndex; }