| 1 | 1 |
/* -*- C++ -*- |
| 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 |
#ifndef LEMON_GRAPH_UTILS_H |
| 20 | 20 |
#define LEMON_GRAPH_UTILS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
#include <vector> |
| 24 | 24 |
#include <map> |
| 25 | 25 |
#include <cmath> |
| 26 | 26 |
#include <algorithm> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/bits/invalid.h> |
| 29 | 29 |
#include <lemon/bits/utility.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/bits/traits.h> |
| 32 | 32 |
|
| 33 | 33 |
#include <lemon/bits/alteration_notifier.h> |
| 34 | 34 |
#include <lemon/bits/default_map.h> |
| 35 | 35 |
|
| 36 | 36 |
///\ingroup gutils |
| 37 | 37 |
///\file |
| 38 | 38 |
///\brief Graph utilities. |
| 39 | 39 |
|
| 40 | 40 |
namespace lemon {
|
| 41 | 41 |
|
| 42 | 42 |
/// \addtogroup gutils |
| 43 | 43 |
/// @{
|
| 44 | 44 |
|
| 45 | 45 |
namespace _graph_utils_bits {
|
| 46 | 46 |
template <typename Graph> |
| 47 | 47 |
struct Node { typedef typename Graph::Node type; };
|
| 48 | 48 |
|
| 49 | 49 |
template <typename Graph> |
| 50 | 50 |
struct NodeIt { typedef typename Graph::NodeIt type; };
|
| 51 | 51 |
|
| 52 | 52 |
template <typename Graph> |
| 53 | 53 |
struct Arc { typedef typename Graph::Arc type; };
|
| 54 | 54 |
|
| 55 | 55 |
template <typename Graph> |
| 56 | 56 |
struct ArcIt { typedef typename Graph::ArcIt type; };
|
| 57 | 57 |
|
| 58 | 58 |
template <typename Graph> |
| 59 | 59 |
struct Edge { typedef typename Graph::Edge type; };
|
| 60 | 60 |
|
| 61 | 61 |
template <typename Graph> |
| 62 | 62 |
struct EdgeIt { typedef typename Graph::EdgeIt type; };
|
| 63 | 63 |
|
| 64 | 64 |
template <typename Graph> |
| 65 | 65 |
struct OutArcIt { typedef typename Graph::OutArcIt type; };
|
| 66 | 66 |
|
| 67 | 67 |
template <typename Graph> |
| 68 | 68 |
struct InArcIt { typedef typename Graph::InArcIt type; };
|
| 69 | 69 |
|
| 70 | 70 |
template <typename Graph> |
| 71 | 71 |
struct IncEdgeIt { typedef typename Graph::IncEdgeIt type; };
|
| 72 | 72 |
|
| 73 | 73 |
template <typename Graph> |
| 74 | 74 |
struct BoolNodeMap {
|
| 75 | 75 |
typedef typename Graph::template NodeMap<bool> type; |
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
template <typename Graph> |
| 79 | 79 |
struct IntNodeMap {
|
| 80 | 80 |
typedef typename Graph::template NodeMap<int> type; |
| 81 | 81 |
}; |
| 82 | 82 |
|
| 83 | 83 |
template <typename Graph> |
| 84 | 84 |
struct DoubleNodeMap {
|
| 85 | 85 |
typedef typename Graph::template NodeMap<double> type; |
| 86 | 86 |
}; |
| 87 | 87 |
|
| 88 | 88 |
template <typename Graph> |
| 89 | 89 |
struct BoolArcMap {
|
| 90 | 90 |
typedef typename Graph::template ArcMap<bool> type; |
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
template <typename Graph> |
| 94 | 94 |
struct IntArcMap {
|
| 95 | 95 |
typedef typename Graph::template ArcMap<int> type; |
| 96 | 96 |
}; |
| 97 | 97 |
|
| 98 | 98 |
template <typename Graph> |
| 99 | 99 |
struct DoubleArcMap {
|
| 100 | 100 |
typedef typename Graph::template ArcMap<double> type; |
| 101 | 101 |
}; |
| 102 | 102 |
|
| 103 | 103 |
template <typename Graph> |
| 104 | 104 |
struct BoolEdgeMap {
|
| 105 | 105 |
typedef typename Graph::template EdgeMap<bool> type; |
| 106 | 106 |
}; |
| 107 | 107 |
|
| 108 | 108 |
template <typename Graph> |
| 109 | 109 |
struct IntEdgeMap {
|
| 110 | 110 |
typedef typename Graph::template EdgeMap<int> type; |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
template <typename Graph> |
| 114 | 114 |
struct DoubleEdgeMap {
|
| 115 | 115 |
typedef typename Graph::template EdgeMap<double> type; |
| 116 | 116 |
}; |
| 117 | 117 |
|
| 118 | 118 |
|
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
///Creates convenience typedefs for the digraph types and iterators |
| 122 | 122 |
|
| 123 | 123 |
///This \c \#define creates convenience typedefs for the following types |
| 124 | 124 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
| 125 | 125 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
| 126 | 126 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
| 127 | 127 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
| 128 | 128 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 129 | 129 |
Node<Digraph>::type Node; \ |
| 130 | 130 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 131 | 131 |
NodeIt<Digraph>::type NodeIt; \ |
| 132 | 132 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 133 | 133 |
Arc<Digraph>::type Arc; \ |
| 134 | 134 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 135 | 135 |
ArcIt<Digraph>::type ArcIt; \ |
| 136 | 136 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 137 | 137 |
OutArcIt<Digraph>::type OutArcIt; \ |
| 138 | 138 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 139 | 139 |
InArcIt<Digraph>::type InArcIt; \ |
| 140 | 140 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 141 | 141 |
BoolNodeMap<Digraph>::type BoolNodeMap; \ |
| 142 | 142 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 143 | 143 |
IntNodeMap<Digraph>::type IntNodeMap; \ |
| 144 | 144 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 145 | 145 |
DoubleNodeMap<Digraph>::type DoubleNodeMap; \ |
| 146 | 146 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 147 | 147 |
BoolArcMap<Digraph>::type BoolArcMap; \ |
| 148 | 148 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 149 | 149 |
IntArcMap<Digraph>::type IntArcMap; \ |
| 150 | 150 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 151 | 151 |
DoubleArcMap<Digraph>::type DoubleArcMap |
| 152 | 152 |
|
| 153 | 153 |
|
| 154 | 154 |
///Creates convenience typedefs for the graph types and iterators |
| 155 | 155 |
|
| 156 | 156 |
///This \c \#define creates the same convenience typedefs as defined |
| 157 | 157 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
| 158 | 158 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
| 159 | 159 |
///\c DoubleEdgeMap. |
| 160 | 160 |
#define GRAPH_TYPEDEFS(Graph) \ |
| 161 | 161 |
DIGRAPH_TYPEDEFS(Graph); \ |
| 162 | 162 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 163 | 163 |
Edge<Graph>::type Edge; \ |
| 164 | 164 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 165 | 165 |
EdgeIt<Graph>::type EdgeIt; \ |
| 166 | 166 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 167 |
IncEdgeIt<Graph>::type IncEdgeIt \ |
|
| 167 |
IncEdgeIt<Graph>::type IncEdgeIt; \ |
|
| 168 | 168 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 169 | 169 |
BoolEdgeMap<Graph>::type BoolEdgeMap; \ |
| 170 | 170 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 171 | 171 |
IntEdgeMap<Graph>::type IntEdgeMap; \ |
| 172 | 172 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 173 | 173 |
DoubleEdgeMap<Graph>::type DoubleEdgeMap |
| 174 | 174 |
|
| 175 | 175 |
|
| 176 | 176 |
/// \brief Function to count the items in the graph. |
| 177 | 177 |
/// |
| 178 | 178 |
/// This function counts the items (nodes, arcs etc) in the graph. |
| 179 | 179 |
/// The complexity of the function is O(n) because |
| 180 | 180 |
/// it iterates on all of the items. |
| 181 | 181 |
template <typename Graph, typename Item> |
| 182 | 182 |
inline int countItems(const Graph& g) {
|
| 183 | 183 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
| 184 | 184 |
int num = 0; |
| 185 | 185 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 186 | 186 |
++num; |
| 187 | 187 |
} |
| 188 | 188 |
return num; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
// Node counting: |
| 192 | 192 |
|
| 193 | 193 |
namespace _graph_utils_bits {
|
| 194 | 194 |
|
| 195 | 195 |
template <typename Graph, typename Enable = void> |
| 196 | 196 |
struct CountNodesSelector {
|
| 197 | 197 |
static int count(const Graph &g) {
|
| 198 | 198 |
return countItems<Graph, typename Graph::Node>(g); |
| 199 | 199 |
} |
| 200 | 200 |
}; |
| 201 | 201 |
|
| 202 | 202 |
template <typename Graph> |
| 203 | 203 |
struct CountNodesSelector< |
| 204 | 204 |
Graph, typename |
| 205 | 205 |
enable_if<typename Graph::NodeNumTag, void>::type> |
| 206 | 206 |
{
|
| 207 | 207 |
static int count(const Graph &g) {
|
| 208 | 208 |
return g.nodeNum(); |
| 209 | 209 |
} |
| 210 | 210 |
}; |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
/// \brief Function to count the nodes in the graph. |
| 214 | 214 |
/// |
| 215 | 215 |
/// This function counts the nodes in the graph. |
| 216 | 216 |
/// The complexity of the function is O(n) but for some |
| 217 | 217 |
/// graph structures it is specialized to run in O(1). |
| 218 | 218 |
/// |
| 219 | 219 |
/// If the graph contains a \e nodeNum() member function and a |
| 220 | 220 |
/// \e NodeNumTag tag then this function calls directly the member |
| 221 | 221 |
/// function to query the cardinality of the node set. |
| 222 | 222 |
template <typename Graph> |
| 223 | 223 |
inline int countNodes(const Graph& g) {
|
| 224 | 224 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
// Arc counting: |
| 228 | 228 |
|
| 229 | 229 |
namespace _graph_utils_bits {
|
| 230 | 230 |
|
| 231 | 231 |
template <typename Graph, typename Enable = void> |
| 232 | 232 |
struct CountArcsSelector {
|
| 233 | 233 |
static int count(const Graph &g) {
|
| 234 | 234 |
return countItems<Graph, typename Graph::Arc>(g); |
| 235 | 235 |
} |
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
template <typename Graph> |
| 239 | 239 |
struct CountArcsSelector< |
| 240 | 240 |
Graph, |
| 241 | 241 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
| 242 | 242 |
{
|
| 243 | 243 |
static int count(const Graph &g) {
|
| 244 | 244 |
return g.arcNum(); |
| 245 | 245 |
} |
| 246 | 246 |
}; |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
/// \brief Function to count the arcs in the graph. |
| 250 | 250 |
/// |
| 251 | 251 |
/// This function counts the arcs in the graph. |
| 252 | 252 |
/// The complexity of the function is O(e) but for some |
| 253 | 253 |
/// graph structures it is specialized to run in O(1). |
| 254 | 254 |
/// |
| 255 | 255 |
/// If the graph contains a \e arcNum() member function and a |
| 256 | 256 |
/// \e EdgeNumTag tag then this function calls directly the member |
| 257 | 257 |
/// function to query the cardinality of the arc set. |
| 258 | 258 |
template <typename Graph> |
| 259 | 259 |
inline int countArcs(const Graph& g) {
|
| 260 | 260 |
return _graph_utils_bits::CountArcsSelector<Graph>::count(g); |
| 261 | 261 |
} |
| 262 | 262 |
|
| 263 | 263 |
// Edge counting: |
| 264 | 264 |
namespace _graph_utils_bits {
|
| 265 | 265 |
|
| 266 | 266 |
template <typename Graph, typename Enable = void> |
| 267 | 267 |
struct CountEdgesSelector {
|
| 268 | 268 |
static int count(const Graph &g) {
|
| 269 | 269 |
return countItems<Graph, typename Graph::Edge>(g); |
| 270 | 270 |
} |
| 271 | 271 |
}; |
| 272 | 272 |
|
| 273 | 273 |
template <typename Graph> |
| 274 | 274 |
struct CountEdgesSelector< |
| 275 | 275 |
Graph, |
| 276 | 276 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
| 277 | 277 |
{
|
| 278 | 278 |
static int count(const Graph &g) {
|
| 279 | 279 |
return g.edgeNum(); |
| 280 | 280 |
} |
| 281 | 281 |
}; |
| 282 | 282 |
} |
| 283 | 283 |
|
| 284 | 284 |
/// \brief Function to count the edges in the graph. |
| 285 | 285 |
/// |
| 286 | 286 |
/// This function counts the edges in the graph. |
| 287 | 287 |
/// The complexity of the function is O(m) but for some |
| 288 | 288 |
/// graph structures it is specialized to run in O(1). |
| 289 | 289 |
/// |
| 290 | 290 |
/// If the graph contains a \e edgeNum() member function and a |
| 291 | 291 |
/// \e EdgeNumTag tag then this function calls directly the member |
| 292 | 292 |
/// function to query the cardinality of the edge set. |
| 293 | 293 |
template <typename Graph> |
| 294 | 294 |
inline int countEdges(const Graph& g) {
|
| 295 | 295 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g); |
| 296 | 296 |
|
| 297 | 297 |
} |
| 298 | 298 |
|
| 299 | 299 |
|
| 300 | 300 |
template <typename Graph, typename DegIt> |
| 301 | 301 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
| 302 | 302 |
int num = 0; |
| 303 | 303 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
| 304 | 304 |
++num; |
| 305 | 305 |
} |
| 306 | 306 |
return num; |
| 307 | 307 |
} |
| 308 | 308 |
|
| 309 | 309 |
/// \brief Function to count the number of the out-arcs from node \c n. |
| 310 | 310 |
/// |
| 311 | 311 |
/// This function counts the number of the out-arcs from node \c n |
| 312 | 312 |
/// in the graph. |
| 313 | 313 |
template <typename Graph> |
| 314 | 314 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) {
|
| 315 | 315 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
| 316 | 316 |
} |
| 317 | 317 |
|
| 318 | 318 |
/// \brief Function to count the number of the in-arcs to node \c n. |
| 319 | 319 |
/// |
| 320 | 320 |
/// This function counts the number of the in-arcs to node \c n |
| 321 | 321 |
/// in the graph. |
| 322 | 322 |
template <typename Graph> |
| 323 | 323 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) {
|
| 324 | 324 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
| 325 | 325 |
} |
| 326 | 326 |
|
| 327 | 327 |
/// \brief Function to count the number of the inc-edges to node \c n. |
| 328 | 328 |
/// |
| 329 | 329 |
/// This function counts the number of the inc-edges to node \c n |
| 330 | 330 |
/// in the graph. |
| 331 | 331 |
template <typename Graph> |
| 332 | 332 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
|
| 333 | 333 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
| 334 | 334 |
} |
| 335 | 335 |
|
| 336 | 336 |
namespace _graph_utils_bits {
|
| 337 | 337 |
|
| 338 | 338 |
template <typename Graph, typename Enable = void> |
| 339 | 339 |
struct FindArcSelector {
|
| 340 | 340 |
typedef typename Graph::Node Node; |
| 341 | 341 |
typedef typename Graph::Arc Arc; |
| 342 | 342 |
static Arc find(const Graph &g, Node u, Node v, Arc e) {
|
| 343 | 343 |
if (e == INVALID) {
|
| 344 | 344 |
g.firstOut(e, u); |
| 345 | 345 |
} else {
|
| 346 | 346 |
g.nextOut(e); |
| 347 | 347 |
} |
| 348 | 348 |
while (e != INVALID && g.target(e) != v) {
|
| 349 | 349 |
g.nextOut(e); |
| 350 | 350 |
} |
| 351 | 351 |
return e; |
| 352 | 352 |
} |
| 353 | 353 |
}; |
| 354 | 354 |
|
| 355 | 355 |
template <typename Graph> |
| 356 | 356 |
struct FindArcSelector< |
| 357 | 357 |
Graph, |
| 358 | 358 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
| 359 | 359 |
{
|
| 360 | 360 |
typedef typename Graph::Node Node; |
| 361 | 361 |
typedef typename Graph::Arc Arc; |
| 362 | 362 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
| 363 | 363 |
return g.findArc(u, v, prev); |
| 364 | 364 |
} |
| 365 | 365 |
}; |
| 366 | 366 |
} |
| 367 | 367 |
|
| 368 | 368 |
/// \brief Finds an arc between two nodes of a graph. |
| 369 | 369 |
/// |
| 370 | 370 |
/// Finds an arc from node \c u to node \c v in graph \c g. |
| 371 | 371 |
/// |
| 372 | 372 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 373 | 373 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
| 374 | 374 |
/// the next arc from \c u to \c v after \c prev. |
| 375 | 375 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
| 376 | 376 |
/// |
| 377 | 377 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
| 378 | 378 |
///\code |
| 379 | 379 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
|
| 380 | 380 |
/// ... |
| 381 | 381 |
/// } |
| 382 | 382 |
///\endcode |
| 383 | 383 |
/// |
| 384 | 384 |
///\sa ArcLookUp |
| 385 | 385 |
///\sa AllArcLookUp |
| 386 | 386 |
///\sa DynArcLookUp |
| 387 | 387 |
///\sa ConArcIt |
| 388 | 388 |
template <typename Graph> |
| 389 | 389 |
inline typename Graph::Arc |
| 390 | 390 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 391 | 391 |
typename Graph::Arc prev = INVALID) {
|
| 392 | 392 |
return _graph_utils_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
| 393 | 393 |
} |
| 394 | 394 |
|
| 395 | 395 |
/// \brief Iterator for iterating on arcs connected the same nodes. |
| 396 | 396 |
/// |
| 397 | 397 |
/// Iterator for iterating on arcs connected the same nodes. It is |
| 398 | 398 |
/// higher level interface for the findArc() function. You can |
| 399 | 399 |
/// use it the following way: |
| 400 | 400 |
///\code |
| 401 | 401 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 402 | 402 |
/// ... |
| 403 | 403 |
/// } |
| 404 | 404 |
///\endcode |
| 405 | 405 |
/// |
| 406 | 406 |
///\sa findArc() |
| 407 | 407 |
///\sa ArcLookUp |
| 408 | 408 |
///\sa AllArcLookUp |
| 409 | 409 |
///\sa DynArcLookUp |
| 410 | 410 |
/// |
| 411 | 411 |
/// \author Balazs Dezso |
| 412 | 412 |
template <typename _Graph> |
| 413 | 413 |
class ConArcIt : public _Graph::Arc {
|
| 414 | 414 |
public: |
| 415 | 415 |
|
| 416 | 416 |
typedef _Graph Graph; |
| 417 | 417 |
typedef typename Graph::Arc Parent; |
| 418 | 418 |
|
| 419 | 419 |
typedef typename Graph::Arc Arc; |
| 420 | 420 |
typedef typename Graph::Node Node; |
| 421 | 421 |
|
| 422 | 422 |
/// \brief Constructor. |
| 423 | 423 |
/// |
| 424 | 424 |
/// Construct a new ConArcIt iterating on the arcs which |
| 425 | 425 |
/// connects the \c u and \c v node. |
| 426 | 426 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 427 | 427 |
Parent::operator=(findArc(_graph, u, v)); |
| 428 | 428 |
} |
| 429 | 429 |
|
| 430 | 430 |
/// \brief Constructor. |
| 431 | 431 |
/// |
| 432 | 432 |
/// Construct a new ConArcIt which continues the iterating from |
| 433 | 433 |
/// the \c e arc. |
| 434 | 434 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
|
| 435 | 435 |
|
| 436 | 436 |
/// \brief Increment operator. |
| 437 | 437 |
/// |
| 438 | 438 |
/// It increments the iterator and gives back the next arc. |
| 439 | 439 |
ConArcIt& operator++() {
|
| 440 | 440 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
| 441 | 441 |
_graph.target(*this), *this)); |
| 442 | 442 |
return *this; |
| 443 | 443 |
} |
| 444 | 444 |
private: |
| 445 | 445 |
const Graph& _graph; |
| 446 | 446 |
}; |
| 447 | 447 |
|
| 448 | 448 |
namespace _graph_utils_bits {
|
| 449 | 449 |
|
| 450 | 450 |
template <typename Graph, typename Enable = void> |
| 451 | 451 |
struct FindEdgeSelector {
|
| 452 | 452 |
typedef typename Graph::Node Node; |
| 453 | 453 |
typedef typename Graph::Edge Edge; |
| 454 | 454 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
| 455 | 455 |
bool b; |
| 456 | 456 |
if (u != v) {
|
| 457 | 457 |
if (e == INVALID) {
|
| 458 | 458 |
g.firstInc(e, b, u); |
| 459 | 459 |
} else {
|
| 460 | 460 |
b = g.source(e) == u; |
| 461 | 461 |
g.nextInc(e, b); |
| 462 | 462 |
} |
| 463 | 463 |
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
|
| 464 | 464 |
g.nextInc(e, b); |
| 465 | 465 |
} |
| 466 | 466 |
} else {
|
| 467 | 467 |
if (e == INVALID) {
|
| 468 | 468 |
g.firstInc(e, b, u); |
| 469 | 469 |
} else {
|
| 470 | 470 |
b = true; |
| 471 | 471 |
g.nextInc(e, b); |
| 472 | 472 |
} |
| 473 | 473 |
while (e != INVALID && (!b || g.target(e) != v)) {
|
| 474 | 474 |
g.nextInc(e, b); |
| 475 | 475 |
} |
| 476 | 476 |
} |
| 477 | 477 |
return e; |
| 478 | 478 |
} |
| 479 | 479 |
}; |
| 480 | 480 |
|
| 481 | 481 |
template <typename Graph> |
| 482 | 482 |
struct FindEdgeSelector< |
| 483 | 483 |
Graph, |
| 484 | 484 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
| 485 | 485 |
{
|
| 486 | 486 |
typedef typename Graph::Node Node; |
| 487 | 487 |
typedef typename Graph::Edge Edge; |
| 488 | 488 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
| 489 | 489 |
return g.findEdge(u, v, prev); |
| 490 | 490 |
} |
| 491 | 491 |
}; |
| 492 | 492 |
} |
| 493 | 493 |
|
| 494 | 494 |
/// \brief Finds an edge between two nodes of a graph. |
| 495 | 495 |
/// |
| 496 | 496 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
| 497 | 497 |
/// If the node \c u and node \c v is equal then each loop edge |
| 498 | 498 |
/// will be enumerated once. |
| 499 | 499 |
/// |
| 500 | 500 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 501 | 501 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
| 502 | 502 |
/// the next arc from \c u to \c v after \c prev. |
| 503 | 503 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
| 504 | 504 |
/// |
| 505 | 505 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
| 506 | 506 |
///\code |
| 507 | 507 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
| 508 | 508 |
/// e = findEdge(g,u,v,e)) {
|
| 509 | 509 |
/// ... |
| 510 | 510 |
/// } |
| 511 | 511 |
///\endcode |
| 512 | 512 |
/// |
| 513 | 513 |
///\sa ConArcIt |
| 514 | 514 |
|
| 515 | 515 |
template <typename Graph> |
| 516 | 516 |
inline typename Graph::Edge |
| 517 | 517 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 518 | 518 |
typename Graph::Edge p = INVALID) {
|
| 519 | 519 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
| 520 | 520 |
} |
| 521 | 521 |
|
| 522 | 522 |
/// \brief Iterator for iterating on edges connected the same nodes. |
| 523 | 523 |
/// |
| 524 | 524 |
/// Iterator for iterating on edges connected the same nodes. It is |
| 525 | 525 |
/// higher level interface for the findEdge() function. You can |
| 526 | 526 |
/// use it the following way: |
| 527 | 527 |
///\code |
| 528 | 528 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 529 | 529 |
/// ... |
| 530 | 530 |
/// } |
| 531 | 531 |
///\endcode |
| 532 | 532 |
/// |
| 533 | 533 |
///\sa findEdge() |
| 534 | 534 |
/// |
| 535 | 535 |
/// \author Balazs Dezso |
| 536 | 536 |
template <typename _Graph> |
| 537 | 537 |
class ConEdgeIt : public _Graph::Edge {
|
| 538 | 538 |
public: |
| 539 | 539 |
|
| 540 | 540 |
typedef _Graph Graph; |
| 541 | 541 |
typedef typename Graph::Edge Parent; |
| 542 | 542 |
|
| 543 | 543 |
typedef typename Graph::Edge Edge; |
| 544 | 544 |
typedef typename Graph::Node Node; |
| 545 | 545 |
|
| 546 | 546 |
/// \brief Constructor. |
| 547 | 547 |
/// |
| 548 | 548 |
/// Construct a new ConEdgeIt iterating on the edges which |
| 549 | 549 |
/// connects the \c u and \c v node. |
| 550 | 550 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 551 | 551 |
Parent::operator=(findEdge(_graph, u, v)); |
| 552 | 552 |
} |
| 553 | 553 |
|
| 554 | 554 |
/// \brief Constructor. |
| 555 | 555 |
/// |
| 556 | 556 |
/// Construct a new ConEdgeIt which continues the iterating from |
| 557 | 557 |
/// the \c e edge. |
| 558 | 558 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
|
| 559 | 559 |
|
| 560 | 560 |
/// \brief Increment operator. |
| 561 | 561 |
/// |
| 562 | 562 |
/// It increments the iterator and gives back the next edge. |
| 563 | 563 |
ConEdgeIt& operator++() {
|
| 564 | 564 |
Parent::operator=(findEdge(_graph, _graph.source(*this), |
| 565 | 565 |
_graph.target(*this), *this)); |
| 566 | 566 |
return *this; |
| 567 | 567 |
} |
| 568 | 568 |
private: |
| 569 | 569 |
const Graph& _graph; |
| 570 | 570 |
}; |
| 571 | 571 |
|
| 572 | 572 |
namespace _graph_utils_bits {
|
| 573 | 573 |
|
| 574 | 574 |
template <typename Digraph, typename Item, typename RefMap> |
| 575 | 575 |
class MapCopyBase {
|
| 576 | 576 |
public: |
| 577 | 577 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
| 578 | 578 |
|
| 579 | 579 |
virtual ~MapCopyBase() {}
|
| 580 | 580 |
}; |
| 581 | 581 |
|
| 582 | 582 |
template <typename Digraph, typename Item, typename RefMap, |
| 583 | 583 |
typename ToMap, typename FromMap> |
| 584 | 584 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 585 | 585 |
public: |
| 586 | 586 |
|
| 587 | 587 |
MapCopy(ToMap& tmap, const FromMap& map) |
| 588 | 588 |
: _tmap(tmap), _map(map) {}
|
| 589 | 589 |
|
| 590 | 590 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 591 | 591 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 592 | 592 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 593 | 593 |
_tmap.set(refMap[it], _map[it]); |
| 594 | 594 |
} |
| 595 | 595 |
} |
| 596 | 596 |
|
| 597 | 597 |
private: |
| 598 | 598 |
ToMap& _tmap; |
| 599 | 599 |
const FromMap& _map; |
| 600 | 600 |
}; |
| 601 | 601 |
|
| 602 | 602 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
| 603 | 603 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 604 | 604 |
public: |
| 605 | 605 |
|
| 606 | 606 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
|
| 607 | 607 |
|
| 608 | 608 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
| 609 | 609 |
_it = refMap[_item]; |
| 610 | 610 |
} |
| 611 | 611 |
|
| 612 | 612 |
private: |
| 613 | 613 |
It& _it; |
| 614 | 614 |
Item _item; |
| 615 | 615 |
}; |
| 616 | 616 |
|
| 617 | 617 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
| 618 | 618 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 619 | 619 |
public: |
| 620 | 620 |
|
| 621 | 621 |
RefCopy(Ref& map) : _map(map) {}
|
| 622 | 622 |
|
| 623 | 623 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 624 | 624 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 625 | 625 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 626 | 626 |
_map.set(it, refMap[it]); |
| 627 | 627 |
} |
| 628 | 628 |
} |
| 629 | 629 |
|
| 630 | 630 |
private: |
| 631 | 631 |
Ref& _map; |
| 632 | 632 |
}; |
| 633 | 633 |
|
| 634 | 634 |
template <typename Digraph, typename Item, typename RefMap, |
| 635 | 635 |
typename CrossRef> |
| 636 | 636 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 637 | 637 |
public: |
| 638 | 638 |
|
| 639 | 639 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
| 640 | 640 |
|
| 641 | 641 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 642 | 642 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 643 | 643 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 644 | 644 |
_cmap.set(refMap[it], it); |
| 645 | 645 |
} |
| 646 | 646 |
} |
| 647 | 647 |
|
| 648 | 648 |
private: |
| 649 | 649 |
CrossRef& _cmap; |
| 650 | 650 |
}; |
| 651 | 651 |
|
| 652 | 652 |
template <typename Digraph, typename Enable = void> |
| 653 | 653 |
struct DigraphCopySelector {
|
| 654 | 654 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 655 | 655 |
static void copy(Digraph &to, const From& from, |
| 656 | 656 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 657 | 657 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 658 | 658 |
nodeRefMap[it] = to.addNode(); |
| 659 | 659 |
} |
| 660 | 660 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
| 661 | 661 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
| 662 | 662 |
nodeRefMap[from.target(it)]); |
| 663 | 663 |
} |
| 664 | 664 |
} |
| 665 | 665 |
}; |
| 666 | 666 |
|
| 667 | 667 |
template <typename Digraph> |
| 668 | 668 |
struct DigraphCopySelector< |
| 669 | 669 |
Digraph, |
| 670 | 670 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
| 671 | 671 |
{
|
| 672 | 672 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 673 | 673 |
static void copy(Digraph &to, const From& from, |
| 674 | 674 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 675 | 675 |
to.build(from, nodeRefMap, arcRefMap); |
| 676 | 676 |
} |
| 677 | 677 |
}; |
| 678 | 678 |
|
| 679 | 679 |
template <typename Graph, typename Enable = void> |
| 680 | 680 |
struct GraphCopySelector {
|
| 681 | 681 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 682 | 682 |
static void copy(Graph &to, const From& from, |
| 683 | 683 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 684 | 684 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 685 | 685 |
nodeRefMap[it] = to.addNode(); |
| 686 | 686 |
} |
| 687 | 687 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
| 688 | 688 |
edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
| 689 | 689 |
nodeRefMap[from.target(it)]); |
| 690 | 690 |
} |
| 691 | 691 |
} |
| 692 | 692 |
}; |
| 693 | 693 |
|
| 694 | 694 |
template <typename Graph> |
| 695 | 695 |
struct GraphCopySelector< |
| 696 | 696 |
Graph, |
| 697 | 697 |
typename enable_if<typename Graph::BuildTag, void>::type> |
| 698 | 698 |
{
|
| 699 | 699 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 700 | 700 |
static void copy(Graph &to, const From& from, |
| 701 | 701 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 702 | 702 |
to.build(from, nodeRefMap, edgeRefMap); |
| 703 | 703 |
} |
| 704 | 704 |
}; |
| 705 | 705 |
|
| 706 | 706 |
} |
| 707 | 707 |
|
| 708 | 708 |
/// \brief Class to copy a digraph. |
| 709 | 709 |
/// |
| 710 | 710 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
| 711 | 711 |
/// simplest way of using it is through the \c copyDigraph() function. |
| 712 | 712 |
/// |
| 713 | 713 |
/// This class not just make a copy of a graph, but it can create |
| 714 | 714 |
/// references and cross references between the nodes and arcs of |
| 715 | 715 |
/// the two graphs, it can copy maps for use with the newly created |
| 716 | 716 |
/// graph and copy nodes and arcs. |
| 717 | 717 |
/// |
| 718 | 718 |
/// To make a copy from a graph, first an instance of DigraphCopy |
| 719 | 719 |
/// should be created, then the data belongs to the graph should |
| 720 | 720 |
/// assigned to copy. In the end, the \c run() member should be |
| 721 | 721 |
/// called. |
| 722 | 722 |
/// |
| 723 | 723 |
/// The next code copies a graph with several data: |
| 724 | 724 |
///\code |
| 725 | 725 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
| 726 | 726 |
/// // create a reference for the nodes |
| 727 | 727 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
| 728 | 728 |
/// dc.nodeRef(nr); |
| 729 | 729 |
/// // create a cross reference (inverse) for the arcs |
| 730 | 730 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
| 731 | 731 |
/// dc.arcCrossRef(acr); |
| 732 | 732 |
/// // copy an arc map |
| 733 | 733 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
| 734 | 734 |
/// NewGraph::ArcMap<double> namap(new_graph); |
| 735 | 735 |
/// dc.arcMap(namap, oamap); |
| 736 | 736 |
/// // copy a node |
| 737 | 737 |
/// OrigGraph::Node on; |
| 738 | 738 |
/// NewGraph::Node nn; |
| 739 | 739 |
/// dc.node(nn, on); |
| 740 | 740 |
/// // Executions of copy |
| 741 | 741 |
/// dc.run(); |
| 742 | 742 |
///\endcode |
| 743 | 743 |
template <typename To, typename From> |
| 744 | 744 |
class DigraphCopy {
|
| 745 | 745 |
private: |
| 746 | 746 |
|
| 747 | 747 |
typedef typename From::Node Node; |
| 748 | 748 |
typedef typename From::NodeIt NodeIt; |
| 749 | 749 |
typedef typename From::Arc Arc; |
| 750 | 750 |
typedef typename From::ArcIt ArcIt; |
| 751 | 751 |
|
| 752 | 752 |
typedef typename To::Node TNode; |
| 753 | 753 |
typedef typename To::Arc TArc; |
| 754 | 754 |
|
| 755 | 755 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
| 756 | 756 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
| 757 | 757 |
|
| 758 | 758 |
|
| 759 | 759 |
public: |
| 760 | 760 |
|
| 761 | 761 |
|
| 762 | 762 |
/// \brief Constructor for the DigraphCopy. |
| 763 | 763 |
/// |
| 764 | 764 |
/// It copies the content of the \c _from digraph into the |
| 765 | 765 |
/// \c _to digraph. |
| 766 | 766 |
DigraphCopy(To& to, const From& from) |
| 767 | 767 |
: _from(from), _to(to) {}
|
| 768 | 768 |
|
| 769 | 769 |
/// \brief Destructor of the DigraphCopy |
| 770 | 770 |
/// |
| 771 | 771 |
/// Destructor of the DigraphCopy |
| 772 | 772 |
~DigraphCopy() {
|
| 773 | 773 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 774 | 774 |
delete _node_maps[i]; |
| 775 | 775 |
} |
| 776 | 776 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 777 | 777 |
delete _arc_maps[i]; |
| 778 | 778 |
} |
| 779 | 779 |
|
| 780 | 780 |
} |
| 781 | 781 |
|
| 782 | 782 |
/// \brief Copies the node references into the given map. |
| 783 | 783 |
/// |
| 784 | 784 |
/// Copies the node references into the given map. The parameter |
| 785 | 785 |
/// should be a map, which key type is the Node type of the source |
| 786 | 786 |
/// graph, while the value type is the Node type of the |
| 787 | 787 |
/// destination graph. |
| 788 | 788 |
template <typename NodeRef> |
| 789 | 789 |
DigraphCopy& nodeRef(NodeRef& map) {
|
| 790 | 790 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
| 791 | 791 |
NodeRefMap, NodeRef>(map)); |
| 792 | 792 |
return *this; |
| 793 | 793 |
} |
| 794 | 794 |
|
| 795 | 795 |
/// \brief Copies the node cross references into the given map. |
| 796 | 796 |
/// |
| 797 | 797 |
/// Copies the node cross references (reverse references) into |
| 798 | 798 |
/// the given map. The parameter should be a map, which key type |
| 799 | 799 |
/// is the Node type of the destination graph, while the value type is |
| 800 | 800 |
/// the Node type of the source graph. |
| 801 | 801 |
template <typename NodeCrossRef> |
| 802 | 802 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 803 | 803 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
| 804 | 804 |
NodeRefMap, NodeCrossRef>(map)); |
| 805 | 805 |
return *this; |
| 806 | 806 |
} |
| 807 | 807 |
|
| 808 | 808 |
/// \brief Make copy of the given map. |
| 809 | 809 |
/// |
| 810 | 810 |
/// Makes copy of the given map for the newly created digraph. |
| 811 | 811 |
/// The new map's key type is the destination graph's node type, |
| 812 | 812 |
/// and the copied map's key type is the source graph's node type. |
| 813 | 813 |
template <typename ToMap, typename FromMap> |
| 814 | 814 |
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
|
| 815 | 815 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
| 816 | 816 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
| 817 | 817 |
return *this; |
| 818 | 818 |
} |
| 819 | 819 |
|
| 820 | 820 |
/// \brief Make a copy of the given node. |
| 821 | 821 |
/// |
| 822 | 822 |
/// Make a copy of the given node. |
| 823 | 823 |
DigraphCopy& node(TNode& tnode, const Node& snode) {
|
| 824 | 824 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
| 825 | 825 |
NodeRefMap, TNode>(tnode, snode)); |
| 826 | 826 |
return *this; |
| 827 | 827 |
} |
| 828 | 828 |
|
| 829 | 829 |
/// \brief Copies the arc references into the given map. |
| 830 | 830 |
/// |
| 831 | 831 |
/// Copies the arc references into the given map. |
| 832 | 832 |
template <typename ArcRef> |
| 833 | 833 |
DigraphCopy& arcRef(ArcRef& map) {
|
| 834 | 834 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
| 835 | 835 |
ArcRefMap, ArcRef>(map)); |
| 836 | 836 |
return *this; |
| 837 | 837 |
} |
| 838 | 838 |
|
| 839 | 839 |
/// \brief Copies the arc cross references into the given map. |
| 840 | 840 |
/// |
| 841 | 841 |
/// Copies the arc cross references (reverse references) into |
| 842 | 842 |
/// the given map. |
| 843 | 843 |
template <typename ArcCrossRef> |
| 844 | 844 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 845 | 845 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
| 846 | 846 |
ArcRefMap, ArcCrossRef>(map)); |
| 847 | 847 |
return *this; |
| 848 | 848 |
} |
| 849 | 849 |
|
| 850 | 850 |
/// \brief Make copy of the given map. |
| 851 | 851 |
/// |
| 852 | 852 |
/// Makes copy of the given map for the newly created digraph. |
| 853 | 853 |
/// The new map's key type is the to digraph's arc type, |
| 854 | 854 |
/// and the copied map's key type is the from digraph's arc |
| 855 | 855 |
/// type. |
| 856 | 856 |
template <typename ToMap, typename FromMap> |
| 857 | 857 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
|
| 858 | 858 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
| 859 | 859 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
| 860 | 860 |
return *this; |
| 861 | 861 |
} |
| 862 | 862 |
|
| 863 | 863 |
/// \brief Make a copy of the given arc. |
| 864 | 864 |
/// |
| 865 | 865 |
/// Make a copy of the given arc. |
| 866 | 866 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
|
| 867 | 867 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
| 868 | 868 |
ArcRefMap, TArc>(tarc, sarc)); |
| 869 | 869 |
return *this; |
| 870 | 870 |
} |
| 871 | 871 |
|
| 872 | 872 |
/// \brief Executes the copies. |
| 873 | 873 |
/// |
| 874 | 874 |
/// Executes the copies. |
| 875 | 875 |
void run() {
|
| 876 | 876 |
NodeRefMap nodeRefMap(_from); |
| 877 | 877 |
ArcRefMap arcRefMap(_from); |
| 878 | 878 |
_graph_utils_bits::DigraphCopySelector<To>:: |
| 879 | 879 |
copy(_to, _from, nodeRefMap, arcRefMap); |
| 880 | 880 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 881 | 881 |
_node_maps[i]->copy(_from, nodeRefMap); |
| 882 | 882 |
} |
| 883 | 883 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 884 | 884 |
_arc_maps[i]->copy(_from, arcRefMap); |
| 885 | 885 |
} |
| 886 | 886 |
} |
| 887 | 887 |
|
| 888 | 888 |
protected: |
| 889 | 889 |
|
| 890 | 890 |
|
| 891 | 891 |
const From& _from; |
| 892 | 892 |
To& _to; |
| 893 | 893 |
|
| 894 | 894 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
| 895 | 895 |
_node_maps; |
| 896 | 896 |
|
| 897 | 897 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
| 898 | 898 |
_arc_maps; |
| 899 | 899 |
|
| 900 | 900 |
}; |
| 901 | 901 |
|
| 902 | 902 |
/// \brief Copy a digraph to another digraph. |
| 903 | 903 |
/// |
| 904 | 904 |
/// Copy a digraph to another digraph. The complete usage of the |
| 905 | 905 |
/// function is detailed in the DigraphCopy class, but a short |
| 906 | 906 |
/// example shows a basic work: |
| 907 | 907 |
///\code |
| 908 | 908 |
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
| 909 | 909 |
///\endcode |
| 910 | 910 |
/// |
| 911 | 911 |
/// After the copy the \c nr map will contain the mapping from the |
| 912 | 912 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
| 913 | 913 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
| 914 | 914 |
/// to the arcs of the \c from digraph. |
| 915 | 915 |
/// |
| 916 | 916 |
/// \see DigraphCopy |
| 917 | 917 |
template <typename To, typename From> |
| 918 | 918 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
|
| 919 | 919 |
return DigraphCopy<To, From>(to, from); |
| 920 | 920 |
} |
| 921 | 921 |
|
| 922 | 922 |
/// \brief Class to copy a graph. |
| 923 | 923 |
/// |
| 924 | 924 |
/// Class to copy a graph to another graph (duplicate a graph). The |
| 925 | 925 |
/// simplest way of using it is through the \c copyGraph() function. |
| 926 | 926 |
/// |
| 927 | 927 |
/// This class not just make a copy of a graph, but it can create |
| 928 | 928 |
/// references and cross references between the nodes, edges and arcs of |
| 929 | 929 |
/// the two graphs, it can copy maps for use with the newly created |
| 930 | 930 |
/// graph and copy nodes, edges and arcs. |
| 931 | 931 |
/// |
| 932 | 932 |
/// To make a copy from a graph, first an instance of GraphCopy |
| 933 | 933 |
/// should be created, then the data belongs to the graph should |
| 934 | 934 |
/// assigned to copy. In the end, the \c run() member should be |
| 935 | 935 |
/// called. |
| 936 | 936 |
/// |
| 937 | 937 |
/// The next code copies a graph with several data: |
| 938 | 938 |
///\code |
| 939 | 939 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
| 940 | 940 |
/// // create a reference for the nodes |
| 941 | 941 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
| 942 | 942 |
/// dc.nodeRef(nr); |
| 943 | 943 |
/// // create a cross reference (inverse) for the edges |
| 944 | 944 |
/// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph); |
| 945 | 945 |
/// dc.edgeCrossRef(ecr); |
| 946 | 946 |
/// // copy an arc map |
| 947 | 947 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
| 948 | 948 |
/// NewGraph::ArcMap<double> namap(new_graph); |
| 949 | 949 |
/// dc.arcMap(namap, oamap); |
| 950 | 950 |
/// // copy a node |
| 951 | 951 |
/// OrigGraph::Node on; |
| 952 | 952 |
/// NewGraph::Node nn; |
| 953 | 953 |
/// dc.node(nn, on); |
| 954 | 954 |
/// // Executions of copy |
| 955 | 955 |
/// dc.run(); |
| 956 | 956 |
///\endcode |
| 957 | 957 |
template <typename To, typename From> |
| 958 | 958 |
class GraphCopy {
|
| 959 | 959 |
private: |
| 960 | 960 |
|
| 961 | 961 |
typedef typename From::Node Node; |
| 962 | 962 |
typedef typename From::NodeIt NodeIt; |
| 963 | 963 |
typedef typename From::Arc Arc; |
| 964 | 964 |
typedef typename From::ArcIt ArcIt; |
| 965 | 965 |
typedef typename From::Edge Edge; |
| 966 | 966 |
typedef typename From::EdgeIt EdgeIt; |
| 967 | 967 |
|
| 968 | 968 |
typedef typename To::Node TNode; |
| 969 | 969 |
typedef typename To::Arc TArc; |
| 970 | 970 |
typedef typename To::Edge TEdge; |
| 971 | 971 |
|
| 972 | 972 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
| 973 | 973 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
| 974 | 974 |
|
| 975 | 975 |
struct ArcRefMap {
|
| 976 | 976 |
ArcRefMap(const To& to, const From& from, |
| 977 | 977 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
| 978 | 978 |
: _to(to), _from(from), |
| 979 | 979 |
_edge_ref(edge_ref), _node_ref(node_ref) {}
|
| 980 | 980 |
|
| 981 | 981 |
typedef typename From::Arc Key; |
| 982 | 982 |
typedef typename To::Arc Value; |
| 983 | 983 |
|
| 984 | 984 |
Value operator[](const Key& key) const {
|
| 985 | 985 |
bool forward = |
| 986 | 986 |
(_from.direction(key) == |
| 987 | 987 |
(_node_ref[_from.source(key)] == _to.source(_edge_ref[key]))); |
| 988 | 988 |
return _to.direct(_edge_ref[key], forward); |
| 989 | 989 |
} |
| 990 | 990 |
|
| 991 | 991 |
const To& _to; |
| 992 | 992 |
const From& _from; |
| 993 | 993 |
const EdgeRefMap& _edge_ref; |
| 994 | 994 |
const NodeRefMap& _node_ref; |
| 995 | 995 |
}; |
| 996 | 996 |
|
| 997 | 997 |
|
| 998 | 998 |
public: |
| 999 | 999 |
|
| 1000 | 1000 |
|
| 1001 | 1001 |
/// \brief Constructor for the GraphCopy. |
| 1002 | 1002 |
/// |
| 1003 | 1003 |
/// It copies the content of the \c _from graph into the |
| 1004 | 1004 |
/// \c _to graph. |
| 1005 | 1005 |
GraphCopy(To& to, const From& from) |
| 1006 | 1006 |
: _from(from), _to(to) {}
|
| 1007 | 1007 |
|
| 1008 | 1008 |
/// \brief Destructor of the GraphCopy |
| 1009 | 1009 |
/// |
| 1010 | 1010 |
/// Destructor of the GraphCopy |
| 1011 | 1011 |
~GraphCopy() {
|
| 1012 | 1012 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 1013 | 1013 |
delete _node_maps[i]; |
| 1014 | 1014 |
} |
| 1015 | 1015 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 1016 | 1016 |
delete _arc_maps[i]; |
| 1017 | 1017 |
} |
| 1018 | 1018 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 1019 | 1019 |
delete _edge_maps[i]; |
| 1020 | 1020 |
} |
| 1021 | 1021 |
|
| 1022 | 1022 |
} |
| 1023 | 1023 |
|
| 1024 | 1024 |
/// \brief Copies the node references into the given map. |
| 1025 | 1025 |
/// |
| 1026 | 1026 |
/// Copies the node references into the given map. |
| 1027 | 1027 |
template <typename NodeRef> |
| 1028 | 1028 |
GraphCopy& nodeRef(NodeRef& map) {
|
| 1029 | 1029 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
| 1030 | 1030 |
NodeRefMap, NodeRef>(map)); |
| 1031 | 1031 |
return *this; |
| 1032 | 1032 |
} |
| 1033 | 1033 |
|
| 1034 | 1034 |
/// \brief Copies the node cross references into the given map. |
| 1035 | 1035 |
/// |
| 1036 | 1036 |
/// Copies the node cross references (reverse references) into |
| 1037 | 1037 |
/// the given map. |
| 1038 | 1038 |
template <typename NodeCrossRef> |
| 1039 | 1039 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 1040 | 1040 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
| 1041 | 1041 |
NodeRefMap, NodeCrossRef>(map)); |
| 1042 | 1042 |
return *this; |
| 1043 | 1043 |
} |
| 1044 | 1044 |
|
| 1045 | 1045 |
/// \brief Make copy of the given map. |
| 1046 | 1046 |
/// |
| 1047 | 1047 |
/// Makes copy of the given map for the newly created graph. |
| 1048 | 1048 |
/// The new map's key type is the to graph's node type, |
| 1049 | 1049 |
/// and the copied map's key type is the from graph's node |
| 1050 | 1050 |
/// type. |
| 1051 | 1051 |
template <typename ToMap, typename FromMap> |
| 1052 | 1052 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
|
| 1053 | 1053 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
| 1054 | 1054 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
| 1055 | 1055 |
return *this; |
| 1056 | 1056 |
} |
| 1057 | 1057 |
|
| 1058 | 1058 |
/// \brief Make a copy of the given node. |
| 1059 | 1059 |
/// |
| 1060 | 1060 |
/// Make a copy of the given node. |
| 1061 | 1061 |
GraphCopy& node(TNode& tnode, const Node& snode) {
|
| 1062 | 1062 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
| 1063 | 1063 |
NodeRefMap, TNode>(tnode, snode)); |
| 1064 | 1064 |
return *this; |
| 1065 | 1065 |
} |
| 1066 | 1066 |
|
| 1067 | 1067 |
/// \brief Copies the arc references into the given map. |
| 1068 | 1068 |
/// |
| 1069 | 1069 |
/// Copies the arc references into the given map. |
| 1070 | 1070 |
template <typename ArcRef> |
| 1071 | 1071 |
GraphCopy& arcRef(ArcRef& map) {
|
| 1072 | 1072 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
| 1073 | 1073 |
ArcRefMap, ArcRef>(map)); |
| 1074 | 1074 |
return *this; |
| 1075 | 1075 |
} |
| 1076 | 1076 |
|
| 1077 | 1077 |
/// \brief Copies the arc cross references into the given map. |
| 1078 | 1078 |
/// |
| 1079 | 1079 |
/// Copies the arc cross references (reverse references) into |
| 1080 | 1080 |
/// the given map. |
| 1081 | 1081 |
template <typename ArcCrossRef> |
| 1082 | 1082 |
GraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 1083 | 1083 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
| 1084 | 1084 |
ArcRefMap, ArcCrossRef>(map)); |
| 1085 | 1085 |
return *this; |
| 1086 | 1086 |
} |
| 1087 | 1087 |
|
| 1088 | 1088 |
/// \brief Make copy of the given map. |
| 1089 | 1089 |
/// |
| 1090 | 1090 |
/// Makes copy of the given map for the newly created graph. |
| 1091 | 1091 |
/// The new map's key type is the to graph's arc type, |
| 1092 | 1092 |
/// and the copied map's key type is the from graph's arc |
| 1093 | 1093 |
/// type. |
| 1094 | 1094 |
template <typename ToMap, typename FromMap> |
| 1095 | 1095 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
|
| 1096 | 1096 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
| 1097 | 1097 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
| 1098 | 1098 |
return *this; |
| 1099 | 1099 |
} |
| 1100 | 1100 |
|
| 1101 | 1101 |
/// \brief Make a copy of the given arc. |
| 1102 | 1102 |
/// |
| 1103 | 1103 |
/// Make a copy of the given arc. |
| 1104 | 1104 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) {
|
| 1105 | 1105 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
| 1106 | 1106 |
ArcRefMap, TArc>(tarc, sarc)); |
| 1107 | 1107 |
return *this; |
| 1108 | 1108 |
} |
| 1109 | 1109 |
|
| 1110 | 1110 |
/// \brief Copies the edge references into the given map. |
| 1111 | 1111 |
/// |
| 1112 | 1112 |
/// Copies the edge references into the given map. |
| 1113 | 1113 |
template <typename EdgeRef> |
| 1114 | 1114 |
GraphCopy& edgeRef(EdgeRef& map) {
|
| 1115 | 1115 |
_edge_maps.push_back(new _graph_utils_bits::RefCopy<From, Edge, |
| 1116 | 1116 |
EdgeRefMap, EdgeRef>(map)); |
| 1117 | 1117 |
return *this; |
| 1118 | 1118 |
} |
| 1119 | 1119 |
|
| 1120 | 1120 |
/// \brief Copies the edge cross references into the given map. |
| 1121 | 1121 |
/// |
| 1122 | 1122 |
/// Copies the edge cross references (reverse |
| 1123 | 1123 |
/// references) into the given map. |
| 1124 | 1124 |
template <typename EdgeCrossRef> |
| 1125 | 1125 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
| 1126 | 1126 |
_edge_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, |
| 1127 | 1127 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
| 1128 | 1128 |
return *this; |
| 1129 | 1129 |
} |
| 1130 | 1130 |
|
| 1131 | 1131 |
/// \brief Make copy of the given map. |
| 1132 | 1132 |
/// |
| 1133 | 1133 |
/// Makes copy of the given map for the newly created graph. |
| 1134 | 1134 |
/// The new map's key type is the to graph's edge type, |
| 1135 | 1135 |
/// and the copied map's key type is the from graph's edge |
| 1136 | 1136 |
/// type. |
| 1137 | 1137 |
template <typename ToMap, typename FromMap> |
| 1138 | 1138 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
|
| 1139 | 1139 |
_edge_maps.push_back(new _graph_utils_bits::MapCopy<From, Edge, |
| 1140 | 1140 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
| 1141 | 1141 |
return *this; |
| 1142 | 1142 |
} |
| 1143 | 1143 |
|
| 1144 | 1144 |
/// \brief Make a copy of the given edge. |
| 1145 | 1145 |
/// |
| 1146 | 1146 |
/// Make a copy of the given edge. |
| 1147 | 1147 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
|
| 1148 | 1148 |
_edge_maps.push_back(new _graph_utils_bits::ItemCopy<From, Edge, |
| 1149 | 1149 |
EdgeRefMap, TEdge>(tedge, sedge)); |
| 1150 | 1150 |
return *this; |
| 1151 | 1151 |
} |
| 1152 | 1152 |
|
| 1153 | 1153 |
/// \brief Executes the copies. |
| 1154 | 1154 |
/// |
| 1155 | 1155 |
/// Executes the copies. |
| 1156 | 1156 |
void run() {
|
| 1157 | 1157 |
NodeRefMap nodeRefMap(_from); |
| 1158 | 1158 |
EdgeRefMap edgeRefMap(_from); |
| 1159 | 1159 |
ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap); |
| 1160 | 1160 |
_graph_utils_bits::GraphCopySelector<To>:: |
| 1161 | 1161 |
copy(_to, _from, nodeRefMap, edgeRefMap); |
| 1162 | 1162 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 1163 | 1163 |
_node_maps[i]->copy(_from, nodeRefMap); |
| 1164 | 1164 |
} |
| 1165 | 1165 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 1166 | 1166 |
_edge_maps[i]->copy(_from, edgeRefMap); |
| 1167 | 1167 |
} |
| 1168 | 1168 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 1169 | 1169 |
_arc_maps[i]->copy(_from, arcRefMap); |
| 1170 | 1170 |
} |
| 1171 | 1171 |
} |
| 1172 | 1172 |
|
| 1173 | 1173 |
private: |
| 1174 | 1174 |
|
| 1175 | 1175 |
const From& _from; |
| 1176 | 1176 |
To& _to; |
| 1177 | 1177 |
|
| 1178 | 1178 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
| 1179 | 1179 |
_node_maps; |
| 1180 | 1180 |
|
| 1181 | 1181 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
| 1182 | 1182 |
_arc_maps; |
| 1183 | 1183 |
|
| 1184 | 1184 |
std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
| 1185 | 1185 |
_edge_maps; |
| 1186 | 1186 |
|
| 1187 | 1187 |
}; |
| 1188 | 1188 |
|
| 1189 | 1189 |
/// \brief Copy a graph to another graph. |
| 1190 | 1190 |
/// |
| 1191 | 1191 |
/// Copy a graph to another graph. The complete usage of the |
| 1192 | 1192 |
/// function is detailed in the GraphCopy class, but a short |
| 1193 | 1193 |
/// example shows a basic work: |
| 1194 | 1194 |
///\code |
| 1195 | 1195 |
/// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
| 1196 | 1196 |
///\endcode |
| 1197 | 1197 |
/// |
| 1198 | 1198 |
/// After the copy the \c nr map will contain the mapping from the |
| 1199 | 1199 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
| 1200 | 1200 |
/// \c ecr will contain the mapping from the arcs of the \c to graph |
| 1201 | 1201 |
/// to the arcs of the \c from graph. |
| 1202 | 1202 |
/// |
| 1203 | 1203 |
/// \see GraphCopy |
| 1204 | 1204 |
template <typename To, typename From> |
| 1205 | 1205 |
GraphCopy<To, From> |
| 1206 | 1206 |
copyGraph(To& to, const From& from) {
|
| 1207 | 1207 |
return GraphCopy<To, From>(to, from); |
| 1208 | 1208 |
} |
| 1209 | 1209 |
|
| 1210 | 1210 |
/// @} |
| 1211 | 1211 |
|
| 1212 | 1212 |
/// \addtogroup graph_maps |
| 1213 | 1213 |
/// @{
|
| 1214 | 1214 |
|
| 1215 | 1215 |
/// Provides an immutable and unique id for each item in the graph. |
| 1216 | 1216 |
|
| 1217 | 1217 |
/// The IdMap class provides a unique and immutable id for each item of the |
| 1218 | 1218 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
| 1219 | 1219 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
| 1220 | 1220 |
/// item (node) does not change (even if you delete other nodes). </ul> |
| 1221 | 1221 |
/// Through this map you get access (i.e. can read) the inner id values of |
| 1222 | 1222 |
/// the items stored in the graph. This map can be inverted with its member |
| 1223 | 1223 |
/// class \c InverseMap or with the \c operator() member. |
| 1224 | 1224 |
/// |
| 1225 | 1225 |
template <typename _Graph, typename _Item> |
| 1226 | 1226 |
class IdMap {
|
| 1227 | 1227 |
public: |
| 1228 | 1228 |
typedef _Graph Graph; |
| 1229 | 1229 |
typedef int Value; |
| 1230 | 1230 |
typedef _Item Item; |
| 1231 | 1231 |
typedef _Item Key; |
| 1232 | 1232 |
|
| 1233 | 1233 |
/// \brief Constructor. |
| 1234 | 1234 |
/// |
| 1235 | 1235 |
/// Constructor of the map. |
| 1236 | 1236 |
explicit IdMap(const Graph& graph) : _graph(&graph) {}
|
| 1237 | 1237 |
|
| 1238 | 1238 |
/// \brief Gives back the \e id of the item. |
| 1239 | 1239 |
/// |
| 1240 | 1240 |
/// Gives back the immutable and unique \e id of the item. |
| 1241 | 1241 |
int operator[](const Item& item) const { return _graph->id(item);}
|
| 1242 | 1242 |
|
| 1243 | 1243 |
/// \brief Gives back the item by its id. |
| 1244 | 1244 |
/// |
| 1245 | 1245 |
/// Gives back the item by its id. |
| 1246 | 1246 |
Item operator()(int id) { return _graph->fromId(id, Item()); }
|
| 1247 | 1247 |
|
| 1248 | 1248 |
private: |
| 1249 | 1249 |
const Graph* _graph; |
| 1250 | 1250 |
|
| 1251 | 1251 |
public: |
| 1252 | 1252 |
|
| 1253 | 1253 |
/// \brief The class represents the inverse of its owner (IdMap). |
| 1254 | 1254 |
/// |
| 1255 | 1255 |
/// The class represents the inverse of its owner (IdMap). |
| 1256 | 1256 |
/// \see inverse() |
| 1257 | 1257 |
class InverseMap {
|
| 1258 | 1258 |
public: |
| 1259 | 1259 |
|
| 1260 | 1260 |
/// \brief Constructor. |
| 1261 | 1261 |
/// |
| 1262 | 1262 |
/// Constructor for creating an id-to-item map. |
| 1263 | 1263 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
|
| 1264 | 1264 |
|
| 1265 | 1265 |
/// \brief Constructor. |
| 1266 | 1266 |
/// |
| 1267 | 1267 |
/// Constructor for creating an id-to-item map. |
| 1268 | 1268 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
|
| 1269 | 1269 |
|
| 1270 | 1270 |
/// \brief Gives back the given item from its id. |
| 1271 | 1271 |
/// |
| 1272 | 1272 |
/// Gives back the given item from its id. |
| 1273 | 1273 |
/// |
| 1274 | 1274 |
Item operator[](int id) const { return _graph->fromId(id, Item());}
|
| 1275 | 1275 |
|
| 1276 | 1276 |
private: |
| 1277 | 1277 |
const Graph* _graph; |
| 1278 | 1278 |
}; |
| 1279 | 1279 |
|
| 1280 | 1280 |
/// \brief Gives back the inverse of the map. |
| 1281 | 1281 |
/// |
| 1282 | 1282 |
/// Gives back the inverse of the IdMap. |
| 1283 | 1283 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
| 1284 | 1284 |
|
| 1285 | 1285 |
}; |
| 1286 | 1286 |
|
| 1287 | 1287 |
|
| 1288 | 1288 |
/// \brief General invertable graph-map type. |
| 1289 | 1289 |
|
| 1290 | 1290 |
/// This type provides simple invertable graph-maps. |
| 1291 | 1291 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
| 1292 | 1292 |
/// and if a key is set to a new value then store it |
| 1293 | 1293 |
/// in the inverse map. |
| 1294 | 1294 |
/// |
| 1295 | 1295 |
/// The values of the map can be accessed |
| 1296 | 1296 |
/// with stl compatible forward iterator. |
| 1297 | 1297 |
/// |
| 1298 | 1298 |
/// \param _Graph The graph type. |
| 1299 | 1299 |
/// \param _Item The item type of the graph. |
| 1300 | 1300 |
/// \param _Value The value type of the map. |
| 1301 | 1301 |
/// |
| 1302 | 1302 |
/// \see IterableValueMap |
| 1303 | 1303 |
template <typename _Graph, typename _Item, typename _Value> |
| 1304 | 1304 |
class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
|
| 1305 | 1305 |
private: |
| 1306 | 1306 |
|
| 1307 | 1307 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
| 1308 | 1308 |
typedef _Graph Graph; |
| 1309 | 1309 |
|
| 1310 | 1310 |
typedef std::map<_Value, _Item> Container; |
| 1311 | 1311 |
Container _inv_map; |
| 1312 | 1312 |
|
| 1313 | 1313 |
public: |
| 1314 | 1314 |
|
| 1315 | 1315 |
/// The key type of InvertableMap (Node, Arc, Edge). |
| 1316 | 1316 |
typedef typename Map::Key Key; |
| 1317 | 1317 |
/// The value type of the InvertableMap. |
| 1318 | 1318 |
typedef typename Map::Value Value; |
| 1319 | 1319 |
|
| 1320 | 1320 |
|
| 1321 | 1321 |
|
| 1322 | 1322 |
/// \brief Constructor. |
| 1323 | 1323 |
/// |
| 1324 | 1324 |
/// Construct a new InvertableMap for the graph. |
| 1325 | 1325 |
/// |
| 1326 | 1326 |
explicit InvertableMap(const Graph& graph) : Map(graph) {}
|
| 1327 | 1327 |
|
| 1328 | 1328 |
/// \brief Forward iterator for values. |
| 1329 | 1329 |
/// |
| 1330 | 1330 |
/// This iterator is an stl compatible forward |
| 1331 | 1331 |
/// iterator on the values of the map. The values can |
| 1332 | 1332 |
/// be accessed in the [beginValue, endValue) range. |
| 1333 | 1333 |
/// |
| 1334 | 1334 |
class ValueIterator |
| 1335 | 1335 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 1336 | 1336 |
friend class InvertableMap; |
| 1337 | 1337 |
private: |
| 1338 | 1338 |
ValueIterator(typename Container::const_iterator _it) |
| 1339 | 1339 |
: it(_it) {}
|
| 1340 | 1340 |
public: |
| 1341 | 1341 |
|
| 1342 | 1342 |
ValueIterator() {}
|
| 1343 | 1343 |
|
| 1344 | 1344 |
ValueIterator& operator++() { ++it; return *this; }
|
| 1345 | 1345 |
ValueIterator operator++(int) {
|
| 1346 | 1346 |
ValueIterator tmp(*this); |
| 1347 | 1347 |
operator++(); |
| 1348 | 1348 |
return tmp; |
| 1349 | 1349 |
} |
| 1350 | 1350 |
|
| 1351 | 1351 |
const Value& operator*() const { return it->first; }
|
| 1352 | 1352 |
const Value* operator->() const { return &(it->first); }
|
| 1353 | 1353 |
|
| 1354 | 1354 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
| 1355 | 1355 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
| 1356 | 1356 |
|
| 1357 | 1357 |
private: |
| 1358 | 1358 |
typename Container::const_iterator it; |
| 1359 | 1359 |
}; |
| 1360 | 1360 |
|
| 1361 | 1361 |
/// \brief Returns an iterator to the first value. |
| 1362 | 1362 |
/// |
| 1363 | 1363 |
/// Returns an stl compatible iterator to the |
| 1364 | 1364 |
/// first value of the map. The values of the |
| 1365 | 1365 |
/// map can be accessed in the [beginValue, endValue) |
| 1366 | 1366 |
/// range. |
| 1367 | 1367 |
ValueIterator beginValue() const {
|
| 1368 | 1368 |
return ValueIterator(_inv_map.begin()); |
| 1369 | 1369 |
} |
| 1370 | 1370 |
|
| 1371 | 1371 |
/// \brief Returns an iterator after the last value. |
| 1372 | 1372 |
/// |
| 1373 | 1373 |
/// Returns an stl compatible iterator after the |
| 1374 | 1374 |
/// last value of the map. The values of the |
| 1375 | 1375 |
/// map can be accessed in the [beginValue, endValue) |
| 1376 | 1376 |
/// range. |
| 1377 | 1377 |
ValueIterator endValue() const {
|
| 1378 | 1378 |
return ValueIterator(_inv_map.end()); |
| 1379 | 1379 |
} |
| 1380 | 1380 |
|
| 1381 | 1381 |
/// \brief The setter function of the map. |
| 1382 | 1382 |
/// |
| 1383 | 1383 |
/// Sets the mapped value. |
| 1384 | 1384 |
void set(const Key& key, const Value& val) {
|
| 1385 | 1385 |
Value oldval = Map::operator[](key); |
| 1386 | 1386 |
typename Container::iterator it = _inv_map.find(oldval); |
| 1387 | 1387 |
if (it != _inv_map.end() && it->second == key) {
|
| 1388 | 1388 |
_inv_map.erase(it); |
| 1389 | 1389 |
} |
| 1390 | 1390 |
_inv_map.insert(make_pair(val, key)); |
| 1391 | 1391 |
Map::set(key, val); |
| 1392 | 1392 |
} |
| 1393 | 1393 |
|
| 1394 | 1394 |
/// \brief The getter function of the map. |
| 1395 | 1395 |
/// |
| 1396 | 1396 |
/// It gives back the value associated with the key. |
| 1397 | 1397 |
typename MapTraits<Map>::ConstReturnValue |
| 1398 | 1398 |
operator[](const Key& key) const {
|
| 1399 | 1399 |
return Map::operator[](key); |
| 1400 | 1400 |
} |
| 1401 | 1401 |
|
| 1402 | 1402 |
/// \brief Gives back the item by its value. |
| 1403 | 1403 |
/// |
| 1404 | 1404 |
/// Gives back the item by its value. |
| 1405 | 1405 |
Key operator()(const Value& key) const {
|
| 1406 | 1406 |
typename Container::const_iterator it = _inv_map.find(key); |
| 1407 | 1407 |
return it != _inv_map.end() ? it->second : INVALID; |
| 1408 | 1408 |
} |
| 1409 | 1409 |
|
| 1410 | 1410 |
protected: |
| 1411 | 1411 |
|
| 1412 | 1412 |
/// \brief Erase the key from the map. |
| 1413 | 1413 |
/// |
| 1414 | 1414 |
/// Erase the key to the map. It is called by the |
| 1415 | 1415 |
/// \c AlterationNotifier. |
| 1416 | 1416 |
virtual void erase(const Key& key) {
|
| 1417 | 1417 |
Value val = Map::operator[](key); |
| 1418 | 1418 |
typename Container::iterator it = _inv_map.find(val); |
| 1419 | 1419 |
if (it != _inv_map.end() && it->second == key) {
|
| 1420 | 1420 |
_inv_map.erase(it); |
| 1421 | 1421 |
} |
| 1422 | 1422 |
Map::erase(key); |
| 1423 | 1423 |
} |
| 1424 | 1424 |
|
| 1425 | 1425 |
/// \brief Erase more keys from the map. |
| 1426 | 1426 |
/// |
| 1427 | 1427 |
/// Erase more keys from the map. It is called by the |
| 1428 | 1428 |
/// \c AlterationNotifier. |
| 1429 | 1429 |
virtual void erase(const std::vector<Key>& keys) {
|
| 1430 | 1430 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 1431 | 1431 |
Value val = Map::operator[](keys[i]); |
| 1432 | 1432 |
typename Container::iterator it = _inv_map.find(val); |
| 1433 | 1433 |
if (it != _inv_map.end() && it->second == keys[i]) {
|
| 1434 | 1434 |
_inv_map.erase(it); |
| 1435 | 1435 |
} |
| 1436 | 1436 |
} |
| 1437 | 1437 |
Map::erase(keys); |
| 1438 | 1438 |
} |
| 1439 | 1439 |
|
| 1440 | 1440 |
/// \brief Clear the keys from the map and inverse map. |
| 1441 | 1441 |
/// |
| 1442 | 1442 |
/// Clear the keys from the map and inverse map. It is called by the |
| 1443 | 1443 |
/// \c AlterationNotifier. |
| 1444 | 1444 |
virtual void clear() {
|
| 1445 | 1445 |
_inv_map.clear(); |
| 1446 | 1446 |
Map::clear(); |
| 1447 | 1447 |
} |
| 1448 | 1448 |
|
| 1449 | 1449 |
public: |
| 1450 | 1450 |
|
| 1451 | 1451 |
/// \brief The inverse map type. |
| 1452 | 1452 |
/// |
| 1453 | 1453 |
/// The inverse of this map. The subscript operator of the map |
| 1454 | 1454 |
/// gives back always the item what was last assigned to the value. |
| 1455 | 1455 |
class InverseMap {
|
| 1456 | 1456 |
public: |
| 1457 | 1457 |
/// \brief Constructor of the InverseMap. |
| 1458 | 1458 |
/// |
| 1459 | 1459 |
/// Constructor of the InverseMap. |
| 1460 | 1460 |
explicit InverseMap(const InvertableMap& inverted) |
| 1461 | 1461 |
: _inverted(inverted) {}
|
| 1462 | 1462 |
|
| 1463 | 1463 |
/// The value type of the InverseMap. |
| 1464 | 1464 |
typedef typename InvertableMap::Key Value; |
| 1465 | 1465 |
/// The key type of the InverseMap. |
| 1466 | 1466 |
typedef typename InvertableMap::Value Key; |
| 1467 | 1467 |
|
| 1468 | 1468 |
/// \brief Subscript operator. |
| 1469 | 1469 |
/// |
| 1470 | 1470 |
/// Subscript operator. It gives back always the item |
| 1471 | 1471 |
/// what was last assigned to the value. |
| 1472 | 1472 |
Value operator[](const Key& key) const {
|
| 1473 | 1473 |
return _inverted(key); |
| 1474 | 1474 |
} |
| 1475 | 1475 |
|
| 1476 | 1476 |
private: |
| 1477 | 1477 |
const InvertableMap& _inverted; |
| 1478 | 1478 |
}; |
| 1479 | 1479 |
|
| 1480 | 1480 |
/// \brief It gives back the just readable inverse map. |
| 1481 | 1481 |
/// |
| 1482 | 1482 |
/// It gives back the just readable inverse map. |
| 1483 | 1483 |
InverseMap inverse() const {
|
| 1484 | 1484 |
return InverseMap(*this); |
| 1485 | 1485 |
} |
| 1486 | 1486 |
|
| 1487 | 1487 |
|
| 1488 | 1488 |
|
| 1489 | 1489 |
}; |
| 1490 | 1490 |
|
| 1491 | 1491 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
| 1492 | 1492 |
/// item in the graph. |
| 1493 | 1493 |
/// |
| 1494 | 1494 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
| 1495 | 1495 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
| 1496 | 1496 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
| 1497 | 1497 |
/// different ids <li>\b continuous: the range of the ids is the set of |
| 1498 | 1498 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
| 1499 | 1499 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
| 1500 | 1500 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
| 1501 | 1501 |
/// with its member class \c InverseMap, or with the \c operator() member. |
| 1502 | 1502 |
/// |
| 1503 | 1503 |
/// \param _Graph The graph class the \c DescriptorMap belongs to. |
| 1504 | 1504 |
/// \param _Item The Item is the Key of the Map. It may be Node, Arc or |
| 1505 | 1505 |
/// Edge. |
| 1506 | 1506 |
template <typename _Graph, typename _Item> |
| 1507 | 1507 |
class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
|
| 1508 | 1508 |
|
| 1509 | 1509 |
typedef _Item Item; |
| 1510 | 1510 |
typedef DefaultMap<_Graph, _Item, int> Map; |
| 1511 | 1511 |
|
| 1512 | 1512 |
public: |
| 1513 | 1513 |
/// The graph class of DescriptorMap. |
| 1514 | 1514 |
typedef _Graph Graph; |
| 1515 | 1515 |
|
| 1516 | 1516 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
| 1517 | 1517 |
typedef typename Map::Key Key; |
| 1518 | 1518 |
/// The value type of DescriptorMap. |
| 1519 | 1519 |
typedef typename Map::Value Value; |
| 1520 | 1520 |
|
| 1521 | 1521 |
/// \brief Constructor. |
| 1522 | 1522 |
/// |
| 1523 | 1523 |
/// Constructor for descriptor map. |
| 1524 | 1524 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
|
| 1525 | 1525 |
Item it; |
| 1526 | 1526 |
const typename Map::Notifier* nf = Map::notifier(); |
| 1527 | 1527 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1528 | 1528 |
Map::set(it, _inv_map.size()); |
| 1529 | 1529 |
_inv_map.push_back(it); |
| 1530 | 1530 |
} |
| 1531 | 1531 |
} |
| 1532 | 1532 |
|
| 1533 | 1533 |
protected: |
| 1534 | 1534 |
|
| 1535 | 1535 |
/// \brief Add a new key to the map. |
| 1536 | 1536 |
/// |
| 1537 | 1537 |
/// Add a new key to the map. It is called by the |
| 1538 | 1538 |
/// \c AlterationNotifier. |
| 1539 | 1539 |
virtual void add(const Item& item) {
|
| 1540 | 1540 |
Map::add(item); |
| 1541 | 1541 |
Map::set(item, _inv_map.size()); |
| 1542 | 1542 |
_inv_map.push_back(item); |
| 1543 | 1543 |
} |
| 1544 | 1544 |
|
| 1545 | 1545 |
/// \brief Add more new keys to the map. |
| 1546 | 1546 |
/// |
| 1547 | 1547 |
/// Add more new keys to the map. It is called by the |
| 1548 | 1548 |
/// \c AlterationNotifier. |
| 1549 | 1549 |
virtual void add(const std::vector<Item>& items) {
|
| 1550 | 1550 |
Map::add(items); |
| 1551 | 1551 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 1552 | 1552 |
Map::set(items[i], _inv_map.size()); |
| 1553 | 1553 |
_inv_map.push_back(items[i]); |
| 1554 | 1554 |
} |
| 1555 | 1555 |
} |
| 1556 | 1556 |
|
| 1557 | 1557 |
/// \brief Erase the key from the map. |
| 1558 | 1558 |
/// |
| 1559 | 1559 |
/// Erase the key from the map. It is called by the |
| 1560 | 1560 |
/// \c AlterationNotifier. |
| 1561 | 1561 |
virtual void erase(const Item& item) {
|
| 1562 | 1562 |
Map::set(_inv_map.back(), Map::operator[](item)); |
| 1563 | 1563 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
| 1564 | 1564 |
_inv_map.pop_back(); |
| 1565 | 1565 |
Map::erase(item); |
| 1566 | 1566 |
} |
| 1567 | 1567 |
|
| 1568 | 1568 |
/// \brief Erase more keys from the map. |
| 1569 | 1569 |
/// |
| 1570 | 1570 |
/// Erase more keys from the map. It is called by the |
| 1571 | 1571 |
/// \c AlterationNotifier. |
| 1572 | 1572 |
virtual void erase(const std::vector<Item>& items) {
|
| 1573 | 1573 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 1574 | 1574 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
| 1575 | 1575 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
| 1576 | 1576 |
_inv_map.pop_back(); |
| 1577 | 1577 |
} |
| 1578 | 1578 |
Map::erase(items); |
| 1579 | 1579 |
} |
| 1580 | 1580 |
|
| 1581 | 1581 |
/// \brief Build the unique map. |
| 1582 | 1582 |
/// |
| 1583 | 1583 |
/// Build the unique map. It is called by the |
| 1584 | 1584 |
/// \c AlterationNotifier. |
| 1585 | 1585 |
virtual void build() {
|
| 1586 | 1586 |
Map::build(); |
| 1587 | 1587 |
Item it; |
| 1588 | 1588 |
const typename Map::Notifier* nf = Map::notifier(); |
| 1589 | 1589 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1590 | 1590 |
Map::set(it, _inv_map.size()); |
| 1591 | 1591 |
_inv_map.push_back(it); |
| 1592 | 1592 |
} |
| 1593 | 1593 |
} |
| 1594 | 1594 |
|
| 1595 | 1595 |
/// \brief Clear the keys from the map. |
| 1596 | 1596 |
/// |
| 1597 | 1597 |
/// Clear the keys from the map. It is called by the |
| 1598 | 1598 |
/// \c AlterationNotifier. |
| 1599 | 1599 |
virtual void clear() {
|
| 1600 | 1600 |
_inv_map.clear(); |
| 1601 | 1601 |
Map::clear(); |
| 1602 | 1602 |
} |
| 1603 | 1603 |
|
| 1604 | 1604 |
public: |
| 1605 | 1605 |
|
| 1606 | 1606 |
/// \brief Returns the maximal value plus one. |
| 1607 | 1607 |
/// |
| 1608 | 1608 |
/// Returns the maximal value plus one in the map. |
| 1609 | 1609 |
unsigned int size() const {
|
| 1610 | 1610 |
return _inv_map.size(); |
| 1611 | 1611 |
} |
| 1612 | 1612 |
|
| 1613 | 1613 |
/// \brief Swaps the position of the two items in the map. |
| 1614 | 1614 |
/// |
| 1615 | 1615 |
/// Swaps the position of the two items in the map. |
| 1616 | 1616 |
void swap(const Item& p, const Item& q) {
|
| 1617 | 1617 |
int pi = Map::operator[](p); |
| 1618 | 1618 |
int qi = Map::operator[](q); |
| 1619 | 1619 |
Map::set(p, qi); |
| 1620 | 1620 |
_inv_map[qi] = p; |
| 1621 | 1621 |
Map::set(q, pi); |
| 1622 | 1622 |
_inv_map[pi] = q; |
| 1623 | 1623 |
} |
| 1624 | 1624 |
|
| 1625 | 1625 |
/// \brief Gives back the \e descriptor of the item. |
| 1626 | 1626 |
/// |
| 1627 | 1627 |
/// Gives back the mutable and unique \e descriptor of the map. |
| 1628 | 1628 |
int operator[](const Item& item) const {
|
| 1629 | 1629 |
return Map::operator[](item); |
| 1630 | 1630 |
} |
| 1631 | 1631 |
|
| 1632 | 1632 |
/// \brief Gives back the item by its descriptor. |
| 1633 | 1633 |
/// |
| 1634 | 1634 |
/// Gives back th item by its descriptor. |
| 1635 | 1635 |
Item operator()(int id) const {
|
| 1636 | 1636 |
return _inv_map[id]; |
| 1637 | 1637 |
} |
| 1638 | 1638 |
|
| 1639 | 1639 |
private: |
| 1640 | 1640 |
|
| 1641 | 1641 |
typedef std::vector<Item> Container; |
| 1642 | 1642 |
Container _inv_map; |
| 1643 | 1643 |
|
| 1644 | 1644 |
public: |
| 1645 | 1645 |
/// \brief The inverse map type of DescriptorMap. |
| 1646 | 1646 |
/// |
| 1647 | 1647 |
/// The inverse map type of DescriptorMap. |
| 1648 | 1648 |
class InverseMap {
|
| 1649 | 1649 |
public: |
| 1650 | 1650 |
/// \brief Constructor of the InverseMap. |
| 1651 | 1651 |
/// |
| 1652 | 1652 |
/// Constructor of the InverseMap. |
| 1653 | 1653 |
explicit InverseMap(const DescriptorMap& inverted) |
| 1654 | 1654 |
: _inverted(inverted) {}
|
| 1655 | 1655 |
|
| 1656 | 1656 |
|
| 1657 | 1657 |
/// The value type of the InverseMap. |
| 1658 | 1658 |
typedef typename DescriptorMap::Key Value; |
| 1659 | 1659 |
/// The key type of the InverseMap. |
| 1660 | 1660 |
typedef typename DescriptorMap::Value Key; |
| 1661 | 1661 |
|
| 1662 | 1662 |
/// \brief Subscript operator. |
| 1663 | 1663 |
/// |
| 1664 | 1664 |
/// Subscript operator. It gives back the item |
| 1665 | 1665 |
/// that the descriptor belongs to currently. |
| 1666 | 1666 |
Value operator[](const Key& key) const {
|
| 1667 | 1667 |
return _inverted(key); |
| 1668 | 1668 |
} |
| 1669 | 1669 |
|
| 1670 | 1670 |
/// \brief Size of the map. |
| 1671 | 1671 |
/// |
| 1672 | 1672 |
/// Returns the size of the map. |
| 1673 | 1673 |
unsigned int size() const {
|
| 1674 | 1674 |
return _inverted.size(); |
| 1675 | 1675 |
} |
| 1676 | 1676 |
|
| 1677 | 1677 |
private: |
| 1678 | 1678 |
const DescriptorMap& _inverted; |
| 1679 | 1679 |
}; |
| 1680 | 1680 |
|
| 1681 | 1681 |
/// \brief Gives back the inverse of the map. |
| 1682 | 1682 |
/// |
| 1683 | 1683 |
/// Gives back the inverse of the map. |
| 1684 | 1684 |
const InverseMap inverse() const {
|
| 1685 | 1685 |
return InverseMap(*this); |
| 1686 | 1686 |
} |
| 1687 | 1687 |
}; |
| 1688 | 1688 |
|
| 1689 | 1689 |
/// \brief Returns the source of the given arc. |
| 1690 | 1690 |
/// |
| 1691 | 1691 |
/// The SourceMap gives back the source Node of the given arc. |
| 1692 | 1692 |
/// \see TargetMap |
| 1693 | 1693 |
/// \author Balazs Dezso |
| 1694 | 1694 |
template <typename Digraph> |
| 1695 | 1695 |
class SourceMap {
|
| 1696 | 1696 |
public: |
| 1697 | 1697 |
|
| 1698 | 1698 |
typedef typename Digraph::Node Value; |
| 1699 | 1699 |
typedef typename Digraph::Arc Key; |
| 1700 | 1700 |
|
| 1701 | 1701 |
/// \brief Constructor |
| 1702 | 1702 |
/// |
| 1703 | 1703 |
/// Constructor |
| 1704 | 1704 |
/// \param _digraph The digraph that the map belongs to. |
| 1705 | 1705 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {}
|
| 1706 | 1706 |
|
| 1707 | 1707 |
/// \brief The subscript operator. |
| 1708 | 1708 |
/// |
| 1709 | 1709 |
/// The subscript operator. |
| 1710 | 1710 |
/// \param arc The arc |
| 1711 | 1711 |
/// \return The source of the arc |
| 1712 | 1712 |
Value operator[](const Key& arc) const {
|
| 1713 | 1713 |
return _digraph.source(arc); |
| 1714 | 1714 |
} |
| 1715 | 1715 |
|
| 1716 | 1716 |
private: |
| 1717 | 1717 |
const Digraph& _digraph; |
| 1718 | 1718 |
}; |
| 1719 | 1719 |
|
| 1720 | 1720 |
/// \brief Returns a \ref SourceMap class. |
| 1721 | 1721 |
/// |
| 1722 | 1722 |
/// This function just returns an \ref SourceMap class. |
| 1723 | 1723 |
/// \relates SourceMap |
| 1724 | 1724 |
template <typename Digraph> |
| 1725 | 1725 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
|
| 1726 | 1726 |
return SourceMap<Digraph>(digraph); |
| 1727 | 1727 |
} |
| 1728 | 1728 |
|
| 1729 | 1729 |
/// \brief Returns the target of the given arc. |
| 1730 | 1730 |
/// |
| 1731 | 1731 |
/// The TargetMap gives back the target Node of the given arc. |
| 1732 | 1732 |
/// \see SourceMap |
| 1733 | 1733 |
/// \author Balazs Dezso |
| 1734 | 1734 |
template <typename Digraph> |
| 1735 | 1735 |
class TargetMap {
|
| 1736 | 1736 |
public: |
| 1737 | 1737 |
|
| 1738 | 1738 |
typedef typename Digraph::Node Value; |
| 1739 | 1739 |
typedef typename Digraph::Arc Key; |
| 1740 | 1740 |
|
| 1741 | 1741 |
/// \brief Constructor |
| 1742 | 1742 |
/// |
| 1743 | 1743 |
/// Constructor |
| 1744 | 1744 |
/// \param _digraph The digraph that the map belongs to. |
| 1745 | 1745 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {}
|
| 1746 | 1746 |
|
| 1747 | 1747 |
/// \brief The subscript operator. |
| 1748 | 1748 |
/// |
| 1749 | 1749 |
/// The subscript operator. |
| 1750 | 1750 |
/// \param e The arc |
| 1751 | 1751 |
/// \return The target of the arc |
| 1752 | 1752 |
Value operator[](const Key& e) const {
|
| 1753 | 1753 |
return _digraph.target(e); |
| 1754 | 1754 |
} |
| 1755 | 1755 |
|
| 1756 | 1756 |
private: |
| 1757 | 1757 |
const Digraph& _digraph; |
| 1758 | 1758 |
}; |
| 1759 | 1759 |
|
| 1760 | 1760 |
/// \brief Returns a \ref TargetMap class. |
| 1761 | 1761 |
/// |
| 1762 | 1762 |
/// This function just returns a \ref TargetMap class. |
| 1763 | 1763 |
/// \relates TargetMap |
| 1764 | 1764 |
template <typename Digraph> |
| 1765 | 1765 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
|
| 1766 | 1766 |
return TargetMap<Digraph>(digraph); |
| 1767 | 1767 |
} |
| 1768 | 1768 |
|
| 1769 | 1769 |
/// \brief Returns the "forward" directed arc view of an edge. |
| 1770 | 1770 |
/// |
| 1771 | 1771 |
/// Returns the "forward" directed arc view of an edge. |
| 1772 | 1772 |
/// \see BackwardMap |
| 1773 | 1773 |
/// \author Balazs Dezso |
| 1774 | 1774 |
template <typename Graph> |
| 1775 | 1775 |
class ForwardMap {
|
| 1776 | 1776 |
public: |
| 1777 | 1777 |
|
| 1778 | 1778 |
typedef typename Graph::Arc Value; |
| 1779 | 1779 |
typedef typename Graph::Edge Key; |
| 1780 | 1780 |
|
| 1781 | 1781 |
/// \brief Constructor |
| 1782 | 1782 |
/// |
| 1783 | 1783 |
/// Constructor |
| 1784 | 1784 |
/// \param _graph The graph that the map belongs to. |
| 1785 | 1785 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {}
|
| 1786 | 1786 |
|
| 1787 | 1787 |
/// \brief The subscript operator. |
| 1788 | 1788 |
/// |
| 1789 | 1789 |
/// The subscript operator. |
| 1790 | 1790 |
/// \param key An edge |
| 1791 | 1791 |
/// \return The "forward" directed arc view of edge |
| 1792 | 1792 |
Value operator[](const Key& key) const {
|
| 1793 | 1793 |
return _graph.direct(key, true); |
| 1794 | 1794 |
} |
| 1795 | 1795 |
|
| 1796 | 1796 |
private: |
| 1797 | 1797 |
const Graph& _graph; |
| 1798 | 1798 |
}; |
| 1799 | 1799 |
|
| 1800 | 1800 |
/// \brief Returns a \ref ForwardMap class. |
| 1801 | 1801 |
/// |
| 1802 | 1802 |
/// This function just returns an \ref ForwardMap class. |
| 1803 | 1803 |
/// \relates ForwardMap |
| 1804 | 1804 |
template <typename Graph> |
| 1805 | 1805 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) {
|
| 1806 | 1806 |
return ForwardMap<Graph>(graph); |
| 1807 | 1807 |
} |
| 1808 | 1808 |
|
| 1809 | 1809 |
/// \brief Returns the "backward" directed arc view of an edge. |
| 1810 | 1810 |
/// |
| 1811 | 1811 |
/// Returns the "backward" directed arc view of an edge. |
| 1812 | 1812 |
/// \see ForwardMap |
| 1813 | 1813 |
/// \author Balazs Dezso |
| 1814 | 1814 |
template <typename Graph> |
| 1815 | 1815 |
class BackwardMap {
|
| 1816 | 1816 |
public: |
| 1817 | 1817 |
|
| 1818 | 1818 |
typedef typename Graph::Arc Value; |
| 1819 | 1819 |
typedef typename Graph::Edge Key; |
| 1820 | 1820 |
|
| 1821 | 1821 |
/// \brief Constructor |
| 1822 | 1822 |
/// |
| 1823 | 1823 |
/// Constructor |
| 1824 | 1824 |
/// \param _graph The graph that the map belongs to. |
| 1825 | 1825 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {}
|
| 1826 | 1826 |
|
| 1827 | 1827 |
/// \brief The subscript operator. |
| 1828 | 1828 |
/// |
| 1829 | 1829 |
/// The subscript operator. |
| 1830 | 1830 |
/// \param key An edge |
| 1831 | 1831 |
/// \return The "backward" directed arc view of edge |
| 1832 | 1832 |
Value operator[](const Key& key) const {
|
| 1833 | 1833 |
return _graph.direct(key, false); |
| 1834 | 1834 |
} |
| 1835 | 1835 |
|
| 1836 | 1836 |
private: |
| 1837 | 1837 |
const Graph& _graph; |
| 1838 | 1838 |
}; |
| 1839 | 1839 |
|
| 1840 | 1840 |
/// \brief Returns a \ref BackwardMap class |
| 1841 | 1841 |
|
| 1842 | 1842 |
/// This function just returns a \ref BackwardMap class. |
| 1843 | 1843 |
/// \relates BackwardMap |
| 1844 | 1844 |
template <typename Graph> |
| 1845 | 1845 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) {
|
| 1846 | 1846 |
return BackwardMap<Graph>(graph); |
| 1847 | 1847 |
} |
| 1848 | 1848 |
|
| 1849 | 1849 |
/// \brief Potential difference map |
| 1850 | 1850 |
/// |
| 1851 | 1851 |
/// If there is an potential map on the nodes then we |
| 1852 | 1852 |
/// can get an arc map as we get the substraction of the |
| 1853 | 1853 |
/// values of the target and source. |
| 1854 | 1854 |
template <typename Digraph, typename NodeMap> |
| 1855 | 1855 |
class PotentialDifferenceMap {
|
| 1856 | 1856 |
public: |
| 1857 | 1857 |
typedef typename Digraph::Arc Key; |
| 1858 | 1858 |
typedef typename NodeMap::Value Value; |
| 1859 | 1859 |
|
| 1860 | 1860 |
/// \brief Constructor |
| 1861 | 1861 |
/// |
| 1862 | 1862 |
/// Contructor of the map |
| 1863 | 1863 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
| 1864 | 1864 |
const NodeMap& potential) |
| 1865 | 1865 |
: _digraph(digraph), _potential(potential) {}
|
| 1866 | 1866 |
|
| 1867 | 1867 |
/// \brief Const subscription operator |
| 1868 | 1868 |
/// |
| 1869 | 1869 |
/// Const subscription operator |
| 1870 | 1870 |
Value operator[](const Key& arc) const {
|
| 1871 | 1871 |
return _potential[_digraph.target(arc)] - |
| 1872 | 1872 |
_potential[_digraph.source(arc)]; |
| 1873 | 1873 |
} |
| 1874 | 1874 |
|
| 1875 | 1875 |
private: |
| 1876 | 1876 |
const Digraph& _digraph; |
| 1877 | 1877 |
const NodeMap& _potential; |
| 1878 | 1878 |
}; |
| 1879 | 1879 |
|
| 1880 | 1880 |
/// \brief Returns a PotentialDifferenceMap. |
| 1881 | 1881 |
/// |
| 1882 | 1882 |
/// This function just returns a PotentialDifferenceMap. |
| 1883 | 1883 |
/// \relates PotentialDifferenceMap |
| 1884 | 1884 |
template <typename Digraph, typename NodeMap> |
| 1885 | 1885 |
PotentialDifferenceMap<Digraph, NodeMap> |
| 1886 | 1886 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
|
| 1887 | 1887 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
| 1888 | 1888 |
} |
| 1889 | 1889 |
|
| 1890 | 1890 |
/// \brief Map of the node in-degrees. |
| 1891 | 1891 |
/// |
| 1892 | 1892 |
/// This map returns the in-degree of a node. Once it is constructed, |
| 1893 | 1893 |
/// the degrees are stored in a standard NodeMap, so each query is done |
| 1894 | 1894 |
/// in constant time. On the other hand, the values are updated automatically |
| 1895 | 1895 |
/// whenever the digraph changes. |
| 1896 | 1896 |
/// |
| 1897 | 1897 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
| 1898 | 1898 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
| 1899 | 1899 |
/// is not guarantied if these additional features are used. For example |
| 1900 | 1900 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
| 1901 | 1901 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
| 1902 | 1902 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
| 1903 | 1903 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
| 1904 | 1904 |
/// |
| 1905 | 1905 |
/// \sa OutDegMap |
| 1906 | 1906 |
|
| 1907 | 1907 |
template <typename _Digraph> |
| 1908 | 1908 |
class InDegMap |
| 1909 | 1909 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
| 1910 | 1910 |
::ItemNotifier::ObserverBase {
|
| 1911 | 1911 |
|
| 1912 | 1912 |
public: |
| 1913 | 1913 |
|
| 1914 | 1914 |
typedef _Digraph Digraph; |
| 1915 | 1915 |
typedef int Value; |
| 1916 | 1916 |
typedef typename Digraph::Node Key; |
| 1917 | 1917 |
|
| 1918 | 1918 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
| 1919 | 1919 |
::ItemNotifier::ObserverBase Parent; |
| 1920 | 1920 |
|
| 1921 | 1921 |
private: |
| 1922 | 1922 |
|
| 1923 | 1923 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> {
|
| 1924 | 1924 |
public: |
| 1925 | 1925 |
|
| 1926 | 1926 |
typedef DefaultMap<Digraph, Key, int> Parent; |
| 1927 | 1927 |
|
| 1928 | 1928 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 1929 | 1929 |
|
| 1930 | 1930 |
virtual void add(const Key& key) {
|
| 1931 | 1931 |
Parent::add(key); |
| 1932 | 1932 |
Parent::set(key, 0); |
| 1933 | 1933 |
} |
| 1934 | 1934 |
|
| 1935 | 1935 |
virtual void add(const std::vector<Key>& keys) {
|
| 1936 | 1936 |
Parent::add(keys); |
| 1937 | 1937 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 1938 | 1938 |
Parent::set(keys[i], 0); |
| 1939 | 1939 |
} |
| 1940 | 1940 |
} |
| 1941 | 1941 |
|
| 1942 | 1942 |
virtual void build() {
|
| 1943 | 1943 |
Parent::build(); |
| 1944 | 1944 |
Key it; |
| 1945 | 1945 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 1946 | 1946 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1947 | 1947 |
Parent::set(it, 0); |
| 1948 | 1948 |
} |
| 1949 | 1949 |
} |
| 1950 | 1950 |
}; |
| 1951 | 1951 |
|
| 1952 | 1952 |
public: |
| 1953 | 1953 |
|
| 1954 | 1954 |
/// \brief Constructor. |
| 1955 | 1955 |
/// |
| 1956 | 1956 |
/// Constructor for creating in-degree map. |
| 1957 | 1957 |
explicit InDegMap(const Digraph& digraph) |
| 1958 | 1958 |
: _digraph(digraph), _deg(digraph) {
|
| 1959 | 1959 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
| 1960 | 1960 |
|
| 1961 | 1961 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 1962 | 1962 |
_deg[it] = countInArcs(_digraph, it); |
| 1963 | 1963 |
} |
| 1964 | 1964 |
} |
| 1965 | 1965 |
|
| 1966 | 1966 |
/// Gives back the in-degree of a Node. |
| 1967 | 1967 |
int operator[](const Key& key) const {
|
| 1968 | 1968 |
return _deg[key]; |
| 1969 | 1969 |
} |
| 1970 | 1970 |
|
| 1971 | 1971 |
protected: |
| 1972 | 1972 |
|
| 1973 | 1973 |
typedef typename Digraph::Arc Arc; |
| 1974 | 1974 |
|
| 1975 | 1975 |
virtual void add(const Arc& arc) {
|
| 1976 | 1976 |
++_deg[_digraph.target(arc)]; |
| 1977 | 1977 |
} |
| 1978 | 1978 |
|
| 1979 | 1979 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 1980 | 1980 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1981 | 1981 |
++_deg[_digraph.target(arcs[i])]; |
| 1982 | 1982 |
} |
| 1983 | 1983 |
} |
| 1984 | 1984 |
|
| 1985 | 1985 |
virtual void erase(const Arc& arc) {
|
| 1986 | 1986 |
--_deg[_digraph.target(arc)]; |
| 1987 | 1987 |
} |
| 1988 | 1988 |
|
| 1989 | 1989 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 1990 | 1990 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1991 | 1991 |
--_deg[_digraph.target(arcs[i])]; |
| 1992 | 1992 |
} |
| 1993 | 1993 |
} |
| 1994 | 1994 |
|
| 1995 | 1995 |
virtual void build() {
|
| 1996 | 1996 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 1997 | 1997 |
_deg[it] = countInArcs(_digraph, it); |
| 1998 | 1998 |
} |
| 1999 | 1999 |
} |
| 2000 | 2000 |
|
| 2001 | 2001 |
virtual void clear() {
|
| 2002 | 2002 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2003 | 2003 |
_deg[it] = 0; |
| 2004 | 2004 |
} |
| 2005 | 2005 |
} |
| 2006 | 2006 |
private: |
| 2007 | 2007 |
|
| 2008 | 2008 |
const Digraph& _digraph; |
| 2009 | 2009 |
AutoNodeMap _deg; |
| 2010 | 2010 |
}; |
| 2011 | 2011 |
|
| 2012 | 2012 |
/// \brief Map of the node out-degrees. |
| 2013 | 2013 |
/// |
| 2014 | 2014 |
/// This map returns the out-degree of a node. Once it is constructed, |
| 2015 | 2015 |
/// the degrees are stored in a standard NodeMap, so each query is done |
| 2016 | 2016 |
/// in constant time. On the other hand, the values are updated automatically |
| 2017 | 2017 |
/// whenever the digraph changes. |
| 2018 | 2018 |
/// |
| 2019 | 2019 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
| 2020 | 2020 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap |
| 2021 | 2021 |
/// is not guarantied if these additional features are used. For example |
| 2022 | 2022 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
| 2023 | 2023 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
| 2024 | 2024 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
| 2025 | 2025 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
| 2026 | 2026 |
/// |
| 2027 | 2027 |
/// \sa InDegMap |
| 2028 | 2028 |
|
| 2029 | 2029 |
template <typename _Digraph> |
| 2030 | 2030 |
class OutDegMap |
| 2031 | 2031 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
| 2032 | 2032 |
::ItemNotifier::ObserverBase {
|
| 2033 | 2033 |
|
| 2034 | 2034 |
public: |
| 2035 | 2035 |
|
| 2036 | 2036 |
typedef _Digraph Digraph; |
| 2037 | 2037 |
typedef int Value; |
| 2038 | 2038 |
typedef typename Digraph::Node Key; |
| 2039 | 2039 |
|
| 2040 | 2040 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
| 2041 | 2041 |
::ItemNotifier::ObserverBase Parent; |
| 2042 | 2042 |
|
| 2043 | 2043 |
private: |
| 2044 | 2044 |
|
| 2045 | 2045 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> {
|
| 2046 | 2046 |
public: |
| 2047 | 2047 |
|
| 2048 | 2048 |
typedef DefaultMap<Digraph, Key, int> Parent; |
| 2049 | 2049 |
|
| 2050 | 2050 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 2051 | 2051 |
|
| 2052 | 2052 |
virtual void add(const Key& key) {
|
| 2053 | 2053 |
Parent::add(key); |
| 2054 | 2054 |
Parent::set(key, 0); |
| 2055 | 2055 |
} |
| 2056 | 2056 |
virtual void add(const std::vector<Key>& keys) {
|
| 2057 | 2057 |
Parent::add(keys); |
| 2058 | 2058 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2059 | 2059 |
Parent::set(keys[i], 0); |
| 2060 | 2060 |
} |
| 2061 | 2061 |
} |
| 2062 | 2062 |
virtual void build() {
|
| 2063 | 2063 |
Parent::build(); |
| 2064 | 2064 |
Key it; |
| 2065 | 2065 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 2066 | 2066 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2067 | 2067 |
Parent::set(it, 0); |
| 2068 | 2068 |
} |
| 2069 | 2069 |
} |
| 2070 | 2070 |
}; |
| 2071 | 2071 |
|
| 2072 | 2072 |
public: |
| 2073 | 2073 |
|
| 2074 | 2074 |
/// \brief Constructor. |
| 2075 | 2075 |
/// |
| 2076 | 2076 |
/// Constructor for creating out-degree map. |
| 2077 | 2077 |
explicit OutDegMap(const Digraph& digraph) |
| 2078 | 2078 |
: _digraph(digraph), _deg(digraph) {
|
| 2079 | 2079 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
| 2080 | 2080 |
|
| 2081 | 2081 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2082 | 2082 |
_deg[it] = countOutArcs(_digraph, it); |
| 2083 | 2083 |
} |
| 2084 | 2084 |
} |
| 2085 | 2085 |
|
| 2086 | 2086 |
/// Gives back the out-degree of a Node. |
| 2087 | 2087 |
int operator[](const Key& key) const {
|
| 2088 | 2088 |
return _deg[key]; |
| 2089 | 2089 |
} |
| 2090 | 2090 |
|
| 2091 | 2091 |
protected: |
| 2092 | 2092 |
|
| 2093 | 2093 |
typedef typename Digraph::Arc Arc; |
| 2094 | 2094 |
|
| 2095 | 2095 |
virtual void add(const Arc& arc) {
|
| 2096 | 2096 |
++_deg[_digraph.source(arc)]; |
| 2097 | 2097 |
} |
| 2098 | 2098 |
|
| 2099 | 2099 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 2100 | 2100 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2101 | 2101 |
++_deg[_digraph.source(arcs[i])]; |
| 2102 | 2102 |
} |
| 2103 | 2103 |
} |
| 2104 | 2104 |
|
| 2105 | 2105 |
virtual void erase(const Arc& arc) {
|
| 2106 | 2106 |
--_deg[_digraph.source(arc)]; |
| 2107 | 2107 |
} |
| 2108 | 2108 |
|
| 2109 | 2109 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 2110 | 2110 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2111 | 2111 |
--_deg[_digraph.source(arcs[i])]; |
| 2112 | 2112 |
} |
| 2113 | 2113 |
} |
| 2114 | 2114 |
|
| 2115 | 2115 |
virtual void build() {
|
| 2116 | 2116 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2117 | 2117 |
_deg[it] = countOutArcs(_digraph, it); |
| 2118 | 2118 |
} |
| 2119 | 2119 |
} |
| 2120 | 2120 |
|
| 2121 | 2121 |
virtual void clear() {
|
| 2122 | 2122 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2123 | 2123 |
_deg[it] = 0; |
| 2124 | 2124 |
} |
| 2125 | 2125 |
} |
| 2126 | 2126 |
private: |
| 2127 | 2127 |
|
| 2128 | 2128 |
const Digraph& _digraph; |
| 2129 | 2129 |
AutoNodeMap _deg; |
| 2130 | 2130 |
}; |
| 2131 | 2131 |
|
| 2132 | 2132 |
|
| 2133 | 2133 |
///Dynamic arc look up between given endpoints. |
| 2134 | 2134 |
|
| 2135 | 2135 |
///\ingroup gutils |
| 2136 | 2136 |
///Using this class, you can find an arc in a digraph from a given |
| 2137 | 2137 |
///source to a given target in amortized time <em>O(log d)</em>, |
| 2138 | 2138 |
///where <em>d</em> is the out-degree of the source node. |
| 2139 | 2139 |
/// |
| 2140 | 2140 |
///It is possible to find \e all parallel arcs between two nodes with |
| 2141 | 2141 |
///the \c findFirst() and \c findNext() members. |
| 2142 | 2142 |
/// |
| 2143 | 2143 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
| 2144 | 2144 |
///digraph is not changed so frequently. |
| 2145 | 2145 |
/// |
| 2146 | 2146 |
///This class uses a self-adjusting binary search tree, Sleator's |
| 2147 | 2147 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
| 2148 | 2148 |
///time bound for arc lookups. This class also guarantees the |
| 2149 | 2149 |
///optimal time bound in a constant factor for any distribution of |
| 2150 | 2150 |
///queries. |
| 2151 | 2151 |
/// |
| 2152 | 2152 |
///\param G The type of the underlying digraph. |
| 2153 | 2153 |
/// |
| 2154 | 2154 |
///\sa ArcLookUp |
| 2155 | 2155 |
///\sa AllArcLookUp |
| 2156 | 2156 |
template<class G> |
| 2157 | 2157 |
class DynArcLookUp |
| 2158 | 2158 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
| 2159 | 2159 |
{
|
| 2160 | 2160 |
public: |
| 2161 | 2161 |
typedef typename ItemSetTraits<G, typename G::Arc> |
| 2162 | 2162 |
::ItemNotifier::ObserverBase Parent; |
| 2163 | 2163 |
|
| 2164 | 2164 |
DIGRAPH_TYPEDEFS(G); |
| 2165 | 2165 |
typedef G Digraph; |
| 2166 | 2166 |
|
| 2167 | 2167 |
protected: |
| 2168 | 2168 |
|
| 2169 | 2169 |
class AutoNodeMap : public DefaultMap<G, Node, Arc> {
|
| 2170 | 2170 |
public: |
| 2171 | 2171 |
|
| 2172 | 2172 |
typedef DefaultMap<G, Node, Arc> Parent; |
| 2173 | 2173 |
|
| 2174 | 2174 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
|
| 2175 | 2175 |
|
| 2176 | 2176 |
virtual void add(const Node& node) {
|
| 2177 | 2177 |
Parent::add(node); |
| 2178 | 2178 |
Parent::set(node, INVALID); |
| 2179 | 2179 |
} |
| 2180 | 2180 |
|
| 2181 | 2181 |
virtual void add(const std::vector<Node>& nodes) {
|
| 2182 | 2182 |
Parent::add(nodes); |
| 2183 | 2183 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 2184 | 2184 |
Parent::set(nodes[i], INVALID); |
| 2185 | 2185 |
} |
| 2186 | 2186 |
} |
| 2187 | 2187 |
|
| 2188 | 2188 |
virtual void build() {
|
| 2189 | 2189 |
Parent::build(); |
| 2190 | 2190 |
Node it; |
| 2191 | 2191 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 2192 | 2192 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2193 | 2193 |
Parent::set(it, INVALID); |
| 2194 | 2194 |
} |
| 2195 | 2195 |
} |
| 2196 | 2196 |
}; |
| 2197 | 2197 |
|
| 2198 | 2198 |
const Digraph &_g; |
| 2199 | 2199 |
AutoNodeMap _head; |
| 2200 | 2200 |
typename Digraph::template ArcMap<Arc> _parent; |
| 2201 | 2201 |
typename Digraph::template ArcMap<Arc> _left; |
| 2202 | 2202 |
typename Digraph::template ArcMap<Arc> _right; |
| 2203 | 2203 |
|
| 2204 | 2204 |
class ArcLess {
|
| 2205 | 2205 |
const Digraph &g; |
| 2206 | 2206 |
public: |
| 2207 | 2207 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 2208 | 2208 |
bool operator()(Arc a,Arc b) const |
| 2209 | 2209 |
{
|
| 2210 | 2210 |
return g.target(a)<g.target(b); |
| 2211 | 2211 |
} |
| 2212 | 2212 |
}; |
| 2213 | 2213 |
|
| 2214 | 2214 |
public: |
| 2215 | 2215 |
|
0 comments (0 inline)