... |
... |
@@ -205,411 +205,413 @@
|
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;
|