Compare commits
1 Commits
8c9a1522a0
...
70200735d9
Author | SHA1 | Date |
---|---|---|
mandlm | 70200735d9 |
|
@ -28,12 +28,12 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
MainWindow::~MainWindow() { delete ui; }
|
MainWindow::~MainWindow() { delete ui; }
|
||||||
|
|
||||||
void MainWindow::setupAlgorithmSelector() {
|
void MainWindow::setupAlgorithmSelector() {
|
||||||
search_algorithms_.appendRow(new QStandardItem("Bucket search"));
|
|
||||||
search_algorithms_.appendRow(new QStandardItem("Linear search"));
|
search_algorithms_.appendRow(new QStandardItem("Linear search"));
|
||||||
search_algorithms_.appendRow(new QStandardItem("Sorted linear search"));
|
search_algorithms_.appendRow(new QStandardItem("Sorted linear search"));
|
||||||
search_algorithms_.appendRow(new QStandardItem("Parallel search"));
|
search_algorithms_.appendRow(new QStandardItem("Parallel search"));
|
||||||
search_algorithms_.appendRow(new QStandardItem("Tree search"));
|
search_algorithms_.appendRow(new QStandardItem("Tree search"));
|
||||||
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
|
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
|
||||||
|
search_algorithms_.appendRow(new QStandardItem("Bucket search"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::setupWordListSourceSelector() {
|
void MainWindow::setupWordListSourceSelector() {
|
||||||
|
@ -89,8 +89,8 @@ void MainWindow::search(const QString &search_term) {
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
std::stringstream status_message;
|
std::stringstream status_message;
|
||||||
status_message << "search took " << timer << ", found " << results.size()
|
status_message << "search took " << timer << ", found "
|
||||||
<< " result(s) in " << word_list_.size() << " words.";
|
<< std::distance(results.begin(), results.end()) << " results";
|
||||||
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
|
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
|
||||||
|
|
||||||
showResults(results);
|
showResults(results);
|
||||||
|
@ -102,27 +102,28 @@ void MainWindow::createSelectedFinder() {
|
||||||
switch (selectedFinder) {
|
switch (selectedFinder) {
|
||||||
case 0:
|
case 0:
|
||||||
default:
|
default:
|
||||||
finder_ = std::make_unique<BucketFinder>(word_list_);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
finder_ = std::make_unique<LinearFinder>(word_list_);
|
finder_ = std::make_unique<LinearFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 1:
|
||||||
finder_ = std::make_unique<SortedLinearFinder>(word_list_);
|
finder_ = std::make_unique<SortedLinearFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 2:
|
||||||
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 3:
|
||||||
finder_ = std::make_unique<TreeFinder>(word_list_);
|
finder_ = std::make_unique<TreeFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 4:
|
||||||
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
|
case 5:
|
||||||
|
finder_ = std::make_unique<BucketFinder>(word_list_);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::showResults(const WordRefList &results) {
|
void MainWindow::showResults(
|
||||||
|
const std::forward_list<const std::string *> &results) {
|
||||||
QStringList ui_results;
|
QStringList ui_results;
|
||||||
for (auto word : results) {
|
for (auto word : results) {
|
||||||
ui_results.append(QString::fromStdString(*word));
|
ui_results.append(QString::fromStdString(*word));
|
||||||
|
@ -159,9 +160,3 @@ void MainWindow::on_wordListSizeSelector_valueChanged(int) {
|
||||||
search(ui->searchInput->displayText());
|
search(ui->searchInput->displayText());
|
||||||
QGuiApplication::restoreOverrideCursor();
|
QGuiApplication::restoreOverrideCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::on_searchInput_returnPressed() {
|
|
||||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
||||||
search(ui->searchInput->displayText());
|
|
||||||
QGuiApplication::restoreOverrideCursor();
|
|
||||||
}
|
|
||||||
|
|
|
@ -36,14 +36,13 @@ private:
|
||||||
void loadWordList(std::filesystem::path path);
|
void loadWordList(std::filesystem::path path);
|
||||||
void search(const QString &search_term);
|
void search(const QString &search_term);
|
||||||
void createSelectedFinder();
|
void createSelectedFinder();
|
||||||
void showResults(const WordRefList &results);
|
void showResults(const std::forward_list<const std::string *> &results);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_searchInput_textChanged(const QString &search_term);
|
void on_searchInput_textChanged(const QString &search_term);
|
||||||
void on_searchAlgorithmSelector_currentIndexChanged(int);
|
void on_searchAlgorithmSelector_currentIndexChanged(int);
|
||||||
void on_wordListSourceSelector_currentIndexChanged(int);
|
void on_wordListSourceSelector_currentIndexChanged(int);
|
||||||
void on_wordListSizeSelector_valueChanged(int);
|
void on_wordListSizeSelector_valueChanged(int);
|
||||||
void on_searchInput_returnPressed();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
|
|
|
@ -19,7 +19,6 @@ add_library(
|
||||||
include/tree_finder.h
|
include/tree_finder.h
|
||||||
src/grouped_finder.cpp
|
src/grouped_finder.cpp
|
||||||
include/grouped_finder.h
|
include/grouped_finder.h
|
||||||
include/finder.h
|
|
||||||
src/bucket_finder.cpp
|
src/bucket_finder.cpp
|
||||||
include/bucket_finder.h)
|
include/bucket_finder.h)
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,12 @@ private:
|
||||||
std::map<const char, WordRefList> groups_;
|
std::map<const char, WordRefList> groups_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Bucket() = default;
|
||||||
|
|
||||||
void insert(const WordList &word_list, size_t first_index, size_t last_index);
|
void insert(const WordList &word_list, size_t first_index, size_t last_index);
|
||||||
|
|
||||||
WordRefList find_prefix(std::string_view search_term) const;
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class BucketFinder : public Finder {
|
class BucketFinder : public Finder {
|
||||||
|
@ -22,5 +25,6 @@ private:
|
||||||
public:
|
public:
|
||||||
BucketFinder(const WordList &word_list);
|
BucketFinder(const WordList &word_list);
|
||||||
|
|
||||||
WordRefList find_prefix(std::string_view search_term) const override;
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "word_list.h"
|
#include <forward_list>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class Finder {
|
class Finder {
|
||||||
public:
|
public:
|
||||||
virtual ~Finder() = default;
|
virtual ~Finder() = default;
|
||||||
|
|
||||||
virtual WordRefList find_prefix(std::string_view search_term) const = 0;
|
virtual std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,6 +12,6 @@ private:
|
||||||
public:
|
public:
|
||||||
GroupedFinder(const WordList &word_list);
|
GroupedFinder(const WordList &word_list);
|
||||||
|
|
||||||
virtual WordRefList
|
virtual std::forward_list<const std::string *>
|
||||||
find_prefix(std::string_view search_prefix) const override;
|
find_prefix(std::string_view search_prefix) const override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,5 +10,6 @@ private:
|
||||||
public:
|
public:
|
||||||
LinearFinder(const WordList &word_list);
|
LinearFinder(const WordList &word_list);
|
||||||
|
|
||||||
WordRefList find_prefix(std::string_view search_term) const override;
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,5 +10,6 @@ private:
|
||||||
public:
|
public:
|
||||||
ParallelFinder(const WordList &word_list);
|
ParallelFinder(const WordList &word_list);
|
||||||
|
|
||||||
WordRefList find_prefix(std::string_view search_prefix) const override;
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_prefix) const override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,5 +10,6 @@ private:
|
||||||
public:
|
public:
|
||||||
SortedLinearFinder(const WordList &word_list);
|
SortedLinearFinder(const WordList &word_list);
|
||||||
|
|
||||||
WordRefList find_prefix(std::string_view search_term) const override;
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,14 +7,14 @@
|
||||||
|
|
||||||
class SearchTreeNode {
|
class SearchTreeNode {
|
||||||
private:
|
private:
|
||||||
WordRefList words_;
|
std::forward_list<const std::string *> words_;
|
||||||
std::map<const char, SearchTreeNode> children_;
|
std::map<const char, SearchTreeNode> children_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void insert(std::string_view partial_word, const std::string *original_word);
|
void insert(std::string_view partial_word, const std::string *original_word);
|
||||||
const SearchTreeNode *find(std::string_view search_term) const;
|
const SearchTreeNode *find(std::string_view search_term) const;
|
||||||
|
|
||||||
WordRefList words() const;
|
std::forward_list<const std::string *> words() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SearchTree : public SearchTreeNode {
|
class SearchTree : public SearchTreeNode {
|
||||||
|
@ -29,5 +29,6 @@ private:
|
||||||
public:
|
public:
|
||||||
TreeFinder(const WordList &word_list);
|
TreeFinder(const WordList &word_list);
|
||||||
|
|
||||||
WordRefList find_prefix(std::string_view search_term) const override;
|
virtual std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <forward_list>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class WordRefList;
|
|
||||||
|
|
||||||
class WordList : public std::vector<std::string> {
|
class WordList : public std::vector<std::string> {
|
||||||
public:
|
public:
|
||||||
WordList &multiply(size_t factor);
|
WordList &multiply(size_t factor);
|
||||||
|
@ -16,11 +15,10 @@ public:
|
||||||
static WordList fourCaps();
|
static WordList fourCaps();
|
||||||
static WordList fromFile(const std::filesystem::path &path);
|
static WordList fromFile(const std::filesystem::path &path);
|
||||||
|
|
||||||
static void find_prefix_in_range(const WordList &word_list,
|
static void find_prefix_in_range(
|
||||||
const std::string_view &search_prefix,
|
const WordList &word_list, const std::string_view &search_prefix,
|
||||||
size_t start_index, size_t end_index,
|
size_t start_index, size_t end_index,
|
||||||
WordRefList &result,
|
std::forward_list<const std::string *> &result, std::mutex &result_mutex);
|
||||||
std::mutex &result_mutex);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class WordRefList : public std::vector<const std::string *> {
|
class WordRefList : public std::vector<const std::string *> {
|
||||||
|
@ -28,9 +26,8 @@ public:
|
||||||
WordRefList() = default;
|
WordRefList() = default;
|
||||||
WordRefList(const WordList &source);
|
WordRefList(const WordList &source);
|
||||||
|
|
||||||
static void find_prefix_in_range(const WordRefList &word_list,
|
static void find_prefix_in_range(
|
||||||
const std::string_view &search_prefix,
|
const WordRefList &word_list, const std::string_view &search_prefix,
|
||||||
size_t start_index, size_t end_index,
|
size_t start_index, size_t end_index,
|
||||||
WordRefList &result,
|
std::forward_list<const std::string *> &result, std::mutex &result_mutex);
|
||||||
std::mutex &result_mutex);
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,19 +12,19 @@ void Bucket::insert(const WordList &word_list, size_t first_index,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WordRefList Bucket::find_prefix(std::string_view search_term) const {
|
std::forward_list<const std::string *>
|
||||||
auto group_it = groups_.find(search_term.front());
|
Bucket::find_prefix(std::string_view search_term) const {
|
||||||
if (group_it == groups_.cend()) {
|
const auto group = groups_.find(search_term.front());
|
||||||
|
if (group == groups_.cend()) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
WordRefList result;
|
std::forward_list<const std::string *> result;
|
||||||
for (const auto *word : group_it->second) {
|
for (const auto *word : group->second) {
|
||||||
if (word->starts_with(search_term)) {
|
if (word->starts_with(search_term)) {
|
||||||
result.push_back(word);
|
result.push_front(word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,45 +40,45 @@ BucketFinder::BucketFinder(const WordList &word_list) {
|
||||||
|
|
||||||
buckets_.resize(bucket_count);
|
buckets_.resize(bucket_count);
|
||||||
|
|
||||||
std::vector<std::thread> 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) {
|
||||||
|
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];
|
auto &bucket = buckets_[bucket_index];
|
||||||
|
|
||||||
bool is_last_bucket = bucket_index == bucket_count - 1;
|
insert_threads.emplace_back([&bucket, &word_list, first_index, last_index] {
|
||||||
|
|
||||||
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);
|
bucket.insert(word_list, first_index, last_index);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &thread : threads) {
|
for (auto &thread : insert_threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WordRefList BucketFinder::find_prefix(std::string_view search_term) const {
|
std::forward_list<const std::string *>
|
||||||
WordRefList search_results;
|
BucketFinder::find_prefix(std::string_view search_term) const {
|
||||||
std::mutex search_results_mutex;
|
std::forward_list<const std::string *> result;
|
||||||
|
std::mutex result_mutex;
|
||||||
|
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> search_threads;
|
||||||
for (const auto &bucket : buckets_) {
|
for (const auto &bucket : buckets_) {
|
||||||
threads.emplace_back([&] {
|
|
||||||
auto thread_search_results = bucket.find_prefix(search_term);
|
search_threads.emplace_back([&] {
|
||||||
if (!thread_search_results.empty()) {
|
auto thread_result = bucket.find_prefix(search_term);
|
||||||
std::lock_guard result_lock(search_results_mutex);
|
if (!thread_result.empty()) {
|
||||||
std::move(thread_search_results.begin(), thread_search_results.end(),
|
std::lock_guard<std::mutex> result_lock(result_mutex);
|
||||||
std::back_inserter(search_results));
|
result.merge(thread_result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &thread : threads) {
|
for (auto &thread : search_threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
return search_results;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using std::mutex, std::vector, std::thread, std::string, std::string_view;
|
using std::mutex, std::vector, std::thread, std::string, std::forward_list,
|
||||||
|
std::string_view;
|
||||||
|
|
||||||
GroupedFinder::GroupedFinder(const WordList &word_list) {
|
GroupedFinder::GroupedFinder(const WordList &word_list) {
|
||||||
for (const auto &word : word_list) {
|
for (const auto &word : word_list) {
|
||||||
|
@ -13,7 +14,8 @@ GroupedFinder::GroupedFinder(const WordList &word_list) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WordRefList GroupedFinder::find_prefix(string_view search_prefix) const {
|
forward_list<const string *>
|
||||||
|
GroupedFinder::find_prefix(string_view search_prefix) const {
|
||||||
const auto group = groups_.find(search_prefix.front());
|
const auto group = groups_.find(search_prefix.front());
|
||||||
if (group == groups_.cend()) {
|
if (group == groups_.cend()) {
|
||||||
return {};
|
return {};
|
||||||
|
@ -25,7 +27,7 @@ WordRefList GroupedFinder::find_prefix(string_view search_prefix) const {
|
||||||
const auto thread_count =
|
const auto thread_count =
|
||||||
std::min<size_t>(std::thread::hardware_concurrency(), word_list_size);
|
std::min<size_t>(std::thread::hardware_concurrency(), word_list_size);
|
||||||
|
|
||||||
WordRefList result;
|
forward_list<const string *> result;
|
||||||
mutex result_mutex;
|
mutex result_mutex;
|
||||||
|
|
||||||
vector<thread> search_threads;
|
vector<thread> search_threads;
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
|
#include <forward_list>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
using std::string, std::string_view;
|
using std::string, std::forward_list, std::string_view;
|
||||||
|
|
||||||
LinearFinder::LinearFinder(const WordList &word_list) : word_list_(word_list) {}
|
LinearFinder::LinearFinder(const WordList &word_list) : word_list_(word_list) {}
|
||||||
|
|
||||||
WordRefList LinearFinder::find_prefix(string_view search_term) const {
|
forward_list<const string *>
|
||||||
WordRefList matching_words;
|
LinearFinder::find_prefix(string_view search_term) const {
|
||||||
|
forward_list<const string *> matching_words;
|
||||||
|
|
||||||
for (const auto ¤t_word : word_list_) {
|
for (const auto ¤t_word : word_list_) {
|
||||||
if (current_word.starts_with(search_term)) {
|
if (current_word.starts_with(search_term)) {
|
||||||
matching_words.push_back(¤t_word);
|
matching_words.push_front(¤t_word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,15 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
using std::mutex, std::thread, std::vector, std::string, std::string_view;
|
using std::mutex, std::thread, std::vector, std::forward_list, std::string,
|
||||||
|
std::string_view;
|
||||||
|
|
||||||
ParallelFinder::ParallelFinder(const WordList &word_list)
|
ParallelFinder::ParallelFinder(const WordList &word_list)
|
||||||
: word_list_(word_list) {}
|
: word_list_(word_list) {}
|
||||||
|
|
||||||
WordRefList ParallelFinder::find_prefix(string_view search_prefix) const {
|
forward_list<const string *>
|
||||||
WordRefList result;
|
ParallelFinder::find_prefix(string_view search_prefix) const {
|
||||||
|
forward_list<const string *> result;
|
||||||
mutex result_mutex;
|
mutex result_mutex;
|
||||||
|
|
||||||
const size_t word_list_size = word_list_.size();
|
const size_t word_list_size = word_list_.size();
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
using std::string, std::string_view;
|
using std::forward_list, std::string, std::string_view;
|
||||||
|
|
||||||
SortedLinearFinder::SortedLinearFinder(const WordList &word_list)
|
SortedLinearFinder::SortedLinearFinder(const WordList &word_list)
|
||||||
: word_list_(word_list) {
|
: word_list_(word_list) {
|
||||||
|
@ -11,13 +11,14 @@ SortedLinearFinder::SortedLinearFinder(const WordList &word_list)
|
||||||
[](const string *left, const string *right) { return *left < *right; });
|
[](const string *left, const string *right) { return *left < *right; });
|
||||||
}
|
}
|
||||||
|
|
||||||
WordRefList SortedLinearFinder::find_prefix(string_view search_term) const {
|
forward_list<const string *>
|
||||||
WordRefList matching_words;
|
SortedLinearFinder::find_prefix(string_view search_term) const {
|
||||||
|
forward_list<const string *> matching_words;
|
||||||
|
|
||||||
bool in_range = false;
|
bool in_range = false;
|
||||||
for (const auto *word : word_list_) {
|
for (const auto *word : word_list_) {
|
||||||
if (word->starts_with(search_term)) {
|
if (word->starts_with(search_term)) {
|
||||||
matching_words.push_back(word);
|
matching_words.push_front(word);
|
||||||
in_range = true;
|
in_range = true;
|
||||||
} else if (in_range) {
|
} else if (in_range) {
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include "tree_finder.h"
|
#include "tree_finder.h"
|
||||||
|
|
||||||
using std::string, std::string_view;
|
using std::string, std::string_view, std::forward_list;
|
||||||
|
|
||||||
void SearchTreeNode::insert(string_view partial_word,
|
void SearchTreeNode::insert(string_view partial_word,
|
||||||
const string *original_word) {
|
const string *original_word) {
|
||||||
if (partial_word.empty()) {
|
if (partial_word.empty()) {
|
||||||
words_.push_back(original_word);
|
words_.push_front(original_word);
|
||||||
} else {
|
} else {
|
||||||
children_[partial_word.front()].insert(
|
children_[partial_word.front()].insert(
|
||||||
string_view(partial_word).substr(1, partial_word.length()),
|
string_view(partial_word).substr(1, partial_word.length()),
|
||||||
|
@ -26,12 +26,10 @@ const SearchTreeNode *SearchTreeNode::find(string_view search_term) const {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
WordRefList SearchTreeNode::words() const {
|
forward_list<const string *> SearchTreeNode::words() const {
|
||||||
WordRefList results(words_);
|
forward_list<const string *> results(words_);
|
||||||
for (const auto &child : children_) {
|
for (const auto &child : children_) {
|
||||||
auto child_words = child.second.words();
|
results.merge(child.second.words());
|
||||||
std::move(child_words.begin(), child_words.end(),
|
|
||||||
std::back_inserter(results));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
@ -45,7 +43,8 @@ SearchTree::SearchTree(const WordList &word_list) {
|
||||||
|
|
||||||
TreeFinder::TreeFinder(const WordList &word_list) : search_tree_(word_list) {}
|
TreeFinder::TreeFinder(const WordList &word_list) : search_tree_(word_list) {}
|
||||||
|
|
||||||
WordRefList TreeFinder::find_prefix(string_view search_term) const {
|
forward_list<const string *>
|
||||||
|
TreeFinder::find_prefix(string_view search_term) const {
|
||||||
const auto *result_node = search_tree_.find(search_term);
|
const auto *result_node = search_tree_.find(search_term);
|
||||||
if (result_node == nullptr) {
|
if (result_node == nullptr) {
|
||||||
return {};
|
return {};
|
||||||
|
|
|
@ -71,24 +71,22 @@ WordList WordList::fromFile(const std::filesystem::path &path) {
|
||||||
return word_list;
|
return word_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WordList::find_prefix_in_range(const WordList &word_list,
|
void WordList::find_prefix_in_range(
|
||||||
const std::string_view &search_prefix,
|
const WordList &word_list, const std::string_view &search_prefix,
|
||||||
size_t start_index, size_t end_index,
|
size_t start_index, size_t end_index,
|
||||||
WordRefList &result,
|
std::forward_list<const std::string *> &result, std::mutex &result_mutex) {
|
||||||
std::mutex &result_mutex) {
|
std::forward_list<const std::string *> local_results;
|
||||||
WordRefList local_results;
|
|
||||||
|
|
||||||
for (size_t index = start_index; index < end_index; ++index) {
|
for (size_t index = start_index; index < end_index; ++index) {
|
||||||
const auto ¤t_word = word_list[index];
|
const auto ¤t_word = word_list[index];
|
||||||
if (current_word.starts_with(search_prefix)) {
|
if (current_word.starts_with(search_prefix)) {
|
||||||
local_results.push_back(¤t_word);
|
local_results.push_front(¤t_word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!local_results.empty()) {
|
if (!local_results.empty()) {
|
||||||
const std::lock_guard<std::mutex> lock(result_mutex);
|
const std::lock_guard<std::mutex> lock(result_mutex);
|
||||||
std::move(local_results.begin(), local_results.end(),
|
result.merge(local_results);
|
||||||
std::back_inserter(result));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -98,23 +96,21 @@ WordRefList::WordRefList(const WordList &source) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WordRefList::find_prefix_in_range(const WordRefList &word_list,
|
void WordRefList::find_prefix_in_range(
|
||||||
const std::string_view &search_prefix,
|
const WordRefList &word_list, const std::string_view &search_prefix,
|
||||||
size_t start_index, size_t end_index,
|
size_t start_index, size_t end_index,
|
||||||
WordRefList &result,
|
std::forward_list<const std::string *> &result, std::mutex &result_mutex) {
|
||||||
std::mutex &result_mutex) {
|
std::forward_list<const std::string *> local_results;
|
||||||
WordRefList local_results;
|
|
||||||
|
|
||||||
for (size_t index = start_index; index < end_index; ++index) {
|
for (size_t index = start_index; index < end_index; ++index) {
|
||||||
const auto *current_word = word_list[index];
|
const auto *current_word = word_list[index];
|
||||||
if (current_word->starts_with(search_prefix)) {
|
if (current_word->starts_with(search_prefix)) {
|
||||||
local_results.push_back(current_word);
|
local_results.push_front(current_word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!local_results.empty()) {
|
if (!local_results.empty()) {
|
||||||
const std::lock_guard<std::mutex> lock(result_mutex);
|
const std::lock_guard<std::mutex> lock(result_mutex);
|
||||||
std::move(local_results.begin(), local_results.end(),
|
result.merge(local_results);
|
||||||
std::back_inserter(result));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -55,12 +55,13 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
auto word_list = generate_word_list(5);
|
auto word_list = generate_word_list(5);
|
||||||
|
|
||||||
|
test_finder<BucketFinder>(word_list, "bucket finder");
|
||||||
|
|
||||||
test_finder<LinearFinder>(word_list, "linear finder");
|
test_finder<LinearFinder>(word_list, "linear finder");
|
||||||
test_finder<SortedLinearFinder>(word_list, "sorted linear finder");
|
test_finder<SortedLinearFinder>(word_list, "sorted linear finder");
|
||||||
test_finder<ParallelFinder>(word_list, "parallel finder");
|
test_finder<ParallelFinder>(word_list, "parallel finder");
|
||||||
test_finder<TreeFinder>(word_list, "tree finder");
|
test_finder<TreeFinder>(word_list, "tree finder");
|
||||||
test_finder<GroupedFinder>(word_list, "grouped finder");
|
test_finder<GroupedFinder>(word_list, "grouped finder");
|
||||||
test_finder<BucketFinder>(word_list, "bucket finder");
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue