-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysortfilterproxymodel.cpp
More file actions
331 lines (269 loc) · 8.96 KB
/
Copy pathmysortfilterproxymodel.cpp
File metadata and controls
331 lines (269 loc) · 8.96 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "mysortfilterproxymodel.h"
#include <QDebug>
#include <QTime>
MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) :
QSortFilterProxyModel(parent),
sortedList(new QList<int>)
{
prevColumn = -1;
sorted = false;
connect(this, SIGNAL(modelReset()), this, SLOT(revertList()));
}
MySortFilterProxyModel::~MySortFilterProxyModel()
{
}
void MySortFilterProxyModel::sort(int column, Qt::SortOrder order)
{
QTime timer;
timer.start();
if ((prevColumn >= 0) && (prevColumn == column))
{
emit layoutAboutToBeChanged();
if (!sorted)
std::reverse(sortedList->begin(),sortedList->end());
emit layoutChanged();
}
else
{
emit layoutAboutToBeChanged();
switch (choice)
{
case QtMap:
sortByQMap(column, order);
break;
default:
sortWithFunc(column, order);
break;
}
emit layoutChanged();
}
sorted = true;
}
void MySortFilterProxyModel::sortByQMap(int column, Qt::SortOrder order)
{
QMultiMap<QVariant, int> sortedColumn = QMultiMap<QVariant, int>();
QVariant currentValue;
int currentRow;
for (int i=0; i<sourceModel()->rowCount(); i++)
{
currentValue = sourceModel()->data(sourceModel()->index(i,column));
currentRow = i;
sortedColumn.insert(currentValue, currentRow);
}
*sortedList = sortedColumn.values();
if (order == Qt::DescendingOrder)
std::reverse(sortedList->begin(),sortedList->end());
}
void MySortFilterProxyModel::sortWithFunc(int column, Qt::SortOrder order)
{
Container currentItem;
QVector<Container> dataColumn = QVector<Container>();
QVariant currentValue;
int currentRow;
for (int i=0; i<sourceModel()->rowCount(); i++)
{
currentValue = sourceModel()->data(sourceModel()->index(i,column));
currentRow = i;
currentItem.m_key = currentValue;
currentItem.m_value = currentRow;
dataColumn.append(currentItem);
}
switch (choice)
{
case QuickSort:
{
// void (*quickSortPtr) (QVector<Container>::Iterator, QVector<Container>::Iterator) = &quickSort;
// worker(&dataColumn, quickSortPtr, QThread::idealThreadCount());
quickSort(&dataColumn);
break;
}
case HeapSort:
{
// void (*heapSortPtr) (QVector<Container>::Iterator, QVector<Container>::Iterator) = &heapSort;
// worker(&dataColumn, heapSortPtr, QThread::idealThreadCount());
heapSort(&dataColumn);
break;
}
case StableSort:
{
// void (*stableSortPtr) (QVector<Container>::Iterator, QVector<Container>::Iterator) = &stableSort;
// worker(&dataColumn, stableSortPtr, QThread::idealThreadCount());
stableSort(&dataColumn);
break;
}
case SimpleSort:
{
// void (*simpleSortPtr) (QVector<Container>::Iterator, QVector<Container>::Iterator) = &simpleSort;
// worker(&dataColumn, simpleSortPtr, QThread::idealThreadCount());
simpleSort(&dataColumn);
break;
}
case TimSort:
{
// void (*timSortPtr) (QVector<Container>::Iterator, QVector<Container>::Iterator) = &timSort;
// worker(&dataColumn, timSortPtr, QThread::idealThreadCount());
timSort(&dataColumn);
break;
}
default:
break;
}
*sortedList = values(dataColumn);
if (order == Qt::DescendingOrder)
std::reverse(sortedList->begin(),sortedList->end());
}
// вернуть индекс прокси модели который соответствует индексу исходной модели
QModelIndex MySortFilterProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
if (!sourceIndex.isValid())
return QModelIndex();
else
{
if (sortedList->isEmpty())
return QSortFilterProxyModel::mapFromSource(sourceIndex);
else
return createIndex(sortedList->indexOf(sourceIndex.row()),sourceIndex.column());
}
}
// вернуть индекс исходной модели который соответствует индексу прокси модели
QModelIndex MySortFilterProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
if (!proxyIndex.isValid())
return QModelIndex();
else
{
if (sortedList->isEmpty())
return QSortFilterProxyModel::mapToSource(proxyIndex);
else
return createIndex(sortedList->at(proxyIndex.row()),proxyIndex.column());
}
}
Qt::ItemFlags MySortFilterProxyModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
void MySortFilterProxyModel::setSorted(bool value)
{
sorted = value;
}
void MySortFilterProxyModel::setPrevColumn(int value)
{
prevColumn = value;
}
void MySortFilterProxyModel::revertList()
{
prevColumn = -1;
sortedList->clear();
}
void MySortFilterProxyModel::giveSortChoice(MySortingMethods ch)
{
choice = ch;
}
QTime stTimer;
StProxyModel::StProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
{
}
bool StProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
stTimer.restart();
bool res = QSortFilterProxyModel::lessThan(left, right);
m_time += stTimer.elapsed();
return res;
}
void worker(QVector<Container> *arr, void (*sortFunc)(QVector<Container>::Iterator, QVector<Container>::Iterator), int threadCount)
{
int size = arr->size();
QVector< QFuture<void> > futureMass;
QVector<Container>::Iterator begin = arr->begin();
QVector<Container>::Iterator end = begin + size/threadCount + 1;
for (auto i = 0; i < threadCount; i++)
{
QFuture<void> future = QtConcurrent::run(sortFunc, begin, end);
begin = end;
end = begin + size/threadCount;
futureMass.append(future);
}
for (auto i = 0; i < threadCount; i++)
{
futureMass.value(i).waitForFinished();
}
myMerge(arr, (threadCount / 2));
}
// не удавшаяся реализация многопоточного слияния массивов
/*
void myMerge(QVector<Container> *arr, int count)
{
int step = count * 2;
int size = arr->size();
if (count > 0)
{
QVector<Container> tmp = QVector<Container>();
QVector< QFuture<void> > futureMass = QVector< QFuture<void> >();
tmp.fill(Container(), size);
QVector<Container>::Iterator begin1 = arr->begin();
QVector<Container>::Iterator end1 = begin1 + size/step + 1;
QVector<Container>::Iterator begin2 = end1;
QVector<Container>::Iterator end2 = begin2 + size/step;
QVector<Container>::Iterator tmpIt = tmp.begin();
for (auto i = 0; i < count; i++)
{
QFuture<void> future = QtConcurrent::run(std::merge, begin1, end1, begin2, end2, tmpIt,
[](const Container& a, const Container& b) -> bool
{
return a.m_key < b.m_key;
});
if (i)
tmpIt += size/count;
else
tmpIt += size/count + 1;
begin1 = end2;
end1 = begin1 + size/step;
begin2 = end1;
end2 = begin2 + size/step;
if (i)
tmpIt += count;
else
tmpIt += count + 1;
futureMass.append(future);
}
for (auto i = 0; i < count; i++)
{
futureMass.value(i).waitForFinished();
}
std::copy(tmp.begin(), tmp.end(), arr->begin());
myMerge(arr, (count / 2));
}
}
*/
void myMerge(QVector<Container> *arr, int count)
{
int step = count * 2;
int size = arr->size();
if (count > 0)
{
QVector<Container> tmp;
tmp.fill(Container(), size);
QVector<Container>::Iterator begin1 = arr->begin();
QVector<Container>::Iterator end1 = begin1 + size/step + 1;
QVector<Container>::Iterator begin2 = end1;
QVector<Container>::Iterator end2 = begin2 + size/step;
QVector<Container>::Iterator tmpIt = tmp.begin();
for (auto i = 0; i < count; i++)
{
tmpIt = std::merge(begin1, end1, begin2, end2, tmpIt,
[](const Container& a, const Container& b) -> bool
{
return a.m_key < b.m_key;
});
begin1 = end2;
end1 = begin1 + size/step;
begin2 = end1;
end2 = begin2 + size/step;
}
std::copy(tmp.begin(), tmp.end(), arr->begin());
myMerge(arr, (count / 2));
}
}