00001
00002
00003 #include <boost/iostreams/filtering_stream.hpp>
00004 #include <boost/shared_ptr.hpp>
00005 #include <algorithm>
00006 #include <string>
00007 #include <vector>
00008 #include "moses/TranslationModel/UG/generic/file_io/ug_stream.h"
00009 using namespace std;
00010 using namespace ugdiss;
00011 using namespace boost::iostreams;
00012
00013 typedef boost::shared_ptr<filtering_istream> fptr;
00014
00015 class Part
00016 {
00017 string fname;
00018 fptr f;
00019 string my_lines[2];
00020 size_t ctr;
00021 public:
00022 string const& line() const {
00023 static string empty_line;
00024 return f ? my_lines[ctr%2] : empty_line;
00025 }
00026
00027 Part(string _fname) : ctr(0) {
00028 fname = _fname;
00029 f.reset(open_input_stream(fname));
00030 if (!getline(*f, my_lines[0])) f.reset();
00031 }
00032
00033 bool next() {
00034 if (!f) return false;
00035 if (!getline(*f, my_lines[++ctr%2])) {
00036 f.reset();
00037 --ctr;
00038 return false;
00039 }
00040 assert(my_lines[(ctr-1)%2] <= my_lines[ctr%2]);
00041 return true;
00042 }
00043
00044 bool operator <(Part const& other) const {
00045 return line() < other.line();
00046 }
00047
00048 bool operator <=(Part const& other) const {
00049 return line() <= other.line();
00050 }
00051
00052 bool operator >(Part const& other) const {
00053 return line() > other.line();
00054 }
00055
00056 bool operator >=(Part const& other) const {
00057 return line() >= other.line();
00058 }
00059
00060 bool go(ostream& out) {
00061 if (!f) return false;
00062 #if 0
00063 if (ctr) {
00064 out << fname << "-" << ctr - 1 << "-";
00065 out << my_lines[(ctr - 1)%2] << endl;
00066 }
00067 do {
00068 out << fname << " " << ctr << " ";
00069 out << line() << "\n";
00070 } while (next() && my_lines[0] == my_lines[1]);
00071 #else
00072 do {
00073 out << line() << "\n";
00074 } while (next() && my_lines[0] == my_lines[1]);
00075 out.flush();
00076 #endif
00077 return f != NULL;
00078 }
00079
00080 };
00081
00082
00083 int main(int argc, char* argv[])
00084 {
00085 vector<Part> parts;
00086 for (int i = 1; i < argc; ++i)
00087 parts.push_back(Part(argv[i]));
00088 make_heap(parts.begin(), parts.end(), greater<Part>());
00089 while (parts.size()) {
00090 pop_heap(parts.begin(), parts.end(), greater<Part>());
00091 if (parts.back().go(cout))
00092 push_heap(parts.begin(), parts.end(), greater<Part>());
00093 else parts.pop_back();
00094 }
00095 }