[946] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library |
---|
[946] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[946] | 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #ifndef LEMON_GRAPH_UTILS_H |
---|
| 18 | #define LEMON_GRAPH_UTILS_H |
---|
| 19 | |
---|
| 20 | #include <iterator> |
---|
[1419] | 21 | #include <vector> |
---|
[1402] | 22 | #include <map> |
---|
[946] | 23 | |
---|
| 24 | #include <lemon/invalid.h> |
---|
[977] | 25 | #include <lemon/utility.h> |
---|
[1413] | 26 | #include <lemon/maps.h> |
---|
[1459] | 27 | #include <lemon/bits/alteration_notifier.h> |
---|
[946] | 28 | |
---|
[947] | 29 | ///\ingroup gutils |
---|
[946] | 30 | ///\file |
---|
[947] | 31 | ///\brief Graph utilities. |
---|
[946] | 32 | /// |
---|
[964] | 33 | ///\todo Please |
---|
| 34 | ///revise the documentation. |
---|
| 35 | /// |
---|
[946] | 36 | |
---|
| 37 | |
---|
| 38 | namespace lemon { |
---|
| 39 | |
---|
[1267] | 40 | /// \addtogroup gutils |
---|
| 41 | /// @{ |
---|
[947] | 42 | |
---|
[946] | 43 | /// \brief Function to count the items in the graph. |
---|
| 44 | /// |
---|
| 45 | /// This function counts the items in the graph. |
---|
| 46 | /// The complexity of the function is O(n) because |
---|
| 47 | /// it iterates on all of the items. |
---|
| 48 | |
---|
| 49 | template <typename Graph, typename ItemIt> |
---|
[977] | 50 | inline int countItems(const Graph& g) { |
---|
[946] | 51 | int num = 0; |
---|
[977] | 52 | for (ItemIt it(g); it != INVALID; ++it) { |
---|
[946] | 53 | ++num; |
---|
| 54 | } |
---|
| 55 | return num; |
---|
| 56 | } |
---|
| 57 | |
---|
[977] | 58 | // Node counting: |
---|
| 59 | |
---|
| 60 | template <typename Graph> |
---|
| 61 | inline |
---|
| 62 | typename enable_if<typename Graph::NodeNumTag, int>::type |
---|
| 63 | _countNodes(const Graph &g) { |
---|
| 64 | return g.nodeNum(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | template <typename Graph> |
---|
| 68 | inline int _countNodes(Wrap<Graph> w) { |
---|
| 69 | return countItems<Graph, typename Graph::NodeIt>(w.value); |
---|
| 70 | } |
---|
| 71 | |
---|
[946] | 72 | /// \brief Function to count the nodes in the graph. |
---|
| 73 | /// |
---|
| 74 | /// This function counts the nodes in the graph. |
---|
| 75 | /// The complexity of the function is O(n) but for some |
---|
[1526] | 76 | /// graph structures it is specialized to run in O(1). |
---|
[977] | 77 | /// |
---|
| 78 | /// \todo refer how to specialize it |
---|
[946] | 79 | |
---|
| 80 | template <typename Graph> |
---|
[977] | 81 | inline int countNodes(const Graph& g) { |
---|
| 82 | return _countNodes<Graph>(g); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // Edge counting: |
---|
| 86 | |
---|
| 87 | template <typename Graph> |
---|
| 88 | inline |
---|
| 89 | typename enable_if<typename Graph::EdgeNumTag, int>::type |
---|
| 90 | _countEdges(const Graph &g) { |
---|
| 91 | return g.edgeNum(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | template <typename Graph> |
---|
| 95 | inline int _countEdges(Wrap<Graph> w) { |
---|
| 96 | return countItems<Graph, typename Graph::EdgeIt>(w.value); |
---|
[946] | 97 | } |
---|
| 98 | |
---|
| 99 | /// \brief Function to count the edges in the graph. |
---|
| 100 | /// |
---|
| 101 | /// This function counts the edges in the graph. |
---|
| 102 | /// The complexity of the function is O(e) but for some |
---|
[1526] | 103 | /// graph structures it is specialized to run in O(1). |
---|
[977] | 104 | |
---|
[946] | 105 | template <typename Graph> |
---|
[977] | 106 | inline int countEdges(const Graph& g) { |
---|
| 107 | return _countEdges<Graph>(g); |
---|
[946] | 108 | } |
---|
| 109 | |
---|
[1053] | 110 | // Undirected edge counting: |
---|
| 111 | |
---|
| 112 | template <typename Graph> |
---|
| 113 | inline |
---|
| 114 | typename enable_if<typename Graph::EdgeNumTag, int>::type |
---|
| 115 | _countUndirEdges(const Graph &g) { |
---|
| 116 | return g.undirEdgeNum(); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | template <typename Graph> |
---|
| 120 | inline int _countUndirEdges(Wrap<Graph> w) { |
---|
| 121 | return countItems<Graph, typename Graph::UndirEdgeIt>(w.value); |
---|
| 122 | } |
---|
| 123 | |
---|
[1526] | 124 | /// \brief Function to count the undirected edges in the graph. |
---|
[946] | 125 | /// |
---|
[1526] | 126 | /// This function counts the undirected edges in the graph. |
---|
[946] | 127 | /// The complexity of the function is O(e) but for some |
---|
[964] | 128 | /// graph structure it is specialized to run in O(1). |
---|
[1053] | 129 | |
---|
[946] | 130 | template <typename Graph> |
---|
[1053] | 131 | inline int countUndirEdges(const Graph& g) { |
---|
| 132 | return _countUndirEdges<Graph>(g); |
---|
[946] | 133 | } |
---|
| 134 | |
---|
[977] | 135 | |
---|
[1053] | 136 | |
---|
[946] | 137 | template <typename Graph, typename DegIt> |
---|
| 138 | inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 139 | int num = 0; |
---|
| 140 | for (DegIt it(_g, _n); it != INVALID; ++it) { |
---|
| 141 | ++num; |
---|
| 142 | } |
---|
| 143 | return num; |
---|
| 144 | } |
---|
[967] | 145 | |
---|
[1531] | 146 | /// \brief Function to count the number of the out-edges from node \c n. |
---|
| 147 | /// |
---|
| 148 | /// This function counts the number of the out-edges from node \c n |
---|
| 149 | /// in the graph. |
---|
| 150 | template <typename Graph> |
---|
| 151 | inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 152 | return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /// \brief Function to count the number of the in-edges to node \c n. |
---|
| 156 | /// |
---|
| 157 | /// This function counts the number of the in-edges to node \c n |
---|
| 158 | /// in the graph. |
---|
| 159 | template <typename Graph> |
---|
| 160 | inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) { |
---|
| 161 | return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
[967] | 165 | /// Finds an edge between two nodes of a graph. |
---|
| 166 | |
---|
| 167 | /// Finds an edge from node \c u to node \c v in graph \c g. |
---|
| 168 | /// |
---|
| 169 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 170 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 171 | /// the next edge from \c u to \c v after \c prev. |
---|
| 172 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
| 173 | /// |
---|
| 174 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
| 175 | /// \code |
---|
| 176 | /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) { |
---|
| 177 | /// ... |
---|
| 178 | /// } |
---|
| 179 | /// \endcode |
---|
[1536] | 180 | /// \todo We may want to use the "GraphBase" |
---|
[967] | 181 | /// interface here... |
---|
| 182 | /// \bug Untested ... |
---|
| 183 | template <typename Graph> |
---|
| 184 | typename Graph::Edge findEdge(const Graph &g, |
---|
[1267] | 185 | typename Graph::Node u, typename Graph::Node v, |
---|
| 186 | typename Graph::Edge prev = INVALID) |
---|
[967] | 187 | { |
---|
| 188 | typename Graph::OutEdgeIt e(g,prev); |
---|
[1079] | 189 | // if(prev==INVALID) g.first(e,u); |
---|
| 190 | if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u); |
---|
[967] | 191 | else ++e; |
---|
[1079] | 192 | while(e!=INVALID && g.target(e)!=v) ++e; |
---|
[967] | 193 | return e; |
---|
| 194 | } |
---|
[1531] | 195 | |
---|
| 196 | /// \brief Copy the source map to the target map. |
---|
[964] | 197 | /// |
---|
[1531] | 198 | /// Copy the \c source map to the \c target map. It uses the given iterator |
---|
| 199 | /// to iterate on the data structure and it use the \c ref mapping to |
---|
| 200 | /// convert the source's keys to the target's keys. |
---|
| 201 | template <typename Target, typename Source, |
---|
| 202 | typename ItemIt, typename Ref> |
---|
| 203 | void copyMap(Target& target, const Source& source, |
---|
| 204 | ItemIt it, const Ref& ref) { |
---|
| 205 | for (; it != INVALID; ++it) { |
---|
| 206 | target[ref[it]] = source[it]; |
---|
[946] | 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
[1531] | 210 | /// \brief Copy the source map to the target map. |
---|
| 211 | /// |
---|
| 212 | /// Copy the \c source map to the \c target map. It uses the given iterator |
---|
| 213 | /// to iterate on the data structure. |
---|
| 214 | template <typename Target, typename Source, |
---|
| 215 | typename ItemIt> |
---|
| 216 | void copyMap(Target& target, const Source& source, ItemIt it) { |
---|
| 217 | for (; it != INVALID; ++it) { |
---|
| 218 | target[it] = source[it]; |
---|
[946] | 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
[1531] | 222 | |
---|
| 223 | /// \brief Class to copy a graph to an other graph. |
---|
| 224 | /// |
---|
| 225 | /// Class to copy a graph to an other graph. It can be used easier |
---|
| 226 | /// with the \c copyGraph() function. |
---|
| 227 | template <typename Target, typename Source> |
---|
[1267] | 228 | class GraphCopy { |
---|
[1531] | 229 | public: |
---|
| 230 | typedef typename Source::Node Node; |
---|
| 231 | typedef typename Source::NodeIt NodeIt; |
---|
| 232 | typedef typename Source::Edge Edge; |
---|
| 233 | typedef typename Source::EdgeIt EdgeIt; |
---|
[946] | 234 | |
---|
[1531] | 235 | typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap; |
---|
| 236 | typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap; |
---|
[946] | 237 | |
---|
[1531] | 238 | /// \brief Constructor for the GraphCopy. |
---|
| 239 | /// |
---|
| 240 | /// It copies the content of the \c _source graph into the |
---|
| 241 | /// \c _target graph. It creates also two references, one beetween |
---|
| 242 | /// the two nodeset and one beetween the two edgesets. |
---|
| 243 | GraphCopy(Target& _target, const Source& _source) |
---|
| 244 | : source(_source), target(_target), |
---|
| 245 | nodeRefMap(_source), edgeRefMap(_source) { |
---|
| 246 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 247 | nodeRefMap[it] = target.addNode(); |
---|
| 248 | } |
---|
| 249 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 250 | edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], |
---|
| 251 | nodeRefMap[source.target(it)]); |
---|
| 252 | } |
---|
[1267] | 253 | } |
---|
[946] | 254 | |
---|
[1531] | 255 | /// \brief Copies the node references into the given map. |
---|
| 256 | /// |
---|
| 257 | /// Copies the node references into the given map. |
---|
| 258 | template <typename NodeRef> |
---|
| 259 | const GraphCopy& nodeRef(NodeRef& map) const { |
---|
| 260 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 261 | map.set(it, nodeRefMap[it]); |
---|
| 262 | } |
---|
| 263 | return *this; |
---|
[1267] | 264 | } |
---|
[1531] | 265 | |
---|
| 266 | /// \brief Reverse and copies the node references into the given map. |
---|
| 267 | /// |
---|
| 268 | /// Reverse and copies the node references into the given map. |
---|
| 269 | template <typename NodeRef> |
---|
| 270 | const GraphCopy& nodeCrossRef(NodeRef& map) const { |
---|
| 271 | for (NodeIt it(source); it != INVALID; ++it) { |
---|
| 272 | map.set(nodeRefMap[it], it); |
---|
| 273 | } |
---|
| 274 | return *this; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /// \brief Copies the edge references into the given map. |
---|
| 278 | /// |
---|
| 279 | /// Copies the edge references into the given map. |
---|
| 280 | template <typename EdgeRef> |
---|
| 281 | const GraphCopy& edgeRef(EdgeRef& map) const { |
---|
| 282 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 283 | map.set(it, edgeRefMap[it]); |
---|
| 284 | } |
---|
| 285 | return *this; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /// \brief Reverse and copies the edge references into the given map. |
---|
| 289 | /// |
---|
| 290 | /// Reverse and copies the edge references into the given map. |
---|
| 291 | template <typename EdgeRef> |
---|
| 292 | const GraphCopy& edgeCrossRef(EdgeRef& map) const { |
---|
| 293 | for (EdgeIt it(source); it != INVALID; ++it) { |
---|
| 294 | map.set(edgeRefMap[it], it); |
---|
| 295 | } |
---|
| 296 | return *this; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /// \brief Make copy of the given map. |
---|
| 300 | /// |
---|
| 301 | /// Makes copy of the given map for the newly created graph. |
---|
| 302 | /// The new map's key type is the target graph's node type, |
---|
| 303 | /// and the copied map's key type is the source graph's node |
---|
| 304 | /// type. |
---|
| 305 | template <typename TargetMap, typename SourceMap> |
---|
| 306 | const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const { |
---|
| 307 | copyMap(tMap, sMap, NodeIt(source), nodeRefMap); |
---|
| 308 | return *this; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | /// \brief Make copy of the given map. |
---|
| 312 | /// |
---|
| 313 | /// Makes copy of the given map for the newly created graph. |
---|
| 314 | /// The new map's key type is the target graph's edge type, |
---|
| 315 | /// and the copied map's key type is the source graph's edge |
---|
| 316 | /// type. |
---|
| 317 | template <typename TargetMap, typename SourceMap> |
---|
| 318 | const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const { |
---|
| 319 | copyMap(tMap, sMap, EdgeIt(source), edgeRefMap); |
---|
| 320 | return *this; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | /// \brief Gives back the stored node references. |
---|
| 324 | /// |
---|
| 325 | /// Gives back the stored node references. |
---|
| 326 | const NodeRefMap& nodeRef() const { |
---|
| 327 | return nodeRefMap; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | /// \brief Gives back the stored edge references. |
---|
| 331 | /// |
---|
| 332 | /// Gives back the stored edge references. |
---|
| 333 | const EdgeRefMap& edgeRef() const { |
---|
| 334 | return edgeRefMap; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | private: |
---|
| 338 | |
---|
| 339 | const Source& source; |
---|
| 340 | Target& target; |
---|
| 341 | |
---|
| 342 | NodeRefMap nodeRefMap; |
---|
| 343 | EdgeRefMap edgeRefMap; |
---|
[1267] | 344 | }; |
---|
[946] | 345 | |
---|
[1531] | 346 | /// \brief Copy a graph to an other graph. |
---|
| 347 | /// |
---|
| 348 | /// Copy a graph to an other graph. |
---|
| 349 | /// The usage of the function: |
---|
| 350 | /// |
---|
| 351 | /// \code |
---|
| 352 | /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr); |
---|
| 353 | /// \endcode |
---|
| 354 | /// |
---|
| 355 | /// After the copy the \c nr map will contain the mapping from the |
---|
| 356 | /// source graph's nodes to the target graph's nodes and the \c ecr will |
---|
| 357 | /// contain the mapping from the target graph's edge to the source's |
---|
| 358 | /// edges. |
---|
| 359 | template <typename Target, typename Source> |
---|
| 360 | GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) { |
---|
| 361 | return GraphCopy<Target, Source>(target, source); |
---|
| 362 | } |
---|
[946] | 363 | |
---|
[1267] | 364 | template <typename _Graph, typename _Item> |
---|
[1419] | 365 | class ItemSetTraits {}; |
---|
[1192] | 366 | |
---|
| 367 | template <typename _Graph> |
---|
[1267] | 368 | class ItemSetTraits<_Graph, typename _Graph::Node> { |
---|
[1192] | 369 | public: |
---|
| 370 | |
---|
| 371 | typedef _Graph Graph; |
---|
[947] | 372 | |
---|
[1192] | 373 | typedef typename Graph::Node Item; |
---|
| 374 | typedef typename Graph::NodeIt ItemIt; |
---|
| 375 | |
---|
| 376 | template <typename _Value> |
---|
| 377 | class Map : public Graph::template NodeMap<_Value> { |
---|
| 378 | public: |
---|
| 379 | typedef typename Graph::template NodeMap<_Value> Parent; |
---|
| 380 | typedef typename Parent::Value Value; |
---|
| 381 | |
---|
| 382 | Map(const Graph& _graph) : Parent(_graph) {} |
---|
| 383 | Map(const Graph& _graph, const Value& _value) |
---|
| 384 | : Parent(_graph, _value) {} |
---|
| 385 | }; |
---|
| 386 | |
---|
| 387 | }; |
---|
| 388 | |
---|
| 389 | template <typename _Graph> |
---|
[1267] | 390 | class ItemSetTraits<_Graph, typename _Graph::Edge> { |
---|
[1192] | 391 | public: |
---|
| 392 | |
---|
| 393 | typedef _Graph Graph; |
---|
| 394 | |
---|
| 395 | typedef typename Graph::Edge Item; |
---|
| 396 | typedef typename Graph::EdgeIt ItemIt; |
---|
| 397 | |
---|
| 398 | template <typename _Value> |
---|
| 399 | class Map : public Graph::template EdgeMap<_Value> { |
---|
| 400 | public: |
---|
| 401 | typedef typename Graph::template EdgeMap<_Value> Parent; |
---|
| 402 | typedef typename Parent::Value Value; |
---|
| 403 | |
---|
| 404 | Map(const Graph& _graph) : Parent(_graph) {} |
---|
| 405 | Map(const Graph& _graph, const Value& _value) |
---|
| 406 | : Parent(_graph, _value) {} |
---|
| 407 | }; |
---|
| 408 | |
---|
| 409 | }; |
---|
| 410 | |
---|
[1267] | 411 | template <typename _Graph> |
---|
| 412 | class ItemSetTraits<_Graph, typename _Graph::UndirEdge> { |
---|
| 413 | public: |
---|
| 414 | |
---|
| 415 | typedef _Graph Graph; |
---|
| 416 | |
---|
| 417 | typedef typename Graph::UndirEdge Item; |
---|
| 418 | typedef typename Graph::UndirEdgeIt ItemIt; |
---|
| 419 | |
---|
| 420 | template <typename _Value> |
---|
| 421 | class Map : public Graph::template UndirEdgeMap<_Value> { |
---|
| 422 | public: |
---|
| 423 | typedef typename Graph::template UndirEdgeMap<_Value> Parent; |
---|
| 424 | typedef typename Parent::Value Value; |
---|
| 425 | |
---|
| 426 | Map(const Graph& _graph) : Parent(_graph) {} |
---|
| 427 | Map(const Graph& _graph, const Value& _value) |
---|
| 428 | : Parent(_graph, _value) {} |
---|
| 429 | }; |
---|
| 430 | |
---|
| 431 | }; |
---|
[1192] | 432 | |
---|
| 433 | /// @} |
---|
[1402] | 434 | |
---|
| 435 | /// \addtogroup graph_maps |
---|
| 436 | /// @{ |
---|
| 437 | |
---|
| 438 | template <typename Map, typename Enable = void> |
---|
| 439 | struct ReferenceMapTraits { |
---|
| 440 | typedef typename Map::Value Value; |
---|
| 441 | typedef typename Map::Value& Reference; |
---|
| 442 | typedef const typename Map::Value& ConstReference; |
---|
| 443 | typedef typename Map::Value* Pointer; |
---|
| 444 | typedef const typename Map::Value* ConstPointer; |
---|
| 445 | }; |
---|
| 446 | |
---|
| 447 | template <typename Map> |
---|
| 448 | struct ReferenceMapTraits< |
---|
| 449 | Map, |
---|
| 450 | typename enable_if<typename Map::FullTypeTag, void>::type |
---|
| 451 | > { |
---|
| 452 | typedef typename Map::Value Value; |
---|
| 453 | typedef typename Map::Reference Reference; |
---|
| 454 | typedef typename Map::ConstReference ConstReference; |
---|
| 455 | typedef typename Map::Pointer Pointer; |
---|
| 456 | typedef typename Map::ConstPointer ConstPointer; |
---|
| 457 | }; |
---|
| 458 | |
---|
[1413] | 459 | /// Provides an immutable and unique id for each item in the graph. |
---|
| 460 | |
---|
[1526] | 461 | /// The IdMap class provides a unique and immutable mapping for each item |
---|
[1413] | 462 | /// in the graph. |
---|
| 463 | /// |
---|
| 464 | template <typename _Graph, typename _Item> |
---|
| 465 | class IdMap { |
---|
| 466 | public: |
---|
| 467 | typedef _Graph Graph; |
---|
| 468 | typedef int Value; |
---|
| 469 | typedef _Item Item; |
---|
| 470 | typedef _Item Key; |
---|
| 471 | |
---|
[1419] | 472 | typedef True NeedCopy; |
---|
| 473 | |
---|
[1413] | 474 | /// \brief Constructor. |
---|
| 475 | /// |
---|
| 476 | /// Constructor for creating id map. |
---|
| 477 | IdMap(const Graph& _graph) : graph(&_graph) {} |
---|
| 478 | |
---|
| 479 | /// \brief Gives back the \e id of the item. |
---|
| 480 | /// |
---|
| 481 | /// Gives back the immutable and unique \e id of the map. |
---|
| 482 | int operator[](const Item& item) const { return graph->id(item);} |
---|
| 483 | |
---|
| 484 | |
---|
| 485 | private: |
---|
| 486 | const Graph* graph; |
---|
| 487 | |
---|
| 488 | public: |
---|
| 489 | |
---|
| 490 | /// \brief The class represents the inverse of the map. |
---|
| 491 | /// |
---|
| 492 | /// The class represents the inverse of the map. |
---|
| 493 | /// \see inverse() |
---|
| 494 | class InverseMap { |
---|
| 495 | public: |
---|
[1419] | 496 | |
---|
| 497 | typedef True NeedCopy; |
---|
| 498 | |
---|
[1413] | 499 | /// \brief Constructor. |
---|
| 500 | /// |
---|
| 501 | /// Constructor for creating an id-to-item map. |
---|
| 502 | InverseMap(const Graph& _graph) : graph(&_graph) {} |
---|
| 503 | |
---|
| 504 | /// \brief Constructor. |
---|
| 505 | /// |
---|
| 506 | /// Constructor for creating an id-to-item map. |
---|
| 507 | InverseMap(const IdMap& idMap) : graph(idMap.graph) {} |
---|
| 508 | |
---|
| 509 | /// \brief Gives back the given item from its id. |
---|
| 510 | /// |
---|
| 511 | /// Gives back the given item from its id. |
---|
| 512 | /// |
---|
| 513 | Item operator[](int id) const { return graph->fromId(id, Item());} |
---|
| 514 | private: |
---|
| 515 | const Graph* graph; |
---|
| 516 | }; |
---|
| 517 | |
---|
| 518 | /// \brief Gives back the inverse of the map. |
---|
| 519 | /// |
---|
| 520 | /// Gives back the inverse of the map. |
---|
| 521 | InverseMap inverse() const { return InverseMap(*graph);} |
---|
| 522 | |
---|
| 523 | }; |
---|
| 524 | |
---|
| 525 | |
---|
[1526] | 526 | /// \brief General invertable graph-map type. |
---|
[1402] | 527 | |
---|
[1526] | 528 | /// This type provides simple invertable map functions. |
---|
| 529 | /// The InvertableMap wraps an arbitrary ReadWriteMap |
---|
| 530 | /// and if a key is set to a new value then store it |
---|
[1402] | 531 | /// in the inverse map. |
---|
| 532 | /// \param _Graph The graph type. |
---|
[1526] | 533 | /// \param _Map The map to extend with invertable functionality. |
---|
[1402] | 534 | template < |
---|
| 535 | typename _Graph, |
---|
| 536 | typename _Item, |
---|
| 537 | typename _Value, |
---|
| 538 | typename _Map |
---|
[1413] | 539 | = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent |
---|
[1402] | 540 | > |
---|
[1413] | 541 | class InvertableMap : protected _Map { |
---|
[1402] | 542 | |
---|
| 543 | public: |
---|
| 544 | |
---|
| 545 | typedef _Map Map; |
---|
| 546 | typedef _Graph Graph; |
---|
[1413] | 547 | |
---|
| 548 | /// The key type of InvertableMap (Node, Edge, UndirEdge). |
---|
[1402] | 549 | typedef typename _Map::Key Key; |
---|
[1413] | 550 | /// The value type of the InvertableMap. |
---|
[1402] | 551 | typedef typename _Map::Value Value; |
---|
| 552 | |
---|
| 553 | /// \brief Constructor. |
---|
| 554 | /// |
---|
[1413] | 555 | /// Construct a new InvertableMap for the graph. |
---|
[1402] | 556 | /// |
---|
[1413] | 557 | InvertableMap(const Graph& graph) : Map(graph) {} |
---|
[1402] | 558 | |
---|
| 559 | /// \brief The setter function of the map. |
---|
| 560 | /// |
---|
[1413] | 561 | /// Sets the mapped value. |
---|
[1402] | 562 | void set(const Key& key, const Value& val) { |
---|
| 563 | Value oldval = Map::operator[](key); |
---|
[1413] | 564 | typename Container::iterator it = invMap.find(oldval); |
---|
[1402] | 565 | if (it != invMap.end() && it->second == key) { |
---|
| 566 | invMap.erase(it); |
---|
| 567 | } |
---|
| 568 | invMap.insert(make_pair(val, key)); |
---|
| 569 | Map::set(key, val); |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | /// \brief The getter function of the map. |
---|
| 573 | /// |
---|
| 574 | /// It gives back the value associated with the key. |
---|
[1413] | 575 | const Value operator[](const Key& key) const { |
---|
[1402] | 576 | return Map::operator[](key); |
---|
| 577 | } |
---|
| 578 | |
---|
[1515] | 579 | protected: |
---|
| 580 | |
---|
[1402] | 581 | /// \brief Add a new key to the map. |
---|
| 582 | /// |
---|
| 583 | /// Add a new key to the map. It is called by the |
---|
| 584 | /// \c AlterationNotifier. |
---|
| 585 | virtual void add(const Key& key) { |
---|
| 586 | Map::add(key); |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | /// \brief Erase the key from the map. |
---|
| 590 | /// |
---|
| 591 | /// Erase the key to the map. It is called by the |
---|
| 592 | /// \c AlterationNotifier. |
---|
| 593 | virtual void erase(const Key& key) { |
---|
| 594 | Value val = Map::operator[](key); |
---|
[1413] | 595 | typename Container::iterator it = invMap.find(val); |
---|
[1402] | 596 | if (it != invMap.end() && it->second == key) { |
---|
| 597 | invMap.erase(it); |
---|
| 598 | } |
---|
| 599 | Map::erase(key); |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | /// \brief Clear the keys from the map and inverse map. |
---|
| 603 | /// |
---|
| 604 | /// Clear the keys from the map and inverse map. It is called by the |
---|
| 605 | /// \c AlterationNotifier. |
---|
| 606 | virtual void clear() { |
---|
| 607 | invMap.clear(); |
---|
| 608 | Map::clear(); |
---|
| 609 | } |
---|
| 610 | |
---|
[1413] | 611 | private: |
---|
| 612 | |
---|
| 613 | typedef std::map<Value, Key> Container; |
---|
| 614 | Container invMap; |
---|
| 615 | |
---|
| 616 | public: |
---|
| 617 | |
---|
| 618 | /// \brief The inverse map type. |
---|
| 619 | /// |
---|
| 620 | /// The inverse of this map. The subscript operator of the map |
---|
| 621 | /// gives back always the item what was last assigned to the value. |
---|
| 622 | class InverseMap { |
---|
| 623 | public: |
---|
| 624 | /// \brief Constructor of the InverseMap. |
---|
| 625 | /// |
---|
| 626 | /// Constructor of the InverseMap. |
---|
| 627 | InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {} |
---|
| 628 | |
---|
| 629 | /// The value type of the InverseMap. |
---|
| 630 | typedef typename InvertableMap::Key Value; |
---|
| 631 | /// The key type of the InverseMap. |
---|
| 632 | typedef typename InvertableMap::Value Key; |
---|
| 633 | |
---|
| 634 | /// \brief Subscript operator. |
---|
| 635 | /// |
---|
| 636 | /// Subscript operator. It gives back always the item |
---|
| 637 | /// what was last assigned to the value. |
---|
| 638 | Value operator[](const Key& key) const { |
---|
| 639 | typename Container::const_iterator it = inverted.invMap.find(key); |
---|
| 640 | return it->second; |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | private: |
---|
| 644 | const InvertableMap& inverted; |
---|
| 645 | }; |
---|
| 646 | |
---|
[1402] | 647 | /// \brief It gives back the just readeable inverse map. |
---|
| 648 | /// |
---|
| 649 | /// It gives back the just readeable inverse map. |
---|
[1413] | 650 | InverseMap inverse() const { |
---|
| 651 | return InverseMap(*this); |
---|
[1402] | 652 | } |
---|
| 653 | |
---|
| 654 | |
---|
[1413] | 655 | |
---|
[1402] | 656 | }; |
---|
| 657 | |
---|
| 658 | /// \brief Provides a mutable, continuous and unique descriptor for each |
---|
| 659 | /// item in the graph. |
---|
| 660 | /// |
---|
| 661 | /// The DescriptorMap class provides a mutable, continuous and immutable |
---|
[1413] | 662 | /// mapping for each item in the graph. The value for an item may mutated |
---|
| 663 | /// on each operation when the an item erased or added to graph. |
---|
[1402] | 664 | /// |
---|
| 665 | /// \param _Graph The graph class the \c DescriptorMap belongs to. |
---|
| 666 | /// \param _Item The Item is the Key of the Map. It may be Node, Edge or |
---|
| 667 | /// UndirEdge. |
---|
| 668 | /// \param _Map A ReadWriteMap mapping from the item type to integer. |
---|
| 669 | template < |
---|
| 670 | typename _Graph, |
---|
| 671 | typename _Item, |
---|
[1413] | 672 | typename _Map |
---|
| 673 | = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent |
---|
[1402] | 674 | > |
---|
| 675 | class DescriptorMap : protected _Map { |
---|
| 676 | |
---|
| 677 | typedef _Item Item; |
---|
| 678 | typedef _Map Map; |
---|
| 679 | |
---|
| 680 | public: |
---|
| 681 | /// The graph class of DescriptorMap. |
---|
| 682 | typedef _Graph Graph; |
---|
| 683 | |
---|
| 684 | /// The key type of DescriptorMap (Node, Edge, UndirEdge). |
---|
| 685 | typedef typename _Map::Key Key; |
---|
| 686 | /// The value type of DescriptorMap. |
---|
| 687 | typedef typename _Map::Value Value; |
---|
| 688 | |
---|
| 689 | /// \brief Constructor. |
---|
| 690 | /// |
---|
[1413] | 691 | /// Constructor for descriptor map. |
---|
[1402] | 692 | DescriptorMap(const Graph& _graph) : Map(_graph) { |
---|
| 693 | build(); |
---|
| 694 | } |
---|
| 695 | |
---|
[1515] | 696 | protected: |
---|
| 697 | |
---|
[1402] | 698 | /// \brief Add a new key to the map. |
---|
| 699 | /// |
---|
| 700 | /// Add a new key to the map. It is called by the |
---|
| 701 | /// \c AlterationNotifier. |
---|
| 702 | virtual void add(const Item& item) { |
---|
| 703 | Map::add(item); |
---|
| 704 | Map::set(item, invMap.size()); |
---|
| 705 | invMap.push_back(item); |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | /// \brief Erase the key from the map. |
---|
| 709 | /// |
---|
| 710 | /// Erase the key to the map. It is called by the |
---|
| 711 | /// \c AlterationNotifier. |
---|
| 712 | virtual void erase(const Item& item) { |
---|
| 713 | Map::set(invMap.back(), Map::operator[](item)); |
---|
| 714 | invMap[Map::operator[](item)] = invMap.back(); |
---|
[1413] | 715 | invMap.pop_back(); |
---|
[1402] | 716 | Map::erase(item); |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | /// \brief Build the unique map. |
---|
| 720 | /// |
---|
| 721 | /// Build the unique map. It is called by the |
---|
| 722 | /// \c AlterationNotifier. |
---|
| 723 | virtual void build() { |
---|
| 724 | Map::build(); |
---|
| 725 | Item it; |
---|
| 726 | const typename Map::Graph* graph = Map::getGraph(); |
---|
| 727 | for (graph->first(it); it != INVALID; graph->next(it)) { |
---|
| 728 | Map::set(it, invMap.size()); |
---|
| 729 | invMap.push_back(it); |
---|
| 730 | } |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | /// \brief Clear the keys from the map. |
---|
| 734 | /// |
---|
| 735 | /// Clear the keys from the map. It is called by the |
---|
| 736 | /// \c AlterationNotifier. |
---|
| 737 | virtual void clear() { |
---|
| 738 | invMap.clear(); |
---|
| 739 | Map::clear(); |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | /// \brief Gives back the \e descriptor of the item. |
---|
| 743 | /// |
---|
| 744 | /// Gives back the mutable and unique \e descriptor of the map. |
---|
| 745 | int operator[](const Item& item) const { |
---|
| 746 | return Map::operator[](item); |
---|
| 747 | } |
---|
| 748 | |
---|
[1413] | 749 | private: |
---|
| 750 | |
---|
| 751 | typedef std::vector<Item> Container; |
---|
| 752 | Container invMap; |
---|
| 753 | |
---|
| 754 | public: |
---|
| 755 | /// \brief The inverse map type. |
---|
| 756 | /// |
---|
| 757 | /// The inverse map type. |
---|
| 758 | class InverseMap { |
---|
| 759 | public: |
---|
| 760 | /// \brief Constructor of the InverseMap. |
---|
| 761 | /// |
---|
| 762 | /// Constructor of the InverseMap. |
---|
| 763 | InverseMap(const DescriptorMap& _inverted) |
---|
| 764 | : inverted(_inverted) {} |
---|
| 765 | |
---|
| 766 | |
---|
| 767 | /// The value type of the InverseMap. |
---|
| 768 | typedef typename DescriptorMap::Key Value; |
---|
| 769 | /// The key type of the InverseMap. |
---|
| 770 | typedef typename DescriptorMap::Value Key; |
---|
| 771 | |
---|
| 772 | /// \brief Subscript operator. |
---|
| 773 | /// |
---|
| 774 | /// Subscript operator. It gives back the item |
---|
| 775 | /// that the descriptor belongs to currently. |
---|
| 776 | Value operator[](const Key& key) const { |
---|
| 777 | return inverted.invMap[key]; |
---|
| 778 | } |
---|
[1470] | 779 | |
---|
| 780 | /// \brief Size of the map. |
---|
| 781 | /// |
---|
| 782 | /// Returns the size of the map. |
---|
| 783 | unsigned size() const { |
---|
| 784 | return inverted.invMap.size(); |
---|
| 785 | } |
---|
[1413] | 786 | |
---|
| 787 | private: |
---|
| 788 | const DescriptorMap& inverted; |
---|
| 789 | }; |
---|
| 790 | |
---|
[1402] | 791 | /// \brief Gives back the inverse of the map. |
---|
| 792 | /// |
---|
| 793 | /// Gives back the inverse of the map. |
---|
| 794 | const InverseMap inverse() const { |
---|
[1413] | 795 | return InverseMap(*this); |
---|
[1402] | 796 | } |
---|
| 797 | }; |
---|
| 798 | |
---|
| 799 | /// \brief Returns the source of the given edge. |
---|
| 800 | /// |
---|
| 801 | /// The SourceMap gives back the source Node of the given edge. |
---|
| 802 | /// \author Balazs Dezso |
---|
| 803 | template <typename Graph> |
---|
| 804 | class SourceMap { |
---|
| 805 | public: |
---|
[1419] | 806 | |
---|
| 807 | typedef True NeedCopy; |
---|
| 808 | |
---|
[1402] | 809 | typedef typename Graph::Node Value; |
---|
| 810 | typedef typename Graph::Edge Key; |
---|
| 811 | |
---|
| 812 | /// \brief Constructor |
---|
| 813 | /// |
---|
| 814 | /// Constructor |
---|
| 815 | /// \param _graph The graph that the map belongs to. |
---|
| 816 | SourceMap(const Graph& _graph) : graph(_graph) {} |
---|
| 817 | |
---|
| 818 | /// \brief The subscript operator. |
---|
| 819 | /// |
---|
| 820 | /// The subscript operator. |
---|
| 821 | /// \param edge The edge |
---|
| 822 | /// \return The source of the edge |
---|
| 823 | Value operator[](const Key& edge) { |
---|
| 824 | return graph.source(edge); |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | private: |
---|
| 828 | const Graph& graph; |
---|
| 829 | }; |
---|
| 830 | |
---|
| 831 | /// \brief Returns a \ref SourceMap class |
---|
| 832 | /// |
---|
| 833 | /// This function just returns an \ref SourceMap class. |
---|
| 834 | /// \relates SourceMap |
---|
| 835 | template <typename Graph> |
---|
| 836 | inline SourceMap<Graph> sourceMap(const Graph& graph) { |
---|
| 837 | return SourceMap<Graph>(graph); |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | /// \brief Returns the target of the given edge. |
---|
| 841 | /// |
---|
| 842 | /// The TargetMap gives back the target Node of the given edge. |
---|
| 843 | /// \author Balazs Dezso |
---|
| 844 | template <typename Graph> |
---|
| 845 | class TargetMap { |
---|
| 846 | public: |
---|
[1419] | 847 | |
---|
| 848 | typedef True NeedCopy; |
---|
| 849 | |
---|
[1402] | 850 | typedef typename Graph::Node Value; |
---|
| 851 | typedef typename Graph::Edge Key; |
---|
| 852 | |
---|
| 853 | /// \brief Constructor |
---|
| 854 | /// |
---|
| 855 | /// Constructor |
---|
| 856 | /// \param _graph The graph that the map belongs to. |
---|
| 857 | TargetMap(const Graph& _graph) : graph(_graph) {} |
---|
| 858 | |
---|
| 859 | /// \brief The subscript operator. |
---|
| 860 | /// |
---|
| 861 | /// The subscript operator. |
---|
[1536] | 862 | /// \param e The edge |
---|
[1402] | 863 | /// \return The target of the edge |
---|
[1536] | 864 | Value operator[](const Key& e) { |
---|
| 865 | return graph.target(e); |
---|
[1402] | 866 | } |
---|
| 867 | |
---|
| 868 | private: |
---|
| 869 | const Graph& graph; |
---|
| 870 | }; |
---|
| 871 | |
---|
| 872 | /// \brief Returns a \ref TargetMap class |
---|
[1515] | 873 | /// |
---|
[1402] | 874 | /// This function just returns an \ref TargetMap class. |
---|
| 875 | /// \relates TargetMap |
---|
| 876 | template <typename Graph> |
---|
| 877 | inline TargetMap<Graph> targetMap(const Graph& graph) { |
---|
| 878 | return TargetMap<Graph>(graph); |
---|
| 879 | } |
---|
| 880 | |
---|
[1419] | 881 | /// \brief Returns the "forward" directed edge view of undirected edge. |
---|
| 882 | /// |
---|
| 883 | /// Returns the "forward" directed edge view of undirected edge. |
---|
| 884 | /// \author Balazs Dezso |
---|
| 885 | template <typename Graph> |
---|
| 886 | class ForwardMap { |
---|
| 887 | public: |
---|
| 888 | |
---|
| 889 | typedef True NeedCopy; |
---|
| 890 | |
---|
| 891 | typedef typename Graph::Edge Value; |
---|
| 892 | typedef typename Graph::UndirEdge Key; |
---|
| 893 | |
---|
| 894 | /// \brief Constructor |
---|
| 895 | /// |
---|
| 896 | /// Constructor |
---|
| 897 | /// \param _graph The graph that the map belongs to. |
---|
| 898 | ForwardMap(const Graph& _graph) : graph(_graph) {} |
---|
| 899 | |
---|
| 900 | /// \brief The subscript operator. |
---|
| 901 | /// |
---|
| 902 | /// The subscript operator. |
---|
| 903 | /// \param key An undirected edge |
---|
| 904 | /// \return The "forward" directed edge view of undirected edge |
---|
| 905 | Value operator[](const Key& key) const { |
---|
| 906 | return graph.edgeWithSource(key, graph.source(key)); |
---|
| 907 | } |
---|
| 908 | |
---|
| 909 | private: |
---|
| 910 | const Graph& graph; |
---|
| 911 | }; |
---|
| 912 | |
---|
| 913 | /// \brief Returns a \ref ForwardMap class |
---|
[1515] | 914 | /// |
---|
[1419] | 915 | /// This function just returns an \ref ForwardMap class. |
---|
| 916 | /// \relates ForwardMap |
---|
| 917 | template <typename Graph> |
---|
| 918 | inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
---|
| 919 | return ForwardMap<Graph>(graph); |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | /// \brief Returns the "backward" directed edge view of undirected edge. |
---|
| 923 | /// |
---|
| 924 | /// Returns the "backward" directed edge view of undirected edge. |
---|
| 925 | /// \author Balazs Dezso |
---|
| 926 | template <typename Graph> |
---|
| 927 | class BackwardMap { |
---|
| 928 | public: |
---|
| 929 | typedef True NeedCopy; |
---|
| 930 | |
---|
| 931 | typedef typename Graph::Edge Value; |
---|
| 932 | typedef typename Graph::UndirEdge Key; |
---|
| 933 | |
---|
| 934 | /// \brief Constructor |
---|
| 935 | /// |
---|
| 936 | /// Constructor |
---|
| 937 | /// \param _graph The graph that the map belongs to. |
---|
| 938 | BackwardMap(const Graph& _graph) : graph(_graph) {} |
---|
| 939 | |
---|
| 940 | /// \brief The subscript operator. |
---|
| 941 | /// |
---|
| 942 | /// The subscript operator. |
---|
| 943 | /// \param key An undirected edge |
---|
| 944 | /// \return The "backward" directed edge view of undirected edge |
---|
| 945 | Value operator[](const Key& key) const { |
---|
| 946 | return graph.edgeWithSource(key, graph.target(key)); |
---|
| 947 | } |
---|
| 948 | |
---|
| 949 | private: |
---|
| 950 | const Graph& graph; |
---|
| 951 | }; |
---|
| 952 | |
---|
| 953 | /// \brief Returns a \ref BackwardMap class |
---|
| 954 | |
---|
| 955 | /// This function just returns an \ref BackwardMap class. |
---|
| 956 | /// \relates BackwardMap |
---|
| 957 | template <typename Graph> |
---|
| 958 | inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
---|
| 959 | return BackwardMap<Graph>(graph); |
---|
| 960 | } |
---|
| 961 | |
---|
[1515] | 962 | template <typename _Graph> |
---|
| 963 | class DegMapBase { |
---|
| 964 | public: |
---|
| 965 | |
---|
| 966 | typedef _Graph Graph; |
---|
[1402] | 967 | |
---|
[1515] | 968 | protected: |
---|
[1453] | 969 | |
---|
[1515] | 970 | typedef typename Graph::template NodeMap<int> IntNodeMap; |
---|
| 971 | |
---|
| 972 | }; |
---|
[1453] | 973 | |
---|
[1515] | 974 | /// \brief Map of the node in-degrees. |
---|
[1453] | 975 | /// |
---|
[1515] | 976 | /// This map returns the in-degree of a node. Ones it is constructed, |
---|
| 977 | /// the degrees are stored in a standard NodeMap, so each query is done |
---|
| 978 | /// in constant time. On the other hand, the values updates automatically |
---|
| 979 | /// whenever the graph changes. |
---|
| 980 | /// |
---|
| 981 | /// \sa OutDegMap |
---|
| 982 | |
---|
[1453] | 983 | template <typename _Graph> |
---|
[1515] | 984 | class InDegMap |
---|
| 985 | : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase { |
---|
| 986 | |
---|
[1453] | 987 | public: |
---|
[1515] | 988 | |
---|
| 989 | typedef _Graph Graph; |
---|
[1453] | 990 | typedef int Value; |
---|
[1515] | 991 | typedef typename Graph::Node Key; |
---|
| 992 | |
---|
| 993 | private: |
---|
| 994 | |
---|
| 995 | class AutoNodeMap : public Graph::template NodeMap<int> { |
---|
| 996 | public: |
---|
| 997 | |
---|
| 998 | typedef typename Graph::template NodeMap<int> Parent; |
---|
| 999 | |
---|
| 1000 | typedef typename Parent::Key Key; |
---|
| 1001 | typedef typename Parent::Value Value; |
---|
| 1002 | |
---|
| 1003 | AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} |
---|
| 1004 | |
---|
| 1005 | void add(const Key& key) { |
---|
| 1006 | Parent::add(key); |
---|
| 1007 | Parent::set(key, 0); |
---|
| 1008 | } |
---|
| 1009 | }; |
---|
| 1010 | |
---|
| 1011 | public: |
---|
[1453] | 1012 | |
---|
| 1013 | /// \brief Constructor. |
---|
| 1014 | /// |
---|
| 1015 | /// Constructor for creating in-degree map. |
---|
[1515] | 1016 | InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { |
---|
[1459] | 1017 | AlterationNotifier<typename _Graph::Edge> |
---|
| 1018 | ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge())); |
---|
[1515] | 1019 | |
---|
| 1020 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1021 | deg[it] = countInEdges(graph, it); |
---|
| 1022 | } |
---|
[1453] | 1023 | } |
---|
| 1024 | |
---|
[1515] | 1025 | virtual ~InDegMap() { |
---|
[1459] | 1026 | AlterationNotifier<typename _Graph::Edge>:: |
---|
[1453] | 1027 | ObserverBase::detach(); |
---|
| 1028 | } |
---|
| 1029 | |
---|
[1459] | 1030 | /// Gives back the in-degree of a Node. |
---|
[1515] | 1031 | int operator[](const Key& key) const { |
---|
| 1032 | return deg[key]; |
---|
[1459] | 1033 | } |
---|
[1453] | 1034 | |
---|
| 1035 | protected: |
---|
[1515] | 1036 | |
---|
| 1037 | typedef typename Graph::Edge Edge; |
---|
| 1038 | |
---|
| 1039 | virtual void add(const Edge& edge) { |
---|
| 1040 | ++deg[graph.target(edge)]; |
---|
[1453] | 1041 | } |
---|
| 1042 | |
---|
[1515] | 1043 | virtual void erase(const Edge& edge) { |
---|
| 1044 | --deg[graph.target(edge)]; |
---|
| 1045 | } |
---|
| 1046 | |
---|
| 1047 | |
---|
| 1048 | virtual void build() { |
---|
| 1049 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1050 | deg[it] = countInEdges(graph, it); |
---|
| 1051 | } |
---|
| 1052 | } |
---|
| 1053 | |
---|
| 1054 | virtual void clear() { |
---|
| 1055 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1056 | deg[it] = 0; |
---|
| 1057 | } |
---|
| 1058 | } |
---|
| 1059 | private: |
---|
[1506] | 1060 | |
---|
[1515] | 1061 | const _Graph& graph; |
---|
| 1062 | AutoNodeMap deg; |
---|
[1459] | 1063 | }; |
---|
| 1064 | |
---|
| 1065 | |
---|
[1515] | 1066 | /// \brief Map of the node out-degrees. |
---|
| 1067 | /// |
---|
| 1068 | /// This map returns the out-degree of a node. One it is constructed, |
---|
| 1069 | /// the degrees are stored in a standard NodeMap, so each query is done |
---|
| 1070 | /// in constant time. On the other hand, the values updates automatically |
---|
| 1071 | /// whenever the graph changes. |
---|
| 1072 | /// |
---|
| 1073 | /// \sa OutDegMap |
---|
[1459] | 1074 | |
---|
| 1075 | template <typename _Graph> |
---|
[1515] | 1076 | class OutDegMap |
---|
| 1077 | : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase { |
---|
| 1078 | |
---|
[1459] | 1079 | public: |
---|
[1515] | 1080 | |
---|
| 1081 | typedef _Graph Graph; |
---|
[1459] | 1082 | typedef int Value; |
---|
[1515] | 1083 | typedef typename Graph::Node Key; |
---|
| 1084 | |
---|
| 1085 | private: |
---|
| 1086 | |
---|
| 1087 | class AutoNodeMap : public Graph::template NodeMap<int> { |
---|
| 1088 | public: |
---|
| 1089 | |
---|
| 1090 | typedef typename Graph::template NodeMap<int> Parent; |
---|
| 1091 | |
---|
| 1092 | typedef typename Parent::Key Key; |
---|
| 1093 | typedef typename Parent::Value Value; |
---|
| 1094 | |
---|
| 1095 | AutoNodeMap(const Graph& graph) : Parent(graph, 0) {} |
---|
| 1096 | |
---|
| 1097 | void add(const Key& key) { |
---|
| 1098 | Parent::add(key); |
---|
| 1099 | Parent::set(key, 0); |
---|
| 1100 | } |
---|
| 1101 | }; |
---|
| 1102 | |
---|
| 1103 | public: |
---|
[1459] | 1104 | |
---|
| 1105 | /// \brief Constructor. |
---|
| 1106 | /// |
---|
| 1107 | /// Constructor for creating out-degree map. |
---|
[1515] | 1108 | OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { |
---|
[1459] | 1109 | AlterationNotifier<typename _Graph::Edge> |
---|
| 1110 | ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge())); |
---|
[1515] | 1111 | |
---|
| 1112 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1113 | deg[it] = countOutEdges(graph, it); |
---|
| 1114 | } |
---|
[1459] | 1115 | } |
---|
| 1116 | |
---|
[1515] | 1117 | virtual ~OutDegMap() { |
---|
[1459] | 1118 | AlterationNotifier<typename _Graph::Edge>:: |
---|
| 1119 | ObserverBase::detach(); |
---|
| 1120 | } |
---|
| 1121 | |
---|
| 1122 | /// Gives back the in-degree of a Node. |
---|
[1515] | 1123 | int operator[](const Key& key) const { |
---|
| 1124 | return deg[key]; |
---|
[1459] | 1125 | } |
---|
| 1126 | |
---|
| 1127 | protected: |
---|
[1515] | 1128 | |
---|
| 1129 | typedef typename Graph::Edge Edge; |
---|
| 1130 | |
---|
| 1131 | virtual void add(const Edge& edge) { |
---|
| 1132 | ++deg[graph.source(edge)]; |
---|
[1459] | 1133 | } |
---|
| 1134 | |
---|
[1515] | 1135 | virtual void erase(const Edge& edge) { |
---|
| 1136 | --deg[graph.source(edge)]; |
---|
| 1137 | } |
---|
| 1138 | |
---|
| 1139 | |
---|
| 1140 | virtual void build() { |
---|
| 1141 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1142 | deg[it] = countOutEdges(graph, it); |
---|
| 1143 | } |
---|
| 1144 | } |
---|
| 1145 | |
---|
| 1146 | virtual void clear() { |
---|
| 1147 | for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { |
---|
| 1148 | deg[it] = 0; |
---|
| 1149 | } |
---|
| 1150 | } |
---|
| 1151 | private: |
---|
[1506] | 1152 | |
---|
[1515] | 1153 | const _Graph& graph; |
---|
| 1154 | AutoNodeMap deg; |
---|
[1453] | 1155 | }; |
---|
| 1156 | |
---|
[1402] | 1157 | /// @} |
---|
| 1158 | |
---|
[947] | 1159 | } //END OF NAMESPACE LEMON |
---|
[946] | 1160 | |
---|
| 1161 | #endif |
---|