refactor: clean up

This commit is contained in:
Michael Mandl 2024-03-23 12:22:53 +01:00
parent bc58ca5af8
commit 1441a869c2
Signed by: mandlm
GPG key ID: 4AA25D647AA54CC7

View file

@ -7,6 +7,8 @@
#include <thread>
#include <vector>
// class Bucket
void Bucket::insert(const WordList &word_list, size_t first_index,
size_t last_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;
}
// class BucketFinder
BucketFinder::BucketFinder(const WordList &word_list) { insert(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;
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 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;
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;
std::mutex search_results_mutex;
std::vector<std::thread> threads;
std::vector<std::thread> search_threads;
for (const auto &bucket : buckets_) {
threads.emplace_back([&] {
search_threads.emplace_back([&] {
auto thread_search_results = bucket.find_prefix(search_term);
if (!thread_search_results.empty()) {
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();
}