Skip to content
Snippets Groups Projects
Commit 60e8b2f5 authored by Franck Dary's avatar Franck Dary
Browse files

Added classes Action and Transition

parent 6c03c4c3
No related branches found
No related tags found
No related merge requests found
#ifndef ACTION__H
#define ACTION__H
#include <functional>
#include <string>
#include <vector>
#include "Config.hpp"
class Action
{
public :
enum Type
{
Push,
Pop,
Write,
MoveWord,
MoveChar,
AddLines,
Check
};
private :
Type type;
std::function<void(Config & config, Action & action)> apply;
std::function<void(Config & config, Action & action)> undo;
std::function<bool(Config & config, Action & action)> appliable;
std::vector<std::string> data;
public :
Action(Type type, std::function<void(Config & config, Action & action)> apply, std::function<void(Config & config, Action & action)> undo, std::function<bool(Config & config, Action & action)> appliable);
public :
static Action addLinesIfNeeded(int nbLines);
static Action moveWordIndex(int movement);
static Action moveCharacterIndex(int movement);
};
#endif
......@@ -72,6 +72,8 @@ class Config
const String & getConst(const std::string & colName, int lineIndex, int hypothesisIndex) const;
String & getLastNotEmpty(const std::string & colName, int lineIndex);
const String & getLastNotEmptyConst(const std::string & colName, int lineIndex) const;
String & getFirstEmpty(int colIndex, int lineIndex);
String & getFirstEmpty(const std::string & colName, int lineIndex);
bool hasCharacter(int letterIndex) const;
util::utf8char getLetter(int letterIndex) const;
void addToHistory(const std::string & transition);
......
#ifndef TRANSITION__H
#define TRANSITION__H
#include <vector>
class Transition
{
private :
};
#endif
#include "Action.hpp"
Action::Action(Action::Type type, std::function<void(Config & config, Action & action)> apply, std::function<void(Config & config, Action & action)> undo, std::function<bool(Config & config, Action & action)> appliable)
{
this->type = type;
this->apply = apply;
this->undo = undo;
this->appliable = appliable;
}
Action Action::addLinesIfNeeded(int nbLines)
{
auto apply = [nbLines](Config & config, Action &)
{
config.addLines(1);
};
auto undo = [](Config &, Action &)
{
};
auto appliable = [](Config &, Action &)
{
return true;
};
return {Type::AddLines, apply, undo, appliable};
}
Action Action::moveWordIndex(int movement)
{
auto apply = [movement](Config & config, Action &)
{
config.moveWordIndex(movement);
};
auto undo = [movement](Config & config, Action &)
{
config.moveWordIndex(movement);
};
auto appliable = [movement](Config & config, Action &)
{
bool possible = config.moveWordIndex(movement);
if (possible)
moveWordIndex(-movement);
return possible;
};
return {Type::MoveWord, apply, undo, appliable};
}
Action Action::moveCharacterIndex(int movement)
{
auto apply = [movement](Config & config, Action &)
{
config.moveCharacterIndex(movement);
};
auto undo = [movement](Config & config, Action &)
{
config.moveCharacterIndex(movement);
};
auto appliable = [movement](Config & config, Action &)
{
bool possible = config.moveCharacterIndex(movement);
if (possible)
moveCharacterIndex(-movement);
return possible;
};
return {Type::MoveChar, apply, undo, appliable};
}
......@@ -166,6 +166,22 @@ Config::String & Config::getLastNotEmpty(int colIndex, int lineIndex)
return lines[baseIndex];
}
Config::String & Config::getFirstEmpty(int colIndex, int lineIndex)
{
int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex);
for (int i = 0; i <= nbHypothesesMax; ++i)
if (util::isEmpty(lines[baseIndex+i]))
return lines[baseIndex+i];
return lines[baseIndex+nbHypothesesMax];
}
Config::String & Config::getFirstEmpty(const std::string & colName, int lineIndex)
{
return getFirstEmpty(getColIndex(colName), lineIndex);
}
const Config::String & Config::getLastNotEmptyConst(int colIndex, int lineIndex) const
{
int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex);
......@@ -241,11 +257,11 @@ bool Config::isToken(std::size_t lineIndex) const
bool Config::moveWordIndex(int relativeMovement)
{
int nbMovements = 0;
int oldVal = wordIndex;
while (nbMovements != relativeMovement)
{
do
{
int oldVal = wordIndex;
relativeMovement > 0 ? wordIndex++ : wordIndex--;
if (!has(0,wordIndex,0))
{
......@@ -262,9 +278,9 @@ bool Config::moveWordIndex(int relativeMovement)
bool Config::moveCharacterIndex(int relativeMovement)
{
int oldVal = characterIndex;
for (int i = 0; i < relativeMovement; i++)
{
int oldVal = characterIndex;
relativeMovement > 0 ? characterIndex++ : characterIndex--;
if (!hasCharacter(characterIndex))
{
......
#include "Transition.hpp"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment