alpar@209
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@57
|
2 |
*
|
alpar@209
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@57
|
4 |
*
|
alpar@107
|
5 |
* Copyright (C) 2003-2008
|
deba@57
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@57
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@57
|
8 |
*
|
deba@57
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@57
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@57
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@57
|
12 |
*
|
deba@57
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@57
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@57
|
15 |
* purpose.
|
deba@57
|
16 |
*
|
deba@57
|
17 |
*/
|
deba@57
|
18 |
|
deba@57
|
19 |
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
|
deba@57
|
20 |
#define LEMON_BITS_GRAPH_EXTENDER_H
|
deba@57
|
21 |
|
deba@220
|
22 |
#include <lemon/core.h>
|
deba@57
|
23 |
|
deba@57
|
24 |
#include <lemon/bits/map_extender.h>
|
deba@57
|
25 |
#include <lemon/bits/default_map.h>
|
deba@57
|
26 |
|
deba@57
|
27 |
#include <lemon/concept_check.h>
|
deba@57
|
28 |
#include <lemon/concepts/maps.h>
|
deba@57
|
29 |
|
deba@57
|
30 |
///\ingroup graphbits
|
deba@57
|
31 |
///\file
|
deba@57
|
32 |
///\brief Extenders for the digraph types
|
deba@57
|
33 |
namespace lemon {
|
deba@57
|
34 |
|
deba@57
|
35 |
/// \ingroup graphbits
|
deba@57
|
36 |
///
|
deba@57
|
37 |
/// \brief Extender for the Digraphs
|
deba@57
|
38 |
template <typename Base>
|
deba@57
|
39 |
class DigraphExtender : public Base {
|
deba@57
|
40 |
public:
|
deba@57
|
41 |
|
deba@57
|
42 |
typedef Base Parent;
|
deba@57
|
43 |
typedef DigraphExtender Digraph;
|
deba@57
|
44 |
|
deba@57
|
45 |
// Base extensions
|
deba@57
|
46 |
|
deba@57
|
47 |
typedef typename Parent::Node Node;
|
deba@57
|
48 |
typedef typename Parent::Arc Arc;
|
deba@57
|
49 |
|
deba@57
|
50 |
int maxId(Node) const {
|
deba@57
|
51 |
return Parent::maxNodeId();
|
deba@57
|
52 |
}
|
deba@57
|
53 |
|
deba@57
|
54 |
int maxId(Arc) const {
|
deba@57
|
55 |
return Parent::maxArcId();
|
deba@57
|
56 |
}
|
deba@57
|
57 |
|
deba@57
|
58 |
Node fromId(int id, Node) const {
|
deba@57
|
59 |
return Parent::nodeFromId(id);
|
deba@57
|
60 |
}
|
deba@57
|
61 |
|
deba@57
|
62 |
Arc fromId(int id, Arc) const {
|
deba@57
|
63 |
return Parent::arcFromId(id);
|
deba@57
|
64 |
}
|
deba@57
|
65 |
|
deba@78
|
66 |
Node oppositeNode(const Node &node, const Arc &arc) const {
|
deba@78
|
67 |
if (node == Parent::source(arc))
|
alpar@209
|
68 |
return Parent::target(arc);
|
deba@78
|
69 |
else if(node == Parent::target(arc))
|
alpar@209
|
70 |
return Parent::source(arc);
|
deba@57
|
71 |
else
|
alpar@209
|
72 |
return INVALID;
|
deba@57
|
73 |
}
|
deba@57
|
74 |
|
deba@57
|
75 |
// Alterable extension
|
deba@57
|
76 |
|
deba@57
|
77 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
|
deba@57
|
78 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
|
deba@57
|
79 |
|
deba@57
|
80 |
|
deba@57
|
81 |
protected:
|
deba@57
|
82 |
|
deba@57
|
83 |
mutable NodeNotifier node_notifier;
|
deba@57
|
84 |
mutable ArcNotifier arc_notifier;
|
deba@57
|
85 |
|
deba@57
|
86 |
public:
|
deba@57
|
87 |
|
deba@57
|
88 |
NodeNotifier& notifier(Node) const {
|
deba@57
|
89 |
return node_notifier;
|
deba@57
|
90 |
}
|
alpar@209
|
91 |
|
deba@57
|
92 |
ArcNotifier& notifier(Arc) const {
|
deba@57
|
93 |
return arc_notifier;
|
deba@57
|
94 |
}
|
deba@57
|
95 |
|
alpar@209
|
96 |
class NodeIt : public Node {
|
deba@78
|
97 |
const Digraph* _digraph;
|
deba@57
|
98 |
public:
|
deba@57
|
99 |
|
deba@57
|
100 |
NodeIt() {}
|
deba@57
|
101 |
|
deba@57
|
102 |
NodeIt(Invalid i) : Node(i) { }
|
deba@57
|
103 |
|
deba@78
|
104 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
|
alpar@209
|
105 |
_digraph->first(static_cast<Node&>(*this));
|
deba@57
|
106 |
}
|
deba@57
|
107 |
|
alpar@209
|
108 |
NodeIt(const Digraph& digraph, const Node& node)
|
alpar@209
|
109 |
: Node(node), _digraph(&digraph) {}
|
deba@57
|
110 |
|
alpar@209
|
111 |
NodeIt& operator++() {
|
alpar@209
|
112 |
_digraph->next(*this);
|
alpar@209
|
113 |
return *this;
|
deba@57
|
114 |
}
|
deba@57
|
115 |
|
deba@57
|
116 |
};
|
deba@57
|
117 |
|
deba@57
|
118 |
|
alpar@209
|
119 |
class ArcIt : public Arc {
|
deba@78
|
120 |
const Digraph* _digraph;
|
deba@57
|
121 |
public:
|
deba@57
|
122 |
|
deba@57
|
123 |
ArcIt() { }
|
deba@57
|
124 |
|
deba@57
|
125 |
ArcIt(Invalid i) : Arc(i) { }
|
deba@57
|
126 |
|
deba@78
|
127 |
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
|
alpar@209
|
128 |
_digraph->first(static_cast<Arc&>(*this));
|
deba@57
|
129 |
}
|
deba@57
|
130 |
|
alpar@209
|
131 |
ArcIt(const Digraph& digraph, const Arc& arc) :
|
alpar@209
|
132 |
Arc(arc), _digraph(&digraph) { }
|
deba@57
|
133 |
|
alpar@209
|
134 |
ArcIt& operator++() {
|
alpar@209
|
135 |
_digraph->next(*this);
|
alpar@209
|
136 |
return *this;
|
deba@57
|
137 |
}
|
deba@57
|
138 |
|
deba@57
|
139 |
};
|
deba@57
|
140 |
|
deba@57
|
141 |
|
alpar@209
|
142 |
class OutArcIt : public Arc {
|
deba@78
|
143 |
const Digraph* _digraph;
|
deba@57
|
144 |
public:
|
deba@57
|
145 |
|
deba@57
|
146 |
OutArcIt() { }
|
deba@57
|
147 |
|
deba@57
|
148 |
OutArcIt(Invalid i) : Arc(i) { }
|
deba@57
|
149 |
|
alpar@209
|
150 |
OutArcIt(const Digraph& digraph, const Node& node)
|
alpar@209
|
151 |
: _digraph(&digraph) {
|
alpar@209
|
152 |
_digraph->firstOut(*this, node);
|
deba@57
|
153 |
}
|
deba@57
|
154 |
|
alpar@209
|
155 |
OutArcIt(const Digraph& digraph, const Arc& arc)
|
alpar@209
|
156 |
: Arc(arc), _digraph(&digraph) {}
|
deba@57
|
157 |
|
alpar@209
|
158 |
OutArcIt& operator++() {
|
alpar@209
|
159 |
_digraph->nextOut(*this);
|
alpar@209
|
160 |
return *this;
|
deba@57
|
161 |
}
|
deba@57
|
162 |
|
deba@57
|
163 |
};
|
deba@57
|
164 |
|
deba@57
|
165 |
|
alpar@209
|
166 |
class InArcIt : public Arc {
|
deba@78
|
167 |
const Digraph* _digraph;
|
deba@57
|
168 |
public:
|
deba@57
|
169 |
|
deba@57
|
170 |
InArcIt() { }
|
deba@57
|
171 |
|
deba@57
|
172 |
InArcIt(Invalid i) : Arc(i) { }
|
deba@57
|
173 |
|
alpar@209
|
174 |
InArcIt(const Digraph& digraph, const Node& node)
|
alpar@209
|
175 |
: _digraph(&digraph) {
|
alpar@209
|
176 |
_digraph->firstIn(*this, node);
|
deba@57
|
177 |
}
|
deba@57
|
178 |
|
alpar@209
|
179 |
InArcIt(const Digraph& digraph, const Arc& arc) :
|
alpar@209
|
180 |
Arc(arc), _digraph(&digraph) {}
|
deba@57
|
181 |
|
alpar@209
|
182 |
InArcIt& operator++() {
|
alpar@209
|
183 |
_digraph->nextIn(*this);
|
alpar@209
|
184 |
return *this;
|
deba@57
|
185 |
}
|
deba@57
|
186 |
|
deba@57
|
187 |
};
|
deba@57
|
188 |
|
deba@57
|
189 |
/// \brief Base node of the iterator
|
deba@57
|
190 |
///
|
deba@57
|
191 |
/// Returns the base node (i.e. the source in this case) of the iterator
|
deba@78
|
192 |
Node baseNode(const OutArcIt &arc) const {
|
deba@78
|
193 |
return Parent::source(arc);
|
deba@57
|
194 |
}
|
deba@57
|
195 |
/// \brief Running node of the iterator
|
deba@57
|
196 |
///
|
deba@57
|
197 |
/// Returns the running node (i.e. the target in this case) of the
|
deba@57
|
198 |
/// iterator
|
deba@78
|
199 |
Node runningNode(const OutArcIt &arc) const {
|
deba@78
|
200 |
return Parent::target(arc);
|
deba@57
|
201 |
}
|
deba@57
|
202 |
|
deba@57
|
203 |
/// \brief Base node of the iterator
|
deba@57
|
204 |
///
|
deba@57
|
205 |
/// Returns the base node (i.e. the target in this case) of the iterator
|
deba@78
|
206 |
Node baseNode(const InArcIt &arc) const {
|
deba@78
|
207 |
return Parent::target(arc);
|
deba@57
|
208 |
}
|
deba@57
|
209 |
/// \brief Running node of the iterator
|
deba@57
|
210 |
///
|
deba@57
|
211 |
/// Returns the running node (i.e. the source in this case) of the
|
deba@57
|
212 |
/// iterator
|
deba@78
|
213 |
Node runningNode(const InArcIt &arc) const {
|
deba@78
|
214 |
return Parent::source(arc);
|
deba@57
|
215 |
}
|
deba@57
|
216 |
|
alpar@209
|
217 |
|
deba@57
|
218 |
template <typename _Value>
|
alpar@209
|
219 |
class NodeMap
|
deba@57
|
220 |
: public MapExtender<DefaultMap<Digraph, Node, _Value> > {
|
deba@57
|
221 |
public:
|
deba@57
|
222 |
typedef DigraphExtender Digraph;
|
deba@57
|
223 |
typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
|
deba@57
|
224 |
|
alpar@209
|
225 |
explicit NodeMap(const Digraph& digraph)
|
alpar@209
|
226 |
: Parent(digraph) {}
|
alpar@209
|
227 |
NodeMap(const Digraph& digraph, const _Value& value)
|
alpar@209
|
228 |
: Parent(digraph, value) {}
|
deba@57
|
229 |
|
kpeter@263
|
230 |
private:
|
deba@57
|
231 |
NodeMap& operator=(const NodeMap& cmap) {
|
alpar@209
|
232 |
return operator=<NodeMap>(cmap);
|
deba@57
|
233 |
}
|
deba@57
|
234 |
|
deba@57
|
235 |
template <typename CMap>
|
deba@57
|
236 |
NodeMap& operator=(const CMap& cmap) {
|
deba@57
|
237 |
Parent::operator=(cmap);
|
alpar@209
|
238 |
return *this;
|
deba@57
|
239 |
}
|
deba@57
|
240 |
|
deba@57
|
241 |
};
|
deba@57
|
242 |
|
deba@57
|
243 |
template <typename _Value>
|
alpar@209
|
244 |
class ArcMap
|
deba@57
|
245 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
|
deba@57
|
246 |
public:
|
deba@57
|
247 |
typedef DigraphExtender Digraph;
|
deba@57
|
248 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
|
deba@57
|
249 |
|
alpar@209
|
250 |
explicit ArcMap(const Digraph& digraph)
|
alpar@209
|
251 |
: Parent(digraph) {}
|
alpar@209
|
252 |
ArcMap(const Digraph& digraph, const _Value& value)
|
alpar@209
|
253 |
: Parent(digraph, value) {}
|
deba@57
|
254 |
|
kpeter@263
|
255 |
private:
|
deba@57
|
256 |
ArcMap& operator=(const ArcMap& cmap) {
|
alpar@209
|
257 |
return operator=<ArcMap>(cmap);
|
deba@57
|
258 |
}
|
deba@57
|
259 |
|
deba@57
|
260 |
template <typename CMap>
|
deba@57
|
261 |
ArcMap& operator=(const CMap& cmap) {
|
deba@57
|
262 |
Parent::operator=(cmap);
|
alpar@209
|
263 |
return *this;
|
deba@57
|
264 |
}
|
deba@57
|
265 |
};
|
deba@57
|
266 |
|
deba@57
|
267 |
|
deba@57
|
268 |
Node addNode() {
|
deba@57
|
269 |
Node node = Parent::addNode();
|
deba@57
|
270 |
notifier(Node()).add(node);
|
deba@57
|
271 |
return node;
|
deba@57
|
272 |
}
|
alpar@209
|
273 |
|
deba@57
|
274 |
Arc addArc(const Node& from, const Node& to) {
|
deba@57
|
275 |
Arc arc = Parent::addArc(from, to);
|
deba@57
|
276 |
notifier(Arc()).add(arc);
|
deba@57
|
277 |
return arc;
|
deba@57
|
278 |
}
|
deba@57
|
279 |
|
deba@57
|
280 |
void clear() {
|
deba@57
|
281 |
notifier(Arc()).clear();
|
deba@57
|
282 |
notifier(Node()).clear();
|
deba@57
|
283 |
Parent::clear();
|
deba@57
|
284 |
}
|
deba@57
|
285 |
|
deba@57
|
286 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
|
deba@57
|
287 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
deba@57
|
288 |
Parent::build(digraph, nodeRef, arcRef);
|
deba@57
|
289 |
notifier(Node()).build();
|
deba@57
|
290 |
notifier(Arc()).build();
|
deba@57
|
291 |
}
|
deba@57
|
292 |
|
deba@57
|
293 |
void erase(const Node& node) {
|
deba@57
|
294 |
Arc arc;
|
deba@57
|
295 |
Parent::firstOut(arc, node);
|
deba@57
|
296 |
while (arc != INVALID ) {
|
alpar@209
|
297 |
erase(arc);
|
alpar@209
|
298 |
Parent::firstOut(arc, node);
|
alpar@209
|
299 |
}
|
deba@57
|
300 |
|
deba@57
|
301 |
Parent::firstIn(arc, node);
|
deba@57
|
302 |
while (arc != INVALID ) {
|
alpar@209
|
303 |
erase(arc);
|
alpar@209
|
304 |
Parent::firstIn(arc, node);
|
deba@57
|
305 |
}
|
deba@57
|
306 |
|
deba@57
|
307 |
notifier(Node()).erase(node);
|
deba@57
|
308 |
Parent::erase(node);
|
deba@57
|
309 |
}
|
alpar@209
|
310 |
|
deba@57
|
311 |
void erase(const Arc& arc) {
|
deba@57
|
312 |
notifier(Arc()).erase(arc);
|
deba@57
|
313 |
Parent::erase(arc);
|
deba@57
|
314 |
}
|
deba@57
|
315 |
|
deba@57
|
316 |
DigraphExtender() {
|
deba@57
|
317 |
node_notifier.setContainer(*this);
|
deba@57
|
318 |
arc_notifier.setContainer(*this);
|
alpar@209
|
319 |
}
|
alpar@209
|
320 |
|
deba@57
|
321 |
|
deba@57
|
322 |
~DigraphExtender() {
|
deba@57
|
323 |
arc_notifier.clear();
|
deba@57
|
324 |
node_notifier.clear();
|
deba@57
|
325 |
}
|
deba@57
|
326 |
};
|
deba@57
|
327 |
|
deba@78
|
328 |
/// \ingroup _graphbits
|
deba@57
|
329 |
///
|
deba@57
|
330 |
/// \brief Extender for the Graphs
|
alpar@209
|
331 |
template <typename Base>
|
deba@57
|
332 |
class GraphExtender : public Base {
|
deba@57
|
333 |
public:
|
alpar@209
|
334 |
|
deba@57
|
335 |
typedef Base Parent;
|
deba@78
|
336 |
typedef GraphExtender Graph;
|
deba@57
|
337 |
|
deba@77
|
338 |
typedef True UndirectedTag;
|
deba@77
|
339 |
|
deba@57
|
340 |
typedef typename Parent::Node Node;
|
deba@57
|
341 |
typedef typename Parent::Arc Arc;
|
deba@57
|
342 |
typedef typename Parent::Edge Edge;
|
deba@57
|
343 |
|
alpar@209
|
344 |
// Graph extension
|
deba@57
|
345 |
|
deba@57
|
346 |
int maxId(Node) const {
|
deba@57
|
347 |
return Parent::maxNodeId();
|
deba@57
|
348 |
}
|
deba@57
|
349 |
|
deba@57
|
350 |
int maxId(Arc) const {
|
deba@57
|
351 |
return Parent::maxArcId();
|
deba@57
|
352 |
}
|
deba@57
|
353 |
|
deba@57
|
354 |
int maxId(Edge) const {
|
deba@57
|
355 |
return Parent::maxEdgeId();
|
deba@57
|
356 |
}
|
deba@57
|
357 |
|
deba@57
|
358 |
Node fromId(int id, Node) const {
|
deba@57
|
359 |
return Parent::nodeFromId(id);
|
deba@57
|
360 |
}
|
deba@57
|
361 |
|
deba@57
|
362 |
Arc fromId(int id, Arc) const {
|
deba@57
|
363 |
return Parent::arcFromId(id);
|
deba@57
|
364 |
}
|
deba@57
|
365 |
|
deba@57
|
366 |
Edge fromId(int id, Edge) const {
|
deba@57
|
367 |
return Parent::edgeFromId(id);
|
deba@57
|
368 |
}
|
deba@57
|
369 |
|
deba@57
|
370 |
Node oppositeNode(const Node &n, const Edge &e) const {
|
deba@125
|
371 |
if( n == Parent::u(e))
|
alpar@209
|
372 |
return Parent::v(e);
|
deba@125
|
373 |
else if( n == Parent::v(e))
|
alpar@209
|
374 |
return Parent::u(e);
|
deba@57
|
375 |
else
|
alpar@209
|
376 |
return INVALID;
|
deba@57
|
377 |
}
|
deba@57
|
378 |
|
deba@78
|
379 |
Arc oppositeArc(const Arc &arc) const {
|
deba@78
|
380 |
return Parent::direct(arc, !Parent::direction(arc));
|
deba@57
|
381 |
}
|
deba@57
|
382 |
|
deba@57
|
383 |
using Parent::direct;
|
deba@78
|
384 |
Arc direct(const Edge &edge, const Node &node) const {
|
deba@125
|
385 |
return Parent::direct(edge, Parent::u(edge) == node);
|
deba@57
|
386 |
}
|
deba@57
|
387 |
|
deba@57
|
388 |
// Alterable extension
|
deba@57
|
389 |
|
deba@57
|
390 |
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
|
deba@57
|
391 |
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
|
deba@57
|
392 |
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
|
deba@57
|
393 |
|
deba@57
|
394 |
|
deba@57
|
395 |
protected:
|
deba@57
|
396 |
|
deba@57
|
397 |
mutable NodeNotifier node_notifier;
|
deba@57
|
398 |
mutable ArcNotifier arc_notifier;
|
deba@57
|
399 |
mutable EdgeNotifier edge_notifier;
|
deba@57
|
400 |
|
deba@57
|
401 |
public:
|
deba@57
|
402 |
|
deba@57
|
403 |
NodeNotifier& notifier(Node) const {
|
deba@57
|
404 |
return node_notifier;
|
deba@57
|
405 |
}
|
alpar@209
|
406 |
|
deba@57
|
407 |
ArcNotifier& notifier(Arc) const {
|
deba@57
|
408 |
return arc_notifier;
|
deba@57
|
409 |
}
|
deba@57
|
410 |
|
deba@57
|
411 |
EdgeNotifier& notifier(Edge) const {
|
deba@57
|
412 |
return edge_notifier;
|
deba@57
|
413 |
}
|
deba@57
|
414 |
|
deba@57
|
415 |
|
deba@57
|
416 |
|
alpar@209
|
417 |
class NodeIt : public Node {
|
deba@78
|
418 |
const Graph* _graph;
|
deba@57
|
419 |
public:
|
deba@57
|
420 |
|
deba@57
|
421 |
NodeIt() {}
|
deba@57
|
422 |
|
deba@57
|
423 |
NodeIt(Invalid i) : Node(i) { }
|
deba@57
|
424 |
|
deba@78
|
425 |
explicit NodeIt(const Graph& graph) : _graph(&graph) {
|
alpar@209
|
426 |
_graph->first(static_cast<Node&>(*this));
|
deba@57
|
427 |
}
|
deba@57
|
428 |
|
alpar@209
|
429 |
NodeIt(const Graph& graph, const Node& node)
|
alpar@209
|
430 |
: Node(node), _graph(&graph) {}
|
deba@57
|
431 |
|
alpar@209
|
432 |
NodeIt& operator++() {
|
alpar@209
|
433 |
_graph->next(*this);
|
alpar@209
|
434 |
return *this;
|
deba@57
|
435 |
}
|
deba@57
|
436 |
|
deba@57
|
437 |
};
|
deba@57
|
438 |
|
deba@57
|
439 |
|
alpar@209
|
440 |
class ArcIt : public Arc {
|
deba@78
|
441 |
const Graph* _graph;
|
deba@57
|
442 |
public:
|
deba@57
|
443 |
|
deba@57
|
444 |
ArcIt() { }
|
deba@57
|
445 |
|
deba@57
|
446 |
ArcIt(Invalid i) : Arc(i) { }
|
deba@57
|
447 |
|
deba@78
|
448 |
explicit ArcIt(const Graph& graph) : _graph(&graph) {
|
alpar@209
|
449 |
_graph->first(static_cast<Arc&>(*this));
|
deba@57
|
450 |
}
|
deba@57
|
451 |
|
alpar@209
|
452 |
ArcIt(const Graph& graph, const Arc& arc) :
|
alpar@209
|
453 |
Arc(arc), _graph(&graph) { }
|
deba@57
|
454 |
|
alpar@209
|
455 |
ArcIt& operator++() {
|
alpar@209
|
456 |
_graph->next(*this);
|
alpar@209
|
457 |
return *this;
|
deba@57
|
458 |
}
|
deba@57
|
459 |
|
deba@57
|
460 |
};
|
deba@57
|
461 |
|
deba@57
|
462 |
|
alpar@209
|
463 |
class OutArcIt : public Arc {
|
deba@78
|
464 |
const Graph* _graph;
|
deba@57
|
465 |
public:
|
deba@57
|
466 |
|
deba@57
|
467 |
OutArcIt() { }
|
deba@57
|
468 |
|
deba@57
|
469 |
OutArcIt(Invalid i) : Arc(i) { }
|
deba@57
|
470 |
|
alpar@209
|
471 |
OutArcIt(const Graph& graph, const Node& node)
|
alpar@209
|
472 |
: _graph(&graph) {
|
alpar@209
|
473 |
_graph->firstOut(*this, node);
|
deba@57
|
474 |
}
|
deba@57
|
475 |
|
alpar@209
|
476 |
OutArcIt(const Graph& graph, const Arc& arc)
|
alpar@209
|
477 |
: Arc(arc), _graph(&graph) {}
|
deba@57
|
478 |
|
alpar@209
|
479 |
OutArcIt& operator++() {
|
alpar@209
|
480 |
_graph->nextOut(*this);
|
alpar@209
|
481 |
return *this;
|
deba@57
|
482 |
}
|
deba@57
|
483 |
|
deba@57
|
484 |
};
|
deba@57
|
485 |
|
deba@57
|
486 |
|
alpar@209
|
487 |
class InArcIt : public Arc {
|
deba@78
|
488 |
const Graph* _graph;
|
deba@57
|
489 |
public:
|
deba@57
|
490 |
|
deba@57
|
491 |
InArcIt() { }
|
deba@57
|
492 |
|
deba@57
|
493 |
InArcIt(Invalid i) : Arc(i) { }
|
deba@57
|
494 |
|
alpar@209
|
495 |
InArcIt(const Graph& graph, const Node& node)
|
alpar@209
|
496 |
: _graph(&graph) {
|
alpar@209
|
497 |
_graph->firstIn(*this, node);
|
deba@57
|
498 |
}
|
deba@57
|
499 |
|
alpar@209
|
500 |
InArcIt(const Graph& graph, const Arc& arc) :
|
alpar@209
|
501 |
Arc(arc), _graph(&graph) {}
|
deba@57
|
502 |
|
alpar@209
|
503 |
InArcIt& operator++() {
|
alpar@209
|
504 |
_graph->nextIn(*this);
|
alpar@209
|
505 |
return *this;
|
deba@57
|
506 |
}
|
deba@57
|
507 |
|
deba@57
|
508 |
};
|
deba@57
|
509 |
|
deba@57
|
510 |
|
alpar@209
|
511 |
class EdgeIt : public Parent::Edge {
|
deba@78
|
512 |
const Graph* _graph;
|
deba@57
|
513 |
public:
|
deba@57
|
514 |
|
deba@57
|
515 |
EdgeIt() { }
|
deba@57
|
516 |
|
deba@57
|
517 |
EdgeIt(Invalid i) : Edge(i) { }
|
deba@57
|
518 |
|
deba@78
|
519 |
explicit EdgeIt(const Graph& graph) : _graph(&graph) {
|
alpar@209
|
520 |
_graph->first(static_cast<Edge&>(*this));
|
deba@57
|
521 |
}
|
deba@57
|
522 |
|
alpar@209
|
523 |
EdgeIt(const Graph& graph, const Edge& edge) :
|
alpar@209
|
524 |
Edge(edge), _graph(&graph) { }
|
deba@57
|
525 |
|
alpar@209
|
526 |
EdgeIt& operator++() {
|
alpar@209
|
527 |
_graph->next(*this);
|
alpar@209
|
528 |
return *this;
|
deba@57
|
529 |
}
|
deba@57
|
530 |
|
deba@57
|
531 |
};
|
deba@57
|
532 |
|
deba@78
|
533 |
class IncEdgeIt : public Parent::Edge {
|
deba@57
|
534 |
friend class GraphExtender;
|
deba@78
|
535 |
const Graph* _graph;
|
deba@78
|
536 |
bool _direction;
|
deba@57
|
537 |
public:
|
deba@57
|
538 |
|
deba@78
|
539 |
IncEdgeIt() { }
|
deba@57
|
540 |
|
deba@78
|
541 |
IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
|
deba@57
|
542 |
|
deba@78
|
543 |
IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
|
alpar@209
|
544 |
_graph->firstInc(*this, _direction, node);
|
deba@57
|
545 |
}
|
deba@57
|
546 |
|
deba@78
|
547 |
IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
|
alpar@209
|
548 |
: _graph(&graph), Edge(edge) {
|
alpar@209
|
549 |
_direction = (_graph->source(edge) == node);
|
deba@57
|
550 |
}
|
deba@57
|
551 |
|
deba@78
|
552 |
IncEdgeIt& operator++() {
|
alpar@209
|
553 |
_graph->nextInc(*this, _direction);
|
alpar@209
|
554 |
return *this;
|
deba@57
|
555 |
}
|
deba@57
|
556 |
};
|
deba@57
|
557 |
|
deba@57
|
558 |
/// \brief Base node of the iterator
|
deba@57
|
559 |
///
|
deba@57
|
560 |
/// Returns the base node (ie. the source in this case) of the iterator
|
deba@78
|
561 |
Node baseNode(const OutArcIt &arc) const {
|
deba@78
|
562 |
return Parent::source(static_cast<const Arc&>(arc));
|
deba@57
|
563 |
}
|
deba@57
|
564 |
/// \brief Running node of the iterator
|
deba@57
|
565 |
///
|
deba@57
|
566 |
/// Returns the running node (ie. the target in this case) of the
|
deba@57
|
567 |
/// iterator
|
deba@78
|
568 |
Node runningNode(const OutArcIt &arc) const {
|
deba@78
|
569 |
return Parent::target(static_cast<const Arc&>(arc));
|
deba@57
|
570 |
}
|
deba@57
|
571 |
|
deba@57
|
572 |
/// \brief Base node of the iterator
|
deba@57
|
573 |
///
|
deba@57
|
574 |
/// Returns the base node (ie. the target in this case) of the iterator
|
deba@78
|
575 |
Node baseNode(const InArcIt &arc) const {
|
deba@78
|
576 |
return Parent::target(static_cast<const Arc&>(arc));
|
deba@57
|
577 |
}
|
deba@57
|
578 |
/// \brief Running node of the iterator
|
deba@57
|
579 |
///
|
deba@57
|
580 |
/// Returns the running node (ie. the source in this case) of the
|
deba@57
|
581 |
/// iterator
|
deba@78
|
582 |
Node runningNode(const InArcIt &arc) const {
|
deba@78
|
583 |
return Parent::source(static_cast<const Arc&>(arc));
|
deba@57
|
584 |
}
|
deba@57
|
585 |
|
deba@57
|
586 |
/// Base node of the iterator
|
deba@57
|
587 |
///
|
deba@57
|
588 |
/// Returns the base node of the iterator
|
deba@78
|
589 |
Node baseNode(const IncEdgeIt &edge) const {
|
deba@125
|
590 |
return edge._direction ? u(edge) : v(edge);
|
deba@57
|
591 |
}
|
deba@57
|
592 |
/// Running node of the iterator
|
deba@57
|
593 |
///
|
deba@57
|
594 |
/// Returns the running node of the iterator
|
deba@78
|
595 |
Node runningNode(const IncEdgeIt &edge) const {
|
deba@125
|
596 |
return edge._direction ? v(edge) : u(edge);
|
deba@57
|
597 |
}
|
deba@57
|
598 |
|
deba@57
|
599 |
// Mappable extension
|
deba@57
|
600 |
|
deba@57
|
601 |
template <typename _Value>
|
alpar@209
|
602 |
class NodeMap
|
deba@78
|
603 |
: public MapExtender<DefaultMap<Graph, Node, _Value> > {
|
deba@57
|
604 |
public:
|
deba@78
|
605 |
typedef GraphExtender Graph;
|
deba@78
|
606 |
typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
|
deba@57
|
607 |
|
alpar@209
|
608 |
NodeMap(const Graph& graph)
|
alpar@209
|
609 |
: Parent(graph) {}
|
alpar@209
|
610 |
NodeMap(const Graph& graph, const _Value& value)
|
alpar@209
|
611 |
: Parent(graph, value) {}
|
deba@57
|
612 |
|
kpeter@263
|
613 |
private:
|
deba@57
|
614 |
NodeMap& operator=(const NodeMap& cmap) {
|
alpar@209
|
615 |
return operator=<NodeMap>(cmap);
|
deba@57
|
616 |
}
|
deba@57
|
617 |
|
deba@57
|
618 |
template <typename CMap>
|
deba@57
|
619 |
NodeMap& operator=(const CMap& cmap) {
|
deba@57
|
620 |
Parent::operator=(cmap);
|
alpar@209
|
621 |
return *this;
|
deba@57
|
622 |
}
|
deba@57
|
623 |
|
deba@57
|
624 |
};
|
deba@57
|
625 |
|
deba@57
|
626 |
template <typename _Value>
|
alpar@209
|
627 |
class ArcMap
|
deba@78
|
628 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > {
|
deba@57
|
629 |
public:
|
deba@78
|
630 |
typedef GraphExtender Graph;
|
deba@78
|
631 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
|
deba@57
|
632 |
|
alpar@209
|
633 |
ArcMap(const Graph& graph)
|
alpar@209
|
634 |
: Parent(graph) {}
|
alpar@209
|
635 |
ArcMap(const Graph& graph, const _Value& value)
|
alpar@209
|
636 |
: Parent(graph, value) {}
|
deba@57
|
637 |
|
kpeter@263
|
638 |
private:
|
deba@57
|
639 |
ArcMap& operator=(const ArcMap& cmap) {
|
alpar@209
|
640 |
return operator=<ArcMap>(cmap);
|
deba@57
|
641 |
}
|
deba@57
|
642 |
|
deba@57
|
643 |
template <typename CMap>
|
deba@57
|
644 |
ArcMap& operator=(const CMap& cmap) {
|
deba@57
|
645 |
Parent::operator=(cmap);
|
alpar@209
|
646 |
return *this;
|
deba@57
|
647 |
}
|
deba@57
|
648 |
};
|
deba@57
|
649 |
|
deba@57
|
650 |
|
deba@57
|
651 |
template <typename _Value>
|
alpar@209
|
652 |
class EdgeMap
|
deba@78
|
653 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > {
|
deba@57
|
654 |
public:
|
deba@78
|
655 |
typedef GraphExtender Graph;
|
deba@78
|
656 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
|
deba@57
|
657 |
|
alpar@209
|
658 |
EdgeMap(const Graph& graph)
|
alpar@209
|
659 |
: Parent(graph) {}
|
deba@57
|
660 |
|
alpar@209
|
661 |
EdgeMap(const Graph& graph, const _Value& value)
|
alpar@209
|
662 |
: Parent(graph, value) {}
|
deba@57
|
663 |
|
kpeter@263
|
664 |
private:
|
deba@57
|
665 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
alpar@209
|
666 |
return operator=<EdgeMap>(cmap);
|
deba@57
|
667 |
}
|
deba@57
|
668 |
|
deba@57
|
669 |
template <typename CMap>
|
deba@57
|
670 |
EdgeMap& operator=(const CMap& cmap) {
|
deba@57
|
671 |
Parent::operator=(cmap);
|
alpar@209
|
672 |
return *this;
|
deba@57
|
673 |
}
|
deba@57
|
674 |
|
deba@57
|
675 |
};
|
deba@57
|
676 |
|
deba@57
|
677 |
// Alteration extension
|
deba@57
|
678 |
|
deba@57
|
679 |
Node addNode() {
|
deba@57
|
680 |
Node node = Parent::addNode();
|
deba@57
|
681 |
notifier(Node()).add(node);
|
deba@57
|
682 |
return node;
|
deba@57
|
683 |
}
|
deba@57
|
684 |
|
deba@57
|
685 |
Edge addEdge(const Node& from, const Node& to) {
|
deba@57
|
686 |
Edge edge = Parent::addEdge(from, to);
|
deba@57
|
687 |
notifier(Edge()).add(edge);
|
deba@57
|
688 |
std::vector<Arc> ev;
|
deba@57
|
689 |
ev.push_back(Parent::direct(edge, true));
|
alpar@209
|
690 |
ev.push_back(Parent::direct(edge, false));
|
deba@57
|
691 |
notifier(Arc()).add(ev);
|
deba@57
|
692 |
return edge;
|
deba@57
|
693 |
}
|
alpar@209
|
694 |
|
deba@57
|
695 |
void clear() {
|
deba@57
|
696 |
notifier(Arc()).clear();
|
deba@57
|
697 |
notifier(Edge()).clear();
|
deba@57
|
698 |
notifier(Node()).clear();
|
deba@57
|
699 |
Parent::clear();
|
deba@57
|
700 |
}
|
deba@57
|
701 |
|
deba@78
|
702 |
template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
|
alpar@209
|
703 |
void build(const Graph& graph, NodeRefMap& nodeRef,
|
deba@57
|
704 |
EdgeRefMap& edgeRef) {
|
deba@78
|
705 |
Parent::build(graph, nodeRef, edgeRef);
|
deba@57
|
706 |
notifier(Node()).build();
|
deba@57
|
707 |
notifier(Edge()).build();
|
deba@57
|
708 |
notifier(Arc()).build();
|
deba@57
|
709 |
}
|
deba@57
|
710 |
|
deba@57
|
711 |
void erase(const Node& node) {
|
deba@57
|
712 |
Arc arc;
|
deba@57
|
713 |
Parent::firstOut(arc, node);
|
deba@57
|
714 |
while (arc != INVALID ) {
|
alpar@209
|
715 |
erase(arc);
|
alpar@209
|
716 |
Parent::firstOut(arc, node);
|
alpar@209
|
717 |
}
|
deba@57
|
718 |
|
deba@57
|
719 |
Parent::firstIn(arc, node);
|
deba@57
|
720 |
while (arc != INVALID ) {
|
alpar@209
|
721 |
erase(arc);
|
alpar@209
|
722 |
Parent::firstIn(arc, node);
|
deba@57
|
723 |
}
|
deba@57
|
724 |
|
deba@57
|
725 |
notifier(Node()).erase(node);
|
deba@57
|
726 |
Parent::erase(node);
|
deba@57
|
727 |
}
|
deba@57
|
728 |
|
deba@57
|
729 |
void erase(const Edge& edge) {
|
deba@78
|
730 |
std::vector<Arc> av;
|
deba@78
|
731 |
av.push_back(Parent::direct(edge, true));
|
alpar@209
|
732 |
av.push_back(Parent::direct(edge, false));
|
deba@78
|
733 |
notifier(Arc()).erase(av);
|
deba@57
|
734 |
notifier(Edge()).erase(edge);
|
deba@57
|
735 |
Parent::erase(edge);
|
deba@57
|
736 |
}
|
deba@57
|
737 |
|
deba@57
|
738 |
GraphExtender() {
|
alpar@209
|
739 |
node_notifier.setContainer(*this);
|
deba@57
|
740 |
arc_notifier.setContainer(*this);
|
deba@57
|
741 |
edge_notifier.setContainer(*this);
|
alpar@209
|
742 |
}
|
deba@57
|
743 |
|
deba@57
|
744 |
~GraphExtender() {
|
deba@57
|
745 |
edge_notifier.clear();
|
deba@57
|
746 |
arc_notifier.clear();
|
alpar@209
|
747 |
node_notifier.clear();
|
alpar@209
|
748 |
}
|
deba@57
|
749 |
|
deba@57
|
750 |
};
|
deba@57
|
751 |
|
deba@57
|
752 |
}
|
deba@57
|
753 |
|
deba@57
|
754 |
#endif
|