Advertisement
GoodiesHQ

Uniqifier

May 11th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <algorithm>
  2. #include <vector>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <unordered_map>
  7. #include <unordered_set>
  8.  
  9. using std::vector;
  10. using std::string;
  11. using std::unordered_map;
  12. using std::unordered_set;
  13.  
  14. class uniqify {
  15.     unordered_map<char, unordered_set<string>> maps;
  16.     //mutable unsigned long long linecount{ 0 };
  17.  
  18. public:
  19.  
  20.     inline bool exists(const char *filename) const noexcept {
  21.         std::ifstream file(filename);
  22.         bool good = file.good();
  23.         return file.close(), good;
  24.     }
  25.  
  26.     inline bool exists(const string &filename) const noexcept {
  27.         return exists(filename.c_str());
  28.     }
  29.  
  30.     vector<string> validate(const vector<string> &wordlists) const noexcept {
  31.         vector<string> available(wordlists.size());
  32.         std::copy_if(wordlists.cbegin(), wordlists.cend(), available.begin(), [this](string file) {return exists(file.c_str()); });
  33.         return available;
  34.     }
  35.  
  36.     void read(const string &filename) noexcept {
  37.         std::ifstream input(filename);
  38.         string line;
  39.         while (std::getline(input, line)) {
  40.             if (line != "") {
  41.                 linecount += 1;
  42.                 maps[line.c_str()[0]].insert(line);
  43.                 //if (!(linecount % 1000000))
  44.                 //  std::cout << "Line count: " << linecount << std::endl;
  45.             }
  46.         }
  47.     }
  48.  
  49.     void read(const vector<string> &wordlists) noexcept{
  50.         for (auto list : wordlists)
  51.             read(list);
  52.     }
  53.  
  54.     void write(const char *filename) const noexcept {
  55.         std::ofstream fout(filename);
  56.         for (auto m : maps) {
  57.             for (auto s : m.second) {
  58.                 fout << s << "\n";
  59.             }
  60.         }
  61.         fout.close();
  62.     }
  63.  
  64.     void write(const string &filename) const noexcept {
  65.         write(filename.c_str());
  66.     }
  67.  
  68.     inline void clear() {
  69.         maps.clear();
  70.     }
  71. };
  72.  
  73. int main(int argc, char **argv) {
  74.     uniqify u;
  75.     vector<string> lists;
  76.     for (auto i = 2; i < argc; ++i)
  77.         lists.push_back(argv[i]);
  78.     lists.swap(u.validate(lists));
  79.     if (!lists.size())
  80.         return std::cout << "No valid wordlists provided.\n", 0;
  81.     u.read(lists);
  82.     u.write(argv[1]);
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement