Compare commits
No commits in common. "6a67e5685f1e4bda9ca7a84e0ece9f08068ac5b2" and "9fd306204145b46a808304254ffc11f2b2e29266" have entirely different histories.
6a67e5685f
...
9fd3062041
|
@ -2,7 +2,6 @@
|
||||||
#include "./ui_mainwindow.h"
|
#include "./ui_mainwindow.h"
|
||||||
|
|
||||||
#include "finder.h"
|
#include "finder.h"
|
||||||
#include "grouped_finder.h"
|
|
||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
#include "parallel_finder.h"
|
#include "parallel_finder.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
@ -10,6 +9,7 @@
|
||||||
#include "word_list_generator.h"
|
#include "word_list_generator.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent)
|
MainWindow::MainWindow(QWidget *parent)
|
||||||
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||||
|
@ -28,7 +28,6 @@ void MainWindow::setupAlgorithmSelector() {
|
||||||
search_algorithms_.appendRow(new QStandardItem("Linear search"));
|
search_algorithms_.appendRow(new QStandardItem("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"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::generateWordList() {
|
void MainWindow::generateWordList() {
|
||||||
|
@ -69,14 +68,12 @@ 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<ParallelFinder>(
|
||||||
|
word_list_, std::thread::hardware_concurrency());
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
finder_ = std::make_unique<TreeFinder>(word_list_);
|
finder_ = std::make_unique<TreeFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
case 3:
|
|
||||||
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,7 @@ add_library(
|
||||||
src/parallel_finder.cpp
|
src/parallel_finder.cpp
|
||||||
include/parallel_finder.h
|
include/parallel_finder.h
|
||||||
src/tree_finder.cpp
|
src/tree_finder.cpp
|
||||||
include/tree_finder.h
|
include/tree_finder.h)
|
||||||
src/grouped_finder.cpp
|
|
||||||
include/grouped_finder.h)
|
|
||||||
|
|
||||||
target_include_directories(vector_search PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
target_include_directories(vector_search PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "finder.h"
|
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class GroupedFinder : public Finder {
|
|
||||||
private:
|
|
||||||
std::map<char, std::vector<const std::string *>> groups_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
GroupedFinder(const std::vector<string> &word_list);
|
|
||||||
|
|
||||||
virtual std::forward_list<const std::string *>
|
|
||||||
find_prefix(std::string_view search_term) const override;
|
|
||||||
};
|
|
|
@ -6,10 +6,11 @@
|
||||||
|
|
||||||
class ParallelFinder : public Finder {
|
class ParallelFinder : public Finder {
|
||||||
private:
|
private:
|
||||||
|
const size_t thread_count_;
|
||||||
const std::vector<string> &word_list_;
|
const std::vector<string> &word_list_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ParallelFinder(const std::vector<string> &word_list);
|
ParallelFinder(const std::vector<string> &word_list, size_t thread_count);
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
#include "grouped_finder.h"
|
|
||||||
|
|
||||||
#include <mutex>
|
|
||||||
#include <thread>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using std::mutex, std::vector, std::thread, std::lock_guard;
|
|
||||||
|
|
||||||
GroupedFinder::GroupedFinder(const std::vector<string> &word_list) {
|
|
||||||
for (const auto &word : word_list) {
|
|
||||||
groups_[word.front()].push_back(&word);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::forward_list<const std::string *>
|
|
||||||
GroupedFinder::find_prefix(std::string_view search_term) const {
|
|
||||||
const auto group = groups_.find(search_term.front());
|
|
||||||
if (group == groups_.cend()) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto word_list = group->second;
|
|
||||||
const auto word_list_size = word_list.size();
|
|
||||||
|
|
||||||
const auto thread_count = std::thread::hardware_concurrency();
|
|
||||||
|
|
||||||
forward_list<const string *> matching_words;
|
|
||||||
mutex matching_words_mutex;
|
|
||||||
|
|
||||||
vector<thread> search_threads;
|
|
||||||
for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
|
|
||||||
const size_t first_word_index =
|
|
||||||
thread_index * (word_list_size / thread_count);
|
|
||||||
const size_t last_word_index =
|
|
||||||
(thread_index == thread_count - 1)
|
|
||||||
? word_list_size
|
|
||||||
: (thread_index + 1) * (word_list_size / thread_count);
|
|
||||||
|
|
||||||
search_threads.emplace_back(
|
|
||||||
[](const vector<const string *> &word_list,
|
|
||||||
const string_view &search_term, forward_list<const string *> &result,
|
|
||||||
size_t start_index, size_t end_index, mutex &result_mutex) {
|
|
||||||
forward_list<const string *> thread_results;
|
|
||||||
for (size_t index = start_index; index < end_index; ++index) {
|
|
||||||
const auto ¤t_word = word_list[index];
|
|
||||||
if (current_word->starts_with(search_term)) {
|
|
||||||
thread_results.push_front(current_word);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!thread_results.empty()) {
|
|
||||||
const lock_guard<mutex> lock(result_mutex);
|
|
||||||
result.merge(thread_results);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
cref(word_list), cref(search_term), ref(matching_words),
|
|
||||||
first_word_index, last_word_index, ref(matching_words_mutex));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &thread : search_threads) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
return matching_words;
|
|
||||||
}
|
|
|
@ -5,26 +5,23 @@
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
ParallelFinder::ParallelFinder(const vector<string> &word_list)
|
ParallelFinder::ParallelFinder(const vector<string> &word_list,
|
||||||
: word_list_(word_list) {}
|
size_t thread_count)
|
||||||
|
: word_list_(word_list), thread_count_(thread_count) {}
|
||||||
|
|
||||||
forward_list<const string *>
|
forward_list<const string *>
|
||||||
ParallelFinder::find_prefix(string_view search_term) const {
|
ParallelFinder::find_prefix(string_view search_term) const {
|
||||||
forward_list<const string *> result;
|
forward_list<const string *> result;
|
||||||
mutex result_mutex;
|
mutex result_mutex;
|
||||||
|
|
||||||
const auto word_list_size = word_list_.size();
|
|
||||||
|
|
||||||
const size_t thread_count = thread::hardware_concurrency();
|
|
||||||
|
|
||||||
vector<thread> threads;
|
vector<thread> threads;
|
||||||
for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
|
for (size_t thread_index = 0; thread_index < thread_count_; ++thread_index) {
|
||||||
const size_t first_word_index =
|
const size_t first_word_index =
|
||||||
thread_index * (word_list_size / thread_count);
|
thread_index * (word_list_.size() / thread_count_);
|
||||||
const size_t last_word_index =
|
const size_t last_word_index =
|
||||||
(thread_index == thread_count - 1)
|
(thread_index == thread_count_ - 1)
|
||||||
? word_list_size
|
? word_list_.size()
|
||||||
: (thread_index + 1) * (word_list_size / thread_count);
|
: (thread_index + 1) * (word_list_.size() / thread_count_);
|
||||||
|
|
||||||
threads.emplace_back(
|
threads.emplace_back(
|
||||||
[](const vector<string> &word_list, const string_view &search_term,
|
[](const vector<string> &word_list, const string_view &search_term,
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
#include "grouped_finder.h"
|
|
||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
#include "parallel_finder.h"
|
#include "parallel_finder.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
@ -25,37 +24,36 @@ vector<string> generate_word_list() {
|
||||||
return word_list;
|
return word_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_finder_search(Finder &finder, std::string_view name,
|
|
||||||
std::string_view search_term) {
|
|
||||||
Timer find_timer;
|
|
||||||
auto result = finder.find_prefix(search_term);
|
|
||||||
find_timer.stop();
|
|
||||||
|
|
||||||
cout << name << "(" << search_term << ") took " << find_timer << endl;
|
|
||||||
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
|
||||||
<< " element(s) long" << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_finder(Finder &finder, std::string_view name) {
|
|
||||||
for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) {
|
|
||||||
test_finder_search(finder, name, search_term);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_linear_finder(const vector<string> &word_list) {
|
void test_linear_finder(const vector<string> &word_list) {
|
||||||
cout << "\nrunning linear finder" << endl;
|
cout << "\nrunning linear finder" << endl;
|
||||||
|
|
||||||
LinearFinder linear_finder(word_list);
|
LinearFinder linear_finder(word_list);
|
||||||
|
|
||||||
test_finder(linear_finder, "linear finder");
|
Timer find_timer;
|
||||||
|
auto result = linear_finder.find_prefix("ABCD");
|
||||||
|
find_timer.stop();
|
||||||
|
|
||||||
|
cout << "linear finder took " << find_timer << endl;
|
||||||
|
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
||||||
|
<< " element(s) long" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_parallel_finder(const vector<string> &word_list) {
|
void test_parallel_finder(const vector<string> &word_list) {
|
||||||
cout << "\nrunning parallel finder" << endl;
|
cout << "\nrunning parallel finder" << endl;
|
||||||
|
|
||||||
ParallelFinder parallel_finder(word_list);
|
const size_t thread_count = thread::hardware_concurrency();
|
||||||
|
|
||||||
test_finder(parallel_finder, "parallel finder");
|
cout << "using " << thread_count << " threads" << endl;
|
||||||
|
|
||||||
|
ParallelFinder parallel_finder(word_list, thread_count);
|
||||||
|
|
||||||
|
Timer find_timer;
|
||||||
|
auto result = parallel_finder.find_prefix("ABCD");
|
||||||
|
find_timer.stop();
|
||||||
|
|
||||||
|
cout << "parallel finder took " << find_timer << endl;
|
||||||
|
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
||||||
|
<< " element(s) long" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_tree_finder(const vector<string> &word_list) {
|
void test_tree_finder(const vector<string> &word_list) {
|
||||||
|
@ -64,20 +62,16 @@ void test_tree_finder(const vector<string> &word_list) {
|
||||||
Timer constructor_timer;
|
Timer constructor_timer;
|
||||||
TreeFinder tree_finder(word_list);
|
TreeFinder tree_finder(word_list);
|
||||||
constructor_timer.stop();
|
constructor_timer.stop();
|
||||||
|
|
||||||
cout << "tree finder constructor took " << constructor_timer << endl;
|
cout << "tree finder constructor took " << constructor_timer << endl;
|
||||||
|
|
||||||
test_finder(tree_finder, "tree finder");
|
Timer find_timer;
|
||||||
}
|
auto result = tree_finder.find_prefix("ABCD");
|
||||||
|
find_timer.stop();
|
||||||
|
|
||||||
void test_grouped_finder(const vector<string> &word_list) {
|
cout << "tree finder took " << find_timer << endl;
|
||||||
cout << "\nrunning grouped finder" << endl;
|
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
||||||
|
<< " element(s) long" << 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[]) {
|
||||||
|
@ -88,7 +82,6 @@ int main(int argc, char *argv[]) {
|
||||||
test_linear_finder(word_list);
|
test_linear_finder(word_list);
|
||||||
test_parallel_finder(word_list);
|
test_parallel_finder(word_list);
|
||||||
test_tree_finder(word_list);
|
test_tree_finder(word_list);
|
||||||
test_grouped_finder(word_list);
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue