gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge bugfix #195
0 1 0
merge 1.0
0 files changed with 4 insertions and 4 deletions:
↑ Collapse diff ↑
Ignore white space 4096 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/bits/enable_if.h>
26 26
#include <lemon/bits/traits.h>
27 27
#include <lemon/assert.h>
28 28

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

	
36 36
namespace lemon {
37 37

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

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

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

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

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

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

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

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

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

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

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

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

	
155 155
  // Node counting:
156 156

	
157 157
  namespace _core_bits {
158 158

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

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

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

	
191 191
  // Arc counting:
192 192

	
193 193
  namespace _core_bits {
194 194

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

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

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

	
227 227
  // Edge counting:
228 228

	
229 229
  namespace _core_bits {
230 230

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

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

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

	
262 262
  }
263 263

	
264 264

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

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

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

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

	
301 301
  namespace _core_bits {
302 302

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
435 435
  }
436 436

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

	
476 476
    typedef typename From::Node Node;
477 477
    typedef typename From::NodeIt NodeIt;
478 478
    typedef typename From::Arc Arc;
479 479
    typedef typename From::ArcIt ArcIt;
480 480

	
481 481
    typedef typename To::Node TNode;
482 482
    typedef typename To::Arc TArc;
483 483

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

	
487 487
  public:
488 488

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

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

	
507 507
    }
508 508

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

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

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

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

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

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

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

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

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

	
624 624
  protected:
625 625

	
626 626
    const From& _from;
627 627
    To& _to;
628 628

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

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

	
635 635
  };
636 636

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

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

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

	
703 703
    typedef typename To::Node TNode;
704 704
    typedef typename To::Arc TArc;
705 705
    typedef typename To::Edge TEdge;
706 706

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

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

	
716 716
      typedef typename From::Arc Key;
717 717
      typedef typename To::Arc Value;
718 718

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

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

	
733 733
  public:
734 734

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
925 925
  private:
926 926

	
927 927
    const From& _from;
928 928
    To& _to;
929 929

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

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

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

	
939 939
  };
940 940

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

	
962 962
  namespace _core_bits {
963 963

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

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

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

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

	
1040 1040
    typedef _Graph Graph;
1041 1041
    typedef typename Graph::Arc Parent;
1042 1042

	
1043 1043
    typedef typename Graph::Arc Arc;
1044 1044
    typedef typename Graph::Node Node;
1045 1045

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

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

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

	
1071 1071
  namespace _core_bits {
1072 1072

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

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

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

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

	
1163 1163
    typedef _Graph Graph;
1164 1164
    typedef typename Graph::Edge Parent;
1165 1165

	
1166 1166
    typedef typename Graph::Edge Edge;
1167 1167
    typedef typename Graph::Node Node;
1168 1168

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

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

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

	
1194 1194

	
1195 1195
  ///Dynamic arc look-up between given endpoints.
1196 1196

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

	
1225 1225
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1226 1226
    typedef G Digraph;
1227 1227

	
1228 1228
  protected:
1229 1229

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

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

	
1235 1235
      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
1236 1236

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

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

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

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

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

	
1275 1275
  public:
1276 1276

	
1277 1277
    ///Constructor
1278 1278

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

	
1289 1289
  protected:
1290 1290

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

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

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

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

	
1311 1311
    virtual void build() {
1312 1312
      refresh();
1313 1313
    }
1314 1314

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

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

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

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

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

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

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

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

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

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

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

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

	
1528 1528

	
1529 1529
  public:
1530 1530

	
1531 1531
    ///Find an arc between two nodes.
1532 1532

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

	
1609 1609
  };
1610 1610

	
1611 1611
  ///Fast arc look-up between given endpoints.
1612 1612

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

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

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

	
1652 1652
  public:
1653 1653

	
1654 1654
    ///Constructor
1655 1655

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

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

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

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

	
1701 1701
    ///Find an arc between two nodes.
1702 1702

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

	
1722 1722
  };
1723 1723

	
1724 1724
  ///Fast look-up of all arcs between given endpoints.
1725 1725

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

	
1747 1747
    TEMPLATE_DIGRAPH_TYPEDEFS(G);
1748 1748
    typedef G Digraph;
1749 1749

	
1750 1750
    typename Digraph::template ArcMap<Arc> _next;
1751 1751

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

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

	
1768 1768
  public:
1769 1769
    ///Constructor
1770 1770

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

	
1777 1777
    ///Refresh the data structure at a node.
1778 1778

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

	
1789 1789
    ///Refresh the full data structure.
1790 1790

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

	
1802 1802
    ///Find an arc between two nodes.
1803 1803

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

	
1839 1839
  };
1840 1840

	
1841 1841
  /// @}
1842 1842

	
1843 1843
} //namespace lemon
1844 1844

	
1845 1845
#endif
0 comments (0 inline)