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

main
mandlm 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

@ -122,8 +122,7 @@ void MainWindow::createSelectedFinder() {
}
}
void MainWindow::showResults(
const std::forward_list<const std::string *> &results) {
void MainWindow::showResults(const WordRefList &results) {
QStringList ui_results;
for (auto word : results) {
ui_results.append(QString::fromStdString(*word));

View File

@ -36,7 +36,7 @@ private:
void loadWordList(std::filesystem::path path);
void search(const QString &search_term);
void createSelectedFinder();
void showResults(const std::forward_list<const std::string *> &results);
void showResults(const WordRefList &results);
private slots:
void on_searchInput_textChanged(const QString &search_term);

View File

@ -19,6 +19,7 @@ add_library(
include/tree_finder.h
src/grouped_finder.cpp
include/grouped_finder.h
include/finder.h
src/bucket_finder.cpp
include/bucket_finder.h)

View File

@ -12,8 +12,7 @@ private:
public:
void insert(const WordList &word_list, size_t first_index, size_t last_index);
std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const;
WordRefList find_prefix(std::string_view search_term) const;
};
class BucketFinder : public Finder {
@ -23,6 +22,5 @@ private:
public:
BucketFinder(const WordList &word_list);
std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const override;
WordRefList find_prefix(std::string_view search_term) const override;
};

View File

@ -1,12 +1,10 @@
#pragma once
#include <forward_list>
#include <string>
#include "word_list.h"
class Finder {
public:
virtual ~Finder() = default;
virtual std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const = 0;
virtual WordRefList find_prefix(std::string_view search_term) const = 0;
};

View File

@ -12,6 +12,6 @@ private:
public:
GroupedFinder(const WordList &word_list);
virtual std::forward_list<const std::string *>
virtual WordRefList
find_prefix(std::string_view search_prefix) const override;
};

View File

@ -10,6 +10,5 @@ private:
public:
LinearFinder(const WordList &word_list);
std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const override;
WordRefList find_prefix(std::string_view search_term) const override;
};

View File

@ -10,6 +10,5 @@ private:
public:
ParallelFinder(const WordList &word_list);
std::forward_list<const std::string *>
find_prefix(std::string_view search_prefix) const override;
WordRefList find_prefix(std::string_view search_prefix) const override;
};

View File

@ -10,6 +10,5 @@ private:
public:
SortedLinearFinder(const WordList &word_list);
std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const override;
WordRefList find_prefix(std::string_view search_term) const override;
};

View File

@ -7,14 +7,14 @@
class SearchTreeNode {
private:
std::forward_list<const std::string *> words_;
WordRefList words_;
std::map<const char, SearchTreeNode> children_;
public:
void insert(std::string_view partial_word, const std::string *original_word);
const SearchTreeNode *find(std::string_view search_term) const;
std::forward_list<const std::string *> words() const;
WordRefList words() const;
};
class SearchTree : public SearchTreeNode {
@ -29,6 +29,5 @@ private:
public:
TreeFinder(const WordList &word_list);
virtual std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const override;
WordRefList find_prefix(std::string_view search_term) const override;
};

View File

@ -1,11 +1,12 @@
#pragma once
#include <filesystem>
#include <forward_list>
#include <mutex>
#include <string>
#include <vector>
class WordRefList;
class WordList : public std::vector<std::string> {
public:
WordList &multiply(size_t factor);
@ -15,10 +16,11 @@ public:
static WordList fourCaps();
static WordList fromFile(const std::filesystem::path &path);
static void find_prefix_in_range(
const WordList &word_list, const std::string_view &search_prefix,
size_t start_index, size_t end_index,
std::forward_list<const std::string *> &result, std::mutex &result_mutex);
static void find_prefix_in_range(const WordList &word_list,
const std::string_view &search_prefix,
size_t start_index, size_t end_index,
WordRefList &result,
std::mutex &result_mutex);
};
class WordRefList : public std::vector<const std::string *> {
@ -26,8 +28,9 @@ public:
WordRefList() = default;
WordRefList(const WordList &source);
static void find_prefix_in_range(
const WordRefList &word_list, const std::string_view &search_prefix,
size_t start_index, size_t end_index,
std::forward_list<const std::string *> &result, std::mutex &result_mutex);
static void find_prefix_in_range(const WordRefList &word_list,
const std::string_view &search_prefix,
size_t start_index, size_t end_index,
WordRefList &result,
std::mutex &result_mutex);
};

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));
}
});
}

View File

@ -5,8 +5,7 @@
#include <thread>
#include <vector>
using std::mutex, std::vector, std::thread, std::string, std::forward_list,
std::string_view;
using std::mutex, std::vector, std::thread, std::string, std::string_view;
GroupedFinder::GroupedFinder(const WordList &word_list) {
for (const auto &word : word_list) {
@ -14,8 +13,7 @@ GroupedFinder::GroupedFinder(const WordList &word_list) {
}
}
forward_list<const string *>
GroupedFinder::find_prefix(string_view search_prefix) const {
WordRefList GroupedFinder::find_prefix(string_view search_prefix) const {
const auto group = groups_.find(search_prefix.front());
if (group == groups_.cend()) {
return {};
@ -27,7 +25,7 @@ GroupedFinder::find_prefix(string_view search_prefix) const {
const auto thread_count =
std::min<size_t>(std::thread::hardware_concurrency(), word_list_size);
forward_list<const string *> result;
WordRefList result;
mutex result_mutex;
vector<thread> search_threads;

View File

@ -1,18 +1,16 @@
#include "linear_finder.h"
#include <forward_list>
#include <string_view>
using std::string, std::forward_list, std::string_view;
using std::string, std::string_view;
LinearFinder::LinearFinder(const WordList &word_list) : word_list_(word_list) {}
forward_list<const string *>
LinearFinder::find_prefix(string_view search_term) const {
forward_list<const string *> matching_words;
WordRefList LinearFinder::find_prefix(string_view search_term) const {
WordRefList matching_words;
for (const auto &current_word : word_list_) {
if (current_word.starts_with(search_term)) {
matching_words.push_front(&current_word);
matching_words.push_back(&current_word);
}
}

View File

@ -3,15 +3,13 @@
#include <mutex>
#include <thread>
using std::mutex, std::thread, std::vector, std::forward_list, std::string,
std::string_view;
using std::mutex, std::thread, std::vector, std::string, std::string_view;
ParallelFinder::ParallelFinder(const WordList &word_list)
: word_list_(word_list) {}
forward_list<const string *>
ParallelFinder::find_prefix(string_view search_prefix) const {
forward_list<const string *> result;
WordRefList ParallelFinder::find_prefix(string_view search_prefix) const {
WordRefList result;
mutex result_mutex;
const size_t word_list_size = word_list_.size();

View File

@ -2,7 +2,7 @@
#include <algorithm>
using std::forward_list, std::string, std::string_view;
using std::string, std::string_view;
SortedLinearFinder::SortedLinearFinder(const WordList &word_list)
: word_list_(word_list) {
@ -11,14 +11,13 @@ SortedLinearFinder::SortedLinearFinder(const WordList &word_list)
[](const string *left, const string *right) { return *left < *right; });
}
forward_list<const string *>
SortedLinearFinder::find_prefix(string_view search_term) const {
forward_list<const string *> matching_words;
WordRefList SortedLinearFinder::find_prefix(string_view search_term) const {
WordRefList matching_words;
bool in_range = false;
for (const auto *word : word_list_) {
if (word->starts_with(search_term)) {
matching_words.push_front(word);
matching_words.push_back(word);
in_range = true;
} else if (in_range) {
break;

View File

@ -1,11 +1,11 @@
#include "tree_finder.h"
using std::string, std::string_view, std::forward_list;
using std::string, std::string_view;
void SearchTreeNode::insert(string_view partial_word,
const string *original_word) {
if (partial_word.empty()) {
words_.push_front(original_word);
words_.push_back(original_word);
} else {
children_[partial_word.front()].insert(
string_view(partial_word).substr(1, partial_word.length()),
@ -26,10 +26,12 @@ const SearchTreeNode *SearchTreeNode::find(string_view search_term) const {
return nullptr;
}
forward_list<const string *> SearchTreeNode::words() const {
forward_list<const string *> results(words_);
WordRefList SearchTreeNode::words() const {
WordRefList results(words_);
for (const auto &child : children_) {
results.merge(child.second.words());
auto child_words = child.second.words();
std::move(child_words.begin(), child_words.end(),
std::back_inserter(results));
}
return results;
@ -43,8 +45,7 @@ SearchTree::SearchTree(const WordList &word_list) {
TreeFinder::TreeFinder(const WordList &word_list) : search_tree_(word_list) {}
forward_list<const string *>
TreeFinder::find_prefix(string_view search_term) const {
WordRefList TreeFinder::find_prefix(string_view search_term) const {
const auto *result_node = search_tree_.find(search_term);
if (result_node == nullptr) {
return {};

View File

@ -71,22 +71,24 @@ WordList WordList::fromFile(const std::filesystem::path &path) {
return word_list;
}
void WordList::find_prefix_in_range(
const WordList &word_list, const std::string_view &search_prefix,
size_t start_index, size_t end_index,
std::forward_list<const std::string *> &result, std::mutex &result_mutex) {
std::forward_list<const std::string *> local_results;
void WordList::find_prefix_in_range(const WordList &word_list,
const std::string_view &search_prefix,
size_t start_index, size_t end_index,
WordRefList &result,
std::mutex &result_mutex) {
WordRefList local_results;
for (size_t index = start_index; index < end_index; ++index) {
const auto &current_word = word_list[index];
if (current_word.starts_with(search_prefix)) {
local_results.push_front(&current_word);
local_results.push_back(&current_word);
}
}
if (!local_results.empty()) {
const std::lock_guard<std::mutex> lock(result_mutex);
result.merge(local_results);
std::move(local_results.begin(), local_results.end(),
std::back_inserter(result));
}
};
@ -96,21 +98,23 @@ WordRefList::WordRefList(const WordList &source) {
}
}
void WordRefList::find_prefix_in_range(
const WordRefList &word_list, const std::string_view &search_prefix,
size_t start_index, size_t end_index,
std::forward_list<const std::string *> &result, std::mutex &result_mutex) {
std::forward_list<const std::string *> local_results;
void WordRefList::find_prefix_in_range(const WordRefList &word_list,
const std::string_view &search_prefix,
size_t start_index, size_t end_index,
WordRefList &result,
std::mutex &result_mutex) {
WordRefList local_results;
for (size_t index = start_index; index < end_index; ++index) {
const auto *current_word = word_list[index];
if (current_word->starts_with(search_prefix)) {
local_results.push_front(current_word);
local_results.push_back(current_word);
}
}
if (!local_results.empty()) {
const std::lock_guard<std::mutex> lock(result_mutex);
result.merge(local_results);
std::move(local_results.begin(), local_results.end(),
std::back_inserter(result));
}
};

View File

@ -55,13 +55,12 @@ int main(int argc, char *argv[]) {
auto word_list = generate_word_list(5);
test_finder<BucketFinder>(word_list, "bucket finder");
test_finder<LinearFinder>(word_list, "linear finder");
test_finder<SortedLinearFinder>(word_list, "sorted linear finder");
test_finder<ParallelFinder>(word_list, "parallel finder");
test_finder<TreeFinder>(word_list, "tree finder");
test_finder<GroupedFinder>(word_list, "grouped finder");
test_finder<BucketFinder>(word_list, "bucket finder");
return EXIT_SUCCESS;
}