Ticket #67: e67acd83a9ca.patch
File e67acd83a9ca.patch, 171.4 KB (added by , 16 years ago) |
---|
-
lemon/Makefile.am
# HG changeset patch # User Balazs Dezso <deba@inf.elte.hu> # Date 1216652518 -7200 # Node ID e67acd83a9ca0c54b66d8a127273e0301d7cdea8 # Parent b6732e0d38c5c5e6b21123d8425a62591491e391 Porting graph adaptors from svn 3498 diff -r b6732e0d38c5 -r e67acd83a9ca lemon/Makefile.am
a b 26 26 lemon/counter.h \ 27 27 lemon/core.h \ 28 28 lemon/dfs.h \ 29 lemon/digraph_adaptor.h \ 29 30 lemon/dijkstra.h \ 30 31 lemon/dim2.h \ 31 32 lemon/error.h \ 33 lemon/graph_adaptor.h \ 32 34 lemon/graph_to_eps.h \ 33 35 lemon/kruskal.h \ 34 36 lemon/lgf_reader.h \ … … 50 52 lemon/bits/bezier.h \ 51 53 lemon/bits/default_map.h \ 52 54 lemon/bits/enable_if.h \ 55 lemon/bits/graph_adaptor_extender.h \ 53 56 lemon/bits/graph_extender.h \ 54 57 lemon/bits/map_extender.h \ 55 58 lemon/bits/path_dump.h \ 56 59 lemon/bits/traits.h \ 60 lemon/bits/variant.h \ 57 61 lemon/bits/vector_map.h 58 62 59 63 concept_HEADERS += \ -
new file lemon/bits/graph_adaptor_extender.h
diff -r b6732e0d38c5 -r e67acd83a9ca lemon/bits/graph_adaptor_extender.h
- + 1 /* -*- C++ -*- 2 * 3 * This file is a part of LEMON, a generic C++ optimization library 4 * 5 * Copyright (C) 2003-2008 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 19 #ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H 20 #define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H 21 22 #include <lemon/core.h> 23 #include <lemon/error.h> 24 25 #include <lemon/bits/default_map.h> 26 27 28 ///\ingroup digraphbits 29 ///\file 30 ///\brief Extenders for the digraph adaptor types 31 namespace lemon { 32 33 /// \ingroup digraphbits 34 /// 35 /// \brief Extender for the DigraphAdaptors 36 template <typename _Digraph> 37 class DigraphAdaptorExtender : public _Digraph { 38 public: 39 40 typedef _Digraph Parent; 41 typedef _Digraph Digraph; 42 typedef DigraphAdaptorExtender Adaptor; 43 44 // Base extensions 45 46 typedef typename Parent::Node Node; 47 typedef typename Parent::Arc Arc; 48 49 int maxId(Node) const { 50 return Parent::maxNodeId(); 51 } 52 53 int maxId(Arc) const { 54 return Parent::maxArcId(); 55 } 56 57 Node fromId(int id, Node) const { 58 return Parent::nodeFromId(id); 59 } 60 61 Arc fromId(int id, Arc) const { 62 return Parent::arcFromId(id); 63 } 64 65 Node oppositeNode(const Node &n, const Arc &e) const { 66 if (n == Parent::source(e)) 67 return Parent::target(e); 68 else if(n==Parent::target(e)) 69 return Parent::source(e); 70 else 71 return INVALID; 72 } 73 74 class NodeIt : public Node { 75 const Adaptor* _adaptor; 76 public: 77 78 NodeIt() {} 79 80 NodeIt(Invalid i) : Node(i) { } 81 82 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { 83 _adaptor->first(static_cast<Node&>(*this)); 84 } 85 86 NodeIt(const Adaptor& adaptor, const Node& node) 87 : Node(node), _adaptor(&adaptor) {} 88 89 NodeIt& operator++() { 90 _adaptor->next(*this); 91 return *this; 92 } 93 94 }; 95 96 97 class ArcIt : public Arc { 98 const Adaptor* _adaptor; 99 public: 100 101 ArcIt() { } 102 103 ArcIt(Invalid i) : Arc(i) { } 104 105 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { 106 _adaptor->first(static_cast<Arc&>(*this)); 107 } 108 109 ArcIt(const Adaptor& adaptor, const Arc& e) : 110 Arc(e), _adaptor(&adaptor) { } 111 112 ArcIt& operator++() { 113 _adaptor->next(*this); 114 return *this; 115 } 116 117 }; 118 119 120 class OutArcIt : public Arc { 121 const Adaptor* _adaptor; 122 public: 123 124 OutArcIt() { } 125 126 OutArcIt(Invalid i) : Arc(i) { } 127 128 OutArcIt(const Adaptor& adaptor, const Node& node) 129 : _adaptor(&adaptor) { 130 _adaptor->firstOut(*this, node); 131 } 132 133 OutArcIt(const Adaptor& adaptor, const Arc& arc) 134 : Arc(arc), _adaptor(&adaptor) {} 135 136 OutArcIt& operator++() { 137 _adaptor->nextOut(*this); 138 return *this; 139 } 140 141 }; 142 143 144 class InArcIt : public Arc { 145 const Adaptor* _adaptor; 146 public: 147 148 InArcIt() { } 149 150 InArcIt(Invalid i) : Arc(i) { } 151 152 InArcIt(const Adaptor& adaptor, const Node& node) 153 : _adaptor(&adaptor) { 154 _adaptor->firstIn(*this, node); 155 } 156 157 InArcIt(const Adaptor& adaptor, const Arc& arc) : 158 Arc(arc), _adaptor(&adaptor) {} 159 160 InArcIt& operator++() { 161 _adaptor->nextIn(*this); 162 return *this; 163 } 164 165 }; 166 167 /// \brief Base node of the iterator 168 /// 169 /// Returns the base node (ie. the source in this case) of the iterator 170 Node baseNode(const OutArcIt &e) const { 171 return Parent::source(e); 172 } 173 /// \brief Running node of the iterator 174 /// 175 /// Returns the running node (ie. the target in this case) of the 176 /// iterator 177 Node runningNode(const OutArcIt &e) const { 178 return Parent::target(e); 179 } 180 181 /// \brief Base node of the iterator 182 /// 183 /// Returns the base node (ie. the target in this case) of the iterator 184 Node baseNode(const InArcIt &e) const { 185 return Parent::target(e); 186 } 187 /// \brief Running node of the iterator 188 /// 189 /// Returns the running node (ie. the source in this case) of the 190 /// iterator 191 Node runningNode(const InArcIt &e) const { 192 return Parent::source(e); 193 } 194 195 }; 196 197 198 /// \ingroup digraphbits 199 /// 200 /// \brief Extender for the GraphAdaptors 201 template <typename _Graph> 202 class GraphAdaptorExtender : public _Graph { 203 public: 204 205 typedef _Graph Parent; 206 typedef _Graph Graph; 207 typedef GraphAdaptorExtender Adaptor; 208 209 typedef typename Parent::Node Node; 210 typedef typename Parent::Arc Arc; 211 typedef typename Parent::Edge Edge; 212 213 // Graph extension 214 215 int maxId(Node) const { 216 return Parent::maxNodeId(); 217 } 218 219 int maxId(Arc) const { 220 return Parent::maxArcId(); 221 } 222 223 int maxId(Edge) const { 224 return Parent::maxEdgeId(); 225 } 226 227 Node fromId(int id, Node) const { 228 return Parent::nodeFromId(id); 229 } 230 231 Arc fromId(int id, Arc) const { 232 return Parent::arcFromId(id); 233 } 234 235 Edge fromId(int id, Edge) const { 236 return Parent::edgeFromId(id); 237 } 238 239 Node oppositeNode(const Node &n, const Edge &e) const { 240 if( n == Parent::u(e)) 241 return Parent::v(e); 242 else if( n == Parent::v(e)) 243 return Parent::u(e); 244 else 245 return INVALID; 246 } 247 248 Arc oppositeArc(const Arc &a) const { 249 return Parent::direct(a, !Parent::direction(a)); 250 } 251 252 using Parent::direct; 253 Arc direct(const Edge &e, const Node &s) const { 254 return Parent::direct(e, Parent::u(e) == s); 255 } 256 257 258 class NodeIt : public Node { 259 const Adaptor* _adaptor; 260 public: 261 262 NodeIt() {} 263 264 NodeIt(Invalid i) : Node(i) { } 265 266 explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { 267 _adaptor->first(static_cast<Node&>(*this)); 268 } 269 270 NodeIt(const Adaptor& adaptor, const Node& node) 271 : Node(node), _adaptor(&adaptor) {} 272 273 NodeIt& operator++() { 274 _adaptor->next(*this); 275 return *this; 276 } 277 278 }; 279 280 281 class ArcIt : public Arc { 282 const Adaptor* _adaptor; 283 public: 284 285 ArcIt() { } 286 287 ArcIt(Invalid i) : Arc(i) { } 288 289 explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { 290 _adaptor->first(static_cast<Arc&>(*this)); 291 } 292 293 ArcIt(const Adaptor& adaptor, const Arc& e) : 294 Arc(e), _adaptor(&adaptor) { } 295 296 ArcIt& operator++() { 297 _adaptor->next(*this); 298 return *this; 299 } 300 301 }; 302 303 304 class OutArcIt : public Arc { 305 const Adaptor* _adaptor; 306 public: 307 308 OutArcIt() { } 309 310 OutArcIt(Invalid i) : Arc(i) { } 311 312 OutArcIt(const Adaptor& adaptor, const Node& node) 313 : _adaptor(&adaptor) { 314 _adaptor->firstOut(*this, node); 315 } 316 317 OutArcIt(const Adaptor& adaptor, const Arc& arc) 318 : Arc(arc), _adaptor(&adaptor) {} 319 320 OutArcIt& operator++() { 321 _adaptor->nextOut(*this); 322 return *this; 323 } 324 325 }; 326 327 328 class InArcIt : public Arc { 329 const Adaptor* _adaptor; 330 public: 331 332 InArcIt() { } 333 334 InArcIt(Invalid i) : Arc(i) { } 335 336 InArcIt(const Adaptor& adaptor, const Node& node) 337 : _adaptor(&adaptor) { 338 _adaptor->firstIn(*this, node); 339 } 340 341 InArcIt(const Adaptor& adaptor, const Arc& arc) : 342 Arc(arc), _adaptor(&adaptor) {} 343 344 InArcIt& operator++() { 345 _adaptor->nextIn(*this); 346 return *this; 347 } 348 349 }; 350 351 class EdgeIt : public Parent::Edge { 352 const Adaptor* _adaptor; 353 public: 354 355 EdgeIt() { } 356 357 EdgeIt(Invalid i) : Edge(i) { } 358 359 explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { 360 _adaptor->first(static_cast<Edge&>(*this)); 361 } 362 363 EdgeIt(const Adaptor& adaptor, const Edge& e) : 364 Edge(e), _adaptor(&adaptor) { } 365 366 EdgeIt& operator++() { 367 _adaptor->next(*this); 368 return *this; 369 } 370 371 }; 372 373 class IncEdgeIt : public Edge { 374 friend class GraphAdaptorExtender; 375 const Adaptor* _adaptor; 376 bool direction; 377 public: 378 379 IncEdgeIt() { } 380 381 IncEdgeIt(Invalid i) : Edge(i), direction(false) { } 382 383 IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) { 384 _adaptor->firstInc(static_cast<Edge&>(*this), direction, n); 385 } 386 387 IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n) 388 : _adaptor(&adaptor), Edge(e) { 389 direction = (_adaptor->u(e) == n); 390 } 391 392 IncEdgeIt& operator++() { 393 _adaptor->nextInc(*this, direction); 394 return *this; 395 } 396 }; 397 398 /// \brief Base node of the iterator 399 /// 400 /// Returns the base node (ie. the source in this case) of the iterator 401 Node baseNode(const OutArcIt &a) const { 402 return Parent::source(a); 403 } 404 /// \brief Running node of the iterator 405 /// 406 /// Returns the running node (ie. the target in this case) of the 407 /// iterator 408 Node runningNode(const OutArcIt &a) const { 409 return Parent::target(a); 410 } 411 412 /// \brief Base node of the iterator 413 /// 414 /// Returns the base node (ie. the target in this case) of the iterator 415 Node baseNode(const InArcIt &a) const { 416 return Parent::target(a); 417 } 418 /// \brief Running node of the iterator 419 /// 420 /// Returns the running node (ie. the source in this case) of the 421 /// iterator 422 Node runningNode(const InArcIt &a) const { 423 return Parent::source(a); 424 } 425 426 /// Base node of the iterator 427 /// 428 /// Returns the base node of the iterator 429 Node baseNode(const IncEdgeIt &e) const { 430 return e.direction ? Parent::u(e) : Parent::v(e); 431 } 432 /// Running node of the iterator 433 /// 434 /// Returns the running node of the iterator 435 Node runningNode(const IncEdgeIt &e) const { 436 return e.direction ? Parent::v(e) : Parent::u(e); 437 } 438 439 }; 440 441 } 442 443 444 #endif -
new file lemon/bits/variant.h
diff -r b6732e0d38c5 -r e67acd83a9ca lemon/bits/variant.h
- + 1 /* -*- C++ -*- 2 * 3 * This file is a part of LEMON, a generic C++ optimization library 4 * 5 * Copyright (C) 2003-2008 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 19 #ifndef LEMON_BITS_VARIANT_H 20 #define LEMON_BITS_VARIANT_H 21 22 #include <lemon/assert.h> 23 24 /// \file 25 /// \brief Variant types 26 27 namespace lemon { 28 29 namespace _variant_bits { 30 31 template <int left, int right> 32 struct CTMax { 33 static const int value = left < right ? right : left; 34 }; 35 36 } 37 38 39 /// \brief Simple Variant type for two types 40 /// 41 /// Simple Variant type for two types. The Variant type is a type 42 /// safe union. The C++ has strong limitations for using unions, by 43 /// example we can not store type with non default constructor or 44 /// destructor in an union. This class always knowns the current 45 /// state of the variant and it cares for the proper construction 46 /// and destruction. 47 template <typename _First, typename _Second> 48 class BiVariant { 49 public: 50 51 /// \brief The \c First type. 52 typedef _First First; 53 /// \brief The \c Second type. 54 typedef _Second Second; 55 56 struct WrongStateError : public lemon::LogicError { 57 public: 58 virtual const char* what() const throw() { 59 return "lemon::BiVariant::WrongStateError"; 60 } 61 }; 62 63 /// \brief Constructor 64 /// 65 /// This constructor initalizes to the default value of the \c First 66 /// type. 67 BiVariant() { 68 flag = true; 69 new(reinterpret_cast<First*>(data)) First(); 70 } 71 72 /// \brief Constructor 73 /// 74 /// This constructor initalizes to the given value of the \c First 75 /// type. 76 BiVariant(const First& f) { 77 flag = true; 78 new(reinterpret_cast<First*>(data)) First(f); 79 } 80 81 /// \brief Constructor 82 /// 83 /// This constructor initalizes to the given value of the \c 84 /// Second type. 85 BiVariant(const Second& s) { 86 flag = false; 87 new(reinterpret_cast<Second*>(data)) Second(s); 88 } 89 90 /// \brief Copy constructor 91 /// 92 /// Copy constructor 93 BiVariant(const BiVariant& bivariant) { 94 flag = bivariant.flag; 95 if (flag) { 96 new(reinterpret_cast<First*>(data)) First(bivariant.first()); 97 } else { 98 new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); 99 } 100 } 101 102 /// \brief Destrcutor 103 /// 104 /// Destructor 105 ~BiVariant() { 106 destroy(); 107 } 108 109 /// \brief Set to the default value of the \c First type. 110 /// 111 /// This function sets the variant to the default value of the \c 112 /// First type. 113 BiVariant& setFirst() { 114 destroy(); 115 flag = true; 116 new(reinterpret_cast<First*>(data)) First(); 117 return *this; 118 } 119 120 /// \brief Set to the given value of the \c First type. 121 /// 122 /// This function sets the variant to the given value of the \c 123 /// First type. 124 BiVariant& setFirst(const First& f) { 125 destroy(); 126 flag = true; 127 new(reinterpret_cast<First*>(data)) First(f); 128 return *this; 129 } 130 131 /// \brief Set to the default value of the \c Second type. 132 /// 133 /// This function sets the variant to the default value of the \c 134 /// Second type. 135 BiVariant& setSecond() { 136 destroy(); 137 flag = false; 138 new(reinterpret_cast<Second*>(data)) Second(); 139 return *this; 140 } 141 142 /// \brief Set to the given value of the \c Second type. 143 /// 144 /// This function sets the variant to the given value of the \c 145 /// Second type. 146 BiVariant& setSecond(const Second& s) { 147 destroy(); 148 flag = false; 149 new(reinterpret_cast<Second*>(data)) Second(s); 150 return *this; 151 } 152 153 /// \brief Operator form of the \c setFirst() 154 BiVariant& operator=(const First& f) { 155 return setFirst(f); 156 } 157 158 /// \brief Operator form of the \c setSecond() 159 BiVariant& operator=(const Second& s) { 160 return setSecond(s); 161 } 162 163 /// \brief Assign operator 164 BiVariant& operator=(const BiVariant& bivariant) { 165 if (this == &bivariant) return *this; 166 destroy(); 167 flag = bivariant.flag; 168 if (flag) { 169 new(reinterpret_cast<First*>(data)) First(bivariant.first()); 170 } else { 171 new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); 172 } 173 return *this; 174 } 175 176 /// \brief Reference to the value 177 /// 178 /// Reference to the value of the \c First type. 179 /// \pre The BiVariant should store value of \c First type. 180 First& first() { 181 LEMON_DEBUG(flag, WrongStateError()); 182 return *reinterpret_cast<First*>(data); 183 } 184 185 /// \brief Const reference to the value 186 /// 187 /// Const reference to the value of the \c First type. 188 /// \pre The BiVariant should store value of \c First type. 189 const First& first() const { 190 LEMON_DEBUG(flag, WrongStateError()); 191 return *reinterpret_cast<const First*>(data); 192 } 193 194 /// \brief Operator form of the \c first() 195 operator First&() { return first(); } 196 /// \brief Operator form of the const \c first() 197 operator const First&() const { return first(); } 198 199 /// \brief Reference to the value 200 /// 201 /// Reference to the value of the \c Second type. 202 /// \pre The BiVariant should store value of \c Second type. 203 Second& second() { 204 LEMON_DEBUG(!flag, WrongStateError()); 205 return *reinterpret_cast<Second*>(data); 206 } 207 208 /// \brief Const reference to the value 209 /// 210 /// Const reference to the value of the \c Second type. 211 /// \pre The BiVariant should store value of \c Second type. 212 const Second& second() const { 213 LEMON_DEBUG(!flag, WrongStateError()); 214 return *reinterpret_cast<const Second*>(data); 215 } 216 217 /// \brief Operator form of the \c second() 218 operator Second&() { return second(); } 219 /// \brief Operator form of the const \c second() 220 operator const Second&() const { return second(); } 221 222 /// \brief %True when the variant is in the first state 223 /// 224 /// %True when the variant stores value of the \c First type. 225 bool firstState() const { return flag; } 226 227 /// \brief %True when the variant is in the second state 228 /// 229 /// %True when the variant stores value of the \c Second type. 230 bool secondState() const { return !flag; } 231 232 private: 233 234 void destroy() { 235 if (flag) { 236 reinterpret_cast<First*>(data)->~First(); 237 } else { 238 reinterpret_cast<Second*>(data)->~Second(); 239 } 240 } 241 242 char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value]; 243 bool flag; 244 }; 245 246 namespace _variant_bits { 247 248 template <int _idx, typename _TypeMap> 249 struct Memory { 250 251 typedef typename _TypeMap::template Map<_idx>::Type Current; 252 253 static void destroy(int index, char* place) { 254 if (index == _idx) { 255 reinterpret_cast<Current*>(place)->~Current(); 256 } else { 257 Memory<_idx - 1, _TypeMap>::destroy(index, place); 258 } 259 } 260 261 static void copy(int index, char* to, const char* from) { 262 if (index == _idx) { 263 new (reinterpret_cast<Current*>(to)) 264 Current(reinterpret_cast<const Current*>(from)); 265 } else { 266 Memory<_idx - 1, _TypeMap>::copy(index, to, from); 267 } 268 } 269 270 }; 271 272 template <typename _TypeMap> 273 struct Memory<-1, _TypeMap> { 274 275 static void destroy(int, char*) { 276 LEMON_DEBUG(false, "Wrong Variant Index."); 277 } 278 279 static void copy(int, char*, const char*) { 280 LEMON_DEBUG(false, "Wrong Variant Index."); 281 } 282 }; 283 284 template <int _idx, typename _TypeMap> 285 struct Size { 286 static const int value = 287 CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type), 288 Size<_idx - 1, _TypeMap>::value>::value; 289 }; 290 291 template <typename _TypeMap> 292 struct Size<0, _TypeMap> { 293 static const int value = 294 sizeof(typename _TypeMap::template Map<0>::Type); 295 }; 296 297 } 298 299 /// \brief Variant type 300 /// 301 /// Simple Variant type. The Variant type is a type safe union. The 302 /// C++ has strong limitations for using unions, for example we 303 /// cannot store type with non default constructor or destructor in 304 /// a union. This class always knowns the current state of the 305 /// variant and it cares for the proper construction and 306 /// destruction. 307 /// 308 /// \param _num The number of the types which can be stored in the 309 /// variant type. 310 /// \param _TypeMap This class describes the types of the Variant. The 311 /// _TypeMap::Map<index>::Type should be a valid type for each index 312 /// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper 313 /// class to define such type mappings up to 10 types. 314 /// 315 /// And the usage of the class: 316 ///\code 317 /// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant; 318 /// MyVariant var; 319 /// var.set<0>(12); 320 /// std::cout << var.get<0>() << std::endl; 321 /// var.set<1>("alpha"); 322 /// std::cout << var.get<1>() << std::endl; 323 /// var.set<2>(0.75); 324 /// std::cout << var.get<2>() << std::endl; 325 ///\endcode 326 /// 327 /// The result of course: 328 ///\code 329 /// 12 330 /// alpha 331 /// 0.75 332 ///\endcode 333 template <int _num, typename _TypeMap> 334 class Variant { 335 public: 336 337 static const int num = _num; 338 339 typedef _TypeMap TypeMap; 340 341 struct WrongStateError : public lemon::LogicError { 342 public: 343 virtual const char* what() const throw() { 344 return "lemon::Variant::WrongStateError"; 345 } 346 }; 347 348 /// \brief Constructor 349 /// 350 /// This constructor initalizes to the default value of the \c type 351 /// with 0 index. 352 Variant() { 353 flag = 0; 354 new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data)) 355 typename TypeMap::template Map<0>::Type(); 356 } 357 358 359 /// \brief Copy constructor 360 /// 361 /// Copy constructor 362 Variant(const Variant& variant) { 363 flag = variant.flag; 364 _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data); 365 } 366 367 /// \brief Assign operator 368 /// 369 /// Assign operator 370 Variant& operator=(const Variant& variant) { 371 if (this == &variant) return *this; 372 _variant_bits::Memory<num - 1, TypeMap>:: 373 destroy(flag, data); 374 flag = variant.flag; 375 _variant_bits::Memory<num - 1, TypeMap>:: 376 copy(flag, data, variant.data); 377 return *this; 378 } 379 380 /// \brief Destrcutor 381 /// 382 /// Destructor 383 ~Variant() { 384 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); 385 } 386 387 /// \brief Set to the default value of the type with \c _idx index. 388 /// 389 /// This function sets the variant to the default value of the 390 /// type with \c _idx index. 391 template <int _idx> 392 Variant& set() { 393 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); 394 flag = _idx; 395 new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) 396 typename TypeMap::template Map<_idx>::Type(); 397 return *this; 398 } 399 400 /// \brief Set to the given value of the type with \c _idx index. 401 /// 402 /// This function sets the variant to the given value of the type 403 /// with \c _idx index. 404 template <int _idx> 405 Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) { 406 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); 407 flag = _idx; 408 new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) 409 typename TypeMap::template Map<_idx>::Type(init); 410 return *this; 411 } 412 413 /// \brief Gets the current value of the type with \c _idx index. 414 /// 415 /// Gets the current value of the type with \c _idx index. 416 template <int _idx> 417 const typename TypeMap::template Map<_idx>::Type& get() const { 418 LEMON_DEBUG(_idx == flag, "Wrong Variant Index."); 419 return *reinterpret_cast<const typename TypeMap:: 420 template Map<_idx>::Type*>(data); 421 } 422 423 /// \brief Gets the current value of the type with \c _idx index. 424 /// 425 /// Gets the current value of the type with \c _idx index. 426 template <int _idx> 427 typename _TypeMap::template Map<_idx>::Type& get() { 428 LEMON_DEBUG(_idx == flag, "Wrong Variant Index."); 429 return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*> 430 (data); 431 } 432 433 /// \brief Returns the current state of the variant. 434 /// 435 /// Returns the current state of the variant. 436 int state() const { 437 return flag; 438 } 439 440 private: 441 442 char data[_variant_bits::Size<num - 1, TypeMap>::value]; 443 int flag; 444 }; 445 446 namespace _variant_bits { 447 448 template <int _index, typename _List> 449 struct Get { 450 typedef typename Get<_index - 1, typename _List::Next>::Type Type; 451 }; 452 453 template <typename _List> 454 struct Get<0, _List> { 455 typedef typename _List::Type Type; 456 }; 457 458 struct List {}; 459 460 template <typename _Type, typename _List> 461 struct Insert { 462 typedef _List Next; 463 typedef _Type Type; 464 }; 465 466 template <int _idx, typename _T0, typename _T1, typename _T2, 467 typename _T3, typename _T5, typename _T4, typename _T6, 468 typename _T7, typename _T8, typename _T9> 469 struct Mapper { 470 typedef List L10; 471 typedef Insert<_T9, L10> L9; 472 typedef Insert<_T8, L9> L8; 473 typedef Insert<_T7, L8> L7; 474 typedef Insert<_T6, L7> L6; 475 typedef Insert<_T5, L6> L5; 476 typedef Insert<_T4, L5> L4; 477 typedef Insert<_T3, L4> L3; 478 typedef Insert<_T2, L3> L2; 479 typedef Insert<_T1, L2> L1; 480 typedef Insert<_T0, L1> L0; 481 typedef typename Get<_idx, L0>::Type Type; 482 }; 483 484 } 485 486 /// \brief Helper class for Variant 487 /// 488 /// Helper class to define type mappings for Variant. This class 489 /// converts the template parameters to be mappable by integer. 490 /// \see Variant 491 template < 492 typename _T0, 493 typename _T1 = void, typename _T2 = void, typename _T3 = void, 494 typename _T5 = void, typename _T4 = void, typename _T6 = void, 495 typename _T7 = void, typename _T8 = void, typename _T9 = void> 496 struct VariantTypeMap { 497 template <int _idx> 498 struct Map { 499 typedef typename _variant_bits:: 500 Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type 501 Type; 502 }; 503 }; 504 505 } 506 507 508 #endif -
new file lemon/digraph_adaptor.h
diff -r b6732e0d38c5 -r e67acd83a9ca lemon/digraph_adaptor.h
- + 1 /* -*- C++ -*- 2 * 3 * This file is a part of LEMON, a generic C++ optimization library 4 * 5 * Copyright (C) 2003-2008 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 19 #ifndef LEMON_DIGRAPH_ADAPTOR_H 20 #define LEMON_DIGRAPH_ADAPTOR_H 21 22 ///\ingroup graph_adaptors 23 ///\file 24 ///\brief Several digraph adaptors. 25 /// 26 ///This file contains several useful digraph adaptor functions. 27 28 #include <lemon/core.h> 29 #include <lemon/maps.h> 30 #include <lemon/bits/variant.h> 31 32 #include <lemon/bits/base_extender.h> 33 #include <lemon/bits/graph_adaptor_extender.h> 34 #include <lemon/bits/graph_extender.h> 35 #include <lemon/tolerance.h> 36 37 #include <algorithm> 38 39 namespace lemon { 40 41 ///\brief Base type for the Digraph Adaptors 42 /// 43 ///Base type for the Digraph Adaptors 44 /// 45 ///This is the base type for most of LEMON digraph adaptors. This 46 ///class implements a trivial digraph adaptor i.e. it only wraps the 47 ///functions and types of the digraph. The purpose of this class is 48 ///to make easier implementing digraph adaptors. E.g. if an adaptor 49 ///is considered which differs from the wrapped digraph only in some 50 ///of its functions or types, then it can be derived from 51 ///DigraphAdaptor, and only the differences should be implemented. 52 template<typename _Digraph> 53 class DigraphAdaptorBase { 54 public: 55 typedef _Digraph Digraph; 56 typedef DigraphAdaptorBase Adaptor; 57 typedef Digraph ParentDigraph; 58 59 protected: 60 Digraph* _digraph; 61 DigraphAdaptorBase() : _digraph(0) { } 62 void setDigraph(Digraph& digraph) { _digraph = &digraph; } 63 64 public: 65 DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { } 66 67 typedef typename Digraph::Node Node; 68 typedef typename Digraph::Arc Arc; 69 70 void first(Node& i) const { _digraph->first(i); } 71 void first(Arc& i) const { _digraph->first(i); } 72 void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } 73 void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } 74 75 void next(Node& i) const { _digraph->next(i); } 76 void next(Arc& i) const { _digraph->next(i); } 77 void nextIn(Arc& i) const { _digraph->nextIn(i); } 78 void nextOut(Arc& i) const { _digraph->nextOut(i); } 79 80 Node source(const Arc& a) const { return _digraph->source(a); } 81 Node target(const Arc& a) const { return _digraph->target(a); } 82 83 typedef NodeNumTagIndicator<Digraph> NodeNumTag; 84 int nodeNum() const { return _digraph->nodeNum(); } 85 86 typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; 87 int arcNum() const { return _digraph->arcNum(); } 88 89 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; 90 Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { 91 return _digraph->findArc(u, v, prev); 92 } 93 94 Node addNode() { return _digraph->addNode(); } 95 Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } 96 97 void erase(const Node& n) const { _digraph->erase(n); } 98 void erase(const Arc& a) const { _digraph->erase(a); } 99 100 void clear() const { _digraph->clear(); } 101 102 int id(const Node& n) const { return _digraph->id(n); } 103 int id(const Arc& a) const { return _digraph->id(a); } 104 105 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } 106 Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } 107 108 int maxNodeId() const { return _digraph->maxNodeId(); } 109 int maxArcId() const { return _digraph->maxArcId(); } 110 111 typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; 112 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } 113 114 typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier; 115 ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } 116 117 template <typename _Value> 118 class NodeMap : public Digraph::template NodeMap<_Value> { 119 public: 120 121 typedef typename Digraph::template NodeMap<_Value> Parent; 122 123 explicit NodeMap(const Adaptor& adaptor) 124 : Parent(*adaptor._digraph) {} 125 126 NodeMap(const Adaptor& adaptor, const _Value& value) 127 : Parent(*adaptor._digraph, value) { } 128 129 NodeMap& operator=(const NodeMap& cmap) { 130 return operator=<NodeMap>(cmap); 131 } 132 133 template <typename CMap> 134 NodeMap& operator=(const CMap& cmap) { 135 Parent::operator=(cmap); 136 return *this; 137 } 138 139 }; 140 141 template <typename _Value> 142 class ArcMap : public Digraph::template ArcMap<_Value> { 143 public: 144 145 typedef typename Digraph::template ArcMap<_Value> Parent; 146 147 explicit ArcMap(const Adaptor& adaptor) 148 : Parent(*adaptor._digraph) {} 149 150 ArcMap(const Adaptor& adaptor, const _Value& value) 151 : Parent(*adaptor._digraph, value) {} 152 153 ArcMap& operator=(const ArcMap& cmap) { 154 return operator=<ArcMap>(cmap); 155 } 156 157 template <typename CMap> 158 ArcMap& operator=(const CMap& cmap) { 159 Parent::operator=(cmap); 160 return *this; 161 } 162 163 }; 164 165 }; 166 167 ///\ingroup graph_adaptors 168 /// 169 ///\brief Trivial Digraph Adaptor 170 /// 171 /// This class is an adaptor which does not change the adapted 172 /// digraph. It can be used only to test the digraph adaptors. 173 template <typename _Digraph> 174 class DigraphAdaptor : 175 public DigraphAdaptorExtender<DigraphAdaptorBase<_Digraph> > { 176 public: 177 typedef _Digraph Digraph; 178 typedef DigraphAdaptorExtender<DigraphAdaptorBase<_Digraph> > Parent; 179 protected: 180 DigraphAdaptor() : Parent() { } 181 182 public: 183 explicit DigraphAdaptor(Digraph& digraph) { setDigraph(digraph); } 184 }; 185 186 /// \brief Just gives back a digraph adaptor 187 /// 188 /// Just gives back a digraph adaptor which 189 /// should be provide original digraph 190 template<typename Digraph> 191 DigraphAdaptor<const Digraph> 192 digraphAdaptor(const Digraph& digraph) { 193 return DigraphAdaptor<const Digraph>(digraph); 194 } 195 196 197 template <typename _Digraph> 198 class RevDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { 199 public: 200 typedef _Digraph Digraph; 201 typedef DigraphAdaptorBase<_Digraph> Parent; 202 protected: 203 RevDigraphAdaptorBase() : Parent() { } 204 public: 205 typedef typename Parent::Node Node; 206 typedef typename Parent::Arc Arc; 207 208 void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } 209 void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } 210 211 void nextIn(Arc& a) const { Parent::nextOut(a); } 212 void nextOut(Arc& a) const { Parent::nextIn(a); } 213 214 Node source(const Arc& a) const { return Parent::target(a); } 215 Node target(const Arc& a) const { return Parent::source(a); } 216 217 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; 218 Arc findArc(const Node& u, const Node& v, 219 const Arc& prev = INVALID) { 220 return Parent::findArc(v, u, prev); 221 } 222 223 }; 224 225 226 ///\ingroup graph_adaptors 227 /// 228 ///\brief A digraph adaptor which reverses the orientation of the arcs. 229 /// 230 /// If \c g is defined as 231 ///\code 232 /// ListDigraph g; 233 ///\endcode 234 /// then 235 ///\code 236 /// RevDigraphAdaptor<ListDigraph> ga(g); 237 ///\endcode 238 /// implements the digraph obtained from \c g by 239 /// reversing the orientation of its arcs. 240 /// 241 /// A good example of using RevDigraphAdaptor is to decide that the 242 /// directed graph is wheter strongly connected or not. If from one 243 /// node each node is reachable and from each node is reachable this 244 /// node then and just then the digraph is strongly 245 /// connected. Instead of this condition we use a little bit 246 /// different. From one node each node ahould be reachable in the 247 /// digraph and in the reversed digraph. Now this condition can be 248 /// checked with the Dfs algorithm class and the RevDigraphAdaptor 249 /// algorithm class. 250 /// 251 /// And look at the code: 252 /// 253 ///\code 254 /// bool stronglyConnected(const Digraph& digraph) { 255 /// if (NodeIt(digraph) == INVALID) return true; 256 /// Dfs<Digraph> dfs(digraph); 257 /// dfs.run(NodeIt(digraph)); 258 /// for (NodeIt it(digraph); it != INVALID; ++it) { 259 /// if (!dfs.reached(it)) { 260 /// return false; 261 /// } 262 /// } 263 /// typedef RevDigraphAdaptor<const Digraph> RDigraph; 264 /// RDigraph rdigraph(digraph); 265 /// DfsVisit<RDigraph> rdfs(rdigraph); 266 /// rdfs.run(NodeIt(digraph)); 267 /// for (NodeIt it(digraph); it != INVALID; ++it) { 268 /// if (!rdfs.reached(it)) { 269 /// return false; 270 /// } 271 /// } 272 /// return true; 273 /// } 274 ///\endcode 275 template<typename _Digraph> 276 class RevDigraphAdaptor : 277 public DigraphAdaptorExtender<RevDigraphAdaptorBase<_Digraph> > { 278 public: 279 typedef _Digraph Digraph; 280 typedef DigraphAdaptorExtender< 281 RevDigraphAdaptorBase<_Digraph> > Parent; 282 protected: 283 RevDigraphAdaptor() { } 284 public: 285 explicit RevDigraphAdaptor(Digraph& digraph) { 286 Parent::setDigraph(digraph); 287 } 288 }; 289 290 /// \brief Just gives back a reverse digraph adaptor 291 /// 292 /// Just gives back a reverse digraph adaptor 293 template<typename Digraph> 294 RevDigraphAdaptor<const Digraph> 295 revDigraphAdaptor(const Digraph& digraph) { 296 return RevDigraphAdaptor<const Digraph>(digraph); 297 } 298 299 template <typename _Digraph, typename _NodeFilterMap, 300 typename _ArcFilterMap, bool checked = true> 301 class SubDigraphAdaptorBase : public DigraphAdaptorBase<_Digraph> { 302 public: 303 typedef _Digraph Digraph; 304 typedef _NodeFilterMap NodeFilterMap; 305 typedef _ArcFilterMap ArcFilterMap; 306 307 typedef SubDigraphAdaptorBase Adaptor; 308 typedef DigraphAdaptorBase<_Digraph> Parent; 309 protected: 310 NodeFilterMap* _node_filter; 311 ArcFilterMap* _arc_filter; 312 SubDigraphAdaptorBase() 313 : Parent(), _node_filter(0), _arc_filter(0) { } 314 315 void setNodeFilterMap(NodeFilterMap& node_filter) { 316 _node_filter = &node_filter; 317 } 318 void setArcFilterMap(ArcFilterMap& arc_filter) { 319 _arc_filter = &arc_filter; 320 } 321 322 public: 323 324 typedef typename Parent::Node Node; 325 typedef typename Parent::Arc Arc; 326 327 void first(Node& i) const { 328 Parent::first(i); 329 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); 330 } 331 332 void first(Arc& i) const { 333 Parent::first(i); 334 while (i != INVALID && (!(*_arc_filter)[i] 335 || !(*_node_filter)[Parent::source(i)] 336 || !(*_node_filter)[Parent::target(i)])) Parent::next(i); 337 } 338 339 void firstIn(Arc& i, const Node& n) const { 340 Parent::firstIn(i, n); 341 while (i != INVALID && (!(*_arc_filter)[i] 342 || !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); 343 } 344 345 void firstOut(Arc& i, const Node& n) const { 346 Parent::firstOut(i, n); 347 while (i != INVALID && (!(*_arc_filter)[i] 348 || !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); 349 } 350 351 void next(Node& i) const { 352 Parent::next(i); 353 while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); 354 } 355 356 void next(Arc& i) const { 357 Parent::next(i); 358 while (i != INVALID && (!(*_arc_filter)[i] 359 || !(*_node_filter)[Parent::source(i)] 360 || !(*_node_filter)[Parent::target(i)])) Parent::next(i); 361 } 362 363 void nextIn(Arc& i) const { 364 Parent::nextIn(i); 365 while (i != INVALID && (!(*_arc_filter)[i] 366 || !(*_node_filter)[Parent::source(i)])) Parent::nextIn(i); 367 } 368 369 void nextOut(Arc& i) const { 370 Parent::nextOut(i); 371 while (i != INVALID && (!(*_arc_filter)[i] 372 || !(*_node_filter)[Parent::target(i)])) Parent::nextOut(i); 373 } 374 375 ///\e 376 377 /// This function hides \c n in the digraph, i.e. the iteration 378 /// jumps over it. This is done by simply setting the value of \c n 379 /// to be false in the corresponding node-map. 380 void hide(const Node& n) const { _node_filter->set(n, false); } 381 382 ///\e 383 384 /// This function hides \c a in the digraph, i.e. the iteration 385 /// jumps over it. This is done by simply setting the value of \c a 386 /// to be false in the corresponding arc-map. 387 void hide(const Arc& a) const { _arc_filter->set(a, false); } 388 389 ///\e 390 391 /// The value of \c n is set to be true in the node-map which stores 392 /// hide information. If \c n was hidden previuosly, then it is shown 393 /// again 394 void unHide(const Node& n) const { _node_filter->set(n, true); } 395 396 ///\e 397 398 /// The value of \c a is set to be true in the arc-map which stores 399 /// hide information. If \c a was hidden previuosly, then it is shown 400 /// again 401 void unHide(const Arc& a) const { _arc_filter->set(a, true); } 402 403 /// Returns true if \c n is hidden. 404 405 ///\e 406 /// 407 bool hidden(const Node& n) const { return !(*_node_filter)[n]; } 408 409 /// Returns true if \c a is hidden. 410 411 ///\e 412 /// 413 bool hidden(const Arc& a) const { return !(*_arc_filter)[a]; } 414 415 typedef False NodeNumTag; 416 typedef False EdgeNumTag; 417 418 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; 419 Arc findArc(const Node& source, const Node& target, 420 const Arc& prev = INVALID) { 421 if (!(*_node_filter)[source] || !(*_node_filter)[target]) { 422 return INVALID; 423 } 424 Arc arc = Parent::findArc(source, target, prev); 425 while (arc != INVALID && !(*_arc_filter)[arc]) { 426 arc = Parent::findArc(source, target, arc); 427 } 428 return arc; 429 } 430 431 template <typename _Value> 432 class NodeMap : public SubMapExtender<Adaptor, 433 typename Parent::template NodeMap<_Value> > { 434 public: 435 typedef _Value Value; 436 typedef SubMapExtender<Adaptor, typename Parent:: 437 template NodeMap<Value> > MapParent; 438 439 NodeMap(const Adaptor& adaptor) 440 : MapParent(adaptor) {} 441 NodeMap(const Adaptor& adaptor, const Value& value) 442 : MapParent(adaptor, value) {} 443 444 NodeMap& operator=(const NodeMap& cmap) { 445 return operator=<NodeMap>(cmap); 446 } 447 448 template <typename CMap> 449 NodeMap& operator=(const CMap& cmap) { 450 MapParent::operator=(cmap); 451 return *this; 452 } 453 }; 454 455 template <typename _Value> 456 class ArcMap : public SubMapExtender<Adaptor, 457 typename Parent::template ArcMap<_Value> > { 458 public: 459 typedef _Value Value; 460 typedef SubMapExtender<Adaptor, typename Parent:: 461 template ArcMap<Value> > MapParent; 462 463 ArcMap(const Adaptor& adaptor) 464 : MapParent(adaptor) {} 465 ArcMap(const Adaptor& adaptor, const Value& value) 466 : MapParent(adaptor, value) {} 467 468 ArcMap& operator=(const ArcMap& cmap) { 469 return operator=<ArcMap>(cmap); 470 } 471 472 template <typename CMap> 473 ArcMap& operator=(const CMap& cmap) { 474 MapParent::operator=(cmap); 475 return *this; 476 } 477 }; 478 479 }; 480 481 template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap> 482 class SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false> 483 : public DigraphAdaptorBase<_Digraph> { 484 public: 485 typedef _Digraph Digraph; 486 typedef _NodeFilterMap NodeFilterMap; 487 typedef _ArcFilterMap ArcFilterMap; 488 489 typedef SubDigraphAdaptorBase Adaptor; 490 typedef DigraphAdaptorBase<Digraph> Parent; 491 protected: 492 NodeFilterMap* _node_filter; 493 ArcFilterMap* _arc_filter; 494 SubDigraphAdaptorBase() 495 : Parent(), _node_filter(0), _arc_filter(0) { } 496 497 void setNodeFilterMap(NodeFilterMap& node_filter) { 498 _node_filter = &node_filter; 499 } 500 void setArcFilterMap(ArcFilterMap& arc_filter) { 501 _arc_filter = &arc_filter; 502 } 503 504 public: 505 506 typedef typename Parent::Node Node; 507 typedef typename Parent::Arc Arc; 508 509 void first(Node& i) const { 510 Parent::first(i); 511 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); 512 } 513 514 void first(Arc& i) const { 515 Parent::first(i); 516 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); 517 } 518 519 void firstIn(Arc& i, const Node& n) const { 520 Parent::firstIn(i, n); 521 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); 522 } 523 524 void firstOut(Arc& i, const Node& n) const { 525 Parent::firstOut(i, n); 526 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); 527 } 528 529 void next(Node& i) const { 530 Parent::next(i); 531 while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); 532 } 533 void next(Arc& i) const { 534 Parent::next(i); 535 while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); 536 } 537 void nextIn(Arc& i) const { 538 Parent::nextIn(i); 539 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); 540 } 541 542 void nextOut(Arc& i) const { 543 Parent::nextOut(i); 544 while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); 545 } 546 547 ///\e 548 549 /// This function hides \c n in the digraph, i.e. the iteration 550 /// jumps over it. This is done by simply setting the value of \c n 551 /// to be false in the corresponding node-map. 552 void hide(const Node& n) const { _node_filter->set(n, false); } 553 554 ///\e 555 556 /// This function hides \c e in the digraph, i.e. the iteration 557 /// jumps over it. This is done by simply setting the value of \c e 558 /// to be false in the corresponding arc-map. 559 void hide(const Arc& e) const { _arc_filter->set(e, false); } 560 561 ///\e 562 563 /// The value of \c n is set to be true in the node-map which stores 564 /// hide information. If \c n was hidden previuosly, then it is shown 565 /// again 566 void unHide(const Node& n) const { _node_filter->set(n, true); } 567 568 ///\e 569 570 /// The value of \c e is set to be true in the arc-map which stores 571 /// hide information. If \c e was hidden previuosly, then it is shown 572 /// again 573 void unHide(const Arc& e) const { _arc_filter->set(e, true); } 574 575 /// Returns true if \c n is hidden. 576 577 ///\e 578 /// 579 bool hidden(const Node& n) const { return !(*_node_filter)[n]; } 580 581 /// Returns true if \c n is hidden. 582 583 ///\e 584 /// 585 bool hidden(const Arc& e) const { return !(*_arc_filter)[e]; } 586 587 typedef False NodeNumTag; 588 typedef False EdgeNumTag; 589 590 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; 591 Arc findArc(const Node& source, const Node& target, 592 const Arc& prev = INVALID) { 593 if (!(*_node_filter)[source] || !(*_node_filter)[target]) { 594 return INVALID; 595 } 596 Arc arc = Parent::findArc(source, target, prev); 597 while (arc != INVALID && !(*_arc_filter)[arc]) { 598 arc = Parent::findArc(source, target, arc); 599 } 600 return arc; 601 } 602 603 template <typename _Value> 604 class NodeMap : public SubMapExtender<Adaptor, 605 typename Parent::template NodeMap<_Value> > { 606 public: 607 typedef _Value Value; 608 typedef SubMapExtender<Adaptor, typename Parent:: 609 template NodeMap<Value> > MapParent; 610 611 NodeMap(const Adaptor& adaptor) 612 : MapParent(adaptor) {} 613 NodeMap(const Adaptor& adaptor, const Value& value) 614 : MapParent(adaptor, value) {} 615 616 NodeMap& operator=(const NodeMap& cmap) { 617 return operator=<NodeMap>(cmap); 618 } 619 620 template <typename CMap> 621 NodeMap& operator=(const CMap& cmap) { 622 MapParent::operator=(cmap); 623 return *this; 624 } 625 }; 626 627 template <typename _Value> 628 class ArcMap : public SubMapExtender<Adaptor, 629 typename Parent::template ArcMap<_Value> > { 630 public: 631 typedef _Value Value; 632 typedef SubMapExtender<Adaptor, typename Parent:: 633 template ArcMap<Value> > MapParent; 634 635 ArcMap(const Adaptor& adaptor) 636 : MapParent(adaptor) {} 637 ArcMap(const Adaptor& adaptor, const Value& value) 638 : MapParent(adaptor, value) {} 639 640 ArcMap& operator=(const ArcMap& cmap) { 641 return operator=<ArcMap>(cmap); 642 } 643 644 template <typename CMap> 645 ArcMap& operator=(const CMap& cmap) { 646 MapParent::operator=(cmap); 647 return *this; 648 } 649 }; 650 651 }; 652 653 /// \ingroup graph_adaptors 654 /// 655 /// \brief A digraph adaptor for hiding nodes and arcs from a digraph. 656 /// 657 /// SubDigraphAdaptor shows the digraph with filtered node-set and 658 /// arc-set. If the \c checked parameter is true then it filters the arcset 659 /// to do not get invalid arcs without source or target. 660 /// Let \f$ G=(V, A) \f$ be a directed digraph 661 /// and suppose that the digraph instance \c g of type ListDigraph 662 /// implements \f$ G \f$. 663 /// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp. 664 /// on the node-set and arc-set. 665 /// SubDigraphAdaptor<...>::NodeIt iterates 666 /// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and 667 /// SubDigraphAdaptor<...>::ArcIt iterates 668 /// on the arc-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly, 669 /// SubDigraphAdaptor<...>::OutArcIt and 670 /// SubDigraphAdaptor<...>::InArcIt iterates 671 /// only on arcs leaving and entering a specific node which have true value. 672 /// 673 /// If the \c checked template parameter is false then we have to 674 /// note that the node-iterator cares only the filter on the 675 /// node-set, and the arc-iterator cares only the filter on the 676 /// arc-set. This way the arc-map should filter all arcs which's 677 /// source or target is filtered by the node-filter. 678 ///\code 679 /// typedef ListDigraph Digraph; 680 /// DIGRAPH_TYPEDEFS(Digraph); 681 /// Digraph g; 682 /// Node u=g.addNode(); //node of id 0 683 /// Node v=g.addNode(); //node of id 1 684 /// Arc a=g.addArc(u, v); //arc of id 0 685 /// Arc f=g.addArc(v, u); //arc of id 1 686 /// BoolNodeMap nm(g, true); 687 /// nm.set(u, false); 688 /// BoolArcMap am(g, true); 689 /// am.set(a, false); 690 /// typedef SubDigraphAdaptor<Digraph, BoolNodeMap, BoolArcMap> SubGA; 691 /// SubGA ga(g, nm, am); 692 /// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) 693 /// std::cout << g.id(n) << std::endl; 694 /// std::cout << ":-)" << std::endl; 695 /// for (SubGA::ArcIt a(ga); a!=INVALID; ++a) 696 /// std::cout << g.id(a) << std::endl; 697 ///\endcode 698 /// The output of the above code is the following. 699 ///\code 700 /// 1 701 /// :-) 702 /// 1 703 ///\endcode 704 /// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to 705 /// \c Digraph::Node that is why \c g.id(n) can be applied. 706 /// 707 /// For other examples see also the documentation of 708 /// NodeSubDigraphAdaptor and ArcSubDigraphAdaptor. 709 template<typename _Digraph, 710 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, 711 typename _ArcFilterMap = typename _Digraph::template ArcMap<bool>, 712 bool checked = true> 713 class SubDigraphAdaptor : 714 public DigraphAdaptorExtender< 715 SubDigraphAdaptorBase<_Digraph, _NodeFilterMap, _ArcFilterMap, checked> > { 716 public: 717 typedef _Digraph Digraph; 718 typedef _NodeFilterMap NodeFilterMap; 719 typedef _ArcFilterMap ArcFilterMap; 720 721 typedef DigraphAdaptorExtender< 722 SubDigraphAdaptorBase<Digraph, NodeFilterMap, ArcFilterMap, checked> > 723 Parent; 724 725 protected: 726 SubDigraphAdaptor() { } 727 public: 728 729 SubDigraphAdaptor(Digraph& digraph, NodeFilterMap& node_filter, 730 ArcFilterMap& arc_filter) { 731 setDigraph(digraph); 732 setNodeFilterMap(node_filter); 733 setArcFilterMap(arc_filter); 734 } 735 736 }; 737 738 /// \brief Just gives back a sub digraph adaptor 739 /// 740 /// Just gives back a sub digraph adaptor 741 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> 742 SubDigraphAdaptor<const Digraph, NodeFilterMap, ArcFilterMap> 743 subDigraphAdaptor(const Digraph& digraph, 744 NodeFilterMap& nfm, ArcFilterMap& afm) { 745 return SubDigraphAdaptor<const Digraph, NodeFilterMap, ArcFilterMap> 746 (digraph, nfm, afm); 747 } 748 749 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> 750 SubDigraphAdaptor<const Digraph, const NodeFilterMap, ArcFilterMap> 751 subDigraphAdaptor(const Digraph& digraph, 752 NodeFilterMap& nfm, ArcFilterMap& afm) { 753 return SubDigraphAdaptor<const Digraph, const NodeFilterMap, ArcFilterMap> 754 (digraph, nfm, afm); 755 } 756 757 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> 758 SubDigraphAdaptor<const Digraph, NodeFilterMap, const ArcFilterMap> 759 subDigraphAdaptor(const Digraph& digraph, 760 NodeFilterMap& nfm, ArcFilterMap& afm) { 761 return SubDigraphAdaptor<const Digraph, NodeFilterMap, const ArcFilterMap> 762 (digraph, nfm, afm); 763 } 764 765 template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> 766 SubDigraphAdaptor<const Digraph, const NodeFilterMap, const ArcFilterMap> 767 subDigraphAdaptor(const Digraph& digraph, 768 NodeFilterMap& nfm, ArcFilterMap& afm) { 769 return SubDigraphAdaptor<const Digraph, const NodeFilterMap, 770 const ArcFilterMap>(digraph, nfm, afm); 771 } 772 773 774 775 ///\ingroup graph_adaptors 776 /// 777 ///\brief An adaptor for hiding nodes from a digraph. 778 /// 779 ///An adaptor for hiding nodes from a digraph. This adaptor 780 ///specializes SubDigraphAdaptor in the way that only the node-set 781 ///can be filtered. In usual case the checked parameter is true, we 782 ///get the induced subgraph. But if the checked parameter is false 783 ///then we can filter only isolated nodes. 784 template<typename _Digraph, 785 typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, 786 bool checked = true> 787 class NodeSubDigraphAdaptor : 788 public SubDigraphAdaptor<_Digraph, _NodeFilterMap, 789 ConstMap<typename _Digraph::Arc, bool>, checked> { 790 public: 791 792 typedef _Digraph Digraph; 793 typedef _NodeFilterMap NodeFilterMap; 794 795 typedef SubDigraphAdaptor<Digraph, NodeFilterMap, 796 ConstMap<typename Digraph::Arc, bool>, checked> 797 Parent; 798 799 protected: 800 ConstMap<typename Digraph::Arc, bool> const_true_map; 801 802 NodeSubDigraphAdaptor() : const_true_map(true) { 803 Parent::setArcFilterMap(const_true_map); 804 } 805 806 public: 807 808 NodeSubDigraphAdaptor(Digraph& _digraph, NodeFilterMap& node_filter) : 809 Parent(), const_true_map(true) { 810 Parent::setDigraph(_digraph); 811 Parent::setNodeFilterMap(node_filter); 812 Parent::setArcFilterMap(const_true_map); 813 } 814 815 }; 816 817 818 /// \brief Just gives back a \c NodeSubDigraphAdaptor 819 /// 820 /// Just gives back a \c NodeSubDigraphAdaptor 821 template<typename Digraph, typename NodeFilterMap> 822 NodeSubDigraphAdaptor<const Digraph, NodeFilterMap> 823 nodeSubDigraphAdaptor(const Digraph& digraph, NodeFilterMap& nfm) { 824 return NodeSubDigraphAdaptor<const Digraph, NodeFilterMap>(digraph, nfm); 825 } 826 827 template<typename Digraph, typename NodeFilterMap> 828 NodeSubDigraphAdaptor<const Digraph, const NodeFilterMap> 829 nodeSubDigraphAdaptor(const Digraph& digraph, const NodeFilterMap& nfm) { 830 return NodeSubDigraphAdaptor<const Digraph, const NodeFilterMap> 831 (digraph, nfm); 832 } 833 834 ///\ingroup graph_adaptors 835 /// 836 ///\brief An adaptor for hiding arcs from a digraph. 837 /// 838 ///An adaptor for hiding arcs from a digraph. This adaptor 839 ///specializes SubDigraphAdaptor in the way that only the arc-set 840 ///can be filtered. The usefulness of this adaptor is demonstrated 841 ///in the problem of searching a maximum number of arc-disjoint 842 ///shortest paths between two nodes \c s and \c t. Shortest here 843 ///means being shortest w.r.t. non-negative arc-lengths. Note that 844 ///the comprehension of the presented solution need's some 845 ///elementary knowlarc from combinatorial optimization. 846 /// 847 ///If a single shortest path is to be searched between \c s and \c 848 ///t, then this can be done easily by applying the Dijkstra 849 ///algorithm. What happens, if a maximum number of arc-disjoint 850 ///shortest paths is to be computed. It can be proved that an arc 851 ///can be in a shortest path if and only if it is tight with respect 852 ///to the potential function computed by Dijkstra. Moreover, any 853 ///path containing only such arcs is a shortest one. Thus we have 854 ///to compute a maximum number of arc-disjoint paths between \c s 855 ///and \c t in the digraph which has arc-set all the tight arcs. The 856 ///computation will be demonstrated on the following digraph, which 857 ///is read from the dimacs file \c sub_digraph_adaptor_demo.dim. 858 ///The full source code is available in \ref 859 ///sub_digraph_adaptor_demo.cc. If you are interested in more demo 860 ///programs, you can use \ref dim_to_dot.cc to generate .dot files 861 ///from dimacs files. The .dot file of the following figure was 862 ///generated by the demo program \ref dim_to_dot.cc. 863 /// 864 ///\dot 865 ///didigraph lemon_dot_example { 866 ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; 867 ///n0 [ label="0 (s)" ]; 868 ///n1 [ label="1" ]; 869 ///n2 [ label="2" ]; 870 ///n3 [ label="3" ]; 871 ///n4 [ label="4" ]; 872 ///n5 [ label="5" ]; 873 ///n6 [ label="6 (t)" ]; 874 ///arc [ shape=ellipse, fontname=Helvetica, fontsize=10 ]; 875 ///n5 -> n6 [ label="9, length:4" ]; 876 ///n4 -> n6 [ label="8, length:2" ]; 877 ///n3 -> n5 [ label="7, length:1" ]; 878 ///n2 -> n5 [ label="6, length:3" ]; 879 ///n2 -> n6 [ label="5, length:5" ]; 880 ///n2 -> n4 [ label="4, length:2" ]; 881 ///n1 -> n4 [ label="3, length:3" ]; 882 ///n0 -> n3 [ label="2, length:1" ]; 883 ///n0 -> n2 [ label="1, length:2" ]; 884 ///n0 -> n1 [ label="0, length:3" ]; 885 ///} 886 ///\enddot 887 /// 888 ///\code 889 ///Digraph g; 890 ///Node s, t; 891 ///LengthMap length(g); 892 /// 893 ///readDimacs(std::cin, g, length, s, t); 894 /// 895 ///cout << "arcs with lengths (of form id, source--length->target): " << endl; 896 ///for(ArcIt e(g); e!=INVALID; ++e) 897 /// cout << g.id(e) << ", " << g.id(g.source(e)) << "--" 898 /// << length[e] << "->" << g.id(g.target(e)) << endl; 899 /// 900 ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl; 901 ///\endcode 902 ///Next, the potential function is computed with Dijkstra. 903 ///\code 904 ///typedef Dijkstra<Digraph, LengthMap> Dijkstra; 905 ///Dijkstra dijkstra(g, length); 906 ///dijkstra.run(s); 907 ///\endcode 908 ///Next, we consrtruct a map which filters the arc-set to the tight arcs. 909 ///\code 910 ///typedef TightArcFilterMap<Digraph, const Dijkstra::DistMap, LengthMap> 911 /// TightArcFilter; 912 ///TightArcFilter tight_arc_filter(g, dijkstra.distMap(), length); 913 /// 914 ///typedef ArcSubDigraphAdaptor<Digraph, TightArcFilter> SubGA; 915 ///SubGA ga(g, tight_arc_filter); 916 ///\endcode 917 ///Then, the maximum nimber of arc-disjoint \c s-\c t paths are computed 918 ///with a max flow algorithm Preflow. 919 ///\code 920 ///ConstMap<Arc, int> const_1_map(1); 921 ///Digraph::ArcMap<int> flow(g, 0); 922 /// 923 ///Preflow<SubGA, ConstMap<Arc, int>, Digraph::ArcMap<int> > 924 /// preflow(ga, const_1_map, s, t); 925 ///preflow.run(); 926 ///\endcode 927 ///Last, the output is: 928 ///\code 929 ///cout << "maximum number of arc-disjoint shortest path: " 930 /// << preflow.flowValue() << endl; 931 ///cout << "arcs of the maximum number of arc-disjoint shortest s-t paths: " 932 /// << endl; 933 ///for(ArcIt e(g); e!=INVALID; ++e) 934 /// if (preflow.flow(e)) 935 /// cout << " " << g.id(g.source(e)) << "--" 936 /// << length[e] << "->" << g.id(g.target(e)) << endl; 937 ///\endcode 938 ///The program has the following (expected :-)) output: 939 ///\code 940 ///arcs with lengths (of form id, source--length->target): 941 /// 9, 5--4->6 942 /// 8, 4--2->6 943 /// 7, 3--1->5 944 /// 6, 2--3->5 945 /// 5, 2--5->6 946 /// 4, 2--2->4 947 /// 3, 1--3->4 948 /// 2, 0--1->3 949 /// 1, 0--2->2 950 /// 0, 0--3->1 951 ///s: 0 t: 6 952 ///maximum number of arc-disjoint shortest path: 2 953 ///arcs of the maximum number of arc-disjoint shortest s-t paths: 954 /// 9, 5--4->6 955 /// 8, 4--2->6 956 /// 7, 3--1->5 957 /// 4, 2--2->4 958 /// 2, 0--1->3 959 /// 1, 0--2->2 960 ///\endcode 961 template<typename _Digraph, typename _ArcFilterMap> 962 class ArcSubDigraphAdaptor : 963 public SubDigraphAdaptor<_Digraph, ConstMap<typename _Digraph::Node, bool>, 964 _ArcFilterMap, false> { 965 public: 966 typedef _Digraph Digraph; 967 typedef _ArcFilterMap ArcFilterMap; 968 969 typedef SubDigraphAdaptor<Digraph, ConstMap<typename Digraph::Node, bool>, 970 ArcFilterMap, false> Parent; 971 protected: 972 ConstMap<typename Digraph::Node, bool> const_true_map; 973 974 ArcSubDigraphAdaptor() : const_true_map(true) { 975 Parent::setNodeFilterMap(const_true_map); 976 } 977 978 public: 979 980 ArcSubDigraphAdaptor(Digraph& digraph, ArcFilterMap& arc_filter) 981 : Parent(), const_true_map(true) { 982 Parent::setDigraph(digraph); 983 Parent::setNodeFilterMap(const_true_map); 984 Parent::setArcFilterMap(arc_filter); 985 } 986 987 }; 988 989 /// \brief Just gives back an arc sub digraph adaptor 990 /// 991 /// Just gives back an arc sub digraph adaptor 992 template<typename Digraph, typename ArcFilterMap> 993 ArcSubDigraphAdaptor<const Digraph, ArcFilterMap> 994 arcSubDigraphAdaptor(const Digraph& digraph, ArcFilterMap& afm) { 995 return ArcSubDigraphAdaptor<const Digraph, ArcFilterMap>(digraph, afm); 996 } 997 998 template<typename Digraph, typename ArcFilterMap> 999 ArcSubDigraphAdaptor<const Digraph, const ArcFilterMap> 1000 arcSubDigraphAdaptor(const Digraph& digraph, const ArcFilterMap& afm) { 1001 return ArcSubDigraphAdaptor<const Digraph, const ArcFilterMap> 1002 (digraph, afm); 1003 } 1004 1005 template <typename _Digraph> 1006 class UndirDigraphAdaptorBase { 1007 public: 1008 typedef _Digraph Digraph; 1009 typedef UndirDigraphAdaptorBase Adaptor; 1010 1011 typedef True UndirectedTag; 1012 1013 typedef typename Digraph::Arc Edge; 1014 typedef typename Digraph::Node Node; 1015 1016 class Arc : public Edge { 1017 friend class UndirDigraphAdaptorBase; 1018 protected: 1019 bool _forward; 1020 1021 Arc(const Edge& edge, bool forward) : 1022 Edge(edge), _forward(forward) {} 1023 1024 public: 1025 Arc() {} 1026 1027 Arc(Invalid) : Edge(INVALID), _forward(true) {} 1028 1029 bool operator==(const Arc &other) const { 1030 return _forward == other._forward && 1031 static_cast<const Edge&>(*this) == static_cast<const Edge&>(other); 1032 } 1033 bool operator!=(const Arc &other) const { 1034 return _forward != other._forward || 1035 static_cast<const Edge&>(*this) != static_cast<const Edge&>(other); 1036 } 1037 bool operator<(const Arc &other) const { 1038 return _forward < other._forward || 1039 (_forward == other._forward && 1040 static_cast<const Edge&>(*this) < static_cast<const Edge&>(other)); 1041 } 1042 }; 1043 1044 1045 1046 void first(Node& n) const { 1047 _digraph->first(n); 1048 } 1049 1050 void next(Node& n) const { 1051 _digraph->next(n); 1052 } 1053 1054 void first(Arc& a) const { 1055 _digraph->first(a); 1056 a._forward = true; 1057 } 1058 1059 void next(Arc& a) const { 1060 if (a._forward) { 1061 a._forward = false; 1062 } else { 1063 _digraph->next(a); 1064 a._forward = true; 1065 } 1066 } 1067 1068 void first(Edge& e) const { 1069 _digraph->first(e); 1070 } 1071 1072 void next(Edge& e) const { 1073 _digraph->next(e); 1074 } 1075 1076 void firstOut(Arc& a, const Node& n) const { 1077 _digraph->firstIn(a, n); 1078 if( static_cast<const Edge&>(a) != INVALID ) { 1079 a._forward = false; 1080 } else { 1081 _digraph->firstOut(a, n); 1082 a._forward = true; 1083 } 1084 } 1085 void nextOut(Arc &a) const { 1086 if (!a._forward) { 1087 Node n = _digraph->target(a); 1088 _digraph->nextIn(a); 1089 if (static_cast<const Edge&>(a) == INVALID ) { 1090 _digraph->firstOut(a, n); 1091 a._forward = true; 1092 } 1093 } 1094 else { 1095 _digraph->nextOut(a); 1096 } 1097 } 1098 1099 void firstIn(Arc &a, const Node &n) const { 1100 _digraph->firstOut(a, n); 1101 if (static_cast<const Edge&>(a) != INVALID ) { 1102 a._forward = false; 1103 } else { 1104 _digraph->firstIn(a, n); 1105 a._forward = true; 1106 } 1107 } 1108 void nextIn(Arc &a) const { 1109 if (!a._forward) { 1110 Node n = _digraph->source(a); 1111 _digraph->nextOut(a); 1112 if( static_cast<const Edge&>(a) == INVALID ) { 1113 _digraph->firstIn(a, n); 1114 a._forward = true; 1115 } 1116 } 1117 else { 1118 _digraph->nextIn(a); 1119 } 1120 } 1121 1122 void firstInc(Edge &e, bool &d, const Node &n) const { 1123 d = true; 1124 _digraph->firstOut(e, n); 1125 if (e != INVALID) return; 1126 d = false; 1127 _digraph->firstIn(e, n); 1128 } 1129 1130 void nextInc(Edge &e, bool &d) const { 1131 if (d) { 1132 Node s = _digraph->source(e); 1133 _digraph->nextOut(e); 1134 if (e != INVALID) return; 1135 d = false; 1136 _digraph->firstIn(e, s); 1137 } else { 1138 _digraph->nextIn(e); 1139 } 1140 } 1141 1142 Node u(const Edge& e) const { 1143 return _digraph->source(e); 1144 } 1145 1146 Node v(const Edge& e) const { 1147 return _digraph->target(e); 1148 } 1149 1150 Node source(const Arc &a) const { 1151 return a._forward ? _digraph->source(a) : _digraph->target(a); 1152 } 1153 1154 Node target(const Arc &a) const { 1155 return a._forward ? _digraph->target(a) : _digraph->source(a); 1156 } 1157 1158 static Arc direct(const Edge &e, bool d) { 1159 return Arc(e, d); 1160 } 1161 Arc direct(const Edge &e, const Node& n) const { 1162 return Arc(e, _digraph->source(e) == n); 1163 } 1164 1165 static bool direction(const Arc &a) { return a._forward; } 1166 1167 Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } 1168 Arc arcFromId(int ix) const { 1169 return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); 1170 } 1171 Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } 1172 1173 int id(const Node &n) const { return _digraph->id(n); } 1174 int id(const Arc &a) const { 1175 return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); 1176 } 1177 int id(const Edge &e) const { return _digraph->id(e); } 1178 1179 int maxNodeId() const { return _digraph->maxNodeId(); } 1180 int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } 1181 int maxEdgeId() const { return _digraph->maxArcId(); } 1182 1183 Node addNode() { return _digraph->addNode(); } 1184 Edge addEdge(const Node& u, const Node& v) { 1185 return _digraph->addArc(u, v); 1186 } 1187 1188 void erase(const Node& i) { _digraph->erase(i); } 1189 void erase(const Edge& i) { _digraph->erase(i); } 1190 1191 void clear() { _digraph->clear(); } 1192 1193 typedef NodeNumTagIndicator<Digraph> NodeNumTag; 1194 int nodeNum() const { return 2 * _digraph->arcNum(); } 1195 typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; 1196 int arcNum() const { return 2 * _digraph->arcNum(); } 1197 int edgeNum() const { return _digraph->arcNum(); } 1198 1199 typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; 1200 Arc findArc(Node s, Node t, Arc p = INVALID) const { 1201 if (p == INVALID) { 1202 Edge arc = _digraph->findArc(s, t); 1203 if (arc != INVALID) return direct(arc, true); 1204 arc = _digraph->findArc(t, s); 1205 if (arc != INVALID) return direct(arc, false); 1206 } else if (direction(p)) { 1207 Edge arc = _digraph->findArc(s, t, p); 1208 if (arc != INVALID) return direct(arc, true); 1209 arc = _digraph->findArc(t, s); 1210 if (arc != INVALID) return direct(arc, false); 1211 } else { 1212 Edge arc = _digraph->findArc(t, s, p); 1213 if (arc != INVALID) return direct(arc, false); 1214 } 1215 return INVALID; 1216 } 1217 1218 Edge findEdge(Node s, Node t, Edge p = INVALID) const { 1219 if (s != t) { 1220 if (p == INVALID) { 1221 Edge arc = _digraph->findArc(s, t); 1222 if (arc != INVALID) return arc; 1223 arc = _digraph->findArc(t, s); 1224 if (arc != INVALID) return arc; 1225 } else if (_digraph->s(p) == s) { 1226 Edge arc = _digraph->findArc(s, t, p); 1227 if (arc != INVALID) return arc; 1228 arc = _digraph->findArc(t, s); 1229 if (arc != INVALID) return arc; 1230 } else { 1231 Edge arc = _digraph->findArc(t, s, p); 1232 if (arc != INVALID) return arc; 1233 } 1234 } else { 1235 return _digraph->findArc(s, t, p); 1236 } 1237 return INVALID; 1238 } 1239 1240 private: 1241 1242 template <typename _Value> 1243 class ArcMapBase { 1244 private: 1245 1246 typedef typename Digraph::template ArcMap<_Value> MapImpl; 1247 1248 public: 1249 1250 typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; 1251 1252 typedef _Value Value; 1253 typedef Arc Key; 1254 1255 ArcMapBase(const Adaptor& adaptor) : 1256 _forward(*adaptor._digraph), _backward(*adaptor._digraph) {} 1257 1258 ArcMapBase(const Adaptor& adaptor, const Value& v) 1259 : _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {} 1260 1261 void set(const Arc& a, const Value& v) { 1262 if (direction(a)) { 1263 _forward.set(a, v); 1264 } else { 1265 _backward.set(a, v); 1266 } 1267 } 1268 1269 typename MapTraits<MapImpl>::ConstReturnValue 1270 operator[](const Arc& a) const { 1271 if (direction(a)) { 1272 return _forward[a]; 1273 } else { 1274 return _backward[a]; 1275 } 1276 } 1277 1278 typename MapTraits<MapImpl>::ReturnValue 1279 operator[](const Arc& a) { 1280 if (direction(a)) { 1281 return _forward[a]; 1282 } else { 1283 return _backward[a]; 1284 } 1285 } 1286 1287 protected: 1288 1289 MapImpl _forward, _backward; 1290 1291 }; 1292 1293 public: 1294 1295 template <typename _Value> 1296 class NodeMap : public Digraph::template NodeMap<_Value> { 1297 public: 1298 1299 typedef _Value Value; 1300 typedef typename Digraph::template NodeMap<Value> Parent; 1301 1302 explicit NodeMap(const Adaptor& adaptor) 1303 : Parent(*adaptor._digraph) {} 1304 1305 NodeMap(const Adaptor& adaptor, const _Value& value) 1306 : Parent(*adaptor._digraph, value) { } 1307 1308 NodeMap& operator=(const NodeMap& cmap) { 1309 return operator=<NodeMap>(cmap); 1310 } 1311 1312 template <typename CMap> 1313 NodeMap& operator=(const CMap& cmap) { 1314 Parent::operator=(cmap); 1315 return *this; 1316 } 1317 1318 }; 1319 1320 template <typename _Value> 1321 class ArcMap 1322 : public SubMapExtender<Adaptor, ArcMapBase<_Value> > 1323 { 1324 public: 1325 typedef _Value Value; 1326 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent; 1327 1328 ArcMap(const Adaptor& adaptor) 1329 : Parent(adaptor) {} 1330 1331 ArcMap(const Adaptor& adaptor, const Value& value) 1332 : Parent(adaptor, value) {} 1333 1334 ArcMap& operator=(const ArcMap& cmap) { 1335 return operator=<ArcMap>(cmap); 1336 } 1337 1338 template <typename CMap> 1339 ArcMap& operator=(const CMap& cmap) { 1340 Parent::operator=(cmap); 1341 return *this; 1342 } 1343 }; 1344 1345 template <typename _Value> 1346 class EdgeMap : public Digraph::template ArcMap<_Value> { 1347 public: 1348 1349 typedef _Value Value; 1350 typedef typename Digraph::template ArcMap<Value> Parent; 1351 1352 explicit EdgeMap(const Adaptor& adaptor) 1353 : Parent(*adaptor._digraph) {} 1354 1355 EdgeMap(const Adaptor& adaptor, const Value& value) 1356 : Parent(*adaptor._digraph, value) {} 1357 1358 EdgeMap& operator=(const EdgeMap& cmap) { 1359 return operator=<EdgeMap>(cmap); 1360 } 1361 1362 template <typename CMap> 1363 EdgeMap& operator=(const CMap& cmap) { 1364 Parent::operator=(cmap); 1365 return *this; 1366 } 1367 1368 }; 1369 1370 typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; 1371 NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } 1372 1373 protected: 1374 1375 UndirDigraphAdaptorBase() : _digraph(0) {} 1376 1377 Digraph* _digraph; 1378 1379 void setDigraph(Digraph& digraph) { 1380 _digraph = &digraph; 1381 } 1382 1383 }; 1384 1385 ///\ingroup graph_adaptors 1386 /// 1387 /// \brief An graph is made from a directed digraph by an adaptor 1388 /// 1389 /// This adaptor makes an undirected graph from a directed 1390 /// digraph. All arc of the underlying will be showed in the adaptor 1391 /// as an edge. Let's see an informal example about using 1392 /// this adaptor: 1393 /// 1394 /// There is a network of the streets of a town. Of course there are 1395 /// some one-way street in the town hence the network is a directed 1396 /// one. There is a crazy driver who go oppositely in the one-way 1397 /// street without moral sense. Of course he can pass this streets 1398 /// slower than the regular way, in fact his speed is half of the 1399 /// normal speed. How long should he drive to get from a source 1400 /// point to the target? Let see the example code which calculate it: 1401 /// 1402 /// \todo BadCode, SimpleMap does no exists 1403 ///\code 1404 /// typedef UndirDigraphAdaptor<Digraph> Graph; 1405 /// Graph graph(digraph); 1406 /// 1407 /// typedef SimpleMap<LengthMap> FLengthMap; 1408 /// FLengthMap flength(length); 1409 /// 1410 /// typedef ScaleMap<LengthMap> RLengthMap; 1411 /// RLengthMap rlength(length, 2.0); 1412 /// 1413 /// typedef Graph::CombinedArcMap<FLengthMap, RLengthMap > ULengthMap; 1414 /// ULengthMap ulength(flength, rlength); 1415 /// 1416 /// Dijkstra<Graph, ULengthMap> dijkstra(graph, ulength); 1417 /// std::cout << "Driving time : " << dijkstra.run(src, trg) << std::endl; 1418 ///\endcode 1419 /// 1420 /// The combined arc map makes the length map for the undirected 1421 /// graph. It is created from a forward and reverse map. The forward 1422 /// map is created from the original length map with a SimpleMap 1423 /// adaptor which just makes a read-write map from the reference map 1424 /// i.e. it forgets that it can be return reference to values. The 1425 /// reverse map is just the scaled original map with the ScaleMap 1426 /// adaptor. The combination solves that passing the reverse way 1427 /// takes double time than the original. To get the driving time we 1428 /// run the dijkstra algorithm on the graph. 1429 template<typename _Digraph> 1430 class UndirDigraphAdaptor 1431 : public GraphAdaptorExtender<UndirDigraphAdaptorBase<_Digraph> > { 1432 public: 1433 typedef _Digraph Digraph; 1434 typedef GraphAdaptorExtender<UndirDigraphAdaptorBase<Digraph> > Parent; 1435 protected: 1436 UndirDigraphAdaptor() { } 1437 public: 1438 1439 /// \brief Constructor 1440 /// 1441 /// Constructor 1442 UndirDigraphAdaptor(_Digraph& _digraph) { 1443 setDigraph(_digraph); 1444 } 1445 1446 /// \brief ArcMap combined from two original ArcMap 1447 /// 1448 /// This class adapts two original digraph ArcMap to 1449 /// get an arc map on the adaptor. 1450 template <typename _ForwardMap, typename _BackwardMap> 1451 class CombinedArcMap { 1452 public: 1453 1454 typedef _ForwardMap ForwardMap; 1455 typedef _BackwardMap BackwardMap; 1456 1457 typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag; 1458 1459 typedef typename ForwardMap::Value Value; 1460 typedef typename Parent::Arc Key; 1461 1462 /// \brief Constructor 1463 /// 1464 /// Constructor 1465 CombinedArcMap() : _forward(0), _backward(0) {} 1466 1467 /// \brief Constructor 1468 /// 1469 /// Constructor 1470 CombinedArcMap(ForwardMap& forward, BackwardMap& backward) 1471 : _forward(&forward), _backward(&backward) {} 1472 1473 1474 /// \brief Sets the value associated with a key. 1475 /// 1476 /// Sets the value associated with a key. 1477 void set(const Key& e, const Value& a) { 1478 if (Parent::direction(e)) { 1479 _forward->set(e, a); 1480 } else { 1481 _backward->set(e, a); 1482 } 1483 } 1484 1485 /// \brief Returns the value associated with a key. 1486 /// 1487 /// Returns the value associated with a key. 1488 typename MapTraits<ForwardMap>::ConstReturnValue 1489 operator[](const Key& e) const { 1490 if (Parent::direction(e)) { 1491 return (*_forward)[e]; 1492 } else { 1493 return (*_backward)[e]; 1494 } 1495 } 1496 1497 /// \brief Returns the value associated with a key. 1498 /// 1499 /// Returns the value associated with a key. 1500 typename MapTraits<ForwardMap>::ReturnValue 1501 operator[](const Key& e) { 1502 if (Parent::direction(e)) { 1503 return (*_forward)[e]; 1504 } else { 1505 return (*_backward)[e]; 1506 } 1507 } 1508 1509 /// \brief Sets the forward map 1510 /// 1511 /// Sets the forward map 1512 void setForwardMap(ForwardMap& forward) { 1513 _forward = &forward; 1514 } 1515 1516 /// \brief Sets the backward map 1517 /// 1518 /// Sets the backward map 1519 void setBackwardMap(BackwardMap& backward) { 1520 _backward = &backward; 1521 } 1522 1523 protected: 1524 1525 ForwardMap* _forward; 1526 BackwardMap* _backward; 1527 1528 }; 1529 1530 }; 1531 1532 /// \brief Just gives back an undir digraph adaptor 1533 /// 1534 /// Just gives back an undir digraph adaptor 1535 template<typename Digraph> 1536 UndirDigraphAdaptor<const Digraph> 1537 undirDigraphAdaptor(const Digraph& digraph) { 1538 return UndirDigraphAdaptor<const Digraph>(digraph); 1539 } 1540 1541 template<typename _Digraph, 1542 typename _CapacityMap = typename _Digraph::template ArcMap<int>, 1543 typename _FlowMap = _CapacityMap, 1544 typename _Tolerance = Tolerance<typename _CapacityMap::Value> > 1545 class ResForwardFilter { 1546 public: 1547 1548 typedef _Digraph Digraph; 1549 typedef _CapacityMap CapacityMap; 1550 typedef _FlowMap FlowMap; 1551 typedef _Tolerance Tolerance; 1552 1553 typedef typename Digraph::Arc Key; 1554 typedef bool Value; 1555 1556 private: 1557 1558 const CapacityMap* _capacity; 1559 const FlowMap* _flow; 1560 Tolerance _tolerance; 1561 public: 1562 1563 ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow, 1564 const Tolerance& tolerance = Tolerance()) 1565 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } 1566 1567 ResForwardFilter(const Tolerance& tolerance = Tolerance()) 1568 : _capacity(0), _flow(0), _tolerance(tolerance) { } 1569 1570 void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } 1571 void setFlow(const FlowMap& flow) { _flow = &flow; } 1572 1573 bool operator[](const typename Digraph::Arc& a) const { 1574 return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); 1575 } 1576 }; 1577 1578 template<typename _Digraph, 1579 typename _CapacityMap = typename _Digraph::template ArcMap<int>, 1580 typename _FlowMap = _CapacityMap, 1581 typename _Tolerance = Tolerance<typename _CapacityMap::Value> > 1582 class ResBackwardFilter { 1583 public: 1584 1585 typedef _Digraph Digraph; 1586 typedef _CapacityMap CapacityMap; 1587 typedef _FlowMap FlowMap; 1588 typedef _Tolerance Tolerance; 1589 1590 typedef typename Digraph::Arc Key; 1591 typedef bool Value; 1592 1593 private: 1594 1595 const CapacityMap* _capacity; 1596 const FlowMap* _flow; 1597 Tolerance _tolerance; 1598 1599 public: 1600 1601 ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow, 1602 const Tolerance& tolerance = Tolerance()) 1603 : _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } 1604 ResBackwardFilter(const Tolerance& tolerance = Tolerance()) 1605 : _capacity(0), _flow(0), _tolerance(tolerance) { } 1606 1607 void setCapacity(const CapacityMap& capacity) { _capacity = &capacity; } 1608 void setFlow(const FlowMap& flow) { _flow = &flow; } 1609 1610 bool operator[](const typename Digraph::Arc& a) const { 1611 return _tolerance.positive((*_flow)[a]); 1612 } 1613 }; 1614 1615 1616 ///\ingroup graph_adaptors 1617 /// 1618 ///\brief An adaptor for composing the residual graph for directed 1619 ///flow and circulation problems. 1620 /// 1621 ///An adaptor for composing the residual graph for directed flow and 1622 ///circulation problems. Let \f$ G=(V, A) \f$ be a directed digraph 1623 ///and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F 1624 ///\f$, be functions on the arc-set. 1625 /// 1626 ///In the appications of ResDigraphAdaptor, \f$ f \f$ usually stands 1627 ///for a flow and \f$ c \f$ for a capacity function. Suppose that a 1628 ///graph instance \c g of type \c ListDigraph implements \f$ G \f$. 1629 /// 1630 ///\code 1631 /// ListDigraph g; 1632 ///\endcode 1633 /// 1634 ///Then ResDigraphAdaptor implements the digraph structure with 1635 /// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} 1636 /// \f$, where \f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and 1637 /// \f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, i.e. the so 1638 /// called residual graph. When we take the union \f$ 1639 /// A_{forward}\cup A_{backward} \f$, multilicities are counted, 1640 /// i.e. if an arc is in both \f$ A_{forward} \f$ and \f$ 1641 /// A_{backward} \f$, then in the adaptor it appears twice. The 1642 /// following code shows how such an instance can be constructed. 1643 /// 1644 ///\code 1645 /// typedef ListDigraph Digraph; 1646 /// IntArcMap f(g), c(g); 1647 /// ResDigraphAdaptor<Digraph, int, IntArcMap, IntArcMap> ga(g); 1648 ///\endcode 1649 template<typename _Digraph, 1650 typename _CapacityMap = typename _Digraph::template ArcMap<int>, 1651 typename _FlowMap = _CapacityMap, 1652 typename _Tolerance = Tolerance<typename _CapacityMap::Value> > 1653 class ResDigraphAdaptor : 1654 public ArcSubDigraphAdaptor< 1655 UndirDigraphAdaptor<const _Digraph>, 1656 typename UndirDigraphAdaptor<const _Digraph>::template CombinedArcMap< 1657 ResForwardFilter<const _Digraph, _CapacityMap, _FlowMap>, 1658 ResBackwardFilter<const _Digraph, _CapacityMap, _FlowMap> > > { 1659 public: 1660 1661 typedef _Digraph Digraph; 1662 typedef _CapacityMap CapacityMap; 1663 typedef _FlowMap FlowMap; 1664 typedef _Tolerance Tolerance; 1665 1666 typedef typename CapacityMap::Value Value; 1667 typedef ResDigraphAdaptor Adaptor; 1668 1669 protected: 1670 1671 typedef UndirDigraphAdaptor<const Digraph> UndirDigraph; 1672 1673 typedef ResForwardFilter<const Digraph, CapacityMap, FlowMap> 1674 ForwardFilter; 1675 1676 typedef ResBackwardFilter<const Digraph, CapacityMap, FlowMap> 1677 BackwardFilter; 1678 1679 typedef typename UndirDigraph:: 1680 template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; 1681 1682 typedef ArcSubDigraphAdaptor<UndirDigraph, ArcFilter> Parent; 1683 1684 const CapacityMap* _capacity; 1685 FlowMap* _flow; 1686 1687 UndirDigraph _graph; 1688 ForwardFilter _forward_filter; 1689 BackwardFilter _backward_filter; 1690 ArcFilter _arc_filter; 1691 1692 void setCapacityMap(const CapacityMap& capacity) { 1693 _capacity = &capacity; 1694 _forward_filter.setCapacity(capacity); 1695 _backward_filter.setCapacity(capacity); 1696 } 1697 1698 void setFlowMap(FlowMap& flow) { 1699 _flow = &flow; 1700 _forward_filter.setFlow(flow); 1701 _backward_filter.setFlow(flow); 1702 } 1703 1704 public: 1705 1706 /// \brief Constructor of the residual digraph. 1707 /// 1708 /// Constructor of the residual graph. The parameters are the digraph type, 1709 /// the flow map, the capacity map and a tolerance object. 1710 ResDigraphAdaptor(const Digraph& digraph, const CapacityMap& capacity, 1711 FlowMap& flow, const Tolerance& tolerance = Tolerance()) 1712 : Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph), 1713 _forward_filter(capacity, flow, tolerance), 1714 _backward_filter(capacity, flow, tolerance), 1715 _arc_filter(_forward_filter, _backward_filter) 1716 { 1717 Parent::setDigraph(_graph); 1718 Parent::setArcFilterMap(_arc_filter); 1719 } 1720 1721 typedef typename Parent::Arc Arc; 1722 1723 /// \brief Gives back the residual capacity of the arc. 1724 /// 1725 /// Gives back the residual capacity of the arc. 1726 Value rescap(const Arc& arc) const { 1727 if (UndirDigraph::direction(arc)) { 1728 return (*_capacity)[arc] - (*_flow)[arc]; 1729 } else { 1730 return (*_flow)[arc]; 1731 } 1732 } 1733 1734 /// \brief Augment on the given arc in the residual digraph. 1735 /// 1736 /// Augment on the given arc in the residual digraph. It increase 1737 /// or decrease the flow on the original arc depend on the direction 1738 /// of the residual arc. 1739 void augment(const Arc& e, const Value& a) const { 1740 if (UndirDigraph::direction(e)) { 1741 _flow->set(e, (*_flow)[e] + a); 1742 } else { 1743 _flow->set(e, (*_flow)[e] - a); 1744 } 1745 } 1746 1747 /// \brief Returns the direction of the arc. 1748 /// 1749 /// Returns true when the arc is same oriented as the original arc. 1750 static bool forward(const Arc& e) { 1751 return UndirDigraph::direction(e); 1752 } 1753 1754 /// \brief Returns the direction of the arc. 1755 /// 1756 /// Returns true when the arc is opposite oriented as the original arc. 1757 static bool backward(const Arc& e) { 1758 return !UndirDigraph::direction(e); 1759 } 1760 1761 /// \brief Gives back the forward oriented residual arc. 1762 /// 1763 /// Gives back the forward oriented residual arc. 1764 static Arc forward(const typename Digraph::Arc& e) { 1765 return UndirDigraph::direct(e, true); 1766 } 1767 1768 /// \brief Gives back the backward oriented residual arc. 1769 /// 1770 /// Gives back the backward oriented residual arc. 1771 static Arc backward(const typename Digraph::Arc& e) { 1772 return UndirDigraph::direct(e, false); 1773 } 1774 1775 /// \brief Residual capacity map. 1776 /// 1777 /// In generic residual digraphs the residual capacity can be obtained 1778 /// as a map. 1779 class ResCap { 1780 protected: 1781 const Adaptor* _adaptor; 1782 public: 1783 typedef Arc Key; 1784 typedef typename _CapacityMap::Value Value; 1785 1786 ResCap(const Adaptor& adaptor) : _adaptor(&adaptor) {} 1787 1788 Value operator[](const Arc& e) const { 1789 return _adaptor->rescap(e); 1790 } 1791 1792 }; 1793 1794 }; 1795 1796 /// \brief Base class for split digraph adaptor 1797 /// 1798 /// Base class of split digraph adaptor. In most case you do not need to 1799 /// use it directly but the documented member functions of this class can 1800 /// be used with the SplitDigraphAdaptor class. 1801 /// \sa SplitDigraphAdaptor 1802 template <typename _Digraph> 1803 class SplitDigraphAdaptorBase { 1804 public: 1805 1806 typedef _Digraph Digraph; 1807 typedef DigraphAdaptorBase<const _Digraph> Parent; 1808 typedef SplitDigraphAdaptorBase Adaptor; 1809 1810 typedef typename Digraph::Node DigraphNode; 1811 typedef typename Digraph::Arc DigraphArc; 1812 1813 class Node; 1814 class Arc; 1815 1816 private: 1817 1818 template <typename T> class NodeMapBase; 1819 template <typename T> class ArcMapBase; 1820 1821 public: 1822 1823 class Node : public DigraphNode { 1824 friend class SplitDigraphAdaptorBase; 1825 template <typename T> friend class NodeMapBase; 1826 private: 1827 1828 bool _in; 1829 Node(DigraphNode node, bool in) 1830 : DigraphNode(node), _in(in) {} 1831 1832 public: 1833 1834 Node() {} 1835 Node(Invalid) : DigraphNode(INVALID), _in(true) {} 1836 1837 bool operator==(const Node& node) const { 1838 return DigraphNode::operator==(node) && _in == node._in; 1839 } 1840 1841 bool operator!=(const Node& node) const { 1842 return !(*this == node); 1843 } 1844 1845 bool operator<(const Node& node) const { 1846 return DigraphNode::operator<(node) || 1847 (DigraphNode::operator==(node) && _in < node._in); 1848 } 1849 }; 1850 1851 class Arc { 1852 friend class SplitDigraphAdaptorBase; 1853 template <typename T> friend class ArcMapBase; 1854 private: 1855 typedef BiVariant<DigraphArc, DigraphNode> ArcImpl; 1856 1857 explicit Arc(const DigraphArc& arc) : _item(arc) {} 1858 explicit Arc(const DigraphNode& node) : _item(node) {} 1859 1860 ArcImpl _item; 1861 1862 public: 1863 Arc() {} 1864 Arc(Invalid) : _item(DigraphArc(INVALID)) {} 1865 1866 bool operator==(const Arc& arc) const { 1867 if (_item.firstState()) { 1868 if (arc._item.firstState()) { 1869 return _item.first() == arc._item.first(); 1870 } 1871 } else { 1872 if (arc._item.secondState()) { 1873 return _item.second() == arc._item.second(); 1874 } 1875 } 1876 return false; 1877 } 1878 1879 bool operator!=(const Arc& arc) const { 1880 return !(*this == arc); 1881 } 1882 1883 bool operator<(const Arc& arc) const { 1884 if (_item.firstState()) { 1885 if (arc._item.firstState()) { 1886 return _item.first() < arc._item.first(); 1887 } 1888 return false; 1889 } else { 1890 if (arc._item.secondState()) { 1891 return _item.second() < arc._item.second(); 1892 } 1893 return true; 1894 } 1895 } 1896 1897 operator DigraphArc() const { return _item.first(); } 1898 operator DigraphNode() const { return _item.second(); } 1899 1900 }; 1901 1902 void first(Node& n) const { 1903 _digraph->first(n); 1904 n._in = true; 1905 } 1906 1907 void next(Node& n) const { 1908 if (n._in) { 1909 n._in = false; 1910 } else { 1911 n._in = true; 1912 _digraph->next(n); 1913 } 1914 } 1915 1916 void first(Arc& e) const { 1917 e._item.setSecond(); 1918 _digraph->first(e._item.second()); 1919 if (e._item.second() == INVALID) { 1920 e._item.setFirst(); 1921 _digraph->first(e._item.first()); 1922 } 1923 } 1924 1925 void next(Arc& e) const { 1926 if (e._item.secondState()) { 1927 _digraph->next(e._item.second()); 1928 if (e._item.second() == INVALID) { 1929 e._item.setFirst(); 1930 _digraph->first(e._item.first()); 1931 } 1932 } else { 1933 _digraph->next(e._item.first()); 1934 } 1935 } 1936 1937 void firstOut(Arc& e, const Node& n) const { 1938 if (n._in) { 1939 e._item.setSecond(n); 1940 } else { 1941 e._item.setFirst(); 1942 _digraph->firstOut(e._item.first(), n); 1943 } 1944 } 1945 1946 void nextOut(Arc& e) const { 1947 if (!e._item.firstState()) { 1948 e._item.setFirst(INVALID); 1949 } else { 1950 _digraph->nextOut(e._item.first()); 1951 } 1952 } 1953 1954 void firstIn(Arc& e, const Node& n) const { 1955 if (!n._in) { 1956 e._item.setSecond(n); 1957 } else { 1958 e._item.setFirst(); 1959 _digraph->firstIn(e._item.first(), n); 1960 } 1961 } 1962 1963 void nextIn(Arc& e) const { 1964 if (!e._item.firstState()) { 1965 e._item.setFirst(INVALID); 1966 } else { 1967 _digraph->nextIn(e._item.first()); 1968 } 1969 } 1970 1971 Node source(const Arc& e) const { 1972 if (e._item.firstState()) { 1973 return Node(_digraph->source(e._item.first()), false); 1974 } else { 1975 return Node(e._item.second(), true); 1976 } 1977 } 1978 1979 Node target(const Arc& e) const { 1980 if (e._item.firstState()) { 1981 return Node(_digraph->target(e._item.first()), true); 1982 } else { 1983 return Node(e._item.second(), false); 1984 } 1985 } 1986 1987 int id(const Node& n) const { 1988 return (_digraph->id(n) << 1) | (n._in ? 0 : 1); 1989 } 1990 Node nodeFromId(int ix) const { 1991 return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0); 1992 } 1993 int maxNodeId() const { 1994 return 2 * _digraph->maxNodeId() + 1; 1995 } 1996 1997 int id(const Arc& e) const { 1998 if (e._item.firstState()) { 1999 return _digraph->id(e._item.first()) << 1; 2000 } else { 2001 return (_digraph->id(e._item.second()) << 1) | 1; 2002 } 2003 } 2004 Arc arcFromId(int ix) const { 2005 if ((ix & 1) == 0) { 2006 return Arc(_digraph->arcFromId(ix >> 1)); 2007 } else { 2008 return Arc(_digraph->nodeFromId(ix >> 1)); 2009 } 2010 } 2011 int maxArcId() const { 2012 return std::max(_digraph->maxNodeId() << 1, 2013 (_digraph->maxArcId() << 1) | 1); 2014 } 2015 2016 /// \brief Returns true when the node is in-node. 2017 /// 2018 /// Returns true when the node is in-node. 2019 static bool inNode(const Node& n) { 2020 return n._in; 2021 } 2022 2023 /// \brief Returns true when the node is out-node. 2024 /// 2025 /// Returns true when the node is out-node. 2026 static bool outNode(const Node& n) { 2027 return !n._in; 2028 } 2029 2030 /// \brief Returns true when the arc is arc in the original digraph. 2031 /// 2032 /// Returns true when the arc is arc in the original digraph. 2033 static bool origArc(const Arc& e) { 2034 return e._item.firstState(); 2035 } 2036 2037 /// \brief Returns true when the arc binds an in-node and an out-node. 2038 /// 2039 /// Returns true when the arc binds an in-node and an out-node. 2040 static bool bindArc(const Arc& e) { 2041 return e._item.secondState(); 2042 } 2043 2044 /// \brief Gives back the in-node created from the \c node. 2045 /// 2046 /// Gives back the in-node created from the \c node. 2047 static Node inNode(const DigraphNode& n) { 2048 return Node(n, true); 2049 } 2050 2051 /// \brief Gives back the out-node created from the \c node. 2052 /// 2053 /// Gives back the out-node created from the \c node. 2054 static Node outNode(const DigraphNode& n) { 2055 return Node(n, false); 2056 } 2057 2058 /// \brief Gives back the arc binds the two part of the node. 2059 /// 2060 /// Gives back the arc binds the two part of the node. 2061 static Arc arc(const DigraphNode& n) { 2062 return Arc(n); 2063 } 2064 2065 /// \brief Gives back the arc of the original arc. 2066 /// 2067 /// Gives back the arc of the original arc. 2068 static Arc arc(const DigraphArc& e) { 2069 return Arc(e); 2070 } 2071 2072 typedef True NodeNumTag; 2073 2074 int nodeNum() const { 2075 return 2 * countNodes(*_digraph); 2076 } 2077 2078 typedef True EdgeNumTag; 2079 int arcNum() const { 2080 return countArcs(*_digraph) + countNodes(*_digraph); 2081 } 2082 2083 typedef True FindEdgeTag; 2084 Arc findArc(const Node& u, const Node& v, 2085 const Arc& prev = INVALID) const { 2086 if (inNode(u)) { 2087 if (outNode(v)) { 2088 if (static_cast<const DigraphNode&>(u) == 2089 static_cast<const DigraphNode&>(v) && prev == INVALID) { 2090 return Arc(u); 2091 } 2092 } 2093 } else { 2094 if (inNode(v)) { 2095 return Arc(::lemon::findArc(*_digraph, u, v, prev)); 2096 } 2097 } 2098 return INVALID; 2099 } 2100 2101 private: 2102 2103 template <typename _Value> 2104 class NodeMapBase 2105 : public MapTraits<typename Parent::template NodeMap<_Value> > { 2106 typedef typename Parent::template NodeMap<_Value> NodeImpl; 2107 public: 2108 typedef Node Key; 2109 typedef _Value Value; 2110 2111 NodeMapBase(const Adaptor& adaptor) 2112 : _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} 2113 NodeMapBase(const Adaptor& adaptor, const Value& value) 2114 : _in_map(*adaptor._digraph, value), 2115 _out_map(*adaptor._digraph, value) {} 2116 2117 void set(const Node& key, const Value& val) { 2118 if (Adaptor::inNode(key)) { _in_map.set(key, val); } 2119 else {_out_map.set(key, val); } 2120 } 2121 2122 typename MapTraits<NodeImpl>::ReturnValue 2123 operator[](const Node& key) { 2124 if (Adaptor::inNode(key)) { return _in_map[key]; } 2125 else { return _out_map[key]; } 2126 } 2127 2128 typename MapTraits<NodeImpl>::ConstReturnValue 2129 operator[](const Node& key) const { 2130 if (Adaptor::inNode(key)) { return _in_map[key]; } 2131 else { return _out_map[key]; } 2132 } 2133 2134 private: 2135 NodeImpl _in_map, _out_map; 2136 }; 2137 2138 template <typename _Value> 2139 class ArcMapBase 2140 : public MapTraits<typename Parent::template ArcMap<_Value> > { 2141 typedef typename Parent::template ArcMap<_Value> ArcImpl; 2142 typedef typename Parent::template NodeMap<_Value> NodeImpl; 2143 public: 2144 typedef Arc Key; 2145 typedef _Value Value; 2146 2147 ArcMapBase(const Adaptor& adaptor) 2148 : _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} 2149 ArcMapBase(const Adaptor& adaptor, const Value& value) 2150 : _arc_map(*adaptor._digraph, value), 2151 _node_map(*adaptor._digraph, value) {} 2152 2153 void set(const Arc& key, const Value& val) { 2154 if (Adaptor::origArc(key)) { 2155 _arc_map.set(key._item.first(), val); 2156 } else { 2157 _node_map.set(key._item.second(), val); 2158 } 2159 } 2160 2161 typename MapTraits<ArcImpl>::ReturnValue 2162 operator[](const Arc& key) { 2163 if (Adaptor::origArc(key)) { 2164 return _arc_map[key._item.first()]; 2165 } else { 2166 return _node_map[key._item.second()]; 2167 } 2168 } 2169 2170 typename MapTraits<ArcImpl>::ConstReturnValue 2171 operator[](const Arc& key) const { 2172 if (Adaptor::origArc(key)) { 2173 return _arc_map[key._item.first()]; 2174 } else { 2175 return _node_map[key._item.second()]; 2176 } 2177 } 2178 2179 private: 2180 ArcImpl _arc_map; 2181 NodeImpl _node_map; 2182 }; 2183 2184 public: 2185 2186 template <typename _Value> 2187 class NodeMap 2188 : public SubMapExtender<Adaptor, NodeMapBase<_Value> > 2189 { 2190 public: 2191 typedef _Value Value; 2192 typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent; 2193 2194 NodeMap(const Adaptor& adaptor) 2195 : Parent(adaptor) {} 2196 2197 NodeMap(const Adaptor& adaptor, const Value& value) 2198 : Parent(adaptor, value) {} 2199 2200 NodeMap& operator=(const NodeMap& cmap) { 2201 return operator=<NodeMap>(cmap); 2202 } 2203 2204 template <typename CMap> 2205 NodeMap& operator=(const CMap& cmap) { 2206 Parent::operator=(cmap); 2207 return *this; 2208 } 2209 }; 2210 2211 template <typename _Value> 2212 class ArcMap 2213 : public SubMapExtender<Adaptor, ArcMapBase<_Value> > 2214 { 2215 public: 2216 typedef _Value Value; 2217 typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent; 2218 2219 ArcMap(const Adaptor& adaptor) 2220 : Parent(adaptor) {} 2221 2222 ArcMap(const Adaptor& adaptor, const Value& value) 2223 : Parent(adaptor, value) {} 2224 2225 ArcMap& operator=(const ArcMap& cmap) { 2226 return operator=<ArcMap>(cmap); 2227 } 2228 2229 template <typename CMap> 2230 ArcMap& operator=(const CMap& cmap) { 2231 Parent::operator=(cmap); 2232 return *this; 2233 } 2234 }; 2235 2236 protected: 2237 2238 SplitDigraphAdaptorBase() : _digraph(0) {} 2239 2240 Digraph* _digraph; 2241 2242 void setDigraph(Digraph& digraph) { 2243 _digraph = &digraph; 2244 } 2245 2246 }; 2247 2248 /// \ingroup graph_adaptors 2249 /// 2250 /// \brief Split digraph adaptor class 2251 /// 2252 /// This is an digraph adaptor which splits all node into an in-node 2253 /// and an out-node. Formaly, the adaptor replaces each \f$ u \f$ 2254 /// node in the digraph with two node, \f$ u_{in} \f$ node and 2255 /// \f$ u_{out} \f$ node. If there is an \f$ (v, u) \f$ arc in the 2256 /// original digraph the new target of the arc will be \f$ u_{in} \f$ and 2257 /// similarly the source of the original \f$ (u, v) \f$ arc will be 2258 /// \f$ u_{out} \f$. The adaptor will add for each node in the 2259 /// original digraph an additional arc which will connect 2260 /// \f$ (u_{in}, u_{out}) \f$. 2261 /// 2262 /// The aim of this class is to run algorithm with node costs if the 2263 /// algorithm can use directly just arc costs. In this case we should use 2264 /// a \c SplitDigraphAdaptor and set the node cost of the digraph to the 2265 /// bind arc in the adapted digraph. 2266 /// 2267 /// By example a maximum flow algoritm can compute how many arc 2268 /// disjoint paths are in the digraph. But we would like to know how 2269 /// many node disjoint paths are in the digraph. First we have to 2270 /// adapt the digraph with the \c SplitDigraphAdaptor. Then run the flow 2271 /// algorithm on the adapted digraph. The bottleneck of the flow will 2272 /// be the bind arcs which bounds the flow with the count of the 2273 /// node disjoint paths. 2274 /// 2275 ///\code 2276 /// 2277 /// typedef SplitDigraphAdaptor<SmartDigraph> SDigraph; 2278 /// 2279 /// SDigraph sdigraph(digraph); 2280 /// 2281 /// typedef ConstMap<SDigraph::Arc, int> SCapacity; 2282 /// SCapacity scapacity(1); 2283 /// 2284 /// SDigraph::ArcMap<int> sflow(sdigraph); 2285 /// 2286 /// Preflow<SDigraph, SCapacity> 2287 /// spreflow(sdigraph, scapacity, 2288 /// SDigraph::outNode(source), SDigraph::inNode(target)); 2289 /// 2290 /// spreflow.run(); 2291 /// 2292 ///\endcode 2293 /// 2294 /// The result of the mamixum flow on the original digraph 2295 /// shows the next figure: 2296 /// 2297 /// \image html arc_disjoint.png 2298 /// \image latex arc_disjoint.eps "Arc disjoint paths" width=\textwidth 2299 /// 2300 /// And the maximum flow on the adapted digraph: 2301 /// 2302 /// \image html node_disjoint.png 2303 /// \image latex node_disjoint.eps "Node disjoint paths" width=\textwidth 2304 /// 2305 /// The second solution contains just 3 disjoint paths while the first 4. 2306 /// The full code can be found in the \ref disjoint_paths_demo.cc demo file. 2307 /// 2308 /// This digraph adaptor is fully conform to the 2309 /// \ref concepts::Digraph "Digraph" concept and 2310 /// contains some additional member functions and types. The 2311 /// documentation of some member functions may be found just in the 2312 /// SplitDigraphAdaptorBase class. 2313 /// 2314 /// \sa SplitDigraphAdaptorBase 2315 template <typename _Digraph> 2316 class SplitDigraphAdaptor 2317 : public DigraphAdaptorExtender<SplitDigraphAdaptorBase<_Digraph> > { 2318 public: 2319 typedef _Digraph Digraph; 2320 typedef DigraphAdaptorExtender<SplitDigraphAdaptorBase<Digraph> > Parent; 2321 2322 typedef typename Parent::Node Node; 2323 typedef typename Parent::Arc Arc; 2324 2325 /// \brief Constructor of the adaptor. 2326 /// 2327 /// Constructor of the adaptor. 2328 SplitDigraphAdaptor(Digraph& g) { 2329 Parent::setDigraph(g); 2330 } 2331 2332 /// \brief NodeMap combined from two original NodeMap 2333 /// 2334 /// This class adapt two of the original digraph NodeMap to 2335 /// get a node map on the adapted digraph. 2336 template <typename InNodeMap, typename OutNodeMap> 2337 class CombinedNodeMap { 2338 public: 2339 2340 typedef Node Key; 2341 typedef typename InNodeMap::Value Value; 2342 2343 /// \brief Constructor 2344 /// 2345 /// Constructor. 2346 CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) 2347 : _in_map(in_map), _out_map(out_map) {} 2348 2349 /// \brief The subscript operator. 2350 /// 2351 /// The subscript operator. 2352 Value& operator[](const Key& key) { 2353 if (Parent::inNode(key)) { 2354 return _in_map[key]; 2355 } else { 2356 return _out_map[key]; 2357 } 2358 } 2359 2360 /// \brief The const subscript operator. 2361 /// 2362 /// The const subscript operator. 2363 Value operator[](const Key& key) const { 2364 if (Parent::inNode(key)) { 2365 return _in_map[key]; 2366 } else { 2367 return _out_map[key]; 2368 } 2369 } 2370 2371 /// \brief The setter function of the map. 2372 /// 2373 /// The setter function of the map. 2374 void set(const Key& key, const Value& value) { 2375 if (Parent::inNode(key)) { 2376 _in_map.set(key, value); 2377 } else { 2378 _out_map.set(key, value); 2379 } 2380 } 2381 2382 private: 2383 2384 InNodeMap& _in_map; 2385 OutNodeMap& _out_map; 2386 2387 }; 2388 2389 2390 /// \brief Just gives back a combined node map. 2391 /// 2392 /// Just gives back a combined node map. 2393 template <typename InNodeMap, typename OutNodeMap> 2394 static CombinedNodeMap<InNodeMap, OutNodeMap> 2395 combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) { 2396 return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map); 2397 } 2398 2399 template <typename InNodeMap, typename OutNodeMap> 2400 static CombinedNodeMap<const InNodeMap, OutNodeMap> 2401 combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) { 2402 return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map); 2403 } 2404 2405 template <typename InNodeMap, typename OutNodeMap> 2406 static CombinedNodeMap<InNodeMap, const OutNodeMap> 2407 combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) { 2408 return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map); 2409 } 2410 2411 template <typename InNodeMap, typename OutNodeMap> 2412 static CombinedNodeMap<const InNodeMap, const OutNodeMap> 2413 combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) { 2414 return CombinedNodeMap<const InNodeMap, 2415 const OutNodeMap>(in_map, out_map); 2416 } 2417 2418 /// \brief ArcMap combined from an original ArcMap and NodeMap 2419 /// 2420 /// This class adapt an original digraph ArcMap and NodeMap to 2421 /// get an arc map on the adapted digraph. 2422 template <typename DigraphArcMap, typename DigraphNodeMap> 2423 class CombinedArcMap { 2424 public: 2425 2426 typedef Arc Key; 2427 typedef typename DigraphArcMap::Value Value; 2428 2429 /// \brief Constructor 2430 /// 2431 /// Constructor. 2432 CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) 2433 : _arc_map(arc_map), _node_map(node_map) {} 2434 2435 /// \brief The subscript operator. 2436 /// 2437 /// The subscript operator. 2438 void set(const Arc& arc, const Value& val) { 2439 if (Parent::origArc(arc)) { 2440 _arc_map.set(arc, val); 2441 } else { 2442 _node_map.set(arc, val); 2443 } 2444 } 2445 2446 /// \brief The const subscript operator. 2447 /// 2448 /// The const subscript operator. 2449 Value operator[](const Key& arc) const { 2450 if (Parent::origArc(arc)) { 2451 return _arc_map[arc]; 2452 } else { 2453 return _node_map[arc]; 2454 } 2455 } 2456 2457 /// \brief The const subscript operator. 2458 /// 2459 /// The const subscript operator. 2460 Value& operator[](const Key& arc) { 2461 if (Parent::origArc(arc)) { 2462 return _arc_map[arc]; 2463 } else { 2464 return _node_map[arc]; 2465 } 2466 } 2467 2468 private: 2469 DigraphArcMap& _arc_map; 2470 DigraphNodeMap& _node_map; 2471 }; 2472 2473 /// \brief Just gives back a combined arc map. 2474 /// 2475 /// Just gives back a combined arc map. 2476 template <typename DigraphArcMap, typename DigraphNodeMap> 2477 static CombinedArcMap<DigraphArcMap, DigraphNodeMap> 2478 combinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) { 2479 return CombinedArcMap<DigraphArcMap, DigraphNodeMap>(arc_map, node_map); 2480 } 2481 2482 template <typename DigraphArcMap, typename DigraphNodeMap> 2483 static CombinedArcMap<const DigraphArcMap, DigraphNodeMap> 2484 combinedArcMap(const DigraphArcMap& arc_map, DigraphNodeMap& node_map) { 2485 return CombinedArcMap<const DigraphArcMap, 2486 DigraphNodeMap>(arc_map, node_map); 2487 } 2488 2489 template <typename DigraphArcMap, typename DigraphNodeMap> 2490 static CombinedArcMap<DigraphArcMap, const DigraphNodeMap> 2491 combinedArcMap(DigraphArcMap& arc_map, const DigraphNodeMap& node_map) { 2492 return CombinedArcMap<DigraphArcMap, 2493 const DigraphNodeMap>(arc_map, node_map); 2494 } 2495 2496 template <typename DigraphArcMap, typename DigraphNodeMap> 2497 static CombinedArcMap<const DigraphArcMap, const DigraphNodeMap> 2498 combinedArcMap(const DigraphArcMap& arc_map, 2499 const DigraphNodeMap& node_map) { 2500 return CombinedArcMap<const DigraphArcMap, 2501 const DigraphNodeMap>(arc_map, node_map); 2502 } 2503 2504 }; 2505 2506 /// \brief Just gives back a split digraph adaptor 2507 /// 2508 /// Just gives back a split digraph adaptor 2509 template<typename Digraph> 2510 SplitDigraphAdaptor<Digraph> 2511 splitDigraphAdaptor(const Digraph& digraph) { 2512 return SplitDigraphAdaptor<Digraph>(digraph); 2513 } 2514 2515 2516 } //namespace lemon 2517 2518 #endif //LEMON_DIGRAPH_ADAPTOR_H 2519 -
new file lemon/graph_adaptor.h
diff -r b6732e0d38c5 -r e67acd83a9ca lemon/graph_adaptor.h
- + 1 /* -*- C++ -*- 2 * 3 * This file is a part of LEMON, a generic C++ optimization library 4 * 5 * Copyright (C) 2003-2008 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 19 #ifndef LEMON_GRAPH_ADAPTOR_H 20 #define LEMON_GRAPH_ADAPTOR_H 21 22 ///\ingroup graph_adaptors 23 ///\file 24 ///\brief Several graph adaptors. 25 /// 26 ///This file contains several useful undirected graph adaptor classes. 27 28 #include <lemon/core.h> 29 #include <lemon/maps.h> 30 #include <lemon/bits/graph_adaptor_extender.h> 31 32 namespace lemon { 33 34 /// \brief Base type for the Graph Adaptors 35 /// 36 /// This is the base type for most of LEMON graph adaptors. 37 /// This class implements a trivial graph adaptor i.e. it only wraps the 38 /// functions and types of the graph. The purpose of this class is to 39 /// make easier implementing graph adaptors. E.g. if an adaptor is 40 /// considered which differs from the wrapped graph only in some of its 41 /// functions or types, then it can be derived from GraphAdaptor, and only 42 /// the differences should be implemented. 43 template<typename _Graph> 44 class GraphAdaptorBase { 45 public: 46 typedef _Graph Graph; 47 typedef Graph ParentGraph; 48 49 protected: 50 Graph* _graph; 51 52 GraphAdaptorBase() : _graph(0) {} 53 54 void setGraph(Graph& graph) { _graph = &graph; } 55 56 public: 57 GraphAdaptorBase(Graph& graph) : _graph(&graph) {} 58 59 typedef typename Graph::Node Node; 60 typedef typename Graph::Arc Arc; 61 typedef typename Graph::Edge Edge; 62 63 void first(Node& i) const { _graph->first(i); } 64 void first(Arc& i) const { _graph->first(i); } 65 void first(Edge& i) const { _graph->first(i); } 66 void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } 67 void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } 68 void firstInc(Edge &i, bool &d, const Node &n) const { 69 _graph->firstInc(i, d, n); 70 } 71 72 void next(Node& i) const { _graph->next(i); } 73 void next(Arc& i) const { _graph->next(i); } 74 void next(Edge& i) const { _graph->next(i); } 75 void nextIn(Arc& i) const { _graph->nextIn(i); } 76 void nextOut(Arc& i) const { _graph->nextOut(i); } 77 void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } 78 79 Node u(const Edge& e) const { return _graph->u(e); } 80 Node v(const Edge& e) const { return _graph->v(e); } 81 82 Node source(const Arc& a) const { return _graph->source(a); } 83 Node target(const Arc& a) const { return _graph->target(a); } 84 85 typedef NodeNumTagIndicator<Graph> NodeNumTag; 86 int nodeNum() const { return _graph->nodeNum(); } 87 88 typedef EdgeNumTagIndicator<Graph> EdgeNumTag; 89 int arcNum() const { return _graph->arcNum(); } 90 int edgeNum() const { return _graph->edgeNum(); } 91 92 typedef FindEdgeTagIndicator<Graph> FindEdgeTag; 93 Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { 94 return _graph->findArc(u, v, prev); 95 } 96 Edge findEdge(const Node& u, const Node& v, const Edge& prev = INVALID) { 97 return _graph->findEdge(u, v, prev); 98 } 99 100 Node addNode() { return _graph->addNode(); } 101 Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } 102 103 void erase(const Node& i) { _graph->erase(i); } 104 void erase(const Edge& i) { _graph->erase(i); } 105 106 void clear() { _graph->clear(); } 107 108 bool direction(const Arc& a) const { return _graph->direction(a); } 109 Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } 110 111 int id(const Node& v) const { return _graph->id(v); } 112 int id(const Arc& a) const { return _graph->id(a); } 113 int id(const Edge& e) const { return _graph->id(e); } 114 115 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } 116 Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } 117 Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } 118 119 int maxNodeId() const { return _graph->maxNodeId(); } 120 int maxArcId() const { return _graph->maxArcId(); } 121 int maxEdgeId() const { return _graph->maxEdgeId(); } 122 123 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; 124 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } 125 126 typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; 127 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } 128 129 typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier; 130 EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } 131 132 template <typename _Value> 133 class NodeMap : public Graph::template NodeMap<_Value> { 134 public: 135 typedef typename Graph::template NodeMap<_Value> Parent; 136 explicit NodeMap(const GraphAdaptorBase<Graph>& adapter) 137 : Parent(*adapter._graph) {} 138 NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) 139 : Parent(*adapter._graph, value) {} 140 141 NodeMap& operator=(const NodeMap& cmap) { 142 return operator=<NodeMap>(cmap); 143 } 144 145 template <typename CMap> 146 NodeMap& operator=(const CMap& cmap) { 147 Parent::operator=(cmap); 148 return *this; 149 } 150 151 }; 152 153 template <typename _Value> 154 class ArcMap : public Graph::template ArcMap<_Value> { 155 public: 156 typedef typename Graph::template ArcMap<_Value> Parent; 157 explicit ArcMap(const GraphAdaptorBase<Graph>& adapter) 158 : Parent(*adapter._graph) {} 159 ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) 160 : Parent(*adapter._graph, value) {} 161 162 ArcMap& operator=(const ArcMap& cmap) { 163 return operator=<ArcMap>(cmap); 164 } 165 166 template <typename CMap> 167 ArcMap& operator=(const CMap& cmap) { 168 Parent::operator=(cmap); 169 return *this; 170 } 171 }; 172 173 template <typename _Value> 174 class EdgeMap : public Graph::template EdgeMap<_Value> { 175 public: 176 typedef typename Graph::template EdgeMap<_Value> Parent; 177 explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter) 178 : Parent(*adapter._graph) {} 179 EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) 180 : Parent(*adapter._graph, value) {} 181 182 EdgeMap& operator=(const EdgeMap& cmap) { 183 return operator=<EdgeMap>(cmap); 184 } 185 186 template <typename CMap> 187 EdgeMap& operator=(const CMap& cmap) { 188 Parent::operator=(cmap); 189 return *this; 190 } 191 }; 192 193 }; 194 195 /// \ingroup graph_adaptors 196 /// 197 /// \brief Trivial graph adaptor 198 /// 199 /// This class is an adaptor which does not change the adapted undirected 200 /// graph. It can be used only to test the graph adaptors. 201 template <typename _Graph> 202 class GraphAdaptor 203 : public GraphAdaptorExtender< GraphAdaptorBase<_Graph> > { 204 public: 205 typedef _Graph Graph; 206 typedef GraphAdaptorExtender<GraphAdaptorBase<_Graph> > Parent; 207 protected: 208 GraphAdaptor() : Parent() {} 209 210 public: 211 explicit GraphAdaptor(Graph& graph) { setGraph(graph); } 212 }; 213 214 template <typename _Graph, typename NodeFilterMap, 215 typename EdgeFilterMap, bool checked = true> 216 class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> { 217 public: 218 typedef _Graph Graph; 219 typedef SubGraphAdaptorBase Adaptor; 220 typedef GraphAdaptorBase<_Graph> Parent; 221 protected: 222 223 NodeFilterMap* _node_filter_map; 224 EdgeFilterMap* _edge_filter_map; 225 226 SubGraphAdaptorBase() 227 : Parent(), _node_filter_map(0), _edge_filter_map(0) { } 228 229 void setNodeFilterMap(NodeFilterMap& node_filter_map) { 230 _node_filter_map=&node_filter_map; 231 } 232 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { 233 _edge_filter_map=&edge_filter_map; 234 } 235 236 public: 237 238 typedef typename Parent::Node Node; 239 typedef typename Parent::Arc Arc; 240 typedef typename Parent::Edge Edge; 241 242 void first(Node& i) const { 243 Parent::first(i); 244 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); 245 } 246 247 void first(Arc& i) const { 248 Parent::first(i); 249 while (i!=INVALID && (!(*_edge_filter_map)[i] 250 || !(*_node_filter_map)[Parent::source(i)] 251 || !(*_node_filter_map)[Parent::target(i)])) Parent::next(i); 252 } 253 254 void first(Edge& i) const { 255 Parent::first(i); 256 while (i!=INVALID && (!(*_edge_filter_map)[i] 257 || !(*_node_filter_map)[Parent::u(i)] 258 || !(*_node_filter_map)[Parent::v(i)])) Parent::next(i); 259 } 260 261 void firstIn(Arc& i, const Node& n) const { 262 Parent::firstIn(i, n); 263 while (i!=INVALID && (!(*_edge_filter_map)[i] 264 || !(*_node_filter_map)[Parent::source(i)])) Parent::nextIn(i); 265 } 266 267 void firstOut(Arc& i, const Node& n) const { 268 Parent::firstOut(i, n); 269 while (i!=INVALID && (!(*_edge_filter_map)[i] 270 || !(*_node_filter_map)[Parent::target(i)])) Parent::nextOut(i); 271 } 272 273 void firstInc(Edge& i, bool& d, const Node& n) const { 274 Parent::firstInc(i, d, n); 275 while (i!=INVALID && (!(*_edge_filter_map)[i] 276 || !(*_node_filter_map)[Parent::u(i)] 277 || !(*_node_filter_map)[Parent::v(i)])) Parent::nextInc(i, d); 278 } 279 280 void next(Node& i) const { 281 Parent::next(i); 282 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); 283 } 284 285 void next(Arc& i) const { 286 Parent::next(i); 287 while (i!=INVALID && (!(*_edge_filter_map)[i] 288 || !(*_node_filter_map)[Parent::source(i)] 289 || !(*_node_filter_map)[Parent::target(i)])) Parent::next(i); 290 } 291 292 void next(Edge& i) const { 293 Parent::next(i); 294 while (i!=INVALID && (!(*_edge_filter_map)[i] 295 || !(*_node_filter_map)[Parent::u(i)] 296 || !(*_node_filter_map)[Parent::v(i)])) Parent::next(i); 297 } 298 299 void nextIn(Arc& i) const { 300 Parent::nextIn(i); 301 while (i!=INVALID && (!(*_edge_filter_map)[i] 302 || !(*_node_filter_map)[Parent::source(i)])) Parent::nextIn(i); 303 } 304 305 void nextOut(Arc& i) const { 306 Parent::nextOut(i); 307 while (i!=INVALID && (!(*_edge_filter_map)[i] 308 || !(*_node_filter_map)[Parent::target(i)])) Parent::nextOut(i); 309 } 310 311 void nextInc(Edge& i, bool& d) const { 312 Parent::nextInc(i, d); 313 while (i!=INVALID && (!(*_edge_filter_map)[i] 314 || !(*_node_filter_map)[Parent::u(i)] 315 || !(*_node_filter_map)[Parent::v(i)])) Parent::nextInc(i, d); 316 } 317 318 /// \brief Hide the given node in the graph. 319 /// 320 /// This function hides \c n in the graph, i.e. the iteration 321 /// jumps over it. This is done by simply setting the value of \c n 322 /// to be false in the corresponding node-map. 323 void hide(const Node& n) const { _node_filter_map->set(n, false); } 324 325 /// \brief Hide the given edge in the graph. 326 /// 327 /// This function hides \c e in the graph, i.e. the iteration 328 /// jumps over it. This is done by simply setting the value of \c e 329 /// to be false in the corresponding edge-map. 330 void hide(const Edge& e) const { _edge_filter_map->set(e, false); } 331 332 /// \brief Unhide the given node in the graph. 333 /// 334 /// The value of \c n is set to be true in the node-map which stores 335 /// hide information. If \c n was hidden previuosly, then it is shown 336 /// again 337 void unHide(const Node& n) const { _node_filter_map->set(n, true); } 338 339 /// \brief Hide the given edge in the graph. 340 /// 341 /// The value of \c e is set to be true in the edge-map which stores 342 /// hide information. If \c e was hidden previuosly, then it is shown 343 /// again 344 void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } 345 346 /// \brief Returns true if \c n is hidden. 347 /// 348 /// Returns true if \c n is hidden. 349 bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } 350 351 /// \brief Returns true if \c e is hidden. 352 /// 353 /// Returns true if \c e is hidden. 354 bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } 355 356 typedef False NodeNumTag; 357 typedef False EdgeNumTag; 358 359 typedef FindEdgeTagIndicator<Graph> FindEdgeTag; 360 Arc findArc(const Node& u, const Node& v, 361 const Arc& prev = INVALID) { 362 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { 363 return INVALID; 364 } 365 Arc arc = Parent::findArc(u, v, prev); 366 while (arc != INVALID && !(*_edge_filter_map)[arc]) { 367 arc = Parent::findArc(u, v, arc); 368 } 369 return arc; 370 } 371 Edge findEdge(const Node& u, const Node& v, 372 const Edge& prev = INVALID) { 373 if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { 374 return INVALID; 375 } 376 Edge edge = Parent::findEdge(u, v, prev); 377 while (edge != INVALID && !(*_edge_filter_map)[edge]) { 378 edge = Parent::findEdge(u, v, edge); 379 } 380 return edge; 381 } 382 383 template <typename _Value> 384 class NodeMap : public SubMapExtender<Adaptor, 385 typename Parent::template NodeMap<_Value> > { 386 public: 387 typedef _Value Value; 388 typedef SubMapExtender<Adaptor, typename Parent:: 389 template NodeMap<Value> > MapParent; 390 391 NodeMap(const Adaptor& adaptor) 392 : MapParent(adaptor) {} 393 NodeMap(const Adaptor& adaptor, const Value& value) 394 : MapParent(adaptor, value) {} 395 396 NodeMap& operator=(const NodeMap& cmap) { 397 return operator=<NodeMap>(cmap); 398 } 399 400 template <typename CMap> 401 NodeMap& operator=(const CMap& cmap) { 402 MapParent::operator=(cmap); 403 return *this; 404 } 405 }; 406 407 template <typename _Value> 408 class ArcMap : public SubMapExtender<Adaptor, 409 typename Parent::template ArcMap<_Value> > { 410 public: 411 typedef _Value Value; 412 typedef SubMapExtender<Adaptor, typename Parent:: 413 template ArcMap<Value> > MapParent; 414 415 ArcMap(const Adaptor& adaptor) 416 : MapParent(adaptor) {} 417 ArcMap(const Adaptor& adaptor, const Value& value) 418 : MapParent(adaptor, value) {} 419 420 ArcMap& operator=(const ArcMap& cmap) { 421 return operator=<ArcMap>(cmap); 422 } 423 424 template <typename CMap> 425 ArcMap& operator=(const CMap& cmap) { 426 MapParent::operator=(cmap); 427 return *this; 428 } 429 }; 430 431 template <typename _Value> 432 class EdgeMap : public SubMapExtender<Adaptor, 433 typename Parent::template EdgeMap<_Value> > { 434 public: 435 typedef _Value Value; 436 typedef SubMapExtender<Adaptor, typename Parent:: 437 template EdgeMap<Value> > MapParent; 438 439 EdgeMap(const Adaptor& adaptor) 440 : MapParent(adaptor) {} 441 442 EdgeMap(const Adaptor& adaptor, const Value& value) 443 : MapParent(adaptor, value) {} 444 445 EdgeMap& operator=(const EdgeMap& cmap) { 446 return operator=<EdgeMap>(cmap); 447 } 448 449 template <typename CMap> 450 EdgeMap& operator=(const CMap& cmap) { 451 MapParent::operator=(cmap); 452 return *this; 453 } 454 }; 455 456 }; 457 458 template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap> 459 class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false> 460 : public GraphAdaptorBase<_Graph> { 461 public: 462 typedef _Graph Graph; 463 typedef SubGraphAdaptorBase Adaptor; 464 typedef GraphAdaptorBase<_Graph> Parent; 465 protected: 466 NodeFilterMap* _node_filter_map; 467 EdgeFilterMap* _edge_filter_map; 468 SubGraphAdaptorBase() : Parent(), 469 _node_filter_map(0), _edge_filter_map(0) { } 470 471 void setNodeFilterMap(NodeFilterMap& node_filter_map) { 472 _node_filter_map=&node_filter_map; 473 } 474 void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { 475 _edge_filter_map=&edge_filter_map; 476 } 477 478 public: 479 480 typedef typename Parent::Node Node; 481 typedef typename Parent::Arc Arc; 482 typedef typename Parent::Edge Edge; 483 484 void first(Node& i) const { 485 Parent::first(i); 486 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); 487 } 488 489 void first(Arc& i) const { 490 Parent::first(i); 491 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); 492 } 493 494 void first(Edge& i) const { 495 Parent::first(i); 496 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); 497 } 498 499 void firstIn(Arc& i, const Node& n) const { 500 Parent::firstIn(i, n); 501 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); 502 } 503 504 void firstOut(Arc& i, const Node& n) const { 505 Parent::firstOut(i, n); 506 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); 507 } 508 509 void firstInc(Edge& i, bool& d, const Node& n) const { 510 Parent::firstInc(i, d, n); 511 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); 512 } 513 514 void next(Node& i) const { 515 Parent::next(i); 516 while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); 517 } 518 void next(Arc& i) const { 519 Parent::next(i); 520 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); 521 } 522 void next(Edge& i) const { 523 Parent::next(i); 524 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); 525 } 526 void nextIn(Arc& i) const { 527 Parent::nextIn(i); 528 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); 529 } 530 531 void nextOut(Arc& i) const { 532 Parent::nextOut(i); 533 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); 534 } 535 void nextInc(Edge& i, bool& d) const { 536 Parent::nextInc(i, d); 537 while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); 538 } 539 540 /// \brief Hide the given node in the graph. 541 /// 542 /// This function hides \c n in the graph, i.e. the iteration 543 /// jumps over it. This is done by simply setting the value of \c n 544 /// to be false in the corresponding node-map. 545 void hide(const Node& n) const { _node_filter_map->set(n, false); } 546 547 /// \brief Hide the given edge in the graph. 548 /// 549 /// This function hides \c e in the graph, i.e. the iteration 550 /// jumps over it. This is done by simply setting the value of \c e 551 /// to be false in the corresponding edge-map. 552 void hide(const Edge& e) const { _edge_filter_map->set(e, false); } 553 554 /// \brief Unhide the given node in the graph. 555 /// 556 /// The value of \c n is set to be true in the node-map which stores 557 /// hide information. If \c n was hidden previuosly, then it is shown 558 /// again 559 void unHide(const Node& n) const { _node_filter_map->set(n, true); } 560 561 /// \brief Hide the given edge in the graph. 562 /// 563 /// The value of \c e is set to be true in the edge-map which stores 564 /// hide information. If \c e was hidden previuosly, then it is shown 565 /// again 566 void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } 567 568 /// \brief Returns true if \c n is hidden. 569 /// 570 /// Returns true if \c n is hidden. 571 bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } 572 573 /// \brief Returns true if \c e is hidden. 574 /// 575 /// Returns true if \c e is hidden. 576 bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } 577 578 typedef False NodeNumTag; 579 typedef False EdgeNumTag; 580 581 typedef FindEdgeTagIndicator<Graph> FindEdgeTag; 582 Arc findArc(const Node& u, const Node& v, 583 const Arc& prev = INVALID) { 584 Arc arc = Parent::findArc(u, v, prev); 585 while (arc != INVALID && !(*_edge_filter_map)[arc]) { 586 arc = Parent::findArc(u, v, arc); 587 } 588 return arc; 589 } 590 Edge findEdge(const Node& u, const Node& v, 591 const Edge& prev = INVALID) { 592 Edge edge = Parent::findEdge(u, v, prev); 593 while (edge != INVALID && !(*_edge_filter_map)[edge]) { 594 edge = Parent::findEdge(u, v, edge); 595 } 596 return edge; 597 } 598 599 template <typename _Value> 600 class NodeMap : public SubMapExtender<Adaptor, 601 typename Parent::template NodeMap<_Value> > { 602 public: 603 typedef _Value Value; 604 typedef SubMapExtender<Adaptor, typename Parent:: 605 template NodeMap<Value> > MapParent; 606 607 NodeMap(const Adaptor& adaptor) 608 : MapParent(adaptor) {} 609 NodeMap(const Adaptor& adaptor, const Value& value) 610 : MapParent(adaptor, value) {} 611 612 NodeMap& operator=(const NodeMap& cmap) { 613 return operator=<NodeMap>(cmap); 614 } 615 616 template <typename CMap> 617 NodeMap& operator=(const CMap& cmap) { 618 MapParent::operator=(cmap); 619 return *this; 620 } 621 }; 622 623 template <typename _Value> 624 class ArcMap : public SubMapExtender<Adaptor, 625 typename Parent::template ArcMap<_Value> > { 626 public: 627 typedef _Value Value; 628 typedef SubMapExtender<Adaptor, typename Parent:: 629 template ArcMap<Value> > MapParent; 630 631 ArcMap(const Adaptor& adaptor) 632 : MapParent(adaptor) {} 633 ArcMap(const Adaptor& adaptor, const Value& value) 634 : MapParent(adaptor, value) {} 635 636 ArcMap& operator=(const ArcMap& cmap) { 637 return operator=<ArcMap>(cmap); 638 } 639 640 template <typename CMap> 641 ArcMap& operator=(const CMap& cmap) { 642 MapParent::operator=(cmap); 643 return *this; 644 } 645 }; 646 647 template <typename _Value> 648 class EdgeMap : public SubMapExtender<Adaptor, 649 typename Parent::template EdgeMap<_Value> > { 650 public: 651 typedef _Value Value; 652 typedef SubMapExtender<Adaptor, typename Parent:: 653 template EdgeMap<Value> > MapParent; 654 655 EdgeMap(const Adaptor& adaptor) 656 : MapParent(adaptor) {} 657 658 EdgeMap(const Adaptor& adaptor, const _Value& value) 659 : MapParent(adaptor, value) {} 660 661 EdgeMap& operator=(const EdgeMap& cmap) { 662 return operator=<EdgeMap>(cmap); 663 } 664 665 template <typename CMap> 666 EdgeMap& operator=(const CMap& cmap) { 667 MapParent::operator=(cmap); 668 return *this; 669 } 670 }; 671 672 }; 673 674 /// \ingroup graph_adaptors 675 /// 676 /// \brief A graph adaptor for hiding nodes and arcs from an 677 /// undirected graph. 678 /// 679 /// SubGraphAdaptor shows the graph with filtered node-set and 680 /// edge-set. If the \c checked parameter is true then it filters 681 /// the edge-set to do not get invalid edges which incident node is 682 /// filtered. 683 /// 684 /// If the \c checked template parameter is false then we have to 685 /// note that the node-iterator cares only the filter on the 686 /// node-set, and the edge-iterator cares only the filter on the 687 /// edge-set. This way the edge-map should filter all arcs which 688 /// has filtered end node. 689 template<typename _Graph, typename NodeFilterMap, 690 typename EdgeFilterMap, bool checked = true> 691 class SubGraphAdaptor : 692 public GraphAdaptorExtender< 693 SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > { 694 public: 695 typedef _Graph Graph; 696 typedef GraphAdaptorExtender< 697 SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent; 698 protected: 699 SubGraphAdaptor() { } 700 public: 701 SubGraphAdaptor(Graph& _graph, NodeFilterMap& node_filter_map, 702 EdgeFilterMap& edge_filter_map) { 703 setGraph(_graph); 704 setNodeFilterMap(node_filter_map); 705 setEdgeFilterMap(edge_filter_map); 706 } 707 }; 708 709 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> 710 SubGraphAdaptor<const Graph, NodeFilterMap, ArcFilterMap> 711 subGraphAdaptor(const Graph& graph, 712 NodeFilterMap& nfm, ArcFilterMap& efm) { 713 return SubGraphAdaptor<const Graph, NodeFilterMap, ArcFilterMap> 714 (graph, nfm, efm); 715 } 716 717 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> 718 SubGraphAdaptor<const Graph, const NodeFilterMap, ArcFilterMap> 719 subGraphAdaptor(const Graph& graph, 720 NodeFilterMap& nfm, ArcFilterMap& efm) { 721 return SubGraphAdaptor<const Graph, const NodeFilterMap, ArcFilterMap> 722 (graph, nfm, efm); 723 } 724 725 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> 726 SubGraphAdaptor<const Graph, NodeFilterMap, const ArcFilterMap> 727 subGraphAdaptor(const Graph& graph, 728 NodeFilterMap& nfm, ArcFilterMap& efm) { 729 return SubGraphAdaptor<const Graph, NodeFilterMap, const ArcFilterMap> 730 (graph, nfm, efm); 731 } 732 733 template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> 734 SubGraphAdaptor<const Graph, const NodeFilterMap, const ArcFilterMap> 735 subGraphAdaptor(const Graph& graph, 736 NodeFilterMap& nfm, ArcFilterMap& efm) { 737 return SubGraphAdaptor<const Graph, const NodeFilterMap, 738 const ArcFilterMap>(graph, nfm, efm); 739 } 740 741 /// \ingroup graph_adaptors 742 /// 743 /// \brief An adaptor for hiding nodes from an graph. 744 /// 745 /// An adaptor for hiding nodes from an graph. This 746 /// adaptor specializes SubGraphAdaptor in the way that only the 747 /// node-set can be filtered. In usual case the checked parameter is 748 /// true, we get the induced subgraph. But if the checked parameter 749 /// is false then we can filter only isolated nodes. 750 template<typename _Graph, typename _NodeFilterMap, bool checked = true> 751 class NodeSubGraphAdaptor : 752 public SubGraphAdaptor<_Graph, _NodeFilterMap, 753 ConstMap<typename _Graph::Edge, bool>, checked> { 754 public: 755 typedef _Graph Graph; 756 typedef _NodeFilterMap NodeFilterMap; 757 typedef SubGraphAdaptor<Graph, NodeFilterMap, 758 ConstMap<typename Graph::Edge, bool> > Parent; 759 protected: 760 ConstMap<typename Graph::Edge, bool> const_true_map; 761 762 NodeSubGraphAdaptor() : const_true_map(true) { 763 Parent::setEdgeFilterMap(const_true_map); 764 } 765 766 public: 767 NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& node_filter_map) : 768 Parent(), const_true_map(true) { 769 Parent::setGraph(_graph); 770 Parent::setNodeFilterMap(node_filter_map); 771 Parent::setEdgeFilterMap(const_true_map); 772 } 773 }; 774 775 template<typename Graph, typename NodeFilterMap> 776 NodeSubGraphAdaptor<const Graph, NodeFilterMap> 777 nodeSubGraphAdaptor(const Graph& graph, NodeFilterMap& nfm) { 778 return NodeSubGraphAdaptor<const Graph, NodeFilterMap>(graph, nfm); 779 } 780 781 template<typename Graph, typename NodeFilterMap> 782 NodeSubGraphAdaptor<const Graph, const NodeFilterMap> 783 nodeSubGraphAdaptor(const Graph& graph, const NodeFilterMap& nfm) { 784 return NodeSubGraphAdaptor<const Graph, const NodeFilterMap>(graph, nfm); 785 } 786 787 /// \ingroup graph_adaptors 788 /// 789 /// \brief An adaptor for hiding edges from an graph. 790 /// 791 /// \warning Graph adaptors are in even more experimental state 792 /// than the other parts of the lib. Use them at you own risk. 793 /// 794 /// An adaptor for hiding edges from an graph. 795 /// This adaptor specializes SubGraphAdaptor in the way that 796 /// only the arc-set 797 /// can be filtered. 798 template<typename _Graph, typename _EdgeFilterMap> 799 class EdgeSubGraphAdaptor : 800 public SubGraphAdaptor<_Graph, ConstMap<typename _Graph::Node,bool>, 801 _EdgeFilterMap, false> { 802 public: 803 typedef _Graph Graph; 804 typedef _EdgeFilterMap EdgeFilterMap; 805 typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>, 806 EdgeFilterMap, false> Parent; 807 protected: 808 ConstMap<typename Graph::Node, bool> const_true_map; 809 810 EdgeSubGraphAdaptor() : const_true_map(true) { 811 Parent::setNodeFilterMap(const_true_map); 812 } 813 814 public: 815 816 EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& edge_filter_map) : 817 Parent(), const_true_map(true) { 818 Parent::setGraph(_graph); 819 Parent::setNodeFilterMap(const_true_map); 820 Parent::setEdgeFilterMap(edge_filter_map); 821 } 822 823 }; 824 825 template<typename Graph, typename EdgeFilterMap> 826 EdgeSubGraphAdaptor<const Graph, EdgeFilterMap> 827 edgeSubGraphAdaptor(const Graph& graph, EdgeFilterMap& efm) { 828 return EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>(graph, efm); 829 } 830 831 template<typename Graph, typename EdgeFilterMap> 832 EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap> 833 edgeSubGraphAdaptor(const Graph& graph, const EdgeFilterMap& efm) { 834 return EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>(graph, efm); 835 } 836 837 /// \brief Base of direct graph adaptor 838 /// 839 /// Base class of the direct graph adaptor. All public member 840 /// of this class can be used with the DirGraphAdaptor too. 841 /// \sa DirGraphAdaptor 842 template <typename _Graph, typename _DirectionMap> 843 class DirGraphAdaptorBase { 844 public: 845 846 typedef _Graph Graph; 847 typedef _DirectionMap DirectionMap; 848 849 typedef typename Graph::Node Node; 850 typedef typename Graph::Edge Arc; 851 852 /// \brief Reverse arc 853 /// 854 /// It reverse the given arc. It simply negate the direction in the map. 855 void reverseArc(const Arc& arc) { 856 _direction->set(arc, !(*_direction)[arc]); 857 } 858 859 void first(Node& i) const { _graph->first(i); } 860 void first(Arc& i) const { _graph->first(i); } 861 void firstIn(Arc& i, const Node& n) const { 862 bool d; 863 _graph->firstInc(i, d, n); 864 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); 865 } 866 void firstOut(Arc& i, const Node& n ) const { 867 bool d; 868 _graph->firstInc(i, d, n); 869 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); 870 } 871 872 void next(Node& i) const { _graph->next(i); } 873 void next(Arc& i) const { _graph->next(i); } 874 void nextIn(Arc& i) const { 875 bool d = !(*_direction)[i]; 876 _graph->nextInc(i, d); 877 while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); 878 } 879 void nextOut(Arc& i) const { 880 bool d = (*_direction)[i]; 881 _graph->nextInc(i, d); 882 while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); 883 } 884 885 Node source(const Arc& e) const { 886 return (*_direction)[e] ? _graph->u(e) : _graph->v(e); 887 } 888 Node target(const Arc& e) const { 889 return (*_direction)[e] ? _graph->v(e) : _graph->u(e); 890 } 891 892 typedef NodeNumTagIndicator<Graph> NodeNumTag; 893 int nodeNum() const { return _graph->nodeNum(); } 894 895 typedef EdgeNumTagIndicator<Graph> EdgeNumTag; 896 int arcNum() const { return _graph->edgeNum(); } 897 898 typedef FindEdgeTagIndicator<Graph> FindEdgeTag; 899 Arc findArc(const Node& u, const Node& v, 900 const Arc& prev = INVALID) { 901 Arc arc = prev; 902 bool d = arc == INVALID ? true : (*_direction)[arc]; 903 if (d) { 904 arc = _graph->findEdge(u, v, arc); 905 while (arc != INVALID && !(*_direction)[arc]) { 906 _graph->findEdge(u, v, arc); 907 } 908 if (arc != INVALID) return arc; 909 } 910 _graph->findEdge(v, u, arc); 911 while (arc != INVALID && (*_direction)[arc]) { 912 _graph->findEdge(u, v, arc); 913 } 914 return arc; 915 } 916 917 Node addNode() { 918 return Node(_graph->addNode()); 919 } 920 921 Arc addArc(const Node& u, const Node& v) { 922 Arc arc = _graph->addArc(u, v); 923 _direction->set(arc, _graph->source(arc) == u); 924 return arc; 925 } 926 927 void erase(const Node& i) { _graph->erase(i); } 928 void erase(const Arc& i) { _graph->erase(i); } 929 930 void clear() { _graph->clear(); } 931 932 int id(const Node& v) const { return _graph->id(v); } 933 int id(const Arc& e) const { return _graph->id(e); } 934 935 Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } 936 Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } 937 938 int maxNodeId() const { return _graph->maxNodeId(); } 939 int maxArcId() const { return _graph->maxEdgeId(); } 940 941 typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; 942 NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } 943 944 typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; 945 ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } 946 947 template <typename _Value> 948 class NodeMap : public _Graph::template NodeMap<_Value> { 949 public: 950 951 typedef typename _Graph::template NodeMap<_Value> Parent; 952 953 explicit NodeMap(const DirGraphAdaptorBase& adapter) 954 : Parent(*adapter._graph) {} 955 956 NodeMap(const DirGraphAdaptorBase& adapter, const _Value& value) 957 : Parent(*adapter._graph, value) {} 958 959 NodeMap& operator=(const NodeMap& cmap) { 960 return operator=<NodeMap>(cmap); 961 } 962 963 template <typename CMap> 964 NodeMap& operator=(const CMap& cmap) { 965 Parent::operator=(cmap); 966 return *this; 967 } 968 969 }; 970 971 template <typename _Value> 972 class ArcMap : public _Graph::template EdgeMap<_Value> { 973 public: 974 975 typedef typename Graph::template EdgeMap<_Value> Parent; 976 977 explicit ArcMap(const DirGraphAdaptorBase& adapter) 978 : Parent(*adapter._graph) { } 979 980 ArcMap(const DirGraphAdaptorBase& adapter, const _Value& value) 981 : Parent(*adapter._graph, value) { } 982 983 ArcMap& operator=(const ArcMap& cmap) { 984 return operator=<ArcMap>(cmap); 985 } 986 987 template <typename CMap> 988 ArcMap& operator=(const CMap& cmap) { 989 Parent::operator=(cmap); 990 return *this; 991 } 992 }; 993 994 995 996 protected: 997 Graph* _graph; 998 DirectionMap* _direction; 999 1000 void setDirectionMap(DirectionMap& direction) { 1001 _direction = &direction; 1002 } 1003 1004 void setGraph(Graph& graph) { 1005 _graph = &graph; 1006 } 1007 1008 }; 1009 1010 1011 /// \ingroup graph_adaptors 1012 /// 1013 /// \brief A directed graph is made from an graph by an adaptor 1014 /// 1015 /// This adaptor gives a direction for each edge in the undirected 1016 /// graph. The direction of the arcs stored in the 1017 /// DirectionMap. This map is a bool map on the edges. If 1018 /// the edge is mapped to true then the direction of the directed 1019 /// arc will be the same as the default direction of the edge. The 1020 /// arcs can be easily reverted by the \ref 1021 /// DirGraphAdaptorBase::reverseArc "reverseArc()" member in the 1022 /// adaptor. 1023 /// 1024 /// It can be used to solve orientation problems on directed graphs. 1025 /// For example how can we orient an graph to get the minimum 1026 /// number of strongly connected components. If we orient the arcs with 1027 /// the dfs algorithm out from the source then we will get such an 1028 /// orientation. 1029 /// 1030 /// We use the \ref DfsVisitor "visitor" interface of the 1031 /// \ref DfsVisit "dfs" algorithm: 1032 ///\code 1033 /// template <typename DirMap> 1034 /// class OrientVisitor : public DfsVisitor<Graph> { 1035 /// public: 1036 /// 1037 /// OrientVisitor(const Graph& graph, DirMap& dirMap) 1038 /// : _graph(graph), _dirMap(dirMap), _processed(graph, false) {} 1039 /// 1040 /// void discover(const Arc& arc) { 1041 /// _processed.set(arc, true); 1042 /// _dirMap.set(arc, _graph.direction(arc)); 1043 /// } 1044 /// 1045 /// void examine(const Arc& arc) { 1046 /// if (_processed[arc]) return; 1047 /// _processed.set(arc, true); 1048 /// _dirMap.set(arc, _graph.direction(arc)); 1049 /// } 1050 /// 1051 /// private: 1052 /// const Graph& _graph; 1053 /// DirMap& _dirMap; 1054 /// Graph::EdgeMap<bool> _processed; 1055 /// }; 1056 ///\endcode 1057 /// 1058 /// And now we can use the orientation: 1059 ///\code 1060 /// Graph::EdgeMap<bool> dmap(graph); 1061 /// 1062 /// typedef OrientVisitor<Graph::EdgeMap<bool> > Visitor; 1063 /// Visitor visitor(graph, dmap); 1064 /// 1065 /// DfsVisit<Graph, Visitor> dfs(graph, visitor); 1066 /// 1067 /// dfs.run(); 1068 /// 1069 /// typedef DirGraphAdaptor<Graph> DGraph; 1070 /// DGraph dgraph(graph, dmap); 1071 /// 1072 /// LEMON_ASSERT(countStronglyConnectedComponents(dgraph) == 1073 /// countBiArcConnectedComponents(graph), "Wrong Orientation"); 1074 ///\endcode 1075 /// 1076 /// The number of the bi-connected components is a lower bound for 1077 /// the number of the strongly connected components in the directed 1078 /// graph because if we contract the bi-connected components to 1079 /// nodes we will get a tree therefore we cannot orient arcs in 1080 /// both direction between bi-connected components. In the other way 1081 /// the algorithm will orient one component to be strongly 1082 /// connected. The two relations proof that the assertion will 1083 /// be always true and the found solution is optimal. 1084 /// 1085 /// \sa DirGraphAdaptorBase 1086 /// \sa dirGraphAdaptor 1087 template<typename _Graph, 1088 typename DirectionMap = typename _Graph::template EdgeMap<bool> > 1089 class DirGraphAdaptor : 1090 public DigraphAdaptorExtender<DirGraphAdaptorBase<_Graph, DirectionMap> > { 1091 public: 1092 typedef _Graph Graph; 1093 typedef DigraphAdaptorExtender< 1094 DirGraphAdaptorBase<_Graph, DirectionMap> > Parent; 1095 protected: 1096 DirGraphAdaptor() { } 1097 public: 1098 1099 /// \brief Constructor of the adaptor 1100 /// 1101 /// Constructor of the adaptor 1102 DirGraphAdaptor(Graph& graph, DirectionMap& direction) { 1103 setGraph(graph); 1104 setDirectionMap(direction); 1105 } 1106 }; 1107 1108 /// \brief Just gives back a DirGraphAdaptor 1109 /// 1110 /// Just gives back a DirGraphAdaptor 1111 template<typename Graph, typename DirectionMap> 1112 DirGraphAdaptor<const Graph, DirectionMap> 1113 dirGraphAdaptor(const Graph& graph, DirectionMap& dm) { 1114 return DirGraphAdaptor<const Graph, DirectionMap>(graph, dm); 1115 } 1116 1117 template<typename Graph, typename DirectionMap> 1118 DirGraphAdaptor<const Graph, const DirectionMap> 1119 dirGraphAdaptor(const Graph& graph, const DirectionMap& dm) { 1120 return DirGraphAdaptor<const Graph, const DirectionMap>(graph, dm); 1121 } 1122 1123 } 1124 1125 #endif -
test/Makefile.am
diff -r b6732e0d38c5 -r e67acd83a9ca test/Makefile.am
a b 13 13 test/dijkstra_test \ 14 14 test/dim_test \ 15 15 test/error_test \ 16 test/graph_adaptor_test \ 16 17 test/graph_copy_test \ 17 18 test/graph_test \ 18 19 test/graph_utils_test \ … … 36 37 test_dijkstra_test_SOURCES = test/dijkstra_test.cc 37 38 test_dim_test_SOURCES = test/dim_test.cc 38 39 test_error_test_SOURCES = test/error_test.cc 40 test_graph_adaptor_test_SOURCES = test/graph_adaptor_test.cc 39 41 test_graph_copy_test_SOURCES = test/graph_copy_test.cc 40 42 test_graph_test_SOURCES = test/graph_test.cc 41 43 test_graph_utils_test_SOURCES = test/graph_utils_test.cc -
new file test/graph_adaptor_test.cc
diff -r b6732e0d38c5 -r e67acd83a9ca test/graph_adaptor_test.cc
- + 1 /* -*- C++ -*- 2 * 3 * This file is a part of LEMON, a generic C++ optimization library 4 * 5 * Copyright (C) 2003-2008 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 19 #include<iostream> 20 #include<lemon/concept_check.h> 21 22 #include<lemon/list_graph.h> 23 #include<lemon/smart_graph.h> 24 25 #include<lemon/concepts/digraph.h> 26 #include<lemon/concepts/graph.h> 27 28 #include<lemon/digraph_adaptor.h> 29 #include<lemon/graph_adaptor.h> 30 31 #include <limits> 32 #include <lemon/bfs.h> 33 #include <lemon/path.h> 34 35 #include"test/test_tools.h" 36 #include"test/graph_test.h" 37 38 using namespace lemon; 39 40 void checkDigraphAdaptor() { 41 checkConcept<concepts::Digraph, DigraphAdaptor<concepts::Digraph> >(); 42 43 typedef ListDigraph Digraph; 44 typedef DigraphAdaptor<Digraph> Adaptor; 45 46 Digraph digraph; 47 Adaptor adaptor(digraph); 48 49 Digraph::Node n1 = digraph.addNode(); 50 Digraph::Node n2 = digraph.addNode(); 51 Digraph::Node n3 = digraph.addNode(); 52 53 Digraph::Arc a1 = digraph.addArc(n1, n2); 54 Digraph::Arc a2 = digraph.addArc(n1, n3); 55 Digraph::Arc a3 = digraph.addArc(n2, n3); 56 57 checkGraphNodeList(adaptor, 3); 58 checkGraphArcList(adaptor, 3); 59 checkGraphConArcList(adaptor, 3); 60 61 checkGraphOutArcList(adaptor, n1, 2); 62 checkGraphOutArcList(adaptor, n2, 1); 63 checkGraphOutArcList(adaptor, n3, 0); 64 65 checkGraphInArcList(adaptor, n1, 0); 66 checkGraphInArcList(adaptor, n2, 1); 67 checkGraphInArcList(adaptor, n3, 2); 68 69 checkNodeIds(adaptor); 70 checkArcIds(adaptor); 71 72 checkGraphNodeMap(adaptor); 73 checkGraphArcMap(adaptor); 74 } 75 76 void checkRevDigraphAdaptor() { 77 checkConcept<concepts::Digraph, RevDigraphAdaptor<concepts::Digraph> >(); 78 79 typedef ListDigraph Digraph; 80 typedef RevDigraphAdaptor<Digraph> Adaptor; 81 82 Digraph digraph; 83 Adaptor adaptor(digraph); 84 85 Digraph::Node n1 = digraph.addNode(); 86 Digraph::Node n2 = digraph.addNode(); 87 Digraph::Node n3 = digraph.addNode(); 88 89 Digraph::Arc a1 = digraph.addArc(n1, n2); 90 Digraph::Arc a2 = digraph.addArc(n1, n3); 91 Digraph::Arc a3 = digraph.addArc(n2, n3); 92 93 checkGraphNodeList(adaptor, 3); 94 checkGraphArcList(adaptor, 3); 95 checkGraphConArcList(adaptor, 3); 96 97 checkGraphOutArcList(adaptor, n1, 0); 98 checkGraphOutArcList(adaptor, n2, 1); 99 checkGraphOutArcList(adaptor, n3, 2); 100 101 checkGraphInArcList(adaptor, n1, 2); 102 checkGraphInArcList(adaptor, n2, 1); 103 checkGraphInArcList(adaptor, n3, 0); 104 105 checkNodeIds(adaptor); 106 checkArcIds(adaptor); 107 108 checkGraphNodeMap(adaptor); 109 checkGraphArcMap(adaptor); 110 111 for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { 112 check(adaptor.source(a) == digraph.target(a), "Wrong reverse"); 113 check(adaptor.target(a) == digraph.source(a), "Wrong reverse"); 114 } 115 } 116 117 void checkSubDigraphAdaptor() { 118 checkConcept<concepts::Digraph, 119 SubDigraphAdaptor<concepts::Digraph, 120 concepts::Digraph::NodeMap<bool>, 121 concepts::Digraph::ArcMap<bool> > >(); 122 123 typedef ListDigraph Digraph; 124 typedef Digraph::NodeMap<bool> NodeFilter; 125 typedef Digraph::ArcMap<bool> ArcFilter; 126 typedef SubDigraphAdaptor<Digraph, NodeFilter, ArcFilter> Adaptor; 127 128 Digraph digraph; 129 NodeFilter node_filter(digraph); 130 ArcFilter arc_filter(digraph); 131 Adaptor adaptor(digraph, node_filter, arc_filter); 132 133 Digraph::Node n1 = digraph.addNode(); 134 Digraph::Node n2 = digraph.addNode(); 135 Digraph::Node n3 = digraph.addNode(); 136 137 Digraph::Arc a1 = digraph.addArc(n1, n2); 138 Digraph::Arc a2 = digraph.addArc(n1, n3); 139 Digraph::Arc a3 = digraph.addArc(n2, n3); 140 141 node_filter = constMap<Digraph::Node>(true); 142 arc_filter = constMap<Digraph::Arc>(true); 143 144 checkGraphNodeList(adaptor, 3); 145 checkGraphArcList(adaptor, 3); 146 checkGraphConArcList(adaptor, 3); 147 148 checkGraphOutArcList(adaptor, n1, 2); 149 checkGraphOutArcList(adaptor, n2, 1); 150 checkGraphOutArcList(adaptor, n3, 0); 151 152 checkGraphInArcList(adaptor, n1, 0); 153 checkGraphInArcList(adaptor, n2, 1); 154 checkGraphInArcList(adaptor, n3, 2); 155 156 checkNodeIds(adaptor); 157 checkArcIds(adaptor); 158 159 checkGraphNodeMap(adaptor); 160 checkGraphArcMap(adaptor); 161 162 arc_filter[a2] = false; 163 164 checkGraphNodeList(adaptor, 3); 165 checkGraphArcList(adaptor, 2); 166 checkGraphConArcList(adaptor, 2); 167 168 checkGraphOutArcList(adaptor, n1, 1); 169 checkGraphOutArcList(adaptor, n2, 1); 170 checkGraphOutArcList(adaptor, n3, 0); 171 172 checkGraphInArcList(adaptor, n1, 0); 173 checkGraphInArcList(adaptor, n2, 1); 174 checkGraphInArcList(adaptor, n3, 1); 175 176 checkNodeIds(adaptor); 177 checkArcIds(adaptor); 178 179 checkGraphNodeMap(adaptor); 180 checkGraphArcMap(adaptor); 181 182 node_filter[n1] = false; 183 184 checkGraphNodeList(adaptor, 2); 185 checkGraphArcList(adaptor, 1); 186 checkGraphConArcList(adaptor, 1); 187 188 checkGraphOutArcList(adaptor, n2, 1); 189 checkGraphOutArcList(adaptor, n3, 0); 190 191 checkGraphInArcList(adaptor, n2, 0); 192 checkGraphInArcList(adaptor, n3, 1); 193 194 checkNodeIds(adaptor); 195 checkArcIds(adaptor); 196 197 checkGraphNodeMap(adaptor); 198 checkGraphArcMap(adaptor); 199 200 node_filter = constMap<Digraph::Node>(false); 201 arc_filter = constMap<Digraph::Arc>(false); 202 203 checkGraphNodeList(adaptor, 0); 204 checkGraphArcList(adaptor, 0); 205 checkGraphConArcList(adaptor, 0); 206 207 checkNodeIds(adaptor); 208 checkArcIds(adaptor); 209 210 checkGraphNodeMap(adaptor); 211 checkGraphArcMap(adaptor); 212 } 213 214 void checkNodeSubDigraphAdaptor() { 215 checkConcept<concepts::Digraph, 216 NodeSubDigraphAdaptor<concepts::Digraph, 217 concepts::Digraph::NodeMap<bool> > >(); 218 219 typedef ListDigraph Digraph; 220 typedef Digraph::NodeMap<bool> NodeFilter; 221 typedef NodeSubDigraphAdaptor<Digraph, NodeFilter> Adaptor; 222 223 Digraph digraph; 224 NodeFilter node_filter(digraph); 225 Adaptor adaptor(digraph, node_filter); 226 227 Digraph::Node n1 = digraph.addNode(); 228 Digraph::Node n2 = digraph.addNode(); 229 Digraph::Node n3 = digraph.addNode(); 230 231 Digraph::Arc a1 = digraph.addArc(n1, n2); 232 Digraph::Arc a2 = digraph.addArc(n1, n3); 233 Digraph::Arc a3 = digraph.addArc(n2, n3); 234 235 node_filter = constMap<Digraph::Node>(true); 236 237 checkGraphNodeList(adaptor, 3); 238 checkGraphArcList(adaptor, 3); 239 checkGraphConArcList(adaptor, 3); 240 241 checkGraphOutArcList(adaptor, n1, 2); 242 checkGraphOutArcList(adaptor, n2, 1); 243 checkGraphOutArcList(adaptor, n3, 0); 244 245 checkGraphInArcList(adaptor, n1, 0); 246 checkGraphInArcList(adaptor, n2, 1); 247 checkGraphInArcList(adaptor, n3, 2); 248 249 checkNodeIds(adaptor); 250 checkArcIds(adaptor); 251 252 checkGraphNodeMap(adaptor); 253 checkGraphArcMap(adaptor); 254 255 node_filter[n1] = false; 256 257 checkGraphNodeList(adaptor, 2); 258 checkGraphArcList(adaptor, 1); 259 checkGraphConArcList(adaptor, 1); 260 261 checkGraphOutArcList(adaptor, n2, 1); 262 checkGraphOutArcList(adaptor, n3, 0); 263 264 checkGraphInArcList(adaptor, n2, 0); 265 checkGraphInArcList(adaptor, n3, 1); 266 267 checkNodeIds(adaptor); 268 checkArcIds(adaptor); 269 270 checkGraphNodeMap(adaptor); 271 checkGraphArcMap(adaptor); 272 273 node_filter = constMap<Digraph::Node>(false); 274 275 checkGraphNodeList(adaptor, 0); 276 checkGraphArcList(adaptor, 0); 277 checkGraphConArcList(adaptor, 0); 278 279 checkNodeIds(adaptor); 280 checkArcIds(adaptor); 281 282 checkGraphNodeMap(adaptor); 283 checkGraphArcMap(adaptor); 284 } 285 286 void checkArcSubDigraphAdaptor() { 287 checkConcept<concepts::Digraph, 288 ArcSubDigraphAdaptor<concepts::Digraph, 289 concepts::Digraph::ArcMap<bool> > >(); 290 291 typedef ListDigraph Digraph; 292 typedef Digraph::ArcMap<bool> ArcFilter; 293 typedef ArcSubDigraphAdaptor<Digraph, ArcFilter> Adaptor; 294 295 Digraph digraph; 296 ArcFilter arc_filter(digraph); 297 Adaptor adaptor(digraph, arc_filter); 298 299 Digraph::Node n1 = digraph.addNode(); 300 Digraph::Node n2 = digraph.addNode(); 301 Digraph::Node n3 = digraph.addNode(); 302 303 Digraph::Arc a1 = digraph.addArc(n1, n2); 304 Digraph::Arc a2 = digraph.addArc(n1, n3); 305 Digraph::Arc a3 = digraph.addArc(n2, n3); 306 307 arc_filter = constMap<Digraph::Arc>(true); 308 309 checkGraphNodeList(adaptor, 3); 310 checkGraphArcList(adaptor, 3); 311 checkGraphConArcList(adaptor, 3); 312 313 checkGraphOutArcList(adaptor, n1, 2); 314 checkGraphOutArcList(adaptor, n2, 1); 315 checkGraphOutArcList(adaptor, n3, 0); 316 317 checkGraphInArcList(adaptor, n1, 0); 318 checkGraphInArcList(adaptor, n2, 1); 319 checkGraphInArcList(adaptor, n3, 2); 320 321 checkNodeIds(adaptor); 322 checkArcIds(adaptor); 323 324 checkGraphNodeMap(adaptor); 325 checkGraphArcMap(adaptor); 326 327 arc_filter[a2] = false; 328 329 checkGraphNodeList(adaptor, 3); 330 checkGraphArcList(adaptor, 2); 331 checkGraphConArcList(adaptor, 2); 332 333 checkGraphOutArcList(adaptor, n1, 1); 334 checkGraphOutArcList(adaptor, n2, 1); 335 checkGraphOutArcList(adaptor, n3, 0); 336 337 checkGraphInArcList(adaptor, n1, 0); 338 checkGraphInArcList(adaptor, n2, 1); 339 checkGraphInArcList(adaptor, n3, 1); 340 341 checkNodeIds(adaptor); 342 checkArcIds(adaptor); 343 344 checkGraphNodeMap(adaptor); 345 checkGraphArcMap(adaptor); 346 347 arc_filter = constMap<Digraph::Arc>(false); 348 349 checkGraphNodeList(adaptor, 3); 350 checkGraphArcList(adaptor, 0); 351 checkGraphConArcList(adaptor, 0); 352 353 checkNodeIds(adaptor); 354 checkArcIds(adaptor); 355 356 checkGraphNodeMap(adaptor); 357 checkGraphArcMap(adaptor); 358 } 359 360 void checkUndirDigraphAdaptor() { 361 checkConcept<concepts::Graph, UndirDigraphAdaptor<concepts::Digraph> >(); 362 363 typedef ListDigraph Digraph; 364 typedef UndirDigraphAdaptor<Digraph> Adaptor; 365 366 Digraph digraph; 367 Adaptor adaptor(digraph); 368 369 Digraph::Node n1 = digraph.addNode(); 370 Digraph::Node n2 = digraph.addNode(); 371 Digraph::Node n3 = digraph.addNode(); 372 373 Digraph::Arc a1 = digraph.addArc(n1, n2); 374 Digraph::Arc a2 = digraph.addArc(n1, n3); 375 Digraph::Arc a3 = digraph.addArc(n2, n3); 376 377 checkGraphNodeList(adaptor, 3); 378 checkGraphArcList(adaptor, 6); 379 checkGraphEdgeList(adaptor, 3); 380 checkGraphConArcList(adaptor, 6); 381 checkGraphConEdgeList(adaptor, 3); 382 383 checkGraphOutArcList(adaptor, n1, 2); 384 checkGraphOutArcList(adaptor, n2, 2); 385 checkGraphOutArcList(adaptor, n3, 2); 386 387 checkGraphInArcList(adaptor, n1, 2); 388 checkGraphInArcList(adaptor, n2, 2); 389 checkGraphInArcList(adaptor, n3, 2); 390 391 checkGraphIncEdgeList(adaptor, n1, 2); 392 checkGraphIncEdgeList(adaptor, n2, 2); 393 checkGraphIncEdgeList(adaptor, n3, 2); 394 395 checkNodeIds(adaptor); 396 checkArcIds(adaptor); 397 checkEdgeIds(adaptor); 398 399 checkGraphNodeMap(adaptor); 400 checkGraphArcMap(adaptor); 401 checkGraphEdgeMap(adaptor); 402 403 for (Adaptor::EdgeIt e(adaptor); e != INVALID; ++e) { 404 check(adaptor.u(e) == digraph.source(e), "Wrong undir"); 405 check(adaptor.v(e) == digraph.target(e), "Wrong undir"); 406 } 407 408 } 409 410 void checkResDigraphAdaptor() { 411 checkConcept<concepts::Digraph, 412 ResDigraphAdaptor<concepts::Digraph, 413 concepts::Digraph::ArcMap<int>, 414 concepts::Digraph::ArcMap<int> > >(); 415 416 typedef ListDigraph Digraph; 417 typedef Digraph::ArcMap<int> IntArcMap; 418 typedef ResDigraphAdaptor<Digraph, IntArcMap> Adaptor; 419 420 Digraph digraph; 421 IntArcMap capacity(digraph), flow(digraph); 422 Adaptor adaptor(digraph, capacity, flow); 423 424 Digraph::Node n1 = digraph.addNode(); 425 Digraph::Node n2 = digraph.addNode(); 426 Digraph::Node n3 = digraph.addNode(); 427 Digraph::Node n4 = digraph.addNode(); 428 429 Digraph::Arc a1 = digraph.addArc(n1, n2); 430 Digraph::Arc a2 = digraph.addArc(n1, n3); 431 Digraph::Arc a3 = digraph.addArc(n1, n4); 432 Digraph::Arc a4 = digraph.addArc(n2, n3); 433 Digraph::Arc a5 = digraph.addArc(n2, n4); 434 Digraph::Arc a6 = digraph.addArc(n3, n4); 435 436 capacity[a1] = 8; 437 capacity[a2] = 6; 438 capacity[a3] = 4; 439 capacity[a4] = 4; 440 capacity[a5] = 6; 441 capacity[a6] = 10; 442 443 for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { 444 flow[a] = 0; 445 } 446 447 checkGraphNodeList(adaptor, 4); 448 checkGraphArcList(adaptor, 6); 449 checkGraphConArcList(adaptor, 6); 450 451 checkGraphOutArcList(adaptor, n1, 3); 452 checkGraphOutArcList(adaptor, n2, 2); 453 checkGraphOutArcList(adaptor, n3, 1); 454 checkGraphOutArcList(adaptor, n4, 0); 455 456 checkGraphInArcList(adaptor, n1, 0); 457 checkGraphInArcList(adaptor, n2, 1); 458 checkGraphInArcList(adaptor, n3, 2); 459 checkGraphInArcList(adaptor, n4, 3); 460 461 for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { 462 flow[a] = capacity[a] / 2; 463 } 464 465 checkGraphNodeList(adaptor, 4); 466 checkGraphArcList(adaptor, 12); 467 checkGraphConArcList(adaptor, 12); 468 469 checkGraphOutArcList(adaptor, n1, 3); 470 checkGraphOutArcList(adaptor, n2, 3); 471 checkGraphOutArcList(adaptor, n3, 3); 472 checkGraphOutArcList(adaptor, n4, 3); 473 474 checkGraphInArcList(adaptor, n1, 3); 475 checkGraphInArcList(adaptor, n2, 3); 476 checkGraphInArcList(adaptor, n3, 3); 477 checkGraphInArcList(adaptor, n4, 3); 478 479 checkNodeIds(adaptor); 480 checkArcIds(adaptor); 481 482 checkGraphNodeMap(adaptor); 483 checkGraphArcMap(adaptor); 484 485 for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { 486 flow[a] = capacity[a]; 487 } 488 489 checkGraphNodeList(adaptor, 4); 490 checkGraphArcList(adaptor, 6); 491 checkGraphConArcList(adaptor, 6); 492 493 checkGraphOutArcList(adaptor, n1, 0); 494 checkGraphOutArcList(adaptor, n2, 1); 495 checkGraphOutArcList(adaptor, n3, 2); 496 checkGraphOutArcList(adaptor, n4, 3); 497 498 checkGraphInArcList(adaptor, n1, 3); 499 checkGraphInArcList(adaptor, n2, 2); 500 checkGraphInArcList(adaptor, n3, 1); 501 checkGraphInArcList(adaptor, n4, 0); 502 503 for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { 504 flow[a] = 0; 505 } 506 507 int flow_value = 0; 508 while (true) { 509 510 Bfs<Adaptor> bfs(adaptor); 511 bfs.run(n1, n4); 512 513 if (!bfs.reached(n4)) break; 514 515 Path<Adaptor> p = bfs.path(n4); 516 517 int min = std::numeric_limits<int>::max(); 518 for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) { 519 if (adaptor.rescap(a) < min) min = adaptor.rescap(a); 520 } 521 522 for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) { 523 adaptor.augment(a, min); 524 } 525 flow_value += min; 526 } 527 528 check(flow_value == 18, "Wrong flow with res graph adaptor"); 529 530 } 531 532 void checkSplitDigraphAdaptor() { 533 checkConcept<concepts::Digraph, SplitDigraphAdaptor<concepts::Digraph> >(); 534 535 typedef ListDigraph Digraph; 536 typedef SplitDigraphAdaptor<Digraph> Adaptor; 537 538 Digraph digraph; 539 Adaptor adaptor(digraph); 540 541 Digraph::Node n1 = digraph.addNode(); 542 Digraph::Node n2 = digraph.addNode(); 543 Digraph::Node n3 = digraph.addNode(); 544 545 Digraph::Arc a1 = digraph.addArc(n1, n2); 546 Digraph::Arc a2 = digraph.addArc(n1, n3); 547 Digraph::Arc a3 = digraph.addArc(n2, n3); 548 549 checkGraphNodeList(adaptor, 6); 550 checkGraphArcList(adaptor, 6); 551 checkGraphConArcList(adaptor, 6); 552 553 checkGraphOutArcList(adaptor, adaptor.inNode(n1), 1); 554 checkGraphOutArcList(adaptor, adaptor.outNode(n1), 2); 555 checkGraphOutArcList(adaptor, adaptor.inNode(n2), 1); 556 checkGraphOutArcList(adaptor, adaptor.outNode(n2), 1); 557 checkGraphOutArcList(adaptor, adaptor.inNode(n3), 1); 558 checkGraphOutArcList(adaptor, adaptor.outNode(n3), 0); 559 560 checkGraphInArcList(adaptor, adaptor.inNode(n1), 0); 561 checkGraphInArcList(adaptor, adaptor.outNode(n1), 1); 562 checkGraphInArcList(adaptor, adaptor.inNode(n2), 1); 563 checkGraphInArcList(adaptor, adaptor.outNode(n2), 1); 564 checkGraphInArcList(adaptor, adaptor.inNode(n3), 2); 565 checkGraphInArcList(adaptor, adaptor.outNode(n3), 1); 566 567 checkNodeIds(adaptor); 568 checkArcIds(adaptor); 569 570 checkGraphNodeMap(adaptor); 571 checkGraphArcMap(adaptor); 572 573 for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { 574 if (adaptor.origArc(a)) { 575 Digraph::Arc oa = a; 576 check(adaptor.source(a) == adaptor.outNode(digraph.source(oa)), 577 "Wrong split"); 578 check(adaptor.target(a) == adaptor.inNode(digraph.target(oa)), 579 "Wrong split"); 580 } else { 581 Digraph::Node on = a; 582 check(adaptor.source(a) == adaptor.inNode(on), "Wrong split"); 583 check(adaptor.target(a) == adaptor.outNode(on), "Wrong split"); 584 } 585 } 586 } 587 588 void checkGraphAdaptor() { 589 checkConcept<concepts::Graph, GraphAdaptor<concepts::Graph> >(); 590 591 typedef ListGraph Graph; 592 typedef GraphAdaptor<Graph> Adaptor; 593 594 Graph graph; 595 Adaptor adaptor(graph); 596 597 Graph::Node n1 = graph.addNode(); 598 Graph::Node n2 = graph.addNode(); 599 Graph::Node n3 = graph.addNode(); 600 Graph::Node n4 = graph.addNode(); 601 602 Graph::Edge a1 = graph.addEdge(n1, n2); 603 Graph::Edge a2 = graph.addEdge(n1, n3); 604 Graph::Edge a3 = graph.addEdge(n2, n3); 605 Graph::Edge a4 = graph.addEdge(n3, n4); 606 607 checkGraphNodeList(adaptor, 4); 608 checkGraphArcList(adaptor, 8); 609 checkGraphEdgeList(adaptor, 4); 610 checkGraphConArcList(adaptor, 8); 611 checkGraphConEdgeList(adaptor, 4); 612 613 checkGraphOutArcList(adaptor, n1, 2); 614 checkGraphOutArcList(adaptor, n2, 2); 615 checkGraphOutArcList(adaptor, n3, 3); 616 checkGraphOutArcList(adaptor, n4, 1); 617 618 checkGraphInArcList(adaptor, n1, 2); 619 checkGraphInArcList(adaptor, n2, 2); 620 checkGraphInArcList(adaptor, n3, 3); 621 checkGraphInArcList(adaptor, n4, 1); 622 623 checkGraphIncEdgeList(adaptor, n1, 2); 624 checkGraphIncEdgeList(adaptor, n2, 2); 625 checkGraphIncEdgeList(adaptor, n3, 3); 626 checkGraphIncEdgeList(adaptor, n4, 1); 627 628 629 checkNodeIds(adaptor); 630 checkArcIds(adaptor); 631 checkEdgeIds(adaptor); 632 633 checkGraphNodeMap(adaptor); 634 checkGraphArcMap(adaptor); 635 checkGraphEdgeMap(adaptor); 636 } 637 638 void checkSubGraphAdaptor() { 639 checkConcept<concepts::Graph, 640 SubGraphAdaptor<concepts::Graph, 641 concepts::Graph::NodeMap<bool>, 642 concepts::Graph::EdgeMap<bool> > >(); 643 644 typedef ListGraph Graph; 645 typedef Graph::NodeMap<bool> NodeFilter; 646 typedef Graph::EdgeMap<bool> EdgeFilter; 647 typedef SubGraphAdaptor<Graph, NodeFilter, EdgeFilter> Adaptor; 648 649 Graph graph; 650 NodeFilter node_filter(graph); 651 EdgeFilter edge_filter(graph); 652 Adaptor adaptor(graph, node_filter, edge_filter); 653 654 Graph::Node n1 = graph.addNode(); 655 Graph::Node n2 = graph.addNode(); 656 Graph::Node n3 = graph.addNode(); 657 Graph::Node n4 = graph.addNode(); 658 659 Graph::Edge e1 = graph.addEdge(n1, n2); 660 Graph::Edge e2 = graph.addEdge(n1, n3); 661 Graph::Edge e3 = graph.addEdge(n2, n3); 662 Graph::Edge e4 = graph.addEdge(n3, n4); 663 664 node_filter = constMap<Graph::Node>(true); 665 edge_filter = constMap<Graph::Edge>(true); 666 667 checkGraphNodeList(adaptor, 4); 668 checkGraphArcList(adaptor, 8); 669 checkGraphEdgeList(adaptor, 4); 670 checkGraphConArcList(adaptor, 8); 671 checkGraphConEdgeList(adaptor, 4); 672 673 checkGraphOutArcList(adaptor, n1, 2); 674 checkGraphOutArcList(adaptor, n2, 2); 675 checkGraphOutArcList(adaptor, n3, 3); 676 checkGraphOutArcList(adaptor, n4, 1); 677 678 checkGraphInArcList(adaptor, n1, 2); 679 checkGraphInArcList(adaptor, n2, 2); 680 checkGraphInArcList(adaptor, n3, 3); 681 checkGraphInArcList(adaptor, n4, 1); 682 683 checkGraphIncEdgeList(adaptor, n1, 2); 684 checkGraphIncEdgeList(adaptor, n2, 2); 685 checkGraphIncEdgeList(adaptor, n3, 3); 686 checkGraphIncEdgeList(adaptor, n4, 1); 687 688 checkNodeIds(adaptor); 689 checkArcIds(adaptor); 690 checkEdgeIds(adaptor); 691 692 checkGraphNodeMap(adaptor); 693 checkGraphArcMap(adaptor); 694 checkGraphEdgeMap(adaptor); 695 696 edge_filter[e2] = false; 697 698 checkGraphNodeList(adaptor, 4); 699 checkGraphArcList(adaptor, 6); 700 checkGraphEdgeList(adaptor, 3); 701 checkGraphConArcList(adaptor, 6); 702 checkGraphConEdgeList(adaptor, 3); 703 704 checkGraphOutArcList(adaptor, n1, 1); 705 checkGraphOutArcList(adaptor, n2, 2); 706 checkGraphOutArcList(adaptor, n3, 2); 707 checkGraphOutArcList(adaptor, n4, 1); 708 709 checkGraphInArcList(adaptor, n1, 1); 710 checkGraphInArcList(adaptor, n2, 2); 711 checkGraphInArcList(adaptor, n3, 2); 712 checkGraphInArcList(adaptor, n4, 1); 713 714 checkGraphIncEdgeList(adaptor, n1, 1); 715 checkGraphIncEdgeList(adaptor, n2, 2); 716 checkGraphIncEdgeList(adaptor, n3, 2); 717 checkGraphIncEdgeList(adaptor, n4, 1); 718 719 checkNodeIds(adaptor); 720 checkArcIds(adaptor); 721 checkEdgeIds(adaptor); 722 723 checkGraphNodeMap(adaptor); 724 checkGraphArcMap(adaptor); 725 checkGraphEdgeMap(adaptor); 726 727 node_filter[n1] = false; 728 729 checkGraphNodeList(adaptor, 3); 730 checkGraphArcList(adaptor, 4); 731 checkGraphEdgeList(adaptor, 2); 732 checkGraphConArcList(adaptor, 4); 733 checkGraphConEdgeList(adaptor, 2); 734 735 checkGraphOutArcList(adaptor, n2, 1); 736 checkGraphOutArcList(adaptor, n3, 2); 737 checkGraphOutArcList(adaptor, n4, 1); 738 739 checkGraphInArcList(adaptor, n2, 1); 740 checkGraphInArcList(adaptor, n3, 2); 741 checkGraphInArcList(adaptor, n4, 1); 742 743 checkGraphIncEdgeList(adaptor, n2, 1); 744 checkGraphIncEdgeList(adaptor, n3, 2); 745 checkGraphIncEdgeList(adaptor, n4, 1); 746 747 checkNodeIds(adaptor); 748 checkArcIds(adaptor); 749 checkEdgeIds(adaptor); 750 751 checkGraphNodeMap(adaptor); 752 checkGraphArcMap(adaptor); 753 checkGraphEdgeMap(adaptor); 754 755 node_filter = constMap<Graph::Node>(false); 756 edge_filter = constMap<Graph::Edge>(false); 757 758 checkGraphNodeList(adaptor, 0); 759 checkGraphArcList(adaptor, 0); 760 checkGraphEdgeList(adaptor, 0); 761 checkGraphConArcList(adaptor, 0); 762 checkGraphConEdgeList(adaptor, 0); 763 764 checkNodeIds(adaptor); 765 checkArcIds(adaptor); 766 checkEdgeIds(adaptor); 767 768 checkGraphNodeMap(adaptor); 769 checkGraphArcMap(adaptor); 770 checkGraphEdgeMap(adaptor); 771 } 772 773 void checkNodeSubGraphAdaptor() { 774 checkConcept<concepts::Graph, 775 NodeSubGraphAdaptor<concepts::Graph, 776 concepts::Graph::NodeMap<bool> > >(); 777 778 typedef ListGraph Graph; 779 typedef Graph::NodeMap<bool> NodeFilter; 780 typedef NodeSubGraphAdaptor<Graph, NodeFilter> Adaptor; 781 782 Graph graph; 783 NodeFilter node_filter(graph); 784 Adaptor adaptor(graph, node_filter); 785 786 Graph::Node n1 = graph.addNode(); 787 Graph::Node n2 = graph.addNode(); 788 Graph::Node n3 = graph.addNode(); 789 Graph::Node n4 = graph.addNode(); 790 791 Graph::Edge e1 = graph.addEdge(n1, n2); 792 Graph::Edge e2 = graph.addEdge(n1, n3); 793 Graph::Edge e3 = graph.addEdge(n2, n3); 794 Graph::Edge e4 = graph.addEdge(n3, n4); 795 796 node_filter = constMap<Graph::Node>(true); 797 798 checkGraphNodeList(adaptor, 4); 799 checkGraphArcList(adaptor, 8); 800 checkGraphEdgeList(adaptor, 4); 801 checkGraphConArcList(adaptor, 8); 802 checkGraphConEdgeList(adaptor, 4); 803 804 checkGraphOutArcList(adaptor, n1, 2); 805 checkGraphOutArcList(adaptor, n2, 2); 806 checkGraphOutArcList(adaptor, n3, 3); 807 checkGraphOutArcList(adaptor, n4, 1); 808 809 checkGraphInArcList(adaptor, n1, 2); 810 checkGraphInArcList(adaptor, n2, 2); 811 checkGraphInArcList(adaptor, n3, 3); 812 checkGraphInArcList(adaptor, n4, 1); 813 814 checkGraphIncEdgeList(adaptor, n1, 2); 815 checkGraphIncEdgeList(adaptor, n2, 2); 816 checkGraphIncEdgeList(adaptor, n3, 3); 817 checkGraphIncEdgeList(adaptor, n4, 1); 818 819 checkNodeIds(adaptor); 820 checkArcIds(adaptor); 821 checkEdgeIds(adaptor); 822 823 checkGraphNodeMap(adaptor); 824 checkGraphArcMap(adaptor); 825 checkGraphEdgeMap(adaptor); 826 827 node_filter[n1] = false; 828 829 checkGraphNodeList(adaptor, 3); 830 checkGraphArcList(adaptor, 4); 831 checkGraphEdgeList(adaptor, 2); 832 checkGraphConArcList(adaptor, 4); 833 checkGraphConEdgeList(adaptor, 2); 834 835 checkGraphOutArcList(adaptor, n2, 1); 836 checkGraphOutArcList(adaptor, n3, 2); 837 checkGraphOutArcList(adaptor, n4, 1); 838 839 checkGraphInArcList(adaptor, n2, 1); 840 checkGraphInArcList(adaptor, n3, 2); 841 checkGraphInArcList(adaptor, n4, 1); 842 843 checkGraphIncEdgeList(adaptor, n2, 1); 844 checkGraphIncEdgeList(adaptor, n3, 2); 845 checkGraphIncEdgeList(adaptor, n4, 1); 846 847 checkNodeIds(adaptor); 848 checkArcIds(adaptor); 849 checkEdgeIds(adaptor); 850 851 checkGraphNodeMap(adaptor); 852 checkGraphArcMap(adaptor); 853 checkGraphEdgeMap(adaptor); 854 855 node_filter = constMap<Graph::Node>(false); 856 857 checkGraphNodeList(adaptor, 0); 858 checkGraphArcList(adaptor, 0); 859 checkGraphEdgeList(adaptor, 0); 860 checkGraphConArcList(adaptor, 0); 861 checkGraphConEdgeList(adaptor, 0); 862 863 checkNodeIds(adaptor); 864 checkArcIds(adaptor); 865 checkEdgeIds(adaptor); 866 867 checkGraphNodeMap(adaptor); 868 checkGraphArcMap(adaptor); 869 checkGraphEdgeMap(adaptor); 870 } 871 872 void checkEdgeSubGraphAdaptor() { 873 checkConcept<concepts::Graph, 874 EdgeSubGraphAdaptor<concepts::Graph, 875 concepts::Graph::EdgeMap<bool> > >(); 876 877 typedef ListGraph Graph; 878 typedef Graph::EdgeMap<bool> EdgeFilter; 879 typedef EdgeSubGraphAdaptor<Graph, EdgeFilter> Adaptor; 880 881 Graph graph; 882 EdgeFilter edge_filter(graph); 883 Adaptor adaptor(graph, edge_filter); 884 885 Graph::Node n1 = graph.addNode(); 886 Graph::Node n2 = graph.addNode(); 887 Graph::Node n3 = graph.addNode(); 888 Graph::Node n4 = graph.addNode(); 889 890 Graph::Edge e1 = graph.addEdge(n1, n2); 891 Graph::Edge e2 = graph.addEdge(n1, n3); 892 Graph::Edge e3 = graph.addEdge(n2, n3); 893 Graph::Edge e4 = graph.addEdge(n3, n4); 894 895 edge_filter = constMap<Graph::Edge>(true); 896 897 checkGraphNodeList(adaptor, 4); 898 checkGraphArcList(adaptor, 8); 899 checkGraphEdgeList(adaptor, 4); 900 checkGraphConArcList(adaptor, 8); 901 checkGraphConEdgeList(adaptor, 4); 902 903 checkGraphOutArcList(adaptor, n1, 2); 904 checkGraphOutArcList(adaptor, n2, 2); 905 checkGraphOutArcList(adaptor, n3, 3); 906 checkGraphOutArcList(adaptor, n4, 1); 907 908 checkGraphInArcList(adaptor, n1, 2); 909 checkGraphInArcList(adaptor, n2, 2); 910 checkGraphInArcList(adaptor, n3, 3); 911 checkGraphInArcList(adaptor, n4, 1); 912 913 checkGraphIncEdgeList(adaptor, n1, 2); 914 checkGraphIncEdgeList(adaptor, n2, 2); 915 checkGraphIncEdgeList(adaptor, n3, 3); 916 checkGraphIncEdgeList(adaptor, n4, 1); 917 918 checkNodeIds(adaptor); 919 checkArcIds(adaptor); 920 checkEdgeIds(adaptor); 921 922 checkGraphNodeMap(adaptor); 923 checkGraphArcMap(adaptor); 924 checkGraphEdgeMap(adaptor); 925 926 edge_filter[e2] = false; 927 928 checkGraphNodeList(adaptor, 4); 929 checkGraphArcList(adaptor, 6); 930 checkGraphEdgeList(adaptor, 3); 931 checkGraphConArcList(adaptor, 6); 932 checkGraphConEdgeList(adaptor, 3); 933 934 checkGraphOutArcList(adaptor, n1, 1); 935 checkGraphOutArcList(adaptor, n2, 2); 936 checkGraphOutArcList(adaptor, n3, 2); 937 checkGraphOutArcList(adaptor, n4, 1); 938 939 checkGraphInArcList(adaptor, n1, 1); 940 checkGraphInArcList(adaptor, n2, 2); 941 checkGraphInArcList(adaptor, n3, 2); 942 checkGraphInArcList(adaptor, n4, 1); 943 944 checkGraphIncEdgeList(adaptor, n1, 1); 945 checkGraphIncEdgeList(adaptor, n2, 2); 946 checkGraphIncEdgeList(adaptor, n3, 2); 947 checkGraphIncEdgeList(adaptor, n4, 1); 948 949 checkNodeIds(adaptor); 950 checkArcIds(adaptor); 951 checkEdgeIds(adaptor); 952 953 checkGraphNodeMap(adaptor); 954 checkGraphArcMap(adaptor); 955 checkGraphEdgeMap(adaptor); 956 957 edge_filter = constMap<Graph::Edge>(false); 958 959 checkGraphNodeList(adaptor, 4); 960 checkGraphArcList(adaptor, 0); 961 checkGraphEdgeList(adaptor, 0); 962 checkGraphConArcList(adaptor, 0); 963 checkGraphConEdgeList(adaptor, 0); 964 965 checkNodeIds(adaptor); 966 checkArcIds(adaptor); 967 checkEdgeIds(adaptor); 968 969 checkGraphNodeMap(adaptor); 970 checkGraphArcMap(adaptor); 971 checkGraphEdgeMap(adaptor); 972 } 973 974 void checkDirGraphAdaptor() { 975 checkConcept<concepts::Digraph, 976 DirGraphAdaptor<concepts::Graph, concepts::Graph::EdgeMap<bool> > >(); 977 978 typedef ListGraph Graph; 979 typedef ListGraph::EdgeMap<bool> DirMap; 980 typedef DirGraphAdaptor<Graph> Adaptor; 981 982 Graph graph; 983 DirMap dir(graph, true); 984 Adaptor adaptor(graph, dir); 985 986 Graph::Node n1 = graph.addNode(); 987 Graph::Node n2 = graph.addNode(); 988 Graph::Node n3 = graph.addNode(); 989 990 Graph::Edge e1 = graph.addEdge(n1, n2); 991 Graph::Edge e2 = graph.addEdge(n1, n3); 992 Graph::Edge e3 = graph.addEdge(n2, n3); 993 994 checkGraphNodeList(adaptor, 3); 995 checkGraphArcList(adaptor, 3); 996 checkGraphConArcList(adaptor, 3); 997 998 { 999 dir[e1] = true; 1000 Adaptor::Node u = adaptor.source(e1); 1001 Adaptor::Node v = adaptor.target(e1); 1002 1003 dir[e1] = false; 1004 check (u == adaptor.target(e1), "Wrong dir"); 1005 check (v == adaptor.source(e1), "Wrong dir"); 1006 1007 check ((u == n1 && v == n2) || (u == n2 && v == n1), "Wrong dir"); 1008 dir[e1] = n1 == u; 1009 } 1010 1011 { 1012 dir[e2] = true; 1013 Adaptor::Node u = adaptor.source(e2); 1014 Adaptor::Node v = adaptor.target(e2); 1015 1016 dir[e2] = false; 1017 check (u == adaptor.target(e2), "Wrong dir"); 1018 check (v == adaptor.source(e2), "Wrong dir"); 1019 1020 check ((u == n1 && v == n3) || (u == n3 && v == n1), "Wrong dir"); 1021 dir[e2] = n3 == u; 1022 } 1023 1024 { 1025 dir[e3] = true; 1026 Adaptor::Node u = adaptor.source(e3); 1027 Adaptor::Node v = adaptor.target(e3); 1028 1029 dir[e3] = false; 1030 check (u == adaptor.target(e3), "Wrong dir"); 1031 check (v == adaptor.source(e3), "Wrong dir"); 1032 1033 check ((u == n2 && v == n3) || (u == n3 && v == n2), "Wrong dir"); 1034 dir[e3] = n2 == u; 1035 } 1036 1037 checkGraphOutArcList(adaptor, n1, 1); 1038 checkGraphOutArcList(adaptor, n2, 1); 1039 checkGraphOutArcList(adaptor, n3, 1); 1040 1041 checkGraphInArcList(adaptor, n1, 1); 1042 checkGraphInArcList(adaptor, n2, 1); 1043 checkGraphInArcList(adaptor, n3, 1); 1044 1045 checkNodeIds(adaptor); 1046 checkArcIds(adaptor); 1047 1048 checkGraphNodeMap(adaptor); 1049 checkGraphArcMap(adaptor); 1050 1051 } 1052 1053 1054 int main(int, const char **) { 1055 1056 checkDigraphAdaptor(); 1057 checkRevDigraphAdaptor(); 1058 checkSubDigraphAdaptor(); 1059 checkNodeSubDigraphAdaptor(); 1060 checkArcSubDigraphAdaptor(); 1061 checkUndirDigraphAdaptor(); 1062 checkResDigraphAdaptor(); 1063 checkSplitDigraphAdaptor(); 1064 1065 checkGraphAdaptor(); 1066 checkSubGraphAdaptor(); 1067 checkNodeSubGraphAdaptor(); 1068 checkEdgeSubGraphAdaptor(); 1069 checkDirGraphAdaptor(); 1070 1071 return 0; 1072 }