tikimyster

lab2.cpp

Oct 11th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1.  
  2. #include "lab2.h"
  3. void WordCount::parse(string &sWord)
  4. {
  5.     int i=0, iSize = 0;
  6.     string sWord2;
  7.     iSize = sWord.length();
  8.     while(i <= iSize)
  9.     {
  10.         if(isalnum(sWord[i]))
  11.             sWord2+= tolower(sWord[i]);
  12.         i++;
  13.     }
  14.     sWord = sWord2;
  15. }
  16. void WordCount::ReadFile(ifstream &input, string sWord, vector<pair<string,int> > &vec)
  17. {
  18.     while(input >> sWord)
  19.     {
  20.         parse(sWord);
  21.         if(MapOfWords.count(sWord))
  22.         { MapIt=MapOfWords.find(sWord);
  23.             MapIt->second++;
  24.         }
  25.         else
  26.             MapOfWords.insert((pair<string,int>(sWord,1)));
  27.     }
  28.     for(MapIt=MapOfWords.begin(); MapIt!=MapOfWords.end(); MapIt++)
  29.     {
  30.         string tmp = MapIt->first;
  31.         if(!isalnum(tmp[0]))
  32.         {
  33.         }
  34.         else
  35.             vec.push_back(*MapIt);
  36.     }
  37.  
  38. }
  39. void WordCount::print(vector<pair<string,int> > &vec)
  40. {
  41.     for(VecIt=vec.begin(); VecIt != vec.end(); VecIt++)
  42.         cout << VecIt->first << " " << VecIt->second << endl;
  43. }
  44. void WordCount::quicksort(string KEY_VALUE, int left, int right, vector<pair<string, int> > &vec)
  45. {
  46.     int i = left, j = right;
  47.     //  srand (time(NULL ));
  48.     //  int pivot = rand() % ((right - left));
  49.     int pivot = (left+right)/2;
  50.     if(KEY_VALUE == "key") //sort by key string
  51.     {
  52.         //partition
  53.         while(i < j )
  54.         {
  55.             //moving from the left to the right untill the pivot is no longer greater than the vec [i]
  56.             while(vec[i].first < vec[pivot].first)
  57.                 i++;
  58.             //moving from right to the left of the pivot so that the vec [j] is no longer greater than the pivot
  59.             while(vec[j].first > vec[pivot].first)
  60.                 j--;
  61.             if(i < j)
  62.             {
  63.                 swap(vec[i], vec[j]);
  64.                 i++;
  65.                 j--;
  66.             }
  67.         }
  68.         //recursion
  69.         if (left < j)
  70.             quicksort(KEY_VALUE, left, j-1, vec);
  71.         if (i < right)
  72.             quicksort(KEY_VALUE, i+1, right, vec);
  73.     }
  74.     else //sort by Values
  75.     {
  76.         while(i < j ) //partition
  77.         {
  78.             while((vec[i].second <= vec[pivot].second))
  79.             {
  80.                 if(vec[i].second == vec[pivot].second)
  81.                 {
  82.                     if(vec[i].first < vec[pivot].first)
  83.                         {}
  84.                     else
  85.                         break;
  86.                 }
  87.                 i++;
  88.             }
  89.             while((vec[j].second > vec[pivot].second))
  90.                 j--;
  91.             if(i < j)
  92.             {
  93.                 swap(vec[i], vec[j]);
  94.                 i++;
  95.                 j--;
  96.             }
  97.         }
  98.     }
  99.     //recursion
  100.     if (left < j)
  101.         quicksort(KEY_VALUE, left, j-1, vec);
  102.     if (i < right)
  103.         quicksort(KEY_VALUE, i+1, right, vec);
  104. }
  105. void WordCount::insert_sort(string KEY_VALUE, vector<pair<string,int> > &vec)
  106. {
  107.     int size = vec.size();
  108.     int j = 0;
  109.     pair<string,int> temp;
  110.     if(KEY_VALUE == "key")
  111.     {
  112.         for(int i = 1; i < size; i++)
  113.         {
  114.             temp = vec[i];
  115.             j = i;
  116.             while((j > 0) && (vec[j-1].first > temp.first))
  117.             {
  118.                 vec[j] = vec[j-1];
  119.                 j--;
  120.             }
  121.             vec[j] = temp;
  122.         }
  123.     }
  124.     else
  125.     {
  126.         for(int i = 1; i < size; i++)
  127.         {
  128.             temp = vec[i];
  129.             j = i;
  130.             while((j > 0) && (vec[j-1].second > temp.second))
  131.             {
  132.                 vec[j] = vec[j-1];
  133.                 j--;
  134.             }
  135.             vec[j] = temp;
  136.         }
  137.     }
  138. }
  139.  
  140. void WordCount::getmyvec(vector<pair<string, int> > &avector)
  141. {
  142.     avector = myVector;
  143. }
  144. void WordCount::setmyvec(vector<pair<string, int> > vec)
  145. {
  146.     myVector = vec;
  147. }
  148.  
  149. /*void WordCount::merge_sort(string KEY_VALUE, int left, int right)
  150.     {  
  151.     int middle;
  152.     if( left < right){
  153.     middle = (left + right)/2 ;
  154.     merge_sort(KEY_VALUE,left,middle);
  155.     merge_sort(KEY_VALUE,middle + 1, right);
  156.     merge(KEY_VALUE, left, middle, right); 
  157.     }
  158.     }*/
  159.  
  160. void WordCount::merge(string KEY_VALUE,vector<pair<string,int> > &sortvec)
  161. {
  162.     if(sortvec.size() < 2){}
  163.     else
  164.     {
  165.         vector<pair<string,int> > leftVec;
  166.         vector<pair<string,int> > rightVec;
  167.  
  168.         int size = sortvec.size();
  169.         int middle = size /2;
  170.  
  171.         for(int i = 0; i < middle; i++) //set everything on the left side of middle to the left vector
  172.             leftVec.push_back(sortvec[i]);
  173.  
  174.         for(int i = middle; i < size; i++) // everything on the right side of middle to the right vector
  175.             rightVec.push_back(sortvec[i]);
  176.  
  177.         merge(KEY_VALUE, leftVec);
  178.         merge(KEY_VALUE, rightVec);
  179.  
  180.         int leftSize = leftVec.size(), rightSize = rightVec.size();
  181.         int rightIndex = 0, leftIndex = 0, myVectorIndex = 0; //indexes so i no where i am in the array err vector
  182.         //--------------sort by keys-------------------//  
  183.  
  184.         if(KEY_VALUE == "key")
  185.         {
  186.             while(leftIndex < leftSize && rightIndex < rightSize) //left and right index starts at 0
  187.             {
  188.                 if(leftVec[leftIndex].first < rightVec[rightIndex].first)
  189.                     sortvec[myVectorIndex++] = leftVec[leftIndex++];
  190.                 else
  191.                     sortvec[myVectorIndex++] = rightVec[rightIndex++];
  192.             }
  193.             if(leftIndex == leftSize) //if left is completley sorted
  194.             {
  195.                 while(rightIndex < rightSize)
  196.                     sortvec[myVectorIndex++] = rightVec[rightIndex++];
  197.             }
  198.             else if(rightIndex == rightSize)
  199.             {
  200.                 while(leftIndex < leftSize)
  201.                     sortvec[myVectorIndex++] = leftVec[leftIndex++];
  202.             }
  203.         }
  204.  
  205.         //------------------ sort by values ------------- //
  206.  
  207.         else //sorting values
  208.         {
  209.             while(leftIndex < leftSize && rightIndex < rightSize)
  210.             {
  211.                 if(leftVec[leftIndex].second < rightVec[rightIndex].second) //merge cause left side is less than right
  212.                     sortvec[myVectorIndex++] = leftVec[leftIndex++];
  213.                 else if(leftVec[leftIndex].second == rightVec[rightIndex].second) //both values are the same so we have to check there key to detrmin which to sort
  214.                 {
  215.                     if(leftVec[leftIndex].first < rightVec[rightIndex].first)
  216.                         sortvec[myVectorIndex++] = leftVec[leftIndex++];
  217.                     else
  218.                         sortvec[myVectorIndex++] = rightVec[rightIndex++];
  219.                 }
  220.                 else //left has to be bigger because we checked all the other cases
  221.                     sortvec[myVectorIndex++] = rightVec[rightIndex++];
  222.             }
  223.             if(leftIndex == leftSize) //now in the right side
  224.             {
  225.                 while(rightIndex < rightSize)
  226.                     sortvec[myVectorIndex++] = rightVec[rightIndex++];
  227.             }
  228.             else if(rightIndex == rightSize) //now in the left side
  229.             {
  230.                 while(leftIndex < leftSize)
  231.                     sortvec[myVectorIndex++] = leftVec[leftIndex++];
  232.             }
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment