diff --git a/reading_machine/include/Action.hpp b/reading_machine/include/Action.hpp new file mode 100644 index 0000000000000000000000000000000000000000..fa3ea7ab20484d4019c9271d770f86da9a64218c --- /dev/null +++ b/reading_machine/include/Action.hpp @@ -0,0 +1,43 @@ +#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 diff --git a/reading_machine/include/Config.hpp b/reading_machine/include/Config.hpp index c046f1f369147dd11119eea2427e333c87742057..ac7199367db62a054850a0a9561be65261349af2 100644 --- a/reading_machine/include/Config.hpp +++ b/reading_machine/include/Config.hpp @@ -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); diff --git a/reading_machine/include/Transition.hpp b/reading_machine/include/Transition.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b55dac043edfbc3100e8c3756bdc4e46ee854e4c --- /dev/null +++ b/reading_machine/include/Transition.hpp @@ -0,0 +1,12 @@ +#ifndef TRANSITION__H +#define TRANSITION__H + +#include <vector> + +class Transition +{ + private : + +}; + +#endif diff --git a/reading_machine/src/Action.cpp b/reading_machine/src/Action.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54fb1e647ef6d8f6a35e238fbfbee83074409372 --- /dev/null +++ b/reading_machine/src/Action.cpp @@ -0,0 +1,75 @@ +#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}; +} + diff --git a/reading_machine/src/Config.cpp b/reading_machine/src/Config.cpp index 9cde239802339abe61c8896ef5052dcf01eb932d..f89578b632b565fcb6a5c26492684fb2dae973f0 100644 --- a/reading_machine/src/Config.cpp +++ b/reading_machine/src/Config.cpp @@ -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)) { diff --git a/reading_machine/src/Transition.cpp b/reading_machine/src/Transition.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0819cf075fae460e71989515f89de716cdc0e04a --- /dev/null +++ b/reading_machine/src/Transition.cpp @@ -0,0 +1,3 @@ +#include "Transition.hpp" + +