-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.cpp
309 lines (254 loc) · 8.06 KB
/
controller.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* 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 "controller.h"
#include <QDebug>
#include <taskperthread.h>
#include <algorithm>
/**
* @brief Controller::getTaskDivisions generates a list of ranges for each thread to work on.
* @param n is the amount of numbers to pick
* @param maxExclusive is the exclusive upper limit,
* @return
*
* Note: as it is now, the list has an aesthetic/cosmetic Endian-like issue. The algorithm works backwards, but the segments are
* distributed forwards. This is hard-ish to solve because the last thread gets the remainder from an uneven division, making it hard
* to inverse the nOffset.
*
* So, this is an example list (for the trained eye; yes, with random seeding issue):
*
* 24 -> created by thread 1
* 14 -> created by thread 1.
* 49 -> created by thread 2.
* 39 -> created by thread 2.
* 74 -> created by thread 3.
* 64 -> created by thread 3.
* 99 -> created by thread 4.
* 93 -> created by thread 4.
* 89 -> created by thread 4.
* 79 -> created by thread 4.
*
* If you were to sort descending by thread number but keep the order per thread, you get the order as a single thread version
* of the algorithm would have created. In other words, a list of unique descending values.
*/
QList<TaskPerThread> Controller::getTaskDivisions(const qint64 n, const qint64 maxExclusive)
{
QList<TaskPerThread> result;
const qint64 nPerThread = n / mNoOfThreads;
const qint64 nRemainder = n % mNoOfThreads;
const qint64 segmentSize = maxExclusive / mNoOfThreads;
const qint64 segmentRemainder = maxExclusive % mNoOfThreads;
for (int i = 0; i < mNoOfThreads; ++i)
{
qint64 min = segmentSize * i;
qint64 max = min + segmentSize;
TaskPerThread task(nPerThread, i*nPerThread, min, max);
result.append(task);
}
result.last().appendN(nRemainder);
result.last().appendMax(segmentRemainder);
return result;
}
void Controller::sort()
{
// TODO: Hmm, how am I going to emit progress events for this?
emit sortProgress(0);
std::sort(&mResultList[0], &mResultList[mN]);
emit sortProgress(100);
}
bool Controller::verifyUniqueness()
{
qint64 lastOne = -1;
qint64 counter = 0;
int lastProgress = -1;
for(qint64 i = 0; i < mN; ++i)
{
const qint64 value = mResultList[i];
if (lastOne == value)
{
emit error(QString("%1 was encountered twice.").arg(value));
return false;
}
if (value >= mMaxExclusive)
{
emit error(QString("Found %1, which is higher than the max-exclusive limit of %2.").arg(value).arg(mMaxExclusive));
return false;
}
if (value < 0)
{
emit error(QString("Negative value found: %1").arg(value));
return false;
}
lastOne = value;
++counter;
int progress = (counter / (double)mN) * 100;
if (lastProgress != progress) // to avoid millions of emits.
{
lastProgress = progress;
emit verifyProgress(progress);
}
}
return true;
}
/**
* Basic Fischer-Yates shuffle
*/
void Controller::shuffle()
{
int lastProgress = -1;
int counter = 1; // We start at 1, because the algorithm does n - 1 elements, because it doesn't need to swap the last one with itself.
for (int i = mN-1; i > 0; --i)
{
int j = mTsRandom.threadUnsafeNext(i + 1);
const qint64 temp = mResultList[j];
mResultList[j] = mResultList[i];
mResultList[i] = temp;
int progress = (++counter / (double)mN) * 100;
if (lastProgress != progress) // to avoid millions of emits.
{
lastProgress = progress;
emit shuffleProgress(progress);
}
}
}
QString Controller::nrToCharacterString(quint64 n) const
{
if ((qint64)n >= mMaxExclusive)
{
throw std::runtime_error("Trying to convert a number outside the allowed range.");
}
const int base = mChars.length();
QString result;
while(n != 0)
{
int remainder = n % base;
n = n / base;
result.insert(0, mChars[remainder]);
}
result = result.rightJustified(lineLength, mChars[0]);
return result;
}
void Controller::saveList(QString filename) const
{
QFile outputFile(filename);
if (outputFile.exists())
{
QString msg("%1 already exists. Not saving");
throw std::runtime_error(msg.toLatin1().data());
}
outputFile.open(QFile::ReadWrite);
QTextStream stream(&outputFile);
qDebug() << "Saving the list to " << filename;
for(qint64 i = 0; i < mN; i++)
{
quint64 value = mResultList[i];
QString line = nrToCharacterString(value);
stream << line << "\n";
}
qDebug() << "Done Saving the list";
}
Controller::Controller(const qint64 n, const qint64 maxExclusive, QObject *parent) :
QObject(parent),
mNoOfThreads(QThread::idealThreadCount ()),
mRunning(false),
mResultList(new qint64[n]),
mN(n),
mMaxExclusive(maxExclusive),
mWorkersDone(0),
mChars("234679acdefghjkmnpqrtxyz")
{
qRegisterMetaType<QSharedPointer<QVector<qint64> > >("QSharedPointer<QVector<qint64> >");
foreach(TaskPerThread task, getTaskDivisions(n, maxExclusive))
{
QThread * thread = new QThread(this);
mThreads.append(thread);
NumberWorker * worker = new NumberWorker(task, *thread, mResultList, mN, mTsRandom.threadSafeNext());
mWorkers.append(worker);
connect(this, &Controller::operate, worker, &NumberWorker::doWork);
connect(worker, &NumberWorker::resultReady, this, &Controller::handleWorkerResults);
connect(worker, &NumberWorker::error, this, &Controller::error);
thread->start();
}
}
Controller::~Controller()
{
cancel();
foreach (QThread *thread, mThreads)
{
thread->quit();
thread->wait();
}
foreach (NumberWorker *worker, mWorkers)
{
// We could use delete later, but that used to require an event loop, which we no longer have because we quit the thread. However,
// I think since qt 4.8, that also works without event loop. But, since no queued connections are anything of the sort are
// active anymore, we can just delete it now.
delete worker;
}
delete[] mResultList;
}
void Controller::start()
{
if (mRunning)
return;
mRunning = true;
mWorkersDone = 0;
emit operate(); // Not calling stuff directly on the worker because it's been moved to another thread. Instead using this queued connection.
emit started();
}
int Controller::getNrOfThreads()
{
return mNoOfThreads;
}
QList<NumberWorker *> Controller::getWorkers()
{
return mWorkers;
}
void Controller::cancel()
{
foreach (NumberWorker *worker, mWorkers)
{
worker->cancel();
}
mWorkersDone = 0;
mRunning = false;
emit stopped(false);
}
bool Controller::isRunning() const
{
return mRunning;
}
void Controller::handleWorkerResults()
{
mWorkersDone++;
if (mWorkersDone == mNoOfThreads)
{
bool result = true;
sort();
if (!verifyUniqueness())
result = false;
shuffle();
// Debug; remove later.
//for(qint64 i = 0; i < mN; ++i)
//{
// qDebug() << mResultList[i];
//}
mRunning = false;
emit stopped(result);
}
}