gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Revert faulty changes of [dc9e8d2c0df9]
0 1 0
default
1 file changed with 8 insertions and 8 deletions:
↑ Collapse diff ↑
Ignore white space 1536 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

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

	
35 35
namespace lemon {
36 36

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

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

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

	
61
  ///Create convenient typedefs for the digraph types and iterators
61
  ///Create convenience typedefs for the digraph types and iterators
62 62

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

	
85
  ///Create convenient typedefs for the digraph types and iterators
85
  ///Create convenience typedefs for the digraph types and iterators
86 86

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

	
105
  ///Create convenient typedefs for the graph types and iterators
105
  ///Create convenience typedefs for the graph types and iterators
106 106

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

	
124
  ///Create convenient typedefs for the graph types and iterators
124
  ///Create convenience typedefs for the graph types and iterators
125 125

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

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

	
154 154
  // Node counting:
155 155

	
156 156
  namespace _core_bits {
157 157

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

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

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

	
190 190
  // Arc counting:
191 191

	
192 192
  namespace _core_bits {
193 193

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

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

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

	
226 226
  // Edge counting:
227 227

	
228 228
  namespace _core_bits {
229 229

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

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

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

	
261 261
  }
262 262

	
263 263

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

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

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

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

	
300 300
  namespace _core_bits {
301 301

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
434 434
  }
435 435

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

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

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

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

	
486 486
  public:
487 487

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

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

	
506 506
    }
507 507

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

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

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

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

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

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

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

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

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

	
623 623
  protected:
624 624

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

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

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

	
634 634
  };
635 635

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

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

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

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

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

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

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

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

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

	
732 732
  public:
733 733

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
903 903
    /// \brief Execute copying.
904 904
    ///
905 905
    /// This function executes the copying of the graph along with the
0 comments (0 inline)