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

fixed bugs

parent 8b06ef70
No related branches found
No related tags found
No related merge requests found
...@@ -49,10 +49,10 @@ class File ...@@ -49,10 +49,10 @@ class File
/// @param filename The filename of the file. /// @param filename The filename of the file.
/// @param mode C's style opening mode. "r"ead or "w"rite. /// @param mode C's style opening mode. "r"ead or "w"rite.
File(const std::string & filename, const std::string & mode); File(const std::string & filename, const std::string & mode);
/// @brief File is not moveable. /// @brief Open the file again and place the reading cursor at the same point.
/// ///
/// @param model /// @param model
File(File && model) = delete; File(const File & model);
/// @brief File is not copyable. /// @brief File is not copyable.
/// ///
/// @param model /// @param model
...@@ -61,7 +61,6 @@ class File ...@@ -61,7 +61,6 @@ class File
File & operator=(const File & model) = delete; File & operator=(const File & model) = delete;
/// @brief The destructor, in which we close the file. /// @brief The destructor, in which we close the file.
~File(); ~File();
/// @brief Peek the next char in the file but without consuming it. /// @brief Peek the next char in the file but without consuming it.
/// ///
/// @return The next char in the file. /// @return The next char in the file.
......
...@@ -91,6 +91,14 @@ class LimitedArray ...@@ -91,6 +91,14 @@ class LimitedArray
return res; return res;
} }
int getNextOverridenRealIndex() const
{
if (lastElementRealIndex < (int)data.size()-1)
return -1;
return lastElementRealIndex - (data.size()-1);
}
int getDataSize() const int getDataSize() const
{ {
return std::min((int)data.size(), nbElements); return std::min((int)data.size(), nbElements);
......
/// @file ProgramOutput.hpp
/// @author Franck Dary
/// @version 1.0
/// @date 2019-02-12
#ifndef PROGRAMOUTPUT__H
#define PROGRAMOUTPUT__H
#include <vector>
#include <string>
#include <cstdio>
struct ProgramOutput
{
private :
std::vector<std::string> lines;
std::vector<float> entropies;
std::vector<unsigned int> lineIndexes;
std::vector<int> outputIndexes;
public :
static ProgramOutput instance;
public :
void print(FILE * output);
void addLine(const std::string & line, float entropy, unsigned int index);
};
#endif
...@@ -7,6 +7,26 @@ File::File(FILE * file) ...@@ -7,6 +7,26 @@ File::File(FILE * file)
this->file = file; this->file = file;
} }
File::File(const File & model)
{
this->buffer = model.buffer;
this->endHasBeenReached = model.endHasBeenReached;
this->filename = model.filename;
std::fpos_t otherPos;
std::fgetpos(model.file, &otherPos);
file = fopen(filename.c_str(), "r");
if (!file)
{
fprintf(stderr, "ERROR (%s) : cannot open file %s\n", ERRINFO, filename.c_str());
exit(1);
}
std::fsetpos(file, &otherPos);
}
char File::peek() char File::peek()
{ {
if (!buffer.empty()) if (!buffer.empty())
......
#include "ProgramOutput.hpp"
ProgramOutput ProgramOutput::instance;
void ProgramOutput::print(FILE * output)
{
for (auto & i : outputIndexes)
if (i != -1)
fprintf(output, "%s\n", lines[i].c_str());
}
void ProgramOutput::addLine(const std::string & line, float entropy, unsigned int index)
{
lines.emplace_back(line);
entropies.emplace_back(entropy);
while (index > outputIndexes.size()-1)
outputIndexes.emplace_back(-1);
if (outputIndexes[index] == -1 || entropies[outputIndexes[index]] < entropy)
outputIndexes[index] = lines.size()-1;
}
...@@ -116,6 +116,8 @@ class Config ...@@ -116,6 +116,8 @@ class Config
void copyPart(Tape & other, unsigned int from, unsigned int to); void copyPart(Tape & other, unsigned int from, unsigned int to);
/// @brief Get the last tape index that will be overriden with the next read. /// @brief Get the last tape index that will be overriden with the next read.
int getNextOverridenDataIndex(); int getNextOverridenDataIndex();
/// @brief Get the last tape index that will be overriden with the next read.
int getNextOverridenRealIndex();
}; };
private : private :
...@@ -162,6 +164,10 @@ class Config ...@@ -162,6 +164,10 @@ class Config
/// @param bd The BD that describes the tapes of this Config. /// @param bd The BD that describes the tapes of this Config.
/// @param inputFilename The name of the input file. /// @param inputFilename The name of the input file.
Config(BD & bd, const std::string inputFilename); Config(BD & bd, const std::string inputFilename);
/// @brief Copy constructor.
///
/// @param other The model.
Config(const Config & other);
/// @brief Get a Tape by its name. /// @brief Get a Tape by its name.
/// ///
/// @param name The name of the Tape. /// @param name The name of the Tape.
...@@ -183,8 +189,9 @@ class Config ...@@ -183,8 +189,9 @@ class Config
/// @brief Print the tapes as the output of the program. /// @brief Print the tapes as the output of the program.
/// ///
/// @param output Where to print. /// @param output Where to print.
/// @param index Index of line to print. /// @param dataIndex Index of line to print.
void printAsOutput(FILE * output, int index); /// @param realIndex Index of line to print.
void printAsOutput(FILE * output, int dataIndex, int realIndex);
/// @brief Print the Config without information loss. /// @brief Print the Config without information loss.
/// ///
/// @param output Where to print. /// @param output Where to print.
......
...@@ -190,7 +190,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na ...@@ -190,7 +190,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
auto undo = [](Config & c, Action::BasicAction & ba) auto undo = [](Config & c, Action::BasicAction & ba)
{c.stackPush(std::stoi(ba.data));}; {c.stackPush(std::stoi(ba.data));};
auto appliable = [](Config & c, Action::BasicAction &) auto appliable = [](Config & c, Action::BasicAction &)
{return !c.stackEmpty() && !c.getTape("GOV")[c.stackTop()].empty();}; {return !c.stackEmpty() && !c.getTape("GOV")[c.stackTop()-c.getHead()].empty();};
Action::BasicAction basicAction = Action::BasicAction basicAction =
{Action::BasicAction::Type::Pop, "", apply, undo, appliable}; {Action::BasicAction::Type::Pop, "", apply, undo, appliable};
...@@ -367,7 +367,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na ...@@ -367,7 +367,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
else else
{ {
simpleBufferWrite(c, "GOV", std::to_string(rootIndex - s), s-b0); simpleBufferWrite(c, "GOV", std::to_string(rootIndex - s), s-b0);
ba.data += "+"+s-b0; ba.data += "+"+std::to_string(s-b0);
} }
} }
...@@ -599,7 +599,7 @@ bool ActionBank::isRuleAppliable(Config & config, ...@@ -599,7 +599,7 @@ bool ActionBank::isRuleAppliable(Config & config,
{ {
if (!simpleBufferWriteAppliable(config, tapeName, relativeIndex)) if (!simpleBufferWriteAppliable(config, tapeName, relativeIndex))
return false; return false;
return ruleIsAppliable(config.getTape(tapeName)[config.getHead()+relativeIndex], rule); return ruleIsAppliable(config.getTape(tapeName)[relativeIndex], rule);
} }
void ActionBank::writeRuleResult(Config & config, const std::string & fromTapeName, const std::string & targetTapeName, const std::string & rule, int relativeIndex) void ActionBank::writeRuleResult(Config & config, const std::string & fromTapeName, const std::string & targetTapeName, const std::string & rule, int relativeIndex)
......
...@@ -17,6 +17,23 @@ Config::Config(BD & bd, const std::string inputFilename) : bd(bd), hashHistory(1 ...@@ -17,6 +17,23 @@ Config::Config(BD & bd, const std::string inputFilename) : bd(bd), hashHistory(1
tapes.emplace_back(bd.getNameOfLine(i), bd.lineIsKnown(i)); tapes.emplace_back(bd.getNameOfLine(i), bd.lineIsKnown(i));
} }
Config::Config(const Config & other) : bd(other.bd), hashHistory(other.hashHistory), pastActions(other.pastActions)
{
this->currentStateName = other.currentStateName;
this->actionHistory = other.actionHistory;
this->entropyHistory = other.entropyHistory;
this->stack = other.stack;
this->stackHistory = other.stackHistory;
this->head = other.head;
this->outputFile = other.outputFile;
this->lastIndexPrinted = other.lastIndexPrinted;
this->tapes = other.tapes;
this->inputFilename = other.inputFilename;
this->inputAllRead = other.inputAllRead;
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), hyp(ProgramParameters::readSize*4+1)
{ {
this->head = 0; this->head = 0;
...@@ -62,7 +79,7 @@ void Config::readInput() ...@@ -62,7 +79,7 @@ void Config::readInput()
exit(1); exit(1);
} }
printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex()); printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex(), tapes[0].getNextOverridenRealIndex());
for(unsigned int i = 0; i < cols.size(); i++) for(unsigned int i = 0; i < cols.size(); i++)
if(bd.hasLineOfInputCol(i)) if(bd.hasLineOfInputCol(i))
...@@ -83,7 +100,7 @@ void Config::readInput() ...@@ -83,7 +100,7 @@ void Config::readInput()
if (haveRead < toRead || tapes[0].size() == ProgramParameters::tapeSize) if (haveRead < toRead || tapes[0].size() == ProgramParameters::tapeSize)
{ {
printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex()); printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex(), tapes[0].getNextOverridenRealIndex());
inputAllRead = true; inputAllRead = true;
} }
...@@ -176,23 +193,30 @@ void Config::printAsExample(FILE *) ...@@ -176,23 +193,30 @@ void Config::printAsExample(FILE *)
exit(1); exit(1);
} }
void Config::printAsOutput(FILE * output, int index) void Config::printAsOutput(FILE * output, int dataIndex, int realIndex)
{ {
if (index == -1 || !output) if (dataIndex == -1 || !output)
return; return;
lastIndexPrinted = index; lastIndexPrinted = dataIndex;
unsigned int lastToPrint = 0; unsigned int lastToPrint = 0;
for (unsigned int j = 0; j < tapes.size(); j++) for (unsigned int j = 0; j < tapes.size(); j++)
if(bd.mustPrintLine(j)) if(bd.mustPrintLine(j))
lastToPrint = j; lastToPrint = j;
std::string toPrint = "";
for (unsigned int j = 0; j < tapes.size(); j++) for (unsigned int j = 0; j < tapes.size(); j++)
{ {
if(bd.mustPrintLine(j)) if(bd.mustPrintLine(j))
fprintf(output, "%s%s", tapes[j][index-head].empty() ? "0" : tapes[j][index-head].c_str(), j == lastToPrint ? "\n" : "\t"); {
toPrint += tapes[j][dataIndex-head].empty() ? "0" : tapes[j][dataIndex-head].c_str();
toPrint += j == lastToPrint ? "" : "\t";
//fprintf(output, "%s%s", tapes[j][index-head].empty() ? "0" : tapes[j][index-head].c_str(), j == lastToPrint ? "\n" : "\t");
}
} }
fprintf(stderr, "Print %d\n", realIndex);
} }
void Config::moveHead(int mvt) void Config::moveHead(int mvt)
...@@ -528,15 +552,33 @@ int Config::Tape::getNextOverridenDataIndex() ...@@ -528,15 +552,33 @@ int Config::Tape::getNextOverridenDataIndex()
return ref.getNextOverridenDataIndex(); return ref.getNextOverridenDataIndex();
} }
int Config::Tape::getNextOverridenRealIndex()
{
return ref.getNextOverridenRealIndex();
}
void Config::printTheRest() void Config::printTheRest()
{ {
if (!outputFile) if (!outputFile)
return; return;
int tapeSize = tapes[0].size();
int goalPrintIndex = lastIndexPrinted; int goalPrintIndex = lastIndexPrinted;
int realIndex = tapeSize - ((()-())+());
for (int i = goalPrintIndex+1; i < (tapes[0].dataSize()-(goalPrintIndex == -1 ? 1 : 0)); i++) for (int i = goalPrintIndex+1; i < (tapes[0].dataSize()-(goalPrintIndex == -1 ? 1 : 0)); i++)
printAsOutput(outputFile, i); {
printAsOutput(outputFile, i, 0);
fprintf(stderr, "i=%d tapeSize=%d dataSize=%d\n", i, tapeSize, tapes[0].dataSize());
fprintf(stderr, "calcul = %d\n", realIndex);
realIndex++;
}
for (int i = 0; i < goalPrintIndex; i++) for (int i = 0; i < goalPrintIndex; i++)
printAsOutput(outputFile, i); {
printAsOutput(outputFile, i, 0);
fprintf(stderr, "i=%d tapeSize=%d dataSize=%d\n", i, tapeSize, tapes[0].dataSize());
fprintf(stderr, "calcul = %d\n", realIndex);
realIndex++;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment