From ab43a43c581bdfc15ba6bcf42f7ded56bf67cfd0 Mon Sep 17 00:00:00 2001 From: "valery.isaev@gmail.com" Date: Sun, 27 Mar 2011 18:46:05 +0000 Subject: [PATCH] precompare git-svn-id: https://projectname.googlecode.com/svn/trunk@14 c416075f-80b4-e980-4839-00ea3ed24e77 --- trunk/src/clusterization.cpp | 20 ++++++++++++++++++ trunk/src/clusterization.h | 38 ++++++++++++++--------------------- trunk/src/default_main.cpp | 2 +- trunk/src/file_types/base.cpp | 9 +++++---- trunk/src/file_types/base.h | 9 ++++++++- trunk/src/file_types/img.cpp | 4 ++++ trunk/src/file_types/img.h | 1 + trunk/src/file_types/text.cpp | 4 ++++ trunk/src/file_types/text.h | 1 + trunk/todo.txt | 1 - 10 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 trunk/src/clusterization.cpp diff --git a/trunk/src/clusterization.cpp b/trunk/src/clusterization.cpp new file mode 100644 index 0000000..62a7fbb --- /dev/null +++ b/trunk/src/clusterization.cpp @@ -0,0 +1,20 @@ +#include "clusterization.h" + +void clusterization::next(const T_ptr& t) { + std::map::iterator it = _continuations.find(t); + if (it == _continuations.end()) { + Cont cont = category::kleisli::sink::continuation().clone(); + _continuations.insert(std::make_pair(t, cont)); + cont->next(t); + } else { + it->second->next(t); + } +} + +void clusterization::stop() { + std::map::iterator it = _continuations.begin(); + for (; it != _continuations.end(); ++it) { + it->second->stop(); + } + _continuations.clear(); +} diff --git a/trunk/src/clusterization.h b/trunk/src/clusterization.h index b613fdb..937aa73 100644 --- a/trunk/src/clusterization.h +++ b/trunk/src/clusterization.h @@ -6,35 +6,27 @@ #include #include "kleisli.h" +#include "file_types/base.h" -template -class clusterization: public category::kleisli::arr, boost::shared_ptr > { - typedef boost::shared_ptr T_ptr; +class clusterization: public category::kleisli::arr + , boost::shared_ptr > { + typedef boost::shared_ptr T_ptr; + typedef boost::shared_ptr > Cont; struct less { bool operator()(const T_ptr& t1, const T_ptr& t2) { - return typeid(*t1).before(typeid(*t2)); + if (typeid(*t1).before(typeid(*t2))) { + return true; + } + if (typeid(*t2).before(typeid(*t1))) { + return false; + } + return t1->precompare(t2) == file_type::less; } }; - typedef boost::shared_ptr > Cont; - typedef std::map map; - map _continuations; + std::map _continuations; public: - void next(const T_ptr& t) { - typename std::map::iterator it = _continuations.find(t); - if (it == _continuations.end()) { - Cont cont = category::kleisli::sink::continuation().clone(); - _continuations.insert(std::make_pair(t, cont)); - cont->next(t); - } else { - it->second->next(t); - } - } - void stop() { - for (typename map::iterator it = _continuations.begin(); it != _continuations.end(); ++it) { - it->second->stop(); - } - _continuations.clear(); - } + void next(const T_ptr& t); + void stop(); }; #endif diff --git a/trunk/src/default_main.cpp b/trunk/src/default_main.cpp index 44dccc0..696f2f9 100644 --- a/trunk/src/default_main.cpp +++ b/trunk/src/default_main.cpp @@ -49,7 +49,7 @@ void default_main(const program_options& po) { >>= fs::recursive() >>= (po.extensions().empty() ? The< arr >() : The(po.extensions())) >>= file_typer_match_first() - >>= clusterization() + >>= clusterization() >>= comparator() >>= accumulator() >>= output; diff --git a/trunk/src/file_types/base.cpp b/trunk/src/file_types/base.cpp index 6aaa5fa..8fd66bf 100644 --- a/trunk/src/file_types/base.cpp +++ b/trunk/src/file_types/base.cpp @@ -9,16 +9,13 @@ namespace file_type { -base::base(const fs::path& p): _path(p), size(fs::file_size(p)) { } +base::base(const fs::path& p): _path(p), _size(fs::file_size(p)) { } boost::shared_ptr base::try_file(const fs::path& file) { return boost::make_shared(file); } boost::shared_ptr base::compare(const boost::shared_ptr& a) const { - if (fs::file_size(_path) != fs::file_size(a->_path)) { - return boost::shared_ptr(); - } const int buf_size = 4096; char buf1[buf_size], buf2[buf_size]; fs::ifstream file1(_path), file2(a->_path); @@ -34,4 +31,8 @@ boost::shared_ptr base::compare(const boost::shared_ptr& a) const { } } +inline comparison_result base::precompare(const boost::shared_ptr& a) const { + return _size < a->_size ? less : _size == a->_size ? equal : greater; +} + } diff --git a/trunk/src/file_types/base.h b/trunk/src/file_types/base.h index 7f8df54..20bb31d 100644 --- a/trunk/src/file_types/base.h +++ b/trunk/src/file_types/base.h @@ -10,14 +10,21 @@ namespace file_type { +enum comparison_result + { less + , greater + , equal + }; + class base { fs::path _path; - unsigned int size; + unsigned int _size; public: base(const fs::path& p); const fs::path& path() const { return _path; } static boost::shared_ptr try_file(const fs::path& file); virtual boost::shared_ptr compare(const boost::shared_ptr& a) const; + virtual comparison_result precompare(const boost::shared_ptr& a) const; }; } diff --git a/trunk/src/file_types/img.cpp b/trunk/src/file_types/img.cpp index 2ecf037..6abdfa1 100644 --- a/trunk/src/file_types/img.cpp +++ b/trunk/src/file_types/img.cpp @@ -31,4 +31,8 @@ boost::shared_ptr img::compare(const boost::shared_ptr& a) const { return boost::shared_ptr(); } +inline comparison_result img::precompare(const boost::shared_ptr& a) const { + return equal; +} + } diff --git a/trunk/src/file_types/img.h b/trunk/src/file_types/img.h index d67c869..66dbeca 100644 --- a/trunk/src/file_types/img.h +++ b/trunk/src/file_types/img.h @@ -12,6 +12,7 @@ struct img: base { img(const fs::path& p): base(p) {} static boost::shared_ptr try_file(const boost::shared_ptr& file); boost::shared_ptr compare(const boost::shared_ptr& a) const; + comparison_result precompare(const boost::shared_ptr& a) const; }; } diff --git a/trunk/src/file_types/text.cpp b/trunk/src/file_types/text.cpp index d477092..c4d80cf 100644 --- a/trunk/src/file_types/text.cpp +++ b/trunk/src/file_types/text.cpp @@ -77,4 +77,8 @@ boost::shared_ptr text::compare(const boost::shared_ptr& a) const { } } +inline comparison_result text::precompare(const boost::shared_ptr& a) const { + return equal; +} + } diff --git a/trunk/src/file_types/text.h b/trunk/src/file_types/text.h index 44d2ed6..f33b50c 100644 --- a/trunk/src/file_types/text.h +++ b/trunk/src/file_types/text.h @@ -10,6 +10,7 @@ struct text: base { text(const fs::path& p): base(p) {} static boost::shared_ptr try_file(const boost::shared_ptr& file); boost::shared_ptr compare(const boost::shared_ptr& a) const; + comparison_result precompare(const boost::shared_ptr& a) const; }; } diff --git a/trunk/todo.txt b/trunk/todo.txt index 520ceb5..e69de29 100644 --- a/trunk/todo.txt +++ b/trunk/todo.txt @@ -1 +0,0 @@ -precompare !!!