Skip to content
Snippets Groups Projects
SubConfig.cpp 2.46 KiB
#include "SubConfig.hpp"

SubConfig::SubConfig(BaseConfig & model) : model(model)
{
  wordIndex = model.wordIndex;
  characterIndex = model.characterIndex;
  state = model.state;
  history = model.history;
  update();
}

bool SubConfig::update()
{
  unsigned int currentLastLineIndex = firstLineIndex + getNbLines();

  if (currentLastLineIndex >= model.getNbLines()-1)
    return false;

  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++;

    firstToSave = model.getConstIterator(0, currentLastLineIndex, 0);
    lastToSave = model.getConstIterator(0, newLastLineIndex, 0);

    while (firstToSave != lastToSave)
      *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;
}

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;
}