diff --git a/src/MarkovMatrix.cpp b/src/MarkovMatrix.cpp index a1ac54b..87b3726 100644 --- a/src/MarkovMatrix.cpp +++ b/src/MarkovMatrix.cpp @@ -11,7 +11,7 @@ MarkovMatrix::MarkovMatrix() } -MarkovMatrix::MarkovMatrix(char length) +MarkovMatrix::MarkovMatrix(int length) { this->length = length; } @@ -57,14 +57,44 @@ std::unordered_map MarkovMatrix::hiccup(std::string& previous) void MarkovMatrix::load(std::istream& is) { - char file_version; - is.read(&file_version, sizeof(char)); + int version; + is >> version; + if (version != this->file_version) { + return; + } + + is >> this->length; + + int size; + is >> size; + std::cout << "size: " << size << std::endl; + std::string read; std::getline(is, read); + for (int i=0; imatrix[before][current] = count; + } + } } void MarkovMatrix::save(std::ostream& os) { os << this->file_version << std::endl; + os << this->length << std::endl; os << this->matrix.size() << std::endl; for (auto map : this->matrix) { diff --git a/src/MarkovMatrix.hpp b/src/MarkovMatrix.hpp index 03981f8..e57b67c 100644 --- a/src/MarkovMatrix.hpp +++ b/src/MarkovMatrix.hpp @@ -9,7 +9,7 @@ class MarkovMatrix { public: MarkovMatrix(); - MarkovMatrix(char length); + MarkovMatrix(int length); ~MarkovMatrix(); void feed(std::string& line); @@ -21,10 +21,10 @@ public: virtual void save(std::ostream& os); private: - char length = 0; + int length = 0; std::unordered_map > matrix; - const char file_version = 1; // version of file format + const int file_version = 1; // version of file format }; diff --git a/src/SimpleMarkov.cpp b/src/SimpleMarkov.cpp index 5c77df7..46dbe75 100644 --- a/src/SimpleMarkov.cpp +++ b/src/SimpleMarkov.cpp @@ -4,6 +4,12 @@ #include #include + + + + +#include + /* class SimpleMarkov : public Markov { @@ -72,7 +78,13 @@ void SimpleMarkov::load(std::string filename) { std::ifstream ifs(filename); + char gentype; + int version; + ifs >> gentype >> version; + if (gentype == this->file_gentype and version == this->file_version) { + ifs >> this->matrix; + } ifs.close(); } diff --git a/src/SimpleMarkov.hpp b/src/SimpleMarkov.hpp index 47da904..729fc06 100644 --- a/src/SimpleMarkov.hpp +++ b/src/SimpleMarkov.hpp @@ -29,7 +29,7 @@ private: std::default_random_engine generator; const char file_gentype = 's'; // type of the generator - const char file_version = 1; // version of file format + const int file_version = 1; // version of file format }; #endif