[416] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[414] | 2 | * |
---|
[416] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[414] | 4 | * |
---|
[454] | 5 | * Copyright (C) 2003-2009 |
---|
[414] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[416] | 19 | #ifndef LEMON_ADAPTORS_H |
---|
| 20 | #define LEMON_ADAPTORS_H |
---|
| 21 | |
---|
| 22 | /// \ingroup graph_adaptors |
---|
| 23 | /// \file |
---|
[451] | 24 | /// \brief Adaptor classes for digraphs and graphs |
---|
[414] | 25 | /// |
---|
[416] | 26 | /// This file contains several useful adaptors for digraphs and graphs. |
---|
[414] | 27 | |
---|
| 28 | #include <lemon/core.h> |
---|
| 29 | #include <lemon/maps.h> |
---|
| 30 | #include <lemon/bits/variant.h> |
---|
| 31 | |
---|
| 32 | #include <lemon/bits/graph_adaptor_extender.h> |
---|
[519] | 33 | #include <lemon/bits/map_extender.h> |
---|
[414] | 34 | #include <lemon/tolerance.h> |
---|
| 35 | |
---|
| 36 | #include <algorithm> |
---|
| 37 | |
---|
| 38 | namespace lemon { |
---|
| 39 | |
---|
[512] | 40 | #ifdef _MSC_VER |
---|
| 41 | #define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED |
---|
| 42 | #else |
---|
| 43 | #define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED |
---|
| 44 | #endif |
---|
| 45 | |
---|
| 46 | template<typename DGR> |
---|
[414] | 47 | class DigraphAdaptorBase { |
---|
| 48 | public: |
---|
[512] | 49 | typedef DGR Digraph; |
---|
[414] | 50 | typedef DigraphAdaptorBase Adaptor; |
---|
| 51 | |
---|
| 52 | protected: |
---|
[512] | 53 | DGR* _digraph; |
---|
[414] | 54 | DigraphAdaptorBase() : _digraph(0) { } |
---|
[512] | 55 | void initialize(DGR& digraph) { _digraph = &digraph; } |
---|
[414] | 56 | |
---|
| 57 | public: |
---|
[512] | 58 | DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { } |
---|
| 59 | |
---|
| 60 | typedef typename DGR::Node Node; |
---|
| 61 | typedef typename DGR::Arc Arc; |
---|
[416] | 62 | |
---|
[414] | 63 | void first(Node& i) const { _digraph->first(i); } |
---|
| 64 | void first(Arc& i) const { _digraph->first(i); } |
---|
| 65 | void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
---|
| 66 | void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
---|
| 67 | |
---|
| 68 | void next(Node& i) const { _digraph->next(i); } |
---|
| 69 | void next(Arc& i) const { _digraph->next(i); } |
---|
| 70 | void nextIn(Arc& i) const { _digraph->nextIn(i); } |
---|
| 71 | void nextOut(Arc& i) const { _digraph->nextOut(i); } |
---|
| 72 | |
---|
| 73 | Node source(const Arc& a) const { return _digraph->source(a); } |
---|
| 74 | Node target(const Arc& a) const { return _digraph->target(a); } |
---|
| 75 | |
---|
[512] | 76 | typedef NodeNumTagIndicator<DGR> NodeNumTag; |
---|
[414] | 77 | int nodeNum() const { return _digraph->nodeNum(); } |
---|
[416] | 78 | |
---|
[512] | 79 | typedef ArcNumTagIndicator<DGR> ArcNumTag; |
---|
[414] | 80 | int arcNum() const { return _digraph->arcNum(); } |
---|
| 81 | |
---|
[512] | 82 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
[448] | 83 | Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const { |
---|
[414] | 84 | return _digraph->findArc(u, v, prev); |
---|
| 85 | } |
---|
[416] | 86 | |
---|
[414] | 87 | Node addNode() { return _digraph->addNode(); } |
---|
| 88 | Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
---|
| 89 | |
---|
[448] | 90 | void erase(const Node& n) { _digraph->erase(n); } |
---|
| 91 | void erase(const Arc& a) { _digraph->erase(a); } |
---|
| 92 | |
---|
| 93 | void clear() { _digraph->clear(); } |
---|
[416] | 94 | |
---|
[414] | 95 | int id(const Node& n) const { return _digraph->id(n); } |
---|
| 96 | int id(const Arc& a) const { return _digraph->id(a); } |
---|
| 97 | |
---|
| 98 | Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
---|
| 99 | Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
---|
| 100 | |
---|
| 101 | int maxNodeId() const { return _digraph->maxNodeId(); } |
---|
| 102 | int maxArcId() const { return _digraph->maxArcId(); } |
---|
| 103 | |
---|
[512] | 104 | typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
---|
[416] | 105 | NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
---|
[414] | 106 | |
---|
[512] | 107 | typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier; |
---|
[416] | 108 | ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
---|
| 109 | |
---|
[512] | 110 | template <typename V> |
---|
| 111 | class NodeMap : public DGR::template NodeMap<V> { |
---|
[617] | 112 | typedef typename DGR::template NodeMap<V> Parent; |
---|
| 113 | |
---|
[414] | 114 | public: |
---|
[416] | 115 | explicit NodeMap(const Adaptor& adaptor) |
---|
| 116 | : Parent(*adaptor._digraph) {} |
---|
[512] | 117 | NodeMap(const Adaptor& adaptor, const V& value) |
---|
[416] | 118 | : Parent(*adaptor._digraph, value) { } |
---|
[414] | 119 | |
---|
| 120 | private: |
---|
| 121 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 122 | return operator=<NodeMap>(cmap); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | template <typename CMap> |
---|
| 126 | NodeMap& operator=(const CMap& cmap) { |
---|
| 127 | Parent::operator=(cmap); |
---|
| 128 | return *this; |
---|
| 129 | } |
---|
[416] | 130 | |
---|
[414] | 131 | }; |
---|
| 132 | |
---|
[512] | 133 | template <typename V> |
---|
| 134 | class ArcMap : public DGR::template ArcMap<V> { |
---|
[617] | 135 | typedef typename DGR::template ArcMap<V> Parent; |
---|
| 136 | |
---|
[414] | 137 | public: |
---|
[512] | 138 | explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor) |
---|
[416] | 139 | : Parent(*adaptor._digraph) {} |
---|
[512] | 140 | ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value) |
---|
[416] | 141 | : Parent(*adaptor._digraph, value) {} |
---|
[414] | 142 | |
---|
| 143 | private: |
---|
| 144 | ArcMap& operator=(const ArcMap& cmap) { |
---|
| 145 | return operator=<ArcMap>(cmap); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | template <typename CMap> |
---|
| 149 | ArcMap& operator=(const CMap& cmap) { |
---|
| 150 | Parent::operator=(cmap); |
---|
| 151 | return *this; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | }; |
---|
| 155 | |
---|
| 156 | }; |
---|
| 157 | |
---|
[512] | 158 | template<typename GR> |
---|
[416] | 159 | class GraphAdaptorBase { |
---|
| 160 | public: |
---|
[512] | 161 | typedef GR Graph; |
---|
[416] | 162 | |
---|
| 163 | protected: |
---|
[512] | 164 | GR* _graph; |
---|
[416] | 165 | |
---|
| 166 | GraphAdaptorBase() : _graph(0) {} |
---|
| 167 | |
---|
[512] | 168 | void initialize(GR& graph) { _graph = &graph; } |
---|
[416] | 169 | |
---|
| 170 | public: |
---|
[512] | 171 | GraphAdaptorBase(GR& graph) : _graph(&graph) {} |
---|
| 172 | |
---|
| 173 | typedef typename GR::Node Node; |
---|
| 174 | typedef typename GR::Arc Arc; |
---|
| 175 | typedef typename GR::Edge Edge; |
---|
[416] | 176 | |
---|
| 177 | void first(Node& i) const { _graph->first(i); } |
---|
| 178 | void first(Arc& i) const { _graph->first(i); } |
---|
| 179 | void first(Edge& i) const { _graph->first(i); } |
---|
| 180 | void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } |
---|
| 181 | void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } |
---|
| 182 | void firstInc(Edge &i, bool &d, const Node &n) const { |
---|
| 183 | _graph->firstInc(i, d, n); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void next(Node& i) const { _graph->next(i); } |
---|
| 187 | void next(Arc& i) const { _graph->next(i); } |
---|
| 188 | void next(Edge& i) const { _graph->next(i); } |
---|
| 189 | void nextIn(Arc& i) const { _graph->nextIn(i); } |
---|
| 190 | void nextOut(Arc& i) const { _graph->nextOut(i); } |
---|
| 191 | void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } |
---|
| 192 | |
---|
| 193 | Node u(const Edge& e) const { return _graph->u(e); } |
---|
| 194 | Node v(const Edge& e) const { return _graph->v(e); } |
---|
| 195 | |
---|
| 196 | Node source(const Arc& a) const { return _graph->source(a); } |
---|
| 197 | Node target(const Arc& a) const { return _graph->target(a); } |
---|
| 198 | |
---|
| 199 | typedef NodeNumTagIndicator<Graph> NodeNumTag; |
---|
| 200 | int nodeNum() const { return _graph->nodeNum(); } |
---|
| 201 | |
---|
[446] | 202 | typedef ArcNumTagIndicator<Graph> ArcNumTag; |
---|
| 203 | int arcNum() const { return _graph->arcNum(); } |
---|
| 204 | |
---|
[416] | 205 | typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
---|
| 206 | int edgeNum() const { return _graph->edgeNum(); } |
---|
| 207 | |
---|
[446] | 208 | typedef FindArcTagIndicator<Graph> FindArcTag; |
---|
[448] | 209 | Arc findArc(const Node& u, const Node& v, |
---|
| 210 | const Arc& prev = INVALID) const { |
---|
[416] | 211 | return _graph->findArc(u, v, prev); |
---|
| 212 | } |
---|
[446] | 213 | |
---|
| 214 | typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
---|
[448] | 215 | Edge findEdge(const Node& u, const Node& v, |
---|
| 216 | const Edge& prev = INVALID) const { |
---|
[416] | 217 | return _graph->findEdge(u, v, prev); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | Node addNode() { return _graph->addNode(); } |
---|
| 221 | Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } |
---|
| 222 | |
---|
| 223 | void erase(const Node& i) { _graph->erase(i); } |
---|
| 224 | void erase(const Edge& i) { _graph->erase(i); } |
---|
| 225 | |
---|
| 226 | void clear() { _graph->clear(); } |
---|
| 227 | |
---|
| 228 | bool direction(const Arc& a) const { return _graph->direction(a); } |
---|
| 229 | Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } |
---|
| 230 | |
---|
| 231 | int id(const Node& v) const { return _graph->id(v); } |
---|
| 232 | int id(const Arc& a) const { return _graph->id(a); } |
---|
| 233 | int id(const Edge& e) const { return _graph->id(e); } |
---|
| 234 | |
---|
| 235 | Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
---|
| 236 | Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } |
---|
| 237 | Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } |
---|
| 238 | |
---|
| 239 | int maxNodeId() const { return _graph->maxNodeId(); } |
---|
| 240 | int maxArcId() const { return _graph->maxArcId(); } |
---|
| 241 | int maxEdgeId() const { return _graph->maxEdgeId(); } |
---|
| 242 | |
---|
[512] | 243 | typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
---|
[416] | 244 | NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
---|
| 245 | |
---|
[512] | 246 | typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
---|
[416] | 247 | ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
---|
| 248 | |
---|
[512] | 249 | typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier; |
---|
[416] | 250 | EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } |
---|
| 251 | |
---|
[512] | 252 | template <typename V> |
---|
| 253 | class NodeMap : public GR::template NodeMap<V> { |
---|
[617] | 254 | typedef typename GR::template NodeMap<V> Parent; |
---|
| 255 | |
---|
[416] | 256 | public: |
---|
[512] | 257 | explicit NodeMap(const GraphAdaptorBase<GR>& adapter) |
---|
[416] | 258 | : Parent(*adapter._graph) {} |
---|
[512] | 259 | NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
---|
[416] | 260 | : Parent(*adapter._graph, value) {} |
---|
| 261 | |
---|
| 262 | private: |
---|
| 263 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 264 | return operator=<NodeMap>(cmap); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | template <typename CMap> |
---|
| 268 | NodeMap& operator=(const CMap& cmap) { |
---|
| 269 | Parent::operator=(cmap); |
---|
| 270 | return *this; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | }; |
---|
| 274 | |
---|
[512] | 275 | template <typename V> |
---|
| 276 | class ArcMap : public GR::template ArcMap<V> { |
---|
[617] | 277 | typedef typename GR::template ArcMap<V> Parent; |
---|
| 278 | |
---|
[416] | 279 | public: |
---|
[512] | 280 | explicit ArcMap(const GraphAdaptorBase<GR>& adapter) |
---|
[416] | 281 | : Parent(*adapter._graph) {} |
---|
[512] | 282 | ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
---|
[416] | 283 | : Parent(*adapter._graph, value) {} |
---|
| 284 | |
---|
| 285 | private: |
---|
| 286 | ArcMap& operator=(const ArcMap& cmap) { |
---|
| 287 | return operator=<ArcMap>(cmap); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | template <typename CMap> |
---|
| 291 | ArcMap& operator=(const CMap& cmap) { |
---|
| 292 | Parent::operator=(cmap); |
---|
| 293 | return *this; |
---|
| 294 | } |
---|
| 295 | }; |
---|
| 296 | |
---|
[512] | 297 | template <typename V> |
---|
| 298 | class EdgeMap : public GR::template EdgeMap<V> { |
---|
[617] | 299 | typedef typename GR::template EdgeMap<V> Parent; |
---|
| 300 | |
---|
[416] | 301 | public: |
---|
[512] | 302 | explicit EdgeMap(const GraphAdaptorBase<GR>& adapter) |
---|
[416] | 303 | : Parent(*adapter._graph) {} |
---|
[512] | 304 | EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
---|
[416] | 305 | : Parent(*adapter._graph, value) {} |
---|
| 306 | |
---|
| 307 | private: |
---|
| 308 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
| 309 | return operator=<EdgeMap>(cmap); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | template <typename CMap> |
---|
| 313 | EdgeMap& operator=(const CMap& cmap) { |
---|
| 314 | Parent::operator=(cmap); |
---|
| 315 | return *this; |
---|
| 316 | } |
---|
| 317 | }; |
---|
| 318 | |
---|
| 319 | }; |
---|
[414] | 320 | |
---|
[512] | 321 | template <typename DGR> |
---|
| 322 | class ReverseDigraphBase : public DigraphAdaptorBase<DGR> { |
---|
[617] | 323 | typedef DigraphAdaptorBase<DGR> Parent; |
---|
[414] | 324 | public: |
---|
[512] | 325 | typedef DGR Digraph; |
---|
[414] | 326 | protected: |
---|
[416] | 327 | ReverseDigraphBase() : Parent() { } |
---|
[414] | 328 | public: |
---|
| 329 | typedef typename Parent::Node Node; |
---|
| 330 | typedef typename Parent::Arc Arc; |
---|
| 331 | |
---|
| 332 | void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } |
---|
| 333 | void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } |
---|
| 334 | |
---|
| 335 | void nextIn(Arc& a) const { Parent::nextOut(a); } |
---|
| 336 | void nextOut(Arc& a) const { Parent::nextIn(a); } |
---|
| 337 | |
---|
| 338 | Node source(const Arc& a) const { return Parent::target(a); } |
---|
| 339 | Node target(const Arc& a) const { return Parent::source(a); } |
---|
| 340 | |
---|
[416] | 341 | Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); } |
---|
| 342 | |
---|
[512] | 343 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
[416] | 344 | Arc findArc(const Node& u, const Node& v, |
---|
[448] | 345 | const Arc& prev = INVALID) const { |
---|
[414] | 346 | return Parent::findArc(v, u, prev); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | }; |
---|
[416] | 350 | |
---|
| 351 | /// \ingroup graph_adaptors |
---|
[414] | 352 | /// |
---|
[451] | 353 | /// \brief Adaptor class for reversing the orientation of the arcs in |
---|
| 354 | /// a digraph. |
---|
[414] | 355 | /// |
---|
[451] | 356 | /// ReverseDigraph can be used for reversing the arcs in a digraph. |
---|
| 357 | /// It conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
[414] | 358 | /// |
---|
[451] | 359 | /// The adapted digraph can also be modified through this adaptor |
---|
[453] | 360 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
[451] | 361 | /// parameter is set to be \c const. |
---|
| 362 | /// |
---|
[512] | 363 | /// \tparam DGR The type of the adapted digraph. |
---|
[451] | 364 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
| 365 | /// It can also be specified to be \c const. |
---|
| 366 | /// |
---|
| 367 | /// \note The \c Node and \c Arc types of this adaptor and the adapted |
---|
| 368 | /// digraph are convertible to each other. |
---|
[512] | 369 | template<typename DGR> |
---|
[453] | 370 | #ifdef DOXYGEN |
---|
| 371 | class ReverseDigraph { |
---|
| 372 | #else |
---|
[416] | 373 | class ReverseDigraph : |
---|
[512] | 374 | public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > { |
---|
[453] | 375 | #endif |
---|
[617] | 376 | typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent; |
---|
[414] | 377 | public: |
---|
[453] | 378 | /// The type of the adapted digraph. |
---|
[512] | 379 | typedef DGR Digraph; |
---|
[414] | 380 | protected: |
---|
[416] | 381 | ReverseDigraph() { } |
---|
[414] | 382 | public: |
---|
[415] | 383 | |
---|
| 384 | /// \brief Constructor |
---|
| 385 | /// |
---|
[451] | 386 | /// Creates a reverse digraph adaptor for the given digraph. |
---|
[512] | 387 | explicit ReverseDigraph(DGR& digraph) { |
---|
| 388 | Parent::initialize(digraph); |
---|
[414] | 389 | } |
---|
| 390 | }; |
---|
| 391 | |
---|
[451] | 392 | /// \brief Returns a read-only ReverseDigraph adaptor |
---|
[414] | 393 | /// |
---|
[451] | 394 | /// This function just returns a read-only \ref ReverseDigraph adaptor. |
---|
| 395 | /// \ingroup graph_adaptors |
---|
| 396 | /// \relates ReverseDigraph |
---|
[512] | 397 | template<typename DGR> |
---|
| 398 | ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) { |
---|
| 399 | return ReverseDigraph<const DGR>(digraph); |
---|
[414] | 400 | } |
---|
| 401 | |
---|
[451] | 402 | |
---|
[512] | 403 | template <typename DGR, typename NF, typename AF, bool ch = true> |
---|
| 404 | class SubDigraphBase : public DigraphAdaptorBase<DGR> { |
---|
[617] | 405 | typedef DigraphAdaptorBase<DGR> Parent; |
---|
[414] | 406 | public: |
---|
[512] | 407 | typedef DGR Digraph; |
---|
| 408 | typedef NF NodeFilterMap; |
---|
| 409 | typedef AF ArcFilterMap; |
---|
[414] | 410 | |
---|
[416] | 411 | typedef SubDigraphBase Adaptor; |
---|
[414] | 412 | protected: |
---|
[512] | 413 | NF* _node_filter; |
---|
| 414 | AF* _arc_filter; |
---|
[416] | 415 | SubDigraphBase() |
---|
[414] | 416 | : Parent(), _node_filter(0), _arc_filter(0) { } |
---|
| 417 | |
---|
[512] | 418 | void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) { |
---|
| 419 | Parent::initialize(digraph); |
---|
[414] | 420 | _node_filter = &node_filter; |
---|
[512] | 421 | _arc_filter = &arc_filter; |
---|
[414] | 422 | } |
---|
| 423 | |
---|
| 424 | public: |
---|
| 425 | |
---|
| 426 | typedef typename Parent::Node Node; |
---|
| 427 | typedef typename Parent::Arc Arc; |
---|
| 428 | |
---|
[416] | 429 | void first(Node& i) const { |
---|
| 430 | Parent::first(i); |
---|
| 431 | while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[414] | 432 | } |
---|
| 433 | |
---|
[416] | 434 | void first(Arc& i) const { |
---|
| 435 | Parent::first(i); |
---|
| 436 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
| 437 | || !(*_node_filter)[Parent::source(i)] |
---|
| 438 | || !(*_node_filter)[Parent::target(i)])) |
---|
| 439 | Parent::next(i); |
---|
[414] | 440 | } |
---|
| 441 | |
---|
[416] | 442 | void firstIn(Arc& i, const Node& n) const { |
---|
| 443 | Parent::firstIn(i, n); |
---|
| 444 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
| 445 | || !(*_node_filter)[Parent::source(i)])) |
---|
| 446 | Parent::nextIn(i); |
---|
[414] | 447 | } |
---|
| 448 | |
---|
[416] | 449 | void firstOut(Arc& i, const Node& n) const { |
---|
| 450 | Parent::firstOut(i, n); |
---|
| 451 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
| 452 | || !(*_node_filter)[Parent::target(i)])) |
---|
| 453 | Parent::nextOut(i); |
---|
[414] | 454 | } |
---|
| 455 | |
---|
[416] | 456 | void next(Node& i) const { |
---|
| 457 | Parent::next(i); |
---|
| 458 | while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[414] | 459 | } |
---|
| 460 | |
---|
[416] | 461 | void next(Arc& i) const { |
---|
| 462 | Parent::next(i); |
---|
| 463 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
| 464 | || !(*_node_filter)[Parent::source(i)] |
---|
| 465 | || !(*_node_filter)[Parent::target(i)])) |
---|
| 466 | Parent::next(i); |
---|
[414] | 467 | } |
---|
| 468 | |
---|
[416] | 469 | void nextIn(Arc& i) const { |
---|
| 470 | Parent::nextIn(i); |
---|
| 471 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
| 472 | || !(*_node_filter)[Parent::source(i)])) |
---|
| 473 | Parent::nextIn(i); |
---|
[414] | 474 | } |
---|
| 475 | |
---|
[416] | 476 | void nextOut(Arc& i) const { |
---|
| 477 | Parent::nextOut(i); |
---|
| 478 | while (i != INVALID && (!(*_arc_filter)[i] |
---|
| 479 | || !(*_node_filter)[Parent::target(i)])) |
---|
| 480 | Parent::nextOut(i); |
---|
[414] | 481 | } |
---|
| 482 | |
---|
[452] | 483 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
| 484 | void status(const Arc& a, bool v) const { _arc_filter->set(a, v); } |
---|
| 485 | |
---|
| 486 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
| 487 | bool status(const Arc& a) const { return (*_arc_filter)[a]; } |
---|
[414] | 488 | |
---|
| 489 | typedef False NodeNumTag; |
---|
[446] | 490 | typedef False ArcNumTag; |
---|
| 491 | |
---|
[512] | 492 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
[416] | 493 | Arc findArc(const Node& source, const Node& target, |
---|
[448] | 494 | const Arc& prev = INVALID) const { |
---|
[414] | 495 | if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
---|
| 496 | return INVALID; |
---|
| 497 | } |
---|
| 498 | Arc arc = Parent::findArc(source, target, prev); |
---|
| 499 | while (arc != INVALID && !(*_arc_filter)[arc]) { |
---|
| 500 | arc = Parent::findArc(source, target, arc); |
---|
| 501 | } |
---|
| 502 | return arc; |
---|
| 503 | } |
---|
| 504 | |
---|
[512] | 505 | public: |
---|
| 506 | |
---|
| 507 | template <typename V> |
---|
| 508 | class NodeMap |
---|
| 509 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
| 510 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> { |
---|
[617] | 511 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
| 512 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
---|
| 513 | |
---|
[414] | 514 | public: |
---|
[512] | 515 | typedef V Value; |
---|
| 516 | |
---|
| 517 | NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
---|
| 518 | : Parent(adaptor) {} |
---|
| 519 | NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
---|
| 520 | : Parent(adaptor, value) {} |
---|
[416] | 521 | |
---|
[414] | 522 | private: |
---|
| 523 | NodeMap& operator=(const NodeMap& cmap) { |
---|
[416] | 524 | return operator=<NodeMap>(cmap); |
---|
[414] | 525 | } |
---|
[416] | 526 | |
---|
[414] | 527 | template <typename CMap> |
---|
| 528 | NodeMap& operator=(const CMap& cmap) { |
---|
[512] | 529 | Parent::operator=(cmap); |
---|
[416] | 530 | return *this; |
---|
[414] | 531 | } |
---|
| 532 | }; |
---|
| 533 | |
---|
[512] | 534 | template <typename V> |
---|
| 535 | class ArcMap |
---|
| 536 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
| 537 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> { |
---|
[617] | 538 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
---|
| 539 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
---|
| 540 | |
---|
[414] | 541 | public: |
---|
[512] | 542 | typedef V Value; |
---|
| 543 | |
---|
| 544 | ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
---|
| 545 | : Parent(adaptor) {} |
---|
| 546 | ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
---|
| 547 | : Parent(adaptor, value) {} |
---|
[416] | 548 | |
---|
[414] | 549 | private: |
---|
| 550 | ArcMap& operator=(const ArcMap& cmap) { |
---|
[416] | 551 | return operator=<ArcMap>(cmap); |
---|
[414] | 552 | } |
---|
[416] | 553 | |
---|
[414] | 554 | template <typename CMap> |
---|
| 555 | ArcMap& operator=(const CMap& cmap) { |
---|
[512] | 556 | Parent::operator=(cmap); |
---|
[416] | 557 | return *this; |
---|
[414] | 558 | } |
---|
| 559 | }; |
---|
| 560 | |
---|
| 561 | }; |
---|
| 562 | |
---|
[512] | 563 | template <typename DGR, typename NF, typename AF> |
---|
| 564 | class SubDigraphBase<DGR, NF, AF, false> |
---|
| 565 | : public DigraphAdaptorBase<DGR> { |
---|
[617] | 566 | typedef DigraphAdaptorBase<DGR> Parent; |
---|
[414] | 567 | public: |
---|
[512] | 568 | typedef DGR Digraph; |
---|
| 569 | typedef NF NodeFilterMap; |
---|
| 570 | typedef AF ArcFilterMap; |
---|
[414] | 571 | |
---|
[416] | 572 | typedef SubDigraphBase Adaptor; |
---|
[414] | 573 | protected: |
---|
[512] | 574 | NF* _node_filter; |
---|
| 575 | AF* _arc_filter; |
---|
[416] | 576 | SubDigraphBase() |
---|
[414] | 577 | : Parent(), _node_filter(0), _arc_filter(0) { } |
---|
| 578 | |
---|
[512] | 579 | void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) { |
---|
| 580 | Parent::initialize(digraph); |
---|
[414] | 581 | _node_filter = &node_filter; |
---|
[512] | 582 | _arc_filter = &arc_filter; |
---|
[414] | 583 | } |
---|
| 584 | |
---|
| 585 | public: |
---|
| 586 | |
---|
| 587 | typedef typename Parent::Node Node; |
---|
| 588 | typedef typename Parent::Arc Arc; |
---|
| 589 | |
---|
[416] | 590 | void first(Node& i) const { |
---|
| 591 | Parent::first(i); |
---|
| 592 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[414] | 593 | } |
---|
| 594 | |
---|
[416] | 595 | void first(Arc& i) const { |
---|
| 596 | Parent::first(i); |
---|
| 597 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
---|
[414] | 598 | } |
---|
| 599 | |
---|
[416] | 600 | void firstIn(Arc& i, const Node& n) const { |
---|
| 601 | Parent::firstIn(i, n); |
---|
| 602 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
---|
[414] | 603 | } |
---|
| 604 | |
---|
[416] | 605 | void firstOut(Arc& i, const Node& n) const { |
---|
| 606 | Parent::firstOut(i, n); |
---|
| 607 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
---|
[414] | 608 | } |
---|
| 609 | |
---|
[416] | 610 | void next(Node& i) const { |
---|
| 611 | Parent::next(i); |
---|
| 612 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[414] | 613 | } |
---|
[416] | 614 | void next(Arc& i) const { |
---|
| 615 | Parent::next(i); |
---|
| 616 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
---|
[414] | 617 | } |
---|
[416] | 618 | void nextIn(Arc& i) const { |
---|
| 619 | Parent::nextIn(i); |
---|
| 620 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
---|
[414] | 621 | } |
---|
| 622 | |
---|
[416] | 623 | void nextOut(Arc& i) const { |
---|
| 624 | Parent::nextOut(i); |
---|
| 625 | while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
---|
[414] | 626 | } |
---|
| 627 | |
---|
[452] | 628 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
| 629 | void status(const Arc& a, bool v) const { _arc_filter->set(a, v); } |
---|
| 630 | |
---|
| 631 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
| 632 | bool status(const Arc& a) const { return (*_arc_filter)[a]; } |
---|
[414] | 633 | |
---|
| 634 | typedef False NodeNumTag; |
---|
[446] | 635 | typedef False ArcNumTag; |
---|
| 636 | |
---|
[512] | 637 | typedef FindArcTagIndicator<DGR> FindArcTag; |
---|
[416] | 638 | Arc findArc(const Node& source, const Node& target, |
---|
[448] | 639 | const Arc& prev = INVALID) const { |
---|
[414] | 640 | if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
---|
| 641 | return INVALID; |
---|
| 642 | } |
---|
| 643 | Arc arc = Parent::findArc(source, target, prev); |
---|
| 644 | while (arc != INVALID && !(*_arc_filter)[arc]) { |
---|
| 645 | arc = Parent::findArc(source, target, arc); |
---|
| 646 | } |
---|
| 647 | return arc; |
---|
| 648 | } |
---|
| 649 | |
---|
[512] | 650 | template <typename V> |
---|
| 651 | class NodeMap |
---|
| 652 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
| 653 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> { |
---|
[617] | 654 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
| 655 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
---|
| 656 | |
---|
[414] | 657 | public: |
---|
[512] | 658 | typedef V Value; |
---|
| 659 | |
---|
| 660 | NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
---|
| 661 | : Parent(adaptor) {} |
---|
| 662 | NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
---|
| 663 | : Parent(adaptor, value) {} |
---|
[416] | 664 | |
---|
[414] | 665 | private: |
---|
| 666 | NodeMap& operator=(const NodeMap& cmap) { |
---|
[416] | 667 | return operator=<NodeMap>(cmap); |
---|
[414] | 668 | } |
---|
[416] | 669 | |
---|
[414] | 670 | template <typename CMap> |
---|
| 671 | NodeMap& operator=(const CMap& cmap) { |
---|
[512] | 672 | Parent::operator=(cmap); |
---|
[416] | 673 | return *this; |
---|
[414] | 674 | } |
---|
| 675 | }; |
---|
| 676 | |
---|
[512] | 677 | template <typename V> |
---|
| 678 | class ArcMap |
---|
| 679 | : public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
| 680 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> { |
---|
[617] | 681 | typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
---|
| 682 | LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
---|
| 683 | |
---|
[414] | 684 | public: |
---|
[512] | 685 | typedef V Value; |
---|
| 686 | |
---|
| 687 | ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
---|
| 688 | : Parent(adaptor) {} |
---|
| 689 | ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
---|
| 690 | : Parent(adaptor, value) {} |
---|
[416] | 691 | |
---|
[414] | 692 | private: |
---|
| 693 | ArcMap& operator=(const ArcMap& cmap) { |
---|
[416] | 694 | return operator=<ArcMap>(cmap); |
---|
[414] | 695 | } |
---|
[416] | 696 | |
---|
[414] | 697 | template <typename CMap> |
---|
| 698 | ArcMap& operator=(const CMap& cmap) { |
---|
[512] | 699 | Parent::operator=(cmap); |
---|
[416] | 700 | return *this; |
---|
[414] | 701 | } |
---|
| 702 | }; |
---|
| 703 | |
---|
| 704 | }; |
---|
| 705 | |
---|
| 706 | /// \ingroup graph_adaptors |
---|
| 707 | /// |
---|
[451] | 708 | /// \brief Adaptor class for hiding nodes and arcs in a digraph |
---|
[416] | 709 | /// |
---|
[451] | 710 | /// SubDigraph can be used for hiding nodes and arcs in a digraph. |
---|
| 711 | /// A \c bool node map and a \c bool arc map must be specified, which |
---|
| 712 | /// define the filters for nodes and arcs. |
---|
| 713 | /// Only the nodes and arcs with \c true filter value are |
---|
[453] | 714 | /// shown in the subdigraph. The arcs that are incident to hidden |
---|
| 715 | /// nodes are also filtered out. |
---|
| 716 | /// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
[416] | 717 | /// |
---|
[451] | 718 | /// The adapted digraph can also be modified through this adaptor |
---|
[453] | 719 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
[451] | 720 | /// parameter is set to be \c const. |
---|
| 721 | /// |
---|
[512] | 722 | /// \tparam DGR The type of the adapted digraph. |
---|
[451] | 723 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
| 724 | /// It can also be specified to be \c const. |
---|
[453] | 725 | /// \tparam NF The type of the node filter map. |
---|
| 726 | /// It must be a \c bool (or convertible) node map of the |
---|
| 727 | /// adapted digraph. The default type is |
---|
[512] | 728 | /// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
---|
[453] | 729 | /// \tparam AF The type of the arc filter map. |
---|
| 730 | /// It must be \c bool (or convertible) arc map of the |
---|
| 731 | /// adapted digraph. The default type is |
---|
[512] | 732 | /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
---|
[451] | 733 | /// |
---|
| 734 | /// \note The \c Node and \c Arc types of this adaptor and the adapted |
---|
| 735 | /// digraph are convertible to each other. |
---|
[416] | 736 | /// |
---|
| 737 | /// \see FilterNodes |
---|
| 738 | /// \see FilterArcs |
---|
[451] | 739 | #ifdef DOXYGEN |
---|
[512] | 740 | template<typename DGR, typename NF, typename AF> |
---|
[453] | 741 | class SubDigraph { |
---|
[451] | 742 | #else |
---|
[512] | 743 | template<typename DGR, |
---|
| 744 | typename NF = typename DGR::template NodeMap<bool>, |
---|
| 745 | typename AF = typename DGR::template ArcMap<bool> > |
---|
[453] | 746 | class SubDigraph : |
---|
[512] | 747 | public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > { |
---|
[451] | 748 | #endif |
---|
[414] | 749 | public: |
---|
[451] | 750 | /// The type of the adapted digraph. |
---|
[512] | 751 | typedef DGR Digraph; |
---|
[451] | 752 | /// The type of the node filter map. |
---|
[453] | 753 | typedef NF NodeFilterMap; |
---|
[451] | 754 | /// The type of the arc filter map. |
---|
[453] | 755 | typedef AF ArcFilterMap; |
---|
| 756 | |
---|
[512] | 757 | typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > |
---|
[453] | 758 | Parent; |
---|
[414] | 759 | |
---|
[415] | 760 | typedef typename Parent::Node Node; |
---|
| 761 | typedef typename Parent::Arc Arc; |
---|
| 762 | |
---|
[414] | 763 | protected: |
---|
[416] | 764 | SubDigraph() { } |
---|
[414] | 765 | public: |
---|
| 766 | |
---|
[415] | 767 | /// \brief Constructor |
---|
| 768 | /// |
---|
[451] | 769 | /// Creates a subdigraph for the given digraph with the |
---|
| 770 | /// given node and arc filter maps. |
---|
[512] | 771 | SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) { |
---|
| 772 | Parent::initialize(digraph, node_filter, arc_filter); |
---|
[414] | 773 | } |
---|
| 774 | |
---|
[452] | 775 | /// \brief Sets the status of the given node |
---|
[415] | 776 | /// |
---|
[452] | 777 | /// This function sets the status of the given node. |
---|
[451] | 778 | /// It is done by simply setting the assigned value of \c n |
---|
[452] | 779 | /// to \c v in the node filter map. |
---|
| 780 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
| 781 | |
---|
| 782 | /// \brief Sets the status of the given arc |
---|
[415] | 783 | /// |
---|
[452] | 784 | /// This function sets the status of the given arc. |
---|
[451] | 785 | /// It is done by simply setting the assigned value of \c a |
---|
[452] | 786 | /// to \c v in the arc filter map. |
---|
| 787 | void status(const Arc& a, bool v) const { Parent::status(a, v); } |
---|
| 788 | |
---|
| 789 | /// \brief Returns the status of the given node |
---|
[415] | 790 | /// |
---|
[452] | 791 | /// This function returns the status of the given node. |
---|
| 792 | /// It is \c true if the given node is enabled (i.e. not hidden). |
---|
| 793 | bool status(const Node& n) const { return Parent::status(n); } |
---|
| 794 | |
---|
| 795 | /// \brief Returns the status of the given arc |
---|
[415] | 796 | /// |
---|
[452] | 797 | /// This function returns the status of the given arc. |
---|
| 798 | /// It is \c true if the given arc is enabled (i.e. not hidden). |
---|
| 799 | bool status(const Arc& a) const { return Parent::status(a); } |
---|
| 800 | |
---|
| 801 | /// \brief Disables the given node |
---|
[415] | 802 | /// |
---|
[452] | 803 | /// This function disables the given node in the subdigraph, |
---|
| 804 | /// so the iteration jumps over it. |
---|
| 805 | /// It is the same as \ref status() "status(n, false)". |
---|
| 806 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
| 807 | |
---|
| 808 | /// \brief Disables the given arc |
---|
[415] | 809 | /// |
---|
[452] | 810 | /// This function disables the given arc in the subdigraph, |
---|
| 811 | /// so the iteration jumps over it. |
---|
| 812 | /// It is the same as \ref status() "status(a, false)". |
---|
| 813 | void disable(const Arc& a) const { Parent::status(a, false); } |
---|
| 814 | |
---|
| 815 | /// \brief Enables the given node |
---|
| 816 | /// |
---|
| 817 | /// This function enables the given node in the subdigraph. |
---|
| 818 | /// It is the same as \ref status() "status(n, true)". |
---|
| 819 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
| 820 | |
---|
| 821 | /// \brief Enables the given arc |
---|
| 822 | /// |
---|
| 823 | /// This function enables the given arc in the subdigraph. |
---|
| 824 | /// It is the same as \ref status() "status(a, true)". |
---|
| 825 | void enable(const Arc& a) const { Parent::status(a, true); } |
---|
[415] | 826 | |
---|
[414] | 827 | }; |
---|
| 828 | |
---|
[451] | 829 | /// \brief Returns a read-only SubDigraph adaptor |
---|
[414] | 830 | /// |
---|
[451] | 831 | /// This function just returns a read-only \ref SubDigraph adaptor. |
---|
| 832 | /// \ingroup graph_adaptors |
---|
| 833 | /// \relates SubDigraph |
---|
[512] | 834 | template<typename DGR, typename NF, typename AF> |
---|
| 835 | SubDigraph<const DGR, NF, AF> |
---|
| 836 | subDigraph(const DGR& digraph, |
---|
| 837 | NF& node_filter, AF& arc_filter) { |
---|
| 838 | return SubDigraph<const DGR, NF, AF> |
---|
| 839 | (digraph, node_filter, arc_filter); |
---|
[414] | 840 | } |
---|
| 841 | |
---|
[512] | 842 | template<typename DGR, typename NF, typename AF> |
---|
| 843 | SubDigraph<const DGR, const NF, AF> |
---|
| 844 | subDigraph(const DGR& digraph, |
---|
| 845 | const NF& node_filter, AF& arc_filter) { |
---|
| 846 | return SubDigraph<const DGR, const NF, AF> |
---|
| 847 | (digraph, node_filter, arc_filter); |
---|
[414] | 848 | } |
---|
| 849 | |
---|
[512] | 850 | template<typename DGR, typename NF, typename AF> |
---|
| 851 | SubDigraph<const DGR, NF, const AF> |
---|
| 852 | subDigraph(const DGR& digraph, |
---|
| 853 | NF& node_filter, const AF& arc_filter) { |
---|
| 854 | return SubDigraph<const DGR, NF, const AF> |
---|
| 855 | (digraph, node_filter, arc_filter); |
---|
[414] | 856 | } |
---|
| 857 | |
---|
[512] | 858 | template<typename DGR, typename NF, typename AF> |
---|
| 859 | SubDigraph<const DGR, const NF, const AF> |
---|
| 860 | subDigraph(const DGR& digraph, |
---|
| 861 | const NF& node_filter, const AF& arc_filter) { |
---|
| 862 | return SubDigraph<const DGR, const NF, const AF> |
---|
| 863 | (digraph, node_filter, arc_filter); |
---|
[414] | 864 | } |
---|
| 865 | |
---|
| 866 | |
---|
[512] | 867 | template <typename GR, typename NF, typename EF, bool ch = true> |
---|
| 868 | class SubGraphBase : public GraphAdaptorBase<GR> { |
---|
[617] | 869 | typedef GraphAdaptorBase<GR> Parent; |
---|
[416] | 870 | public: |
---|
[512] | 871 | typedef GR Graph; |
---|
| 872 | typedef NF NodeFilterMap; |
---|
| 873 | typedef EF EdgeFilterMap; |
---|
[449] | 874 | |
---|
[416] | 875 | typedef SubGraphBase Adaptor; |
---|
| 876 | protected: |
---|
| 877 | |
---|
[512] | 878 | NF* _node_filter; |
---|
| 879 | EF* _edge_filter; |
---|
[416] | 880 | |
---|
| 881 | SubGraphBase() |
---|
[512] | 882 | : Parent(), _node_filter(0), _edge_filter(0) { } |
---|
| 883 | |
---|
| 884 | void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
---|
| 885 | Parent::initialize(graph); |
---|
| 886 | _node_filter = &node_filter; |
---|
| 887 | _edge_filter = &edge_filter; |
---|
[416] | 888 | } |
---|
| 889 | |
---|
| 890 | public: |
---|
| 891 | |
---|
| 892 | typedef typename Parent::Node Node; |
---|
| 893 | typedef typename Parent::Arc Arc; |
---|
| 894 | typedef typename Parent::Edge Edge; |
---|
| 895 | |
---|
| 896 | void first(Node& i) const { |
---|
| 897 | Parent::first(i); |
---|
[512] | 898 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[416] | 899 | } |
---|
| 900 | |
---|
| 901 | void first(Arc& i) const { |
---|
| 902 | Parent::first(i); |
---|
[512] | 903 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 904 | || !(*_node_filter)[Parent::source(i)] |
---|
| 905 | || !(*_node_filter)[Parent::target(i)])) |
---|
[416] | 906 | Parent::next(i); |
---|
| 907 | } |
---|
| 908 | |
---|
| 909 | void first(Edge& i) const { |
---|
| 910 | Parent::first(i); |
---|
[512] | 911 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 912 | || !(*_node_filter)[Parent::u(i)] |
---|
| 913 | || !(*_node_filter)[Parent::v(i)])) |
---|
[416] | 914 | Parent::next(i); |
---|
| 915 | } |
---|
| 916 | |
---|
| 917 | void firstIn(Arc& i, const Node& n) const { |
---|
| 918 | Parent::firstIn(i, n); |
---|
[512] | 919 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 920 | || !(*_node_filter)[Parent::source(i)])) |
---|
[416] | 921 | Parent::nextIn(i); |
---|
| 922 | } |
---|
| 923 | |
---|
| 924 | void firstOut(Arc& i, const Node& n) const { |
---|
| 925 | Parent::firstOut(i, n); |
---|
[512] | 926 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 927 | || !(*_node_filter)[Parent::target(i)])) |
---|
[416] | 928 | Parent::nextOut(i); |
---|
| 929 | } |
---|
| 930 | |
---|
| 931 | void firstInc(Edge& i, bool& d, const Node& n) const { |
---|
| 932 | Parent::firstInc(i, d, n); |
---|
[512] | 933 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 934 | || !(*_node_filter)[Parent::u(i)] |
---|
| 935 | || !(*_node_filter)[Parent::v(i)])) |
---|
[416] | 936 | Parent::nextInc(i, d); |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | void next(Node& i) const { |
---|
| 940 | Parent::next(i); |
---|
[512] | 941 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[416] | 942 | } |
---|
| 943 | |
---|
| 944 | void next(Arc& i) const { |
---|
| 945 | Parent::next(i); |
---|
[512] | 946 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 947 | || !(*_node_filter)[Parent::source(i)] |
---|
| 948 | || !(*_node_filter)[Parent::target(i)])) |
---|
[416] | 949 | Parent::next(i); |
---|
| 950 | } |
---|
| 951 | |
---|
| 952 | void next(Edge& i) const { |
---|
| 953 | Parent::next(i); |
---|
[512] | 954 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 955 | || !(*_node_filter)[Parent::u(i)] |
---|
| 956 | || !(*_node_filter)[Parent::v(i)])) |
---|
[416] | 957 | Parent::next(i); |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | void nextIn(Arc& i) const { |
---|
| 961 | Parent::nextIn(i); |
---|
[512] | 962 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 963 | || !(*_node_filter)[Parent::source(i)])) |
---|
[416] | 964 | Parent::nextIn(i); |
---|
| 965 | } |
---|
| 966 | |
---|
| 967 | void nextOut(Arc& i) const { |
---|
| 968 | Parent::nextOut(i); |
---|
[512] | 969 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 970 | || !(*_node_filter)[Parent::target(i)])) |
---|
[416] | 971 | Parent::nextOut(i); |
---|
| 972 | } |
---|
| 973 | |
---|
| 974 | void nextInc(Edge& i, bool& d) const { |
---|
| 975 | Parent::nextInc(i, d); |
---|
[512] | 976 | while (i!=INVALID && (!(*_edge_filter)[i] |
---|
| 977 | || !(*_node_filter)[Parent::u(i)] |
---|
| 978 | || !(*_node_filter)[Parent::v(i)])) |
---|
[416] | 979 | Parent::nextInc(i, d); |
---|
| 980 | } |
---|
| 981 | |
---|
[512] | 982 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
| 983 | void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
---|
| 984 | |
---|
| 985 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
| 986 | bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
---|
[416] | 987 | |
---|
| 988 | typedef False NodeNumTag; |
---|
[446] | 989 | typedef False ArcNumTag; |
---|
[416] | 990 | typedef False EdgeNumTag; |
---|
| 991 | |
---|
[446] | 992 | typedef FindArcTagIndicator<Graph> FindArcTag; |
---|
[416] | 993 | Arc findArc(const Node& u, const Node& v, |
---|
[448] | 994 | const Arc& prev = INVALID) const { |
---|
[512] | 995 | if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
---|
[416] | 996 | return INVALID; |
---|
| 997 | } |
---|
| 998 | Arc arc = Parent::findArc(u, v, prev); |
---|
[512] | 999 | while (arc != INVALID && !(*_edge_filter)[arc]) { |
---|
[416] | 1000 | arc = Parent::findArc(u, v, arc); |
---|
| 1001 | } |
---|
| 1002 | return arc; |
---|
| 1003 | } |
---|
[446] | 1004 | |
---|
| 1005 | typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
---|
[416] | 1006 | Edge findEdge(const Node& u, const Node& v, |
---|
[448] | 1007 | const Edge& prev = INVALID) const { |
---|
[512] | 1008 | if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
---|
[416] | 1009 | return INVALID; |
---|
| 1010 | } |
---|
| 1011 | Edge edge = Parent::findEdge(u, v, prev); |
---|
[512] | 1012 | while (edge != INVALID && !(*_edge_filter)[edge]) { |
---|
[416] | 1013 | edge = Parent::findEdge(u, v, edge); |
---|
| 1014 | } |
---|
| 1015 | return edge; |
---|
| 1016 | } |
---|
| 1017 | |
---|
[512] | 1018 | template <typename V> |
---|
| 1019 | class NodeMap |
---|
| 1020 | : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
| 1021 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
---|
[617] | 1022 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
| 1023 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
---|
| 1024 | |
---|
[416] | 1025 | public: |
---|
[512] | 1026 | typedef V Value; |
---|
| 1027 | |
---|
| 1028 | NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
---|
| 1029 | : Parent(adaptor) {} |
---|
| 1030 | NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
---|
| 1031 | : Parent(adaptor, value) {} |
---|
[416] | 1032 | |
---|
| 1033 | private: |
---|
| 1034 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 1035 | return operator=<NodeMap>(cmap); |
---|
| 1036 | } |
---|
| 1037 | |
---|
| 1038 | template <typename CMap> |
---|
| 1039 | NodeMap& operator=(const CMap& cmap) { |
---|
[512] | 1040 | Parent::operator=(cmap); |
---|
[416] | 1041 | return *this; |
---|
| 1042 | } |
---|
| 1043 | }; |
---|
| 1044 | |
---|
[512] | 1045 | template <typename V> |
---|
| 1046 | class ArcMap |
---|
| 1047 | : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
| 1048 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
---|
[617] | 1049 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
| 1050 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
---|
| 1051 | |
---|
[416] | 1052 | public: |
---|
[512] | 1053 | typedef V Value; |
---|
| 1054 | |
---|
| 1055 | ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
---|
| 1056 | : Parent(adaptor) {} |
---|
| 1057 | ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
---|
| 1058 | : Parent(adaptor, value) {} |
---|
[416] | 1059 | |
---|
| 1060 | private: |
---|
| 1061 | ArcMap& operator=(const ArcMap& cmap) { |
---|
| 1062 | return operator=<ArcMap>(cmap); |
---|
| 1063 | } |
---|
| 1064 | |
---|
| 1065 | template <typename CMap> |
---|
| 1066 | ArcMap& operator=(const CMap& cmap) { |
---|
[512] | 1067 | Parent::operator=(cmap); |
---|
[416] | 1068 | return *this; |
---|
| 1069 | } |
---|
| 1070 | }; |
---|
| 1071 | |
---|
[512] | 1072 | template <typename V> |
---|
| 1073 | class EdgeMap |
---|
| 1074 | : public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
| 1075 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
---|
[617] | 1076 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
---|
| 1077 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
---|
| 1078 | |
---|
[416] | 1079 | public: |
---|
[512] | 1080 | typedef V Value; |
---|
| 1081 | |
---|
| 1082 | EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
---|
| 1083 | : Parent(adaptor) {} |
---|
| 1084 | |
---|
| 1085 | EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
---|
| 1086 | : Parent(adaptor, value) {} |
---|
[416] | 1087 | |
---|
| 1088 | private: |
---|
| 1089 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
| 1090 | return operator=<EdgeMap>(cmap); |
---|
| 1091 | } |
---|
| 1092 | |
---|
| 1093 | template <typename CMap> |
---|
| 1094 | EdgeMap& operator=(const CMap& cmap) { |
---|
[512] | 1095 | Parent::operator=(cmap); |
---|
[416] | 1096 | return *this; |
---|
| 1097 | } |
---|
| 1098 | }; |
---|
| 1099 | |
---|
| 1100 | }; |
---|
| 1101 | |
---|
[512] | 1102 | template <typename GR, typename NF, typename EF> |
---|
| 1103 | class SubGraphBase<GR, NF, EF, false> |
---|
| 1104 | : public GraphAdaptorBase<GR> { |
---|
[617] | 1105 | typedef GraphAdaptorBase<GR> Parent; |
---|
[416] | 1106 | public: |
---|
[512] | 1107 | typedef GR Graph; |
---|
| 1108 | typedef NF NodeFilterMap; |
---|
| 1109 | typedef EF EdgeFilterMap; |
---|
[449] | 1110 | |
---|
[416] | 1111 | typedef SubGraphBase Adaptor; |
---|
| 1112 | protected: |
---|
[512] | 1113 | NF* _node_filter; |
---|
| 1114 | EF* _edge_filter; |
---|
| 1115 | SubGraphBase() |
---|
| 1116 | : Parent(), _node_filter(0), _edge_filter(0) { } |
---|
| 1117 | |
---|
| 1118 | void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
---|
| 1119 | Parent::initialize(graph); |
---|
| 1120 | _node_filter = &node_filter; |
---|
| 1121 | _edge_filter = &edge_filter; |
---|
[416] | 1122 | } |
---|
| 1123 | |
---|
| 1124 | public: |
---|
| 1125 | |
---|
| 1126 | typedef typename Parent::Node Node; |
---|
| 1127 | typedef typename Parent::Arc Arc; |
---|
| 1128 | typedef typename Parent::Edge Edge; |
---|
| 1129 | |
---|
| 1130 | void first(Node& i) const { |
---|
| 1131 | Parent::first(i); |
---|
[512] | 1132 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[416] | 1133 | } |
---|
| 1134 | |
---|
| 1135 | void first(Arc& i) const { |
---|
| 1136 | Parent::first(i); |
---|
[512] | 1137 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
[416] | 1138 | } |
---|
| 1139 | |
---|
| 1140 | void first(Edge& i) const { |
---|
| 1141 | Parent::first(i); |
---|
[512] | 1142 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
[416] | 1143 | } |
---|
| 1144 | |
---|
| 1145 | void firstIn(Arc& i, const Node& n) const { |
---|
| 1146 | Parent::firstIn(i, n); |
---|
[512] | 1147 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
---|
[416] | 1148 | } |
---|
| 1149 | |
---|
| 1150 | void firstOut(Arc& i, const Node& n) const { |
---|
| 1151 | Parent::firstOut(i, n); |
---|
[512] | 1152 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
---|
[416] | 1153 | } |
---|
| 1154 | |
---|
| 1155 | void firstInc(Edge& i, bool& d, const Node& n) const { |
---|
| 1156 | Parent::firstInc(i, d, n); |
---|
[512] | 1157 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
---|
[416] | 1158 | } |
---|
| 1159 | |
---|
| 1160 | void next(Node& i) const { |
---|
| 1161 | Parent::next(i); |
---|
[512] | 1162 | while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
---|
[416] | 1163 | } |
---|
| 1164 | void next(Arc& i) const { |
---|
| 1165 | Parent::next(i); |
---|
[512] | 1166 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
[416] | 1167 | } |
---|
| 1168 | void next(Edge& i) const { |
---|
| 1169 | Parent::next(i); |
---|
[512] | 1170 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
---|
[416] | 1171 | } |
---|
| 1172 | void nextIn(Arc& i) const { |
---|
| 1173 | Parent::nextIn(i); |
---|
[512] | 1174 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
---|
[416] | 1175 | } |
---|
| 1176 | |
---|
| 1177 | void nextOut(Arc& i) const { |
---|
| 1178 | Parent::nextOut(i); |
---|
[512] | 1179 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
---|
[416] | 1180 | } |
---|
| 1181 | void nextInc(Edge& i, bool& d) const { |
---|
| 1182 | Parent::nextInc(i, d); |
---|
[512] | 1183 | while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
---|
[416] | 1184 | } |
---|
| 1185 | |
---|
[512] | 1186 | void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
---|
| 1187 | void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
---|
| 1188 | |
---|
| 1189 | bool status(const Node& n) const { return (*_node_filter)[n]; } |
---|
| 1190 | bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
---|
[416] | 1191 | |
---|
| 1192 | typedef False NodeNumTag; |
---|
[446] | 1193 | typedef False ArcNumTag; |
---|
[416] | 1194 | typedef False EdgeNumTag; |
---|
| 1195 | |
---|
[446] | 1196 | typedef FindArcTagIndicator<Graph> FindArcTag; |
---|
[416] | 1197 | Arc findArc(const Node& u, const Node& v, |
---|
[448] | 1198 | const Arc& prev = INVALID) const { |
---|
[416] | 1199 | Arc arc = Parent::findArc(u, v, prev); |
---|
[512] | 1200 | while (arc != INVALID && !(*_edge_filter)[arc]) { |
---|
[416] | 1201 | arc = Parent::findArc(u, v, arc); |
---|
| 1202 | } |
---|
| 1203 | return arc; |
---|
| 1204 | } |
---|
[446] | 1205 | |
---|
| 1206 | typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
---|
[416] | 1207 | Edge findEdge(const Node& u, const Node& v, |
---|
[448] | 1208 | const Edge& prev = INVALID) const { |
---|
[416] | 1209 | Edge edge = Parent::findEdge(u, v, prev); |
---|
[512] | 1210 | while (edge != INVALID && !(*_edge_filter)[edge]) { |
---|
[416] | 1211 | edge = Parent::findEdge(u, v, edge); |
---|
| 1212 | } |
---|
| 1213 | return edge; |
---|
| 1214 | } |
---|
| 1215 | |
---|
[512] | 1216 | template <typename V> |
---|
| 1217 | class NodeMap |
---|
| 1218 | : public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
| 1219 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
---|
[617] | 1220 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
| 1221 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
---|
| 1222 | |
---|
[416] | 1223 | public: |
---|
[512] | 1224 | typedef V Value; |
---|
| 1225 | |
---|
| 1226 | NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
---|
| 1227 | : Parent(adaptor) {} |
---|
| 1228 | NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
---|
| 1229 | : Parent(adaptor, value) {} |
---|
[416] | 1230 | |
---|
| 1231 | private: |
---|
| 1232 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 1233 | return operator=<NodeMap>(cmap); |
---|
| 1234 | } |
---|
| 1235 | |
---|
| 1236 | template <typename CMap> |
---|
| 1237 | NodeMap& operator=(const CMap& cmap) { |
---|
[512] | 1238 | Parent::operator=(cmap); |
---|
[416] | 1239 | return *this; |
---|
| 1240 | } |
---|
| 1241 | }; |
---|
| 1242 | |
---|
[512] | 1243 | template <typename V> |
---|
| 1244 | class ArcMap |
---|
| 1245 | : public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
| 1246 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
---|
[617] | 1247 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
| 1248 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
---|
| 1249 | |
---|
[416] | 1250 | public: |
---|
[512] | 1251 | typedef V Value; |
---|
| 1252 | |
---|
| 1253 | ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
---|
| 1254 | : Parent(adaptor) {} |
---|
| 1255 | ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
---|
| 1256 | : Parent(adaptor, value) {} |
---|
[416] | 1257 | |
---|
| 1258 | private: |
---|
| 1259 | ArcMap& operator=(const ArcMap& cmap) { |
---|
| 1260 | return operator=<ArcMap>(cmap); |
---|
| 1261 | } |
---|
| 1262 | |
---|
| 1263 | template <typename CMap> |
---|
| 1264 | ArcMap& operator=(const CMap& cmap) { |
---|
[512] | 1265 | Parent::operator=(cmap); |
---|
[416] | 1266 | return *this; |
---|
| 1267 | } |
---|
| 1268 | }; |
---|
| 1269 | |
---|
[512] | 1270 | template <typename V> |
---|
| 1271 | class EdgeMap |
---|
| 1272 | : public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
| 1273 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
---|
[617] | 1274 | typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
---|
| 1275 | LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
---|
| 1276 | |
---|
[416] | 1277 | public: |
---|
[512] | 1278 | typedef V Value; |
---|
| 1279 | |
---|
| 1280 | EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
---|
| 1281 | : Parent(adaptor) {} |
---|
| 1282 | |
---|
| 1283 | EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
---|
| 1284 | : Parent(adaptor, value) {} |
---|
[416] | 1285 | |
---|
| 1286 | private: |
---|
| 1287 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
| 1288 | return operator=<EdgeMap>(cmap); |
---|
| 1289 | } |
---|
| 1290 | |
---|
| 1291 | template <typename CMap> |
---|
| 1292 | EdgeMap& operator=(const CMap& cmap) { |
---|
[512] | 1293 | Parent::operator=(cmap); |
---|
[416] | 1294 | return *this; |
---|
| 1295 | } |
---|
| 1296 | }; |
---|
| 1297 | |
---|
| 1298 | }; |
---|
| 1299 | |
---|
| 1300 | /// \ingroup graph_adaptors |
---|
[414] | 1301 | /// |
---|
[451] | 1302 | /// \brief Adaptor class for hiding nodes and edges in an undirected |
---|
| 1303 | /// graph. |
---|
[414] | 1304 | /// |
---|
[451] | 1305 | /// SubGraph can be used for hiding nodes and edges in a graph. |
---|
| 1306 | /// A \c bool node map and a \c bool edge map must be specified, which |
---|
| 1307 | /// define the filters for nodes and edges. |
---|
| 1308 | /// Only the nodes and edges with \c true filter value are |
---|
[453] | 1309 | /// shown in the subgraph. The edges that are incident to hidden |
---|
| 1310 | /// nodes are also filtered out. |
---|
| 1311 | /// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
---|
[416] | 1312 | /// |
---|
[451] | 1313 | /// The adapted graph can also be modified through this adaptor |
---|
[453] | 1314 | /// by adding or removing nodes or edges, unless the \c GR template |
---|
[451] | 1315 | /// parameter is set to be \c const. |
---|
| 1316 | /// |
---|
[453] | 1317 | /// \tparam GR The type of the adapted graph. |
---|
[451] | 1318 | /// It must conform to the \ref concepts::Graph "Graph" concept. |
---|
| 1319 | /// It can also be specified to be \c const. |
---|
[453] | 1320 | /// \tparam NF The type of the node filter map. |
---|
| 1321 | /// It must be a \c bool (or convertible) node map of the |
---|
| 1322 | /// adapted graph. The default type is |
---|
| 1323 | /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
---|
| 1324 | /// \tparam EF The type of the edge filter map. |
---|
| 1325 | /// It must be a \c bool (or convertible) edge map of the |
---|
| 1326 | /// adapted graph. The default type is |
---|
| 1327 | /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
---|
[451] | 1328 | /// |
---|
| 1329 | /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
---|
| 1330 | /// adapted graph are convertible to each other. |
---|
[416] | 1331 | /// |
---|
| 1332 | /// \see FilterNodes |
---|
| 1333 | /// \see FilterEdges |
---|
[451] | 1334 | #ifdef DOXYGEN |
---|
[453] | 1335 | template<typename GR, typename NF, typename EF> |
---|
| 1336 | class SubGraph { |
---|
[451] | 1337 | #else |
---|
[453] | 1338 | template<typename GR, |
---|
| 1339 | typename NF = typename GR::template NodeMap<bool>, |
---|
| 1340 | typename EF = typename GR::template EdgeMap<bool> > |
---|
| 1341 | class SubGraph : |
---|
| 1342 | public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > { |
---|
[451] | 1343 | #endif |
---|
[414] | 1344 | public: |
---|
[451] | 1345 | /// The type of the adapted graph. |
---|
[453] | 1346 | typedef GR Graph; |
---|
[451] | 1347 | /// The type of the node filter map. |
---|
[453] | 1348 | typedef NF NodeFilterMap; |
---|
[451] | 1349 | /// The type of the edge filter map. |
---|
[453] | 1350 | typedef EF EdgeFilterMap; |
---|
| 1351 | |
---|
[512] | 1352 | typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > |
---|
[453] | 1353 | Parent; |
---|
[414] | 1354 | |
---|
[415] | 1355 | typedef typename Parent::Node Node; |
---|
[416] | 1356 | typedef typename Parent::Edge Edge; |
---|
[415] | 1357 | |
---|
[414] | 1358 | protected: |
---|
[416] | 1359 | SubGraph() { } |
---|
[414] | 1360 | public: |
---|
| 1361 | |
---|
[415] | 1362 | /// \brief Constructor |
---|
| 1363 | /// |
---|
[451] | 1364 | /// Creates a subgraph for the given graph with the given node |
---|
| 1365 | /// and edge filter maps. |
---|
[512] | 1366 | SubGraph(GR& graph, NF& node_filter, EF& edge_filter) { |
---|
| 1367 | initialize(graph, node_filter, edge_filter); |
---|
[414] | 1368 | } |
---|
| 1369 | |
---|
[452] | 1370 | /// \brief Sets the status of the given node |
---|
[415] | 1371 | /// |
---|
[452] | 1372 | /// This function sets the status of the given node. |
---|
[451] | 1373 | /// It is done by simply setting the assigned value of \c n |
---|
[452] | 1374 | /// to \c v in the node filter map. |
---|
| 1375 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
| 1376 | |
---|
| 1377 | /// \brief Sets the status of the given edge |
---|
[416] | 1378 | /// |
---|
[452] | 1379 | /// This function sets the status of the given edge. |
---|
[451] | 1380 | /// It is done by simply setting the assigned value of \c e |
---|
[452] | 1381 | /// to \c v in the edge filter map. |
---|
| 1382 | void status(const Edge& e, bool v) const { Parent::status(e, v); } |
---|
| 1383 | |
---|
| 1384 | /// \brief Returns the status of the given node |
---|
[415] | 1385 | /// |
---|
[452] | 1386 | /// This function returns the status of the given node. |
---|
| 1387 | /// It is \c true if the given node is enabled (i.e. not hidden). |
---|
| 1388 | bool status(const Node& n) const { return Parent::status(n); } |
---|
| 1389 | |
---|
| 1390 | /// \brief Returns the status of the given edge |
---|
[416] | 1391 | /// |
---|
[452] | 1392 | /// This function returns the status of the given edge. |
---|
| 1393 | /// It is \c true if the given edge is enabled (i.e. not hidden). |
---|
| 1394 | bool status(const Edge& e) const { return Parent::status(e); } |
---|
| 1395 | |
---|
| 1396 | /// \brief Disables the given node |
---|
[415] | 1397 | /// |
---|
[452] | 1398 | /// This function disables the given node in the subdigraph, |
---|
| 1399 | /// so the iteration jumps over it. |
---|
| 1400 | /// It is the same as \ref status() "status(n, false)". |
---|
| 1401 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
| 1402 | |
---|
| 1403 | /// \brief Disables the given edge |
---|
[415] | 1404 | /// |
---|
[452] | 1405 | /// This function disables the given edge in the subgraph, |
---|
| 1406 | /// so the iteration jumps over it. |
---|
| 1407 | /// It is the same as \ref status() "status(e, false)". |
---|
| 1408 | void disable(const Edge& e) const { Parent::status(e, false); } |
---|
| 1409 | |
---|
| 1410 | /// \brief Enables the given node |
---|
| 1411 | /// |
---|
| 1412 | /// This function enables the given node in the subdigraph. |
---|
| 1413 | /// It is the same as \ref status() "status(n, true)". |
---|
| 1414 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
| 1415 | |
---|
| 1416 | /// \brief Enables the given edge |
---|
| 1417 | /// |
---|
| 1418 | /// This function enables the given edge in the subgraph. |
---|
| 1419 | /// It is the same as \ref status() "status(e, true)". |
---|
| 1420 | void enable(const Edge& e) const { Parent::status(e, true); } |
---|
| 1421 | |
---|
[414] | 1422 | }; |
---|
| 1423 | |
---|
[451] | 1424 | /// \brief Returns a read-only SubGraph adaptor |
---|
[414] | 1425 | /// |
---|
[451] | 1426 | /// This function just returns a read-only \ref SubGraph adaptor. |
---|
| 1427 | /// \ingroup graph_adaptors |
---|
| 1428 | /// \relates SubGraph |
---|
[453] | 1429 | template<typename GR, typename NF, typename EF> |
---|
| 1430 | SubGraph<const GR, NF, EF> |
---|
[512] | 1431 | subGraph(const GR& graph, NF& node_filter, EF& edge_filter) { |
---|
[453] | 1432 | return SubGraph<const GR, NF, EF> |
---|
[512] | 1433 | (graph, node_filter, edge_filter); |
---|
[416] | 1434 | } |
---|
| 1435 | |
---|
[453] | 1436 | template<typename GR, typename NF, typename EF> |
---|
| 1437 | SubGraph<const GR, const NF, EF> |
---|
[512] | 1438 | subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) { |
---|
[453] | 1439 | return SubGraph<const GR, const NF, EF> |
---|
[512] | 1440 | (graph, node_filter, edge_filter); |
---|
[416] | 1441 | } |
---|
| 1442 | |
---|
[453] | 1443 | template<typename GR, typename NF, typename EF> |
---|
| 1444 | SubGraph<const GR, NF, const EF> |
---|
[512] | 1445 | subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) { |
---|
[453] | 1446 | return SubGraph<const GR, NF, const EF> |
---|
[512] | 1447 | (graph, node_filter, edge_filter); |
---|
[416] | 1448 | } |
---|
| 1449 | |
---|
[453] | 1450 | template<typename GR, typename NF, typename EF> |
---|
| 1451 | SubGraph<const GR, const NF, const EF> |
---|
[512] | 1452 | subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) { |
---|
[453] | 1453 | return SubGraph<const GR, const NF, const EF> |
---|
[512] | 1454 | (graph, node_filter, edge_filter); |
---|
[416] | 1455 | } |
---|
| 1456 | |
---|
[451] | 1457 | |
---|
[416] | 1458 | /// \ingroup graph_adaptors |
---|
| 1459 | /// |
---|
[451] | 1460 | /// \brief Adaptor class for hiding nodes in a digraph or a graph. |
---|
[416] | 1461 | /// |
---|
[451] | 1462 | /// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
---|
| 1463 | /// graph. A \c bool node map must be specified, which defines the filter |
---|
| 1464 | /// for the nodes. Only the nodes with \c true filter value and the |
---|
| 1465 | /// arcs/edges incident to nodes both with \c true filter value are shown |
---|
| 1466 | /// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
---|
| 1467 | /// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
---|
[453] | 1468 | /// depending on the \c GR template parameter. |
---|
[416] | 1469 | /// |
---|
[451] | 1470 | /// The adapted (di)graph can also be modified through this adaptor |
---|
[453] | 1471 | /// by adding or removing nodes or arcs/edges, unless the \c GR template |
---|
[451] | 1472 | /// parameter is set to be \c const. |
---|
| 1473 | /// |
---|
[453] | 1474 | /// \tparam GR The type of the adapted digraph or graph. |
---|
[451] | 1475 | /// It must conform to the \ref concepts::Digraph "Digraph" concept |
---|
| 1476 | /// or the \ref concepts::Graph "Graph" concept. |
---|
| 1477 | /// It can also be specified to be \c const. |
---|
[453] | 1478 | /// \tparam NF The type of the node filter map. |
---|
| 1479 | /// It must be a \c bool (or convertible) node map of the |
---|
| 1480 | /// adapted (di)graph. The default type is |
---|
| 1481 | /// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
---|
[451] | 1482 | /// |
---|
| 1483 | /// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
---|
| 1484 | /// adapted (di)graph are convertible to each other. |
---|
[416] | 1485 | #ifdef DOXYGEN |
---|
[453] | 1486 | template<typename GR, typename NF> |
---|
| 1487 | class FilterNodes { |
---|
[416] | 1488 | #else |
---|
[453] | 1489 | template<typename GR, |
---|
| 1490 | typename NF = typename GR::template NodeMap<bool>, |
---|
[416] | 1491 | typename Enable = void> |
---|
[453] | 1492 | class FilterNodes : |
---|
| 1493 | public DigraphAdaptorExtender< |
---|
[512] | 1494 | SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
---|
| 1495 | true> > { |
---|
[416] | 1496 | #endif |
---|
[453] | 1497 | typedef DigraphAdaptorExtender< |
---|
[512] | 1498 | SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
---|
| 1499 | true> > Parent; |
---|
[416] | 1500 | |
---|
[617] | 1501 | public: |
---|
| 1502 | |
---|
| 1503 | typedef GR Digraph; |
---|
| 1504 | typedef NF NodeFilterMap; |
---|
| 1505 | |
---|
[416] | 1506 | typedef typename Parent::Node Node; |
---|
| 1507 | |
---|
| 1508 | protected: |
---|
[512] | 1509 | ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
---|
| 1510 | |
---|
| 1511 | FilterNodes() : const_true_map() {} |
---|
[416] | 1512 | |
---|
| 1513 | public: |
---|
| 1514 | |
---|
| 1515 | /// \brief Constructor |
---|
| 1516 | /// |
---|
[451] | 1517 | /// Creates a subgraph for the given digraph or graph with the |
---|
[416] | 1518 | /// given node filter map. |
---|
[512] | 1519 | FilterNodes(GR& graph, NF& node_filter) |
---|
| 1520 | : Parent(), const_true_map() |
---|
[453] | 1521 | { |
---|
[512] | 1522 | Parent::initialize(graph, node_filter, const_true_map); |
---|
[416] | 1523 | } |
---|
| 1524 | |
---|
[452] | 1525 | /// \brief Sets the status of the given node |
---|
[416] | 1526 | /// |
---|
[452] | 1527 | /// This function sets the status of the given node. |
---|
[451] | 1528 | /// It is done by simply setting the assigned value of \c n |
---|
[452] | 1529 | /// to \c v in the node filter map. |
---|
| 1530 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
| 1531 | |
---|
| 1532 | /// \brief Returns the status of the given node |
---|
[416] | 1533 | /// |
---|
[452] | 1534 | /// This function returns the status of the given node. |
---|
| 1535 | /// It is \c true if the given node is enabled (i.e. not hidden). |
---|
| 1536 | bool status(const Node& n) const { return Parent::status(n); } |
---|
| 1537 | |
---|
| 1538 | /// \brief Disables the given node |
---|
[416] | 1539 | /// |
---|
[452] | 1540 | /// This function disables the given node, so the iteration |
---|
| 1541 | /// jumps over it. |
---|
| 1542 | /// It is the same as \ref status() "status(n, false)". |
---|
| 1543 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
| 1544 | |
---|
| 1545 | /// \brief Enables the given node |
---|
| 1546 | /// |
---|
| 1547 | /// This function enables the given node. |
---|
| 1548 | /// It is the same as \ref status() "status(n, true)". |
---|
| 1549 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
[416] | 1550 | |
---|
| 1551 | }; |
---|
| 1552 | |
---|
[453] | 1553 | template<typename GR, typename NF> |
---|
| 1554 | class FilterNodes<GR, NF, |
---|
| 1555 | typename enable_if<UndirectedTagIndicator<GR> >::type> : |
---|
| 1556 | public GraphAdaptorExtender< |
---|
[512] | 1557 | SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
---|
| 1558 | true> > { |
---|
[453] | 1559 | |
---|
| 1560 | typedef GraphAdaptorExtender< |
---|
[512] | 1561 | SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
---|
| 1562 | true> > Parent; |
---|
[416] | 1563 | |
---|
[617] | 1564 | public: |
---|
| 1565 | |
---|
| 1566 | typedef GR Graph; |
---|
| 1567 | typedef NF NodeFilterMap; |
---|
| 1568 | |
---|
[416] | 1569 | typedef typename Parent::Node Node; |
---|
[617] | 1570 | |
---|
[416] | 1571 | protected: |
---|
[512] | 1572 | ConstMap<typename GR::Edge, Const<bool, true> > const_true_map; |
---|
| 1573 | |
---|
| 1574 | FilterNodes() : const_true_map() {} |
---|
[416] | 1575 | |
---|
| 1576 | public: |
---|
| 1577 | |
---|
[512] | 1578 | FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
---|
| 1579 | Parent(), const_true_map() { |
---|
| 1580 | Parent::initialize(graph, node_filter, const_true_map); |
---|
[416] | 1581 | } |
---|
| 1582 | |
---|
[452] | 1583 | void status(const Node& n, bool v) const { Parent::status(n, v); } |
---|
| 1584 | bool status(const Node& n) const { return Parent::status(n); } |
---|
| 1585 | void disable(const Node& n) const { Parent::status(n, false); } |
---|
| 1586 | void enable(const Node& n) const { Parent::status(n, true); } |
---|
[416] | 1587 | |
---|
| 1588 | }; |
---|
| 1589 | |
---|
| 1590 | |
---|
[451] | 1591 | /// \brief Returns a read-only FilterNodes adaptor |
---|
[416] | 1592 | /// |
---|
[451] | 1593 | /// This function just returns a read-only \ref FilterNodes adaptor. |
---|
| 1594 | /// \ingroup graph_adaptors |
---|
| 1595 | /// \relates FilterNodes |
---|
[453] | 1596 | template<typename GR, typename NF> |
---|
| 1597 | FilterNodes<const GR, NF> |
---|
[512] | 1598 | filterNodes(const GR& graph, NF& node_filter) { |
---|
| 1599 | return FilterNodes<const GR, NF>(graph, node_filter); |
---|
[414] | 1600 | } |
---|
| 1601 | |
---|
[453] | 1602 | template<typename GR, typename NF> |
---|
| 1603 | FilterNodes<const GR, const NF> |
---|
[512] | 1604 | filterNodes(const GR& graph, const NF& node_filter) { |
---|
| 1605 | return FilterNodes<const GR, const NF>(graph, node_filter); |
---|
[414] | 1606 | } |
---|
| 1607 | |
---|
[416] | 1608 | /// \ingroup graph_adaptors |
---|
[414] | 1609 | /// |
---|
[451] | 1610 | /// \brief Adaptor class for hiding arcs in a digraph. |
---|
[414] | 1611 | /// |
---|
[451] | 1612 | /// FilterArcs adaptor can be used for hiding arcs in a digraph. |
---|
| 1613 | /// A \c bool arc map must be specified, which defines the filter for |
---|
| 1614 | /// the arcs. Only the arcs with \c true filter value are shown in the |
---|
| 1615 | /// subdigraph. This adaptor conforms to the \ref concepts::Digraph |
---|
| 1616 | /// "Digraph" concept. |
---|
[414] | 1617 | /// |
---|
[451] | 1618 | /// The adapted digraph can also be modified through this adaptor |
---|
[453] | 1619 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
[451] | 1620 | /// parameter is set to be \c const. |
---|
| 1621 | /// |
---|
[512] | 1622 | /// \tparam DGR The type of the adapted digraph. |
---|
[451] | 1623 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
| 1624 | /// It can also be specified to be \c const. |
---|
[453] | 1625 | /// \tparam AF The type of the arc filter map. |
---|
| 1626 | /// It must be a \c bool (or convertible) arc map of the |
---|
| 1627 | /// adapted digraph. The default type is |
---|
[512] | 1628 | /// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
---|
[451] | 1629 | /// |
---|
| 1630 | /// \note The \c Node and \c Arc types of this adaptor and the adapted |
---|
| 1631 | /// digraph are convertible to each other. |
---|
| 1632 | #ifdef DOXYGEN |
---|
[512] | 1633 | template<typename DGR, |
---|
[453] | 1634 | typename AF> |
---|
| 1635 | class FilterArcs { |
---|
[451] | 1636 | #else |
---|
[512] | 1637 | template<typename DGR, |
---|
| 1638 | typename AF = typename DGR::template ArcMap<bool> > |
---|
[453] | 1639 | class FilterArcs : |
---|
| 1640 | public DigraphAdaptorExtender< |
---|
[512] | 1641 | SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
---|
| 1642 | AF, false> > { |
---|
[451] | 1643 | #endif |
---|
[617] | 1644 | typedef DigraphAdaptorExtender< |
---|
| 1645 | SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
---|
| 1646 | AF, false> > Parent; |
---|
| 1647 | |
---|
[414] | 1648 | public: |
---|
[617] | 1649 | |
---|
[453] | 1650 | /// The type of the adapted digraph. |
---|
[512] | 1651 | typedef DGR Digraph; |
---|
[453] | 1652 | /// The type of the arc filter map. |
---|
| 1653 | typedef AF ArcFilterMap; |
---|
| 1654 | |
---|
[415] | 1655 | typedef typename Parent::Arc Arc; |
---|
| 1656 | |
---|
[414] | 1657 | protected: |
---|
[512] | 1658 | ConstMap<typename DGR::Node, Const<bool, true> > const_true_map; |
---|
| 1659 | |
---|
| 1660 | FilterArcs() : const_true_map() {} |
---|
[414] | 1661 | |
---|
| 1662 | public: |
---|
| 1663 | |
---|
[415] | 1664 | /// \brief Constructor |
---|
| 1665 | /// |
---|
[451] | 1666 | /// Creates a subdigraph for the given digraph with the given arc |
---|
| 1667 | /// filter map. |
---|
[512] | 1668 | FilterArcs(DGR& digraph, ArcFilterMap& arc_filter) |
---|
| 1669 | : Parent(), const_true_map() { |
---|
| 1670 | Parent::initialize(digraph, const_true_map, arc_filter); |
---|
[414] | 1671 | } |
---|
| 1672 | |
---|
[452] | 1673 | /// \brief Sets the status of the given arc |
---|
[415] | 1674 | /// |
---|
[452] | 1675 | /// This function sets the status of the given arc. |
---|
[451] | 1676 | /// It is done by simply setting the assigned value of \c a |
---|
[452] | 1677 | /// to \c v in the arc filter map. |
---|
| 1678 | void status(const Arc& a, bool v) const { Parent::status(a, v); } |
---|
| 1679 | |
---|
| 1680 | /// \brief Returns the status of the given arc |
---|
[415] | 1681 | /// |
---|
[452] | 1682 | /// This function returns the status of the given arc. |
---|
| 1683 | /// It is \c true if the given arc is enabled (i.e. not hidden). |
---|
| 1684 | bool status(const Arc& a) const { return Parent::status(a); } |
---|
| 1685 | |
---|
| 1686 | /// \brief Disables the given arc |
---|
[415] | 1687 | /// |
---|
[452] | 1688 | /// This function disables the given arc in the subdigraph, |
---|
| 1689 | /// so the iteration jumps over it. |
---|
| 1690 | /// It is the same as \ref status() "status(a, false)". |
---|
| 1691 | void disable(const Arc& a) const { Parent::status(a, false); } |
---|
| 1692 | |
---|
| 1693 | /// \brief Enables the given arc |
---|
| 1694 | /// |
---|
| 1695 | /// This function enables the given arc in the subdigraph. |
---|
| 1696 | /// It is the same as \ref status() "status(a, true)". |
---|
| 1697 | void enable(const Arc& a) const { Parent::status(a, true); } |
---|
[415] | 1698 | |
---|
[414] | 1699 | }; |
---|
| 1700 | |
---|
[451] | 1701 | /// \brief Returns a read-only FilterArcs adaptor |
---|
[414] | 1702 | /// |
---|
[451] | 1703 | /// This function just returns a read-only \ref FilterArcs adaptor. |
---|
| 1704 | /// \ingroup graph_adaptors |
---|
| 1705 | /// \relates FilterArcs |
---|
[512] | 1706 | template<typename DGR, typename AF> |
---|
| 1707 | FilterArcs<const DGR, AF> |
---|
| 1708 | filterArcs(const DGR& digraph, AF& arc_filter) { |
---|
| 1709 | return FilterArcs<const DGR, AF>(digraph, arc_filter); |
---|
[414] | 1710 | } |
---|
| 1711 | |
---|
[512] | 1712 | template<typename DGR, typename AF> |
---|
| 1713 | FilterArcs<const DGR, const AF> |
---|
| 1714 | filterArcs(const DGR& digraph, const AF& arc_filter) { |
---|
| 1715 | return FilterArcs<const DGR, const AF>(digraph, arc_filter); |
---|
[414] | 1716 | } |
---|
| 1717 | |
---|
[416] | 1718 | /// \ingroup graph_adaptors |
---|
| 1719 | /// |
---|
[451] | 1720 | /// \brief Adaptor class for hiding edges in a graph. |
---|
[416] | 1721 | /// |
---|
[451] | 1722 | /// FilterEdges adaptor can be used for hiding edges in a graph. |
---|
| 1723 | /// A \c bool edge map must be specified, which defines the filter for |
---|
| 1724 | /// the edges. Only the edges with \c true filter value are shown in the |
---|
| 1725 | /// subgraph. This adaptor conforms to the \ref concepts::Graph |
---|
| 1726 | /// "Graph" concept. |
---|
[416] | 1727 | /// |
---|
[451] | 1728 | /// The adapted graph can also be modified through this adaptor |
---|
[453] | 1729 | /// by adding or removing nodes or edges, unless the \c GR template |
---|
[451] | 1730 | /// parameter is set to be \c const. |
---|
| 1731 | /// |
---|
[453] | 1732 | /// \tparam GR The type of the adapted graph. |
---|
[451] | 1733 | /// It must conform to the \ref concepts::Graph "Graph" concept. |
---|
| 1734 | /// It can also be specified to be \c const. |
---|
[453] | 1735 | /// \tparam EF The type of the edge filter map. |
---|
| 1736 | /// It must be a \c bool (or convertible) edge map of the |
---|
| 1737 | /// adapted graph. The default type is |
---|
| 1738 | /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
---|
[451] | 1739 | /// |
---|
| 1740 | /// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
---|
| 1741 | /// adapted graph are convertible to each other. |
---|
| 1742 | #ifdef DOXYGEN |
---|
[453] | 1743 | template<typename GR, |
---|
| 1744 | typename EF> |
---|
| 1745 | class FilterEdges { |
---|
[451] | 1746 | #else |
---|
[453] | 1747 | template<typename GR, |
---|
| 1748 | typename EF = typename GR::template EdgeMap<bool> > |
---|
| 1749 | class FilterEdges : |
---|
| 1750 | public GraphAdaptorExtender< |
---|
[512] | 1751 | SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
---|
| 1752 | EF, false> > { |
---|
[451] | 1753 | #endif |
---|
[617] | 1754 | typedef GraphAdaptorExtender< |
---|
| 1755 | SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
---|
| 1756 | EF, false> > Parent; |
---|
| 1757 | |
---|
[416] | 1758 | public: |
---|
[617] | 1759 | |
---|
[453] | 1760 | /// The type of the adapted graph. |
---|
| 1761 | typedef GR Graph; |
---|
| 1762 | /// The type of the edge filter map. |
---|
| 1763 | typedef EF EdgeFilterMap; |
---|
| 1764 | |
---|
[416] | 1765 | typedef typename Parent::Edge Edge; |
---|
[453] | 1766 | |
---|
[416] | 1767 | protected: |
---|
[512] | 1768 | ConstMap<typename GR::Node, Const<bool, true> > const_true_map; |
---|
[416] | 1769 | |
---|
| 1770 | FilterEdges() : const_true_map(true) { |
---|
| 1771 | Parent::setNodeFilterMap(const_true_map); |
---|
| 1772 | } |
---|
| 1773 | |
---|
| 1774 | public: |
---|
| 1775 | |
---|
| 1776 | /// \brief Constructor |
---|
| 1777 | /// |
---|
[451] | 1778 | /// Creates a subgraph for the given graph with the given edge |
---|
| 1779 | /// filter map. |
---|
[512] | 1780 | FilterEdges(GR& graph, EF& edge_filter) |
---|
| 1781 | : Parent(), const_true_map() { |
---|
| 1782 | Parent::initialize(graph, const_true_map, edge_filter); |
---|
[416] | 1783 | } |
---|
| 1784 | |
---|
[452] | 1785 | /// \brief Sets the status of the given edge |
---|
[416] | 1786 | /// |
---|
[452] | 1787 | /// This function sets the status of the given edge. |
---|
[451] | 1788 | /// It is done by simply setting the assigned value of \c e |
---|
[452] | 1789 | /// to \c v in the edge filter map. |
---|
| 1790 | void status(const Edge& e, bool v) const { Parent::status(e, v); } |
---|
| 1791 | |
---|
| 1792 | /// \brief Returns the status of the given edge |
---|
[416] | 1793 | /// |
---|
[452] | 1794 | /// This function returns the status of the given edge. |
---|
| 1795 | /// It is \c true if the given edge is enabled (i.e. not hidden). |
---|
| 1796 | bool status(const Edge& e) const { return Parent::status(e); } |
---|
| 1797 | |
---|
| 1798 | /// \brief Disables the given edge |
---|
[416] | 1799 | /// |
---|
[452] | 1800 | /// This function disables the given edge in the subgraph, |
---|
| 1801 | /// so the iteration jumps over it. |
---|
| 1802 | /// It is the same as \ref status() "status(e, false)". |
---|
| 1803 | void disable(const Edge& e) const { Parent::status(e, false); } |
---|
| 1804 | |
---|
| 1805 | /// \brief Enables the given edge |
---|
| 1806 | /// |
---|
| 1807 | /// This function enables the given edge in the subgraph. |
---|
| 1808 | /// It is the same as \ref status() "status(e, true)". |
---|
| 1809 | void enable(const Edge& e) const { Parent::status(e, true); } |
---|
[416] | 1810 | |
---|
| 1811 | }; |
---|
| 1812 | |
---|
[451] | 1813 | /// \brief Returns a read-only FilterEdges adaptor |
---|
[416] | 1814 | /// |
---|
[451] | 1815 | /// This function just returns a read-only \ref FilterEdges adaptor. |
---|
| 1816 | /// \ingroup graph_adaptors |
---|
| 1817 | /// \relates FilterEdges |
---|
[453] | 1818 | template<typename GR, typename EF> |
---|
| 1819 | FilterEdges<const GR, EF> |
---|
[512] | 1820 | filterEdges(const GR& graph, EF& edge_filter) { |
---|
| 1821 | return FilterEdges<const GR, EF>(graph, edge_filter); |
---|
[416] | 1822 | } |
---|
| 1823 | |
---|
[453] | 1824 | template<typename GR, typename EF> |
---|
| 1825 | FilterEdges<const GR, const EF> |
---|
[512] | 1826 | filterEdges(const GR& graph, const EF& edge_filter) { |
---|
| 1827 | return FilterEdges<const GR, const EF>(graph, edge_filter); |
---|
[416] | 1828 | } |
---|
| 1829 | |
---|
[451] | 1830 | |
---|
[512] | 1831 | template <typename DGR> |
---|
[416] | 1832 | class UndirectorBase { |
---|
[414] | 1833 | public: |
---|
[512] | 1834 | typedef DGR Digraph; |
---|
[416] | 1835 | typedef UndirectorBase Adaptor; |
---|
[414] | 1836 | |
---|
| 1837 | typedef True UndirectedTag; |
---|
| 1838 | |
---|
| 1839 | typedef typename Digraph::Arc Edge; |
---|
| 1840 | typedef typename Digraph::Node Node; |
---|
| 1841 | |
---|
[656] | 1842 | class Arc { |
---|
[416] | 1843 | friend class UndirectorBase; |
---|
[414] | 1844 | protected: |
---|
[656] | 1845 | Edge _edge; |
---|
[414] | 1846 | bool _forward; |
---|
| 1847 | |
---|
[656] | 1848 | Arc(const Edge& edge, bool forward) |
---|
| 1849 | : _edge(edge), _forward(forward) {} |
---|
[414] | 1850 | |
---|
| 1851 | public: |
---|
| 1852 | Arc() {} |
---|
| 1853 | |
---|
[656] | 1854 | Arc(Invalid) : _edge(INVALID), _forward(true) {} |
---|
| 1855 | |
---|
| 1856 | operator const Edge&() const { return _edge; } |
---|
[414] | 1857 | |
---|
| 1858 | bool operator==(const Arc &other) const { |
---|
[656] | 1859 | return _forward == other._forward && _edge == other._edge; |
---|
[414] | 1860 | } |
---|
| 1861 | bool operator!=(const Arc &other) const { |
---|
[656] | 1862 | return _forward != other._forward || _edge != other._edge; |
---|
[414] | 1863 | } |
---|
| 1864 | bool operator<(const Arc &other) const { |
---|
[416] | 1865 | return _forward < other._forward || |
---|
[656] | 1866 | (_forward == other._forward && _edge < other._edge); |
---|
[414] | 1867 | } |
---|
| 1868 | }; |
---|
| 1869 | |
---|
| 1870 | void first(Node& n) const { |
---|
| 1871 | _digraph->first(n); |
---|
| 1872 | } |
---|
| 1873 | |
---|
| 1874 | void next(Node& n) const { |
---|
| 1875 | _digraph->next(n); |
---|
| 1876 | } |
---|
| 1877 | |
---|
| 1878 | void first(Arc& a) const { |
---|
[656] | 1879 | _digraph->first(a._edge); |
---|
[414] | 1880 | a._forward = true; |
---|
| 1881 | } |
---|
| 1882 | |
---|
| 1883 | void next(Arc& a) const { |
---|
| 1884 | if (a._forward) { |
---|
[416] | 1885 | a._forward = false; |
---|
[414] | 1886 | } else { |
---|
[656] | 1887 | _digraph->next(a._edge); |
---|
[416] | 1888 | a._forward = true; |
---|
[414] | 1889 | } |
---|
| 1890 | } |
---|
| 1891 | |
---|
| 1892 | void first(Edge& e) const { |
---|
| 1893 | _digraph->first(e); |
---|
| 1894 | } |
---|
| 1895 | |
---|
| 1896 | void next(Edge& e) const { |
---|
| 1897 | _digraph->next(e); |
---|
| 1898 | } |
---|
| 1899 | |
---|
| 1900 | void firstOut(Arc& a, const Node& n) const { |
---|
[656] | 1901 | _digraph->firstIn(a._edge, n); |
---|
| 1902 | if (a._edge != INVALID ) { |
---|
[416] | 1903 | a._forward = false; |
---|
[414] | 1904 | } else { |
---|
[656] | 1905 | _digraph->firstOut(a._edge, n); |
---|
[416] | 1906 | a._forward = true; |
---|
[414] | 1907 | } |
---|
| 1908 | } |
---|
| 1909 | void nextOut(Arc &a) const { |
---|
| 1910 | if (!a._forward) { |
---|
[656] | 1911 | Node n = _digraph->target(a._edge); |
---|
| 1912 | _digraph->nextIn(a._edge); |
---|
| 1913 | if (a._edge == INVALID) { |
---|
| 1914 | _digraph->firstOut(a._edge, n); |
---|
[416] | 1915 | a._forward = true; |
---|
| 1916 | } |
---|
[414] | 1917 | } |
---|
| 1918 | else { |
---|
[656] | 1919 | _digraph->nextOut(a._edge); |
---|
[414] | 1920 | } |
---|
| 1921 | } |
---|
| 1922 | |
---|
| 1923 | void firstIn(Arc &a, const Node &n) const { |
---|
[656] | 1924 | _digraph->firstOut(a._edge, n); |
---|
| 1925 | if (a._edge != INVALID ) { |
---|
[416] | 1926 | a._forward = false; |
---|
[414] | 1927 | } else { |
---|
[656] | 1928 | _digraph->firstIn(a._edge, n); |
---|
[416] | 1929 | a._forward = true; |
---|
[414] | 1930 | } |
---|
| 1931 | } |
---|
| 1932 | void nextIn(Arc &a) const { |
---|
| 1933 | if (!a._forward) { |
---|
[656] | 1934 | Node n = _digraph->source(a._edge); |
---|
| 1935 | _digraph->nextOut(a._edge); |
---|
| 1936 | if (a._edge == INVALID ) { |
---|
| 1937 | _digraph->firstIn(a._edge, n); |
---|
[416] | 1938 | a._forward = true; |
---|
| 1939 | } |
---|
[414] | 1940 | } |
---|
| 1941 | else { |
---|
[656] | 1942 | _digraph->nextIn(a._edge); |
---|
[414] | 1943 | } |
---|
| 1944 | } |
---|
| 1945 | |
---|
| 1946 | void firstInc(Edge &e, bool &d, const Node &n) const { |
---|
| 1947 | d = true; |
---|
| 1948 | _digraph->firstOut(e, n); |
---|
| 1949 | if (e != INVALID) return; |
---|
| 1950 | d = false; |
---|
| 1951 | _digraph->firstIn(e, n); |
---|
| 1952 | } |
---|
| 1953 | |
---|
| 1954 | void nextInc(Edge &e, bool &d) const { |
---|
| 1955 | if (d) { |
---|
[416] | 1956 | Node s = _digraph->source(e); |
---|
| 1957 | _digraph->nextOut(e); |
---|
| 1958 | if (e != INVALID) return; |
---|
| 1959 | d = false; |
---|
| 1960 | _digraph->firstIn(e, s); |
---|
[414] | 1961 | } else { |
---|
[416] | 1962 | _digraph->nextIn(e); |
---|
[414] | 1963 | } |
---|
| 1964 | } |
---|
| 1965 | |
---|
| 1966 | Node u(const Edge& e) const { |
---|
| 1967 | return _digraph->source(e); |
---|
| 1968 | } |
---|
| 1969 | |
---|
| 1970 | Node v(const Edge& e) const { |
---|
| 1971 | return _digraph->target(e); |
---|
| 1972 | } |
---|
| 1973 | |
---|
| 1974 | Node source(const Arc &a) const { |
---|
[656] | 1975 | return a._forward ? _digraph->source(a._edge) : _digraph->target(a._edge); |
---|
[414] | 1976 | } |
---|
| 1977 | |
---|
| 1978 | Node target(const Arc &a) const { |
---|
[656] | 1979 | return a._forward ? _digraph->target(a._edge) : _digraph->source(a._edge); |
---|
[414] | 1980 | } |
---|
| 1981 | |
---|
| 1982 | static Arc direct(const Edge &e, bool d) { |
---|
| 1983 | return Arc(e, d); |
---|
| 1984 | } |
---|
| 1985 | |
---|
| 1986 | static bool direction(const Arc &a) { return a._forward; } |
---|
| 1987 | |
---|
| 1988 | Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
---|
| 1989 | Arc arcFromId(int ix) const { |
---|
| 1990 | return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); |
---|
| 1991 | } |
---|
| 1992 | Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } |
---|
| 1993 | |
---|
| 1994 | int id(const Node &n) const { return _digraph->id(n); } |
---|
| 1995 | int id(const Arc &a) const { |
---|
| 1996 | return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); |
---|
| 1997 | } |
---|
| 1998 | int id(const Edge &e) const { return _digraph->id(e); } |
---|
| 1999 | |
---|
| 2000 | int maxNodeId() const { return _digraph->maxNodeId(); } |
---|
| 2001 | int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } |
---|
| 2002 | int maxEdgeId() const { return _digraph->maxArcId(); } |
---|
| 2003 | |
---|
| 2004 | Node addNode() { return _digraph->addNode(); } |
---|
[416] | 2005 | Edge addEdge(const Node& u, const Node& v) { |
---|
| 2006 | return _digraph->addArc(u, v); |
---|
[414] | 2007 | } |
---|
| 2008 | |
---|
| 2009 | void erase(const Node& i) { _digraph->erase(i); } |
---|
| 2010 | void erase(const Edge& i) { _digraph->erase(i); } |
---|
[416] | 2011 | |
---|
[414] | 2012 | void clear() { _digraph->clear(); } |
---|
| 2013 | |
---|
| 2014 | typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
---|
[449] | 2015 | int nodeNum() const { return _digraph->nodeNum(); } |
---|
[446] | 2016 | |
---|
| 2017 | typedef ArcNumTagIndicator<Digraph> ArcNumTag; |
---|
[414] | 2018 | int arcNum() const { return 2 * _digraph->arcNum(); } |
---|
[446] | 2019 | |
---|
| 2020 | typedef ArcNumTag EdgeNumTag; |
---|
[414] | 2021 | int edgeNum() const { return _digraph->arcNum(); } |
---|
| 2022 | |
---|
[446] | 2023 | typedef FindArcTagIndicator<Digraph> FindArcTag; |
---|
[414] | 2024 | Arc findArc(Node s, Node t, Arc p = INVALID) const { |
---|
| 2025 | if (p == INVALID) { |
---|
[416] | 2026 | Edge arc = _digraph->findArc(s, t); |
---|
| 2027 | if (arc != INVALID) return direct(arc, true); |
---|
| 2028 | arc = _digraph->findArc(t, s); |
---|
| 2029 | if (arc != INVALID) return direct(arc, false); |
---|
[414] | 2030 | } else if (direction(p)) { |
---|
[416] | 2031 | Edge arc = _digraph->findArc(s, t, p); |
---|
| 2032 | if (arc != INVALID) return direct(arc, true); |
---|
| 2033 | arc = _digraph->findArc(t, s); |
---|
| 2034 | if (arc != INVALID) return direct(arc, false); |
---|
[414] | 2035 | } else { |
---|
[416] | 2036 | Edge arc = _digraph->findArc(t, s, p); |
---|
| 2037 | if (arc != INVALID) return direct(arc, false); |
---|
[414] | 2038 | } |
---|
| 2039 | return INVALID; |
---|
| 2040 | } |
---|
| 2041 | |
---|
[446] | 2042 | typedef FindArcTag FindEdgeTag; |
---|
[414] | 2043 | Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
---|
| 2044 | if (s != t) { |
---|
| 2045 | if (p == INVALID) { |
---|
| 2046 | Edge arc = _digraph->findArc(s, t); |
---|
| 2047 | if (arc != INVALID) return arc; |
---|
| 2048 | arc = _digraph->findArc(t, s); |
---|
| 2049 | if (arc != INVALID) return arc; |
---|
[449] | 2050 | } else if (_digraph->source(p) == s) { |
---|
[414] | 2051 | Edge arc = _digraph->findArc(s, t, p); |
---|
| 2052 | if (arc != INVALID) return arc; |
---|
| 2053 | arc = _digraph->findArc(t, s); |
---|
[416] | 2054 | if (arc != INVALID) return arc; |
---|
[414] | 2055 | } else { |
---|
| 2056 | Edge arc = _digraph->findArc(t, s, p); |
---|
[416] | 2057 | if (arc != INVALID) return arc; |
---|
[414] | 2058 | } |
---|
| 2059 | } else { |
---|
| 2060 | return _digraph->findArc(s, t, p); |
---|
| 2061 | } |
---|
| 2062 | return INVALID; |
---|
| 2063 | } |
---|
| 2064 | |
---|
| 2065 | private: |
---|
[416] | 2066 | |
---|
[512] | 2067 | template <typename V> |
---|
[414] | 2068 | class ArcMapBase { |
---|
| 2069 | private: |
---|
[416] | 2070 | |
---|
[512] | 2071 | typedef typename DGR::template ArcMap<V> MapImpl; |
---|
[416] | 2072 | |
---|
[414] | 2073 | public: |
---|
| 2074 | |
---|
| 2075 | typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
---|
| 2076 | |
---|
[512] | 2077 | typedef V Value; |
---|
[414] | 2078 | typedef Arc Key; |
---|
[449] | 2079 | typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue; |
---|
| 2080 | typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue; |
---|
| 2081 | typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference; |
---|
| 2082 | typedef typename MapTraits<MapImpl>::ReturnValue Reference; |
---|
[416] | 2083 | |
---|
[512] | 2084 | ArcMapBase(const UndirectorBase<DGR>& adaptor) : |
---|
[416] | 2085 | _forward(*adaptor._digraph), _backward(*adaptor._digraph) {} |
---|
| 2086 | |
---|
[512] | 2087 | ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
| 2088 | : _forward(*adaptor._digraph, value), |
---|
| 2089 | _backward(*adaptor._digraph, value) {} |
---|
| 2090 | |
---|
| 2091 | void set(const Arc& a, const V& value) { |
---|
[416] | 2092 | if (direction(a)) { |
---|
[512] | 2093 | _forward.set(a, value); |
---|
[416] | 2094 | } else { |
---|
[512] | 2095 | _backward.set(a, value); |
---|
[414] | 2096 | } |
---|
| 2097 | } |
---|
| 2098 | |
---|
[449] | 2099 | ConstReturnValue operator[](const Arc& a) const { |
---|
[416] | 2100 | if (direction(a)) { |
---|
| 2101 | return _forward[a]; |
---|
| 2102 | } else { |
---|
| 2103 | return _backward[a]; |
---|
[414] | 2104 | } |
---|
| 2105 | } |
---|
| 2106 | |
---|
[449] | 2107 | ReturnValue operator[](const Arc& a) { |
---|
[416] | 2108 | if (direction(a)) { |
---|
| 2109 | return _forward[a]; |
---|
| 2110 | } else { |
---|
| 2111 | return _backward[a]; |
---|
| 2112 | } |
---|
| 2113 | } |
---|
| 2114 | |
---|
[414] | 2115 | protected: |
---|
| 2116 | |
---|
[416] | 2117 | MapImpl _forward, _backward; |
---|
[414] | 2118 | |
---|
| 2119 | }; |
---|
| 2120 | |
---|
| 2121 | public: |
---|
| 2122 | |
---|
[512] | 2123 | template <typename V> |
---|
| 2124 | class NodeMap : public DGR::template NodeMap<V> { |
---|
[617] | 2125 | typedef typename DGR::template NodeMap<V> Parent; |
---|
| 2126 | |
---|
[414] | 2127 | public: |
---|
[512] | 2128 | typedef V Value; |
---|
| 2129 | |
---|
| 2130 | explicit NodeMap(const UndirectorBase<DGR>& adaptor) |
---|
[416] | 2131 | : Parent(*adaptor._digraph) {} |
---|
[414] | 2132 | |
---|
[512] | 2133 | NodeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
[416] | 2134 | : Parent(*adaptor._digraph, value) { } |
---|
[414] | 2135 | |
---|
| 2136 | private: |
---|
| 2137 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 2138 | return operator=<NodeMap>(cmap); |
---|
| 2139 | } |
---|
| 2140 | |
---|
| 2141 | template <typename CMap> |
---|
| 2142 | NodeMap& operator=(const CMap& cmap) { |
---|
| 2143 | Parent::operator=(cmap); |
---|
| 2144 | return *this; |
---|
| 2145 | } |
---|
[416] | 2146 | |
---|
[414] | 2147 | }; |
---|
| 2148 | |
---|
[512] | 2149 | template <typename V> |
---|
[416] | 2150 | class ArcMap |
---|
[617] | 2151 | : public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > { |
---|
| 2152 | typedef SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > Parent; |
---|
| 2153 | |
---|
[414] | 2154 | public: |
---|
[512] | 2155 | typedef V Value; |
---|
| 2156 | |
---|
| 2157 | explicit ArcMap(const UndirectorBase<DGR>& adaptor) |
---|
[416] | 2158 | : Parent(adaptor) {} |
---|
| 2159 | |
---|
[512] | 2160 | ArcMap(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
[416] | 2161 | : Parent(adaptor, value) {} |
---|
| 2162 | |
---|
[414] | 2163 | private: |
---|
| 2164 | ArcMap& operator=(const ArcMap& cmap) { |
---|
[416] | 2165 | return operator=<ArcMap>(cmap); |
---|
[414] | 2166 | } |
---|
[416] | 2167 | |
---|
[414] | 2168 | template <typename CMap> |
---|
| 2169 | ArcMap& operator=(const CMap& cmap) { |
---|
| 2170 | Parent::operator=(cmap); |
---|
[416] | 2171 | return *this; |
---|
[414] | 2172 | } |
---|
| 2173 | }; |
---|
[416] | 2174 | |
---|
[512] | 2175 | template <typename V> |
---|
| 2176 | class EdgeMap : public Digraph::template ArcMap<V> { |
---|
[617] | 2177 | typedef typename Digraph::template ArcMap<V> Parent; |
---|
| 2178 | |
---|
[414] | 2179 | public: |
---|
[512] | 2180 | typedef V Value; |
---|
| 2181 | |
---|
| 2182 | explicit EdgeMap(const UndirectorBase<DGR>& adaptor) |
---|
[416] | 2183 | : Parent(*adaptor._digraph) {} |
---|
[414] | 2184 | |
---|
[512] | 2185 | EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
---|
[416] | 2186 | : Parent(*adaptor._digraph, value) {} |
---|
[414] | 2187 | |
---|
| 2188 | private: |
---|
| 2189 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
| 2190 | return operator=<EdgeMap>(cmap); |
---|
| 2191 | } |
---|
| 2192 | |
---|
| 2193 | template <typename CMap> |
---|
| 2194 | EdgeMap& operator=(const CMap& cmap) { |
---|
| 2195 | Parent::operator=(cmap); |
---|
| 2196 | return *this; |
---|
| 2197 | } |
---|
| 2198 | |
---|
| 2199 | }; |
---|
| 2200 | |
---|
[512] | 2201 | typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
---|
[416] | 2202 | NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
---|
[414] | 2203 | |
---|
[512] | 2204 | typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier; |
---|
[449] | 2205 | EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); } |
---|
[579] | 2206 | |
---|
| 2207 | typedef EdgeNotifier ArcNotifier; |
---|
| 2208 | ArcNotifier& notifier(Arc) const { return _digraph->notifier(Edge()); } |
---|
[449] | 2209 | |
---|
[414] | 2210 | protected: |
---|
| 2211 | |
---|
[416] | 2212 | UndirectorBase() : _digraph(0) {} |
---|
[414] | 2213 | |
---|
[512] | 2214 | DGR* _digraph; |
---|
| 2215 | |
---|
| 2216 | void initialize(DGR& digraph) { |
---|
[414] | 2217 | _digraph = &digraph; |
---|
| 2218 | } |
---|
[416] | 2219 | |
---|
[414] | 2220 | }; |
---|
| 2221 | |
---|
[416] | 2222 | /// \ingroup graph_adaptors |
---|
[414] | 2223 | /// |
---|
[451] | 2224 | /// \brief Adaptor class for viewing a digraph as an undirected graph. |
---|
[414] | 2225 | /// |
---|
[451] | 2226 | /// Undirector adaptor can be used for viewing a digraph as an undirected |
---|
| 2227 | /// graph. All arcs of the underlying digraph are showed in the |
---|
| 2228 | /// adaptor as an edge (and also as a pair of arcs, of course). |
---|
| 2229 | /// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
---|
[414] | 2230 | /// |
---|
[451] | 2231 | /// The adapted digraph can also be modified through this adaptor |
---|
[453] | 2232 | /// by adding or removing nodes or edges, unless the \c GR template |
---|
[451] | 2233 | /// parameter is set to be \c const. |
---|
| 2234 | /// |
---|
[512] | 2235 | /// \tparam DGR The type of the adapted digraph. |
---|
[451] | 2236 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
| 2237 | /// It can also be specified to be \c const. |
---|
| 2238 | /// |
---|
| 2239 | /// \note The \c Node type of this adaptor and the adapted digraph are |
---|
| 2240 | /// convertible to each other, moreover the \c Edge type of the adaptor |
---|
| 2241 | /// and the \c Arc type of the adapted digraph are also convertible to |
---|
| 2242 | /// each other. |
---|
| 2243 | /// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type |
---|
| 2244 | /// of the adapted digraph.) |
---|
[512] | 2245 | template<typename DGR> |
---|
[453] | 2246 | #ifdef DOXYGEN |
---|
| 2247 | class Undirector { |
---|
| 2248 | #else |
---|
| 2249 | class Undirector : |
---|
[512] | 2250 | public GraphAdaptorExtender<UndirectorBase<DGR> > { |
---|
[453] | 2251 | #endif |
---|
[617] | 2252 | typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent; |
---|
[414] | 2253 | public: |
---|
[453] | 2254 | /// The type of the adapted digraph. |
---|
[512] | 2255 | typedef DGR Digraph; |
---|
[414] | 2256 | protected: |
---|
[416] | 2257 | Undirector() { } |
---|
[414] | 2258 | public: |
---|
| 2259 | |
---|
| 2260 | /// \brief Constructor |
---|
| 2261 | /// |
---|
[451] | 2262 | /// Creates an undirected graph from the given digraph. |
---|
[512] | 2263 | Undirector(DGR& digraph) { |
---|
| 2264 | initialize(digraph); |
---|
[414] | 2265 | } |
---|
| 2266 | |
---|
[451] | 2267 | /// \brief Arc map combined from two original arc maps |
---|
[414] | 2268 | /// |
---|
[451] | 2269 | /// This map adaptor class adapts two arc maps of the underlying |
---|
| 2270 | /// digraph to get an arc map of the undirected graph. |
---|
[559] | 2271 | /// Its value type is inherited from the first arc map type (\c FW). |
---|
| 2272 | /// \tparam FW The type of the "foward" arc map. |
---|
| 2273 | /// \tparam BK The type of the "backward" arc map. |
---|
| 2274 | template <typename FW, typename BK> |
---|
[414] | 2275 | class CombinedArcMap { |
---|
| 2276 | public: |
---|
[416] | 2277 | |
---|
[451] | 2278 | /// The key type of the map |
---|
| 2279 | typedef typename Parent::Arc Key; |
---|
| 2280 | /// The value type of the map |
---|
[559] | 2281 | typedef typename FW::Value Value; |
---|
| 2282 | |
---|
| 2283 | typedef typename MapTraits<FW>::ReferenceMapTag ReferenceMapTag; |
---|
| 2284 | |
---|
| 2285 | typedef typename MapTraits<FW>::ReturnValue ReturnValue; |
---|
| 2286 | typedef typename MapTraits<FW>::ConstReturnValue ConstReturnValue; |
---|
| 2287 | typedef typename MapTraits<FW>::ReturnValue Reference; |
---|
| 2288 | typedef typename MapTraits<FW>::ConstReturnValue ConstReference; |
---|
[449] | 2289 | |
---|
[416] | 2290 | /// Constructor |
---|
[559] | 2291 | CombinedArcMap(FW& forward, BK& backward) |
---|
[414] | 2292 | : _forward(&forward), _backward(&backward) {} |
---|
[416] | 2293 | |
---|
[451] | 2294 | /// Sets the value associated with the given key. |
---|
[416] | 2295 | void set(const Key& e, const Value& a) { |
---|
| 2296 | if (Parent::direction(e)) { |
---|
| 2297 | _forward->set(e, a); |
---|
| 2298 | } else { |
---|
| 2299 | _backward->set(e, a); |
---|
| 2300 | } |
---|
[414] | 2301 | } |
---|
| 2302 | |
---|
[451] | 2303 | /// Returns the value associated with the given key. |
---|
[449] | 2304 | ConstReturnValue operator[](const Key& e) const { |
---|
[416] | 2305 | if (Parent::direction(e)) { |
---|
| 2306 | return (*_forward)[e]; |
---|
| 2307 | } else { |
---|
| 2308 | return (*_backward)[e]; |
---|
[414] | 2309 | } |
---|
| 2310 | } |
---|
| 2311 | |
---|
[451] | 2312 | /// Returns a reference to the value associated with the given key. |
---|
[449] | 2313 | ReturnValue operator[](const Key& e) { |
---|
[416] | 2314 | if (Parent::direction(e)) { |
---|
| 2315 | return (*_forward)[e]; |
---|
| 2316 | } else { |
---|
| 2317 | return (*_backward)[e]; |
---|
[414] | 2318 | } |
---|
| 2319 | } |
---|
| 2320 | |
---|
[416] | 2321 | protected: |
---|
| 2322 | |
---|
[559] | 2323 | FW* _forward; |
---|
| 2324 | BK* _backward; |
---|
[416] | 2325 | |
---|
| 2326 | }; |
---|
| 2327 | |
---|
[451] | 2328 | /// \brief Returns a combined arc map |
---|
[416] | 2329 | /// |
---|
[451] | 2330 | /// This function just returns a combined arc map. |
---|
[559] | 2331 | template <typename FW, typename BK> |
---|
| 2332 | static CombinedArcMap<FW, BK> |
---|
| 2333 | combinedArcMap(FW& forward, BK& backward) { |
---|
| 2334 | return CombinedArcMap<FW, BK>(forward, backward); |
---|
[416] | 2335 | } |
---|
| 2336 | |
---|
[559] | 2337 | template <typename FW, typename BK> |
---|
| 2338 | static CombinedArcMap<const FW, BK> |
---|
| 2339 | combinedArcMap(const FW& forward, BK& backward) { |
---|
| 2340 | return CombinedArcMap<const FW, BK>(forward, backward); |
---|
[416] | 2341 | } |
---|
| 2342 | |
---|
[559] | 2343 | template <typename FW, typename BK> |
---|
| 2344 | static CombinedArcMap<FW, const BK> |
---|
| 2345 | combinedArcMap(FW& forward, const BK& backward) { |
---|
| 2346 | return CombinedArcMap<FW, const BK>(forward, backward); |
---|
[416] | 2347 | } |
---|
| 2348 | |
---|
[559] | 2349 | template <typename FW, typename BK> |
---|
| 2350 | static CombinedArcMap<const FW, const BK> |
---|
| 2351 | combinedArcMap(const FW& forward, const BK& backward) { |
---|
| 2352 | return CombinedArcMap<const FW, const BK>(forward, backward); |
---|
[416] | 2353 | } |
---|
| 2354 | |
---|
| 2355 | }; |
---|
| 2356 | |
---|
[451] | 2357 | /// \brief Returns a read-only Undirector adaptor |
---|
[416] | 2358 | /// |
---|
[451] | 2359 | /// This function just returns a read-only \ref Undirector adaptor. |
---|
| 2360 | /// \ingroup graph_adaptors |
---|
| 2361 | /// \relates Undirector |
---|
[512] | 2362 | template<typename DGR> |
---|
| 2363 | Undirector<const DGR> undirector(const DGR& digraph) { |
---|
| 2364 | return Undirector<const DGR>(digraph); |
---|
[416] | 2365 | } |
---|
| 2366 | |
---|
[451] | 2367 | |
---|
[512] | 2368 | template <typename GR, typename DM> |
---|
[416] | 2369 | class OrienterBase { |
---|
| 2370 | public: |
---|
| 2371 | |
---|
[512] | 2372 | typedef GR Graph; |
---|
| 2373 | typedef DM DirectionMap; |
---|
| 2374 | |
---|
| 2375 | typedef typename GR::Node Node; |
---|
| 2376 | typedef typename GR::Edge Arc; |
---|
[416] | 2377 | |
---|
| 2378 | void reverseArc(const Arc& arc) { |
---|
| 2379 | _direction->set(arc, !(*_direction)[arc]); |
---|
| 2380 | } |
---|
| 2381 | |
---|
| 2382 | void first(Node& i) const { _graph->first(i); } |
---|
| 2383 | void first(Arc& i) const { _graph->first(i); } |
---|
| 2384 | void firstIn(Arc& i, const Node& n) const { |
---|
[447] | 2385 | bool d = true; |
---|
[416] | 2386 | _graph->firstInc(i, d, n); |
---|
| 2387 | while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
---|
| 2388 | } |
---|
| 2389 | void firstOut(Arc& i, const Node& n ) const { |
---|
[447] | 2390 | bool d = true; |
---|
[416] | 2391 | _graph->firstInc(i, d, n); |
---|
| 2392 | while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
---|
| 2393 | } |
---|
| 2394 | |
---|
| 2395 | void next(Node& i) const { _graph->next(i); } |
---|
| 2396 | void next(Arc& i) const { _graph->next(i); } |
---|
| 2397 | void nextIn(Arc& i) const { |
---|
| 2398 | bool d = !(*_direction)[i]; |
---|
| 2399 | _graph->nextInc(i, d); |
---|
| 2400 | while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
---|
| 2401 | } |
---|
| 2402 | void nextOut(Arc& i) const { |
---|
| 2403 | bool d = (*_direction)[i]; |
---|
| 2404 | _graph->nextInc(i, d); |
---|
| 2405 | while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
---|
| 2406 | } |
---|
| 2407 | |
---|
| 2408 | Node source(const Arc& e) const { |
---|
| 2409 | return (*_direction)[e] ? _graph->u(e) : _graph->v(e); |
---|
| 2410 | } |
---|
| 2411 | Node target(const Arc& e) const { |
---|
| 2412 | return (*_direction)[e] ? _graph->v(e) : _graph->u(e); |
---|
| 2413 | } |
---|
| 2414 | |
---|
| 2415 | typedef NodeNumTagIndicator<Graph> NodeNumTag; |
---|
| 2416 | int nodeNum() const { return _graph->nodeNum(); } |
---|
| 2417 | |
---|
[446] | 2418 | typedef EdgeNumTagIndicator<Graph> ArcNumTag; |
---|
[416] | 2419 | int arcNum() const { return _graph->edgeNum(); } |
---|
| 2420 | |
---|
[446] | 2421 | typedef FindEdgeTagIndicator<Graph> FindArcTag; |
---|
[416] | 2422 | Arc findArc(const Node& u, const Node& v, |
---|
[448] | 2423 | const Arc& prev = INVALID) const { |
---|
[449] | 2424 | Arc arc = _graph->findEdge(u, v, prev); |
---|
| 2425 | while (arc != INVALID && source(arc) != u) { |
---|
[416] | 2426 | arc = _graph->findEdge(u, v, arc); |
---|
[414] | 2427 | } |
---|
[416] | 2428 | return arc; |
---|
| 2429 | } |
---|
| 2430 | |
---|
| 2431 | Node addNode() { |
---|
| 2432 | return Node(_graph->addNode()); |
---|
| 2433 | } |
---|
| 2434 | |
---|
| 2435 | Arc addArc(const Node& u, const Node& v) { |
---|
[449] | 2436 | Arc arc = _graph->addEdge(u, v); |
---|
| 2437 | _direction->set(arc, _graph->u(arc) == u); |
---|
[416] | 2438 | return arc; |
---|
| 2439 | } |
---|
| 2440 | |
---|
| 2441 | void erase(const Node& i) { _graph->erase(i); } |
---|
| 2442 | void erase(const Arc& i) { _graph->erase(i); } |
---|
| 2443 | |
---|
| 2444 | void clear() { _graph->clear(); } |
---|
| 2445 | |
---|
| 2446 | int id(const Node& v) const { return _graph->id(v); } |
---|
| 2447 | int id(const Arc& e) const { return _graph->id(e); } |
---|
| 2448 | |
---|
| 2449 | Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } |
---|
| 2450 | Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } |
---|
| 2451 | |
---|
| 2452 | int maxNodeId() const { return _graph->maxNodeId(); } |
---|
| 2453 | int maxArcId() const { return _graph->maxEdgeId(); } |
---|
| 2454 | |
---|
[512] | 2455 | typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
---|
[416] | 2456 | NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
---|
| 2457 | |
---|
[512] | 2458 | typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
---|
[416] | 2459 | ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
---|
| 2460 | |
---|
[512] | 2461 | template <typename V> |
---|
| 2462 | class NodeMap : public GR::template NodeMap<V> { |
---|
[617] | 2463 | typedef typename GR::template NodeMap<V> Parent; |
---|
| 2464 | |
---|
[416] | 2465 | public: |
---|
| 2466 | |
---|
[512] | 2467 | explicit NodeMap(const OrienterBase<GR, DM>& adapter) |
---|
[416] | 2468 | : Parent(*adapter._graph) {} |
---|
| 2469 | |
---|
[512] | 2470 | NodeMap(const OrienterBase<GR, DM>& adapter, const V& value) |
---|
[416] | 2471 | : Parent(*adapter._graph, value) {} |
---|
| 2472 | |
---|
| 2473 | private: |
---|
| 2474 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 2475 | return operator=<NodeMap>(cmap); |
---|
| 2476 | } |
---|
| 2477 | |
---|
| 2478 | template <typename CMap> |
---|
| 2479 | NodeMap& operator=(const CMap& cmap) { |
---|
| 2480 | Parent::operator=(cmap); |
---|
| 2481 | return *this; |
---|
| 2482 | } |
---|
[414] | 2483 | |
---|
| 2484 | }; |
---|
| 2485 | |
---|
[512] | 2486 | template <typename V> |
---|
| 2487 | class ArcMap : public GR::template EdgeMap<V> { |
---|
[617] | 2488 | typedef typename Graph::template EdgeMap<V> Parent; |
---|
| 2489 | |
---|
[416] | 2490 | public: |
---|
| 2491 | |
---|
[512] | 2492 | explicit ArcMap(const OrienterBase<GR, DM>& adapter) |
---|
[416] | 2493 | : Parent(*adapter._graph) { } |
---|
| 2494 | |
---|
[512] | 2495 | ArcMap(const OrienterBase<GR, DM>& adapter, const V& value) |
---|
[416] | 2496 | : Parent(*adapter._graph, value) { } |
---|
| 2497 | |
---|
| 2498 | private: |
---|
| 2499 | ArcMap& operator=(const ArcMap& cmap) { |
---|
| 2500 | return operator=<ArcMap>(cmap); |
---|
| 2501 | } |
---|
| 2502 | |
---|
| 2503 | template <typename CMap> |
---|
| 2504 | ArcMap& operator=(const CMap& cmap) { |
---|
| 2505 | Parent::operator=(cmap); |
---|
| 2506 | return *this; |
---|
| 2507 | } |
---|
| 2508 | }; |
---|
| 2509 | |
---|
| 2510 | |
---|
| 2511 | |
---|
| 2512 | protected: |
---|
| 2513 | Graph* _graph; |
---|
[512] | 2514 | DM* _direction; |
---|
| 2515 | |
---|
| 2516 | void initialize(GR& graph, DM& direction) { |
---|
| 2517 | _graph = &graph; |
---|
[416] | 2518 | _direction = &direction; |
---|
| 2519 | } |
---|
| 2520 | |
---|
[414] | 2521 | }; |
---|
| 2522 | |
---|
[416] | 2523 | /// \ingroup graph_adaptors |
---|
[414] | 2524 | /// |
---|
[451] | 2525 | /// \brief Adaptor class for orienting the edges of a graph to get a digraph |
---|
[416] | 2526 | /// |
---|
[451] | 2527 | /// Orienter adaptor can be used for orienting the edges of a graph to |
---|
| 2528 | /// get a digraph. A \c bool edge map of the underlying graph must be |
---|
| 2529 | /// specified, which define the direction of the arcs in the adaptor. |
---|
| 2530 | /// The arcs can be easily reversed by the \c reverseArc() member function |
---|
| 2531 | /// of the adaptor. |
---|
| 2532 | /// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
[416] | 2533 | /// |
---|
[451] | 2534 | /// The adapted graph can also be modified through this adaptor |
---|
[453] | 2535 | /// by adding or removing nodes or arcs, unless the \c GR template |
---|
[451] | 2536 | /// parameter is set to be \c const. |
---|
[416] | 2537 | /// |
---|
[453] | 2538 | /// \tparam GR The type of the adapted graph. |
---|
[451] | 2539 | /// It must conform to the \ref concepts::Graph "Graph" concept. |
---|
| 2540 | /// It can also be specified to be \c const. |
---|
[453] | 2541 | /// \tparam DM The type of the direction map. |
---|
| 2542 | /// It must be a \c bool (or convertible) edge map of the |
---|
| 2543 | /// adapted graph. The default type is |
---|
| 2544 | /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
---|
[451] | 2545 | /// |
---|
| 2546 | /// \note The \c Node type of this adaptor and the adapted graph are |
---|
| 2547 | /// convertible to each other, moreover the \c Arc type of the adaptor |
---|
| 2548 | /// and the \c Edge type of the adapted graph are also convertible to |
---|
| 2549 | /// each other. |
---|
| 2550 | #ifdef DOXYGEN |
---|
[453] | 2551 | template<typename GR, |
---|
| 2552 | typename DM> |
---|
| 2553 | class Orienter { |
---|
[451] | 2554 | #else |
---|
[453] | 2555 | template<typename GR, |
---|
| 2556 | typename DM = typename GR::template EdgeMap<bool> > |
---|
| 2557 | class Orienter : |
---|
| 2558 | public DigraphAdaptorExtender<OrienterBase<GR, DM> > { |
---|
[451] | 2559 | #endif |
---|
[617] | 2560 | typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent; |
---|
[416] | 2561 | public: |
---|
[451] | 2562 | |
---|
| 2563 | /// The type of the adapted graph. |
---|
[453] | 2564 | typedef GR Graph; |
---|
[451] | 2565 | /// The type of the direction edge map. |
---|
[453] | 2566 | typedef DM DirectionMap; |
---|
| 2567 | |
---|
[416] | 2568 | typedef typename Parent::Arc Arc; |
---|
[617] | 2569 | |
---|
[416] | 2570 | protected: |
---|
| 2571 | Orienter() { } |
---|
[617] | 2572 | |
---|
[416] | 2573 | public: |
---|
| 2574 | |
---|
[451] | 2575 | /// \brief Constructor |
---|
[416] | 2576 | /// |
---|
[451] | 2577 | /// Constructor of the adaptor. |
---|
[512] | 2578 | Orienter(GR& graph, DM& direction) { |
---|
| 2579 | Parent::initialize(graph, direction); |
---|
[416] | 2580 | } |
---|
| 2581 | |
---|
[451] | 2582 | /// \brief Reverses the given arc |
---|
[416] | 2583 | /// |
---|
[451] | 2584 | /// This function reverses the given arc. |
---|
| 2585 | /// It is done by simply negate the assigned value of \c a |
---|
| 2586 | /// in the direction map. |
---|
[416] | 2587 | void reverseArc(const Arc& a) { |
---|
| 2588 | Parent::reverseArc(a); |
---|
| 2589 | } |
---|
| 2590 | }; |
---|
| 2591 | |
---|
[451] | 2592 | /// \brief Returns a read-only Orienter adaptor |
---|
[416] | 2593 | /// |
---|
[451] | 2594 | /// This function just returns a read-only \ref Orienter adaptor. |
---|
| 2595 | /// \ingroup graph_adaptors |
---|
| 2596 | /// \relates Orienter |
---|
[453] | 2597 | template<typename GR, typename DM> |
---|
| 2598 | Orienter<const GR, DM> |
---|
[512] | 2599 | orienter(const GR& graph, DM& direction) { |
---|
| 2600 | return Orienter<const GR, DM>(graph, direction); |
---|
[414] | 2601 | } |
---|
| 2602 | |
---|
[453] | 2603 | template<typename GR, typename DM> |
---|
| 2604 | Orienter<const GR, const DM> |
---|
[512] | 2605 | orienter(const GR& graph, const DM& direction) { |
---|
| 2606 | return Orienter<const GR, const DM>(graph, direction); |
---|
[416] | 2607 | } |
---|
| 2608 | |
---|
| 2609 | namespace _adaptor_bits { |
---|
| 2610 | |
---|
[512] | 2611 | template <typename DGR, typename CM, typename FM, typename TL> |
---|
[416] | 2612 | class ResForwardFilter { |
---|
| 2613 | public: |
---|
| 2614 | |
---|
[512] | 2615 | typedef typename DGR::Arc Key; |
---|
[416] | 2616 | typedef bool Value; |
---|
| 2617 | |
---|
| 2618 | private: |
---|
| 2619 | |
---|
[512] | 2620 | const CM* _capacity; |
---|
| 2621 | const FM* _flow; |
---|
| 2622 | TL _tolerance; |
---|
| 2623 | |
---|
[416] | 2624 | public: |
---|
| 2625 | |
---|
[512] | 2626 | ResForwardFilter(const CM& capacity, const FM& flow, |
---|
| 2627 | const TL& tolerance = TL()) |
---|
[416] | 2628 | : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
---|
| 2629 | |
---|
[512] | 2630 | bool operator[](const typename DGR::Arc& a) const { |
---|
[416] | 2631 | return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); |
---|
| 2632 | } |
---|
| 2633 | }; |
---|
| 2634 | |
---|
[512] | 2635 | template<typename DGR,typename CM, typename FM, typename TL> |
---|
[416] | 2636 | class ResBackwardFilter { |
---|
| 2637 | public: |
---|
| 2638 | |
---|
[512] | 2639 | typedef typename DGR::Arc Key; |
---|
[416] | 2640 | typedef bool Value; |
---|
| 2641 | |
---|
| 2642 | private: |
---|
| 2643 | |
---|
[512] | 2644 | const CM* _capacity; |
---|
| 2645 | const FM* _flow; |
---|
| 2646 | TL _tolerance; |
---|
[416] | 2647 | |
---|
| 2648 | public: |
---|
| 2649 | |
---|
[512] | 2650 | ResBackwardFilter(const CM& capacity, const FM& flow, |
---|
| 2651 | const TL& tolerance = TL()) |
---|
[416] | 2652 | : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
---|
| 2653 | |
---|
[512] | 2654 | bool operator[](const typename DGR::Arc& a) const { |
---|
[416] | 2655 | return _tolerance.positive((*_flow)[a]); |
---|
| 2656 | } |
---|
| 2657 | }; |
---|
| 2658 | |
---|
| 2659 | } |
---|
| 2660 | |
---|
| 2661 | /// \ingroup graph_adaptors |
---|
| 2662 | /// |
---|
[451] | 2663 | /// \brief Adaptor class for composing the residual digraph for directed |
---|
[416] | 2664 | /// flow and circulation problems. |
---|
| 2665 | /// |
---|
[464] | 2666 | /// ResidualDigraph can be used for composing the \e residual digraph |
---|
| 2667 | /// for directed flow and circulation problems. Let \f$ G=(V, A) \f$ |
---|
| 2668 | /// be a directed graph and let \f$ F \f$ be a number type. |
---|
| 2669 | /// Let \f$ flow, cap: A\to F \f$ be functions on the arcs. |
---|
[451] | 2670 | /// This adaptor implements a digraph structure with node set \f$ V \f$ |
---|
| 2671 | /// and arc set \f$ A_{forward}\cup A_{backward} \f$, |
---|
| 2672 | /// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and |
---|
| 2673 | /// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so |
---|
| 2674 | /// called residual digraph. |
---|
| 2675 | /// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken, |
---|
| 2676 | /// multiplicities are counted, i.e. the adaptor has exactly |
---|
| 2677 | /// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel |
---|
| 2678 | /// arcs). |
---|
| 2679 | /// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
---|
[416] | 2680 | /// |
---|
[512] | 2681 | /// \tparam DGR The type of the adapted digraph. |
---|
[451] | 2682 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
| 2683 | /// It is implicitly \c const. |
---|
[453] | 2684 | /// \tparam CM The type of the capacity map. |
---|
| 2685 | /// It must be an arc map of some numerical type, which defines |
---|
[451] | 2686 | /// the capacities in the flow problem. It is implicitly \c const. |
---|
[453] | 2687 | /// The default type is |
---|
| 2688 | /// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
---|
| 2689 | /// \tparam FM The type of the flow map. |
---|
| 2690 | /// It must be an arc map of some numerical type, which defines |
---|
| 2691 | /// the flow values in the flow problem. The default type is \c CM. |
---|
| 2692 | /// \tparam TL The tolerance type for handling inexact computation. |
---|
[451] | 2693 | /// The default tolerance type depends on the value type of the |
---|
| 2694 | /// capacity map. |
---|
[416] | 2695 | /// |
---|
[451] | 2696 | /// \note This adaptor is implemented using Undirector and FilterArcs |
---|
| 2697 | /// adaptors. |
---|
| 2698 | /// |
---|
| 2699 | /// \note The \c Node type of this adaptor and the adapted digraph are |
---|
| 2700 | /// convertible to each other, moreover the \c Arc type of the adaptor |
---|
| 2701 | /// is convertible to the \c Arc type of the adapted digraph. |
---|
| 2702 | #ifdef DOXYGEN |
---|
[512] | 2703 | template<typename DGR, typename CM, typename FM, typename TL> |
---|
[464] | 2704 | class ResidualDigraph |
---|
[451] | 2705 | #else |
---|
[512] | 2706 | template<typename DGR, |
---|
| 2707 | typename CM = typename DGR::template ArcMap<int>, |
---|
[453] | 2708 | typename FM = CM, |
---|
| 2709 | typename TL = Tolerance<typename CM::Value> > |
---|
[512] | 2710 | class ResidualDigraph |
---|
| 2711 | : public SubDigraph< |
---|
| 2712 | Undirector<const DGR>, |
---|
| 2713 | ConstMap<typename DGR::Node, Const<bool, true> >, |
---|
| 2714 | typename Undirector<const DGR>::template CombinedArcMap< |
---|
| 2715 | _adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>, |
---|
| 2716 | _adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > > |
---|
[451] | 2717 | #endif |
---|
[416] | 2718 | { |
---|
[414] | 2719 | public: |
---|
| 2720 | |
---|
[451] | 2721 | /// The type of the underlying digraph. |
---|
[512] | 2722 | typedef DGR Digraph; |
---|
[451] | 2723 | /// The type of the capacity map. |
---|
[453] | 2724 | typedef CM CapacityMap; |
---|
[451] | 2725 | /// The type of the flow map. |
---|
[453] | 2726 | typedef FM FlowMap; |
---|
| 2727 | /// The tolerance type. |
---|
| 2728 | typedef TL Tolerance; |
---|
[414] | 2729 | |
---|
| 2730 | typedef typename CapacityMap::Value Value; |
---|
[464] | 2731 | typedef ResidualDigraph Adaptor; |
---|
[414] | 2732 | |
---|
| 2733 | protected: |
---|
| 2734 | |
---|
[416] | 2735 | typedef Undirector<const Digraph> Undirected; |
---|
| 2736 | |
---|
[512] | 2737 | typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter; |
---|
| 2738 | |
---|
| 2739 | typedef _adaptor_bits::ResForwardFilter<const DGR, CM, |
---|
| 2740 | FM, TL> ForwardFilter; |
---|
| 2741 | |
---|
| 2742 | typedef _adaptor_bits::ResBackwardFilter<const DGR, CM, |
---|
| 2743 | FM, TL> BackwardFilter; |
---|
[416] | 2744 | |
---|
| 2745 | typedef typename Undirected:: |
---|
[453] | 2746 | template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
---|
[414] | 2747 | |
---|
[512] | 2748 | typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent; |
---|
[414] | 2749 | |
---|
| 2750 | const CapacityMap* _capacity; |
---|
| 2751 | FlowMap* _flow; |
---|
| 2752 | |
---|
[416] | 2753 | Undirected _graph; |
---|
[512] | 2754 | NodeFilter _node_filter; |
---|
[414] | 2755 | ForwardFilter _forward_filter; |
---|
| 2756 | BackwardFilter _backward_filter; |
---|
| 2757 | ArcFilter _arc_filter; |
---|
| 2758 | |
---|
| 2759 | public: |
---|
| 2760 | |
---|
[451] | 2761 | /// \brief Constructor |
---|
[414] | 2762 | /// |
---|
[451] | 2763 | /// Constructor of the residual digraph adaptor. The parameters are the |
---|
| 2764 | /// digraph, the capacity map, the flow map, and a tolerance object. |
---|
[512] | 2765 | ResidualDigraph(const DGR& digraph, const CM& capacity, |
---|
| 2766 | FM& flow, const TL& tolerance = Tolerance()) |
---|
| 2767 | : Parent(), _capacity(&capacity), _flow(&flow), |
---|
| 2768 | _graph(digraph), _node_filter(), |
---|
[416] | 2769 | _forward_filter(capacity, flow, tolerance), |
---|
[414] | 2770 | _backward_filter(capacity, flow, tolerance), |
---|
| 2771 | _arc_filter(_forward_filter, _backward_filter) |
---|
| 2772 | { |
---|
[512] | 2773 | Parent::initialize(_graph, _node_filter, _arc_filter); |
---|
[414] | 2774 | } |
---|
| 2775 | |
---|
| 2776 | typedef typename Parent::Arc Arc; |
---|
| 2777 | |
---|
[451] | 2778 | /// \brief Returns the residual capacity of the given arc. |
---|
[414] | 2779 | /// |
---|
[451] | 2780 | /// Returns the residual capacity of the given arc. |
---|
[416] | 2781 | Value residualCapacity(const Arc& a) const { |
---|
| 2782 | if (Undirected::direction(a)) { |
---|
| 2783 | return (*_capacity)[a] - (*_flow)[a]; |
---|
[414] | 2784 | } else { |
---|
[416] | 2785 | return (*_flow)[a]; |
---|
[414] | 2786 | } |
---|
[416] | 2787 | } |
---|
| 2788 | |
---|
[452] | 2789 | /// \brief Augments on the given arc in the residual digraph. |
---|
[414] | 2790 | /// |
---|
[452] | 2791 | /// Augments on the given arc in the residual digraph. It increases |
---|
[451] | 2792 | /// or decreases the flow value on the original arc according to the |
---|
| 2793 | /// direction of the residual arc. |
---|
[416] | 2794 | void augment(const Arc& a, const Value& v) const { |
---|
| 2795 | if (Undirected::direction(a)) { |
---|
| 2796 | _flow->set(a, (*_flow)[a] + v); |
---|
| 2797 | } else { |
---|
| 2798 | _flow->set(a, (*_flow)[a] - v); |
---|
[414] | 2799 | } |
---|
| 2800 | } |
---|
| 2801 | |
---|
[451] | 2802 | /// \brief Returns \c true if the given residual arc is a forward arc. |
---|
[414] | 2803 | /// |
---|
[451] | 2804 | /// Returns \c true if the given residual arc has the same orientation |
---|
| 2805 | /// as the original arc, i.e. it is a so called forward arc. |
---|
[416] | 2806 | static bool forward(const Arc& a) { |
---|
| 2807 | return Undirected::direction(a); |
---|
[414] | 2808 | } |
---|
| 2809 | |
---|
[451] | 2810 | /// \brief Returns \c true if the given residual arc is a backward arc. |
---|
[414] | 2811 | /// |
---|
[451] | 2812 | /// Returns \c true if the given residual arc has the opposite orientation |
---|
| 2813 | /// than the original arc, i.e. it is a so called backward arc. |
---|
[416] | 2814 | static bool backward(const Arc& a) { |
---|
| 2815 | return !Undirected::direction(a); |
---|
[414] | 2816 | } |
---|
| 2817 | |
---|
[451] | 2818 | /// \brief Returns the forward oriented residual arc. |
---|
[414] | 2819 | /// |
---|
[451] | 2820 | /// Returns the forward oriented residual arc related to the given |
---|
| 2821 | /// arc of the underlying digraph. |
---|
[416] | 2822 | static Arc forward(const typename Digraph::Arc& a) { |
---|
| 2823 | return Undirected::direct(a, true); |
---|
[414] | 2824 | } |
---|
| 2825 | |
---|
[451] | 2826 | /// \brief Returns the backward oriented residual arc. |
---|
[414] | 2827 | /// |
---|
[451] | 2828 | /// Returns the backward oriented residual arc related to the given |
---|
| 2829 | /// arc of the underlying digraph. |
---|
[416] | 2830 | static Arc backward(const typename Digraph::Arc& a) { |
---|
| 2831 | return Undirected::direct(a, false); |
---|
[414] | 2832 | } |
---|
| 2833 | |
---|
| 2834 | /// \brief Residual capacity map. |
---|
| 2835 | /// |
---|
[451] | 2836 | /// This map adaptor class can be used for obtaining the residual |
---|
| 2837 | /// capacities as an arc map of the residual digraph. |
---|
| 2838 | /// Its value type is inherited from the capacity map. |
---|
[416] | 2839 | class ResidualCapacity { |
---|
[414] | 2840 | protected: |
---|
| 2841 | const Adaptor* _adaptor; |
---|
| 2842 | public: |
---|
[451] | 2843 | /// The key type of the map |
---|
[414] | 2844 | typedef Arc Key; |
---|
[451] | 2845 | /// The value type of the map |
---|
[453] | 2846 | typedef typename CapacityMap::Value Value; |
---|
[414] | 2847 | |
---|
[416] | 2848 | /// Constructor |
---|
[512] | 2849 | ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) |
---|
| 2850 | : _adaptor(&adaptor) {} |
---|
[416] | 2851 | |
---|
[451] | 2852 | /// Returns the value associated with the given residual arc |
---|
[416] | 2853 | Value operator[](const Arc& a) const { |
---|
| 2854 | return _adaptor->residualCapacity(a); |
---|
[414] | 2855 | } |
---|
[416] | 2856 | |
---|
[414] | 2857 | }; |
---|
| 2858 | |
---|
[450] | 2859 | /// \brief Returns a residual capacity map |
---|
| 2860 | /// |
---|
| 2861 | /// This function just returns a residual capacity map. |
---|
| 2862 | ResidualCapacity residualCapacity() const { |
---|
| 2863 | return ResidualCapacity(*this); |
---|
| 2864 | } |
---|
| 2865 | |
---|
[414] | 2866 | }; |
---|
| 2867 | |
---|
[450] | 2868 | /// \brief Returns a (read-only) Residual adaptor |
---|
| 2869 | /// |
---|
[512] | 2870 | /// This function just returns a (read-only) \ref ResidualDigraph adaptor. |
---|
[450] | 2871 | /// \ingroup graph_adaptors |
---|
[512] | 2872 | /// \relates ResidualDigraph |
---|
| 2873 | template<typename DGR, typename CM, typename FM> |
---|
| 2874 | ResidualDigraph<DGR, CM, FM> |
---|
| 2875 | residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) { |
---|
| 2876 | return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map); |
---|
[450] | 2877 | } |
---|
| 2878 | |
---|
| 2879 | |
---|
[512] | 2880 | template <typename DGR> |
---|
[416] | 2881 | class SplitNodesBase { |
---|
[617] | 2882 | typedef DigraphAdaptorBase<const DGR> Parent; |
---|
| 2883 | |
---|
[414] | 2884 | public: |
---|
| 2885 | |
---|
[512] | 2886 | typedef DGR Digraph; |
---|
[416] | 2887 | typedef SplitNodesBase Adaptor; |
---|
[414] | 2888 | |
---|
[512] | 2889 | typedef typename DGR::Node DigraphNode; |
---|
| 2890 | typedef typename DGR::Arc DigraphArc; |
---|
[414] | 2891 | |
---|
| 2892 | class Node; |
---|
| 2893 | class Arc; |
---|
| 2894 | |
---|
| 2895 | private: |
---|
| 2896 | |
---|
| 2897 | template <typename T> class NodeMapBase; |
---|
| 2898 | template <typename T> class ArcMapBase; |
---|
| 2899 | |
---|
| 2900 | public: |
---|
[416] | 2901 | |
---|
[414] | 2902 | class Node : public DigraphNode { |
---|
[416] | 2903 | friend class SplitNodesBase; |
---|
[414] | 2904 | template <typename T> friend class NodeMapBase; |
---|
| 2905 | private: |
---|
| 2906 | |
---|
| 2907 | bool _in; |
---|
| 2908 | Node(DigraphNode node, bool in) |
---|
[416] | 2909 | : DigraphNode(node), _in(in) {} |
---|
| 2910 | |
---|
[414] | 2911 | public: |
---|
| 2912 | |
---|
| 2913 | Node() {} |
---|
| 2914 | Node(Invalid) : DigraphNode(INVALID), _in(true) {} |
---|
| 2915 | |
---|
| 2916 | bool operator==(const Node& node) const { |
---|
[416] | 2917 | return DigraphNode::operator==(node) && _in == node._in; |
---|
[414] | 2918 | } |
---|
[416] | 2919 | |
---|
[414] | 2920 | bool operator!=(const Node& node) const { |
---|
[416] | 2921 | return !(*this == node); |
---|
[414] | 2922 | } |
---|
[416] | 2923 | |
---|
[414] | 2924 | bool operator<(const Node& node) const { |
---|
[416] | 2925 | return DigraphNode::operator<(node) || |
---|
| 2926 | (DigraphNode::operator==(node) && _in < node._in); |
---|
[414] | 2927 | } |
---|
| 2928 | }; |
---|
| 2929 | |
---|
| 2930 | class Arc { |
---|
[416] | 2931 | friend class SplitNodesBase; |
---|
[414] | 2932 | template <typename T> friend class ArcMapBase; |
---|
| 2933 | private: |
---|
| 2934 | typedef BiVariant<DigraphArc, DigraphNode> ArcImpl; |
---|
| 2935 | |
---|
| 2936 | explicit Arc(const DigraphArc& arc) : _item(arc) {} |
---|
| 2937 | explicit Arc(const DigraphNode& node) : _item(node) {} |
---|
[416] | 2938 | |
---|
[414] | 2939 | ArcImpl _item; |
---|
| 2940 | |
---|
| 2941 | public: |
---|
| 2942 | Arc() {} |
---|
| 2943 | Arc(Invalid) : _item(DigraphArc(INVALID)) {} |
---|
| 2944 | |
---|
| 2945 | bool operator==(const Arc& arc) const { |
---|
| 2946 | if (_item.firstState()) { |
---|
| 2947 | if (arc._item.firstState()) { |
---|
| 2948 | return _item.first() == arc._item.first(); |
---|
| 2949 | } |
---|
| 2950 | } else { |
---|
| 2951 | if (arc._item.secondState()) { |
---|
| 2952 | return _item.second() == arc._item.second(); |
---|
| 2953 | } |
---|
| 2954 | } |
---|
| 2955 | return false; |
---|
| 2956 | } |
---|
[416] | 2957 | |
---|
[414] | 2958 | bool operator!=(const Arc& arc) const { |
---|
[416] | 2959 | return !(*this == arc); |
---|
[414] | 2960 | } |
---|
[416] | 2961 | |
---|
[414] | 2962 | bool operator<(const Arc& arc) const { |
---|
| 2963 | if (_item.firstState()) { |
---|
| 2964 | if (arc._item.firstState()) { |
---|
| 2965 | return _item.first() < arc._item.first(); |
---|
| 2966 | } |
---|
| 2967 | return false; |
---|
| 2968 | } else { |
---|
| 2969 | if (arc._item.secondState()) { |
---|
| 2970 | return _item.second() < arc._item.second(); |
---|
| 2971 | } |
---|
| 2972 | return true; |
---|
| 2973 | } |
---|
| 2974 | } |
---|
| 2975 | |
---|
| 2976 | operator DigraphArc() const { return _item.first(); } |
---|
| 2977 | operator DigraphNode() const { return _item.second(); } |
---|
| 2978 | |
---|
| 2979 | }; |
---|
| 2980 | |
---|
| 2981 | void first(Node& n) const { |
---|
| 2982 | _digraph->first(n); |
---|
| 2983 | n._in = true; |
---|
| 2984 | } |
---|
| 2985 | |
---|
| 2986 | void next(Node& n) const { |
---|
| 2987 | if (n._in) { |
---|
[416] | 2988 | n._in = false; |
---|
[414] | 2989 | } else { |
---|
[416] | 2990 | n._in = true; |
---|
| 2991 | _digraph->next(n); |
---|
[414] | 2992 | } |
---|
| 2993 | } |
---|
| 2994 | |
---|
| 2995 | void first(Arc& e) const { |
---|
| 2996 | e._item.setSecond(); |
---|
| 2997 | _digraph->first(e._item.second()); |
---|
| 2998 | if (e._item.second() == INVALID) { |
---|
| 2999 | e._item.setFirst(); |
---|
[416] | 3000 | _digraph->first(e._item.first()); |
---|
[414] | 3001 | } |
---|
| 3002 | } |
---|
| 3003 | |
---|
| 3004 | void next(Arc& e) const { |
---|
| 3005 | if (e._item.secondState()) { |
---|
[416] | 3006 | _digraph->next(e._item.second()); |
---|
[414] | 3007 | if (e._item.second() == INVALID) { |
---|
| 3008 | e._item.setFirst(); |
---|
| 3009 | _digraph->first(e._item.first()); |
---|
| 3010 | } |
---|
| 3011 | } else { |
---|
[416] | 3012 | _digraph->next(e._item.first()); |
---|
| 3013 | } |
---|
[414] | 3014 | } |
---|
| 3015 | |
---|
| 3016 | void firstOut(Arc& e, const Node& n) const { |
---|
| 3017 | if (n._in) { |
---|
| 3018 | e._item.setSecond(n); |
---|
| 3019 | } else { |
---|
| 3020 | e._item.setFirst(); |
---|
[416] | 3021 | _digraph->firstOut(e._item.first(), n); |
---|
[414] | 3022 | } |
---|
| 3023 | } |
---|
| 3024 | |
---|
| 3025 | void nextOut(Arc& e) const { |
---|
| 3026 | if (!e._item.firstState()) { |
---|
[416] | 3027 | e._item.setFirst(INVALID); |
---|
[414] | 3028 | } else { |
---|
[416] | 3029 | _digraph->nextOut(e._item.first()); |
---|
| 3030 | } |
---|
[414] | 3031 | } |
---|
| 3032 | |
---|
| 3033 | void firstIn(Arc& e, const Node& n) const { |
---|
| 3034 | if (!n._in) { |
---|
[416] | 3035 | e._item.setSecond(n); |
---|
[414] | 3036 | } else { |
---|
| 3037 | e._item.setFirst(); |
---|
[416] | 3038 | _digraph->firstIn(e._item.first(), n); |
---|
[414] | 3039 | } |
---|
| 3040 | } |
---|
| 3041 | |
---|
| 3042 | void nextIn(Arc& e) const { |
---|
| 3043 | if (!e._item.firstState()) { |
---|
[416] | 3044 | e._item.setFirst(INVALID); |
---|
[414] | 3045 | } else { |
---|
[416] | 3046 | _digraph->nextIn(e._item.first()); |
---|
[414] | 3047 | } |
---|
| 3048 | } |
---|
| 3049 | |
---|
| 3050 | Node source(const Arc& e) const { |
---|
| 3051 | if (e._item.firstState()) { |
---|
[416] | 3052 | return Node(_digraph->source(e._item.first()), false); |
---|
[414] | 3053 | } else { |
---|
[416] | 3054 | return Node(e._item.second(), true); |
---|
[414] | 3055 | } |
---|
| 3056 | } |
---|
| 3057 | |
---|
| 3058 | Node target(const Arc& e) const { |
---|
| 3059 | if (e._item.firstState()) { |
---|
[416] | 3060 | return Node(_digraph->target(e._item.first()), true); |
---|
[414] | 3061 | } else { |
---|
[416] | 3062 | return Node(e._item.second(), false); |
---|
[414] | 3063 | } |
---|
| 3064 | } |
---|
| 3065 | |
---|
| 3066 | int id(const Node& n) const { |
---|
| 3067 | return (_digraph->id(n) << 1) | (n._in ? 0 : 1); |
---|
| 3068 | } |
---|
| 3069 | Node nodeFromId(int ix) const { |
---|
| 3070 | return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0); |
---|
| 3071 | } |
---|
| 3072 | int maxNodeId() const { |
---|
| 3073 | return 2 * _digraph->maxNodeId() + 1; |
---|
| 3074 | } |
---|
| 3075 | |
---|
| 3076 | int id(const Arc& e) const { |
---|
| 3077 | if (e._item.firstState()) { |
---|
| 3078 | return _digraph->id(e._item.first()) << 1; |
---|
| 3079 | } else { |
---|
| 3080 | return (_digraph->id(e._item.second()) << 1) | 1; |
---|
| 3081 | } |
---|
| 3082 | } |
---|
| 3083 | Arc arcFromId(int ix) const { |
---|
| 3084 | if ((ix & 1) == 0) { |
---|
| 3085 | return Arc(_digraph->arcFromId(ix >> 1)); |
---|
| 3086 | } else { |
---|
| 3087 | return Arc(_digraph->nodeFromId(ix >> 1)); |
---|
| 3088 | } |
---|
| 3089 | } |
---|
| 3090 | int maxArcId() const { |
---|
[416] | 3091 | return std::max(_digraph->maxNodeId() << 1, |
---|
[414] | 3092 | (_digraph->maxArcId() << 1) | 1); |
---|
| 3093 | } |
---|
| 3094 | |
---|
| 3095 | static bool inNode(const Node& n) { |
---|
| 3096 | return n._in; |
---|
| 3097 | } |
---|
| 3098 | |
---|
| 3099 | static bool outNode(const Node& n) { |
---|
| 3100 | return !n._in; |
---|
| 3101 | } |
---|
| 3102 | |
---|
| 3103 | static bool origArc(const Arc& e) { |
---|
| 3104 | return e._item.firstState(); |
---|
| 3105 | } |
---|
| 3106 | |
---|
| 3107 | static bool bindArc(const Arc& e) { |
---|
| 3108 | return e._item.secondState(); |
---|
| 3109 | } |
---|
| 3110 | |
---|
| 3111 | static Node inNode(const DigraphNode& n) { |
---|
| 3112 | return Node(n, true); |
---|
| 3113 | } |
---|
| 3114 | |
---|
| 3115 | static Node outNode(const DigraphNode& n) { |
---|
| 3116 | return Node(n, false); |
---|
| 3117 | } |
---|
| 3118 | |
---|
| 3119 | static Arc arc(const DigraphNode& n) { |
---|
| 3120 | return Arc(n); |
---|
| 3121 | } |
---|
| 3122 | |
---|
| 3123 | static Arc arc(const DigraphArc& e) { |
---|
| 3124 | return Arc(e); |
---|
| 3125 | } |
---|
| 3126 | |
---|
| 3127 | typedef True NodeNumTag; |
---|
| 3128 | int nodeNum() const { |
---|
| 3129 | return 2 * countNodes(*_digraph); |
---|
| 3130 | } |
---|
| 3131 | |
---|
[446] | 3132 | typedef True ArcNumTag; |
---|
[414] | 3133 | int arcNum() const { |
---|
| 3134 | return countArcs(*_digraph) + countNodes(*_digraph); |
---|
| 3135 | } |
---|
| 3136 | |
---|
[446] | 3137 | typedef True FindArcTag; |
---|
[416] | 3138 | Arc findArc(const Node& u, const Node& v, |
---|
| 3139 | const Arc& prev = INVALID) const { |
---|
[449] | 3140 | if (inNode(u) && outNode(v)) { |
---|
| 3141 | if (static_cast<const DigraphNode&>(u) == |
---|
| 3142 | static_cast<const DigraphNode&>(v) && prev == INVALID) { |
---|
| 3143 | return Arc(u); |
---|
[414] | 3144 | } |
---|
[449] | 3145 | } |
---|
| 3146 | else if (outNode(u) && inNode(v)) { |
---|
| 3147 | return Arc(::lemon::findArc(*_digraph, u, v, prev)); |
---|
[414] | 3148 | } |
---|
| 3149 | return INVALID; |
---|
| 3150 | } |
---|
| 3151 | |
---|
| 3152 | private: |
---|
[416] | 3153 | |
---|
[512] | 3154 | template <typename V> |
---|
[416] | 3155 | class NodeMapBase |
---|
[512] | 3156 | : public MapTraits<typename Parent::template NodeMap<V> > { |
---|
| 3157 | typedef typename Parent::template NodeMap<V> NodeImpl; |
---|
[414] | 3158 | public: |
---|
| 3159 | typedef Node Key; |
---|
[512] | 3160 | typedef V Value; |
---|
[449] | 3161 | typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag; |
---|
| 3162 | typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue; |
---|
| 3163 | typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue; |
---|
| 3164 | typedef typename MapTraits<NodeImpl>::ReturnValue Reference; |
---|
| 3165 | typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference; |
---|
[416] | 3166 | |
---|
[512] | 3167 | NodeMapBase(const SplitNodesBase<DGR>& adaptor) |
---|
[416] | 3168 | : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} |
---|
[512] | 3169 | NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
[416] | 3170 | : _in_map(*adaptor._digraph, value), |
---|
| 3171 | _out_map(*adaptor._digraph, value) {} |
---|
[414] | 3172 | |
---|
[512] | 3173 | void set(const Node& key, const V& val) { |
---|
| 3174 | if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); } |
---|
[416] | 3175 | else {_out_map.set(key, val); } |
---|
[414] | 3176 | } |
---|
[416] | 3177 | |
---|
[449] | 3178 | ReturnValue operator[](const Node& key) { |
---|
[512] | 3179 | if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; } |
---|
[416] | 3180 | else { return _out_map[key]; } |
---|
[414] | 3181 | } |
---|
| 3182 | |
---|
[449] | 3183 | ConstReturnValue operator[](const Node& key) const { |
---|
[416] | 3184 | if (Adaptor::inNode(key)) { return _in_map[key]; } |
---|
| 3185 | else { return _out_map[key]; } |
---|
[414] | 3186 | } |
---|
| 3187 | |
---|
| 3188 | private: |
---|
| 3189 | NodeImpl _in_map, _out_map; |
---|
| 3190 | }; |
---|
| 3191 | |
---|
[512] | 3192 | template <typename V> |
---|
[416] | 3193 | class ArcMapBase |
---|
[512] | 3194 | : public MapTraits<typename Parent::template ArcMap<V> > { |
---|
| 3195 | typedef typename Parent::template ArcMap<V> ArcImpl; |
---|
| 3196 | typedef typename Parent::template NodeMap<V> NodeImpl; |
---|
[414] | 3197 | public: |
---|
| 3198 | typedef Arc Key; |
---|
[512] | 3199 | typedef V Value; |
---|
[449] | 3200 | typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag; |
---|
| 3201 | typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue; |
---|
| 3202 | typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue; |
---|
| 3203 | typedef typename MapTraits<ArcImpl>::ReturnValue Reference; |
---|
| 3204 | typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference; |
---|
[414] | 3205 | |
---|
[512] | 3206 | ArcMapBase(const SplitNodesBase<DGR>& adaptor) |
---|
[416] | 3207 | : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} |
---|
[512] | 3208 | ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
[416] | 3209 | : _arc_map(*adaptor._digraph, value), |
---|
| 3210 | _node_map(*adaptor._digraph, value) {} |
---|
[414] | 3211 | |
---|
[512] | 3212 | void set(const Arc& key, const V& val) { |
---|
| 3213 | if (SplitNodesBase<DGR>::origArc(key)) { |
---|
[516] | 3214 | _arc_map.set(static_cast<const DigraphArc&>(key), val); |
---|
[414] | 3215 | } else { |
---|
[516] | 3216 | _node_map.set(static_cast<const DigraphNode&>(key), val); |
---|
[414] | 3217 | } |
---|
| 3218 | } |
---|
[416] | 3219 | |
---|
[449] | 3220 | ReturnValue operator[](const Arc& key) { |
---|
[512] | 3221 | if (SplitNodesBase<DGR>::origArc(key)) { |
---|
[516] | 3222 | return _arc_map[static_cast<const DigraphArc&>(key)]; |
---|
[414] | 3223 | } else { |
---|
[516] | 3224 | return _node_map[static_cast<const DigraphNode&>(key)]; |
---|
[414] | 3225 | } |
---|
| 3226 | } |
---|
| 3227 | |
---|
[449] | 3228 | ConstReturnValue operator[](const Arc& key) const { |
---|
[512] | 3229 | if (SplitNodesBase<DGR>::origArc(key)) { |
---|
[516] | 3230 | return _arc_map[static_cast<const DigraphArc&>(key)]; |
---|
[414] | 3231 | } else { |
---|
[516] | 3232 | return _node_map[static_cast<const DigraphNode&>(key)]; |
---|
[414] | 3233 | } |
---|
| 3234 | } |
---|
| 3235 | |
---|
| 3236 | private: |
---|
| 3237 | ArcImpl _arc_map; |
---|
| 3238 | NodeImpl _node_map; |
---|
| 3239 | }; |
---|
| 3240 | |
---|
| 3241 | public: |
---|
| 3242 | |
---|
[512] | 3243 | template <typename V> |
---|
[416] | 3244 | class NodeMap |
---|
[617] | 3245 | : public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > { |
---|
| 3246 | typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > Parent; |
---|
| 3247 | |
---|
[414] | 3248 | public: |
---|
[512] | 3249 | typedef V Value; |
---|
| 3250 | |
---|
| 3251 | NodeMap(const SplitNodesBase<DGR>& adaptor) |
---|
[416] | 3252 | : Parent(adaptor) {} |
---|
| 3253 | |
---|
[512] | 3254 | NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
[416] | 3255 | : Parent(adaptor, value) {} |
---|
| 3256 | |
---|
[414] | 3257 | private: |
---|
| 3258 | NodeMap& operator=(const NodeMap& cmap) { |
---|
[416] | 3259 | return operator=<NodeMap>(cmap); |
---|
[414] | 3260 | } |
---|
[416] | 3261 | |
---|
[414] | 3262 | template <typename CMap> |
---|
| 3263 | NodeMap& operator=(const CMap& cmap) { |
---|
| 3264 | Parent::operator=(cmap); |
---|
[416] | 3265 | return *this; |
---|
[414] | 3266 | } |
---|
| 3267 | }; |
---|
| 3268 | |
---|
[512] | 3269 | template <typename V> |
---|
[416] | 3270 | class ArcMap |
---|
[617] | 3271 | : public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > { |
---|
| 3272 | typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > Parent; |
---|
| 3273 | |
---|
[414] | 3274 | public: |
---|
[512] | 3275 | typedef V Value; |
---|
| 3276 | |
---|
| 3277 | ArcMap(const SplitNodesBase<DGR>& adaptor) |
---|
[416] | 3278 | : Parent(adaptor) {} |
---|
| 3279 | |
---|
[512] | 3280 | ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value) |
---|
[416] | 3281 | : Parent(adaptor, value) {} |
---|
| 3282 | |
---|
[414] | 3283 | private: |
---|
| 3284 | ArcMap& operator=(const ArcMap& cmap) { |
---|
[416] | 3285 | return operator=<ArcMap>(cmap); |
---|
[414] | 3286 | } |
---|
[416] | 3287 | |
---|
[414] | 3288 | template <typename CMap> |
---|
| 3289 | ArcMap& operator=(const CMap& cmap) { |
---|
| 3290 | Parent::operator=(cmap); |
---|
[416] | 3291 | return *this; |
---|
[414] | 3292 | } |
---|
| 3293 | }; |
---|
| 3294 | |
---|
| 3295 | protected: |
---|
| 3296 | |
---|
[416] | 3297 | SplitNodesBase() : _digraph(0) {} |
---|
[414] | 3298 | |
---|
[512] | 3299 | DGR* _digraph; |
---|
| 3300 | |
---|
| 3301 | void initialize(Digraph& digraph) { |
---|
[414] | 3302 | _digraph = &digraph; |
---|
| 3303 | } |
---|
[416] | 3304 | |
---|
[414] | 3305 | }; |
---|
| 3306 | |
---|
| 3307 | /// \ingroup graph_adaptors |
---|
| 3308 | /// |
---|
[451] | 3309 | /// \brief Adaptor class for splitting the nodes of a digraph. |
---|
[416] | 3310 | /// |
---|
[451] | 3311 | /// SplitNodes adaptor can be used for splitting each node into an |
---|
| 3312 | /// \e in-node and an \e out-node in a digraph. Formaly, the adaptor |
---|
| 3313 | /// replaces each node \f$ u \f$ in the digraph with two nodes, |
---|
| 3314 | /// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$. |
---|
| 3315 | /// If there is a \f$ (v, u) \f$ arc in the original digraph, then the |
---|
| 3316 | /// new target of the arc will be \f$ u_{in} \f$ and similarly the |
---|
| 3317 | /// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$. |
---|
| 3318 | /// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$ |
---|
| 3319 | /// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph. |
---|
[414] | 3320 | /// |
---|
[451] | 3321 | /// The aim of this class is running an algorithm with respect to node |
---|
| 3322 | /// costs or capacities if the algorithm considers only arc costs or |
---|
| 3323 | /// capacities directly. |
---|
| 3324 | /// In this case you can use \c SplitNodes adaptor, and set the node |
---|
| 3325 | /// costs/capacities of the original digraph to the \e bind \e arcs |
---|
| 3326 | /// in the adaptor. |
---|
[414] | 3327 | /// |
---|
[512] | 3328 | /// \tparam DGR The type of the adapted digraph. |
---|
[451] | 3329 | /// It must conform to the \ref concepts::Digraph "Digraph" concept. |
---|
| 3330 | /// It is implicitly \c const. |
---|
| 3331 | /// |
---|
| 3332 | /// \note The \c Node type of this adaptor is converible to the \c Node |
---|
| 3333 | /// type of the adapted digraph. |
---|
[512] | 3334 | template <typename DGR> |
---|
[453] | 3335 | #ifdef DOXYGEN |
---|
| 3336 | class SplitNodes { |
---|
| 3337 | #else |
---|
[416] | 3338 | class SplitNodes |
---|
[512] | 3339 | : public DigraphAdaptorExtender<SplitNodesBase<const DGR> > { |
---|
[453] | 3340 | #endif |
---|
[617] | 3341 | typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent; |
---|
| 3342 | |
---|
[414] | 3343 | public: |
---|
[512] | 3344 | typedef DGR Digraph; |
---|
| 3345 | |
---|
| 3346 | typedef typename DGR::Node DigraphNode; |
---|
| 3347 | typedef typename DGR::Arc DigraphArc; |
---|
[415] | 3348 | |
---|
[414] | 3349 | typedef typename Parent::Node Node; |
---|
| 3350 | typedef typename Parent::Arc Arc; |
---|
| 3351 | |
---|
[451] | 3352 | /// \brief Constructor |
---|
[414] | 3353 | /// |
---|
| 3354 | /// Constructor of the adaptor. |
---|
[512] | 3355 | SplitNodes(const DGR& g) { |
---|
| 3356 | Parent::initialize(g); |
---|
[414] | 3357 | } |
---|
| 3358 | |
---|
[451] | 3359 | /// \brief Returns \c true if the given node is an in-node. |
---|
[415] | 3360 | /// |
---|
[451] | 3361 | /// Returns \c true if the given node is an in-node. |
---|
[415] | 3362 | static bool inNode(const Node& n) { |
---|
| 3363 | return Parent::inNode(n); |
---|
| 3364 | } |
---|
| 3365 | |
---|
[451] | 3366 | /// \brief Returns \c true if the given node is an out-node. |
---|
[415] | 3367 | /// |
---|
[451] | 3368 | /// Returns \c true if the given node is an out-node. |
---|
[415] | 3369 | static bool outNode(const Node& n) { |
---|
| 3370 | return Parent::outNode(n); |
---|
| 3371 | } |
---|
| 3372 | |
---|
[451] | 3373 | /// \brief Returns \c true if the given arc is an original arc. |
---|
[415] | 3374 | /// |
---|
[451] | 3375 | /// Returns \c true if the given arc is one of the arcs in the |
---|
| 3376 | /// original digraph. |
---|
[415] | 3377 | static bool origArc(const Arc& a) { |
---|
| 3378 | return Parent::origArc(a); |
---|
| 3379 | } |
---|
| 3380 | |
---|
[451] | 3381 | /// \brief Returns \c true if the given arc is a bind arc. |
---|
[415] | 3382 | /// |
---|
[451] | 3383 | /// Returns \c true if the given arc is a bind arc, i.e. it connects |
---|
| 3384 | /// an in-node and an out-node. |
---|
[415] | 3385 | static bool bindArc(const Arc& a) { |
---|
| 3386 | return Parent::bindArc(a); |
---|
| 3387 | } |
---|
| 3388 | |
---|
[451] | 3389 | /// \brief Returns the in-node created from the given original node. |
---|
[415] | 3390 | /// |
---|
[451] | 3391 | /// Returns the in-node created from the given original node. |
---|
[415] | 3392 | static Node inNode(const DigraphNode& n) { |
---|
| 3393 | return Parent::inNode(n); |
---|
| 3394 | } |
---|
| 3395 | |
---|
[451] | 3396 | /// \brief Returns the out-node created from the given original node. |
---|
[415] | 3397 | /// |
---|
[451] | 3398 | /// Returns the out-node created from the given original node. |
---|
[415] | 3399 | static Node outNode(const DigraphNode& n) { |
---|
| 3400 | return Parent::outNode(n); |
---|
| 3401 | } |
---|
| 3402 | |
---|
[451] | 3403 | /// \brief Returns the bind arc that corresponds to the given |
---|
| 3404 | /// original node. |
---|
[416] | 3405 | /// |
---|
[451] | 3406 | /// Returns the bind arc in the adaptor that corresponds to the given |
---|
| 3407 | /// original node, i.e. the arc connecting the in-node and out-node |
---|
| 3408 | /// of \c n. |
---|
[415] | 3409 | static Arc arc(const DigraphNode& n) { |
---|
| 3410 | return Parent::arc(n); |
---|
| 3411 | } |
---|
| 3412 | |
---|
[451] | 3413 | /// \brief Returns the arc that corresponds to the given original arc. |
---|
[416] | 3414 | /// |
---|
[451] | 3415 | /// Returns the arc in the adaptor that corresponds to the given |
---|
| 3416 | /// original arc. |
---|
[415] | 3417 | static Arc arc(const DigraphArc& a) { |
---|
| 3418 | return Parent::arc(a); |
---|
| 3419 | } |
---|
| 3420 | |
---|
[451] | 3421 | /// \brief Node map combined from two original node maps |
---|
[414] | 3422 | /// |
---|
[451] | 3423 | /// This map adaptor class adapts two node maps of the original digraph |
---|
| 3424 | /// to get a node map of the split digraph. |
---|
[559] | 3425 | /// Its value type is inherited from the first node map type (\c IN). |
---|
| 3426 | /// \tparam IN The type of the node map for the in-nodes. |
---|
| 3427 | /// \tparam OUT The type of the node map for the out-nodes. |
---|
| 3428 | template <typename IN, typename OUT> |
---|
[414] | 3429 | class CombinedNodeMap { |
---|
| 3430 | public: |
---|
| 3431 | |
---|
[451] | 3432 | /// The key type of the map |
---|
[414] | 3433 | typedef Node Key; |
---|
[451] | 3434 | /// The value type of the map |
---|
[559] | 3435 | typedef typename IN::Value Value; |
---|
| 3436 | |
---|
| 3437 | typedef typename MapTraits<IN>::ReferenceMapTag ReferenceMapTag; |
---|
| 3438 | typedef typename MapTraits<IN>::ReturnValue ReturnValue; |
---|
| 3439 | typedef typename MapTraits<IN>::ConstReturnValue ConstReturnValue; |
---|
| 3440 | typedef typename MapTraits<IN>::ReturnValue Reference; |
---|
| 3441 | typedef typename MapTraits<IN>::ConstReturnValue ConstReference; |
---|
[449] | 3442 | |
---|
[451] | 3443 | /// Constructor |
---|
[559] | 3444 | CombinedNodeMap(IN& in_map, OUT& out_map) |
---|
[416] | 3445 | : _in_map(in_map), _out_map(out_map) {} |
---|
[414] | 3446 | |
---|
[451] | 3447 | /// Returns the value associated with the given key. |
---|
| 3448 | Value operator[](const Key& key) const { |
---|
[512] | 3449 | if (SplitNodesBase<const DGR>::inNode(key)) { |
---|
[451] | 3450 | return _in_map[key]; |
---|
| 3451 | } else { |
---|
| 3452 | return _out_map[key]; |
---|
| 3453 | } |
---|
| 3454 | } |
---|
| 3455 | |
---|
| 3456 | /// Returns a reference to the value associated with the given key. |
---|
[414] | 3457 | Value& operator[](const Key& key) { |
---|
[512] | 3458 | if (SplitNodesBase<const DGR>::inNode(key)) { |
---|
[416] | 3459 | return _in_map[key]; |
---|
| 3460 | } else { |
---|
| 3461 | return _out_map[key]; |
---|
| 3462 | } |
---|
[414] | 3463 | } |
---|
| 3464 | |
---|
[451] | 3465 | /// Sets the value associated with the given key. |
---|
[414] | 3466 | void set(const Key& key, const Value& value) { |
---|
[512] | 3467 | if (SplitNodesBase<const DGR>::inNode(key)) { |
---|
[416] | 3468 | _in_map.set(key, value); |
---|
| 3469 | } else { |
---|
| 3470 | _out_map.set(key, value); |
---|
| 3471 | } |
---|
[414] | 3472 | } |
---|
[416] | 3473 | |
---|
[414] | 3474 | private: |
---|
[416] | 3475 | |
---|
[559] | 3476 | IN& _in_map; |
---|
| 3477 | OUT& _out_map; |
---|
[416] | 3478 | |
---|
[414] | 3479 | }; |
---|
| 3480 | |
---|
| 3481 | |
---|
[451] | 3482 | /// \brief Returns a combined node map |
---|
[416] | 3483 | /// |
---|
[451] | 3484 | /// This function just returns a combined node map. |
---|
[559] | 3485 | template <typename IN, typename OUT> |
---|
| 3486 | static CombinedNodeMap<IN, OUT> |
---|
| 3487 | combinedNodeMap(IN& in_map, OUT& out_map) { |
---|
| 3488 | return CombinedNodeMap<IN, OUT>(in_map, out_map); |
---|
[414] | 3489 | } |
---|
| 3490 | |
---|
[559] | 3491 | template <typename IN, typename OUT> |
---|
| 3492 | static CombinedNodeMap<const IN, OUT> |
---|
| 3493 | combinedNodeMap(const IN& in_map, OUT& out_map) { |
---|
| 3494 | return CombinedNodeMap<const IN, OUT>(in_map, out_map); |
---|
[414] | 3495 | } |
---|
| 3496 | |
---|
[559] | 3497 | template <typename IN, typename OUT> |
---|
| 3498 | static CombinedNodeMap<IN, const OUT> |
---|
| 3499 | combinedNodeMap(IN& in_map, const OUT& out_map) { |
---|
| 3500 | return CombinedNodeMap<IN, const OUT>(in_map, out_map); |
---|
[414] | 3501 | } |
---|
| 3502 | |
---|
[559] | 3503 | template <typename IN, typename OUT> |
---|
| 3504 | static CombinedNodeMap<const IN, const OUT> |
---|
| 3505 | combinedNodeMap(const IN& in_map, const OUT& out_map) { |
---|
| 3506 | return CombinedNodeMap<const IN, const OUT>(in_map, out_map); |
---|
[414] | 3507 | } |
---|
| 3508 | |
---|
[451] | 3509 | /// \brief Arc map combined from an arc map and a node map of the |
---|
| 3510 | /// original digraph. |
---|
[414] | 3511 | /// |
---|
[451] | 3512 | /// This map adaptor class adapts an arc map and a node map of the |
---|
| 3513 | /// original digraph to get an arc map of the split digraph. |
---|
[559] | 3514 | /// Its value type is inherited from the original arc map type (\c AM). |
---|
| 3515 | /// \tparam AM The type of the arc map. |
---|
| 3516 | /// \tparam NM the type of the node map. |
---|
| 3517 | template <typename AM, typename NM> |
---|
[414] | 3518 | class CombinedArcMap { |
---|
| 3519 | public: |
---|
[416] | 3520 | |
---|
[451] | 3521 | /// The key type of the map |
---|
[414] | 3522 | typedef Arc Key; |
---|
[451] | 3523 | /// The value type of the map |
---|
[559] | 3524 | typedef typename AM::Value Value; |
---|
| 3525 | |
---|
| 3526 | typedef typename MapTraits<AM>::ReferenceMapTag ReferenceMapTag; |
---|
| 3527 | typedef typename MapTraits<AM>::ReturnValue ReturnValue; |
---|
| 3528 | typedef typename MapTraits<AM>::ConstReturnValue ConstReturnValue; |
---|
| 3529 | typedef typename MapTraits<AM>::ReturnValue Reference; |
---|
| 3530 | typedef typename MapTraits<AM>::ConstReturnValue ConstReference; |
---|
[449] | 3531 | |
---|
[451] | 3532 | /// Constructor |
---|
[559] | 3533 | CombinedArcMap(AM& arc_map, NM& node_map) |
---|
[416] | 3534 | : _arc_map(arc_map), _node_map(node_map) {} |
---|
[414] | 3535 | |
---|
[451] | 3536 | /// Returns the value associated with the given key. |
---|
| 3537 | Value operator[](const Key& arc) const { |
---|
[512] | 3538 | if (SplitNodesBase<const DGR>::origArc(arc)) { |
---|
[451] | 3539 | return _arc_map[arc]; |
---|
| 3540 | } else { |
---|
| 3541 | return _node_map[arc]; |
---|
| 3542 | } |
---|
| 3543 | } |
---|
| 3544 | |
---|
| 3545 | /// Returns a reference to the value associated with the given key. |
---|
| 3546 | Value& operator[](const Key& arc) { |
---|
[512] | 3547 | if (SplitNodesBase<const DGR>::origArc(arc)) { |
---|
[451] | 3548 | return _arc_map[arc]; |
---|
| 3549 | } else { |
---|
| 3550 | return _node_map[arc]; |
---|
| 3551 | } |
---|
| 3552 | } |
---|
| 3553 | |
---|
| 3554 | /// Sets the value associated with the given key. |
---|
[414] | 3555 | void set(const Arc& arc, const Value& val) { |
---|
[512] | 3556 | if (SplitNodesBase<const DGR>::origArc(arc)) { |
---|
[416] | 3557 | _arc_map.set(arc, val); |
---|
| 3558 | } else { |
---|
| 3559 | _node_map.set(arc, val); |
---|
| 3560 | } |
---|
[414] | 3561 | } |
---|
| 3562 | |
---|
| 3563 | private: |
---|
[559] | 3564 | |
---|
| 3565 | AM& _arc_map; |
---|
| 3566 | NM& _node_map; |
---|
| 3567 | |
---|
[414] | 3568 | }; |
---|
[416] | 3569 | |
---|
[451] | 3570 | /// \brief Returns a combined arc map |
---|
[416] | 3571 | /// |
---|
[451] | 3572 | /// This function just returns a combined arc map. |
---|
[453] | 3573 | template <typename ArcMap, typename NodeMap> |
---|
| 3574 | static CombinedArcMap<ArcMap, NodeMap> |
---|
| 3575 | combinedArcMap(ArcMap& arc_map, NodeMap& node_map) { |
---|
| 3576 | return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map); |
---|
[414] | 3577 | } |
---|
| 3578 | |
---|
[453] | 3579 | template <typename ArcMap, typename NodeMap> |
---|
| 3580 | static CombinedArcMap<const ArcMap, NodeMap> |
---|
| 3581 | combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) { |
---|
| 3582 | return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map); |
---|
[414] | 3583 | } |
---|
| 3584 | |
---|
[453] | 3585 | template <typename ArcMap, typename NodeMap> |
---|
| 3586 | static CombinedArcMap<ArcMap, const NodeMap> |
---|
| 3587 | combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) { |
---|
| 3588 | return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map); |
---|
[414] | 3589 | } |
---|
| 3590 | |
---|
[453] | 3591 | template <typename ArcMap, typename NodeMap> |
---|
| 3592 | static CombinedArcMap<const ArcMap, const NodeMap> |
---|
| 3593 | combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) { |
---|
| 3594 | return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map); |
---|
[414] | 3595 | } |
---|
| 3596 | |
---|
| 3597 | }; |
---|
| 3598 | |
---|
[451] | 3599 | /// \brief Returns a (read-only) SplitNodes adaptor |
---|
[414] | 3600 | /// |
---|
[451] | 3601 | /// This function just returns a (read-only) \ref SplitNodes adaptor. |
---|
| 3602 | /// \ingroup graph_adaptors |
---|
| 3603 | /// \relates SplitNodes |
---|
[512] | 3604 | template<typename DGR> |
---|
| 3605 | SplitNodes<DGR> |
---|
| 3606 | splitNodes(const DGR& digraph) { |
---|
| 3607 | return SplitNodes<DGR>(digraph); |
---|
[414] | 3608 | } |
---|
| 3609 | |
---|
[512] | 3610 | #undef LEMON_SCOPE_FIX |
---|
| 3611 | |
---|
[414] | 3612 | } //namespace lemon |
---|
| 3613 | |
---|
[416] | 3614 | #endif //LEMON_ADAPTORS_H |
---|