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"
|
|
|
|
#include "word_list_generator.h"
|
|
|
|
|
2024-03-20 09:47:05 +00:00
|
|
|
#include <thread>
|
2024-03-20 09:59:07 +00:00
|
|
|
#include <sstream>
|
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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
|
|
|
|
QStringList words;
|
2024-03-20 09:59:07 +00:00
|
|
|
for (const auto &word : word_list_) {
|
2024-03-20 09:16:46 +00:00
|
|
|
words.append(QString::fromStdString(word));
|
|
|
|
}
|
|
|
|
|
|
|
|
result_model_.setStringList(words);
|
|
|
|
}
|
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 " << 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());
|
|
|
|
}
|