refactor: clean up
This commit is contained in:
parent
7af6ac593c
commit
06500fd69a
1 changed files with 20 additions and 20 deletions
|
@ -40,45 +40,45 @@ BucketFinder::BucketFinder(const WordList &word_list) {
|
||||||
|
|
||||||
buckets_.resize(bucket_count);
|
buckets_.resize(bucket_count);
|
||||||
|
|
||||||
std::vector<std::thread> insert_threads;
|
std::vector<std::thread> threads;
|
||||||
for (auto bucket_index = 0; bucket_index < bucket_count; ++bucket_index) {
|
for (auto bucket_index = 0; bucket_index < bucket_count; ++bucket_index) {
|
||||||
const size_t first_index = bucket_index * bucket_size;
|
|
||||||
const size_t last_index = (bucket_index == bucket_count - 1)
|
|
||||||
? word_list_size
|
|
||||||
: (bucket_index + 1) * bucket_size;
|
|
||||||
|
|
||||||
auto &bucket = buckets_[bucket_index];
|
auto &bucket = buckets_[bucket_index];
|
||||||
|
|
||||||
insert_threads.emplace_back([&bucket, &word_list, first_index, last_index] {
|
bool is_last_bucket = bucket_index == bucket_count - 1;
|
||||||
|
|
||||||
|
const size_t first_index = bucket_index * bucket_size;
|
||||||
|
const size_t last_index =
|
||||||
|
is_last_bucket ? word_list_size : first_index + bucket_size;
|
||||||
|
|
||||||
|
threads.emplace_back([&, first_index, last_index] {
|
||||||
bucket.insert(word_list, first_index, last_index);
|
bucket.insert(word_list, first_index, last_index);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &thread : insert_threads) {
|
for (auto &thread : threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
|
WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
|
||||||
WordRefList result;
|
WordRefList search_results;
|
||||||
std::mutex result_mutex;
|
std::mutex search_results_mutex;
|
||||||
|
|
||||||
std::vector<std::thread> search_threads;
|
std::vector<std::thread> threads;
|
||||||
for (const auto &bucket : buckets_) {
|
for (const auto &bucket : buckets_) {
|
||||||
|
threads.emplace_back([&] {
|
||||||
search_threads.emplace_back([&] {
|
auto thread_search_results = bucket.find_prefix(search_term);
|
||||||
auto thread_result = bucket.find_prefix(search_term);
|
if (!thread_search_results.empty()) {
|
||||||
if (!thread_result.empty()) {
|
std::lock_guard result_lock(search_results_mutex);
|
||||||
std::lock_guard<std::mutex> result_lock(result_mutex);
|
std::move(thread_search_results.begin(), thread_search_results.end(),
|
||||||
std::move(thread_result.begin(), thread_result.end(),
|
std::back_inserter(search_results));
|
||||||
std::back_inserter(result));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &thread : search_threads) {
|
for (auto &thread : threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return search_results;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue