From b8d43ee61513190347f62e746b0aac27988960b5 Mon Sep 17 00:00:00 2001 From: larry-fuy Date: Sun, 1 Jun 2014 12:50:47 -0500 Subject: [PATCH 1/6] Todo: heap in merge files --- include/detail/mergesort.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/detail/mergesort.hpp b/include/detail/mergesort.hpp index b93573b..cef58c8 100644 --- a/include/detail/mergesort.hpp +++ b/include/detail/mergesort.hpp @@ -72,6 +72,7 @@ bool const do_file_merge(It first, It last, std::string const &outfilename) if (file_lines.size() == 1) it = file_lines.begin(); else + // todo : heap for performance it = std::min_element(file_lines.begin(), file_lines.end()); outfile << it->second << "\r"; From 141cac18f587caed6cd5bab55c87b935c3af00bf Mon Sep 17 00:00:00 2001 From: larry-fuy Date: Sun, 1 Jun 2014 22:00:04 -0500 Subject: [PATCH 2/6] a simple makefile, will be revised --- examples/friends/Makefile | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 examples/friends/Makefile diff --git a/examples/friends/Makefile b/examples/friends/Makefile new file mode 100644 index 0000000..2b3e871 --- /dev/null +++ b/examples/friends/Makefile @@ -0,0 +1,2 @@ +all: + g++ -Wall -std=c++11 -I../../include -I/usr/include/boost friends.cpp -o friends -lboost_system -lboost_filesystem -lboost_iostreams -pthread From 48af5080601f7a06d56d372e6e2c8395dd5d2f2b Mon Sep 17 00:00:00 2001 From: larry-fuy Date: Wed, 4 Jun 2014 23:53:47 -0500 Subject: [PATCH 3/6] add heap in do_file_merge --- include/detail/mergesort.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/detail/mergesort.hpp b/include/detail/mergesort.hpp index cef58c8..12b79bd 100644 --- a/include/detail/mergesort.hpp +++ b/include/detail/mergesort.hpp @@ -49,7 +49,7 @@ bool const do_file_merge(It first, It last, std::string const &outfilename) // in the meantime, we assert if we go round the loop more than once as it will produce incorrect results assert(++count == 1); - typedef std::list, std::string> > file_lines_t; + typedef std::vector, std::string> > file_lines_t; file_lines_t file_lines; for (; first!=last; ++first) { @@ -66,19 +66,23 @@ bool const do_file_merge(It first, It last, std::string const &outfilename) file_lines.push_back(std::make_pair(file, line)); } + make_heap(begin(file_lines), end(file_lines), [](const std::string& l, const std::string& r) { return l > r; }); while (file_lines.size() > 0) { typename file_lines_t::iterator it; if (file_lines.size() == 1) it = file_lines.begin(); else - // todo : heap for performance - it = std::min_element(file_lines.begin(), file_lines.end()); + { + std::pop_heap(begin(file_lines), end(file_lines)); + it = file_lines.back(); + } outfile << it->second << "\r"; std::getline(*it->first, it->second, '\r'); if (it->first->eof()) file_lines.erase(it); + std::push_heap(begin(file_lines), end(file_lines)); } } From da97c870f28f78e95252402b1c0d16121805cb87 Mon Sep 17 00:00:00 2001 From: larry-fuy Date: Wed, 4 Jun 2014 23:59:07 -0500 Subject: [PATCH 4/6] minor fix of heap --- include/detail/mergesort.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/detail/mergesort.hpp b/include/detail/mergesort.hpp index 12b79bd..f6f2a27 100644 --- a/include/detail/mergesort.hpp +++ b/include/detail/mergesort.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #ifdef __GNUC__ @@ -66,7 +67,7 @@ bool const do_file_merge(It first, It last, std::string const &outfilename) file_lines.push_back(std::make_pair(file, line)); } - make_heap(begin(file_lines), end(file_lines), [](const std::string& l, const std::string& r) { return l > r; }); + std::make_heap(begin(file_lines), end(file_lines), [](const std::string& l, const std::string& r) { return l > r; }); while (file_lines.size() > 0) { typename file_lines_t::iterator it; From e0487f1c5100e68dda98a703b29c72343b131634 Mon Sep 17 00:00:00 2001 From: larry-fuy Date: Thu, 5 Jun 2014 00:09:40 -0500 Subject: [PATCH 5/6] minor fix --- include/detail/mergesort.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/detail/mergesort.hpp b/include/detail/mergesort.hpp index f6f2a27..11ebf2d 100644 --- a/include/detail/mergesort.hpp +++ b/include/detail/mergesort.hpp @@ -75,15 +75,16 @@ bool const do_file_merge(It first, It last, std::string const &outfilename) it = file_lines.begin(); else { - std::pop_heap(begin(file_lines), end(file_lines)); - it = file_lines.back(); + std::pop_heap(begin(file_lines), end(file_lines)); + it = file_lines.back(); } outfile << it->second << "\r"; std::getline(*it->first, it->second, '\r'); if (it->first->eof()) file_lines.erase(it); - std::push_heap(begin(file_lines), end(file_lines)); + else + std::push_heap(begin(file_lines), end(file_lines)); } } From 670ffda9ee4723c74d16ae9e5da42964e012b523 Mon Sep 17 00:00:00 2001 From: Yong Fu Date: Thu, 5 Jun 2014 00:11:31 -0500 Subject: [PATCH 6/6] indent change --- include/detail/mergesort.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/detail/mergesort.hpp b/include/detail/mergesort.hpp index 11ebf2d..8c9e5f7 100644 --- a/include/detail/mergesort.hpp +++ b/include/detail/mergesort.hpp @@ -84,7 +84,7 @@ bool const do_file_merge(It first, It last, std::string const &outfilename) if (it->first->eof()) file_lines.erase(it); else - std::push_heap(begin(file_lines), end(file_lines)); + std::push_heap(begin(file_lines), end(file_lines)); } }