Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <map>
  4. #include <vector>
  5.  
  6. template <typename K, typename V, typename R> class IMapReduce
  7. {
  8.     public:
  9.         virtual ~IMapReduce() {}
  10.         virtual void map( K key, V values ) = 0;
  11.         virtual std::map<K, R> reduce() = 0;
  12. };
  13.  
  14.  
  15. #pragma once
  16. #include "imapreduce.h"
  17.  
  18. #include <string>
  19. #include <map>
  20. #include <vector>
  21.  
  22.  
  23. class WordCount : public IMapReduce<std::string, std::string, int>
  24. {
  25. public:
  26.     WordCount(void);
  27.     ~WordCount(void);
  28.  
  29.     virtual void map( std::string key, std::string values );
  30.     virtual std::map<std::string, int> reduce();
  31. private:
  32.     std::map<std::string, int> m;
  33.     std::vector<std::pair<std::string, int>> preReduce;
  34. };
  35.  
  36. #include "WordCount.h"
  37.  
  38. WordCount::WordCount()
  39. {}
  40.  
  41. WordCount::~WordCount()
  42. {}
  43.  
  44. void WordCount::map( std::string key, std::string values )
  45. {
  46.     unsigned int cur = 0;
  47.    
  48.     while( cur < values.length() )
  49.     {
  50.         int next = values.find( ' ', cur );
  51.  
  52.         if( next == - 1)
  53.         {
  54.             preReduce.insert( preReduce.begin(), std::pair<std::string, int>( values.substr( cur ), 1 ) );
  55.             break;
  56.         }
  57.         else
  58.             preReduce.insert( preReduce.begin(), std::pair<std::string, int>( values.substr( cur, next - cur ), 1 ) );
  59.  
  60.         cur += next - cur + 1;
  61.     }
  62. }
  63.  
  64. std::map<std::string, int> WordCount::reduce()
  65. {
  66.     if( preReduce.size() <= 0 )
  67.     {
  68.         return m;
  69.     }
  70.  
  71.     std::vector<std::pair<std::string, int>>::iterator it;
  72.    
  73.     it = preReduce.begin();
  74.  
  75.     for( std::vector<std::pair<std::string, int>>::iterator it = preReduce.begin(); it < preReduce.end(); it++ )
  76.     {
  77.         if( m.find( (*it).first ) == m.end() )
  78.         {
  79.             m.insert( *it );
  80.         }
  81.         else
  82.         {
  83.             (*m.find( (*it).first )).second++;
  84.         }
  85.     }
  86.  
  87.     return m;
  88. }
  89.  
  90. #include "WordCount.h"
  91.  
  92. #include <iostream>
  93.  
  94. using namespace std;
  95.  
  96. int main( char **args, int argc )
  97. {
  98.     WordCount test;
  99.  
  100.     test.map( "cool", "a a b c b cool people are nice to me and they are nice to other people as well a a a a a a a" );
  101.  
  102.     map<std::string, int> m = test.reduce();
  103.  
  104.     map<std::string, int>::iterator it;
  105.  
  106.     for( it = m.begin(); it != m.end(); it++ )
  107.         cout << (*it).first << "\t: " << (*it).second << endl;
  108.  
  109.     char t[12];
  110.  
  111.     cin >> t;
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement