gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge bugfix #371 to branch 1.2
0 2 0
merge 1.2
2 files changed with 26 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 768 line context
... ...
@@ -13,795 +13,797 @@
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_CORE_H
20 20
#define LEMON_CORE_H
21 21

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

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

	
30 30
// Disable the following warnings when compiling with MSVC:
31 31
// C4250: 'class1' : inherits 'class2::member' via dominance
32 32
// C4355: 'this' : used in base member initializer list
33 33
// C4503: 'function' : decorated name length exceeded, name was truncated
34 34
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
35 35
// C4996: 'function': was declared deprecated
36 36
#ifdef _MSC_VER
37 37
#pragma warning( disable : 4250 4355 4503 4800 4996 )
38 38
#endif
39 39

	
40 40
///\file
41 41
///\brief LEMON core utilities.
42 42
///
43 43
///This header file contains core utilities for LEMON.
44 44
///It is automatically included by all graph types, therefore it usually
45 45
///do not have to be included directly.
46 46

	
47 47
namespace lemon {
48 48

	
49 49
  /// \brief Dummy type to make it easier to create invalid iterators.
50 50
  ///
51 51
  /// Dummy type to make it easier to create invalid iterators.
52 52
  /// See \ref INVALID for the usage.
53 53
  struct Invalid {
54 54
  public:
55 55
    bool operator==(Invalid) { return true;  }
56 56
    bool operator!=(Invalid) { return false; }
57 57
    bool operator< (Invalid) { return false; }
58 58
  };
59 59

	
60 60
  /// \brief Invalid iterators.
61 61
  ///
62 62
  /// \ref Invalid is a global type that converts to each iterator
63 63
  /// in such a way that the value of the target iterator will be invalid.
64 64
#ifdef LEMON_ONLY_TEMPLATES
65 65
  const Invalid INVALID = Invalid();
66 66
#else
67 67
  extern const Invalid INVALID;
68 68
#endif
69 69

	
70 70
  /// \addtogroup gutils
71 71
  /// @{
72 72

	
73 73
  ///Create convenience typedefs for the digraph types and iterators
74 74

	
75 75
  ///This \c \#define creates convenient type definitions for the following
76 76
  ///types of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
77 77
  ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
78 78
  ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
79 79
  ///
80 80
  ///\note If the graph type is a dependent type, ie. the graph type depend
81 81
  ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
82 82
  ///macro.
83 83
#define DIGRAPH_TYPEDEFS(Digraph)                                       \
84 84
  typedef Digraph::Node Node;                                           \
85 85
  typedef Digraph::NodeIt NodeIt;                                       \
86 86
  typedef Digraph::Arc Arc;                                             \
87 87
  typedef Digraph::ArcIt ArcIt;                                         \
88 88
  typedef Digraph::InArcIt InArcIt;                                     \
89 89
  typedef Digraph::OutArcIt OutArcIt;                                   \
90 90
  typedef Digraph::NodeMap<bool> BoolNodeMap;                           \
91 91
  typedef Digraph::NodeMap<int> IntNodeMap;                             \
92 92
  typedef Digraph::NodeMap<double> DoubleNodeMap;                       \
93 93
  typedef Digraph::ArcMap<bool> BoolArcMap;                             \
94 94
  typedef Digraph::ArcMap<int> IntArcMap;                               \
95 95
  typedef Digraph::ArcMap<double> DoubleArcMap
96 96

	
97 97
  ///Create convenience typedefs for the digraph types and iterators
98 98

	
99 99
  ///\see DIGRAPH_TYPEDEFS
100 100
  ///
101 101
  ///\note Use this macro, if the graph type is a dependent type,
102 102
  ///ie. the graph type depend on a template parameter.
103 103
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph)                              \
104 104
  typedef typename Digraph::Node Node;                                  \
105 105
  typedef typename Digraph::NodeIt NodeIt;                              \
106 106
  typedef typename Digraph::Arc Arc;                                    \
107 107
  typedef typename Digraph::ArcIt ArcIt;                                \
108 108
  typedef typename Digraph::InArcIt InArcIt;                            \
109 109
  typedef typename Digraph::OutArcIt OutArcIt;                          \
110 110
  typedef typename Digraph::template NodeMap<bool> BoolNodeMap;         \
111 111
  typedef typename Digraph::template NodeMap<int> IntNodeMap;           \
112 112
  typedef typename Digraph::template NodeMap<double> DoubleNodeMap;     \
113 113
  typedef typename Digraph::template ArcMap<bool> BoolArcMap;           \
114 114
  typedef typename Digraph::template ArcMap<int> IntArcMap;             \
115 115
  typedef typename Digraph::template ArcMap<double> DoubleArcMap
116 116

	
117 117
  ///Create convenience typedefs for the graph types and iterators
118 118

	
119 119
  ///This \c \#define creates the same convenient type definitions as defined
120 120
  ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
121 121
  ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
122 122
  ///\c DoubleEdgeMap.
123 123
  ///
124 124
  ///\note If the graph type is a dependent type, ie. the graph type depend
125 125
  ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
126 126
  ///macro.
127 127
#define GRAPH_TYPEDEFS(Graph)                                           \
128 128
  DIGRAPH_TYPEDEFS(Graph);                                              \
129 129
  typedef Graph::Edge Edge;                                             \
130 130
  typedef Graph::EdgeIt EdgeIt;                                         \
131 131
  typedef Graph::IncEdgeIt IncEdgeIt;                                   \
132 132
  typedef Graph::EdgeMap<bool> BoolEdgeMap;                             \
133 133
  typedef Graph::EdgeMap<int> IntEdgeMap;                               \
134 134
  typedef Graph::EdgeMap<double> DoubleEdgeMap
135 135

	
136 136
  ///Create convenience typedefs for the graph types and iterators
137 137

	
138 138
  ///\see GRAPH_TYPEDEFS
139 139
  ///
140 140
  ///\note Use this macro, if the graph type is a dependent type,
141 141
  ///ie. the graph type depend on a template parameter.
142 142
#define TEMPLATE_GRAPH_TYPEDEFS(Graph)                                  \
143 143
  TEMPLATE_DIGRAPH_TYPEDEFS(Graph);                                     \
144 144
  typedef typename Graph::Edge Edge;                                    \
145 145
  typedef typename Graph::EdgeIt EdgeIt;                                \
146 146
  typedef typename Graph::IncEdgeIt IncEdgeIt;                          \
147 147
  typedef typename Graph::template EdgeMap<bool> BoolEdgeMap;           \
148 148
  typedef typename Graph::template EdgeMap<int> IntEdgeMap;             \
149 149
  typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
150 150

	
151 151
  /// \brief Function to count the items in a graph.
152 152
  ///
153 153
  /// This function counts the items (nodes, arcs etc.) in a graph.
154 154
  /// The complexity of the function is linear because
155 155
  /// it iterates on all of the items.
156 156
  template <typename Graph, typename Item>
157 157
  inline int countItems(const Graph& g) {
158 158
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
159 159
    int num = 0;
160 160
    for (ItemIt it(g); it != INVALID; ++it) {
161 161
      ++num;
162 162
    }
163 163
    return num;
164 164
  }
165 165

	
166 166
  // Node counting:
167 167

	
168 168
  namespace _core_bits {
169 169

	
170 170
    template <typename Graph, typename Enable = void>
171 171
    struct CountNodesSelector {
172 172
      static int count(const Graph &g) {
173 173
        return countItems<Graph, typename Graph::Node>(g);
174 174
      }
175 175
    };
176 176

	
177 177
    template <typename Graph>
178 178
    struct CountNodesSelector<
179 179
      Graph, typename
180 180
      enable_if<typename Graph::NodeNumTag, void>::type>
181 181
    {
182 182
      static int count(const Graph &g) {
183 183
        return g.nodeNum();
184 184
      }
185 185
    };
186 186
  }
187 187

	
188 188
  /// \brief Function to count the nodes in the graph.
189 189
  ///
190 190
  /// This function counts the nodes in the graph.
191 191
  /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
192 192
  /// graph structures it is specialized to run in <em>O</em>(1).
193 193
  ///
194 194
  /// \note If the graph contains a \c nodeNum() member function and a
195 195
  /// \c NodeNumTag tag then this function calls directly the member
196 196
  /// function to query the cardinality of the node set.
197 197
  template <typename Graph>
198 198
  inline int countNodes(const Graph& g) {
199 199
    return _core_bits::CountNodesSelector<Graph>::count(g);
200 200
  }
201 201

	
202 202
  // Arc counting:
203 203

	
204 204
  namespace _core_bits {
205 205

	
206 206
    template <typename Graph, typename Enable = void>
207 207
    struct CountArcsSelector {
208 208
      static int count(const Graph &g) {
209 209
        return countItems<Graph, typename Graph::Arc>(g);
210 210
      }
211 211
    };
212 212

	
213 213
    template <typename Graph>
214 214
    struct CountArcsSelector<
215 215
      Graph,
216 216
      typename enable_if<typename Graph::ArcNumTag, void>::type>
217 217
    {
218 218
      static int count(const Graph &g) {
219 219
        return g.arcNum();
220 220
      }
221 221
    };
222 222
  }
223 223

	
224 224
  /// \brief Function to count the arcs in the graph.
225 225
  ///
226 226
  /// This function counts the arcs in the graph.
227 227
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
228 228
  /// graph structures it is specialized to run in <em>O</em>(1).
229 229
  ///
230 230
  /// \note If the graph contains a \c arcNum() member function and a
231 231
  /// \c ArcNumTag tag then this function calls directly the member
232 232
  /// function to query the cardinality of the arc set.
233 233
  template <typename Graph>
234 234
  inline int countArcs(const Graph& g) {
235 235
    return _core_bits::CountArcsSelector<Graph>::count(g);
236 236
  }
237 237

	
238 238
  // Edge counting:
239 239

	
240 240
  namespace _core_bits {
241 241

	
242 242
    template <typename Graph, typename Enable = void>
243 243
    struct CountEdgesSelector {
244 244
      static int count(const Graph &g) {
245 245
        return countItems<Graph, typename Graph::Edge>(g);
246 246
      }
247 247
    };
248 248

	
249 249
    template <typename Graph>
250 250
    struct CountEdgesSelector<
251 251
      Graph,
252 252
      typename enable_if<typename Graph::EdgeNumTag, void>::type>
253 253
    {
254 254
      static int count(const Graph &g) {
255 255
        return g.edgeNum();
256 256
      }
257 257
    };
258 258
  }
259 259

	
260 260
  /// \brief Function to count the edges in the graph.
261 261
  ///
262 262
  /// This function counts the edges in the graph.
263 263
  /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
264 264
  /// graph structures it is specialized to run in <em>O</em>(1).
265 265
  ///
266 266
  /// \note If the graph contains a \c edgeNum() member function and a
267 267
  /// \c EdgeNumTag tag then this function calls directly the member
268 268
  /// function to query the cardinality of the edge set.
269 269
  template <typename Graph>
270 270
  inline int countEdges(const Graph& g) {
271 271
    return _core_bits::CountEdgesSelector<Graph>::count(g);
272 272

	
273 273
  }
274 274

	
275 275

	
276 276
  template <typename Graph, typename DegIt>
277 277
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
278 278
    int num = 0;
279 279
    for (DegIt it(_g, _n); it != INVALID; ++it) {
280 280
      ++num;
281 281
    }
282 282
    return num;
283 283
  }
284 284

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

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

	
303 303
  /// \brief Function to count the number of the inc-edges to node \c n.
304 304
  ///
305 305
  /// This function counts the number of the inc-edges to node \c n
306 306
  /// in the undirected graph \c g.
307 307
  template <typename Graph>
308 308
  inline int countIncEdges(const Graph& g,  const typename Graph::Node& n) {
309 309
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
310 310
  }
311 311

	
312 312
  namespace _core_bits {
313 313

	
314 314
    template <typename Digraph, typename Item, typename RefMap>
315 315
    class MapCopyBase {
316 316
    public:
317 317
      virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
318 318

	
319 319
      virtual ~MapCopyBase() {}
320 320
    };
321 321

	
322 322
    template <typename Digraph, typename Item, typename RefMap,
323 323
              typename FromMap, typename ToMap>
324 324
    class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
325 325
    public:
326 326

	
327 327
      MapCopy(const FromMap& map, ToMap& tmap)
328 328
        : _map(map), _tmap(tmap) {}
329 329

	
330 330
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
331 331
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
332 332
        for (ItemIt it(digraph); it != INVALID; ++it) {
333 333
          _tmap.set(refMap[it], _map[it]);
334 334
        }
335 335
      }
336 336

	
337 337
    private:
338 338
      const FromMap& _map;
339 339
      ToMap& _tmap;
340 340
    };
341 341

	
342 342
    template <typename Digraph, typename Item, typename RefMap, typename It>
343 343
    class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
344 344
    public:
345 345

	
346 346
      ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
347 347

	
348 348
      virtual void copy(const Digraph&, const RefMap& refMap) {
349 349
        _it = refMap[_item];
350 350
      }
351 351

	
352 352
    private:
353 353
      Item _item;
354 354
      It& _it;
355 355
    };
356 356

	
357 357
    template <typename Digraph, typename Item, typename RefMap, typename Ref>
358 358
    class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
359 359
    public:
360 360

	
361 361
      RefCopy(Ref& map) : _map(map) {}
362 362

	
363 363
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
364 364
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
365 365
        for (ItemIt it(digraph); it != INVALID; ++it) {
366 366
          _map.set(it, refMap[it]);
367 367
        }
368 368
      }
369 369

	
370 370
    private:
371 371
      Ref& _map;
372 372
    };
373 373

	
374 374
    template <typename Digraph, typename Item, typename RefMap,
375 375
              typename CrossRef>
376 376
    class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
377 377
    public:
378 378

	
379 379
      CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
380 380

	
381 381
      virtual void copy(const Digraph& digraph, const RefMap& refMap) {
382 382
        typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
383 383
        for (ItemIt it(digraph); it != INVALID; ++it) {
384 384
          _cmap.set(refMap[it], it);
385 385
        }
386 386
      }
387 387

	
388 388
    private:
389 389
      CrossRef& _cmap;
390 390
    };
391 391

	
392 392
    template <typename Digraph, typename Enable = void>
393 393
    struct DigraphCopySelector {
394 394
      template <typename From, typename NodeRefMap, typename ArcRefMap>
395 395
      static void copy(const From& from, Digraph &to,
396 396
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
397
        to.clear();
397 398
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
398 399
          nodeRefMap[it] = to.addNode();
399 400
        }
400 401
        for (typename From::ArcIt it(from); it != INVALID; ++it) {
401 402
          arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
402 403
                                    nodeRefMap[from.target(it)]);
403 404
        }
404 405
      }
405 406
    };
406 407

	
407 408
    template <typename Digraph>
408 409
    struct DigraphCopySelector<
409 410
      Digraph,
410 411
      typename enable_if<typename Digraph::BuildTag, void>::type>
411 412
    {
412 413
      template <typename From, typename NodeRefMap, typename ArcRefMap>
413 414
      static void copy(const From& from, Digraph &to,
414 415
                       NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
415 416
        to.build(from, nodeRefMap, arcRefMap);
416 417
      }
417 418
    };
418 419

	
419 420
    template <typename Graph, typename Enable = void>
420 421
    struct GraphCopySelector {
421 422
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
422 423
      static void copy(const From& from, Graph &to,
423 424
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
425
        to.clear();
424 426
        for (typename From::NodeIt it(from); it != INVALID; ++it) {
425 427
          nodeRefMap[it] = to.addNode();
426 428
        }
427 429
        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
428 430
          edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
429 431
                                      nodeRefMap[from.v(it)]);
430 432
        }
431 433
      }
432 434
    };
433 435

	
434 436
    template <typename Graph>
435 437
    struct GraphCopySelector<
436 438
      Graph,
437 439
      typename enable_if<typename Graph::BuildTag, void>::type>
438 440
    {
439 441
      template <typename From, typename NodeRefMap, typename EdgeRefMap>
440 442
      static void copy(const From& from, Graph &to,
441 443
                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
442 444
        to.build(from, nodeRefMap, edgeRefMap);
443 445
      }
444 446
    };
445 447

	
446 448
  }
447 449

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

	
487 489
    typedef typename From::Node Node;
488 490
    typedef typename From::NodeIt NodeIt;
489 491
    typedef typename From::Arc Arc;
490 492
    typedef typename From::ArcIt ArcIt;
491 493

	
492 494
    typedef typename To::Node TNode;
493 495
    typedef typename To::Arc TArc;
494 496

	
495 497
    typedef typename From::template NodeMap<TNode> NodeRefMap;
496 498
    typedef typename From::template ArcMap<TArc> ArcRefMap;
497 499

	
498 500
  public:
499 501

	
500 502
    /// \brief Constructor of DigraphCopy.
501 503
    ///
502 504
    /// Constructor of DigraphCopy for copying the content of the
503 505
    /// \c from digraph into the \c to digraph.
504 506
    DigraphCopy(const From& from, To& to)
505 507
      : _from(from), _to(to) {}
506 508

	
507 509
    /// \brief Destructor of DigraphCopy
508 510
    ///
509 511
    /// Destructor of DigraphCopy.
510 512
    ~DigraphCopy() {
511 513
      for (int i = 0; i < int(_node_maps.size()); ++i) {
512 514
        delete _node_maps[i];
513 515
      }
514 516
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
515 517
        delete _arc_maps[i];
516 518
      }
517 519

	
518 520
    }
519 521

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

	
533 535
    /// \brief Copy the node cross references into the given map.
534 536
    ///
535 537
    /// This function copies the node cross references (reverse references)
536 538
    /// into the given map. The parameter should be a map, whose key type
537 539
    /// is the Node type of the destination digraph, while the value type is
538 540
    /// the Node type of the source digraph.
539 541
    template <typename NodeCrossRef>
540 542
    DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
541 543
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
542 544
                           NodeRefMap, NodeCrossRef>(map));
543 545
      return *this;
544 546
    }
545 547

	
546 548
    /// \brief Make a copy of the given node map.
547 549
    ///
548 550
    /// This function makes a copy of the given node map for the newly
549 551
    /// created digraph.
550 552
    /// The key type of the new map \c tmap should be the Node type of the
551 553
    /// destination digraph, and the key type of the original map \c map
552 554
    /// should be the Node type of the source digraph.
553 555
    template <typename FromMap, typename ToMap>
554 556
    DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
555 557
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
556 558
                           NodeRefMap, FromMap, ToMap>(map, tmap));
557 559
      return *this;
558 560
    }
559 561

	
560 562
    /// \brief Make a copy of the given node.
561 563
    ///
562 564
    /// This function makes a copy of the given node.
563 565
    DigraphCopy& node(const Node& node, TNode& tnode) {
564 566
      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
565 567
                           NodeRefMap, TNode>(node, tnode));
566 568
      return *this;
567 569
    }
568 570

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

	
582 584
    /// \brief Copy the arc cross references into the given map.
583 585
    ///
584 586
    /// This function copies the arc cross references (reverse references)
585 587
    /// into the given map. The parameter should be a map, whose key type
586 588
    /// is the Arc type of the destination digraph, while the value type is
587 589
    /// the Arc type of the source digraph.
588 590
    template <typename ArcCrossRef>
589 591
    DigraphCopy& arcCrossRef(ArcCrossRef& map) {
590 592
      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
591 593
                          ArcRefMap, ArcCrossRef>(map));
592 594
      return *this;
593 595
    }
594 596

	
595 597
    /// \brief Make a copy of the given arc map.
596 598
    ///
597 599
    /// This function makes a copy of the given arc map for the newly
598 600
    /// created digraph.
599 601
    /// The key type of the new map \c tmap should be the Arc type of the
600 602
    /// destination digraph, and the key type of the original map \c map
601 603
    /// should be the Arc type of the source digraph.
602 604
    template <typename FromMap, typename ToMap>
603 605
    DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
604 606
      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
605 607
                          ArcRefMap, FromMap, ToMap>(map, tmap));
606 608
      return *this;
607 609
    }
608 610

	
609 611
    /// \brief Make a copy of the given arc.
610 612
    ///
611 613
    /// This function makes a copy of the given arc.
612 614
    DigraphCopy& arc(const Arc& arc, TArc& tarc) {
613 615
      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
614 616
                          ArcRefMap, TArc>(arc, tarc));
615 617
      return *this;
616 618
    }
617 619

	
618 620
    /// \brief Execute copying.
619 621
    ///
620 622
    /// This function executes the copying of the digraph along with the
621 623
    /// copying of the assigned data.
622 624
    void run() {
623 625
      NodeRefMap nodeRefMap(_from);
624 626
      ArcRefMap arcRefMap(_from);
625 627
      _core_bits::DigraphCopySelector<To>::
626 628
        copy(_from, _to, nodeRefMap, arcRefMap);
627 629
      for (int i = 0; i < int(_node_maps.size()); ++i) {
628 630
        _node_maps[i]->copy(_from, nodeRefMap);
629 631
      }
630 632
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
631 633
        _arc_maps[i]->copy(_from, arcRefMap);
632 634
      }
633 635
    }
634 636

	
635 637
  protected:
636 638

	
637 639
    const From& _from;
638 640
    To& _to;
639 641

	
640 642
    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
641 643
      _node_maps;
642 644

	
643 645
    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
644 646
      _arc_maps;
645 647

	
646 648
  };
647 649

	
648 650
  /// \brief Copy a digraph to another digraph.
649 651
  ///
650 652
  /// This function copies a digraph to another digraph.
651 653
  /// The complete usage of it is detailed in the DigraphCopy class, but
652 654
  /// a short example shows a basic work:
653 655
  ///\code
654 656
  /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
655 657
  ///\endcode
656 658
  ///
657 659
  /// After the copy the \c nr map will contain the mapping from the
658 660
  /// nodes of the \c from digraph to the nodes of the \c to digraph and
659 661
  /// \c acr will contain the mapping from the arcs of the \c to digraph
660 662
  /// to the arcs of the \c from digraph.
661 663
  ///
662 664
  /// \see DigraphCopy
663 665
  template <typename From, typename To>
664 666
  DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
665 667
    return DigraphCopy<From, To>(from, to);
666 668
  }
667 669

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

	
707 709
    typedef typename From::Node Node;
708 710
    typedef typename From::NodeIt NodeIt;
709 711
    typedef typename From::Arc Arc;
710 712
    typedef typename From::ArcIt ArcIt;
711 713
    typedef typename From::Edge Edge;
712 714
    typedef typename From::EdgeIt EdgeIt;
713 715

	
714 716
    typedef typename To::Node TNode;
715 717
    typedef typename To::Arc TArc;
716 718
    typedef typename To::Edge TEdge;
717 719

	
718 720
    typedef typename From::template NodeMap<TNode> NodeRefMap;
719 721
    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
720 722

	
721 723
    struct ArcRefMap {
722 724
      ArcRefMap(const From& from, const To& to,
723 725
                const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
724 726
        : _from(from), _to(to),
725 727
          _edge_ref(edge_ref), _node_ref(node_ref) {}
726 728

	
727 729
      typedef typename From::Arc Key;
728 730
      typedef typename To::Arc Value;
729 731

	
730 732
      Value operator[](const Key& key) const {
731 733
        bool forward = _from.u(key) != _from.v(key) ?
732 734
          _node_ref[_from.source(key)] ==
733 735
          _to.source(_to.direct(_edge_ref[key], true)) :
734 736
          _from.direction(key);
735 737
        return _to.direct(_edge_ref[key], forward);
736 738
      }
737 739

	
738 740
      const From& _from;
739 741
      const To& _to;
740 742
      const EdgeRefMap& _edge_ref;
741 743
      const NodeRefMap& _node_ref;
742 744
    };
743 745

	
744 746
  public:
745 747

	
746 748
    /// \brief Constructor of GraphCopy.
747 749
    ///
748 750
    /// Constructor of GraphCopy for copying the content of the
749 751
    /// \c from graph into the \c to graph.
750 752
    GraphCopy(const From& from, To& to)
751 753
      : _from(from), _to(to) {}
752 754

	
753 755
    /// \brief Destructor of GraphCopy
754 756
    ///
755 757
    /// Destructor of GraphCopy.
756 758
    ~GraphCopy() {
757 759
      for (int i = 0; i < int(_node_maps.size()); ++i) {
758 760
        delete _node_maps[i];
759 761
      }
760 762
      for (int i = 0; i < int(_arc_maps.size()); ++i) {
761 763
        delete _arc_maps[i];
762 764
      }
763 765
      for (int i = 0; i < int(_edge_maps.size()); ++i) {
764 766
        delete _edge_maps[i];
765 767
      }
766 768
    }
767 769

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

	
781 783
    /// \brief Copy the node cross references into the given map.
782 784
    ///
783 785
    /// This function copies the node cross references (reverse references)
784 786
    /// into the given map. The parameter should be a map, whose key type
785 787
    /// is the Node type of the destination graph, while the value type is
786 788
    /// the Node type of the source graph.
787 789
    template <typename NodeCrossRef>
788 790
    GraphCopy& nodeCrossRef(NodeCrossRef& map) {
789 791
      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
790 792
                           NodeRefMap, NodeCrossRef>(map));
791 793
      return *this;
792 794
    }
793 795

	
794 796
    /// \brief Make a copy of the given node map.
795 797
    ///
796 798
    /// This function makes a copy of the given node map for the newly
797 799
    /// created graph.
798 800
    /// The key type of the new map \c tmap should be the Node type of the
799 801
    /// destination graph, and the key type of the original map \c map
800 802
    /// should be the Node type of the source graph.
801 803
    template <typename FromMap, typename ToMap>
802 804
    GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
803 805
      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
804 806
                           NodeRefMap, FromMap, ToMap>(map, tmap));
805 807
      return *this;
806 808
    }
807 809

	
Ignore white space 768 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-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/smart_graph.h>
20 20
#include <lemon/list_graph.h>
21 21
#include <lemon/lgf_reader.h>
22 22
#include <lemon/error.h>
23 23

	
24 24
#include "test_tools.h"
25 25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
185 209

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

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