00001 #ifndef REDUCER_HPP
00002 #define REDUCER_HPP
00003
00005 template<class KeyA, class A, class KeyB, class B, class C>
00006 class MapMP_Reducer : public Reducer<KeyA,A,KeyB,B,C>
00007 {
00008 public:
00010 MapMP_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
00020 typename std::multimap<KeyB,B>::iterator it = mapped.begin();
00021 KeyB lastKey = (*it).first;
00022 vector< pair < KeyB,vector<B> > > toReduce(1, pair < KeyB,vector<B> >( lastKey , vector<B>() ) );
00023 for( ; it != mapped.end() ; ++it)
00024 {
00025 if ((*it).first != lastKey) {
00026 toReduce.push_back( pair< KeyB,vector<B> > ( (*it).first , vector<B>() ) );
00027 lastKey = (*it).first;
00028 }
00029 toReduce.back().second.push_back( (*it).second );
00030 }
00031
00032 vector< pair<KeyB,C> > reduced(toReduce.size());
00033
00034 #pragma omp parallel for
00035 for ( int i = 0 ; i < toReduce.size() ; i++ ) {
00036 reduced[i] = reduce( toReduce[i] );
00037 }
00038
00039 return reduced;
00040 };
00042 virtual pair<KeyB,C> reduce( pair<KeyB, vector<B> > ) = 0 ;
00043 protected:
00044 MapReduce<KeyA,A,KeyB,B,C> * mapReduce;
00045 };
00046
00048 class MapMP_StrReducer : public MapMP_Reducer<string,string,string,string,string>
00049 {
00050 public:
00052 MapMP_StrReducer(MapReduce<string,string,string,string,string> * _mapReduce)
00053 :MapMP_Reducer<string,string,string,string,string>(_mapReduce){};
00054 };
00055
00056 #endif