From ab0613e84516d531329a10c89590f8cec03db413 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 20 Mar 2024 16:35:37 +0100 Subject: [PATCH] feat: add grouped finder --- QVectorSearch/mainwindow.cpp | 5 ++ lib_vector_search/CMakeLists.txt | 4 +- lib_vector_search/include/grouped_finder.h | 17 ++++++ lib_vector_search/src/grouped_finder.cpp | 64 ++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 lib_vector_search/include/grouped_finder.h create mode 100644 lib_vector_search/src/grouped_finder.cpp diff --git a/QVectorSearch/mainwindow.cpp b/QVectorSearch/mainwindow.cpp index 9d9652f..c44a8d0 100644 --- a/QVectorSearch/mainwindow.cpp +++ b/QVectorSearch/mainwindow.cpp @@ -2,6 +2,7 @@ #include "./ui_mainwindow.h" #include "finder.h" +#include "grouped_finder.h" #include "linear_finder.h" #include "parallel_finder.h" #include "timer.h" @@ -28,6 +29,7 @@ void MainWindow::setupAlgorithmSelector() { search_algorithms_.appendRow(new QStandardItem("Linear search")); search_algorithms_.appendRow(new QStandardItem("Parallel search")); search_algorithms_.appendRow(new QStandardItem("Tree search")); + search_algorithms_.appendRow(new QStandardItem("Grouped search")); } void MainWindow::generateWordList() { @@ -74,6 +76,9 @@ void MainWindow::createSelectedFinder() { case 2: finder_ = std::make_unique(word_list_); break; + case 3: + finder_ = std::make_unique(word_list_); + break; } } diff --git a/lib_vector_search/CMakeLists.txt b/lib_vector_search/CMakeLists.txt index 6c00ac7..be48138 100644 --- a/lib_vector_search/CMakeLists.txt +++ b/lib_vector_search/CMakeLists.txt @@ -16,7 +16,9 @@ add_library( src/parallel_finder.cpp include/parallel_finder.h src/tree_finder.cpp - include/tree_finder.h) + include/tree_finder.h + src/grouped_finder.cpp + include/grouped_finder.h) target_include_directories(vector_search PUBLIC ${PROJECT_SOURCE_DIR}/include) diff --git a/lib_vector_search/include/grouped_finder.h b/lib_vector_search/include/grouped_finder.h new file mode 100644 index 0000000..93d06ac --- /dev/null +++ b/lib_vector_search/include/grouped_finder.h @@ -0,0 +1,17 @@ +#pragma once + +#include "finder.h" + +#include +#include + +class GroupedFinder : public Finder { +private: + std::map> groups_; + +public: + GroupedFinder(const std::vector &word_list); + + virtual std::forward_list + find_prefix(std::string_view search_term) const override; +}; diff --git a/lib_vector_search/src/grouped_finder.cpp b/lib_vector_search/src/grouped_finder.cpp new file mode 100644 index 0000000..3b4243f --- /dev/null +++ b/lib_vector_search/src/grouped_finder.cpp @@ -0,0 +1,64 @@ +#include "grouped_finder.h" + +#include +#include +#include + +using std::mutex, std::vector, std::thread, std::lock_guard; + +GroupedFinder::GroupedFinder(const std::vector &word_list) { + for (const auto &word : word_list) { + groups_[word.front()].push_back(&word); + } +} + +std::forward_list +GroupedFinder::find_prefix(std::string_view search_term) const { + const auto group = groups_.find(search_term.front()); + if (group == groups_.cend()) { + return {}; + } + + const auto word_list = group->second; + const auto word_list_size = word_list.size(); + + const auto thread_count = std::thread::hardware_concurrency(); + + forward_list matching_words; + mutex matching_words_mutex; + + vector search_threads; + for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) { + const size_t first_word_index = + thread_index * (word_list_size / thread_count); + const size_t last_word_index = + (thread_index == thread_count - 1) + ? word_list_size + : (thread_index + 1) * (word_list_size / thread_count); + + search_threads.emplace_back( + [](const vector &word_list, + const string_view &search_term, forward_list &result, + size_t start_index, size_t end_index, mutex &result_mutex) { + forward_list thread_results; + for (size_t index = start_index; index < end_index; ++index) { + const auto ¤t_word = word_list[index]; + if (current_word->starts_with(search_term)) { + thread_results.push_front(current_word); + } + } + if (!thread_results.empty()) { + const lock_guard lock(result_mutex); + result.merge(thread_results); + } + }, + cref(word_list), cref(search_term), ref(matching_words), + first_word_index, last_word_index, ref(matching_words_mutex)); + } + + for (auto &thread : search_threads) { + thread.join(); + } + + return matching_words; +}