Compare commits
3 Commits
291d8d198e
...
076969f393
Author | SHA1 | Date | |
---|---|---|---|
076969f393 | |||
ad8a9ada83 | |||
26d3839832 |
@ -5,6 +5,7 @@
|
|||||||
#include "grouped_finder.h"
|
#include "grouped_finder.h"
|
||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
#include "parallel_finder.h"
|
#include "parallel_finder.h"
|
||||||
|
#include "sorted_linear_finder.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "tree_finder.h"
|
#include "tree_finder.h"
|
||||||
#include "word_list_generator.h"
|
#include "word_list_generator.h"
|
||||||
@ -26,6 +27,7 @@ MainWindow::~MainWindow() { delete ui; }
|
|||||||
|
|
||||||
void MainWindow::setupAlgorithmSelector() {
|
void MainWindow::setupAlgorithmSelector() {
|
||||||
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("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"));
|
||||||
@ -69,12 +71,15 @@ void MainWindow::createSelectedFinder() {
|
|||||||
finder_ = std::make_unique<LinearFinder>(word_list_);
|
finder_ = std::make_unique<LinearFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
finder_ = std::make_unique<SortedLinearFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
finder_ = std::make_unique<TreeFinder>(word_list_);
|
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
|
finder_ = std::make_unique<TreeFinder>(word_list_);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,8 @@ add_library(
|
|||||||
include/timer.h
|
include/timer.h
|
||||||
src/linear_finder.cpp
|
src/linear_finder.cpp
|
||||||
include/linear_finder.h
|
include/linear_finder.h
|
||||||
|
src/sorted_linear_finder.cpp
|
||||||
|
include/sorted_linear_finder.h
|
||||||
src/parallel_finder.cpp
|
src/parallel_finder.cpp
|
||||||
include/parallel_finder.h
|
include/parallel_finder.h
|
||||||
src/tree_finder.cpp
|
src/tree_finder.cpp
|
||||||
|
@ -3,12 +3,10 @@
|
|||||||
#include <forward_list>
|
#include <forward_list>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using std::string, std::string_view, std::forward_list;
|
|
||||||
|
|
||||||
class Finder {
|
class Finder {
|
||||||
public:
|
public:
|
||||||
virtual ~Finder() = default;
|
virtual ~Finder() = default;
|
||||||
|
|
||||||
virtual forward_list<const string *>
|
virtual std::forward_list<const std::string *>
|
||||||
find_prefix(string_view search_term) const = 0;
|
find_prefix(std::string_view search_term) const = 0;
|
||||||
};
|
};
|
||||||
|
@ -10,7 +10,7 @@ private:
|
|||||||
std::map<char, std::vector<const std::string *>> groups_;
|
std::map<char, std::vector<const std::string *>> groups_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GroupedFinder(const std::vector<string> &word_list);
|
GroupedFinder(const std::vector<std::string> &word_list);
|
||||||
|
|
||||||
virtual std::forward_list<const std::string *>
|
virtual std::forward_list<const std::string *>
|
||||||
find_prefix(std::string_view search_term) const override;
|
find_prefix(std::string_view search_term) const override;
|
||||||
|
@ -8,10 +8,10 @@ using std::vector;
|
|||||||
|
|
||||||
class LinearFinder : public Finder {
|
class LinearFinder : public Finder {
|
||||||
private:
|
private:
|
||||||
const vector<string> &word_list_;
|
const vector<std::string> &word_list_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LinearFinder(const vector<string> &word_list);
|
LinearFinder(const vector<std::string> &word_list);
|
||||||
forward_list<const string *>
|
std::forward_list<const std::string *>
|
||||||
find_prefix(string_view search_term) const override;
|
find_prefix(std::string_view search_term) const override;
|
||||||
};
|
};
|
||||||
|
@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
class ParallelFinder : public Finder {
|
class ParallelFinder : public Finder {
|
||||||
private:
|
private:
|
||||||
const std::vector<string> &word_list_;
|
const std::vector<std::string> &word_list_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ParallelFinder(const std::vector<string> &word_list);
|
ParallelFinder(const std::vector<std::string> &word_list);
|
||||||
|
|
||||||
std::forward_list<const std::string *>
|
std::forward_list<const std::string *>
|
||||||
find_prefix(std::string_view search_term) const override;
|
find_prefix(std::string_view search_term) const override;
|
||||||
|
16
lib_vector_search/include/sorted_linear_finder.h
Normal file
16
lib_vector_search/include/sorted_linear_finder.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "finder.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class SortedLinearFinder : public Finder {
|
||||||
|
private:
|
||||||
|
std::vector<const std::string *> word_list_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
SortedLinearFinder(const std::vector<std::string> &word_list);
|
||||||
|
|
||||||
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const override;
|
||||||
|
};
|
@ -11,7 +11,7 @@ private:
|
|||||||
std::map<const char, SearchTreeNode> children_;
|
std::map<const char, SearchTreeNode> children_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void insert(std::string_view partial_word, const 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;
|
||||||
|
|
||||||
std::forward_list<const std::string *> words() const;
|
std::forward_list<const std::string *> words() const;
|
||||||
@ -27,7 +27,7 @@ private:
|
|||||||
SearchTree search_tree_;
|
SearchTree search_tree_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TreeFinder(const std::vector<string> &word_list);
|
TreeFinder(const std::vector<std::string> &word_list);
|
||||||
|
|
||||||
virtual std::forward_list<const std::string *>
|
virtual std::forward_list<const std::string *>
|
||||||
find_prefix(std::string_view search_term) const override;
|
find_prefix(std::string_view search_term) const override;
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
#include "grouped_finder.h"
|
#include "grouped_finder.h"
|
||||||
|
|
||||||
|
#include <forward_list>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using std::mutex, std::vector, std::thread, std::lock_guard;
|
using std::mutex, std::vector, std::thread, std::lock_guard, std::string,
|
||||||
|
std::forward_list, std::string_view;
|
||||||
|
|
||||||
GroupedFinder::GroupedFinder(const std::vector<string> &word_list) {
|
GroupedFinder::GroupedFinder(const std::vector<string> &word_list) {
|
||||||
for (const auto &word : word_list) {
|
for (const auto &word : word_list) {
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
|
#include <forward_list>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
using std::string, std::forward_list, std::string_view;
|
||||||
|
|
||||||
LinearFinder::LinearFinder(const vector<string> &word_list)
|
LinearFinder::LinearFinder(const vector<string> &word_list)
|
||||||
: word_list_(word_list) {}
|
: word_list_(word_list) {}
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list;
|
using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list,
|
||||||
|
std::string, std::string_view;
|
||||||
|
|
||||||
ParallelFinder::ParallelFinder(const vector<string> &word_list)
|
ParallelFinder::ParallelFinder(const vector<string> &word_list)
|
||||||
: word_list_(word_list) {}
|
: word_list_(word_list) {}
|
||||||
|
32
lib_vector_search/src/sorted_linear_finder.cpp
Normal file
32
lib_vector_search/src/sorted_linear_finder.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "sorted_linear_finder.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
using std::vector, std::forward_list, std::string, std::string_view;
|
||||||
|
|
||||||
|
SortedLinearFinder::SortedLinearFinder(const vector<string> &word_list) {
|
||||||
|
std::transform(word_list.cbegin(), word_list.cend(),
|
||||||
|
std::back_inserter(word_list_),
|
||||||
|
[](const string &word) { return &word; });
|
||||||
|
|
||||||
|
std::sort(
|
||||||
|
word_list_.begin(), word_list_.end(),
|
||||||
|
[](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;
|
||||||
|
|
||||||
|
bool in_range = false;
|
||||||
|
for (const auto *word : word_list_) {
|
||||||
|
if (word->starts_with(search_term)) {
|
||||||
|
matching_words.push_front(word);
|
||||||
|
in_range = true;
|
||||||
|
} else if (in_range) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matching_words;
|
||||||
|
}
|
@ -1,17 +1,19 @@
|
|||||||
#include "tree_finder.h"
|
#include "tree_finder.h"
|
||||||
|
|
||||||
void SearchTreeNode::insert(std::string_view partial_word,
|
using std::string, std::string_view, std::forward_list, std::vector;
|
||||||
|
|
||||||
|
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_front(original_word);
|
words_.push_front(original_word);
|
||||||
} else {
|
} else {
|
||||||
children_[partial_word.front()].insert(
|
children_[partial_word.front()].insert(
|
||||||
std::string_view(partial_word).substr(1, partial_word.length()),
|
string_view(partial_word).substr(1, partial_word.length()),
|
||||||
original_word);
|
original_word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const SearchTreeNode *SearchTreeNode::find(std::string_view search_term) const {
|
const SearchTreeNode *SearchTreeNode::find(string_view search_term) const {
|
||||||
if (search_term.empty()) {
|
if (search_term.empty()) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -24,8 +26,8 @@ const SearchTreeNode *SearchTreeNode::find(std::string_view search_term) const {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::forward_list<const std::string *> SearchTreeNode::words() const {
|
forward_list<const string *> SearchTreeNode::words() const {
|
||||||
std::forward_list<const std::string *> results(words_);
|
forward_list<const string *> results(words_);
|
||||||
for (const auto &child : children_) {
|
for (const auto &child : children_) {
|
||||||
results.merge(child.second.words());
|
results.merge(child.second.words());
|
||||||
}
|
}
|
||||||
@ -33,17 +35,17 @@ std::forward_list<const std::string *> SearchTreeNode::words() const {
|
|||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
SearchTree::SearchTree(const std::vector<std::string> &word_list) {
|
SearchTree::SearchTree(const vector<string> &word_list) {
|
||||||
for (const auto &word : word_list) {
|
for (const auto &word : word_list) {
|
||||||
insert(std::string_view(word), &word);
|
insert(string_view(word), &word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TreeFinder::TreeFinder(const std::vector<string> &word_list)
|
TreeFinder::TreeFinder(const vector<string> &word_list)
|
||||||
: search_tree_(word_list) {}
|
: search_tree_(word_list) {}
|
||||||
|
|
||||||
std::forward_list<const std::string *>
|
forward_list<const string *>
|
||||||
TreeFinder::find_prefix(std::string_view search_term) const {
|
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 {};
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "grouped_finder.h"
|
#include "grouped_finder.h"
|
||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
#include "parallel_finder.h"
|
#include "parallel_finder.h"
|
||||||
|
#include "sorted_linear_finder.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "tree_finder.h"
|
#include "tree_finder.h"
|
||||||
#include "word_list_generator.h"
|
#include "word_list_generator.h"
|
||||||
@ -10,11 +11,11 @@
|
|||||||
|
|
||||||
using std::string, std::string_view, std::vector, std::cout, std::endl;
|
using std::string, std::string_view, std::vector, std::cout, std::endl;
|
||||||
|
|
||||||
vector<string> generate_word_list() {
|
vector<string> generate_word_list(size_t size_factor = 1) {
|
||||||
cout << "\ngenerating word list" << endl;
|
cout << "\ngenerating word list (" << size_factor << "x)" << endl;
|
||||||
|
|
||||||
Timer generator_timer;
|
Timer generator_timer;
|
||||||
auto word_list = WordListGenerator::generate(5);
|
auto word_list = WordListGenerator::generate(size_factor);
|
||||||
generator_timer.stop();
|
generator_timer.stop();
|
||||||
|
|
||||||
cout << "word list generator took " << generator_timer << endl;
|
cout << "word list generator took " << generator_timer << endl;
|
||||||
@ -34,59 +35,31 @@ void test_finder_search(Finder &finder, std::string_view name,
|
|||||||
<< " element(s) long" << endl;
|
<< " element(s) long" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_finder(Finder &finder, std::string_view name) {
|
template <typename FINDER>
|
||||||
|
void test_finder(const vector<string> &word_list,
|
||||||
|
std::string_view finder_name) {
|
||||||
|
cout << "\nrunning " << finder_name << endl;
|
||||||
|
|
||||||
|
Timer constructor_timer;
|
||||||
|
FINDER finder(word_list);
|
||||||
|
constructor_timer.stop();
|
||||||
|
cout << finder_name << " constructor took " << constructor_timer << endl;
|
||||||
|
|
||||||
for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) {
|
for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) {
|
||||||
test_finder_search(finder, name, search_term);
|
test_finder_search(finder, finder_name, search_term);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_linear_finder(const vector<string> &word_list) {
|
|
||||||
cout << "\nrunning linear finder" << endl;
|
|
||||||
|
|
||||||
LinearFinder linear_finder(word_list);
|
|
||||||
|
|
||||||
test_finder(linear_finder, "linear finder");
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_parallel_finder(const vector<string> &word_list) {
|
|
||||||
cout << "\nrunning parallel finder" << endl;
|
|
||||||
|
|
||||||
ParallelFinder parallel_finder(word_list);
|
|
||||||
|
|
||||||
test_finder(parallel_finder, "parallel finder");
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_tree_finder(const vector<string> &word_list) {
|
|
||||||
cout << "\nrunning tree finder" << endl;
|
|
||||||
|
|
||||||
Timer constructor_timer;
|
|
||||||
TreeFinder tree_finder(word_list);
|
|
||||||
constructor_timer.stop();
|
|
||||||
cout << "tree finder constructor took " << constructor_timer << endl;
|
|
||||||
|
|
||||||
test_finder(tree_finder, "tree finder");
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_grouped_finder(const vector<string> &word_list) {
|
|
||||||
cout << "\nrunning grouped finder" << endl;
|
|
||||||
|
|
||||||
Timer constructor_timer;
|
|
||||||
GroupedFinder grouped_finder(word_list);
|
|
||||||
constructor_timer.stop();
|
|
||||||
cout << "grouped finder constructor took " << constructor_timer << endl;
|
|
||||||
|
|
||||||
test_finder(grouped_finder, "grouped_finder");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
cout << "\n== VectorSearch ==" << endl;
|
cout << "\n== VectorSearch ==" << endl;
|
||||||
|
|
||||||
auto word_list = generate_word_list();
|
auto word_list = generate_word_list(5);
|
||||||
|
|
||||||
test_linear_finder(word_list);
|
test_finder<LinearFinder>(word_list, "linear finder");
|
||||||
test_parallel_finder(word_list);
|
test_finder<SortedLinearFinder>(word_list, "sorted linear finder");
|
||||||
test_tree_finder(word_list);
|
test_finder<ParallelFinder>(word_list, "parallel finder");
|
||||||
test_grouped_finder(word_list);
|
test_finder<TreeFinder>(word_list, "tree finder");
|
||||||
|
test_finder<GroupedFinder>(word_list, "grouped finder");
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user