Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "passpattern.h"
- struct PassPattern::pattern {
- pattern() = default;
- pattern(const std::string &pat_, int64_t count_) : pat{ pat_ }, count{ count_ } {}
- // is there really a need to make these private?
- std::string pat{};
- int64_t count{};
- };
- struct PassPattern::patternSorter { // comparator for sorting the patterns by occurance count
- inline bool operator()(const pattern &p1, const pattern &p2) {
- return p1.count > p2.count;
- }
- };
- PassPattern::PassPattern(const std::string& filename) : fname{filename}{
- setCharType("abcdefghijklmnopqrstuvwxyz", 'l');
- setCharType("ABCDEFGHIJKLMNOPQRSTUVWXY", 'L');
- setCharType("0123456789", 'n');
- setCharType("!@#$%^&*()", 'N');
- setCharType("`-=[]\\;',./", 's');
- setCharType("~_+{}|:\"<>?", 'S');
- }
- void PassPattern::write(const std::string &filename, const std::vector<pattern> &vec, char delim = ':') {
- std::ofstream fout(filename, std::ofstream::out);
- if (fout.good())
- for (auto it = vec.begin(); it != vec.end(); ++it)
- fout << it->pat << delim << it->count << std::endl;
- }
- std::string PassPattern::filename() {
- return fname; // will never need this but whatever
- }
- inline bool PassPattern::exists(const std::string &fname) {
- std::ifstream tester(fname);
- bool ret = tester.good();
- return tester.close(), ret;
- }
- std::vector<PassPattern::pattern> PassPattern::parse() {
- std::ifstream fin(fname, std::ifstream::in); // input file stream for reading the file
- std::unordered_map<std::string, int64_t> pmap; // map of each pattern to the number of each occurance
- std::string tmp; // temporary string to hold the current line from the text file
- while (std::getline(fin, tmp)){
- for (auto it = tmp.begin(); it != tmp.end(); ++it) // should not be a const iterator... characters are replaced
- *it = getCharType(*it); // get the pattern string for the character and replace it
- pmap[tmp]++; // increment the occurances of the pattern in the pmap
- }
- std::vector<pattern> sorted; // empty vector for now
- for (auto it = pmap.begin(); it != pmap.end(); ++it) // loop through each of the patterns in the map ...
- sorted.push_back(pattern(it->first, it->second)); // ... and add them to the vector as a pattern struct
- std::sort(sorted.begin(),sorted.end(),patternSorter()); // sort em with a comparator. Lambdas are good, too...
- /*
- If you want a lambda for aesthetic or sexual reasons, use:
- std::sort(
- sorted.begin(),
- sorted.end(),
- [](const pattern &p1, const pattern &p2){ return p1.count > p2.count; }
- );
- */
- return sorted;
- }
- inline void PassPattern::setCharType(const std::string& chars, char c) {
- for (auto it = chars.begin(); it != chars.end(); ++it)
- charmap[*it] = c;
- }
- inline char PassPattern::getCharType(char c) {
- auto it = charmap.find(c); // look for character in the charmap
- if (it == charmap.end()) // if it does not exist ...
- return '?'; // return a ?
- return it->second; // otherwise, return its respective character
- }
- int main() {
- PassPattern pp("file.txt");
- auto vec = pp.parse();
- pp.write("output.txt", vec);
- for (auto it = vec.begin(); it != vec.begin() + 10; ++it)
- std::cout << it->pat << ":" << it->count << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment