feat: implement search
This commit is contained in:
parent
e2f9b630c5
commit
2a8b342b91
@ -1,20 +1,33 @@
|
||||
#include "mainwindow.h"
|
||||
#include "./ui_mainwindow.h"
|
||||
|
||||
#include "finder.h"
|
||||
#include "linear_finder.h"
|
||||
#include "parallel_finder.h"
|
||||
#include "timer.h"
|
||||
#include "word_list_generator.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
ui->setupUi(this);
|
||||
ui->resultView->setModel(&result_model_);
|
||||
|
||||
generate_word_list();
|
||||
ui->resultView->setModel(&result_model_);
|
||||
ui->searchAlgorithmSelector->setModel(&search_algorithms_);
|
||||
|
||||
setupAlgorithmSelector();
|
||||
generateWordList();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() { delete ui; }
|
||||
|
||||
void MainWindow::generate_word_list() {
|
||||
void MainWindow::setupAlgorithmSelector() {
|
||||
search_algorithms_.appendRow(new QStandardItem("Linear search"));
|
||||
search_algorithms_.appendRow(new QStandardItem("Parallel search"));
|
||||
}
|
||||
|
||||
void MainWindow::generateWordList() {
|
||||
Timer timer;
|
||||
word_list_ = WordListGenerator().generate();
|
||||
timer.stop();
|
||||
@ -30,3 +43,53 @@ void MainWindow::generate_word_list() {
|
||||
|
||||
result_model_.setStringList(words);
|
||||
}
|
||||
|
||||
void MainWindow::search(const QString &search_term) {
|
||||
if (search_term.isEmpty()) {
|
||||
result_model_.setStringList(QStringList());
|
||||
ui->mainStatusBar->clearMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
const auto finder = createSelectedFinder();
|
||||
const auto results = finder->find_prefix(search_term.toStdString());
|
||||
timer.stop();
|
||||
|
||||
std::stringstream status_message;
|
||||
status_message << "search took " << timer << ", found " << results.size()
|
||||
<< " results";
|
||||
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
|
||||
|
||||
showResults(results);
|
||||
}
|
||||
|
||||
std::unique_ptr<Finder> MainWindow::createSelectedFinder() const {
|
||||
auto selectedFinder = ui->searchAlgorithmSelector->currentIndex();
|
||||
|
||||
switch (selectedFinder) {
|
||||
case 0:
|
||||
default:
|
||||
return std::make_unique<LinearFinder>(word_list_);
|
||||
case 1:
|
||||
return std::make_unique<ParallelFinder>(
|
||||
word_list_, std::thread::hardware_concurrency());
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::showResults(const std::vector<const std::string *> &results) {
|
||||
QStringList ui_results;
|
||||
for (auto word : results) {
|
||||
ui_results.append(QString::fromStdString(*word));
|
||||
}
|
||||
|
||||
result_model_.setStringList(ui_results);
|
||||
}
|
||||
|
||||
void MainWindow::on_searchInput_textChanged(const QString &search_term) {
|
||||
search(search_term);
|
||||
}
|
||||
|
||||
void MainWindow::on_searchAlgorithmSelector_currentIndexChanged(int) {
|
||||
search(ui->searchInput->displayText());
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include "finder.h"
|
||||
#include <QMainWindow>
|
||||
#include <QStandardItemModel>
|
||||
#include <QStringListModel>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
@ -16,13 +18,22 @@ class MainWindow : public QMainWindow {
|
||||
private:
|
||||
std::vector<std::string> word_list_;
|
||||
QStringListModel result_model_;
|
||||
QStandardItemModel search_algorithms_;
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
private:
|
||||
void generate_word_list();
|
||||
void setupAlgorithmSelector();
|
||||
void generateWordList();
|
||||
void search(const QString &search_term);
|
||||
std::unique_ptr<Finder> createSelectedFinder() const;
|
||||
void showResults(const std::vector<const std::string *> &results);
|
||||
|
||||
private slots:
|
||||
void on_searchInput_textChanged(const QString &search_term);
|
||||
void on_searchAlgorithmSelector_currentIndexChanged(int);
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
|
Reference in New Issue
Block a user