gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge bugfix #371 to branch 1.0
0 2 0
merge 1.0
0 files changed with 26 insertions and 0 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -3,795 +3,797 @@
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

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

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

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

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

	
37 37
namespace lemon {
38 38

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

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

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

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

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

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

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

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

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

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

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

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

	
156 156
  // Node counting:
157 157

	
158 158
  namespace _core_bits {
159 159

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

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

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

	
192 192
  // Arc counting:
193 193

	
194 194
  namespace _core_bits {
195 195

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

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

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

	
228 228
  // Edge counting:
229 229

	
230 230
  namespace _core_bits {
231 231

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

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

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

	
263 263
  }
264 264

	
265 265

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

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

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

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

	
302 302
  namespace _core_bits {
303 303

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
436 438
  }
437 439

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

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

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

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

	
488 490
  public:
489 491

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

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

	
508 510
    }
509 511

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

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

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

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

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

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

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

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

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

	
625 627
  protected:
626 628

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

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

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

	
636 638
  };
637 639

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

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

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

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

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

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

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

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

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

	
734 736
  public:
735 737

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

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

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

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

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

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

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

	
24 24
#include "test_tools.h"
25 25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
185 209

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

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