feat: add sorted linear finder
This commit is contained in:
parent
291d8d198e
commit
26d3839832
5 changed files with 80 additions and 6 deletions
|
@ -5,6 +5,7 @@
|
|||
#include "grouped_finder.h"
|
||||
#include "linear_finder.h"
|
||||
#include "parallel_finder.h"
|
||||
#include "sorted_linear_finder.h"
|
||||
#include "timer.h"
|
||||
#include "tree_finder.h"
|
||||
#include "word_list_generator.h"
|
||||
|
@ -26,6 +27,7 @@ MainWindow::~MainWindow() { delete ui; }
|
|||
|
||||
void MainWindow::setupAlgorithmSelector() {
|
||||
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("Tree search"));
|
||||
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
|
||||
|
@ -69,12 +71,15 @@ void MainWindow::createSelectedFinder() {
|
|||
finder_ = std::make_unique<LinearFinder>(word_list_);
|
||||
break;
|
||||
case 1:
|
||||
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
||||
finder_ = std::make_unique<SortedLinearFinder>(word_list_);
|
||||
break;
|
||||
case 2:
|
||||
finder_ = std::make_unique<TreeFinder>(word_list_);
|
||||
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
||||
break;
|
||||
case 3:
|
||||
finder_ = std::make_unique<TreeFinder>(word_list_);
|
||||
break;
|
||||
case 4:
|
||||
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ add_library(
|
|||
include/timer.h
|
||||
src/linear_finder.cpp
|
||||
include/linear_finder.h
|
||||
src/sorted_linear_finder.cpp
|
||||
include/sorted_linear_finder.h
|
||||
src/parallel_finder.cpp
|
||||
include/parallel_finder.h
|
||||
src/tree_finder.cpp
|
||||
|
|
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;
|
||||
};
|
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,6 +1,7 @@
|
|||
#include "grouped_finder.h"
|
||||
#include "linear_finder.h"
|
||||
#include "parallel_finder.h"
|
||||
#include "sorted_linear_finder.h"
|
||||
#include "timer.h"
|
||||
#include "tree_finder.h"
|
||||
#include "word_list_generator.h"
|
||||
|
@ -10,11 +11,11 @@
|
|||
|
||||
using std::string, std::string_view, std::vector, std::cout, std::endl;
|
||||
|
||||
vector<string> generate_word_list() {
|
||||
cout << "\ngenerating word list" << endl;
|
||||
vector<string> generate_word_list(size_t size_factor = 1) {
|
||||
cout << "\ngenerating word list (" << size_factor << "x)" << endl;
|
||||
|
||||
Timer generator_timer;
|
||||
auto word_list = WordListGenerator::generate(5);
|
||||
auto word_list = WordListGenerator::generate(size_factor);
|
||||
generator_timer.stop();
|
||||
|
||||
cout << "word list generator took " << generator_timer << endl;
|
||||
|
@ -43,15 +44,32 @@ void test_finder(Finder &finder, std::string_view name) {
|
|||
void test_linear_finder(const vector<string> &word_list) {
|
||||
cout << "\nrunning linear finder" << endl;
|
||||
|
||||
Timer constructor_timer;
|
||||
LinearFinder linear_finder(word_list);
|
||||
constructor_timer.stop();
|
||||
cout << "linear finder constructor took " << constructor_timer << endl;
|
||||
|
||||
test_finder(linear_finder, "linear finder");
|
||||
}
|
||||
|
||||
void test_sorted_linear_finder(const vector<string> &word_list) {
|
||||
cout << "\nrunning sorted linear finder" << endl;
|
||||
|
||||
Timer constructor_timer;
|
||||
SortedLinearFinder sorted_linear_finder(word_list);
|
||||
constructor_timer.stop();
|
||||
cout << "sorted linear finder constructor took " << constructor_timer << endl;
|
||||
|
||||
test_finder(sorted_linear_finder, "sorted linear finder");
|
||||
}
|
||||
|
||||
void test_parallel_finder(const vector<string> &word_list) {
|
||||
cout << "\nrunning parallel finder" << endl;
|
||||
|
||||
Timer constructor_timer;
|
||||
ParallelFinder parallel_finder(word_list);
|
||||
constructor_timer.stop();
|
||||
cout << "linear finder constructor took " << constructor_timer << endl;
|
||||
|
||||
test_finder(parallel_finder, "parallel finder");
|
||||
}
|
||||
|
@ -81,9 +99,10 @@ void test_grouped_finder(const vector<string> &word_list) {
|
|||
int main(int argc, char *argv[]) {
|
||||
cout << "\n== VectorSearch ==" << endl;
|
||||
|
||||
auto word_list = generate_word_list();
|
||||
auto word_list = generate_word_list(5);
|
||||
|
||||
test_linear_finder(word_list);
|
||||
test_sorted_linear_finder(word_list);
|
||||
test_parallel_finder(word_list);
|
||||
test_tree_finder(word_list);
|
||||
test_grouped_finder(word_list);
|
||||
|
|
Loading…
Reference in a new issue