From 529744292de6bcba8d28ed21c7706a92f1dc5f24 Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Thu, 28 Feb 2019 15:31:59 +0100
Subject: [PATCH] Fixed a bug related do undoing actions

---
 maca_common/include/LimitedArray.hpp  | 18 ++++++++++++------
 transition_machine/src/ActionBank.cpp | 16 +++++++++++++---
 transition_machine/src/Config.cpp     |  4 ++--
 3 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/maca_common/include/LimitedArray.hpp b/maca_common/include/LimitedArray.hpp
index c6dcda0..738cd16 100644
--- a/maca_common/include/LimitedArray.hpp
+++ b/maca_common/include/LimitedArray.hpp
@@ -13,16 +13,18 @@ class LimitedArray
 {
   private :
 
-  std::vector<T> data;
+  std::vector< std::pair<T,bool> > data;
   int nbElements;
   int lastElementDataIndex;
   int lastElementRealIndex;
+  T mask;
 
   public :
 
-  LimitedArray(unsigned int limit) : data(limit)
+  LimitedArray(unsigned int limit, const T & mask) : data(limit)
   {
     clear();
+    this->mask = mask;
   }
 
   void clear()
@@ -44,17 +46,21 @@ class LimitedArray
 
     lastElementRealIndex++;
 
-    data[lastElementDataIndex] = elem;
+    data[lastElementDataIndex].first = elem;
   }
 
   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)
   {
-    data[index % data.size()] = value;
+    data[index % data.size()].first = value;
+    data[index % data.size()].second = false;
   }
 
   int getLastIndex() const
@@ -70,7 +76,7 @@ class LimitedArray
   void printForDebug(FILE * out) const
   {
     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");
   }
 
diff --git a/transition_machine/src/ActionBank.cpp b/transition_machine/src/ActionBank.cpp
index 4737d48..ed373d8 100644
--- a/transition_machine/src/ActionBank.cpp
+++ b/transition_machine/src/ActionBank.cpp
@@ -401,7 +401,8 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
         }
         auto deps = split(ba.data, '+');
         for (auto s : deps)
-          simpleBufferWrite(c, "GOV", "", std::stoi(s));
+          if (!s.empty())
+            simpleBufferWrite(c, "GOV", "", std::stoi(s));
         ba.data.clear();
       };
     auto appliable = [](Config & c, Action::BasicAction &)
@@ -453,7 +454,9 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
         }
         auto deps = split(ba.data, '+');
         for (auto & dep : deps)
-          simpleBufferWrite(c, "LABEL", "", std::stoi(dep));
+          if (!dep.empty())
+            simpleBufferWrite(c, "LABEL", "", std::stoi(dep));
+        ba.data.clear();
       };
     auto appliable2 = [](Config & c, Action::BasicAction &)
       {
@@ -480,7 +483,9 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
       {
         auto elems = split(ba.data);
         for (auto elem : elems)
-          c.stackPush(std::stoi(elem));
+          if (!elem.empty())
+            c.stackPush(std::stoi(elem));
+        ba.data.clear();
       };
     auto appliable4 = [](Config & c, Action::BasicAction &)
       {
@@ -530,6 +535,11 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
 
             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();
               if (ProgramParameters::debug)
                 fprintf(stderr, "Undoing... <%s><%s>\n", a.first.c_str(), a.second.name.c_str());
diff --git a/transition_machine/src/Config.cpp b/transition_machine/src/Config.cpp
index bbd18c0..f212b77 100644
--- a/transition_machine/src/Config.cpp
+++ b/transition_machine/src/Config.cpp
@@ -5,7 +5,7 @@
 #include "Action.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->stackHistory = -1;
@@ -36,7 +36,7 @@ Config::Config(const Config & other) : bd(other.bd), hashHistory(other.hashHisto
   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->name = name;
-- 
GitLab