... | ... |
@@ -352,192 +352,211 @@ |
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 | 397 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
398 | 398 |
nodeRefMap[it] = to.addNode(); |
399 | 399 |
} |
400 | 400 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
401 | 401 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
402 | 402 |
nodeRefMap[from.target(it)]); |
403 | 403 |
} |
404 | 404 |
} |
405 | 405 |
}; |
406 | 406 |
|
407 | 407 |
template <typename Digraph> |
408 | 408 |
struct DigraphCopySelector< |
409 | 409 |
Digraph, |
410 | 410 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
411 | 411 |
{ |
412 | 412 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
413 | 413 |
static void copy(const From& from, Digraph &to, |
414 | 414 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
415 | 415 |
to.build(from, nodeRefMap, arcRefMap); |
416 | 416 |
} |
417 | 417 |
}; |
418 | 418 |
|
419 | 419 |
template <typename Graph, typename Enable = void> |
420 | 420 |
struct GraphCopySelector { |
421 | 421 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
422 | 422 |
static void copy(const From& from, Graph &to, |
423 | 423 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
424 | 424 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
425 | 425 |
nodeRefMap[it] = to.addNode(); |
426 | 426 |
} |
427 | 427 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
428 | 428 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
429 | 429 |
nodeRefMap[from.v(it)]); |
430 | 430 |
} |
431 | 431 |
} |
432 | 432 |
}; |
433 | 433 |
|
434 | 434 |
template <typename Graph> |
435 | 435 |
struct GraphCopySelector< |
436 | 436 |
Graph, |
437 | 437 |
typename enable_if<typename Graph::BuildTag, void>::type> |
438 | 438 |
{ |
439 | 439 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
440 | 440 |
static void copy(const From& from, Graph &to, |
441 | 441 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
442 | 442 |
to.build(from, nodeRefMap, edgeRefMap); |
443 | 443 |
} |
444 | 444 |
}; |
445 | 445 |
|
446 | 446 |
} |
447 | 447 |
|
448 |
/// Check whether a graph is undirected. |
|
449 |
/// |
|
450 |
/// This function returns \c true if the given graph is undirected. |
|
451 |
#ifdef DOXYGEN |
|
452 |
template <typename GR> |
|
453 |
bool undirected(const GR& g) { return false; } |
|
454 |
#else |
|
455 |
template <typename GR> |
|
456 |
typename enable_if<UndirectedTagIndicator<GR>, bool>::type |
|
457 |
undirected(const GR&) { |
|
458 |
return true; |
|
459 |
} |
|
460 |
template <typename GR> |
|
461 |
typename disable_if<UndirectedTagIndicator<GR>, bool>::type |
|
462 |
undirected(const GR&) { |
|
463 |
return false; |
|
464 |
} |
|
465 |
#endif |
|
466 |
|
|
448 | 467 |
/// \brief Class to copy a digraph. |
449 | 468 |
/// |
450 | 469 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
451 | 470 |
/// simplest way of using it is through the \c digraphCopy() function. |
452 | 471 |
/// |
453 | 472 |
/// This class not only make a copy of a digraph, but it can create |
454 | 473 |
/// references and cross references between the nodes and arcs of |
455 | 474 |
/// the two digraphs, and it can copy maps to use with the newly created |
456 | 475 |
/// digraph. |
457 | 476 |
/// |
458 | 477 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
459 | 478 |
/// should be created, then the data belongs to the digraph should |
460 | 479 |
/// assigned to copy. In the end, the \c run() member should be |
461 | 480 |
/// called. |
462 | 481 |
/// |
463 | 482 |
/// The next code copies a digraph with several data: |
464 | 483 |
///\code |
465 | 484 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
466 | 485 |
/// // Create references for the nodes |
467 | 486 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
468 | 487 |
/// cg.nodeRef(nr); |
469 | 488 |
/// // Create cross references (inverse) for the arcs |
470 | 489 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
471 | 490 |
/// cg.arcCrossRef(acr); |
472 | 491 |
/// // Copy an arc map |
473 | 492 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
474 | 493 |
/// NewGraph::ArcMap<double> namap(new_graph); |
475 | 494 |
/// cg.arcMap(oamap, namap); |
476 | 495 |
/// // Copy a node |
477 | 496 |
/// OrigGraph::Node on; |
478 | 497 |
/// NewGraph::Node nn; |
479 | 498 |
/// cg.node(on, nn); |
480 | 499 |
/// // Execute copying |
481 | 500 |
/// cg.run(); |
482 | 501 |
///\endcode |
483 | 502 |
template <typename From, typename To> |
484 | 503 |
class DigraphCopy { |
485 | 504 |
private: |
486 | 505 |
|
487 | 506 |
typedef typename From::Node Node; |
488 | 507 |
typedef typename From::NodeIt NodeIt; |
489 | 508 |
typedef typename From::Arc Arc; |
490 | 509 |
typedef typename From::ArcIt ArcIt; |
491 | 510 |
|
492 | 511 |
typedef typename To::Node TNode; |
493 | 512 |
typedef typename To::Arc TArc; |
494 | 513 |
|
495 | 514 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
496 | 515 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
497 | 516 |
|
498 | 517 |
public: |
499 | 518 |
|
500 | 519 |
/// \brief Constructor of DigraphCopy. |
501 | 520 |
/// |
502 | 521 |
/// Constructor of DigraphCopy for copying the content of the |
503 | 522 |
/// \c from digraph into the \c to digraph. |
504 | 523 |
DigraphCopy(const From& from, To& to) |
505 | 524 |
: _from(from), _to(to) {} |
506 | 525 |
|
507 | 526 |
/// \brief Destructor of DigraphCopy |
508 | 527 |
/// |
509 | 528 |
/// Destructor of DigraphCopy. |
510 | 529 |
~DigraphCopy() { |
511 | 530 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
512 | 531 |
delete _node_maps[i]; |
513 | 532 |
} |
514 | 533 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
515 | 534 |
delete _arc_maps[i]; |
516 | 535 |
} |
517 | 536 |
|
518 | 537 |
} |
519 | 538 |
|
520 | 539 |
/// \brief Copy the node references into the given map. |
521 | 540 |
/// |
522 | 541 |
/// This function copies the node references into the given map. |
523 | 542 |
/// The parameter should be a map, whose key type is the Node type of |
524 | 543 |
/// the source digraph, while the value type is the Node type of the |
525 | 544 |
/// destination digraph. |
526 | 545 |
template <typename NodeRef> |
527 | 546 |
DigraphCopy& nodeRef(NodeRef& map) { |
528 | 547 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
529 | 548 |
NodeRefMap, NodeRef>(map)); |
530 | 549 |
return *this; |
531 | 550 |
} |
532 | 551 |
|
533 | 552 |
/// \brief Copy the node cross references into the given map. |
534 | 553 |
/// |
535 | 554 |
/// This function copies the node cross references (reverse references) |
536 | 555 |
/// into the given map. The parameter should be a map, whose key type |
537 | 556 |
/// is the Node type of the destination digraph, while the value type is |
538 | 557 |
/// the Node type of the source digraph. |
539 | 558 |
template <typename NodeCrossRef> |
540 | 559 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
541 | 560 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
542 | 561 |
NodeRefMap, NodeCrossRef>(map)); |
543 | 562 |
return *this; |
0 comments (0 inline)