refactor: extract WordRefList and thread finder
This commit is contained in:
parent
48f05ddb4f
commit
95cc7223e8
8 changed files with 84 additions and 63 deletions
|
@ -5,8 +5,8 @@
|
|||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using std::mutex, std::vector, std::thread, std::lock_guard, std::string,
|
||||
std::forward_list, std::string_view;
|
||||
using std::mutex, std::vector, std::thread, std::string, std::forward_list,
|
||||
std::string_view;
|
||||
|
||||
GroupedFinder::GroupedFinder(const WordList &word_list) {
|
||||
for (const auto &word : word_list) {
|
||||
|
@ -15,8 +15,8 @@ GroupedFinder::GroupedFinder(const WordList &word_list) {
|
|||
}
|
||||
|
||||
std::forward_list<const std::string *>
|
||||
GroupedFinder::find_prefix(std::string_view search_term) const {
|
||||
const auto group = groups_.find(search_term.front());
|
||||
GroupedFinder::find_prefix(std::string_view search_prefix) const {
|
||||
const auto group = groups_.find(search_prefix.front());
|
||||
if (group == groups_.cend()) {
|
||||
return {};
|
||||
}
|
||||
|
@ -27,41 +27,26 @@ GroupedFinder::find_prefix(std::string_view search_term) const {
|
|||
const auto thread_count =
|
||||
std::min<size_t>(std::thread::hardware_concurrency(), word_list_size);
|
||||
|
||||
forward_list<const string *> matching_words;
|
||||
mutex matching_words_mutex;
|
||||
forward_list<const string *> result;
|
||||
mutex result_mutex;
|
||||
|
||||
vector<thread> 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 =
|
||||
const size_t first_index = thread_index * (word_list_size / thread_count);
|
||||
|
||||
const size_t last_index =
|
||||
(thread_index == thread_count - 1)
|
||||
? word_list_size
|
||||
: (thread_index + 1) * (word_list_size / thread_count);
|
||||
|
||||
search_threads.emplace_back(
|
||||
[](const vector<const string *> &word_list,
|
||||
const string_view &search_term, forward_list<const string *> &result,
|
||||
size_t start_index, size_t end_index, mutex &result_mutex) {
|
||||
forward_list<const string *> 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<mutex> 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));
|
||||
WordRefList::find_prefix_in_range, cref(word_list), cref(search_prefix),
|
||||
first_index, last_index, ref(result), ref(result_mutex));
|
||||
}
|
||||
|
||||
for (auto &thread : search_threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
return matching_words;
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue