-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
126 lines (103 loc) · 3.81 KB
/
mainwindow.cpp
File metadata and controls
126 lines (103 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <chrono>
#include <QtConcurrent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow), running(false) {
ui->setupUi(this);
int maxThreads = QThread::idealThreadCount();
ui->threadSlider->setMinimum(1);
ui->threadSlider->setMaximum(maxThreads);
ui->threadSlider->setValue(maxThreads);
connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::onStartClicked);
connect(ui->stopButton, &QPushButton::clicked, this, &MainWindow::onStopClicked);
ui->stopButton->setEnabled(false);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onStartClicked() {
int maxNumber = ui->limitSpinBox->value();
int numThreads = ui->threadSlider->value();
ui->startButton->setEnabled(false);
ui->stopButton->setEnabled(true);
ui->resultText->clear();
future = QtConcurrent::run([this, maxNumber, numThreads]() {
startCalculation(maxNumber, numThreads);
});
}
void MainWindow::onStopClicked() {
stopCalculation();
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(false);
}
void MainWindow::startCalculation(int maxNumber, int numThreads) {
running = true;
auto start = std::chrono::high_resolution_clock::now();
int result = findLongestCollatz(maxNumber, numThreads);
auto end = std::chrono::high_resolution_clock::now();
long long duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if (result != -1)
QMetaObject::invokeMethod(this, "onCalculationFinished", Qt::QueuedConnection,
Q_ARG(int, result),
Q_ARG(int, collatzLength(result)),
Q_ARG(long long, duration));
else
QMetaObject::invokeMethod(this, "onErrorMessage", Qt::QueuedConnection,
Q_ARG(QString, "Calculation was stopped."));
}
void MainWindow::stopCalculation() {
running = false;
}
int MainWindow::collatzLength(long long n) {
int length = 1;
while (n != 1) {
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
length++;
}
return length;
}
int MainWindow::findLongestCollatz(int maxNumber, int numThreads) {
int bestNumber = 1, maxLength = 0;
QVector<QFuture<std::pair<int, int>>> futures;
int chunkSize = maxNumber / numThreads;
for (int t = 0; t < numThreads; ++t) {
int start = t * chunkSize + 1;
int end = (t == numThreads - 1) ? maxNumber : start + chunkSize;
futures.append(QtConcurrent::run([this, start, end]() {
int localBest = 1, localMaxLength = 0;
for (int i = start; i <= end; i++) {
if (!running) return std::make_pair(-1, -1);
int len = collatzLength(i);
if (len > localMaxLength) {
localBest = i;
localMaxLength = len;
}
}
return std::make_pair(localBest, localMaxLength);
}));
}
for (auto &fut : futures) {
auto result = fut.result();
if (result.second > maxLength) {
bestNumber = result.first;
maxLength = result.second;
}
}
return bestNumber;
}
void MainWindow::onCalculationFinished(int number, int length, long long duration) {
ui->resultText->setText(QString("Number: %1\nLength: %2\nTime: %3 ms")
.arg(number).arg(length).arg(duration));
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(false);
}
void MainWindow::onErrorMessage(const QString &message) {
QMessageBox::warning(this, "Error", message);
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(false);
}