feat: use WordRefList instead of std::foward_list for results

This commit is contained in:
Michael Mandl 2024-03-21 20:16:00 +01:00
parent b53a4edeee
commit a6146415df
Signed by: mandlm
GPG key ID: 4AA25D647AA54CC7
19 changed files with 78 additions and 86 deletions

View file

@ -12,19 +12,19 @@ void Bucket::insert(const WordList &word_list, size_t first_index,
}
}
std::forward_list<const std::string *>
Bucket::find_prefix(std::string_view search_term) const {
const auto group = groups_.find(search_term.front());
if (group == groups_.cend()) {
WordRefList Bucket::find_prefix(std::string_view search_term) const {
auto group_it = groups_.find(search_term.front());
if (group_it == groups_.cend()) {
return {};
}
std::forward_list<const std::string *> result;
for (const auto *word : group->second) {
WordRefList result;
for (const auto *word : group_it->second) {
if (word->starts_with(search_term)) {
result.push_front(word);
result.push_back(word);
}
}
return result;
}
@ -59,9 +59,8 @@ BucketFinder::BucketFinder(const WordList &word_list) {
}
}
std::forward_list<const std::string *>
BucketFinder::find_prefix(std::string_view search_term) const {
std::forward_list<const std::string *> result;
WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
WordRefList result;
std::mutex result_mutex;
std::vector<std::thread> search_threads;
@ -71,7 +70,8 @@ BucketFinder::find_prefix(std::string_view search_term) const {
auto thread_result = bucket.find_prefix(search_term);
if (!thread_result.empty()) {
std::lock_guard<std::mutex> result_lock(result_mutex);
result.merge(thread_result);
std::move(thread_result.begin(), thread_result.end(),
std::back_inserter(result));
}
});
}