Skip to content

Commit 941948e

Browse files
jbowlermrbean-bremen
authored andcommitted
generator: AbstractMetaClassList: correct sorting (#132)
In some places the list was sorted by pointer (resulting in a random sort) in other cases by name, sometimes with stable_sort, sometimes not. Now sort by name and maintain do a stable_sort.
1 parent 8e57588 commit 941948e

File tree

5 files changed

+20
-22
lines changed

5 files changed

+20
-22
lines changed

generator/abstractmetabuilder.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
**
4040
****************************************************************************/
4141

42-
#include <algorithm> // for std::sort
43-
4442
#include "abstractmetabuilder.h"
4543
#include "reporthandler.h"
4644

@@ -380,15 +378,9 @@ void AbstractMetaBuilder::fixQObjectForScope(TypeDatabase *types,
380378
}
381379
}
382380

383-
static bool class_less_than(AbstractMetaClass *a, AbstractMetaClass *b)
384-
{
385-
return a->name() < b->name();
386-
}
387-
388-
389381
void AbstractMetaBuilder::sortLists()
390382
{
391-
std::sort(m_meta_classes.begin(), m_meta_classes.end(), class_less_than);
383+
m_meta_classes.sort();
392384
for (AbstractMetaClass *cls : m_meta_classes) {
393385
cls->sortFunctions();
394386
}
@@ -2491,7 +2483,7 @@ AbstractMetaClassList AbstractMetaBuilder::classesTopologicalSorted() const
24912483
AbstractMetaClassList res;
24922484

24932485
AbstractMetaClassList classes = m_meta_classes;
2494-
std::sort(classes.begin(), classes.end());
2486+
classes.sort();
24952487

24962488
QSet<AbstractMetaClass*> noDependency;
24972489
QHash<AbstractMetaClass*, QSet<AbstractMetaClass* >* > hash;

generator/abstractmetalang.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
**
4040
****************************************************************************/
4141

42-
#include <algorithm> // for std::sort
42+
#include <algorithm> // for std::stable_sort
4343

4444
#include "abstractmetalang.h"
4545
#include "reporthandler.h"
@@ -2023,3 +2023,9 @@ AbstractMetaClass *AbstractMetaClassList::findClass(const QString &name) const
20232023

20242024
return 0;
20252025
}
2026+
2027+
2028+
void AbstractMetaClassList::sort(void)
2029+
{
2030+
std::stable_sort(begin(), end(), AbstractMetaClass::less_than);
2031+
}

generator/abstractmetalang.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class AbstractMetaClassList : public QList<AbstractMetaClass *>
7171
AbstractMetaClass *findClass(const QString &name) const;
7272
AbstractMetaEnumValue *findEnumValue(const QString &string) const;
7373
AbstractMetaEnum *findEnum(const EnumTypeEntry *entry) const;
74+
void sort();
7475

7576
};
7677

@@ -835,6 +836,11 @@ class AbstractMetaClass : public AbstractMetaAttributes
835836
return qualifiedCppName() < a.qualifiedCppName();
836837
}
837838

839+
static bool less_than(const AbstractMetaClass *cl,
840+
const AbstractMetaClass *cr) {
841+
return cl->name() < cr->name();
842+
}
843+
838844
private:
839845
uint m_namespace : 1;
840846
uint m_qobject : 1;

generator/generator.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
**
4040
****************************************************************************/
4141

42-
#include <algorithm> // for std::stable_sort, std::sort
4342
#include "generator.h"
4443
#include "reporthandler.h"
4544
#include "fileout.h"
@@ -62,7 +61,7 @@ void Generator::generate()
6261
return;
6362
}
6463

65-
std::stable_sort(m_classes.begin(), m_classes.end());
64+
m_classes.sort();
6665

6766
foreach (AbstractMetaClass *cls, m_classes) {
6867
if (!shouldGenerate(cls))
@@ -86,7 +85,7 @@ void Generator::printClasses()
8685
QTextStream s(stdout);
8786

8887
AbstractMetaClassList classes = m_classes;
89-
std::sort(classes.begin(), classes.end());
88+
classes.sort();
9089

9190
foreach (AbstractMetaClass *cls, classes) {
9291
if (!shouldGenerate(cls))

generator/setupgenerator.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
**
4040
****************************************************************************/
4141

42-
#include <algorithm> // for std::sort
42+
#include <algorithm> // for std::sort, std::stable_sort
4343

4444
#include "setupgenerator.h"
4545
#include "shellgenerator.h"
@@ -139,11 +139,6 @@ static QStringList getOperatorCodes(const AbstractMetaClass* cls) {
139139
return result;
140140
}
141141

142-
static bool class_less_than(const AbstractMetaClass *a, const AbstractMetaClass *b)
143-
{
144-
return a->name() < b->name();
145-
}
146-
147142
static QSet<QString> _builtinListTypes = QSet<QString>() << "QByteArray"
148143
<< "QDate"
149144
<< "QTime"
@@ -242,15 +237,15 @@ void SetupGenerator::generate()
242237
}
243238
}
244239
}
245-
std::sort(classes_with_polymorphic_id.begin(), classes_with_polymorphic_id.end(), class_less_than);
240+
classes_with_polymorphic_id.sort();
246241

247242
QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash);
248243
while (pack.hasNext()) {
249244
pack.next();
250245
QList<const AbstractMetaClass*> list = pack.value();
251246
if (list.isEmpty())
252247
continue;
253-
std::sort(list.begin(), list.end(), class_less_than);
248+
std::stable_sort(list.begin(), list.end(), AbstractMetaClass::less_than);
254249

255250
QString packKey = pack.key();
256251
QString packName = pack.key();

0 commit comments

Comments
 (0)