Skip to content

Commit 63be82c

Browse files
committed
Optimize Node::findChildren
- I do not need QQueue for this - This should result in more optimized CPU caching
1 parent afac708 commit 63be82c

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

ResultModel.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <QDebug>
44
#include <QFile>
5-
#include <QQueue>
65
#include <QVector>
76

87
inline Node* indexToNode(const QModelIndex& index)
@@ -64,33 +63,33 @@ class Node
6463
return child;
6564
}
6665

67-
QVector<Node*> findChildren(const std::function<bool(const Node*)>& lambda) const
66+
QVector<Node*> findChildren(const std::function<bool(const Node*)>& predicate) const
6867
{
6968
QVector<Node*> results;
70-
QQueue<Node*> queue;
71-
queue.enqueue(const_cast<Node*>(this));
69+
QVector<Node*> stack;
70+
stack.append(const_cast<Node*>(this));
7271

73-
while (!queue.empty())
72+
while (!stack.isEmpty())
7473
{
75-
Node* node = queue.dequeue();
74+
Node* node = stack.takeLast();
7675

7776
for (Node* child : node->_children)
7877
{
79-
queue.enqueue(child);
78+
stack.append(child);
8079
}
8180

82-
if (lambda(node))
81+
if (predicate(node))
8382
{
84-
results.push_back(node);
83+
results.append(node);
8584
}
8685
}
8786

8887
return results;
8988
}
9089

91-
Node* findChild(const std::function<bool(const Node*)>& lambda) const
90+
Node* findChild(const std::function<bool(const Node*)>& predicate) const
9291
{
93-
auto children = findChildren(lambda);
92+
auto children = findChildren(predicate);
9493
return children.isEmpty() ? nullptr : children.first();
9594
}
9695

0 commit comments

Comments
 (0)