Skip to content
Snippets Groups Projects
Config.cpp 4.93 KiB
Newer Older
Franck Dary's avatar
Franck Dary committed
#include "Config.hpp"
#include "util.hpp"

Config::Config(const Utf8String & rawInput) : rawInput(rawInput)
{
}

std::size_t Config::getIndexOfLine(int lineIndex) const
Franck Dary's avatar
Franck Dary committed
{
  return lineIndex * getNbColumns() * (nbHypothesesMax+1);
Franck Dary's avatar
Franck Dary committed
}
Franck Dary's avatar
Franck Dary committed

std::size_t Config::getIndexOfCol(int colIndex) const
Franck Dary's avatar
Franck Dary committed
{
  return colIndex * (nbHypothesesMax+1);
Franck Dary's avatar
Franck Dary committed
}

void Config::addLines(unsigned int nbLines)
Franck Dary's avatar
Franck Dary committed
{
  lines.resize(lines.size() + nbLines*getNbColumns()*(nbHypothesesMax+1));
Franck Dary's avatar
Franck Dary committed
}

void Config::resizeLines(unsigned int nbLines)
Franck Dary's avatar
Franck Dary committed
{
  lines.resize(nbLines*getNbColumns()*(nbHypothesesMax+1));
Franck Dary's avatar
Franck Dary committed
}

bool Config::has(int colIndex, int lineIndex, int hypothesisIndex) const
{
  return colIndex >= 0 && colIndex < (int)getNbColumns() && lineIndex >= (int)getFirstLineIndex() && lineIndex < (int)getFirstLineIndex() + (int)getNbLines() && hypothesisIndex >= 0 && hypothesisIndex < nbHypothesesMax+1;
}

bool Config::has(const std::string & colName, int lineIndex, int hypothesisIndex) const
{
  return hasColIndex(colName) && has(getColIndex(colName), lineIndex, hypothesisIndex);
}

Config::String & Config::get(const std::string & colName, int lineIndex, int hypothesisIndex)
Franck Dary's avatar
Franck Dary committed
{
  return get(getColIndex(colName), lineIndex, hypothesisIndex);
Franck Dary's avatar
Franck Dary committed
}

Franck Dary's avatar
Franck Dary committed
const Config::String & Config::getConst(const std::string & colName, int lineIndex, int hypothesisIndex) const
{
  return getConst(getColIndex(colName), lineIndex, hypothesisIndex);
}

Config::String & Config::get(int colIndex, int lineIndex, int hypothesisIndex)
  return *getIterator(colIndex, lineIndex, hypothesisIndex);
Franck Dary's avatar
Franck Dary committed
const Config::String & Config::getConst(int colIndex, int lineIndex, int hypothesisIndex) const
{
  return *getConstIterator(colIndex, lineIndex, hypothesisIndex);
}

std::size_t Config::getNbLines() const
  return lines.size() / getIndexOfCol(getNbColumns());
Franck Dary's avatar
Franck Dary committed
void Config::print(FILE * dest) const
  for (unsigned int line = 0; line < getNbLines(); line++)
  {
    for (unsigned int i = 0; i < getNbColumns()-1; i++)
Franck Dary's avatar
Franck Dary committed
      fmt::print(dest, "{}{}", getLastNotEmptyConst(i, getFirstLineIndex()+line), i < getNbColumns()-2 ? "\t" : "\n");
    if (getLastNotEmptyConst(EOSColName, getFirstLineIndex()+line) == EOSSymbol1)
void Config::printForDebug(FILE * dest) const
{
  static constexpr int windowSize = 5;
  int firstLineToPrint = wordIndex;
  int lastLineToPrint = wordIndex;
  while (wordIndex-firstLineToPrint < windowSize and has(0, firstLineToPrint, 0))
    --firstLineToPrint;
  while (lastLineToPrint - wordIndex < windowSize and has(0, lastLineToPrint, 0))
    ++lastLineToPrint;

  std::vector<std::vector<std::string>> toPrint;

  for (int line = firstLineToPrint; line <= lastLineToPrint; line++)
  {
    toPrint.emplace_back();
    toPrint.back().emplace_back(line == (int)wordIndex ? "=>" : "");
    for (unsigned int i = 0; i < getNbColumns(); i++)
      toPrint.back().emplace_back(getLastNotEmptyConst(i, line));
  }

  std::vector<std::size_t> colLength(toPrint[0].size(), 0);
  for (auto & line : toPrint)
    for (unsigned int col = 0; col < line.size()-1; col++)
      colLength[col] = std::max((int)colLength[col], util::printedLength(line[col]));

  for (auto & line : toPrint)
  {
    for (unsigned int col = 0; col < line.size()-1; col++)
      if (col == 0)
        fmt::print(dest, "{:>{}}", line[col], colLength[col]);
      else
        fmt::print(dest, "{:<{}}{}", line[col], colLength[col], col == line.size()-2 ? "\n" : "\t");
    if (line.back() == EOSSymbol1)
      fmt::print(dest, "\n");
  }
}

Config::String & Config::getLastNotEmpty(int colIndex, int lineIndex)
  int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex);
Franck Dary's avatar
Franck Dary committed

  for (int i = nbHypothesesMax; i > 0; --i)
    if (!util::isEmpty(lines[baseIndex+i]))
      return lines[baseIndex+i];

  return lines[baseIndex];
}

const Config::String & Config::getLastNotEmptyConst(int colIndex, int lineIndex) const
{
  int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex);

  for (int i = nbHypothesesMax; i > 0; --i)
    if (!util::isEmpty(lines[baseIndex+i]))
Config::String & Config::getLastNotEmpty(const std::string & colName, int lineIndex)
  return getLastNotEmpty(getColIndex(colName), lineIndex);
Franck Dary's avatar
Franck Dary committed
const Config::String & Config::getLastNotEmptyConst(const std::string & colName, int lineIndex) const
{
  return getLastNotEmptyConst(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;
void Config::addToHistory(const Config::String & transition)
{
  history.push_back(transition);
}