Newer
Older
#include <fmt/core.h>
Config::Config(const Utf8String & rawInput) : rawInput(rawInput)
{
}
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));
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)
return get(getColIndex(colName), lineIndex, hypothesisIndex);
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);
}
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());
}
{
for (unsigned int line = 0; line < getNbLines(); line++)
{
if (isComment(getFirstLineIndex()+line))
{
fmt::print(dest, "{}\n", getConst(0, getFirstLineIndex()+line, 0));
continue;
}
for (unsigned int i = 0; i < getNbColumns()-1; i++)
fmt::print(dest, "{}{}", getLastNotEmptyConst(i, getFirstLineIndex()+line), i < getNbColumns()-2 ? "\t" : "\n");
if (getLastNotEmptyConst(EOSColName, getFirstLineIndex()+line) == EOSSymbol1)
fmt::print(dest, "\n");
}
}
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-1, 0))
while (lastLineToPrint - wordIndex < windowSize and has(0, lastLineToPrint+1, 0))
++lastLineToPrint;
std::vector<std::vector<std::string>> toPrint;
toPrint.emplace_back();
toPrint.back().emplace_back("");
for (unsigned int i = 0; i < getNbColumns(); i++)
toPrint.back().emplace_back(getColName(i));
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]));
int lengthSum = 2*getNbColumns();
for (auto & val : colLength)
lengthSum += val;
std::string longLine = fmt::format("{:-<{}}", "", lengthSum);
std::string historyStr = "";
for (auto & h : history)
historyStr += h;
historyStr += ",";
}
if (!historyStr.empty())
historyStr.pop_back();
std::string stackStr = "";
for (auto & s : stack)
{
if (hasColIndex("ID"))
stackStr += getLastNotEmptyConst("ID", s);
else
stackStr += std::to_string(s);
stackStr += ",";
}
if (!stackStr.empty())
stackStr.pop_back();
fmt::print(dest, "{}\n", longLine);
for (std::size_t index = characterIndex; index < util::getSize(rawInput) and index - characterIndex < lettersWindowSize; index++)
fmt::print(dest, "{}", getLetter(index));
fmt::print(dest, "\n{}\n", longLine);
fmt::print(dest, "State={}\nwordIndex={} characterIndex={}\nhistory=({})\nstack=({})\n", state, wordIndex, characterIndex, historyStr, stackStr);
fmt::print(dest, "{}\n", longLine);
for (unsigned int line = 0; line < toPrint.size(); line++)
{
if (line == 1)
fmt::print(dest, "{}\n", longLine);
for (unsigned int col = 0; col < toPrint[line].size()-1; col++)
fmt::print(dest, "{}{:>{}}{}", toPrint[line][col], "", colLength[col]-util::printedLength(toPrint[line][col]), col == toPrint[line].size()-2 ? "\n" : "\t");
if (toPrint[line].back() == 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];
}
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]))
return lines[baseIndex+i];
return lines[baseIndex];
}
Config::String & Config::getLastNotEmpty(const std::string & colName, int lineIndex)
{
return getLastNotEmpty(getColIndex(colName), lineIndex);
}
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 std::string & transition)
{
history.push_back(String(transition));
}
void Config::addToStack(std::size_t index)
{
stack.push_back(index);
}
bool Config::hasLetter(int letterIndex) const
{
return letterIndex >= 0 and letterIndex < (int)util::getSize(rawInput);
}
util::utf8char Config::getLetter(int letterIndex) const
bool Config::isComment(std::size_t lineIndex) const
{
auto iter = getConstIterator(0, lineIndex, 0);
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
return !iter->get().empty() and iter->get()[0] == '#';
}
bool Config::isMultiword(std::size_t lineIndex) const
{
return hasColIndex("ID") && getConst("ID", lineIndex, 0).get().find('-') != std::string::npos;
}
bool Config::isEmptyNode(std::size_t lineIndex) const
{
return hasColIndex("ID") && getConst("ID", lineIndex, 0).get().find('.') != std::string::npos;
}
bool Config::isToken(std::size_t lineIndex) const
{
return !isComment(lineIndex) && !isMultiword(lineIndex) && !isEmptyNode(lineIndex);
}
bool Config::moveWordIndex(int relativeMovement)
{
int nbMovements = 0;
while (nbMovements != relativeMovement)
{
do
{
int oldVal = wordIndex;
relativeMovement > 0 ? wordIndex++ : wordIndex--;
if (!has(0,wordIndex,0))
{
wordIndex = oldVal;
return false;
}
}
while (!isToken(wordIndex));
nbMovements += relativeMovement > 0 ? 1 : -1;
}
return true;