-
Notifications
You must be signed in to change notification settings - Fork 1
/
numberworker.cpp
102 lines (87 loc) · 2.94 KB
/
numberworker.cpp
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
/*
* Copyright (C) 2018 Wiebe Cazemier <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* https://www.gnu.org/licenses/gpl-2.0.html
*/
#include "numberworker.h"
#include <QThread>
#include <QDebug>
#include <QDateTime>
int NumberWorker::globalThreadCounter = 0;
NumberWorker::NumberWorker(const TaskPerThread task, QThread &thread, qint64 *totalResultList, const qint64 n, qint64 randSeed) :
QObject(0), // We can't have a parent, because then we can't move to a thread.
n(task.getN()),
nOffset(task.getNOffset()),
minInclusive(task.getMinInclusive()),
maxExclusive(task.getMaxExclusive()),
mCancelled(false),
mTotalResultList(totalResultList),
mN(n)
{
if (maxExclusive < n)
{
QString message = "There are more elements in the array then there are numbers available.";
qDebug() << message;
emit error(message);
}
moveToThread(&thread);
mThreadID = globalThreadCounter++;
x = randSeed;
}
void NumberWorker::doWork()
{
// merely for the progress signals
const int period = 10000;
const qint64 nStart = n;
for (qint64 i = maxExclusive; i > minInclusive && !mCancelled; i--)
{
// This is a well known linear congruential function for random number generation.
// Doing it locally, to avoid slow function calls and even slower mutexes. This does mean the seed value needs to be different
// per thread.
x = 48271 * x;
const qint64 random = x % (i - minInclusive);
const qint64 candidateValue = maxExclusive - i + minInclusive;
if (random < n)
{
n--;
// the + part is necessary because all threads are writing their portion in the same result.
mTotalResultList[n+nOffset] = candidateValue;
if (n % period == 0)
{
int percentDone = (nStart - n) / ((double)nStart) * 100;
emit progress(percentDone);
}
}
}
if (mCancelled)
return;
if (n != 0)
{
QString message = QString("n = %1, which is not 0, so the algorithm has a bug.").arg(n);
qDebug() << message;
emit error(message);
return;
}
emit resultReady();
}
void NumberWorker::cancel()
{
mCancelled = true;
}
int NumberWorker::getThreadID() const
{
return mThreadID;
}