From ca3fa6846a84921f6bfb7230f8e73704f04158e9 Mon Sep 17 00:00:00 2001 From: Wiktor Lawski Date: Sat, 12 Sep 2020 19:50:31 +0200 Subject: [PATCH] Allow saving SES to provided data storage It is not always efficient (or possible) to use standard output streams. Moreover, such generated result may be now further processed. --- dtl/Diff.hpp | 9 +++++++++ dtl/functors.hpp | 14 +++++++++++++ examples/storage.hpp | 27 +++++++++++++++++++++++++ examples/strdiff_storage.cpp | 38 ++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 examples/storage.hpp create mode 100644 examples/strdiff_storage.cpp diff --git a/dtl/Diff.hpp b/dtl/Diff.hpp index 222d9ad..cff05fe 100644 --- a/dtl/Diff.hpp +++ b/dtl/Diff.hpp @@ -340,6 +340,15 @@ namespace dtl { sesElemVec ses_v = ses.getSequence (); for_each (ses_v.begin (), ses_v.end(), PT < sesElem, stream > (out)); } + + /** + * store difference between A and B as an SES with custom storage + */ + template < typename storedData, template < typename SEET, typename STRT > class ST > + void storeSES(storedData& sd) const { + sesElemVec ses_v = ses.getSequence(); + for_each(ses_v.begin(), ses_v.end(), ST < sesElem, storedData >(sd)); + } /** * print difference between A and B in the Unified Format diff --git a/dtl/functors.hpp b/dtl/functors.hpp index a4da5e5..86d8709 100644 --- a/dtl/functors.hpp +++ b/dtl/functors.hpp @@ -118,6 +118,20 @@ namespace dtl { private : stream& out_; }; + + /** + * storage class template + */ + template + class Storage + { + public: + Storage(storedData& sd) : storedData_(sd) {} + virtual ~Storage() {} + virtual void operator() (const sesElem& se) const = 0; + protected: + storedData& storedData_; + }; /** * compare class template diff --git a/examples/storage.hpp b/examples/storage.hpp new file mode 100644 index 0000000..1b020f4 --- /dev/null +++ b/examples/storage.hpp @@ -0,0 +1,27 @@ +#ifndef DTL_STORAGE +#define DTL_STORAGE + +#include + +template +class CustomStorage : public dtl::Storage < sesElem, storedData > +{ +public : + CustomStorage(storedData& sd) : dtl::Storage < sesElem, storedData > (sd) {} + ~CustomStorage() {} + void operator() (const sesElem& se) const { + switch (se.second.type) { + case dtl::SES_ADD: + this->storedData_ = this->storedData_ + "Add: " + se.first + "\n"; + break; + case dtl::SES_DELETE: + this->storedData_ = this->storedData_ + "Delete: " + se.first + "\n"; + break; + case dtl::SES_COMMON: + this->storedData_ = this->storedData_ + "Common: " + se.first + "\n"; + break; + } + } +}; + +#endif // DTL_STORAGE diff --git a/examples/strdiff_storage.cpp b/examples/strdiff_storage.cpp new file mode 100644 index 0000000..b1b3ae7 --- /dev/null +++ b/examples/strdiff_storage.cpp @@ -0,0 +1,38 @@ +#include +#include "common.hpp" +#include +#include + +#include "storage.hpp" + +using namespace std; + +using dtl::Diff; + +int main(int argc, char *argv[]){ + + if (isFewArgs(argc)) { + cerr << "Too few arguments." << endl; + return -1; + } + + typedef char elem; + typedef string sequence; + + sequence A(argv[1]); + sequence B(argv[2]); + + Diff< elem, sequence > d(A, B); + d.compose(); + + // Shortest Edit Script + cout << "SES" << endl; + + string result; + + d.storeSES < string, CustomStorage > (result); + + cout << result; + + return 0; +}