feat: use std::forward_list for results
This commit is contained in:
parent
510deb2d54
commit
ba460cc00a
@ -7,8 +7,8 @@
|
|||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "word_list_generator.h"
|
#include "word_list_generator.h"
|
||||||
|
|
||||||
#include <thread>
|
|
||||||
#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) {
|
||||||
@ -51,8 +51,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 "
|
||||||
<< " results";
|
<< 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);
|
||||||
@ -71,7 +71,8 @@ std::unique_ptr<Finder> MainWindow::createSelectedFinder() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::showResults(const std::vector<const std::string *> &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));
|
||||||
|
@ -29,7 +29,7 @@ private:
|
|||||||
void generateWordList();
|
void generateWordList();
|
||||||
void search(const QString &search_term);
|
void search(const QString &search_term);
|
||||||
std::unique_ptr<Finder> createSelectedFinder() const;
|
std::unique_ptr<Finder> createSelectedFinder() const;
|
||||||
void showResults(const std::vector<const std::string *> &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);
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <forward_list>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using std::string, std::string_view, std::vector;
|
using std::string, std::string_view, std::forward_list;
|
||||||
|
|
||||||
class Finder {
|
class Finder {
|
||||||
public:
|
public:
|
||||||
virtual ~Finder() = default;
|
virtual ~Finder() = default;
|
||||||
virtual vector<const string *> find_prefix(string_view search_term) const = 0;
|
|
||||||
|
virtual forward_list<const string *>
|
||||||
|
find_prefix(string_view search_term) const = 0;
|
||||||
};
|
};
|
||||||
|
@ -2,11 +2,16 @@
|
|||||||
|
|
||||||
#include "finder.h"
|
#include "finder.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using std::vector;
|
||||||
|
|
||||||
class LinearFinder : public Finder {
|
class LinearFinder : public Finder {
|
||||||
private:
|
private:
|
||||||
const vector<string> &word_list_;
|
const vector<string> &word_list_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LinearFinder(const vector<string> &word_list);
|
LinearFinder(const vector<string> &word_list);
|
||||||
vector<const string *> find_prefix(string_view search_term) const override;
|
forward_list<const string *>
|
||||||
|
find_prefix(string_view search_term) const override;
|
||||||
};
|
};
|
||||||
|
@ -2,12 +2,16 @@
|
|||||||
|
|
||||||
#include "finder.h"
|
#include "finder.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class ParallelFinder : public Finder {
|
class ParallelFinder : public Finder {
|
||||||
private:
|
private:
|
||||||
const size_t thread_count_;
|
const size_t thread_count_;
|
||||||
const vector<string> &word_list_;
|
const std::vector<string> &word_list_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ParallelFinder(const vector<string> &word_list, size_t thread_count);
|
ParallelFinder(const std::vector<string> &word_list, size_t thread_count);
|
||||||
vector<const string *> find_prefix(string_view search_term) const override;
|
|
||||||
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const override;
|
||||||
};
|
};
|
||||||
|
@ -3,13 +3,13 @@
|
|||||||
LinearFinder::LinearFinder(const vector<string> &word_list)
|
LinearFinder::LinearFinder(const vector<string> &word_list)
|
||||||
: word_list_(word_list) {}
|
: word_list_(word_list) {}
|
||||||
|
|
||||||
vector<const string *>
|
forward_list<const string *>
|
||||||
LinearFinder::find_prefix(string_view search_term) const {
|
LinearFinder::find_prefix(string_view search_term) const {
|
||||||
vector<const string *> matching_words;
|
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,15 +3,15 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
using std::mutex, std::thread, std::lock_guard;
|
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,
|
||||||
size_t thread_count)
|
size_t thread_count)
|
||||||
: word_list_(word_list), thread_count_(thread_count) {}
|
: word_list_(word_list), thread_count_(thread_count) {}
|
||||||
|
|
||||||
vector<const string *>
|
forward_list<const string *>
|
||||||
ParallelFinder::find_prefix(string_view search_term) const {
|
ParallelFinder::find_prefix(string_view search_term) const {
|
||||||
vector<const string *> result;
|
forward_list<const string *> result;
|
||||||
mutex result_mutex;
|
mutex result_mutex;
|
||||||
|
|
||||||
vector<thread> threads;
|
vector<thread> threads;
|
||||||
@ -25,13 +25,13 @@ ParallelFinder::find_prefix(string_view search_term) const {
|
|||||||
|
|
||||||
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,
|
||||||
vector<const string *> &result, size_t start_index, size_t end_index,
|
forward_list<const string *> &result, size_t start_index,
|
||||||
mutex &result_mutex) {
|
size_t end_index, mutex &result_mutex) {
|
||||||
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_term)) {
|
if (current_word.starts_with(search_term)) {
|
||||||
const lock_guard<mutex> lock(result_mutex);
|
const lock_guard<mutex> lock(result_mutex);
|
||||||
result.push_back(¤t_word);
|
result.push_front(¤t_word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -35,7 +35,8 @@ void test_linear_finder(const vector<string> &word_list) {
|
|||||||
find_timer.stop();
|
find_timer.stop();
|
||||||
|
|
||||||
cout << "linear finder took " << find_timer << endl;
|
cout << "linear finder took " << find_timer << endl;
|
||||||
cout << "result list is " << result.size() << " element(s) long" << 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) {
|
||||||
@ -54,7 +55,8 @@ void test_parallel_finder(const vector<string> &word_list) {
|
|||||||
find_timer.stop();
|
find_timer.stop();
|
||||||
|
|
||||||
cout << "parallel finder took " << find_timer << endl;
|
cout << "parallel finder took " << find_timer << endl;
|
||||||
cout << "result list is " << result.size() << " element(s) long" << endl;
|
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
||||||
|
<< " element(s) long" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
Reference in New Issue
Block a user