Skip to content

Commit

Permalink
precompare
Browse files Browse the repository at this point in the history
git-svn-id: https://projectname.googlecode.com/svn/trunk@14 c416075f-80b4-e980-4839-00ea3ed24e77
  • Loading branch information
[email protected] committed Mar 27, 2011
1 parent a0c5f27 commit ab43a43
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 30 deletions.
20 changes: 20 additions & 0 deletions trunk/src/clusterization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "clusterization.h"

void clusterization::next(const T_ptr& t) {
std::map<T_ptr, Cont, less>::iterator it = _continuations.find(t);
if (it == _continuations.end()) {
Cont cont = category::kleisli::sink<T_ptr>::continuation().clone();
_continuations.insert(std::make_pair(t, cont));
cont->next(t);
} else {
it->second->next(t);
}
}

void clusterization::stop() {
std::map<T_ptr, Cont, less>::iterator it = _continuations.begin();
for (; it != _continuations.end(); ++it) {
it->second->stop();
}
_continuations.clear();
}
38 changes: 15 additions & 23 deletions trunk/src/clusterization.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,27 @@
#include <map>

#include "kleisli.h"
#include "file_types/base.h"

template<typename T>
class clusterization: public category::kleisli::arr<boost::shared_ptr<T>, boost::shared_ptr<T> > {
typedef boost::shared_ptr<T> T_ptr;
class clusterization: public category::kleisli::arr
<boost::shared_ptr<file_type::base>, boost::shared_ptr<file_type::base> > {
typedef boost::shared_ptr<file_type::base> T_ptr;
typedef boost::shared_ptr<category::kleisli::end<T_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<category::kleisli::end<T_ptr> > Cont;
typedef std::map<T_ptr, Cont, less> map;
map _continuations;
std::map<T_ptr, Cont, less> _continuations;
public:
void next(const T_ptr& t) {
typename std::map<T_ptr, Cont, less>::iterator it = _continuations.find(t);
if (it == _continuations.end()) {
Cont cont = category::kleisli::sink<T_ptr>::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
2 changes: 1 addition & 1 deletion trunk/src/default_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void default_main(const program_options& po) {
>>= fs::recursive()
>>= (po.extensions().empty() ? The< arr<fs::path, fs::path> >() : The<elem_filter>(po.extensions()))
>>= file_typer_match_first()
>>= clusterization<file_type::base>()
>>= clusterization()
>>= comparator<file_type::base, file_type::base>()
>>= accumulator<file_type::base>()
>>= output;
Expand Down
9 changes: 5 additions & 4 deletions trunk/src/file_types/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> base::try_file(const fs::path& file) {
return boost::make_shared<base>(file);
}

boost::shared_ptr<base> base::compare(const boost::shared_ptr<base>& a) const {
if (fs::file_size(_path) != fs::file_size(a->_path)) {
return boost::shared_ptr<base>();
}
const int buf_size = 4096;
char buf1[buf_size], buf2[buf_size];
fs::ifstream file1(_path), file2(a->_path);
Expand All @@ -34,4 +31,8 @@ boost::shared_ptr<base> base::compare(const boost::shared_ptr<base>& a) const {
}
}

inline comparison_result base::precompare(const boost::shared_ptr<base>& a) const {
return _size < a->_size ? less : _size == a->_size ? equal : greater;
}

}
9 changes: 8 additions & 1 deletion trunk/src/file_types/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<base> try_file(const fs::path& file);
virtual boost::shared_ptr<base> compare(const boost::shared_ptr<base>& a) const;
virtual comparison_result precompare(const boost::shared_ptr<base>& a) const;
};

}
Expand Down
4 changes: 4 additions & 0 deletions trunk/src/file_types/img.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ boost::shared_ptr<base> img::compare(const boost::shared_ptr<base>& a) const {
return boost::shared_ptr<img>();
}

inline comparison_result img::precompare(const boost::shared_ptr<base>& a) const {
return equal;
}

}
1 change: 1 addition & 0 deletions trunk/src/file_types/img.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct img: base {
img(const fs::path& p): base(p) {}
static boost::shared_ptr<img> try_file(const boost::shared_ptr<base>& file);
boost::shared_ptr<base> compare(const boost::shared_ptr<base>& a) const;
comparison_result precompare(const boost::shared_ptr<base>& a) const;
};

}
Expand Down
4 changes: 4 additions & 0 deletions trunk/src/file_types/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ boost::shared_ptr<base> text::compare(const boost::shared_ptr<base>& a) const {
}
}

inline comparison_result text::precompare(const boost::shared_ptr<base>& a) const {
return equal;
}

}
1 change: 1 addition & 0 deletions trunk/src/file_types/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct text: base {
text(const fs::path& p): base(p) {}
static boost::shared_ptr<text> try_file(const boost::shared_ptr<base>& file);
boost::shared_ptr<base> compare(const boost::shared_ptr<base>& a) const;
comparison_result precompare(const boost::shared_ptr<base>& a) const;
};

}
Expand Down
1 change: 0 additions & 1 deletion trunk/todo.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
precompare !!!

0 comments on commit ab43a43

Please sign in to comment.