refactor: clean up word list generator

This commit is contained in:
mandlm 2024-03-20 16:51:46 +01:00
parent 6ba52ab73f
commit 7f18d0d6cc
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
4 changed files with 10 additions and 11 deletions

View File

@ -33,7 +33,7 @@ void MainWindow::setupAlgorithmSelector() {
void MainWindow::generateWordList() { void MainWindow::generateWordList() {
Timer timer; Timer timer;
word_list_ = WordListGenerator().generate(); word_list_ = WordListGenerator::generate();
timer.stop(); timer.stop();
std::stringstream status_message; std::stringstream status_message;

View File

@ -8,5 +8,6 @@ private:
static const std::string charset_; static const std::string charset_;
public: public:
std::vector<std::string> generate(); WordListGenerator() = delete;
static std::vector<std::string> generate(const size_t multiplier = 1);
}; };

View File

@ -6,18 +6,16 @@
const std::string WordListGenerator::charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const std::string WordListGenerator::charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::vector<std::string> WordListGenerator::generate() { std::vector<std::string> WordListGenerator::generate(const size_t multiplier) {
const size_t multiplier = 1; std::vector<std::string> word_list;
word_list.reserve(multiplier * std::pow(charset_.length(), 4));
std::vector<std::string> result;
result.reserve(multiplier * std::pow(charset_.length(), 4));
for (auto char_1 : charset_) { for (auto char_1 : charset_) {
for (auto char_2 : charset_) { for (auto char_2 : charset_) {
for (auto char_3 : charset_) { for (auto char_3 : charset_) {
for (auto char_4 : charset_) { for (auto char_4 : charset_) {
for (auto i = 0; i < multiplier; ++i) { for (auto i = 0; i < multiplier; ++i) {
result.emplace_back( word_list.emplace_back(
std::initializer_list<char>({char_1, char_2, char_3, char_4})); std::initializer_list<char>({char_1, char_2, char_3, char_4}));
} }
} }
@ -28,7 +26,7 @@ std::vector<std::string> WordListGenerator::generate() {
std::random_device random_device; std::random_device random_device;
std::mt19937 random_number_generator(random_device()); std::mt19937 random_number_generator(random_device());
std::shuffle(result.begin(), result.end(), random_number_generator); std::shuffle(word_list.begin(), word_list.end(), random_number_generator);
return result; return word_list;
} }

View File

@ -14,7 +14,7 @@ vector<string> generate_word_list() {
cout << "\ngenerating word list" << endl; cout << "\ngenerating word list" << endl;
Timer generator_timer; Timer generator_timer;
auto word_list = WordListGenerator().generate(); auto word_list = WordListGenerator::generate(5);
generator_timer.stop(); generator_timer.stop();
cout << "word list generator took " << generator_timer << endl; cout << "word list generator took " << generator_timer << endl;