gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge bugfix #371 to branch 1.0
0 2 0
merge 1.0
0 files changed with 26 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
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_CORE_H
20 20
#define LEMON_CORE_H
21 21

	
22 22
#include <vector>
23 23
#include <algorithm>
24 24

	
25 25
#include <lemon/config.h>
26 26
#include <lemon/bits/enable_if.h>
27 27
#include <lemon/bits/traits.h>
28 28
#include <lemon/assert.h>
29 29

	
30 30
///\file
31 31
///\brief LEMON core utilities.
32 32
///
33 33
///This header file contains core utilities for LEMON.
34 34
///It is automatically included by all graph types, therefore it usually
35 35
///do not have to be included directly.
36 36

	
37 37
namespace lemon {
38 38

	
39 39
  /// \brief Dummy type to make it easier to create invalid iterators.
40 40
  ///
41 41
  /// Dummy type to make it easier to create invalid iterators.
42 42
  /// See \ref INVALID for the usage.
43 43
  struct Invalid {
44 44
  public:
45 45
    bool operator==(Invalid) { return true;  }
46 46
    bool operator!=(Invalid) { return false; }
47 47
    bool operator< (Invalid) { return false; }
48 48
  };
49 49

	
50 50
  /// \brief Invalid iterators.
51 51
  ///
52 52
  /// \ref Invalid is a global type that converts to each iterator
53 53
  /// in such a way that the value of the target iterator will be invalid.
54 54
#ifdef LEMON_ONLY_TEMPLATES
55 55
  const Invalid INVALID = Invalid();
56 56
#else
57 57
  extern const Invalid INVALID;
58 58
#endif
59 59

	
60 60
  /// \addtogroup gutils
61 61
  /// @{
62 62

	
63 63
  ///Create convenience typedefs for the digraph types and iterators
64 64

	
65 65
  ///This \c \#define creates convenient type definitions for the following
66 66
  ///types of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
67 67
  ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
68 68
  ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
69 69
  ///
70 70
  ///\note If the graph type is a dependent type, ie. the graph type depend
71 71
  ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
72 72
  ///macro.
73 73
#define DIGRAPH_TYPEDEFS(Digraph)                                       \
74 74
  typedef Digraph::Node Node;                                           \
75 75
  typedef Digraph::NodeIt NodeIt;                                       \
76 76
  typedef Digraph::Arc Arc;                                             \
77 77
  typedef Digraph::ArcIt ArcIt;                                         \
78 78
  typedef Digraph::InArcIt InArcIt;                                     \
79 79
  typedef Digraph::OutArcIt OutArcIt;                                   \
80 80
  typedef Digraph::NodeMap<bool> BoolNodeMap;                           \
81 81
  typedef Digraph::NodeMap<int> IntNodeMap;                             \
82 82
  typedef Digraph::NodeMap<double> DoubleNodeMap;                       \
83 83
  typedef Digraph::ArcMap<bool> BoolArcMap;                             \
84 84
  typedef Digraph::ArcMap<int> IntArcMap;                               \
85 85
  typedef Digraph::ArcMap<double> DoubleArcMap
86 86

	
87 87
  ///Create convenience typedefs for the digraph types and iterators
88 88

	
89 89
  ///\see DIGRAPH_TYPEDEFS
90 90
  ///
91 91
  ///\note Use this macro, if the graph type is a dependent type,
92 92
  ///ie. the graph type depend on a template parameter.
93 93
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph)                              \
94 94
  typedef typename Digraph::Node Node;                                  \
95 95
  typedef typename Digraph::NodeIt NodeIt;                              \
96 96
  typedef typename Digraph::Arc Arc;                                    \
97 97
  typedef typename Digraph::ArcIt ArcIt;                                \
98 98
  typedef typename Digraph::InArcIt InArcIt;                            \
99 99
  typedef typename Digraph::OutArcIt OutArcIt;                          \
100 100
  typedef typename Digraph::template NodeMap<bool> BoolNodeMap;         \
101 101
  typedef typename Digraph::template NodeMap<int> IntNodeMap;           \
102 102
  typedef typename Digraph::template NodeMap<double> DoubleNodeMap;     \
103 103
  typedef typename Digraph::template ArcMap<bool> BoolArcMap;           \
104 104
  typedef typename Digraph::template ArcMap<int> IntArcMap;             \
105 105
  typedef typename Digraph::template ArcMap<double> DoubleArcMap
106 106

	
107 107
  ///Create convenience typedefs for the graph types and iterators
108 108

	
109 109
  ///This \c \#define creates the same convenient type definitions as defined
110 110
  ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
111 111
  ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
112 112
  ///\c DoubleEdgeMap.
113 113
  ///
114 114
  ///\note If the graph type is a dependent type, ie. the graph type depend
115 115
  ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
116 116
  ///macro.
117 117
#define GRAPH_TYPEDEFS(Graph)                                           \
118 118
  DIGRAPH_TYPEDEFS(Graph);                                              \
119 119
  typedef Graph::Edge Edge;                                             \
120 120
  typedef Graph::EdgeIt EdgeIt;                                         \
121 121
  typedef Graph::IncEdgeIt IncEdgeIt;                                   \
122 122
  typedef Graph::EdgeMap<bool> BoolEdgeMap;                             \
123 123
  typedef Graph::EdgeMap<int> IntEdgeMap;                               \
124 124
  typedef Graph::EdgeMap<double> DoubleEdgeMap
125 125

	
126 126
  ///Create convenience typedefs for the graph types and iterators
127 127

	
128 128
  ///\see GRAPH_TYPEDEFS
129 129
  ///
130 130
  ///\note Use this macro, if the graph type is a dependent type,
131 131
  ///ie. the graph type depend on a template parameter.
132 132
#define TEMPLATE_GRAPH_TYPEDEFS(Graph)                                  \
133 133
  TEMPLATE_DIGRAPH_TYPEDEFS(Graph);                                     \
134 134
  typedef typename Graph::Edge Edge;                                    \
135 135
  typedef typename Graph::EdgeIt EdgeIt;                                \
136 136
  typedef typename Graph::IncEdgeIt IncEdgeIt;                          \
137 137
  typedef typename Graph::template EdgeMap<bool> BoolEdgeMap;           \
138 138
  typedef typename Graph::template EdgeMap<int> IntEdgeMap;             \
139 139
  typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
140 140

	
141 141
  /// \brief Function to count the items in a graph.
142 142
  ///
143 143
  /// This function counts the items (nodes, arcs etc.) in a graph.
144 144
  /// The complexity of the function is linear because
145 145
  /// it iterates on all of the items.
146 146
  template <typename Graph, typename Item>
147 147
  inline int countItems(const Graph& g) {
148 148
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
149 149
    int num = 0;
150 150
    for (ItemIt it(g); it != INVALID; ++it) {
151 151
      ++num;
152 152
    }
153 153
    return num;
154 154
  }
155 155

	
156 156
  // Node counting:
157 157

	
158 158
  namespace _core_bits {
159 159

	
160 160
    template <typename Graph, typename Enable = void>
161 161
    struct CountNodesSelector {
162 162
      static int count(const Graph &g) {
163 163
        return countItems<Graph, typename Graph::Node>(g);
164 164
      }
165 165
    };
166 166

	
167 167
    template <typename Graph>
168 168
    struct CountNodesSelector<
169 169
      Graph, typename
170 170
      enable_if<typename Graph::NodeNumTag, void>::type>
171 171
    {
172 172
      static int count(const Graph &g) {
173 173
        return g.nodeNum();
174 174
      }
175 175
    };
176 176
  }
177 177

	
178 178
  /// \brief Function to count the nodes in the graph.
179 179
  ///
180 180
  /// This function counts the nodes in the graph.
181 181
  /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
182 182
  /// graph structures it is specialized to run in <em>O</em>(1).
183 183
  ///
184 184
  /// \note If the graph contains a \c nodeNum() member function and a
185 185
  /// \c NodeNumTag tag then this function calls directly the member
186 186
  /// function to query the cardinality of the node set.
187 187
  template <typename Graph>
188 188
  inline int countNodes(const Graph& g) {
189 189
    return _core_bits::CountNodesSelector<Graph>::count(g);
190 190
  }
191 191

	
192 192
  // Arc counting:
193 193

	
194 194
  namespace _core_bits {
195 195

	
196 196
    template <typename Graph, typename Enable = void>
197 197
    struct CountArcsSelector {
198 198
      static int count(const Graph &g) {
199 199
        return countItems<Graph, typename Graph::Arc>(g);
200 200
      }
201 201
    };
202 202

	
203 203
    template <typename Graph>
204 204
    struct CountArcsSelector<
205 205
      Graph,
206 206
      typename enable_if<typename Graph::ArcNumTag, void>::type>
207 207
    {
208 208
      static int count(const Graph &g) {
209 209
        return g.arcNum();
210 210
      }
211 211
    };
212 212
  }
213 213

	
214 214
  /// \brief Function to count the arcs in the graph.
215 215
  ///
216 216
  /// This function counts the arcs in the graph.
217 217
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
218 218
  /// graph structures it is specialized to run in <em>O</em>(1).
219 219
  ///
220 220
  /// \note If the graph contains a \c arcNum() member function and a
221 221
  /// \c ArcNumTag tag then this function calls directly the member
222 222
  /// function to query the cardinality of the arc set.
223 223
  template <typename Graph>
224 224
  inline int countArcs(const Graph& g) {
225 225
    return _core_bits::CountArcsSelector<Graph>::count(g);
226 226
  }
227 227

	
228 228
  // Edge counting:
229 229

	
230 230
  namespace _core_bits {
231 231

	
232 232
    template <typename Graph, typename Enable = void>
233 233
    struct CountEdgesSelector {
234 234
      static int count(const Graph &g) {
235 235
        return countItems<Graph, typename Graph::Edge>(g);
236 236
      }
237 237
    };
238 238

	
239 239
    template <typename Graph>
240 240
    struct CountEdgesSelector<
241 241
      Graph,
242 242
      typename enable_if<typename Graph::EdgeNumTag, void>::type>
243 243
    {
244 244
      static int count(const Graph &g) {
245 245
        return g.edgeNum();
246 246
      }
247 247
    };
248 248
  }
249 249

	
250 250
  /// \brief Function to count the edges in the graph.
251 251
  ///
252 252
  /// This function counts the edges in the graph.
253 253
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
254 254
  /// graph structures it is specialized to run in <em>O</em>(1).
255 255
  ///
256 256
  /// \note If the graph contains a \c edgeNum() member function and a
257 257
  /// \c EdgeNumTag tag then this function calls directly the member
258 258
  /// function to query the cardinality of the edge set.
259 259
  template <typename Graph>
260 260
  inline int countEdges(const Graph& g) {
261 261
    return _core_bits::CountEdgesSelector<Graph>::count(g);
262 262

	
263 263
  }
264 264

	
265 265

	
266 266
  template <typename Graph, typename DegIt>
267 267
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
268 268
    int num = 0;
269 269
    for (DegIt it(_g, _n); it != INVALID; ++it) {
270 270
      ++num;
271 271
    }
272 272
    return num;
273 273
  }
274 274

	
275 275
  /// \brief Function to count the number of the out-arcs from node \c n.
276 276
  ///
277 277
  /// This function counts the number of the out-arcs from node \c n
278 278
  /// in the graph \c g.
279 279
  template <typename Graph>
280 280
  inline int countOutArcs(const Graph& g,  const typename Graph::Node& n) {
281 281
    return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
282 282
  }
283 283

	
284 284
  /// \brief Function to count the number of the in-arcs to node \c n.
285 285
  ///
286 286
  /// This function counts the number of the in-arcs to node \c n
287 287
  /// in the graph \c g.
288 288
  template <typename Graph>
289 289
  inline int countInArcs(const Graph& g,  const typename Graph::Node& n) {
290 290
    return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
291 291
  }
292 292

	
293 293
  /// \brief Function to count the number of the inc-edges to node \c n.
294 294
  ///
295 295
  /// This function counts the number of the inc-edges to node \c n
296 296
  /// in the undirected graph \c g.
297 297
  template <typename Graph>
298 298
  inline int countIncEdges(const Graph& g,  const typename Graph::Node& n) {
299 299
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
300 300
  }
301 301

	
302 302
  namespace _core_bits {
303 303

	
304 304
    template <typename Digraph, typename Item, typename RefMap>
305 305
    class MapCopyBase {
306 306
    public:
307 307
      virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
308 308

	
309 309
      virtual ~MapCopyBase() {}
310 310
    };
311 311

	
312 312
    template <typename Digraph, typename Item, typename RefMap,
313 313
              typename FromMap, typename ToMap>
314 314
    class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
315 315
    public:
316 316

	
317 317
      MapCopy(const FromMap& map, ToMap& tmap)
318 318
        : _map(map), _tmap(tmap) {}
319 319

	
320 320
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
321 321
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
322 322
        for (ItemIt it(digraph); it != INVALID; ++it) {
323 323
          _tmap.set(refMap[it], _map[it]);
324 324
        }
325 325
      }
326 326

	
327 327
    private:
328 328
      const FromMap& _map;
329 329
      ToMap& _tmap;
330 330
    };
331 331

	
332 332
    template <typename Digraph, typename Item, typename RefMap, typename It>
333 333
    class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
334 334
    public:
335 335

	
336 336
      ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
337 337

	
338 338
      virtual void copy(const Digraph&, const RefMap& refMap) {
339 339
        _it = refMap[_item];
340 340
      }
341 341

	
342 342
    private:
343 343
      Item _item;
344 344
      It& _it;
345 345
    };
346 346

	
347 347
    template <typename Digraph, typename Item, typename RefMap, typename Ref>
348 348
    class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
349 349
    public:
350 350

	
351 351
      RefCopy(Ref& map) : _map(map) {}
352 352

	
353 353
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
354 354
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
355 355
        for (ItemIt it(digraph); it != INVALID; ++it) {
356 356
          _map.set(it, refMap[it]);
357 357
        }
358 358
      }
359 359

	
360 360
    private:
361 361
      Ref& _map;
362 362
    };
363 363

	
364 364
    template <typename Digraph, typename Item, typename RefMap,
365 365
              typename CrossRef>
366 366
    class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
367 367
    public:
368 368

	
369 369
      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
370 370

	
371 371
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
372 372
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
373 373
        for (ItemIt it(digraph); it != INVALID; ++it) {
374 374
          _cmap.set(refMap[it], it);
375 375
        }
376 376
      }
377 377

	
378 378
    private:
379 379
      CrossRef& _cmap;
380 380
    };
381 381

	
382 382
    template <typename Digraph, typename Enable = void>
383 383
    struct DigraphCopySelector {
384 384
      template <typename From, typename NodeRefMap, typename ArcRefMap>
385 385
      static void copy(const From& from, Digraph &to,
386 386
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
387
        to.clear();
387 388
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
388 389
          nodeRefMap[it] = to.addNode();
389 390
        }
390 391
        for (typename From::ArcIt it(from); it != INVALID; ++it) {
391 392
          arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
392 393
                                    nodeRefMap[from.target(it)]);
393 394
        }
394 395
      }
395 396
    };
396 397

	
397 398
    template <typename Digraph>
398 399
    struct DigraphCopySelector<
399 400
      Digraph,
400 401
      typename enable_if<typename Digraph::BuildTag, void>::type>
401 402
    {
402 403
      template <typename From, typename NodeRefMap, typename ArcRefMap>
403 404
      static void copy(const From& from, Digraph &to,
404 405
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
405 406
        to.build(from, nodeRefMap, arcRefMap);
406 407
      }
407 408
    };
408 409

	
409 410
    template <typename Graph, typename Enable = void>
410 411
    struct GraphCopySelector {
411 412
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
412 413
      static void copy(const From& from, Graph &to,
413 414
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
415
        to.clear();
414 416
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
415 417
          nodeRefMap[it] = to.addNode();
416 418
        }
417 419
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
418 420
          edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
419 421
                                      nodeRefMap[from.v(it)]);
420 422
        }
421 423
      }
422 424
    };
423 425

	
424 426
    template <typename Graph>
425 427
    struct GraphCopySelector<
426 428
      Graph,
427 429
      typename enable_if<typename Graph::BuildTag, void>::type>
428 430
    {
429 431
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
430 432
      static void copy(const From& from, Graph &to,
431 433
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
432 434
        to.build(from, nodeRefMap, edgeRefMap);
433 435
      }
434 436
    };
435 437

	
436 438
  }
437 439

	
438 440
  /// \brief Class to copy a digraph.
439 441
  ///
440 442
  /// Class to copy a digraph to another digraph (duplicate a digraph). The
441 443
  /// simplest way of using it is through the \c digraphCopy() function.
442 444
  ///
443 445
  /// This class not only make a copy of a digraph, but it can create
444 446
  /// references and cross references between the nodes and arcs of
445 447
  /// the two digraphs, and it can copy maps to use with the newly created
446 448
  /// digraph.
447 449
  ///
448 450
  /// To make a copy from a digraph, first an instance of DigraphCopy
449 451
  /// should be created, then the data belongs to the digraph should
450 452
  /// assigned to copy. In the end, the \c run() member should be
451 453
  /// called.
452 454
  ///
453 455
  /// The next code copies a digraph with several data:
454 456
  ///\code
455 457
  ///  DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
456 458
  ///  // Create references for the nodes
457 459
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
458 460
  ///  cg.nodeRef(nr);
459 461
  ///  // Create cross references (inverse) for the arcs
460 462
  ///  NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
461 463
  ///  cg.arcCrossRef(acr);
462 464
  ///  // Copy an arc map
463 465
  ///  OrigGraph::ArcMap<double> oamap(orig_graph);
464 466
  ///  NewGraph::ArcMap<double> namap(new_graph);
465 467
  ///  cg.arcMap(oamap, namap);
466 468
  ///  // Copy a node
467 469
  ///  OrigGraph::Node on;
468 470
  ///  NewGraph::Node nn;
469 471
  ///  cg.node(on, nn);
470 472
  ///  // Execute copying
471 473
  ///  cg.run();
472 474
  ///\endcode
473 475
  template <typename From, typename To>
474 476
  class DigraphCopy {
475 477
  private:
476 478

	
477 479
    typedef typename From::Node Node;
478 480
    typedef typename From::NodeIt NodeIt;
479 481
    typedef typename From::Arc Arc;
480 482
    typedef typename From::ArcIt ArcIt;
481 483

	
482 484
    typedef typename To::Node TNode;
483 485
    typedef typename To::Arc TArc;
484 486

	
485 487
    typedef typename From::template NodeMap<TNode> NodeRefMap;
486 488
    typedef typename From::template ArcMap<TArc> ArcRefMap;
487 489

	
488 490
  public:
489 491

	
490 492
    /// \brief Constructor of DigraphCopy.
491 493
    ///
492 494
    /// Constructor of DigraphCopy for copying the content of the
493 495
    /// \c from digraph into the \c to digraph.
494 496
    DigraphCopy(const From& from, To& to)
495 497
      : _from(from), _to(to) {}
496 498

	
497 499
    /// \brief Destructor of DigraphCopy
498 500
    ///
499 501
    /// Destructor of DigraphCopy.
500 502
    ~DigraphCopy() {
501 503
      for (int i = 0; i < int(_node_maps.size()); ++i) {
502 504
        delete _node_maps[i];
503 505
      }
504 506
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
505 507
        delete _arc_maps[i];
506 508
      }
507 509

	
508 510
    }
509 511

	
510 512
    /// \brief Copy the node references into the given map.
511 513
    ///
512 514
    /// This function copies the node references into the given map.
513 515
    /// The parameter should be a map, whose key type is the Node type of
514 516
    /// the source digraph, while the value type is the Node type of the
515 517
    /// destination digraph.
516 518
    template <typename NodeRef>
517 519
    DigraphCopy& nodeRef(NodeRef& map) {
518 520
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
519 521
                           NodeRefMap, NodeRef>(map));
520 522
      return *this;
521 523
    }
522 524

	
523 525
    /// \brief Copy the node cross references into the given map.
524 526
    ///
525 527
    /// This function copies the node cross references (reverse references)
526 528
    /// into the given map. The parameter should be a map, whose key type
527 529
    /// is the Node type of the destination digraph, while the value type is
528 530
    /// the Node type of the source digraph.
529 531
    template <typename NodeCrossRef>
530 532
    DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
531 533
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
532 534
                           NodeRefMap, NodeCrossRef>(map));
533 535
      return *this;
534 536
    }
535 537

	
536 538
    /// \brief Make a copy of the given node map.
537 539
    ///
538 540
    /// This function makes a copy of the given node map for the newly
539 541
    /// created digraph.
540 542
    /// The key type of the new map \c tmap should be the Node type of the
541 543
    /// destination digraph, and the key type of the original map \c map
542 544
    /// should be the Node type of the source digraph.
543 545
    template <typename FromMap, typename ToMap>
544 546
    DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
545 547
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
546 548
                           NodeRefMap, FromMap, ToMap>(map, tmap));
547 549
      return *this;
548 550
    }
549 551

	
550 552
    /// \brief Make a copy of the given node.
551 553
    ///
552 554
    /// This function makes a copy of the given node.
553 555
    DigraphCopy& node(const Node& node, TNode& tnode) {
554 556
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
555 557
                           NodeRefMap, TNode>(node, tnode));
556 558
      return *this;
557 559
    }
558 560

	
559 561
    /// \brief Copy the arc references into the given map.
560 562
    ///
561 563
    /// This function copies the arc references into the given map.
562 564
    /// The parameter should be a map, whose key type is the Arc type of
563 565
    /// the source digraph, while the value type is the Arc type of the
564 566
    /// destination digraph.
565 567
    template <typename ArcRef>
566 568
    DigraphCopy& arcRef(ArcRef& map) {
567 569
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
568 570
                          ArcRefMap, ArcRef>(map));
569 571
      return *this;
570 572
    }
571 573

	
572 574
    /// \brief Copy the arc cross references into the given map.
573 575
    ///
574 576
    /// This function copies the arc cross references (reverse references)
575 577
    /// into the given map. The parameter should be a map, whose key type
576 578
    /// is the Arc type of the destination digraph, while the value type is
577 579
    /// the Arc type of the source digraph.
578 580
    template <typename ArcCrossRef>
579 581
    DigraphCopy& arcCrossRef(ArcCrossRef& map) {
580 582
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
581 583
                          ArcRefMap, ArcCrossRef>(map));
582 584
      return *this;
583 585
    }
584 586

	
585 587
    /// \brief Make a copy of the given arc map.
586 588
    ///
587 589
    /// This function makes a copy of the given arc map for the newly
588 590
    /// created digraph.
589 591
    /// The key type of the new map \c tmap should be the Arc type of the
590 592
    /// destination digraph, and the key type of the original map \c map
591 593
    /// should be the Arc type of the source digraph.
592 594
    template <typename FromMap, typename ToMap>
593 595
    DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
594 596
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
595 597
                          ArcRefMap, FromMap, ToMap>(map, tmap));
596 598
      return *this;
597 599
    }
598 600

	
599 601
    /// \brief Make a copy of the given arc.
600 602
    ///
601 603
    /// This function makes a copy of the given arc.
602 604
    DigraphCopy& arc(const Arc& arc, TArc& tarc) {
603 605
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
604 606
                          ArcRefMap, TArc>(arc, tarc));
605 607
      return *this;
606 608
    }
607 609

	
608 610
    /// \brief Execute copying.
609 611
    ///
610 612
    /// This function executes the copying of the digraph along with the
611 613
    /// copying of the assigned data.
612 614
    void run() {
613 615
      NodeRefMap nodeRefMap(_from);
614 616
      ArcRefMap arcRefMap(_from);
615 617
      _core_bits::DigraphCopySelector<To>::
616 618
        copy(_from, _to, nodeRefMap, arcRefMap);
617 619
      for (int i = 0; i < int(_node_maps.size()); ++i) {
618 620
        _node_maps[i]->copy(_from, nodeRefMap);
619 621
      }
620 622
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
621 623
        _arc_maps[i]->copy(_from, arcRefMap);
622 624
      }
623 625
    }
624 626

	
625 627
  protected:
626 628

	
627 629
    const From& _from;
628 630
    To& _to;
629 631

	
630 632
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
631 633
      _node_maps;
632 634

	
633 635
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
634 636
      _arc_maps;
635 637

	
636 638
  };
637 639

	
638 640
  /// \brief Copy a digraph to another digraph.
639 641
  ///
640 642
  /// This function copies a digraph to another digraph.
641 643
  /// The complete usage of it is detailed in the DigraphCopy class, but
642 644
  /// a short example shows a basic work:
643 645
  ///\code
644 646
  /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
645 647
  ///\endcode
646 648
  ///
647 649
  /// After the copy the \c nr map will contain the mapping from the
648 650
  /// nodes of the \c from digraph to the nodes of the \c to digraph and
649 651
  /// \c acr will contain the mapping from the arcs of the \c to digraph
650 652
  /// to the arcs of the \c from digraph.
651 653
  ///
652 654
  /// \see DigraphCopy
653 655
  template <typename From, typename To>
654 656
  DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
655 657
    return DigraphCopy<From, To>(from, to);
656 658
  }
657 659

	
658 660
  /// \brief Class to copy a graph.
659 661
  ///
660 662
  /// Class to copy a graph to another graph (duplicate a graph). The
661 663
  /// simplest way of using it is through the \c graphCopy() function.
662 664
  ///
663 665
  /// This class not only make a copy of a graph, but it can create
664 666
  /// references and cross references between the nodes, edges and arcs of
665 667
  /// the two graphs, and it can copy maps for using with the newly created
666 668
  /// graph.
667 669
  ///
668 670
  /// To make a copy from a graph, first an instance of GraphCopy
669 671
  /// should be created, then the data belongs to the graph should
670 672
  /// assigned to copy. In the end, the \c run() member should be
671 673
  /// called.
672 674
  ///
673 675
  /// The next code copies a graph with several data:
674 676
  ///\code
675 677
  ///  GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
676 678
  ///  // Create references for the nodes
677 679
  ///  OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
678 680
  ///  cg.nodeRef(nr);
679 681
  ///  // Create cross references (inverse) for the edges
680 682
  ///  NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
681 683
  ///  cg.edgeCrossRef(ecr);
682 684
  ///  // Copy an edge map
683 685
  ///  OrigGraph::EdgeMap<double> oemap(orig_graph);
684 686
  ///  NewGraph::EdgeMap<double> nemap(new_graph);
685 687
  ///  cg.edgeMap(oemap, nemap);
686 688
  ///  // Copy a node
687 689
  ///  OrigGraph::Node on;
688 690
  ///  NewGraph::Node nn;
689 691
  ///  cg.node(on, nn);
690 692
  ///  // Execute copying
691 693
  ///  cg.run();
692 694
  ///\endcode
693 695
  template <typename From, typename To>
694 696
  class GraphCopy {
695 697
  private:
696 698

	
697 699
    typedef typename From::Node Node;
698 700
    typedef typename From::NodeIt NodeIt;
699 701
    typedef typename From::Arc Arc;
700 702
    typedef typename From::ArcIt ArcIt;
701 703
    typedef typename From::Edge Edge;
702 704
    typedef typename From::EdgeIt EdgeIt;
703 705

	
704 706
    typedef typename To::Node TNode;
705 707
    typedef typename To::Arc TArc;
706 708
    typedef typename To::Edge TEdge;
707 709

	
708 710
    typedef typename From::template NodeMap<TNode> NodeRefMap;
709 711
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
710 712

	
711 713
    struct ArcRefMap {
712 714
      ArcRefMap(const From& from, const To& to,
713 715
                const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
714 716
        : _from(from), _to(to),
715 717
          _edge_ref(edge_ref), _node_ref(node_ref) {}
716 718

	
717 719
      typedef typename From::Arc Key;
718 720
      typedef typename To::Arc Value;
719 721

	
720 722
      Value operator[](const Key& key) const {
721 723
        bool forward = _from.u(key) != _from.v(key) ?
722 724
          _node_ref[_from.source(key)] ==
723 725
          _to.source(_to.direct(_edge_ref[key], true)) :
724 726
          _from.direction(key);
725 727
        return _to.direct(_edge_ref[key], forward);
726 728
      }
727 729

	
728 730
      const From& _from;
729 731
      const To& _to;
730 732
      const EdgeRefMap& _edge_ref;
731 733
      const NodeRefMap& _node_ref;
732 734
    };
733 735

	
734 736
  public:
735 737

	
736 738
    /// \brief Constructor of GraphCopy.
737 739
    ///
738 740
    /// Constructor of GraphCopy for copying the content of the
739 741
    /// \c from graph into the \c to graph.
740 742
    GraphCopy(const From& from, To& to)
741 743
      : _from(from), _to(to) {}
742 744

	
743 745
    /// \brief Destructor of GraphCopy
744 746
    ///
745 747
    /// Destructor of GraphCopy.
746 748
    ~GraphCopy() {
747 749
      for (int i = 0; i < int(_node_maps.size()); ++i) {
748 750
        delete _node_maps[i];
749 751
      }
750 752
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
751 753
        delete _arc_maps[i];
752 754
      }
753 755
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
754 756
        delete _edge_maps[i];
755 757
      }
756 758
    }
757 759

	
758 760
    /// \brief Copy the node references into the given map.
759 761
    ///
760 762
    /// This function copies the node references into the given map.
761 763
    /// The parameter should be a map, whose key type is the Node type of
762 764
    /// the source graph, while the value type is the Node type of the
763 765
    /// destination graph.
764 766
    template <typename NodeRef>
765 767
    GraphCopy& nodeRef(NodeRef& map) {
766 768
      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
767 769
                           NodeRefMap, NodeRef>(map));
768 770
      return *this;
769 771
    }
770 772

	
771 773
    /// \brief Copy the node cross references into the given map.
772 774
    ///
773 775
    /// This function copies the node cross references (reverse references)
774 776
    /// into the given map. The parameter should be a map, whose key type
775 777
    /// is the Node type of the destination graph, while the value type is
776 778
    /// the Node type of the source graph.
777 779
    template <typename NodeCrossRef>
778 780
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
779 781
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
780 782
                           NodeRefMap, NodeCrossRef>(map));
781 783
      return *this;
782 784
    }
783 785

	
784 786
    /// \brief Make a copy of the given node map.
785 787
    ///
786 788
    /// This function makes a copy of the given node map for the newly
787 789
    /// created graph.
788 790
    /// The key type of the new map \c tmap should be the Node type of the
789 791
    /// destination graph, and the key type of the original map \c map
790 792
    /// should be the Node type of the source graph.
791 793
    template <typename FromMap, typename ToMap>
792 794
    GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
793 795
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
794 796
                           NodeRefMap, FromMap, ToMap>(map, tmap));
795 797
      return *this;
796 798
    }
797 799

	
798 800
    /// \brief Make a copy of the given node.
799 801
    ///
800 802
    /// This function makes a copy of the given node.
801 803
    GraphCopy& node(const Node& node, TNode& tnode) {
802 804
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
803 805
                           NodeRefMap, TNode>(node, tnode));
804 806
      return *this;
805 807
    }
806 808

	
807 809
    /// \brief Copy the arc references into the given map.
808 810
    ///
809 811
    /// This function copies the arc references into the given map.
810 812
    /// The parameter should be a map, whose key type is the Arc type of
811 813
    /// the source graph, while the value type is the Arc type of the
812 814
    /// destination graph.
813 815
    template <typename ArcRef>
814 816
    GraphCopy& arcRef(ArcRef& map) {
815 817
      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
816 818
                          ArcRefMap, ArcRef>(map));
817 819
      return *this;
818 820
    }
819 821

	
820 822
    /// \brief Copy the arc cross references into the given map.
821 823
    ///
822 824
    /// This function copies the arc cross references (reverse references)
823 825
    /// into the given map. The parameter should be a map, whose key type
824 826
    /// is the Arc type of the destination graph, while the value type is
825 827
    /// the Arc type of the source graph.
826 828
    template <typename ArcCrossRef>
827 829
    GraphCopy& arcCrossRef(ArcCrossRef& map) {
828 830
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
829 831
                          ArcRefMap, ArcCrossRef>(map));
830 832
      return *this;
831 833
    }
832 834

	
833 835
    /// \brief Make a copy of the given arc map.
834 836
    ///
835 837
    /// This function makes a copy of the given arc map for the newly
836 838
    /// created graph.
837 839
    /// The key type of the new map \c tmap should be the Arc type of the
838 840
    /// destination graph, and the key type of the original map \c map
839 841
    /// should be the Arc type of the source graph.
840 842
    template <typename FromMap, typename ToMap>
841 843
    GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
842 844
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
843 845
                          ArcRefMap, FromMap, ToMap>(map, tmap));
844 846
      return *this;
845 847
    }
846 848

	
847 849
    /// \brief Make a copy of the given arc.
848 850
    ///
849 851
    /// This function makes a copy of the given arc.
850 852
    GraphCopy& arc(const Arc& arc, TArc& tarc) {
851 853
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
852 854
                          ArcRefMap, TArc>(arc, tarc));
853 855
      return *this;
854 856
    }
855 857

	
856 858
    /// \brief Copy the edge references into the given map.
857 859
    ///
858 860
    /// This function copies the edge references into the given map.
859 861
    /// The parameter should be a map, whose key type is the Edge type of
860 862
    /// the source graph, while the value type is the Edge type of the
861 863
    /// destination graph.
862 864
    template <typename EdgeRef>
863 865
    GraphCopy& edgeRef(EdgeRef& map) {
864 866
      _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
865 867
                           EdgeRefMap, EdgeRef>(map));
866 868
      return *this;
867 869
    }
868 870

	
869 871
    /// \brief Copy the edge cross references into the given map.
870 872
    ///
871 873
    /// This function copies the edge cross references (reverse references)
872 874
    /// into the given map. The parameter should be a map, whose key type
873 875
    /// is the Edge type of the destination graph, while the value type is
874 876
    /// the Edge type of the source graph.
875 877
    template <typename EdgeCrossRef>
876 878
    GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
877 879
      _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
878 880
                           Edge, EdgeRefMap, EdgeCrossRef>(map));
879 881
      return *this;
880 882
    }
881 883

	
882 884
    /// \brief Make a copy of the given edge map.
883 885
    ///
884 886
    /// This function makes a copy of the given edge map for the newly
885 887
    /// created graph.
886 888
    /// The key type of the new map \c tmap should be the Edge type of the
887 889
    /// destination graph, and the key type of the original map \c map
888 890
    /// should be the Edge type of the source graph.
889 891
    template <typename FromMap, typename ToMap>
890 892
    GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
891 893
      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
892 894
                           EdgeRefMap, FromMap, ToMap>(map, tmap));
893 895
      return *this;
894 896
    }
895 897

	
896 898
    /// \brief Make a copy of the given edge.
897 899
    ///
898 900
    /// This function makes a copy of the given edge.
899 901
    GraphCopy& edge(const Edge& edge, TEdge& tedge) {
900 902
      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
901 903
                           EdgeRefMap, TEdge>(edge, tedge));
902 904
      return *this;
903 905
    }
904 906

	
905 907
    /// \brief Execute copying.
906 908
    ///
907 909
    /// This function executes the copying of the graph along with the
908 910
    /// copying of the assigned data.
909 911
    void run() {
910 912
      NodeRefMap nodeRefMap(_from);
911 913
      EdgeRefMap edgeRefMap(_from);
912 914
      ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
913 915
      _core_bits::GraphCopySelector<To>::
914 916
        copy(_from, _to, nodeRefMap, edgeRefMap);
915 917
      for (int i = 0; i < int(_node_maps.size()); ++i) {
916 918
        _node_maps[i]->copy(_from, nodeRefMap);
917 919
      }
918 920
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
919 921
        _edge_maps[i]->copy(_from, edgeRefMap);
920 922
      }
921 923
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
922 924
        _arc_maps[i]->copy(_from, arcRefMap);
923 925
      }
924 926
    }
925 927

	
926 928
  private:
927 929

	
928 930
    const From& _from;
929 931
    To& _to;
930 932

	
931 933
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
932 934
      _node_maps;
933 935

	
934 936
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
935 937
      _arc_maps;
936 938

	
937 939
    std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
938 940
      _edge_maps;
939 941

	
940 942
  };
941 943

	
942 944
  /// \brief Copy a graph to another graph.
943 945
  ///
944 946
  /// This function copies a graph to another graph.
945 947
  /// The complete usage of it is detailed in the GraphCopy class,
946 948
  /// but a short example shows a basic work:
947 949
  ///\code
948 950
  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
949 951
  ///\endcode
950 952
  ///
951 953
  /// After the copy the \c nr map will contain the mapping from the
952 954
  /// nodes of the \c from graph to the nodes of the \c to graph and
953 955
  /// \c ecr will contain the mapping from the edges of the \c to graph
954 956
  /// to the edges of the \c from graph.
955 957
  ///
956 958
  /// \see GraphCopy
957 959
  template <typename From, typename To>
958 960
  GraphCopy<From, To>
959 961
  graphCopy(const From& from, To& to) {
960 962
    return GraphCopy<From, To>(from, to);
961 963
  }
962 964

	
963 965
  namespace _core_bits {
964 966

	
965 967
    template <typename Graph, typename Enable = void>
966 968
    struct FindArcSelector {
967 969
      typedef typename Graph::Node Node;
968 970
      typedef typename Graph::Arc Arc;
969 971
      static Arc find(const Graph &g, Node u, Node v, Arc e) {
970 972
        if (e == INVALID) {
971 973
          g.firstOut(e, u);
972 974
        } else {
973 975
          g.nextOut(e);
974 976
        }
975 977
        while (e != INVALID && g.target(e) != v) {
976 978
          g.nextOut(e);
977 979
        }
978 980
        return e;
979 981
      }
980 982
    };
981 983

	
982 984
    template <typename Graph>
983 985
    struct FindArcSelector<
984 986
      Graph,
985 987
      typename enable_if<typename Graph::FindArcTag, void>::type>
986 988
    {
987 989
      typedef typename Graph::Node Node;
988 990
      typedef typename Graph::Arc Arc;
989 991
      static Arc find(const Graph &g, Node u, Node v, Arc prev) {
990 992
        return g.findArc(u, v, prev);
991 993
      }
992 994
    };
993 995
  }
994 996

	
995 997
  /// \brief Find an arc between two nodes of a digraph.
996 998
  ///
997 999
  /// This function finds an arc from node \c u to node \c v in the
998 1000
  /// digraph \c g.
999 1001
  ///
1000 1002
  /// If \c prev is \ref INVALID (this is the default value), then
1001 1003
  /// it finds the first arc from \c u to \c v. Otherwise it looks for
1002 1004
  /// the next arc from \c u to \c v after \c prev.
1003 1005
  /// \return The found arc or \ref INVALID if there is no such an arc.
1004 1006
  ///
1005 1007
  /// Thus you can iterate through each arc from \c u to \c v as it follows.
1006 1008
  ///\code
1007 1009
  /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1008 1010
  ///   ...
1009 1011
  /// }
1010 1012
  ///\endcode
1011 1013
  ///
1012 1014
  /// \note \ref ConArcIt provides iterator interface for the same
1013 1015
  /// functionality.
1014 1016
  ///
1015 1017
  ///\sa ConArcIt
1016 1018
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1017 1019
  template <typename Graph>
1018 1020
  inline typename Graph::Arc
1019 1021
  findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1020 1022
          typename Graph::Arc prev = INVALID) {
1021 1023
    return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1022 1024
  }
1023 1025

	
1024 1026
  /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1025 1027
  ///
1026 1028
  /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1027 1029
  /// a higher level interface for the \ref findArc() function. You can
1028 1030
  /// use it the following way:
1029 1031
  ///\code
1030 1032
  /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1031 1033
  ///   ...
1032 1034
  /// }
1033 1035
  ///\endcode
1034 1036
  ///
1035 1037
  ///\sa findArc()
1036 1038
  ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1037 1039
  template <typename _Graph>
1038 1040
  class ConArcIt : public _Graph::Arc {
1039 1041
  public:
1040 1042

	
1041 1043
    typedef _Graph Graph;
1042 1044
    typedef typename Graph::Arc Parent;
1043 1045

	
1044 1046
    typedef typename Graph::Arc Arc;
1045 1047
    typedef typename Graph::Node Node;
1046 1048

	
1047 1049
    /// \brief Constructor.
1048 1050
    ///
1049 1051
    /// Construct a new ConArcIt iterating on the arcs that
1050 1052
    /// connects nodes \c u and \c v.
1051 1053
    ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
1052 1054
      Parent::operator=(findArc(_graph, u, v));
1053 1055
    }
1054 1056

	
1055 1057
    /// \brief Constructor.
1056 1058
    ///
1057 1059
    /// Construct a new ConArcIt that continues the iterating from arc \c a.
1058 1060
    ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
1059 1061

	
1060 1062
    /// \brief Increment operator.
1061 1063
    ///
1062 1064
    /// It increments the iterator and gives back the next arc.
1063 1065
    ConArcIt& operator++() {
1064 1066
      Parent::operator=(findArc(_graph, _graph.source(*this),
1065 1067
                                _graph.target(*this), *this));
1066 1068
      return *this;
1067 1069
    }
1068 1070
  private:
1069 1071
    const Graph& _graph;
1070 1072
  };
1071 1073

	
1072 1074
  namespace _core_bits {
1073 1075

	
1074 1076
    template <typename Graph, typename Enable = void>
1075 1077
    struct FindEdgeSelector {
1076 1078
      typedef typename Graph::Node Node;
1077 1079
      typedef typename Graph::Edge Edge;
1078 1080
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
1079 1081
        bool b;
1080 1082
        if (u != v) {
1081 1083
          if (e == INVALID) {
1082 1084
            g.firstInc(e, b, u);
1083 1085
          } else {
1084 1086
            b = g.u(e) == u;
1085 1087
            g.nextInc(e, b);
1086 1088
          }
1087 1089
          while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1088 1090
            g.nextInc(e, b);
1089 1091
          }
1090 1092
        } else {
1091 1093
          if (e == INVALID) {
1092 1094
            g.firstInc(e, b, u);
1093 1095
          } else {
1094 1096
            b = true;
1095 1097
            g.nextInc(e, b);
1096 1098
          }
1097 1099
          while (e != INVALID && (!b || g.v(e) != v)) {
1098 1100
            g.nextInc(e, b);
1099 1101
          }
1100 1102
        }
1101 1103
        return e;
1102 1104
      }
1103 1105
    };
1104 1106

	
1105 1107
    template <typename Graph>
1106 1108
    struct FindEdgeSelector<
1107 1109
      Graph,
1108 1110
      typename enable_if<typename Graph::FindEdgeTag, void>::type>
1109 1111
    {
1110 1112
      typedef typename Graph::Node Node;
1111 1113
      typedef typename Graph::Edge Edge;
1112 1114
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1113 1115
        return g.findEdge(u, v, prev);
1114 1116
      }
1115 1117
    };
1116 1118
  }
1117 1119

	
1118 1120
  /// \brief Find an edge between two nodes of a graph.
1119 1121
  ///
1120 1122
  /// This function finds an edge from node \c u to node \c v in graph \c g.
1121 1123
  /// If node \c u and node \c v is equal then each loop edge
1122 1124
  /// will be enumerated once.
1123 1125
  ///
1124 1126
  /// If \c prev is \ref INVALID (this is the default value), then
1125 1127
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
1126 1128
  /// the next edge from \c u to \c v after \c prev.
1127 1129
  /// \return The found edge or \ref INVALID if there is no such an edge.
1128 1130
  ///
1129 1131
  /// Thus you can iterate through each edge between \c u and \c v
1130 1132
  /// as it follows.
1131 1133
  ///\code
1132 1134
  /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1133 1135
  ///   ...
1134 1136
  /// }
1135 1137
  ///\endcode
1136 1138
  ///
1137 1139
  /// \note \ref ConEdgeIt provides iterator interface for the same
1138 1140
  /// functionality.
1139 1141
  ///
1140 1142
  ///\sa ConEdgeIt
1141 1143
  template <typename Graph>
1142 1144
  inline typename Graph::Edge
1143 1145
  findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1144 1146
            typename Graph::Edge p = INVALID) {
1145 1147
    return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1146 1148
  }
1147 1149

	
1148 1150
  /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1149 1151
  ///
1150 1152
  /// Iterator for iterating on parallel edges connecting the same nodes.
1151 1153
  /// It is a higher level interface for the findEdge() function. You can
1152 1154
  /// use it the following way:
1153 1155
  ///\code
1154 1156
  /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1155 1157
  ///   ...
1156 1158
  /// }
1157 1159
  ///\endcode
1158 1160
  ///
1159 1161
  ///\sa findEdge()
1160 1162
  template <typename _Graph>
1161 1163
  class ConEdgeIt : public _Graph::Edge {
1162 1164
  public:
1163 1165

	
1164 1166
    typedef _Graph Graph;
1165 1167
    typedef typename Graph::Edge Parent;
1166 1168

	
1167 1169
    typedef typename Graph::Edge Edge;
1168 1170
    typedef typename Graph::Node Node;
1169 1171

	
1170 1172
    /// \brief Constructor.
1171 1173
    ///
1172 1174
    /// Construct a new ConEdgeIt iterating on the edges that
1173 1175
    /// connects nodes \c u and \c v.
1174 1176
    ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1175 1177
      Parent::operator=(findEdge(_graph, _u, _v));
1176 1178
    }
1177 1179

	
1178 1180
    /// \brief Constructor.
1179 1181
    ///
1180 1182
    /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1181 1183
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
1182 1184

	
1183 1185
    /// \brief Increment operator.
1184 1186
    ///
1185 1187
    /// It increments the iterator and gives back the next edge.
1186 1188
    ConEdgeIt& operator++() {
1187 1189
      Parent::operator=(findEdge(_graph, _u, _v, *this));
1188 1190
      return *this;
1189 1191
    }
1190 1192
  private:
1191 1193
    const Graph& _graph;
1192 1194
    Node _u, _v;
1193 1195
  };
1194 1196

	
1195 1197

	
1196 1198
  ///Dynamic arc look-up between given endpoints.
1197 1199

	
1198 1200
  ///Using this class, you can find an arc in a digraph from a given
1199 1201
  ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1200 1202
  ///where <em>d</em> is the out-degree of the source node.
1201 1203
  ///
1202 1204
  ///It is possible to find \e all parallel arcs between two nodes with
1203 1205
  ///the \c operator() member.
1204 1206
  ///
1205 1207
  ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1206 1208
  ///\ref AllArcLookUp if your digraph is not changed so frequently.
1207 1209
  ///
1208 1210
  ///This class uses a self-adjusting binary search tree, the Splay tree
1209 1211
  ///of Sleator and Tarjan to guarantee the logarithmic amortized
1210 1212
  ///time bound for arc look-ups. This class also guarantees the
1211 1213
  ///optimal time bound in a constant factor for any distribution of
1212 1214
  ///queries.
1213 1215
  ///
1214 1216
  ///\tparam G The type of the underlying digraph.
1215 1217
  ///
1216 1218
  ///\sa ArcLookUp
1217 1219
  ///\sa AllArcLookUp
1218 1220
  template<class G>
1219 1221
  class DynArcLookUp
1220 1222
    : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
1221 1223
  {
1222 1224
  public:
1223 1225
    typedef typename ItemSetTraits<G, typename G::Arc>
1224 1226
    ::ItemNotifier::ObserverBase Parent;
1225 1227

	
1226 1228
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1227 1229
    typedef G Digraph;
1228 1230

	
1229 1231
  protected:
1230 1232

	
1231 1233
    class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type {
1232 1234
    public:
1233 1235

	
1234 1236
      typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent;
1235 1237

	
1236 1238
      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
1237 1239

	
1238 1240
      virtual void add(const Node& node) {
1239 1241
        Parent::add(node);
1240 1242
        Parent::set(node, INVALID);
1241 1243
      }
1242 1244

	
1243 1245
      virtual void add(const std::vector<Node>& nodes) {
1244 1246
        Parent::add(nodes);
1245 1247
        for (int i = 0; i < int(nodes.size()); ++i) {
1246 1248
          Parent::set(nodes[i], INVALID);
1247 1249
        }
1248 1250
      }
1249 1251

	
1250 1252
      virtual void build() {
1251 1253
        Parent::build();
1252 1254
        Node it;
1253 1255
        typename Parent::Notifier* nf = Parent::notifier();
1254 1256
        for (nf->first(it); it != INVALID; nf->next(it)) {
1255 1257
          Parent::set(it, INVALID);
1256 1258
        }
1257 1259
      }
1258 1260
    };
1259 1261

	
1260 1262
    const Digraph &_g;
1261 1263
    AutoNodeMap _head;
1262 1264
    typename Digraph::template ArcMap<Arc> _parent;
1263 1265
    typename Digraph::template ArcMap<Arc> _left;
1264 1266
    typename Digraph::template ArcMap<Arc> _right;
1265 1267

	
1266 1268
    class ArcLess {
1267 1269
      const Digraph &g;
1268 1270
    public:
1269 1271
      ArcLess(const Digraph &_g) : g(_g) {}
1270 1272
      bool operator()(Arc a,Arc b) const
1271 1273
      {
1272 1274
        return g.target(a)<g.target(b);
1273 1275
      }
1274 1276
    };
1275 1277

	
1276 1278
  public:
1277 1279

	
1278 1280
    ///Constructor
1279 1281

	
1280 1282
    ///Constructor.
1281 1283
    ///
1282 1284
    ///It builds up the search database.
1283 1285
    DynArcLookUp(const Digraph &g)
1284 1286
      : _g(g),_head(g),_parent(g),_left(g),_right(g)
1285 1287
    {
1286 1288
      Parent::attach(_g.notifier(typename Digraph::Arc()));
1287 1289
      refresh();
1288 1290
    }
1289 1291

	
1290 1292
  protected:
1291 1293

	
1292 1294
    virtual void add(const Arc& arc) {
1293 1295
      insert(arc);
1294 1296
    }
1295 1297

	
1296 1298
    virtual void add(const std::vector<Arc>& arcs) {
1297 1299
      for (int i = 0; i < int(arcs.size()); ++i) {
1298 1300
        insert(arcs[i]);
1299 1301
      }
1300 1302
    }
1301 1303

	
1302 1304
    virtual void erase(const Arc& arc) {
1303 1305
      remove(arc);
1304 1306
    }
1305 1307

	
1306 1308
    virtual void erase(const std::vector<Arc>& arcs) {
1307 1309
      for (int i = 0; i < int(arcs.size()); ++i) {
1308 1310
        remove(arcs[i]);
1309 1311
      }
1310 1312
    }
1311 1313

	
1312 1314
    virtual void build() {
1313 1315
      refresh();
1314 1316
    }
1315 1317

	
1316 1318
    virtual void clear() {
1317 1319
      for(NodeIt n(_g);n!=INVALID;++n) {
1318 1320
        _head.set(n, INVALID);
1319 1321
      }
1320 1322
    }
1321 1323

	
1322 1324
    void insert(Arc arc) {
1323 1325
      Node s = _g.source(arc);
1324 1326
      Node t = _g.target(arc);
1325 1327
      _left.set(arc, INVALID);
1326 1328
      _right.set(arc, INVALID);
1327 1329

	
1328 1330
      Arc e = _head[s];
1329 1331
      if (e == INVALID) {
1330 1332
        _head.set(s, arc);
1331 1333
        _parent.set(arc, INVALID);
1332 1334
        return;
1333 1335
      }
1334 1336
      while (true) {
1335 1337
        if (t < _g.target(e)) {
1336 1338
          if (_left[e] == INVALID) {
1337 1339
            _left.set(e, arc);
1338 1340
            _parent.set(arc, e);
1339 1341
            splay(arc);
1340 1342
            return;
1341 1343
          } else {
1342 1344
            e = _left[e];
1343 1345
          }
1344 1346
        } else {
1345 1347
          if (_right[e] == INVALID) {
1346 1348
            _right.set(e, arc);
1347 1349
            _parent.set(arc, e);
1348 1350
            splay(arc);
1349 1351
            return;
1350 1352
          } else {
1351 1353
            e = _right[e];
1352 1354
          }
1353 1355
        }
1354 1356
      }
1355 1357
    }
1356 1358

	
1357 1359
    void remove(Arc arc) {
1358 1360
      if (_left[arc] == INVALID) {
1359 1361
        if (_right[arc] != INVALID) {
1360 1362
          _parent.set(_right[arc], _parent[arc]);
1361 1363
        }
1362 1364
        if (_parent[arc] != INVALID) {
1363 1365
          if (_left[_parent[arc]] == arc) {
1364 1366
            _left.set(_parent[arc], _right[arc]);
1365 1367
          } else {
1366 1368
            _right.set(_parent[arc], _right[arc]);
1367 1369
          }
1368 1370
        } else {
1369 1371
          _head.set(_g.source(arc), _right[arc]);
1370 1372
        }
1371 1373
      } else if (_right[arc] == INVALID) {
1372 1374
        _parent.set(_left[arc], _parent[arc]);
1373 1375
        if (_parent[arc] != INVALID) {
1374 1376
          if (_left[_parent[arc]] == arc) {
1375 1377
            _left.set(_parent[arc], _left[arc]);
1376 1378
          } else {
1377 1379
            _right.set(_parent[arc], _left[arc]);
1378 1380
          }
1379 1381
        } else {
1380 1382
          _head.set(_g.source(arc), _left[arc]);
1381 1383
        }
1382 1384
      } else {
1383 1385
        Arc e = _left[arc];
1384 1386
        if (_right[e] != INVALID) {
1385 1387
          e = _right[e];
1386 1388
          while (_right[e] != INVALID) {
1387 1389
            e = _right[e];
1388 1390
          }
1389 1391
          Arc s = _parent[e];
1390 1392
          _right.set(_parent[e], _left[e]);
1391 1393
          if (_left[e] != INVALID) {
1392 1394
            _parent.set(_left[e], _parent[e]);
1393 1395
          }
1394 1396

	
1395 1397
          _left.set(e, _left[arc]);
1396 1398
          _parent.set(_left[arc], e);
1397 1399
          _right.set(e, _right[arc]);
1398 1400
          _parent.set(_right[arc], e);
1399 1401

	
1400 1402
          _parent.set(e, _parent[arc]);
1401 1403
          if (_parent[arc] != INVALID) {
1402 1404
            if (_left[_parent[arc]] == arc) {
1403 1405
              _left.set(_parent[arc], e);
1404 1406
            } else {
1405 1407
              _right.set(_parent[arc], e);
1406 1408
            }
1407 1409
          }
1408 1410
          splay(s);
1409 1411
        } else {
1410 1412
          _right.set(e, _right[arc]);
1411 1413
          _parent.set(_right[arc], e);
1412 1414
          _parent.set(e, _parent[arc]);
1413 1415

	
1414 1416
          if (_parent[arc] != INVALID) {
1415 1417
            if (_left[_parent[arc]] == arc) {
1416 1418
              _left.set(_parent[arc], e);
1417 1419
            } else {
1418 1420
              _right.set(_parent[arc], e);
1419 1421
            }
1420 1422
          } else {
1421 1423
            _head.set(_g.source(arc), e);
1422 1424
          }
1423 1425
        }
1424 1426
      }
1425 1427
    }
1426 1428

	
1427 1429
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1428 1430
    {
1429 1431
      int m=(a+b)/2;
1430 1432
      Arc me=v[m];
1431 1433
      if (a < m) {
1432 1434
        Arc left = refreshRec(v,a,m-1);
1433 1435
        _left.set(me, left);
1434 1436
        _parent.set(left, me);
1435 1437
      } else {
1436 1438
        _left.set(me, INVALID);
1437 1439
      }
1438 1440
      if (m < b) {
1439 1441
        Arc right = refreshRec(v,m+1,b);
1440 1442
        _right.set(me, right);
1441 1443
        _parent.set(right, me);
1442 1444
      } else {
1443 1445
        _right.set(me, INVALID);
1444 1446
      }
1445 1447
      return me;
1446 1448
    }
1447 1449

	
1448 1450
    void refresh() {
1449 1451
      for(NodeIt n(_g);n!=INVALID;++n) {
1450 1452
        std::vector<Arc> v;
1451 1453
        for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1452 1454
        if (!v.empty()) {
1453 1455
          std::sort(v.begin(),v.end(),ArcLess(_g));
1454 1456
          Arc head = refreshRec(v,0,v.size()-1);
1455 1457
          _head.set(n, head);
1456 1458
          _parent.set(head, INVALID);
1457 1459
        }
1458 1460
        else _head.set(n, INVALID);
1459 1461
      }
1460 1462
    }
1461 1463

	
1462 1464
    void zig(Arc v) {
1463 1465
      Arc w = _parent[v];
1464 1466
      _parent.set(v, _parent[w]);
1465 1467
      _parent.set(w, v);
1466 1468
      _left.set(w, _right[v]);
1467 1469
      _right.set(v, w);
1468 1470
      if (_parent[v] != INVALID) {
1469 1471
        if (_right[_parent[v]] == w) {
1470 1472
          _right.set(_parent[v], v);
1471 1473
        } else {
1472 1474
          _left.set(_parent[v], v);
1473 1475
        }
1474 1476
      }
1475 1477
      if (_left[w] != INVALID){
1476 1478
        _parent.set(_left[w], w);
1477 1479
      }
1478 1480
    }
1479 1481

	
1480 1482
    void zag(Arc v) {
1481 1483
      Arc w = _parent[v];
1482 1484
      _parent.set(v, _parent[w]);
1483 1485
      _parent.set(w, v);
1484 1486
      _right.set(w, _left[v]);
1485 1487
      _left.set(v, w);
1486 1488
      if (_parent[v] != INVALID){
1487 1489
        if (_left[_parent[v]] == w) {
1488 1490
          _left.set(_parent[v], v);
1489 1491
        } else {
1490 1492
          _right.set(_parent[v], v);
1491 1493
        }
1492 1494
      }
1493 1495
      if (_right[w] != INVALID){
1494 1496
        _parent.set(_right[w], w);
1495 1497
      }
1496 1498
    }
1497 1499

	
1498 1500
    void splay(Arc v) {
1499 1501
      while (_parent[v] != INVALID) {
1500 1502
        if (v == _left[_parent[v]]) {
1501 1503
          if (_parent[_parent[v]] == INVALID) {
1502 1504
            zig(v);
1503 1505
          } else {
1504 1506
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1505 1507
              zig(_parent[v]);
1506 1508
              zig(v);
1507 1509
            } else {
1508 1510
              zig(v);
1509 1511
              zag(v);
1510 1512
            }
1511 1513
          }
1512 1514
        } else {
1513 1515
          if (_parent[_parent[v]] == INVALID) {
1514 1516
            zag(v);
1515 1517
          } else {
1516 1518
            if (_parent[v] == _left[_parent[_parent[v]]]) {
1517 1519
              zag(v);
1518 1520
              zig(v);
1519 1521
            } else {
1520 1522
              zag(_parent[v]);
1521 1523
              zag(v);
1522 1524
            }
1523 1525
          }
1524 1526
        }
1525 1527
      }
1526 1528
      _head[_g.source(v)] = v;
1527 1529
    }
1528 1530

	
1529 1531

	
1530 1532
  public:
1531 1533

	
1532 1534
    ///Find an arc between two nodes.
1533 1535

	
1534 1536
    ///Find an arc between two nodes.
1535 1537
    ///\param s The source node.
1536 1538
    ///\param t The target node.
1537 1539
    ///\param p The previous arc between \c s and \c t. It it is INVALID or
1538 1540
    ///not given, the operator finds the first appropriate arc.
1539 1541
    ///\return An arc from \c s to \c t after \c p or
1540 1542
    ///\ref INVALID if there is no more.
1541 1543
    ///
1542 1544
    ///For example, you can count the number of arcs from \c u to \c v in the
1543 1545
    ///following way.
1544 1546
    ///\code
1545 1547
    ///DynArcLookUp<ListDigraph> ae(g);
1546 1548
    ///...
1547 1549
    ///int n = 0;
1548 1550
    ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1549 1551
    ///\endcode
1550 1552
    ///
1551 1553
    ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1552 1554
    ///amortized time, specifically, the time complexity of the lookups
1553 1555
    ///is equal to the optimal search tree implementation for the
1554 1556
    ///current query distribution in a constant factor.
1555 1557
    ///
1556 1558
    ///\note This is a dynamic data structure, therefore the data
1557 1559
    ///structure is updated after each graph alteration. Thus although
1558 1560
    ///this data structure is theoretically faster than \ref ArcLookUp
1559 1561
    ///and \ref AllArcLookUp, it often provides worse performance than
1560 1562
    ///them.
1561 1563
    Arc operator()(Node s, Node t, Arc p = INVALID) const  {
1562 1564
      if (p == INVALID) {
1563 1565
        Arc a = _head[s];
1564 1566
        if (a == INVALID) return INVALID;
1565 1567
        Arc r = INVALID;
1566 1568
        while (true) {
1567 1569
          if (_g.target(a) < t) {
1568 1570
            if (_right[a] == INVALID) {
1569 1571
              const_cast<DynArcLookUp&>(*this).splay(a);
1570 1572
              return r;
1571 1573
            } else {
1572 1574
              a = _right[a];
1573 1575
            }
1574 1576
          } else {
1575 1577
            if (_g.target(a) == t) {
1576 1578
              r = a;
1577 1579
            }
1578 1580
            if (_left[a] == INVALID) {
1579 1581
              const_cast<DynArcLookUp&>(*this).splay(a);
1580 1582
              return r;
1581 1583
            } else {
1582 1584
              a = _left[a];
1583 1585
            }
1584 1586
          }
1585 1587
        }
1586 1588
      } else {
1587 1589
        Arc a = p;
1588 1590
        if (_right[a] != INVALID) {
1589 1591
          a = _right[a];
1590 1592
          while (_left[a] != INVALID) {
1591 1593
            a = _left[a];
1592 1594
          }
1593 1595
          const_cast<DynArcLookUp&>(*this).splay(a);
1594 1596
        } else {
1595 1597
          while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
1596 1598
            a = _parent[a];
1597 1599
          }
1598 1600
          if (_parent[a] == INVALID) {
1599 1601
            return INVALID;
1600 1602
          } else {
1601 1603
            a = _parent[a];
1602 1604
            const_cast<DynArcLookUp&>(*this).splay(a);
1603 1605
          }
1604 1606
        }
1605 1607
        if (_g.target(a) == t) return a;
1606 1608
        else return INVALID;
1607 1609
      }
1608 1610
    }
1609 1611

	
1610 1612
  };
1611 1613

	
1612 1614
  ///Fast arc look-up between given endpoints.
1613 1615

	
1614 1616
  ///Using this class, you can find an arc in a digraph from a given
1615 1617
  ///source to a given target in time <em>O</em>(log<em>d</em>),
1616 1618
  ///where <em>d</em> is the out-degree of the source node.
1617 1619
  ///
1618 1620
  ///It is not possible to find \e all parallel arcs between two nodes.
1619 1621
  ///Use \ref AllArcLookUp for this purpose.
1620 1622
  ///
1621 1623
  ///\warning This class is static, so you should call refresh() (or at
1622 1624
  ///least refresh(Node)) to refresh this data structure whenever the
1623 1625
  ///digraph changes. This is a time consuming (superlinearly proportional
1624 1626
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1625 1627
  ///
1626 1628
  ///\tparam G The type of the underlying digraph.
1627 1629
  ///
1628 1630
  ///\sa DynArcLookUp
1629 1631
  ///\sa AllArcLookUp
1630 1632
  template<class G>
1631 1633
  class ArcLookUp
1632 1634
  {
1633 1635
  public:
1634 1636
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1635 1637
    typedef G Digraph;
1636 1638

	
1637 1639
  protected:
1638 1640
    const Digraph &_g;
1639 1641
    typename Digraph::template NodeMap<Arc> _head;
1640 1642
    typename Digraph::template ArcMap<Arc> _left;
1641 1643
    typename Digraph::template ArcMap<Arc> _right;
1642 1644

	
1643 1645
    class ArcLess {
1644 1646
      const Digraph &g;
1645 1647
    public:
1646 1648
      ArcLess(const Digraph &_g) : g(_g) {}
1647 1649
      bool operator()(Arc a,Arc b) const
1648 1650
      {
1649 1651
        return g.target(a)<g.target(b);
1650 1652
      }
1651 1653
    };
1652 1654

	
1653 1655
  public:
1654 1656

	
1655 1657
    ///Constructor
1656 1658

	
1657 1659
    ///Constructor.
1658 1660
    ///
1659 1661
    ///It builds up the search database, which remains valid until the digraph
1660 1662
    ///changes.
1661 1663
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1662 1664

	
1663 1665
  private:
1664 1666
    Arc refreshRec(std::vector<Arc> &v,int a,int b)
1665 1667
    {
1666 1668
      int m=(a+b)/2;
1667 1669
      Arc me=v[m];
1668 1670
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1669 1671
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1670 1672
      return me;
1671 1673
    }
1672 1674
  public:
1673 1675
    ///Refresh the search data structure at a node.
1674 1676

	
1675 1677
    ///Build up the search database of node \c n.
1676 1678
    ///
1677 1679
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1678 1680
    ///is the number of the outgoing arcs of \c n.
1679 1681
    void refresh(Node n)
1680 1682
    {
1681 1683
      std::vector<Arc> v;
1682 1684
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1683 1685
      if(v.size()) {
1684 1686
        std::sort(v.begin(),v.end(),ArcLess(_g));
1685 1687
        _head[n]=refreshRec(v,0,v.size()-1);
1686 1688
      }
1687 1689
      else _head[n]=INVALID;
1688 1690
    }
1689 1691
    ///Refresh the full data structure.
1690 1692

	
1691 1693
    ///Build up the full search database. In fact, it simply calls
1692 1694
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1693 1695
    ///
1694 1696
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1695 1697
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1696 1698
    ///out-degree of the digraph.
1697 1699
    void refresh()
1698 1700
    {
1699 1701
      for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1700 1702
    }
1701 1703

	
1702 1704
    ///Find an arc between two nodes.
1703 1705

	
1704 1706
    ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1705 1707
    ///where <em>d</em> is the number of outgoing arcs of \c s.
1706 1708
    ///\param s The source node.
1707 1709
    ///\param t The target node.
1708 1710
    ///\return An arc from \c s to \c t if there exists,
1709 1711
    ///\ref INVALID otherwise.
1710 1712
    ///
1711 1713
    ///\warning If you change the digraph, refresh() must be called before using
1712 1714
    ///this operator. If you change the outgoing arcs of
1713 1715
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1714 1716
    Arc operator()(Node s, Node t) const
1715 1717
    {
1716 1718
      Arc e;
1717 1719
      for(e=_head[s];
1718 1720
          e!=INVALID&&_g.target(e)!=t;
1719 1721
          e = t < _g.target(e)?_left[e]:_right[e]) ;
1720 1722
      return e;
1721 1723
    }
1722 1724

	
1723 1725
  };
1724 1726

	
1725 1727
  ///Fast look-up of all arcs between given endpoints.
1726 1728

	
1727 1729
  ///This class is the same as \ref ArcLookUp, with the addition
1728 1730
  ///that it makes it possible to find all parallel arcs between given
1729 1731
  ///endpoints.
1730 1732
  ///
1731 1733
  ///\warning This class is static, so you should call refresh() (or at
1732 1734
  ///least refresh(Node)) to refresh this data structure whenever the
1733 1735
  ///digraph changes. This is a time consuming (superlinearly proportional
1734 1736
  ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1735 1737
  ///
1736 1738
  ///\tparam G The type of the underlying digraph.
1737 1739
  ///
1738 1740
  ///\sa DynArcLookUp
1739 1741
  ///\sa ArcLookUp
1740 1742
  template<class G>
1741 1743
  class AllArcLookUp : public ArcLookUp<G>
1742 1744
  {
1743 1745
    using ArcLookUp<G>::_g;
1744 1746
    using ArcLookUp<G>::_right;
1745 1747
    using ArcLookUp<G>::_left;
1746 1748
    using ArcLookUp<G>::_head;
1747 1749

	
1748 1750
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1749 1751
    typedef G Digraph;
1750 1752

	
1751 1753
    typename Digraph::template ArcMap<Arc> _next;
1752 1754

	
1753 1755
    Arc refreshNext(Arc head,Arc next=INVALID)
1754 1756
    {
1755 1757
      if(head==INVALID) return next;
1756 1758
      else {
1757 1759
        next=refreshNext(_right[head],next);
1758 1760
        _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1759 1761
          ? next : INVALID;
1760 1762
        return refreshNext(_left[head],head);
1761 1763
      }
1762 1764
    }
1763 1765

	
1764 1766
    void refreshNext()
1765 1767
    {
1766 1768
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1767 1769
    }
1768 1770

	
1769 1771
  public:
1770 1772
    ///Constructor
1771 1773

	
1772 1774
    ///Constructor.
1773 1775
    ///
1774 1776
    ///It builds up the search database, which remains valid until the digraph
1775 1777
    ///changes.
1776 1778
    AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
1777 1779

	
1778 1780
    ///Refresh the data structure at a node.
1779 1781

	
1780 1782
    ///Build up the search database of node \c n.
1781 1783
    ///
1782 1784
    ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1783 1785
    ///the number of the outgoing arcs of \c n.
1784 1786
    void refresh(Node n)
1785 1787
    {
1786 1788
      ArcLookUp<G>::refresh(n);
1787 1789
      refreshNext(_head[n]);
1788 1790
    }
1789 1791

	
1790 1792
    ///Refresh the full data structure.
1791 1793

	
1792 1794
    ///Build up the full search database. In fact, it simply calls
1793 1795
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
1794 1796
    ///
1795 1797
    ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1796 1798
    ///the number of the arcs in the digraph and <em>D</em> is the maximum
1797 1799
    ///out-degree of the digraph.
1798 1800
    void refresh()
1799 1801
    {
1800 1802
      for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1801 1803
    }
1802 1804

	
1803 1805
    ///Find an arc between two nodes.
1804 1806

	
1805 1807
    ///Find an arc between two nodes.
1806 1808
    ///\param s The source node.
1807 1809
    ///\param t The target node.
1808 1810
    ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1809 1811
    ///not given, the operator finds the first appropriate arc.
1810 1812
    ///\return An arc from \c s to \c t after \c prev or
1811 1813
    ///\ref INVALID if there is no more.
1812 1814
    ///
1813 1815
    ///For example, you can count the number of arcs from \c u to \c v in the
1814 1816
    ///following way.
1815 1817
    ///\code
1816 1818
    ///AllArcLookUp<ListDigraph> ae(g);
1817 1819
    ///...
1818 1820
    ///int n = 0;
1819 1821
    ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1820 1822
    ///\endcode
1821 1823
    ///
1822 1824
    ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1823 1825
    ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1824 1826
    ///consecutive arcs are found in constant time.
1825 1827
    ///
1826 1828
    ///\warning If you change the digraph, refresh() must be called before using
1827 1829
    ///this operator. If you change the outgoing arcs of
1828 1830
    ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1829 1831
    ///
1830 1832
#ifdef DOXYGEN
1831 1833
    Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
1832 1834
#else
1833 1835
    using ArcLookUp<G>::operator() ;
1834 1836
    Arc operator()(Node s, Node t, Arc prev) const
1835 1837
    {
1836 1838
      return prev==INVALID?(*this)(s,t):_next[prev];
1837 1839
    }
1838 1840
#endif
1839 1841

	
1840 1842
  };
1841 1843

	
1842 1844
  /// @}
1843 1845

	
1844 1846
} //namespace lemon
1845 1847

	
1846 1848
#endif
Ignore white space 3072 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
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
#include <lemon/smart_graph.h>
20 20
#include <lemon/list_graph.h>
21 21
#include <lemon/lgf_reader.h>
22 22
#include <lemon/error.h>
23 23

	
24 24
#include "test_tools.h"
25 25

	
26 26
using namespace std;
27 27
using namespace lemon;
28 28

	
29 29
void digraph_copy_test() {
30 30
  const int nn = 10;
31 31

	
32
  // Build a digraph
32 33
  SmartDigraph from;
33 34
  SmartDigraph::NodeMap<int> fnm(from);
34 35
  SmartDigraph::ArcMap<int> fam(from);
35 36
  SmartDigraph::Node fn = INVALID;
36 37
  SmartDigraph::Arc fa = INVALID;
37 38

	
38 39
  std::vector<SmartDigraph::Node> fnv;
39 40
  for (int i = 0; i < nn; ++i) {
40 41
    SmartDigraph::Node node = from.addNode();
41 42
    fnv.push_back(node);
42 43
    fnm[node] = i * i;
43 44
    if (i == 0) fn = node;
44 45
  }
45 46

	
46 47
  for (int i = 0; i < nn; ++i) {
47 48
    for (int j = 0; j < nn; ++j) {
48 49
      SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]);
49 50
      fam[arc] = i + j * j;
50 51
      if (i == 0 && j == 0) fa = arc;
51 52
    }
52 53
  }
53 54

	
55
  // Test digraph copy
54 56
  ListDigraph to;
55 57
  ListDigraph::NodeMap<int> tnm(to);
56 58
  ListDigraph::ArcMap<int> tam(to);
57 59
  ListDigraph::Node tn;
58 60
  ListDigraph::Arc ta;
59 61

	
60 62
  SmartDigraph::NodeMap<ListDigraph::Node> nr(from);
61 63
  SmartDigraph::ArcMap<ListDigraph::Arc> er(from);
62 64

	
63 65
  ListDigraph::NodeMap<SmartDigraph::Node> ncr(to);
64 66
  ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to);
65 67

	
66 68
  digraphCopy(from, to).
67 69
    nodeMap(fnm, tnm).arcMap(fam, tam).
68 70
    nodeRef(nr).arcRef(er).
69 71
    nodeCrossRef(ncr).arcCrossRef(ecr).
70 72
    node(fn, tn).arc(fa, ta).run();
73
  
74
  check(countNodes(from) == countNodes(to), "Wrong copy.");
75
  check(countArcs(from) == countArcs(to), "Wrong copy.");
71 76

	
72 77
  for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
73 78
    check(ncr[nr[it]] == it, "Wrong copy.");
74 79
    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
75 80
  }
76 81

	
77 82
  for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) {
78 83
    check(ecr[er[it]] == it, "Wrong copy.");
79 84
    check(fam[it] == tam[er[it]], "Wrong copy.");
80 85
    check(nr[from.source(it)] == to.source(er[it]), "Wrong copy.");
81 86
    check(nr[from.target(it)] == to.target(er[it]), "Wrong copy.");
82 87
  }
83 88

	
84 89
  for (ListDigraph::NodeIt it(to); it != INVALID; ++it) {
85 90
    check(nr[ncr[it]] == it, "Wrong copy.");
86 91
  }
87 92

	
88 93
  for (ListDigraph::ArcIt it(to); it != INVALID; ++it) {
89 94
    check(er[ecr[it]] == it, "Wrong copy.");
90 95
  }
91 96
  check(tn == nr[fn], "Wrong copy.");
92 97
  check(ta == er[fa], "Wrong copy.");
98

	
99
  // Test repeated copy
100
  digraphCopy(from, to).run();
101
  
102
  check(countNodes(from) == countNodes(to), "Wrong copy.");
103
  check(countArcs(from) == countArcs(to), "Wrong copy.");
93 104
}
94 105

	
95 106
void graph_copy_test() {
96 107
  const int nn = 10;
97 108

	
109
  // Build a graph
98 110
  SmartGraph from;
99 111
  SmartGraph::NodeMap<int> fnm(from);
100 112
  SmartGraph::ArcMap<int> fam(from);
101 113
  SmartGraph::EdgeMap<int> fem(from);
102 114
  SmartGraph::Node fn = INVALID;
103 115
  SmartGraph::Arc fa = INVALID;
104 116
  SmartGraph::Edge fe = INVALID;
105 117

	
106 118
  std::vector<SmartGraph::Node> fnv;
107 119
  for (int i = 0; i < nn; ++i) {
108 120
    SmartGraph::Node node = from.addNode();
109 121
    fnv.push_back(node);
110 122
    fnm[node] = i * i;
111 123
    if (i == 0) fn = node;
112 124
  }
113 125

	
114 126
  for (int i = 0; i < nn; ++i) {
115 127
    for (int j = 0; j < nn; ++j) {
116 128
      SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]);
117 129
      fem[edge] = i * i + j * j;
118 130
      fam[from.direct(edge, true)] = i + j * j;
119 131
      fam[from.direct(edge, false)] = i * i + j;
120 132
      if (i == 0 && j == 0) fa = from.direct(edge, true);
121 133
      if (i == 0 && j == 0) fe = edge;
122 134
    }
123 135
  }
124 136

	
137
  // Test graph copy
125 138
  ListGraph to;
126 139
  ListGraph::NodeMap<int> tnm(to);
127 140
  ListGraph::ArcMap<int> tam(to);
128 141
  ListGraph::EdgeMap<int> tem(to);
129 142
  ListGraph::Node tn;
130 143
  ListGraph::Arc ta;
131 144
  ListGraph::Edge te;
132 145

	
133 146
  SmartGraph::NodeMap<ListGraph::Node> nr(from);
134 147
  SmartGraph::ArcMap<ListGraph::Arc> ar(from);
135 148
  SmartGraph::EdgeMap<ListGraph::Edge> er(from);
136 149

	
137 150
  ListGraph::NodeMap<SmartGraph::Node> ncr(to);
138 151
  ListGraph::ArcMap<SmartGraph::Arc> acr(to);
139 152
  ListGraph::EdgeMap<SmartGraph::Edge> ecr(to);
140 153

	
141 154
  graphCopy(from, to).
142 155
    nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem).
143 156
    nodeRef(nr).arcRef(ar).edgeRef(er).
144 157
    nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr).
145 158
    node(fn, tn).arc(fa, ta).edge(fe, te).run();
146 159

	
160
  check(countNodes(from) == countNodes(to), "Wrong copy.");
161
  check(countEdges(from) == countEdges(to), "Wrong copy.");
162
  check(countArcs(from) == countArcs(to), "Wrong copy.");
163

	
147 164
  for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
148 165
    check(ncr[nr[it]] == it, "Wrong copy.");
149 166
    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
150 167
  }
151 168

	
152 169
  for (SmartGraph::ArcIt it(from); it != INVALID; ++it) {
153 170
    check(acr[ar[it]] == it, "Wrong copy.");
154 171
    check(fam[it] == tam[ar[it]], "Wrong copy.");
155 172
    check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy.");
156 173
    check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy.");
157 174
  }
158 175

	
159 176
  for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) {
160 177
    check(ecr[er[it]] == it, "Wrong copy.");
161 178
    check(fem[it] == tem[er[it]], "Wrong copy.");
162 179
    check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]),
163 180
          "Wrong copy.");
164 181
    check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]),
165 182
          "Wrong copy.");
166 183
    check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])),
167 184
          "Wrong copy.");
168 185
  }
169 186

	
170 187
  for (ListGraph::NodeIt it(to); it != INVALID; ++it) {
171 188
    check(nr[ncr[it]] == it, "Wrong copy.");
172 189
  }
173 190

	
174 191
  for (ListGraph::ArcIt it(to); it != INVALID; ++it) {
175 192
    check(ar[acr[it]] == it, "Wrong copy.");
176 193
  }
177 194
  for (ListGraph::EdgeIt it(to); it != INVALID; ++it) {
178 195
    check(er[ecr[it]] == it, "Wrong copy.");
179 196
  }
180 197
  check(tn == nr[fn], "Wrong copy.");
181 198
  check(ta == ar[fa], "Wrong copy.");
182 199
  check(te == er[fe], "Wrong copy.");
200

	
201
  // Test repeated copy
202
  graphCopy(from, to).run();
203
  
204
  check(countNodes(from) == countNodes(to), "Wrong copy.");
205
  check(countEdges(from) == countEdges(to), "Wrong copy.");
206
  check(countArcs(from) == countArcs(to), "Wrong copy.");
183 207
}
184 208

	
185 209

	
186 210
int main() {
187 211
  digraph_copy_test();
188 212
  graph_copy_test();
189 213

	
190 214
  return 0;
191 215
}
0 comments (0 inline)