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