From 7f18d0d6cc2bfe8f578d6c2d8e992a194a9d4823 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 20 Mar 2024 16:51:46 +0100 Subject: [PATCH] refactor: clean up word list generator --- QVectorSearch/mainwindow.cpp | 2 +- lib_vector_search/include/word_list_generator.h | 3 ++- lib_vector_search/src/word_list_generator.cpp | 14 ++++++-------- vector_search_cli/main.cpp | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/QVectorSearch/mainwindow.cpp b/QVectorSearch/mainwindow.cpp index 8f7cdfa..1007502 100644 --- a/QVectorSearch/mainwindow.cpp +++ b/QVectorSearch/mainwindow.cpp @@ -33,7 +33,7 @@ void MainWindow::setupAlgorithmSelector() { void MainWindow::generateWordList() { Timer timer; - word_list_ = WordListGenerator().generate(); + word_list_ = WordListGenerator::generate(); timer.stop(); std::stringstream status_message; diff --git a/lib_vector_search/include/word_list_generator.h b/lib_vector_search/include/word_list_generator.h index 86c4b47..605b749 100644 --- a/lib_vector_search/include/word_list_generator.h +++ b/lib_vector_search/include/word_list_generator.h @@ -8,5 +8,6 @@ private: static const std::string charset_; public: - std::vector generate(); + WordListGenerator() = delete; + static std::vector generate(const size_t multiplier = 1); }; diff --git a/lib_vector_search/src/word_list_generator.cpp b/lib_vector_search/src/word_list_generator.cpp index e4bdf52..ba4cf06 100644 --- a/lib_vector_search/src/word_list_generator.cpp +++ b/lib_vector_search/src/word_list_generator.cpp @@ -6,18 +6,16 @@ const std::string WordListGenerator::charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -std::vector WordListGenerator::generate() { - const size_t multiplier = 1; - - std::vector result; - result.reserve(multiplier * std::pow(charset_.length(), 4)); +std::vector WordListGenerator::generate(const size_t multiplier) { + std::vector word_list; + word_list.reserve(multiplier * std::pow(charset_.length(), 4)); for (auto char_1 : charset_) { for (auto char_2 : charset_) { for (auto char_3 : charset_) { for (auto char_4 : charset_) { for (auto i = 0; i < multiplier; ++i) { - result.emplace_back( + word_list.emplace_back( std::initializer_list({char_1, char_2, char_3, char_4})); } } @@ -28,7 +26,7 @@ std::vector WordListGenerator::generate() { std::random_device 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; } diff --git a/vector_search_cli/main.cpp b/vector_search_cli/main.cpp index 37c2171..7417e9b 100644 --- a/vector_search_cli/main.cpp +++ b/vector_search_cli/main.cpp @@ -14,7 +14,7 @@ vector generate_word_list() { cout << "\ngenerating word list" << endl; Timer generator_timer; - auto word_list = WordListGenerator().generate(); + auto word_list = WordListGenerator::generate(5); generator_timer.stop(); cout << "word list generator took " << generator_timer << endl;