Skip to content

Commit

Permalink
clusterization added
Browse files Browse the repository at this point in the history
git-svn-id: https://projectname.googlecode.com/svn/trunk@9 c416075f-80b4-e980-4839-00ea3ed24e77
  • Loading branch information
valery.isaev@gmail.com committed Mar 21, 2011
1 parent 87d1e30 commit a397a36
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
25 changes: 22 additions & 3 deletions trunk/src/clusterization.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
#ifndef _CLUSTERIZATION_H_
#define _CLUSTERIZATION_H_

#include <boost/shared_ptr.hpp>
#include <typeinfo>
#include <map>

#include "kleisli.h"

template<typename T>
class clusterization: public list_arr<T, T> {
class clusterization: public category::kleisli::arr<boost::shared_ptr<T>, boost::shared_ptr<T> > {
typedef boost::shared_ptr<T> T_ptr;
struct less {
bool operator()(const T_ptr& t1, const T_ptr& t2) {
return typeid(*t1).before(typeid(*t2));
}
};
typedef boost::shared_ptr<category::kleisli::end<T_ptr> > Cont;
std::map<T_ptr, Cont, less> _continuations;
public:
void next(const T& t) {
(*this)(t);
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);
}
}
};

Expand Down
12 changes: 8 additions & 4 deletions trunk/src/default_main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <boost/shared_ptr.hpp>

#include "clusterization.h"
#include "program_options.h"
#include "filesystem.h"
#include "kleisli.h"
Expand All @@ -22,14 +23,17 @@ class elem_filter: public arr<fs::path, fs::path> {

template<typename T>
class comparator: public category::kleisli::arr<boost::shared_ptr<T>, file_type::compare_result> {
std::vector< boost::shared_ptr<T> > values;
std::vector< boost::shared_ptr<T> > _values;
public:
void next(const boost::shared_ptr<T>& t) {
typedef typename std::vector<boost::shared_ptr<T> >::iterator iterator;
for (iterator it = values.begin(); it != values.end(); ++it) {
for (iterator it = _values.begin(); it != _values.end(); ++it) {
(*it)->compare(*t, sink<file_type::compare_result>::continuation());
}
values.push_back(t);
_values.push_back(t);
}
boost::shared_ptr< end< boost::shared_ptr<T> > > clone() const {
return boost::make_shared< comparator<T> >(*this);
}
};

Expand All @@ -38,7 +42,7 @@ void default_main(const program_options& po) {
>>= The<fs::recursive>()
>>= (po.extensions().empty() ? The< arr<fs::path, fs::path> >() : The<elem_filter>(po.extensions()))
>>= The<file_typer_match_first>()
// >>= The<clusterization<file_type::base> >()
>>= clusterization<file_type::base>()
>>= The<comparator<file_type::base> >()
>>= The<iterator_end, file_type::compare_result>
(std::ostream_iterator<file_type::compare_result>(std::cout, "\n"));
Expand Down
1 change: 0 additions & 1 deletion trunk/src/file_types/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <boost/make_shared.hpp>
#include <cstring>
#include <cassert>
#include <iostream>

#include "base.h"
#include "../kleisli.h"
Expand Down
2 changes: 0 additions & 2 deletions trunk/src/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <iterator>

#include "filesystem.h"
#include "logger.h"

Expand Down
6 changes: 6 additions & 0 deletions trunk/src/kleisli.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef _KLEISLI_H_
#define _KLEISLI_H_

#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

namespace category {
namespace kleisli {

Expand All @@ -13,11 +16,13 @@ template<template<typename S, typename T, typename U> class R, typename S, typen
template<typename S>
struct end {
virtual void next(const S&) = 0;
virtual boost::shared_ptr< end<S> > clone() const { return boost::shared_ptr< end<S> >(); }
};

template<typename S>
struct empty: end<S> {
void next(const S&) {}
boost::shared_ptr< end<S> > clone() const { return boost::make_shared< empty<S> >(); }
};

template<typename V>
Expand Down Expand Up @@ -67,6 +72,7 @@ class iterator_end: public end<V> {
public:
iterator_end(const Iter& i): it(i) {}
void next(const V& value) { *it++ = value; }
boost::shared_ptr< end<V> > clone() const { return boost::make_shared< iterator_end<V, Iter> >(it); }
};

}}
Expand Down
1 change: 0 additions & 1 deletion trunk/src/program_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define _PROGRAM_OPTIONS_H_

#include <vector>
#include <iostream>

#include "logger.h"

Expand Down
5 changes: 4 additions & 1 deletion trunk/src/type_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ template<typename H, typename T>
struct cons {
typedef H head;
typedef T tail;
enum { size = T::size + 1 };
};
struct nil {
enum { size = 0 };
};
struct nil;

template<typename T>
struct type_traits {
Expand Down

0 comments on commit a397a36

Please sign in to comment.