refactor: clean up
This commit is contained in:
parent
bc58ca5af8
commit
1441a869c2
1 changed files with 9 additions and 5 deletions
|
@ -7,6 +7,8 @@
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
// class Bucket
|
||||||
|
|
||||||
void Bucket::insert(const WordList &word_list, size_t first_index,
|
void Bucket::insert(const WordList &word_list, size_t first_index,
|
||||||
size_t last_index) {
|
size_t last_index) {
|
||||||
for (auto index = first_index; index < last_index; ++index) {
|
for (auto index = first_index; index < last_index; ++index) {
|
||||||
|
@ -31,6 +33,8 @@ WordRefList Bucket::find_prefix(std::string_view search_term) const {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// class BucketFinder
|
||||||
|
|
||||||
BucketFinder::BucketFinder(const WordList &word_list) { insert(word_list); }
|
BucketFinder::BucketFinder(const WordList &word_list) { insert(word_list); }
|
||||||
|
|
||||||
void BucketFinder::insert(const WordList &word_list) {
|
void BucketFinder::insert(const WordList &word_list) {
|
||||||
|
@ -47,7 +51,7 @@ void BucketFinder::insert(const WordList &word_list) {
|
||||||
|
|
||||||
std::vector<std::thread> insert_threads;
|
std::vector<std::thread> insert_threads;
|
||||||
for (auto bucket_index = 0; bucket_index < bucket_count; ++bucket_index) {
|
for (auto bucket_index = 0; bucket_index < bucket_count; ++bucket_index) {
|
||||||
auto &bucket = buckets_[bucket_index];
|
auto &thread_bucket = buckets_[bucket_index];
|
||||||
|
|
||||||
const bool is_last_bucket = bucket_index == bucket_count - 1;
|
const bool is_last_bucket = bucket_index == bucket_count - 1;
|
||||||
const size_t first_word_index = bucket_index * bucket_size;
|
const size_t first_word_index = bucket_index * bucket_size;
|
||||||
|
@ -55,7 +59,7 @@ void BucketFinder::insert(const WordList &word_list) {
|
||||||
is_last_bucket ? word_list_size : first_word_index + bucket_size;
|
is_last_bucket ? word_list_size : first_word_index + bucket_size;
|
||||||
|
|
||||||
insert_threads.emplace_back([&, first_word_index, last_word_index] {
|
insert_threads.emplace_back([&, first_word_index, last_word_index] {
|
||||||
bucket.insert(word_list, first_word_index, last_word_index);
|
thread_bucket.insert(word_list, first_word_index, last_word_index);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,9 +72,9 @@ WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
|
||||||
WordRefList search_results;
|
WordRefList search_results;
|
||||||
std::mutex search_results_mutex;
|
std::mutex search_results_mutex;
|
||||||
|
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> search_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_search_results = bucket.find_prefix(search_term);
|
||||||
if (!thread_search_results.empty()) {
|
if (!thread_search_results.empty()) {
|
||||||
const std::lock_guard result_lock(search_results_mutex);
|
const std::lock_guard result_lock(search_results_mutex);
|
||||||
|
@ -80,7 +84,7 @@ WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &thread : threads) {
|
for (auto &thread : search_threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue