GoodiesHQ

PassPattern.cpp

Feb 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include "passpattern.h"
  2.  
  3. struct PassPattern::pattern {
  4.     pattern() = default;
  5.     pattern(const std::string &pat_, int64_t count_) : pat{ pat_ }, count{ count_ } {}
  6.  
  7.     // is there really a need to make these private?
  8.     std::string pat{};
  9.     int64_t count{};
  10. };
  11.  
  12. struct PassPattern::patternSorter { // comparator for sorting the patterns by occurance count
  13.     inline bool operator()(const pattern &p1, const pattern &p2) {
  14.         return p1.count > p2.count;
  15.     }
  16. };
  17.  
  18. PassPattern::PassPattern(const std::string& filename) : fname{filename}{
  19.     setCharType("abcdefghijklmnopqrstuvwxyz", 'l');
  20.     setCharType("ABCDEFGHIJKLMNOPQRSTUVWXY", 'L');
  21.     setCharType("0123456789", 'n');
  22.     setCharType("!@#$%^&*()", 'N');
  23.     setCharType("`-=[]\\;',./", 's');
  24.     setCharType("~_+{}|:\"<>?", 'S');
  25. }
  26.  
  27. void PassPattern::write(const std::string &filename, const std::vector<pattern> &vec, char delim = ':') {
  28.     std::ofstream fout(filename, std::ofstream::out);
  29.     if (fout.good())
  30.         for (auto it = vec.begin(); it != vec.end(); ++it)
  31.             fout << it->pat << delim << it->count << std::endl;
  32. }
  33.  
  34. std::string PassPattern::filename() {
  35.     return fname; // will never need this but whatever
  36. }
  37.  
  38. inline bool PassPattern::exists(const std::string &fname) {
  39.     std::ifstream tester(fname);
  40.     bool ret = tester.good();
  41.     return tester.close(), ret;
  42. }
  43.  
  44. std::vector<PassPattern::pattern> PassPattern::parse() {
  45.     std::ifstream fin(fname, std::ifstream::in);            // input file stream for reading the file
  46.     std::unordered_map<std::string, int64_t> pmap;          // map of each pattern to the number of each occurance
  47.     std::string tmp;                                        // temporary string to hold the current line from the text file
  48.     while (std::getline(fin, tmp)){
  49.         for (auto it = tmp.begin(); it != tmp.end(); ++it)  // should not be a const iterator... characters are replaced
  50.             *it = getCharType(*it);                         // get the pattern string for the character and replace it
  51.         pmap[tmp]++;                                        // increment the occurances of the pattern in the pmap
  52.     }
  53.     std::vector<pattern> sorted;                            // empty vector for now
  54.     for (auto it = pmap.begin(); it != pmap.end(); ++it)    // loop through each of the patterns in the map ...
  55.         sorted.push_back(pattern(it->first, it->second));   // ... and add them to the vector as a pattern struct
  56.     std::sort(sorted.begin(),sorted.end(),patternSorter()); // sort em with a comparator. Lambdas are good, too...
  57.     /*
  58.     If you want a lambda for aesthetic or sexual reasons, use:
  59.     std::sort(
  60.         sorted.begin(),
  61.         sorted.end(),
  62.         [](const pattern &p1, const pattern &p2){ return p1.count > p2.count; }
  63.     );
  64.     */
  65.     return sorted;
  66. }
  67. inline void PassPattern::setCharType(const std::string& chars, char c) {
  68.     for (auto it = chars.begin(); it != chars.end(); ++it)
  69.         charmap[*it] = c;
  70. }
  71.  
  72. inline char PassPattern::getCharType(char c) {
  73.     auto it = charmap.find(c);      // look for character in the charmap
  74.     if (it == charmap.end())        // if it does not exist ...
  75.         return '?';                 // return a ?
  76.     return it->second;              // otherwise, return its respective character
  77. }
  78.  
  79. int main() {
  80.     PassPattern pp("file.txt");
  81.     auto vec = pp.parse();
  82.     pp.write("output.txt", vec);
  83.     for (auto it = vec.begin(); it != vec.begin() + 10; ++it)
  84.         std::cout << it->pat << ":" << it->count << std::endl;
  85.     return 0;
  86. }
Add Comment
Please, Sign In to add comment