refactor: clean up
parent
7af6ac593c
commit
06500fd69a
|
@ -40,45 +40,45 @@ BucketFinder::BucketFinder(const WordList &word_list) {
|
|||
|
||||
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) {
|
||||
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];
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
for (auto &thread : insert_threads) {
|
||||
for (auto &thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
|
||||
WordRefList result;
|
||||
std::mutex result_mutex;
|
||||
WordRefList search_results;
|
||||
std::mutex search_results_mutex;
|
||||
|
||||
std::vector<std::thread> search_threads;
|
||||
std::vector<std::thread> threads;
|
||||
for (const auto &bucket : buckets_) {
|
||||
|
||||
search_threads.emplace_back([&] {
|
||||
auto thread_result = bucket.find_prefix(search_term);
|
||||
if (!thread_result.empty()) {
|
||||
std::lock_guard<std::mutex> result_lock(result_mutex);
|
||||
std::move(thread_result.begin(), thread_result.end(),
|
||||
std::back_inserter(result));
|
||||
threads.emplace_back([&] {
|
||||
auto thread_search_results = bucket.find_prefix(search_term);
|
||||
if (!thread_search_results.empty()) {
|
||||
std::lock_guard result_lock(search_results_mutex);
|
||||
std::move(thread_search_results.begin(), thread_search_results.end(),
|
||||
std::back_inserter(search_results));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (auto &thread : search_threads) {
|
||||
for (auto &thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
return result;
|
||||
return search_results;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue