diff --git a/trunk/src/clusterization.h b/trunk/src/clusterization.h index 3881604..183faab 100644 --- a/trunk/src/clusterization.h +++ b/trunk/src/clusterization.h @@ -1,13 +1,32 @@ #ifndef _CLUSTERIZATION_H_ #define _CLUSTERIZATION_H_ +#include +#include +#include + #include "kleisli.h" template -class clusterization: public list_arr { +class clusterization: public category::kleisli::arr, boost::shared_ptr > { + typedef boost::shared_ptr T_ptr; + struct less { + bool operator()(const T_ptr& t1, const T_ptr& t2) { + return typeid(*t1).before(typeid(*t2)); + } + }; + typedef boost::shared_ptr > Cont; + std::map _continuations; public: - void next(const T& t) { - (*this)(t); + 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); + } } }; diff --git a/trunk/src/default_main.cpp b/trunk/src/default_main.cpp index 3009be1..cd066f3 100644 --- a/trunk/src/default_main.cpp +++ b/trunk/src/default_main.cpp @@ -1,5 +1,6 @@ #include +#include "clusterization.h" #include "program_options.h" #include "filesystem.h" #include "kleisli.h" @@ -22,14 +23,17 @@ class elem_filter: public arr { template class comparator: public category::kleisli::arr, file_type::compare_result> { - std::vector< boost::shared_ptr > values; + std::vector< boost::shared_ptr > _values; public: void next(const boost::shared_ptr& t) { typedef typename std::vector >::iterator iterator; - for (iterator it = values.begin(); it != values.end(); ++it) { + for (iterator it = _values.begin(); it != _values.end(); ++it) { (*it)->compare(*t, sink::continuation()); } - values.push_back(t); + _values.push_back(t); + } + boost::shared_ptr< end< boost::shared_ptr > > clone() const { + return boost::make_shared< comparator >(*this); } }; @@ -38,7 +42,7 @@ void default_main(const program_options& po) { >>= The() >>= (po.extensions().empty() ? The< arr >() : The(po.extensions())) >>= The() -// >>= The >() + >>= clusterization() >>= The >() >>= The (std::ostream_iterator(std::cout, "\n")); diff --git a/trunk/src/file_types/base.cpp b/trunk/src/file_types/base.cpp index 4249c30..3af6390 100644 --- a/trunk/src/file_types/base.cpp +++ b/trunk/src/file_types/base.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "base.h" #include "../kleisli.h" diff --git a/trunk/src/filesystem.cpp b/trunk/src/filesystem.cpp index 32f8431..a780b3f 100644 --- a/trunk/src/filesystem.cpp +++ b/trunk/src/filesystem.cpp @@ -1,5 +1,3 @@ -#include - #include "filesystem.h" #include "logger.h" diff --git a/trunk/src/kleisli.h b/trunk/src/kleisli.h index cc06bd0..f4c1929 100644 --- a/trunk/src/kleisli.h +++ b/trunk/src/kleisli.h @@ -1,6 +1,9 @@ #ifndef _KLEISLI_H_ #define _KLEISLI_H_ +#include +#include + namespace category { namespace kleisli { @@ -13,11 +16,13 @@ template class R, typename S, typen template struct end { virtual void next(const S&) = 0; + virtual boost::shared_ptr< end > clone() const { return boost::shared_ptr< end >(); } }; template struct empty: end { void next(const S&) {} + boost::shared_ptr< end > clone() const { return boost::make_shared< empty >(); } }; template @@ -67,6 +72,7 @@ class iterator_end: public end { public: iterator_end(const Iter& i): it(i) {} void next(const V& value) { *it++ = value; } + boost::shared_ptr< end > clone() const { return boost::make_shared< iterator_end >(it); } }; }} diff --git a/trunk/src/program_options.h b/trunk/src/program_options.h index 7686d65..a2f5a61 100644 --- a/trunk/src/program_options.h +++ b/trunk/src/program_options.h @@ -2,7 +2,6 @@ #define _PROGRAM_OPTIONS_H_ #include -#include #include "logger.h" diff --git a/trunk/src/type_list.h b/trunk/src/type_list.h index 276f95c..e60d384 100644 --- a/trunk/src/type_list.h +++ b/trunk/src/type_list.h @@ -5,8 +5,11 @@ template struct cons { typedef H head; typedef T tail; + enum { size = T::size + 1 }; +}; +struct nil { + enum { size = 0 }; }; -struct nil; template struct type_traits {