author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef QGRAPH_P_H |
|
43 |
#define QGRAPH_P_H |
|
44 |
||
45 |
// |
|
46 |
// W A R N I N G |
|
47 |
// ------------- |
|
48 |
// |
|
49 |
// This file is not part of the Qt API. It exists purely as an |
|
50 |
// implementation detail. This header file may change from version to |
|
51 |
// version without notice, or even be removed. |
|
52 |
// |
|
53 |
// We mean it. |
|
54 |
// |
|
55 |
||
56 |
#include <QtCore/QHash> |
|
57 |
#include <QtCore/QQueue> |
|
58 |
#include <QtCore/QString> |
|
59 |
#include <QtCore/QDebug> |
|
60 |
||
61 |
#include <float.h> |
|
62 |
||
63 |
QT_BEGIN_NAMESPACE |
|
64 |
||
65 |
template <typename Vertex, typename EdgeData> |
|
66 |
class Graph |
|
67 |
{ |
|
68 |
public: |
|
69 |
Graph() {} |
|
70 |
||
71 |
class const_iterator { |
|
72 |
public: |
|
73 |
const_iterator(const Graph *graph, bool begin) : g(graph){ |
|
74 |
if (begin) { |
|
75 |
row = g->m_graph.constBegin(); |
|
76 |
//test if the graph is empty |
|
77 |
if (row != g->m_graph.constEnd()) |
|
78 |
{ |
|
79 |
column = (*row)->constBegin(); |
|
80 |
} |
|
81 |
} else { |
|
82 |
row = g->m_graph.constEnd(); |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
inline Vertex *operator*() { |
|
87 |
return column.key(); |
|
88 |
} |
|
89 |
||
90 |
inline Vertex *from() const { |
|
91 |
return row.key(); |
|
92 |
} |
|
93 |
||
94 |
inline Vertex *to() const { |
|
95 |
return column.key(); |
|
96 |
} |
|
97 |
||
98 |
inline bool operator==(const const_iterator &o) const { return !(*this != o); } |
|
99 |
inline bool operator!=(const const_iterator &o) const { |
|
100 |
if (row == g->m_graph.end()) { |
|
101 |
return row != o.row; |
|
102 |
} else { |
|
103 |
return row != o.row || column != o.column; |
|
104 |
} |
|
105 |
} |
|
106 |
inline const_iterator& operator=(const const_iterator &o) const { row = o.row; column = o.column; return *this;} |
|
107 |
||
108 |
// prefix |
|
109 |
const_iterator &operator++() { |
|
110 |
if (row != g->m_graph.constEnd()) { |
|
111 |
++column; |
|
112 |
if (column == (*row)->constEnd()) { |
|
113 |
++row; |
|
114 |
if (row != g->m_graph.constEnd()) { |
|
115 |
column = (*row)->constBegin(); |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
return *this; |
|
120 |
} |
|
121 |
||
122 |
private: |
|
123 |
const Graph *g; |
|
124 |
Q_TYPENAME QHash<Vertex *, QHash<Vertex *, EdgeData *> * >::const_iterator row; |
|
125 |
Q_TYPENAME QHash<Vertex *, EdgeData *>::const_iterator column; |
|
126 |
}; |
|
127 |
||
128 |
const_iterator constBegin() const { |
|
129 |
return const_iterator(this,true); |
|
130 |
} |
|
131 |
||
132 |
const_iterator constEnd() const { |
|
133 |
return const_iterator(this,false); |
|
134 |
} |
|
135 |
||
136 |
/*! |
|
137 |
* \internal |
|
138 |
* |
|
139 |
* If there is an edge between \a first and \a second, it will return a structure |
|
140 |
* containing the data associated with the edge, otherwise it will return 0. |
|
141 |
* |
|
142 |
*/ |
|
143 |
EdgeData *edgeData(Vertex* first, Vertex* second) { |
|
144 |
QHash<Vertex *, EdgeData *> *row = m_graph.value(first); |
|
145 |
return row ? row->value(second) : 0; |
|
146 |
} |
|
147 |
||
148 |
void createEdge(Vertex *first, Vertex *second, EdgeData *data) |
|
149 |
{ |
|
150 |
// Creates a bidirectional edge |
|
151 |
#if defined(QT_DEBUG) && 0 |
|
152 |
qDebug("Graph::createEdge(): %s", |
|
153 |
qPrintable(QString::fromAscii("%1-%2") |
|
154 |
.arg(first->toString()).arg(second->toString()))); |
|
155 |
#endif |
|
156 |
if (edgeData(first, second)) { |
|
157 |
#ifdef QT_DEBUG |
|
158 |
qWarning("%s-%s already has an edge", qPrintable(first->toString()), qPrintable(second->toString())); |
|
159 |
#endif |
|
160 |
} |
|
161 |
createDirectedEdge(first, second, data); |
|
162 |
createDirectedEdge(second, first, data); |
|
163 |
} |
|
164 |
||
165 |
void removeEdge(Vertex *first, Vertex *second) |
|
166 |
{ |
|
167 |
// Removes a bidirectional edge |
|
168 |
#if defined(QT_DEBUG) && 0 |
|
169 |
qDebug("Graph::removeEdge(): %s", |
|
170 |
qPrintable(QString::fromAscii("%1-%2") |
|
171 |
.arg(first->toString()).arg(second->toString()))); |
|
172 |
#endif |
|
173 |
EdgeData *data = edgeData(first, second); |
|
174 |
removeDirectedEdge(first, second); |
|
175 |
removeDirectedEdge(second, first); |
|
176 |
if (data) delete data; |
|
177 |
} |
|
178 |
||
179 |
EdgeData *takeEdge(Vertex* first, Vertex* second) |
|
180 |
{ |
|
181 |
#if defined(QT_DEBUG) && 0 |
|
182 |
qDebug("Graph::takeEdge(): %s", |
|
183 |
qPrintable(QString::fromAscii("%1-%2") |
|
184 |
.arg(first->toString()).arg(second->toString()))); |
|
185 |
#endif |
|
186 |
// Removes a bidirectional edge |
|
187 |
EdgeData *data = edgeData(first, second); |
|
188 |
if (data) { |
|
189 |
removeDirectedEdge(first, second); |
|
190 |
removeDirectedEdge(second, first); |
|
191 |
} |
|
192 |
return data; |
|
193 |
} |
|
194 |
||
195 |
QList<Vertex *> adjacentVertices(Vertex *vertex) const |
|
196 |
{ |
|
197 |
QHash<Vertex *, EdgeData *> *row = m_graph.value(vertex); |
|
198 |
QList<Vertex *> l; |
|
199 |
if (row) |
|
200 |
l = row->keys(); |
|
201 |
return l; |
|
202 |
} |
|
203 |
||
204 |
QSet<Vertex*> vertices() const { |
|
205 |
QSet<Vertex *> setOfVertices; |
|
206 |
for (const_iterator it = constBegin(); it != constEnd(); ++it) { |
|
207 |
setOfVertices.insert(*it); |
|
208 |
} |
|
209 |
return setOfVertices; |
|
210 |
} |
|
211 |
||
212 |
QList<QPair<Vertex*, Vertex*> > connections() const { |
|
213 |
QList<QPair<Vertex*, Vertex*> > conns; |
|
214 |
for (const_iterator it = constBegin(); it != constEnd(); ++it) { |
|
215 |
Vertex *from = it.from(); |
|
216 |
Vertex *to = it.to(); |
|
217 |
// do not return (from,to) *and* (to,from) |
|
218 |
if (from < to) { |
|
219 |
conns.append(qMakePair(from, to)); |
|
220 |
} |
|
221 |
} |
|
222 |
return conns; |
|
223 |
} |
|
224 |
||
225 |
#if defined(QT_DEBUG) |
|
226 |
QString serializeToDot() { // traversal |
|
227 |
QString strVertices; |
|
228 |
QString edges; |
|
229 |
||
230 |
QSet<Vertex *> setOfVertices = vertices(); |
|
231 |
for (Q_TYPENAME QSet<Vertex*>::const_iterator it = setOfVertices.begin(); it != setOfVertices.end(); ++it) { |
|
232 |
Vertex *v = *it; |
|
233 |
QList<Vertex*> adjacents = adjacentVertices(v); |
|
234 |
for (int i = 0; i < adjacents.count(); ++i) { |
|
235 |
Vertex *v1 = adjacents.at(i); |
|
236 |
EdgeData *data = edgeData(v, v1); |
|
237 |
bool forward = data->from == v; |
|
238 |
if (forward) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
239 |
edges += QString::fromAscii("\"%1\"->\"%2\" [label=\"[%3,%4,%5,%6,%7]\" color=\"#000000\"] \n") |
0 | 240 |
.arg(v->toString()) |
241 |
.arg(v1->toString()) |
|
242 |
.arg(data->minSize) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
243 |
.arg(data->minPrefSize) |
0 | 244 |
.arg(data->prefSize) |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
245 |
.arg(data->maxPrefSize) |
0 | 246 |
.arg(data->maxSize) |
247 |
; |
|
248 |
} |
|
249 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
250 |
strVertices += QString::fromAscii("\"%1\" [label=\"%2\"]\n").arg(v->toString()).arg(v->toString()); |
0 | 251 |
} |
252 |
return QString::fromAscii("%1\n%2\n").arg(strVertices).arg(edges); |
|
253 |
} |
|
254 |
#endif |
|
255 |
||
256 |
protected: |
|
257 |
void createDirectedEdge(Vertex *from, Vertex *to, EdgeData *data) |
|
258 |
{ |
|
259 |
QHash<Vertex *, EdgeData *> *adjacentToFirst = m_graph.value(from); |
|
260 |
if (!adjacentToFirst) { |
|
261 |
adjacentToFirst = new QHash<Vertex *, EdgeData *>(); |
|
262 |
m_graph.insert(from, adjacentToFirst); |
|
263 |
} |
|
264 |
adjacentToFirst->insert(to, data); |
|
265 |
} |
|
266 |
||
267 |
void removeDirectedEdge(Vertex *from, Vertex *to) |
|
268 |
{ |
|
269 |
QHash<Vertex *, EdgeData *> *adjacentToFirst = m_graph.value(from); |
|
270 |
Q_ASSERT(adjacentToFirst); |
|
271 |
||
272 |
adjacentToFirst->remove(to); |
|
273 |
if (adjacentToFirst->isEmpty()) { |
|
274 |
//nobody point to 'from' so we can remove it from the graph |
|
275 |
m_graph.remove(from); |
|
276 |
delete adjacentToFirst; |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
private: |
|
281 |
QHash<Vertex *, QHash<Vertex *, EdgeData *> *> m_graph; |
|
282 |
}; |
|
283 |
||
284 |
QT_END_NAMESPACE |
|
285 |
||
286 |
#endif |