Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgraph_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGRAPH_P_H
5#define QGRAPH_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19#include <QtCore/QHash>
20#include <QtCore/QQueue>
21#include <QtCore/QString>
22#include <QtCore/QDebug>
23#include <QtCore/QSet>
24
25#include <functional> // for std::less
26
27#include <float.h>
28
29QT_REQUIRE_CONFIG(graphicsview);
30
32
33template <typename Vertex, typename EdgeData>
34class Graph
35{
36public:
37 Graph() {}
38
40 public:
41 const_iterator(const Graph *graph, bool begin) : g(graph){
42 if (begin) {
43 row = g->m_graph.constBegin();
44 //test if the graph is empty
45 if (row != g->m_graph.constEnd())
46 column = row->cbegin();
47 } else {
48 row = g->m_graph.constEnd();
49 }
50 }
51
52 inline Vertex *operator*() {
53 return column.key();
54 }
55
56 inline Vertex *from() const {
57 return row.key();
58 }
59
60 inline Vertex *to() const {
61 return column.key();
62 }
63
64 inline bool operator==(const const_iterator &o) const { return !(*this != o); }
65 inline bool operator!=(const const_iterator &o) const {
66 if (row == g->m_graph.end()) {
67 return row != o.row;
68 } else {
69 return row != o.row || column != o.column;
70 }
71 }
72
73 // prefix
75 if (row != g->m_graph.constEnd()) {
76 ++column;
77 if (column == row->cend()) {
78 ++row;
79 if (row != g->m_graph.constEnd()) {
80 column = row->cbegin();
81 }
82 }
83 }
84 return *this;
85 }
86
87 private:
88 const Graph *g;
89 typename QHash<Vertex *, QHash<Vertex *, EdgeData *> >::const_iterator row;
91 };
92
94 return const_iterator(this,true);
95 }
96
98 return const_iterator(this,false);
99 }
100
109 const auto it = m_graph.constFind(first);
110 if (it == m_graph.cend())
111 return nullptr;
112 const auto jt = it->constFind(second);
113 if (jt == it->cend())
114 return nullptr;
115 return *jt;
116 }
117
119 {
120 // Creates a bidirectional edge
121#if defined(QT_DEBUG) && 0
122 qDebug("Graph::createEdge(): %s",
124 .arg(first->toString()).arg(second->toString())));
125#endif
126 if (edgeData(first, second)) {
127#ifdef QT_DEBUG
128 qWarning("%s-%s already has an edge", qPrintable(first->toString()), qPrintable(second->toString()));
129#endif
130 }
131 createDirectedEdge(first, second, data);
132 createDirectedEdge(second, first, data);
133 }
134
136 {
137 // Removes a bidirectional edge
138#if defined(QT_DEBUG) && 0
139 qDebug("Graph::removeEdge(): %s",
141 .arg(first->toString()).arg(second->toString())));
142#endif
143 EdgeData *data = edgeData(first, second);
144 removeDirectedEdge(first, second);
145 removeDirectedEdge(second, first);
146 if (data) delete data;
147 }
148
150 {
151#if defined(QT_DEBUG) && 0
152 qDebug("Graph::takeEdge(): %s",
154 .arg(first->toString()).arg(second->toString())));
155#endif
156 // Removes a bidirectional edge
157 EdgeData *data = edgeData(first, second);
158 if (data) {
159 removeDirectedEdge(first, second);
160 removeDirectedEdge(second, first);
161 }
162 return data;
163 }
164
165 QList<Vertex *> adjacentVertices(Vertex *vertex) const
166 {
167 const auto it = m_graph.constFind(vertex);
168 if (it == m_graph.cend())
169 return QList<Vertex *>();
170 else
171 return it->keys();
172 }
173
174 QSet<Vertex*> vertices() const {
175 QSet<Vertex *> setOfVertices;
176 for (const_iterator it = constBegin(); it != constEnd(); ++it) {
177 setOfVertices.insert(*it);
178 }
179 return setOfVertices;
180 }
181
182 QList<QPair<Vertex *, Vertex *>> connections() const
183 {
184 QList<QPair<Vertex *, Vertex *>> conns;
185 for (const_iterator it = constBegin(); it != constEnd(); ++it) {
186 Vertex *from = it.from();
187 Vertex *to = it.to();
188 // do not return (from,to) *and* (to,from)
189 if (std::less<Vertex*>()(from, to))
190 conns.append(qMakePair(from, to));
191 }
192 return conns;
193 }
194
195#if defined(QT_DEBUG)
196 QString serializeToDot() { // traversal
197 QString strVertices;
198 QString edges;
199
200 QSet<Vertex *> setOfVertices = vertices();
201 for (typename QSet<Vertex*>::const_iterator it = setOfVertices.begin(); it != setOfVertices.end(); ++it) {
202 Vertex *v = *it;
203 const QList<Vertex*> adjacents = adjacentVertices(v);
204 for (auto *v1 : adjacents) {
206 bool forward = data->from == v;
207 if (forward) {
208 edges += QString::fromLatin1("\"%1\"->\"%2\" [label=\"[%3,%4,%5,%6,%7]\" color=\"#000000\"] \n")
209 .arg(v->toString())
210 .arg(v1->toString())
211 .arg(data->minSize)
212 .arg(data->minPrefSize)
213 .arg(data->prefSize)
214 .arg(data->maxPrefSize)
215 .arg(data->maxSize)
216 ;
217 }
218 }
219 strVertices += QString::fromLatin1("\"%1\" [label=\"%2\"]\n").arg(v->toString()).arg(v->toString());
220 }
221 return QString::fromLatin1("%1\n%2\n").arg(strVertices, edges);
222 }
223#endif
224
225protected:
227 {
228 m_graph[from][to] = data;
229 }
230
232 {
233 const auto it = m_graph.find(from);
234 Q_ASSERT(it != m_graph.end());
235
236 it->remove(to);
237 if (it->isEmpty()) {
238 //nobody point to 'from' so we can remove it from the graph
239 m_graph.erase(it);
240 }
241 }
242
243private:
244 QHash<Vertex *, QHash<Vertex *, EdgeData *> > m_graph;
245};
246
248
249#endif
Vertex * to() const
Definition qgraph_p.h:60
const_iterator(const Graph *graph, bool begin)
Definition qgraph_p.h:41
Vertex * operator*()
Definition qgraph_p.h:52
const_iterator & operator++()
Definition qgraph_p.h:74
bool operator==(const const_iterator &o) const
Definition qgraph_p.h:64
Vertex * from() const
Definition qgraph_p.h:56
bool operator!=(const const_iterator &o) const
Definition qgraph_p.h:65
EdgeData * takeEdge(Vertex *first, Vertex *second)
Definition qgraph_p.h:149
QList< QPair< Vertex *, Vertex * > > connections() const
Definition qgraph_p.h:182
QList< Vertex * > adjacentVertices(Vertex *vertex) const
Definition qgraph_p.h:165
const_iterator constEnd() const
Definition qgraph_p.h:97
Graph()
Definition qgraph_p.h:37
QSet< Vertex * > vertices() const
Definition qgraph_p.h:174
void removeEdge(Vertex *first, Vertex *second)
Definition qgraph_p.h:135
void createDirectedEdge(Vertex *from, Vertex *to, EdgeData *data)
Definition qgraph_p.h:226
void removeDirectedEdge(Vertex *from, Vertex *to)
Definition qgraph_p.h:231
const_iterator constBegin() const
Definition qgraph_p.h:93
EdgeData * edgeData(Vertex *first, Vertex *second)
Definition qgraph_p.h:108
void createEdge(Vertex *first, Vertex *second, EdgeData *data)
Definition qgraph_p.h:118
\inmodule QtCore
Definition qhash.h:1145
const_iterator constFind(const Key &key) const noexcept
Definition qhash.h:1299
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1291
iterator erase(const_iterator it)
Definition qhash.h:1233
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
const_iterator cend() const noexcept
Definition qhash.h:1218
bool remove(const T &value)
Definition qset.h:63
iterator begin()
Definition qset.h:136
iterator end()
Definition qset.h:140
bool isEmpty() const
Definition qset.h:52
const_iterator cend() const noexcept
Definition qset.h:142
const_iterator constFind(const T &value) const
Definition qset.h:161
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
GLsizei const GLfloat * v
[13]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLfloat GLfloat v1
GLboolean GLboolean g
GLint first
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
QT_BEGIN_NAMESPACE constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
Definition qpair.h:19
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
SSL_CTX int void * arg
#define qPrintable(string)
Definition qstring.h:1531
#define QT_REQUIRE_CONFIG(feature)
\qmltype MapCircle \instantiates QDeclarativeCircleMapItem \inqmlmodule QtLocation