2024-03-19 20:42:32 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "./ui_mainwindow.h"
|
|
|
|
|
2024-03-21 12:23:26 +00:00
|
|
|
#include "bucket_finder.h"
|
2024-03-20 09:47:05 +00:00
|
|
|
#include "finder.h"
|
2024-03-20 15:35:37 +00:00
|
|
|
#include "grouped_finder.h"
|
2024-03-20 09:47:05 +00:00
|
|
|
#include "linear_finder.h"
|
|
|
|
#include "parallel_finder.h"
|
2024-03-20 19:50:43 +00:00
|
|
|
#include "sorted_linear_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
|
|
|
|
2024-03-20 09:59:07 +00:00
|
|
|
#include <sstream>
|
2024-03-20 09:47:05 +00:00
|
|
|
|
2024-03-23 09:25:50 +00:00
|
|
|
MainWindow::MainWindow(QWidget *parent, bool enableIncrementalSearch,
|
|
|
|
bool enableDevMode)
|
|
|
|
: QMainWindow(parent), ui(new Ui::MainWindow),
|
|
|
|
incremental_search_(enableIncrementalSearch) {
|
2024-03-19 20:42:32 +00:00
|
|
|
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 20:45:06 +00:00
|
|
|
ui->wordListSourceSelector->setModel(&word_list_sources_);
|
2024-03-20 09:47:05 +00:00
|
|
|
ui->searchAlgorithmSelector->setModel(&search_algorithms_);
|
2024-03-20 09:16:46 +00:00
|
|
|
|
2024-03-23 09:25:50 +00:00
|
|
|
setDevMode(enableDevMode);
|
|
|
|
|
2024-03-20 09:47:05 +00:00
|
|
|
setupAlgorithmSelector();
|
2024-03-20 20:45:06 +00:00
|
|
|
setupWordListSourceSelector();
|
|
|
|
setupWordList();
|
2024-03-22 09:59:13 +00:00
|
|
|
createSelectedFinder();
|
2024-03-19 20:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow() { delete ui; }
|
2024-03-20 09:16:46 +00:00
|
|
|
|
2024-03-23 09:25:50 +00:00
|
|
|
void MainWindow::setDevMode(bool enable) {
|
|
|
|
ui->searchAlgorithmLabel->setVisible(enable);
|
|
|
|
ui->searchAlgorithmSelector->setVisible(enable);
|
|
|
|
|
|
|
|
ui->selectWordListLabel->setVisible(enable);
|
|
|
|
ui->wordListSourceSelector->setVisible(enable);
|
|
|
|
|
|
|
|
ui->selectWordListSizeLabel->setVisible(enable);
|
|
|
|
ui->wordListSizeSelector->setVisible(enable);
|
|
|
|
}
|
|
|
|
|
2024-03-20 09:47:05 +00:00
|
|
|
void MainWindow::setupAlgorithmSelector() {
|
2024-03-21 19:57:02 +00:00
|
|
|
search_algorithms_.appendRow(new QStandardItem("Bucket search"));
|
2024-03-20 09:47:05 +00:00
|
|
|
search_algorithms_.appendRow(new QStandardItem("Linear search"));
|
2024-03-20 19:50:43 +00:00
|
|
|
search_algorithms_.appendRow(new QStandardItem("Sorted linear search"));
|
2024-03-20 09:47:05 +00:00
|
|
|
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 15:35:37 +00:00
|
|
|
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
|
2024-03-20 09:47:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 20:45:06 +00:00
|
|
|
void MainWindow::setupWordListSourceSelector() {
|
|
|
|
word_list_sources_.appendRow(new QStandardItem("All 4-caps words"));
|
|
|
|
word_list_sources_.appendRow(new QStandardItem("English words"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::setupWordList() {
|
|
|
|
switch (ui->wordListSourceSelector->currentIndex()) {
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
generateWordList();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
loadWordList("words.txt");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 09:47:05 +00:00
|
|
|
void MainWindow::generateWordList() {
|
2024-03-20 09:16:46 +00:00
|
|
|
Timer timer;
|
2024-03-21 07:55:44 +00:00
|
|
|
word_list_ = WordList::fourCaps()
|
|
|
|
.multiply(ui->wordListSizeSelector->value())
|
|
|
|
.shuffle();
|
2024-03-20 09:16:46 +00:00
|
|
|
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
|
|
|
|
2024-03-20 20:45:06 +00:00
|
|
|
void MainWindow::loadWordList(std::filesystem::path path) {
|
|
|
|
Timer timer;
|
2024-03-21 07:55:44 +00:00
|
|
|
word_list_ = WordList::fromFile("words.txt")
|
|
|
|
.multiply(ui->wordListSizeSelector->value())
|
|
|
|
.shuffle();
|
2024-03-20 20:45:06 +00:00
|
|
|
timer.stop();
|
|
|
|
|
|
|
|
std::stringstream status_message;
|
|
|
|
status_message << "loaded " << 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()) {
|
2024-03-22 09:59:13 +00:00
|
|
|
clearResults();
|
2024-03-20 09:47:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer timer;
|
2024-03-20 14:20:46 +00:00
|
|
|
const auto results = finder_->find_prefix(search_term.toStdString());
|
2024-03-20 09:47:05 +00:00
|
|
|
timer.stop();
|
|
|
|
|
|
|
|
std::stringstream status_message;
|
2024-03-21 19:57:02 +00:00
|
|
|
status_message << "search took " << timer << ", found " << results.size()
|
|
|
|
<< " result(s) in " << word_list_.size() << " words.";
|
2024-03-20 09:47:05 +00:00
|
|
|
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
|
|
|
|
|
|
|
|
showResults(results);
|
|
|
|
}
|
|
|
|
|
2024-03-20 14:20:46 +00:00
|
|
|
void MainWindow::createSelectedFinder() {
|
2024-03-20 09:47:05 +00:00
|
|
|
auto selectedFinder = ui->searchAlgorithmSelector->currentIndex();
|
|
|
|
|
2024-03-22 09:59:13 +00:00
|
|
|
Timer timer;
|
2024-03-20 09:47:05 +00:00
|
|
|
switch (selectedFinder) {
|
|
|
|
case 0:
|
|
|
|
default:
|
2024-03-21 19:57:02 +00:00
|
|
|
finder_ = std::make_unique<BucketFinder>(word_list_);
|
2024-03-20 14:20:46 +00:00
|
|
|
break;
|
2024-03-20 09:47:05 +00:00
|
|
|
case 1:
|
2024-03-21 19:57:02 +00:00
|
|
|
finder_ = std::make_unique<LinearFinder>(word_list_);
|
2024-03-20 14:20:46 +00:00
|
|
|
break;
|
2024-03-20 14:05:25 +00:00
|
|
|
case 2:
|
2024-03-21 19:57:02 +00:00
|
|
|
finder_ = std::make_unique<SortedLinearFinder>(word_list_);
|
2024-03-20 14:20:46 +00:00
|
|
|
break;
|
2024-03-20 15:35:37 +00:00
|
|
|
case 3:
|
2024-03-21 19:57:02 +00:00
|
|
|
finder_ = std::make_unique<ParallelFinder>(word_list_);
|
2024-03-20 19:50:43 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2024-03-21 19:57:02 +00:00
|
|
|
finder_ = std::make_unique<TreeFinder>(word_list_);
|
2024-03-20 15:35:37 +00:00
|
|
|
break;
|
2024-03-21 12:23:26 +00:00
|
|
|
case 5:
|
2024-03-21 19:57:02 +00:00
|
|
|
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
2024-03-21 12:23:26 +00:00
|
|
|
break;
|
2024-03-20 09:47:05 +00:00
|
|
|
}
|
2024-03-22 09:59:13 +00:00
|
|
|
timer.stop();
|
|
|
|
|
|
|
|
std::stringstream status_message;
|
|
|
|
status_message << "finder initialized in " << timer << ", "
|
|
|
|
<< word_list_.size() << " words loaded";
|
|
|
|
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
|
2024-03-20 09:47:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 19:16:00 +00:00
|
|
|
void MainWindow::showResults(const WordRefList &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);
|
|
|
|
}
|
|
|
|
|
2024-03-22 09:59:13 +00:00
|
|
|
void MainWindow::clearResults() { result_model_.setStringList({}); }
|
|
|
|
|
2024-03-20 09:47:05 +00:00
|
|
|
void MainWindow::on_searchInput_textChanged(const QString &search_term) {
|
2024-03-23 09:25:50 +00:00
|
|
|
if (!incremental_search_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
search(search_term);
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
2024-03-20 09:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_searchAlgorithmSelector_currentIndexChanged(int) {
|
2024-03-20 17:38:44 +00:00
|
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
2024-03-22 09:59:13 +00:00
|
|
|
clearResults();
|
2024-03-20 14:20:46 +00:00
|
|
|
createSelectedFinder();
|
2024-03-20 17:38:44 +00:00
|
|
|
QGuiApplication::restoreOverrideCursor();
|
|
|
|
}
|
|
|
|
|
2024-03-20 20:45:06 +00:00
|
|
|
void MainWindow::on_wordListSourceSelector_currentIndexChanged(int) {
|
|
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
2024-03-22 09:59:13 +00:00
|
|
|
clearResults();
|
2024-03-20 20:45:06 +00:00
|
|
|
setupWordList();
|
|
|
|
createSelectedFinder();
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
|
|
|
}
|
|
|
|
|
2024-03-20 17:38:44 +00:00
|
|
|
void MainWindow::on_wordListSizeSelector_valueChanged(int) {
|
|
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
2024-03-22 09:59:13 +00:00
|
|
|
clearResults();
|
2024-03-20 20:45:06 +00:00
|
|
|
setupWordList();
|
2024-03-20 17:38:44 +00:00
|
|
|
createSelectedFinder();
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
2024-03-20 09:47:05 +00:00
|
|
|
}
|
2024-03-21 19:56:30 +00:00
|
|
|
|
|
|
|
void MainWindow::on_searchInput_returnPressed() {
|
|
|
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
search(ui->searchInput->displayText());
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
|
|
|
}
|