Compare commits
No commits in common. "1441a869c2388eca1b7e073c97947e962d1d5c2a" and "532e52cc7f035fefb6fd435e9f9b7dcc85f08487" have entirely different histories.
1441a869c2
...
532e52cc7f
@ -5,12 +5,6 @@
|
|||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
/** A Bucket contains a partial WordRefList, split-up and (hash-)mapped by their
|
|
||||||
* first characters.
|
|
||||||
*
|
|
||||||
* It's meant to be read and written by a single thread without the need for
|
|
||||||
* synchronization.
|
|
||||||
*/
|
|
||||||
class Bucket {
|
class Bucket {
|
||||||
private:
|
private:
|
||||||
std::unordered_map<char, WordRefList> directory_;
|
std::unordered_map<char, WordRefList> directory_;
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
#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) {
|
||||||
@ -33,8 +31,6 @@ 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) {
|
||||||
@ -51,7 +47,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 &thread_bucket = buckets_[bucket_index];
|
auto &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;
|
||||||
@ -59,7 +55,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] {
|
||||||
thread_bucket.insert(word_list, first_word_index, last_word_index);
|
bucket.insert(word_list, first_word_index, last_word_index);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,9 +68,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> search_threads;
|
std::vector<std::thread> threads;
|
||||||
for (const auto &bucket : buckets_) {
|
for (const auto &bucket : buckets_) {
|
||||||
search_threads.emplace_back([&] {
|
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);
|
||||||
@ -84,7 +80,7 @@ WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &thread : search_threads) {
|
for (auto &thread : threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user