diff --git a/src/MarkovMatrix.cpp b/src/MarkovMatrix.cpp index cd7c4ca..a1ac54b 100644 --- a/src/MarkovMatrix.cpp +++ b/src/MarkovMatrix.cpp @@ -1,9 +1,17 @@ #include "MarkovMatrix.hpp" #include // for std::max +#include +#include -MarkovMatrix::MarkovMatrix(int length) +MarkovMatrix::MarkovMatrix() +{ + // do nothing +} + + +MarkovMatrix::MarkovMatrix(char length) { this->length = length; } @@ -45,3 +53,41 @@ std::unordered_map MarkovMatrix::hiccup(std::string& previous) std::string before = previous.substr(pos, len); return this->matrix.at(before); } + + +void MarkovMatrix::load(std::istream& is) +{ + char file_version; + is.read(&file_version, sizeof(char)); +} + + +void MarkovMatrix::save(std::ostream& os) +{ + os << this->file_version << std::endl; + + os << this->matrix.size() << std::endl; + for (auto map : this->matrix) { + os << map.first << std::endl; + + os << map.second.size() << std::endl; + for (auto character : map.second) { + os << character.first << std::endl; + os << character.second << std::endl; + } + } +} + + +std::istream& operator>>(std::istream& is, MarkovMatrix& matrix) +{ + matrix.load(is); + return is; +} + + +std::ostream& operator<<(std::ostream& os, MarkovMatrix& matrix) +{ + matrix.save(os); + return os; +} diff --git a/src/MarkovMatrix.hpp b/src/MarkovMatrix.hpp index 9bff329..03981f8 100644 --- a/src/MarkovMatrix.hpp +++ b/src/MarkovMatrix.hpp @@ -8,16 +8,27 @@ class MarkovMatrix { public: - MarkovMatrix(int length); + MarkovMatrix(); + MarkovMatrix(char length); ~MarkovMatrix(); void feed(std::string& line); void reset(int length=0); // reset the matrix, length>0 -> set a new length std::unordered_map hiccup(std::string& previous); + // loading and saving of internal state to files + virtual void load(std::istream& is); + virtual void save(std::ostream& os); + private: - int length; + char length = 0; std::unordered_map > matrix; + + const char file_version = 1; // version of file format }; + +std::istream& operator>>(std::istream& is, MarkovMatrix& matrix); +std::ostream& operator<<(std::ostream& os, MarkovMatrix& matrix); + #endif diff --git a/src/SimpleMarkov.cpp b/src/SimpleMarkov.cpp index e675085..5c77df7 100644 --- a/src/SimpleMarkov.cpp +++ b/src/SimpleMarkov.cpp @@ -1,6 +1,8 @@ #include "SimpleMarkov.hpp" #include +#include +#include /* class SimpleMarkov : public Markov @@ -21,6 +23,10 @@ private: virtual std::string regurgitateLine(std::string start=""); }; */ +SimpleMarkov::SimpleMarkov(std::string filename) +{ + this->load(filename); +} SimpleMarkov::SimpleMarkov(int length) : matrix(length), @@ -36,15 +42,52 @@ SimpleMarkov::~SimpleMarkov() } +/* void SimpleMarkov::load(std::string filename) { - // do nothing + std::ifstream ifs(filename); + std::string type, version; + ifs >> type >> version; + if (type != "s" || version != "0001") { + throw std::invalid_argument("Wrong file type or version."); + } + + ifs >> this->matrix; + ifs.close(); } void SimpleMarkov::save(std::string filename) { - // do nothing + std::ofstream ofs(filename); + ofs << "s" << std::endl; + ofs << "0001" << std::endl; + ofs << this->matrix; + ofs.close(); +} +*/ + + +void SimpleMarkov::load(std::string filename) +{ + std::ifstream ifs(filename); + + + + ifs.close(); +} + + +void SimpleMarkov::save(std::string filename) +{ + std::ofstream ofs(filename); + + // file type and version of format + ofs << this->file_gentype << std::endl; + ofs << this->file_version << std::endl; + ofs << this->matrix; + + ofs.close(); } diff --git a/src/SimpleMarkov.hpp b/src/SimpleMarkov.hpp index 3d7c4d7..47da904 100644 --- a/src/SimpleMarkov.hpp +++ b/src/SimpleMarkov.hpp @@ -11,6 +11,7 @@ class SimpleMarkov : public Markov { public: + SimpleMarkov(std::string filename); SimpleMarkov(int length=10); virtual ~SimpleMarkov(); @@ -26,6 +27,9 @@ private: MarkovMatrix matrix; std::default_random_engine generator; + + const char file_gentype = 's'; // type of the generator + const char file_version = 1; // version of file format }; #endif