Skip to content

Commit

Permalink
Allow saving SES to provided data storage
Browse files Browse the repository at this point in the history
It is not always efficient (or possible) to use standard output
streams. Moreover, such generated result may be now further
processed.
  • Loading branch information
wlawski committed Sep 12, 2020
1 parent 6b030d6 commit ca3fa68
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dtl/Diff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions dtl/functors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ namespace dtl {
private :
stream& out_;
};

/**
* storage class template
*/
template <typename sesElem, typename storedData >
class Storage
{
public:
Storage(storedData& sd) : storedData_(sd) {}
virtual ~Storage() {}
virtual void operator() (const sesElem& se) const = 0;
protected:
storedData& storedData_;
};

/**
* compare class template
Expand Down
27 changes: 27 additions & 0 deletions examples/storage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef DTL_STORAGE
#define DTL_STORAGE

#include <dtl/dtl.hpp>

template <typename sesElem, typename storedData >
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
38 changes: 38 additions & 0 deletions examples/strdiff_storage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <dtl/dtl.hpp>
#include "common.hpp"
#include <iostream>
#include <string>

#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;
}

0 comments on commit ca3fa68

Please sign in to comment.