From d569f37dfd4ca9b2b704e13b95fab96abe0b0f97 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 16 Jan 2016 20:24:03 +0100 Subject: QGraphicsView: Graph: don't allocate QHashes on the heap ... just so you can observe their absence with QHash::value() returning nullptr. Instead, use find() + comparison to end() to detect presence or absence. Simplifies quite a bit of code. Change-Id: Ifd7921bfc8102677ea345ae37d38da31b8105426 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/widgets/graphicsview/qgraph_p.h | 49 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 26 deletions(-) (limited to 'src/widgets/graphicsview') diff --git a/src/widgets/graphicsview/qgraph_p.h b/src/widgets/graphicsview/qgraph_p.h index e442cc3412..34c6a8c3c5 100644 --- a/src/widgets/graphicsview/qgraph_p.h +++ b/src/widgets/graphicsview/qgraph_p.h @@ -75,9 +75,7 @@ public: row = g->m_graph.constBegin(); //test if the graph is empty if (row != g->m_graph.constEnd()) - { - column = (*row)->constBegin(); - } + column = row->cbegin(); } else { row = g->m_graph.constEnd(); } @@ -108,10 +106,10 @@ public: const_iterator &operator++() { if (row != g->m_graph.constEnd()) { ++column; - if (column == (*row)->constEnd()) { + if (column == row->cend()) { ++row; if (row != g->m_graph.constEnd()) { - column = (*row)->constBegin(); + column = row->cbegin(); } } } @@ -120,7 +118,7 @@ public: private: const Graph *g; - typename QHash * >::const_iterator row; + typename QHash >::const_iterator row; typename QHash::const_iterator column; }; @@ -140,8 +138,13 @@ public: * */ EdgeData *edgeData(Vertex* first, Vertex* second) { - QHash *row = m_graph.value(first); - return row ? row->value(second) : 0; + const auto it = m_graph.constFind(first); + if (it == m_graph.cend()) + return nullptr; + const auto jt = it->constFind(second); + if (jt == it->cend()) + return nullptr; + return *jt; } void createEdge(Vertex *first, Vertex *second, EdgeData *data) @@ -193,11 +196,11 @@ public: QList adjacentVertices(Vertex *vertex) const { - QHash *row = m_graph.value(vertex); - QList l; - if (row) - l = row->keys(); - return l; + const auto it = m_graph.constFind(vertex); + if (it == m_graph.cend()) + return QList(); + else + return it->keys(); } QSet vertices() const { @@ -253,29 +256,23 @@ public: protected: void createDirectedEdge(Vertex *from, Vertex *to, EdgeData *data) { - QHash *adjacentToFirst = m_graph.value(from); - if (!adjacentToFirst) { - adjacentToFirst = new QHash(); - m_graph.insert(from, adjacentToFirst); - } - adjacentToFirst->insert(to, data); + m_graph[from][to] = data; } void removeDirectedEdge(Vertex *from, Vertex *to) { - QHash *adjacentToFirst = m_graph.value(from); - Q_ASSERT(adjacentToFirst); + const auto it = m_graph.find(from); + Q_ASSERT(it != m_graph.end()); - adjacentToFirst->remove(to); - if (adjacentToFirst->isEmpty()) { + it->remove(to); + if (it->isEmpty()) { //nobody point to 'from' so we can remove it from the graph - m_graph.remove(from); - delete adjacentToFirst; + m_graph.erase(it); } } private: - QHash *> m_graph; + QHash > m_graph; }; QT_END_NAMESPACE -- cgit v1.2.3