Skip to content
Snippets Groups Projects
SubConfig.cpp 2.74 KiB
Newer Older
  • Learn to ignore specific revisions
  • SubConfig::SubConfig(BaseConfig & model) : Config(model.rawInput), model(model)
    
      wordIndex = model.wordIndex;
      characterIndex = model.characterIndex;
      state = model.state;
      history = model.history;
    
    bool SubConfig::needsUpdate()
    {
      return wordIndex-firstLineIndex >= 0.8*getNbLines();
    }
    
    
    Franck Dary's avatar
    Franck Dary committed
    bool SubConfig::update()
    
    {
      unsigned int currentLastLineIndex = firstLineIndex + getNbLines();
    
      if (currentLastLineIndex >= model.getNbLines()-1)
    
    Franck Dary's avatar
    Franck Dary committed
        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);
    
          *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 !");
      }
    
    
      if (getNbLines() > newLineNumber)
        resizeLines(newLineNumber);
    
    
      firstLineIndex = newFirstLineIndex;
    
    Franck Dary's avatar
    Franck Dary committed
      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);
    }
    
    
    bool SubConfig::hasColIndex(const std::string & colName) const
    {
    
    Franck Dary's avatar
    Franck Dary committed
      return model.hasColIndex(colName);
    
    const std::string & SubConfig::getColName(int colIndex) const
    {
      return model.getColName(colIndex);
    }
    
    std::size_t SubConfig::getFirstLineIndex() const
    {
      return firstLineIndex;
    }