VectorSearch/QVectorSearch/mainwindow.cpp

95 lines
2.6 KiB
C++
Raw Normal View History

2024-03-19 20:42:32 +00:00
#include "mainwindow.h"
#include "./ui_mainwindow.h"
2024-03-20 09:47:05 +00:00
#include "finder.h"
#include "linear_finder.h"
#include "parallel_finder.h"
2024-03-20 09:16:46 +00:00
#include "timer.h"
2024-03-20 14:05:25 +00:00
#include "tree_finder.h"
2024-03-20 09:16:46 +00:00
#include "word_list_generator.h"
2024-03-20 09:59:07 +00:00
#include <sstream>
#include <thread>
2024-03-20 09:47:05 +00:00
2024-03-19 20:42:32 +00:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
2024-03-20 09:47:05 +00:00
2024-03-20 09:16:46 +00:00
ui->resultView->setModel(&result_model_);
2024-03-20 09:47:05 +00:00
ui->searchAlgorithmSelector->setModel(&search_algorithms_);
2024-03-20 09:16:46 +00:00
2024-03-20 09:47:05 +00:00
setupAlgorithmSelector();
generateWordList();
2024-03-19 20:42:32 +00:00
}
MainWindow::~MainWindow() { delete ui; }
2024-03-20 09:16:46 +00:00
2024-03-20 09:47:05 +00:00
void MainWindow::setupAlgorithmSelector() {
search_algorithms_.appendRow(new QStandardItem("Linear search"));
search_algorithms_.appendRow(new QStandardItem("Parallel search"));
2024-03-20 14:05:25 +00:00
search_algorithms_.appendRow(new QStandardItem("Tree search"));
2024-03-20 09:47:05 +00:00
}
void MainWindow::generateWordList() {
2024-03-20 09:16:46 +00:00
Timer timer;
word_list_ = WordListGenerator().generate();
timer.stop();
std::stringstream status_message;
status_message << "generated " << word_list_.size() << " words in " << timer;
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
}
2024-03-20 09:47:05 +00:00
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 "
<< std::distance(results.begin(), results.end()) << " results";
2024-03-20 09:47:05 +00:00
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());
2024-03-20 14:05:25 +00:00
case 2:
return std::make_unique<TreeFinder>(word_list_);
2024-03-20 09:47:05 +00:00
}
}
void MainWindow::showResults(
const std::forward_list<const std::string *> &results) {
2024-03-20 09:47:05 +00:00
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());
}