00001 #ifndef REDUCER_HPP
00002 #define REDUCER_HPP
00003
00005 template<class KeyA, class A, class KeyB, class B, class C>
00006 class SeqMR_Reducer : public Reducer<KeyA,A,KeyB,B,C>
00007 {
00008 public:
00010 SeqMR_Reducer(MapReduce<KeyA,A,KeyB,B,C> * _mapReduce):mapReduce(_mapReduce){};
00012 #ifndef MRI_USE_MULTIMAP
00013 virtual vector< pair<KeyB,C> > run( vector< pair<KeyB,B> > & _mapped) {
00014 multimap<KeyB,B> mapped;
00015 for (int i = 0 ; i < _mapped.size() ; i++) mapped.insert(_mapped[i]);
00016 #else
00017 virtual vector< pair<KeyB,C> > run( multimap<KeyB,B> & mapped) {
00018 #endif
00019 vector< pair<KeyB,C> > reduced;
00020
00021 typename std::multimap<KeyB,B>::iterator it = mapped.begin();
00022 KeyB lastKey = (*it).first;
00023 vector<B> toReduce;
00024 for( ; it != mapped.end() ; ++it)
00025 {
00026 if ((*it).first != lastKey) {
00027 pair<KeyB,C> r = reduce( pair<KeyB, vector<B> >( lastKey , toReduce ) ); toReduce.clear();
00028 reduced.push_back( r );
00029 lastKey = (*it).first;
00030 }
00031 toReduce.push_back( (*it).second );
00032 }
00033
00034 pair<KeyB,C> r = reduce( pair<KeyB, vector<B> >( lastKey , toReduce ) ); toReduce.clear();
00035 reduced.push_back( r );
00036
00037 return reduced;
00038 };
00040 virtual pair<KeyB,C> reduce( pair<KeyB, vector<B> > ) = 0 ;
00041 protected:
00042 MapReduce<KeyA,A,KeyB,B,C> * mapReduce;
00043 };
00044
00046 class SeqMR_StrReducer : public SeqMR_Reducer<string,string,string,string,string>
00047 {
00048 public:
00050 SeqMR_StrReducer(MapReduce<string,string,string,string,string> * _mapReduce)
00051 :SeqMR_Reducer<string,string,string,string,string>(_mapReduce){};
00052 };
00053
00054 #endif