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

Fixed a bug related do undoing actions

parent fe396e89
Branches
No related tags found
No related merge requests found
...@@ -13,16 +13,18 @@ class LimitedArray ...@@ -13,16 +13,18 @@ class LimitedArray
{ {
private : private :
std::vector<T> data; std::vector< std::pair<T,bool> > data;
int nbElements; int nbElements;
int lastElementDataIndex; int lastElementDataIndex;
int lastElementRealIndex; int lastElementRealIndex;
T mask;
public : public :
LimitedArray(unsigned int limit) : data(limit) LimitedArray(unsigned int limit, const T & mask) : data(limit)
{ {
clear(); clear();
this->mask = mask;
} }
void clear() void clear()
...@@ -44,17 +46,21 @@ class LimitedArray ...@@ -44,17 +46,21 @@ class LimitedArray
lastElementRealIndex++; lastElementRealIndex++;
data[lastElementDataIndex] = elem; data[lastElementDataIndex].first = elem;
} }
const T & get(unsigned int index) const const T & get(unsigned int index) const
{ {
return data[index % data.size()]; if (data[index % data.size()].second)
return mask;
return data[index % data.size()].first;
} }
void set(unsigned int index, const T & value) void set(unsigned int index, const T & value)
{ {
data[index % data.size()] = value; data[index % data.size()].first = value;
data[index % data.size()].second = false;
} }
int getLastIndex() const int getLastIndex() const
...@@ -70,7 +76,7 @@ class LimitedArray ...@@ -70,7 +76,7 @@ class LimitedArray
void printForDebug(FILE * out) const void printForDebug(FILE * out) const
{ {
for (int i = 0; i < std::min(nbElements,(int)data.size()); i++) for (int i = 0; i < std::min(nbElements,(int)data.size()); i++)
fprintf(out, "<%s>", data[i].c_str()); fprintf(out, "<%s>", data[i].first.c_str());
fprintf(out, "\n"); fprintf(out, "\n");
} }
......
...@@ -401,6 +401,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na ...@@ -401,6 +401,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
} }
auto deps = split(ba.data, '+'); auto deps = split(ba.data, '+');
for (auto s : deps) for (auto s : deps)
if (!s.empty())
simpleBufferWrite(c, "GOV", "", std::stoi(s)); simpleBufferWrite(c, "GOV", "", std::stoi(s));
ba.data.clear(); ba.data.clear();
}; };
...@@ -453,7 +454,9 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na ...@@ -453,7 +454,9 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
} }
auto deps = split(ba.data, '+'); auto deps = split(ba.data, '+');
for (auto & dep : deps) for (auto & dep : deps)
if (!dep.empty())
simpleBufferWrite(c, "LABEL", "", std::stoi(dep)); simpleBufferWrite(c, "LABEL", "", std::stoi(dep));
ba.data.clear();
}; };
auto appliable2 = [](Config & c, Action::BasicAction &) auto appliable2 = [](Config & c, Action::BasicAction &)
{ {
...@@ -480,7 +483,9 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na ...@@ -480,7 +483,9 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
{ {
auto elems = split(ba.data); auto elems = split(ba.data);
for (auto elem : elems) for (auto elem : elems)
if (!elem.empty())
c.stackPush(std::stoi(elem)); c.stackPush(std::stoi(elem));
ba.data.clear();
}; };
auto appliable4 = [](Config & c, Action::BasicAction &) auto appliable4 = [](Config & c, Action::BasicAction &)
{ {
...@@ -530,6 +535,11 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na ...@@ -530,6 +535,11 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
while (true) while (true)
{ {
if (c.pastActions.empty())
{
fprintf(stderr, "ERROR (%s) : trying to undo action while pastActions is empty. Aborting.\n", ERRINFO);
exit(1);
}
auto a = c.pastActions.pop(); auto a = c.pastActions.pop();
if (ProgramParameters::debug) if (ProgramParameters::debug)
fprintf(stderr, "Undoing... <%s><%s>\n", a.first.c_str(), a.second.name.c_str()); fprintf(stderr, "Undoing... <%s><%s>\n", a.first.c_str(), a.second.name.c_str());
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "Action.hpp" #include "Action.hpp"
#include "ProgramOutput.hpp" #include "ProgramOutput.hpp"
Config::Config(BD & bd, const std::string inputFilename) : bd(bd), hashHistory(30), pastActions(100) Config::Config(BD & bd, const std::string inputFilename) : bd(bd), hashHistory(90), pastActions(100)
{ {
this->outputFile = nullptr; this->outputFile = nullptr;
this->stackHistory = -1; this->stackHistory = -1;
...@@ -36,7 +36,7 @@ Config::Config(const Config & other) : bd(other.bd), hashHistory(other.hashHisto ...@@ -36,7 +36,7 @@ Config::Config(const Config & other) : bd(other.bd), hashHistory(other.hashHisto
this->file.reset(new File(*other.file.get())); this->file.reset(new File(*other.file.get()));
} }
Config::Tape::Tape(const std::string & name, bool isKnown) : ref(ProgramParameters::readSize*4+1), hyp(ProgramParameters::readSize*4+1) Config::Tape::Tape(const std::string & name, bool isKnown) : ref(ProgramParameters::readSize*4+1, Dict::unknownValueStr), hyp(ProgramParameters::readSize*4+1, std::make_pair(Dict::unknownValueStr, 0.0))
{ {
this->head = 0; this->head = 0;
this->name = name; this->name = name;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment