1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2007 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_GRAPH_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/bits/invalid.h> |
23 |
#include <lemon/bits/utility.h> |
|
23 | 24 |
|
24 | 25 |
#include <lemon/bits/map_extender.h> |
25 | 26 |
#include <lemon/bits/default_map.h> |
26 | 27 |
|
27 | 28 |
#include <lemon/concept_check.h> |
28 | 29 |
#include <lemon/concepts/maps.h> |
29 | 30 |
|
30 | 31 |
///\ingroup graphbits |
31 | 32 |
///\file |
32 | 33 |
///\brief Extenders for the digraph types |
33 | 34 |
namespace lemon { |
34 | 35 |
|
35 | 36 |
/// \ingroup graphbits |
36 | 37 |
/// |
37 | 38 |
/// \brief Extender for the Digraphs |
38 | 39 |
template <typename Base> |
39 | 40 |
class DigraphExtender : public Base { |
40 | 41 |
public: |
41 | 42 |
|
42 | 43 |
typedef Base Parent; |
43 | 44 |
typedef DigraphExtender Digraph; |
44 | 45 |
|
45 | 46 |
// Base extensions |
46 | 47 |
|
47 | 48 |
typedef typename Parent::Node Node; |
48 | 49 |
typedef typename Parent::Arc Arc; |
49 | 50 |
|
50 | 51 |
int maxId(Node) const { |
51 | 52 |
return Parent::maxNodeId(); |
52 | 53 |
} |
53 | 54 |
|
54 | 55 |
int maxId(Arc) const { |
55 | 56 |
return Parent::maxArcId(); |
56 | 57 |
} |
57 | 58 |
|
58 | 59 |
Node fromId(int id, Node) const { |
59 | 60 |
return Parent::nodeFromId(id); |
60 | 61 |
} |
61 | 62 |
|
62 | 63 |
Arc fromId(int id, Arc) const { |
63 | 64 |
return Parent::arcFromId(id); |
64 | 65 |
} |
65 | 66 |
|
66 | 67 |
Node oppositeNode(const Node &n, const Arc &e) const { |
67 | 68 |
if (n == Parent::source(e)) |
68 | 69 |
return Parent::target(e); |
69 | 70 |
else if(n==Parent::target(e)) |
70 | 71 |
return Parent::source(e); |
71 | 72 |
else |
72 | 73 |
return INVALID; |
73 | 74 |
} |
74 | 75 |
|
75 | 76 |
// Alterable extension |
76 | 77 |
|
77 | 78 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
78 | 79 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
79 | 80 |
|
80 | 81 |
|
81 | 82 |
protected: |
82 | 83 |
|
83 | 84 |
mutable NodeNotifier node_notifier; |
84 | 85 |
mutable ArcNotifier arc_notifier; |
85 | 86 |
|
86 | 87 |
public: |
87 | 88 |
|
88 | 89 |
NodeNotifier& notifier(Node) const { |
89 | 90 |
return node_notifier; |
90 | 91 |
} |
91 | 92 |
|
92 | 93 |
ArcNotifier& notifier(Arc) const { |
93 | 94 |
return arc_notifier; |
94 | 95 |
} |
95 | 96 |
|
96 | 97 |
class NodeIt : public Node { |
97 | 98 |
const Digraph* digraph; |
98 | 99 |
public: |
99 | 100 |
|
100 | 101 |
NodeIt() {} |
101 | 102 |
|
102 | 103 |
NodeIt(Invalid i) : Node(i) { } |
103 | 104 |
|
104 | 105 |
explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) { |
105 | 106 |
_digraph.first(static_cast<Node&>(*this)); |
106 | 107 |
} |
107 | 108 |
|
108 | 109 |
NodeIt(const Digraph& _digraph, const Node& node) |
109 | 110 |
: Node(node), digraph(&_digraph) {} |
110 | 111 |
|
111 | 112 |
NodeIt& operator++() { |
112 | 113 |
digraph->next(*this); |
113 | 114 |
return *this; |
114 | 115 |
} |
115 | 116 |
|
116 | 117 |
}; |
117 | 118 |
|
118 | 119 |
|
... | ... |
@@ -240,192 +241,194 @@ |
240 | 241 |
}; |
241 | 242 |
|
242 | 243 |
template <typename _Value> |
243 | 244 |
class ArcMap |
244 | 245 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
245 | 246 |
public: |
246 | 247 |
typedef DigraphExtender Digraph; |
247 | 248 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
248 | 249 |
|
249 | 250 |
explicit ArcMap(const Digraph& digraph) |
250 | 251 |
: Parent(digraph) {} |
251 | 252 |
ArcMap(const Digraph& digraph, const _Value& value) |
252 | 253 |
: Parent(digraph, value) {} |
253 | 254 |
|
254 | 255 |
ArcMap& operator=(const ArcMap& cmap) { |
255 | 256 |
return operator=<ArcMap>(cmap); |
256 | 257 |
} |
257 | 258 |
|
258 | 259 |
template <typename CMap> |
259 | 260 |
ArcMap& operator=(const CMap& cmap) { |
260 | 261 |
Parent::operator=(cmap); |
261 | 262 |
return *this; |
262 | 263 |
} |
263 | 264 |
}; |
264 | 265 |
|
265 | 266 |
|
266 | 267 |
Node addNode() { |
267 | 268 |
Node node = Parent::addNode(); |
268 | 269 |
notifier(Node()).add(node); |
269 | 270 |
return node; |
270 | 271 |
} |
271 | 272 |
|
272 | 273 |
Arc addArc(const Node& from, const Node& to) { |
273 | 274 |
Arc arc = Parent::addArc(from, to); |
274 | 275 |
notifier(Arc()).add(arc); |
275 | 276 |
return arc; |
276 | 277 |
} |
277 | 278 |
|
278 | 279 |
void clear() { |
279 | 280 |
notifier(Arc()).clear(); |
280 | 281 |
notifier(Node()).clear(); |
281 | 282 |
Parent::clear(); |
282 | 283 |
} |
283 | 284 |
|
284 | 285 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
285 | 286 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) { |
286 | 287 |
Parent::build(digraph, nodeRef, arcRef); |
287 | 288 |
notifier(Node()).build(); |
288 | 289 |
notifier(Arc()).build(); |
289 | 290 |
} |
290 | 291 |
|
291 | 292 |
void erase(const Node& node) { |
292 | 293 |
Arc arc; |
293 | 294 |
Parent::firstOut(arc, node); |
294 | 295 |
while (arc != INVALID ) { |
295 | 296 |
erase(arc); |
296 | 297 |
Parent::firstOut(arc, node); |
297 | 298 |
} |
298 | 299 |
|
299 | 300 |
Parent::firstIn(arc, node); |
300 | 301 |
while (arc != INVALID ) { |
301 | 302 |
erase(arc); |
302 | 303 |
Parent::firstIn(arc, node); |
303 | 304 |
} |
304 | 305 |
|
305 | 306 |
notifier(Node()).erase(node); |
306 | 307 |
Parent::erase(node); |
307 | 308 |
} |
308 | 309 |
|
309 | 310 |
void erase(const Arc& arc) { |
310 | 311 |
notifier(Arc()).erase(arc); |
311 | 312 |
Parent::erase(arc); |
312 | 313 |
} |
313 | 314 |
|
314 | 315 |
DigraphExtender() { |
315 | 316 |
node_notifier.setContainer(*this); |
316 | 317 |
arc_notifier.setContainer(*this); |
317 | 318 |
} |
318 | 319 |
|
319 | 320 |
|
320 | 321 |
~DigraphExtender() { |
321 | 322 |
arc_notifier.clear(); |
322 | 323 |
node_notifier.clear(); |
323 | 324 |
} |
324 | 325 |
}; |
325 | 326 |
|
326 | 327 |
/// \ingroup graphbits |
327 | 328 |
/// |
328 | 329 |
/// \brief Extender for the Graphs |
329 | 330 |
template <typename Base> |
330 | 331 |
class GraphExtender : public Base { |
331 | 332 |
public: |
332 | 333 |
|
333 | 334 |
typedef Base Parent; |
334 | 335 |
typedef GraphExtender Digraph; |
335 | 336 |
|
337 |
typedef True UndirectedTag; |
|
338 |
|
|
336 | 339 |
typedef typename Parent::Node Node; |
337 | 340 |
typedef typename Parent::Arc Arc; |
338 | 341 |
typedef typename Parent::Edge Edge; |
339 | 342 |
|
340 | 343 |
// Graph extension |
341 | 344 |
|
342 | 345 |
int maxId(Node) const { |
343 | 346 |
return Parent::maxNodeId(); |
344 | 347 |
} |
345 | 348 |
|
346 | 349 |
int maxId(Arc) const { |
347 | 350 |
return Parent::maxArcId(); |
348 | 351 |
} |
349 | 352 |
|
350 | 353 |
int maxId(Edge) const { |
351 | 354 |
return Parent::maxEdgeId(); |
352 | 355 |
} |
353 | 356 |
|
354 | 357 |
Node fromId(int id, Node) const { |
355 | 358 |
return Parent::nodeFromId(id); |
356 | 359 |
} |
357 | 360 |
|
358 | 361 |
Arc fromId(int id, Arc) const { |
359 | 362 |
return Parent::arcFromId(id); |
360 | 363 |
} |
361 | 364 |
|
362 | 365 |
Edge fromId(int id, Edge) const { |
363 | 366 |
return Parent::edgeFromId(id); |
364 | 367 |
} |
365 | 368 |
|
366 | 369 |
Node oppositeNode(const Node &n, const Edge &e) const { |
367 | 370 |
if( n == Parent::source(e)) |
368 | 371 |
return Parent::target(e); |
369 | 372 |
else if( n == Parent::target(e)) |
370 | 373 |
return Parent::source(e); |
371 | 374 |
else |
372 | 375 |
return INVALID; |
373 | 376 |
} |
374 | 377 |
|
375 | 378 |
Arc oppositeArc(const Arc &e) const { |
376 | 379 |
return Parent::direct(e, !Parent::direction(e)); |
377 | 380 |
} |
378 | 381 |
|
379 | 382 |
using Parent::direct; |
380 | 383 |
Arc direct(const Edge &ue, const Node &s) const { |
381 | 384 |
return Parent::direct(ue, Parent::source(ue) == s); |
382 | 385 |
} |
383 | 386 |
|
384 | 387 |
// Alterable extension |
385 | 388 |
|
386 | 389 |
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; |
387 | 390 |
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier; |
388 | 391 |
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; |
389 | 392 |
|
390 | 393 |
|
391 | 394 |
protected: |
392 | 395 |
|
393 | 396 |
mutable NodeNotifier node_notifier; |
394 | 397 |
mutable ArcNotifier arc_notifier; |
395 | 398 |
mutable EdgeNotifier edge_notifier; |
396 | 399 |
|
397 | 400 |
public: |
398 | 401 |
|
399 | 402 |
NodeNotifier& notifier(Node) const { |
400 | 403 |
return node_notifier; |
401 | 404 |
} |
402 | 405 |
|
403 | 406 |
ArcNotifier& notifier(Arc) const { |
404 | 407 |
return arc_notifier; |
405 | 408 |
} |
406 | 409 |
|
407 | 410 |
EdgeNotifier& notifier(Edge) const { |
408 | 411 |
return edge_notifier; |
409 | 412 |
} |
410 | 413 |
|
411 | 414 |
|
412 | 415 |
|
413 | 416 |
class NodeIt : public Node { |
414 | 417 |
const Digraph* digraph; |
415 | 418 |
public: |
416 | 419 |
|
417 | 420 |
NodeIt() {} |
418 | 421 |
|
419 | 422 |
NodeIt(Invalid i) : Node(i) { } |
420 | 423 |
|
421 | 424 |
explicit NodeIt(const Digraph& _digraph) : digraph(&_digraph) { |
422 | 425 |
_digraph.first(static_cast<Node&>(*this)); |
423 | 426 |
} |
424 | 427 |
|
425 | 428 |
NodeIt(const Digraph& _digraph, const Node& node) |
426 | 429 |
: Node(node), digraph(&_digraph) {} |
427 | 430 |
|
428 | 431 |
NodeIt& operator++() { |
429 | 432 |
digraph->next(*this); |
430 | 433 |
return *this; |
431 | 434 |
} |
0 comments (0 inline)