[174] | 1 | // -*- c++ -*- |
---|
[76] | 2 | #ifndef GRAPH_WRAPPER_H |
---|
| 3 | #define GRAPH_WRAPPER_H |
---|
| 4 | |
---|
[174] | 5 | #include <invalid.h> |
---|
| 6 | |
---|
[105] | 7 | namespace hugo { |
---|
[76] | 8 | |
---|
| 9 | template<typename Graph> |
---|
| 10 | class TrivGraphWrapper { |
---|
[199] | 11 | protected: |
---|
[76] | 12 | Graph* graph; |
---|
| 13 | |
---|
| 14 | public: |
---|
| 15 | typedef Graph BaseGraph; |
---|
| 16 | |
---|
[174] | 17 | typedef typename Graph::Node Node; |
---|
[76] | 18 | typedef typename Graph::NodeIt NodeIt; |
---|
| 19 | |
---|
[174] | 20 | typedef typename Graph::Edge Edge; |
---|
[76] | 21 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 22 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
[155] | 23 | //typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 24 | typedef typename Graph::EdgeIt EdgeIt; |
---|
[168] | 25 | |
---|
| 26 | //TrivGraphWrapper() : graph(0) { } |
---|
| 27 | TrivGraphWrapper(Graph& _graph) : graph(&_graph) { } |
---|
| 28 | |
---|
| 29 | void setGraph(Graph& _graph) { graph = &_graph; } |
---|
| 30 | Graph& getGraph() const { return (*graph); } |
---|
[76] | 31 | |
---|
[212] | 32 | template<typename I> I& first(I& i) const { return graph->first(i); } |
---|
| 33 | template<typename I, typename P> I& first(I& i, const P& p) const { |
---|
| 34 | return graph->first(i, p); } |
---|
[155] | 35 | |
---|
| 36 | template<typename I> I getNext(const I& i) const { |
---|
| 37 | return graph->getNext(i); } |
---|
| 38 | template<typename I> I& next(I &i) const { return graph->next(i); } |
---|
[76] | 39 | |
---|
| 40 | template< typename It > It first() const { |
---|
[212] | 41 | It e; first(e); return e; } |
---|
[76] | 42 | |
---|
[174] | 43 | template< typename It > It first(const Node& v) const { |
---|
[212] | 44 | It e; first(e, v); return e; } |
---|
[76] | 45 | |
---|
[174] | 46 | Node head(const Edge& e) const { return graph->head(e); } |
---|
| 47 | Node tail(const Edge& e) const { return graph->tail(e); } |
---|
[155] | 48 | |
---|
| 49 | template<typename I> bool valid(const I& i) const |
---|
| 50 | { return graph->valid(i); } |
---|
| 51 | |
---|
| 52 | //template<typename I> void setInvalid(const I &i); |
---|
| 53 | //{ return graph->setInvalid(i); } |
---|
| 54 | |
---|
| 55 | int nodeNum() const { return graph->nodeNum(); } |
---|
| 56 | int edgeNum() const { return graph->edgeNum(); } |
---|
[76] | 57 | |
---|
[174] | 58 | template<typename I> Node aNode(const I& e) const { |
---|
[76] | 59 | return graph->aNode(e); } |
---|
[174] | 60 | template<typename I> Node bNode(const I& e) const { |
---|
[76] | 61 | return graph->bNode(e); } |
---|
| 62 | |
---|
[174] | 63 | Node addNode() const { return graph->addNode(); } |
---|
| 64 | Edge addEdge(const Node& tail, const Node& head) const { |
---|
[76] | 65 | return graph->addEdge(tail, head); } |
---|
| 66 | |
---|
| 67 | template<typename I> void erase(const I& i) const { graph->erase(i); } |
---|
| 68 | |
---|
| 69 | void clear() const { graph->clear(); } |
---|
[155] | 70 | |
---|
[76] | 71 | template<typename T> class NodeMap : public Graph::NodeMap<T> { |
---|
| 72 | public: |
---|
[155] | 73 | NodeMap(const TrivGraphWrapper<Graph>& _G) : |
---|
[158] | 74 | Graph::NodeMap<T>(_G.getGraph()) { } |
---|
[155] | 75 | NodeMap(const TrivGraphWrapper<Graph>& _G, T a) : |
---|
[158] | 76 | Graph::NodeMap<T>(_G.getGraph(), a) { } |
---|
[76] | 77 | }; |
---|
[168] | 78 | |
---|
[155] | 79 | template<typename T> class EdgeMap : public Graph::EdgeMap<T> { |
---|
| 80 | public: |
---|
| 81 | EdgeMap(const TrivGraphWrapper<Graph>& _G) : |
---|
[158] | 82 | Graph::EdgeMap<T>(_G.getGraph()) { } |
---|
[155] | 83 | EdgeMap(const TrivGraphWrapper<Graph>& _G, T a) : |
---|
[158] | 84 | Graph::EdgeMap<T>(_G.getGraph(), a) { } |
---|
[155] | 85 | }; |
---|
[76] | 86 | }; |
---|
| 87 | |
---|
[212] | 88 | template<typename GraphWrapper> |
---|
| 89 | class GraphWrapperSkeleton { |
---|
| 90 | protected: |
---|
| 91 | GraphWrapper gw; |
---|
| 92 | |
---|
| 93 | public: |
---|
| 94 | typedef typename GraphWrapper::BaseGraph BaseGraph; |
---|
| 95 | |
---|
| 96 | typedef typename GraphWrapper::Node Node; |
---|
| 97 | typedef typename GraphWrapper::NodeIt NodeIt; |
---|
| 98 | |
---|
| 99 | typedef typename GraphWrapper::Edge Edge; |
---|
| 100 | typedef typename GraphWrapper::OutEdgeIt OutEdgeIt; |
---|
| 101 | typedef typename GraphWrapper::InEdgeIt InEdgeIt; |
---|
| 102 | //typedef typename GraphWrapper::SymEdgeIt SymEdgeIt; |
---|
| 103 | typedef typename GraphWrapper::EdgeIt EdgeIt; |
---|
| 104 | |
---|
| 105 | //GraphWrapperSkeleton() : gw() { } |
---|
| 106 | GraphWrapperSkeleton(GraphWrapper& _gw) : gw(_gw) { } |
---|
| 107 | |
---|
| 108 | void setGraph(BaseGraph& _graph) { gw.setGraph(_graph); } |
---|
| 109 | BaseGraph& getGraph() const { return gw.getGraph(); } |
---|
| 110 | |
---|
| 111 | template<typename I> I& first(I& i) const { return gw.first(i); } |
---|
| 112 | template<typename I, typename P> I& first(I& i, const P& p) const { |
---|
| 113 | return gw.first(i, p); } |
---|
| 114 | |
---|
| 115 | template<typename I> I getNext(const I& i) const { return gw.getNext(i); } |
---|
| 116 | template<typename I> I& next(I &i) const { return gw.next(i); } |
---|
| 117 | |
---|
| 118 | template< typename It > It first() const { |
---|
| 119 | It e; this->first(e); return e; } |
---|
| 120 | |
---|
| 121 | template< typename It > It first(const Node& v) const { |
---|
| 122 | It e; this->first(e, v); return e; } |
---|
| 123 | |
---|
| 124 | Node head(const Edge& e) const { return gw.head(e); } |
---|
| 125 | Node tail(const Edge& e) const { return gw.tail(e); } |
---|
| 126 | |
---|
| 127 | template<typename I> bool valid(const I& i) const { return gw.valid(i); } |
---|
| 128 | |
---|
| 129 | //template<typename I> void setInvalid(const I &i); |
---|
| 130 | //{ return graph->setInvalid(i); } |
---|
| 131 | |
---|
| 132 | int nodeNum() const { return gw.nodeNum(); } |
---|
| 133 | int edgeNum() const { return gw.edgeNum(); } |
---|
| 134 | |
---|
| 135 | template<typename I> Node aNode(const I& e) const { return gw.aNode(e); } |
---|
| 136 | template<typename I> Node bNode(const I& e) const { return gw.bNode(e); } |
---|
| 137 | |
---|
| 138 | Node addNode() const { return gw.addNode(); } |
---|
| 139 | Edge addEdge(const Node& tail, const Node& head) const { |
---|
| 140 | return gw.addEdge(tail, head); } |
---|
| 141 | |
---|
| 142 | template<typename I> void erase(const I& i) const { gw.erase(i); } |
---|
| 143 | |
---|
| 144 | void clear() const { gw.clear(); } |
---|
| 145 | |
---|
| 146 | template<typename T> class NodeMap : public GraphWrapper::NodeMap<T> { |
---|
| 147 | public: |
---|
| 148 | NodeMap(const GraphWrapperSkeleton<GraphWrapper>& _G) : |
---|
| 149 | GraphWrapper::NodeMap<T>(_G.gw) { } |
---|
| 150 | NodeMap(const GraphWrapperSkeleton<GraphWrapper>& _G, T a) : |
---|
| 151 | GraphWrapper::NodeMap<T>(_G.gw, a) { } |
---|
| 152 | }; |
---|
| 153 | |
---|
| 154 | template<typename T> class EdgeMap : public GraphWrapper::EdgeMap<T> { |
---|
| 155 | public: |
---|
| 156 | EdgeMap(const GraphWrapperSkeleton<GraphWrapper>& _G) : |
---|
| 157 | GraphWrapper::EdgeMap<T>(_G.gw) { } |
---|
| 158 | EdgeMap(const GraphWrapperSkeleton<GraphWrapper>& _G, T a) : |
---|
| 159 | GraphWrapper::EdgeMap<T>(_G.gw, a) { } |
---|
| 160 | }; |
---|
| 161 | }; |
---|
| 162 | |
---|
[76] | 163 | template<typename Graph> |
---|
| 164 | class RevGraphWrapper |
---|
| 165 | { |
---|
[199] | 166 | protected: |
---|
[76] | 167 | Graph* graph; |
---|
| 168 | |
---|
| 169 | public: |
---|
| 170 | typedef Graph BaseGraph; |
---|
| 171 | |
---|
[174] | 172 | typedef typename Graph::Node Node; |
---|
| 173 | typedef typename Graph::NodeIt NodeIt; |
---|
[76] | 174 | |
---|
[174] | 175 | typedef typename Graph::Edge Edge; |
---|
[76] | 176 | typedef typename Graph::OutEdgeIt InEdgeIt; |
---|
| 177 | typedef typename Graph::InEdgeIt OutEdgeIt; |
---|
[155] | 178 | //typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 179 | typedef typename Graph::EdgeIt EdgeIt; |
---|
[168] | 180 | |
---|
| 181 | //RevGraphWrapper() : graph(0) { } |
---|
| 182 | RevGraphWrapper(Graph& _graph) : graph(&_graph) { } |
---|
| 183 | |
---|
| 184 | void setGraph(Graph& _graph) { graph = &_graph; } |
---|
| 185 | Graph& getGraph() const { return (*graph); } |
---|
[76] | 186 | |
---|
[212] | 187 | template<typename I> I& first(I& i) const { return graph->first(i); } |
---|
| 188 | template<typename I, typename P> I& first(I& i, const P& p) const { |
---|
| 189 | return graph->first(i, p); } |
---|
[155] | 190 | |
---|
| 191 | template<typename I> I getNext(const I& i) const { |
---|
| 192 | return graph->getNext(i); } |
---|
| 193 | template<typename I> I& next(I &i) const { return graph->next(i); } |
---|
[76] | 194 | |
---|
| 195 | template< typename It > It first() const { |
---|
[212] | 196 | It e; first(e); return e; } |
---|
[76] | 197 | |
---|
[174] | 198 | template< typename It > It first(const Node& v) const { |
---|
[212] | 199 | It e; first(e, v); return e; } |
---|
[76] | 200 | |
---|
[174] | 201 | Node head(const Edge& e) const { return graph->tail(e); } |
---|
| 202 | Node tail(const Edge& e) const { return graph->head(e); } |
---|
[76] | 203 | |
---|
[155] | 204 | template<typename I> bool valid(const I& i) const |
---|
| 205 | { return graph->valid(i); } |
---|
| 206 | |
---|
| 207 | //template<typename I> void setInvalid(const I &i); |
---|
| 208 | //{ return graph->setInvalid(i); } |
---|
| 209 | |
---|
[174] | 210 | template<typename I> Node aNode(const I& e) const { |
---|
[76] | 211 | return graph->aNode(e); } |
---|
[174] | 212 | template<typename I> Node bNode(const I& e) const { |
---|
[76] | 213 | return graph->bNode(e); } |
---|
[155] | 214 | |
---|
[174] | 215 | Node addNode() const { return graph->addNode(); } |
---|
| 216 | Edge addEdge(const Node& tail, const Node& head) const { |
---|
[76] | 217 | return graph->addEdge(tail, head); } |
---|
| 218 | |
---|
[155] | 219 | int nodeNum() const { return graph->nodeNum(); } |
---|
| 220 | int edgeNum() const { return graph->edgeNum(); } |
---|
[76] | 221 | |
---|
[155] | 222 | template<typename I> void erase(const I& i) const { graph->erase(i); } |
---|
[76] | 223 | |
---|
[155] | 224 | void clear() const { graph->clear(); } |
---|
| 225 | |
---|
| 226 | template<typename T> class NodeMap : public Graph::NodeMap<T> { |
---|
| 227 | public: |
---|
| 228 | NodeMap(const RevGraphWrapper<Graph>& _G) : |
---|
[158] | 229 | Graph::NodeMap<T>(_G.getGraph()) { } |
---|
[155] | 230 | NodeMap(const RevGraphWrapper<Graph>& _G, T a) : |
---|
[158] | 231 | Graph::NodeMap<T>(_G.getGraph(), a) { } |
---|
[155] | 232 | }; |
---|
[168] | 233 | |
---|
[155] | 234 | template<typename T> class EdgeMap : public Graph::EdgeMap<T> { |
---|
| 235 | public: |
---|
| 236 | EdgeMap(const RevGraphWrapper<Graph>& _G) : |
---|
[158] | 237 | Graph::EdgeMap<T>(_G.getGraph()) { } |
---|
[155] | 238 | EdgeMap(const RevGraphWrapper<Graph>& _G, T a) : |
---|
[158] | 239 | Graph::EdgeMap<T>(_G.getGraph(), a) { } |
---|
[155] | 240 | }; |
---|
[76] | 241 | }; |
---|
| 242 | |
---|
[155] | 243 | |
---|
[158] | 244 | template<typename Graph> |
---|
| 245 | class UndirGraphWrapper { |
---|
[199] | 246 | protected: |
---|
[158] | 247 | Graph* graph; |
---|
| 248 | |
---|
| 249 | public: |
---|
| 250 | typedef Graph BaseGraph; |
---|
| 251 | |
---|
[174] | 252 | typedef typename Graph::Node Node; |
---|
[158] | 253 | typedef typename Graph::NodeIt NodeIt; |
---|
| 254 | |
---|
[174] | 255 | //typedef typename Graph::Edge Edge; |
---|
[158] | 256 | //typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 257 | //typedef typename Graph::InEdgeIt InEdgeIt; |
---|
| 258 | //typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 259 | //typedef typename Graph::EdgeIt EdgeIt; |
---|
[158] | 260 | |
---|
| 261 | //private: |
---|
[174] | 262 | typedef typename Graph::Edge GraphEdge; |
---|
[158] | 263 | typedef typename Graph::OutEdgeIt GraphOutEdgeIt; |
---|
| 264 | typedef typename Graph::InEdgeIt GraphInEdgeIt; |
---|
| 265 | //public: |
---|
| 266 | |
---|
[168] | 267 | //UndirGraphWrapper() : graph(0) { } |
---|
| 268 | UndirGraphWrapper(Graph& _graph) : graph(&_graph) { } |
---|
| 269 | |
---|
| 270 | void setGraph(Graph& _graph) { graph = &_graph; } |
---|
| 271 | Graph& getGraph() const { return (*graph); } |
---|
| 272 | |
---|
[174] | 273 | class Edge { |
---|
[158] | 274 | friend class UndirGraphWrapper<Graph>; |
---|
| 275 | bool out_or_in; //true iff out |
---|
| 276 | GraphOutEdgeIt out; |
---|
| 277 | GraphInEdgeIt in; |
---|
| 278 | public: |
---|
[174] | 279 | Edge() : out_or_in(), out(), in() { } |
---|
| 280 | Edge(const Invalid& i) : out_or_in(false), out(), in(i) { } |
---|
| 281 | operator GraphEdge() const { |
---|
[158] | 282 | if (out_or_in) return(out); else return(in); |
---|
| 283 | } |
---|
[174] | 284 | friend bool operator==(const Edge& u, const Edge& v) { |
---|
| 285 | if (v.out_or_in) |
---|
| 286 | return (u.out_or_in && u.out==v.out); |
---|
| 287 | else |
---|
| 288 | return (!u.out_or_in && u.in==v.in); |
---|
| 289 | } |
---|
| 290 | friend bool operator!=(const Edge& u, const Edge& v) { |
---|
| 291 | if (v.out_or_in) |
---|
| 292 | return (!u.out_or_in || u.out!=v.out); |
---|
| 293 | else |
---|
| 294 | return (u.out_or_in || u.in!=v.in); |
---|
| 295 | } |
---|
[158] | 296 | }; |
---|
| 297 | |
---|
[174] | 298 | class OutEdgeIt : public Edge { |
---|
[158] | 299 | friend class UndirGraphWrapper<Graph>; |
---|
| 300 | public: |
---|
[174] | 301 | OutEdgeIt() : Edge() { } |
---|
| 302 | OutEdgeIt(const Invalid& i) : Edge(i) { } |
---|
| 303 | OutEdgeIt(const UndirGraphWrapper& _G, const Node& n) : Edge() { |
---|
| 304 | out_or_in=true; |
---|
[212] | 305 | _G.graph->first(out, n); |
---|
[158] | 306 | if (!(_G.graph->valid(out))) { |
---|
| 307 | out_or_in=false; |
---|
[212] | 308 | _G.graph->first(in, n); |
---|
[158] | 309 | } |
---|
| 310 | } |
---|
| 311 | }; |
---|
| 312 | |
---|
[212] | 313 | OutEdgeIt& first(OutEdgeIt& e, const Node& n) const { |
---|
[158] | 314 | e.out_or_in=true; |
---|
[212] | 315 | graph->first(e.out, n); |
---|
[158] | 316 | if (!(graph->valid(e.out))) { |
---|
| 317 | e.out_or_in=false; |
---|
[212] | 318 | graph->first(e.in, n); |
---|
[158] | 319 | } |
---|
| 320 | return e; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | OutEdgeIt& next(OutEdgeIt& e) const { |
---|
| 324 | if (e.out_or_in) { |
---|
[174] | 325 | Node n=graph->tail(e.out); |
---|
[158] | 326 | graph->next(e.out); |
---|
| 327 | if (!graph->valid(e.out)) { |
---|
| 328 | e.out_or_in=false; |
---|
[212] | 329 | graph->first(e.in, n); |
---|
[158] | 330 | } |
---|
| 331 | } else { |
---|
| 332 | graph->next(e.in); |
---|
| 333 | } |
---|
| 334 | return e; |
---|
| 335 | } |
---|
| 336 | |
---|
[174] | 337 | Node aNode(const OutEdgeIt& e) const { |
---|
[158] | 338 | if (e.out_or_in) return graph->tail(e); else return graph->head(e); } |
---|
[174] | 339 | Node bNode(const OutEdgeIt& e) const { |
---|
[158] | 340 | if (e.out_or_in) return graph->head(e); else return graph->tail(e); } |
---|
| 341 | |
---|
| 342 | typedef OutEdgeIt InEdgeIt; |
---|
| 343 | |
---|
[212] | 344 | template<typename I> I& first(I& i) const { return graph->first(i); } |
---|
| 345 | // template<typename I, typename P> I& first(I& i, const P& p) const { |
---|
| 346 | // return graph->first(i, p); } |
---|
[158] | 347 | |
---|
| 348 | template<typename I> I getNext(const I& i) const { |
---|
| 349 | return graph->getNext(i); } |
---|
| 350 | template<typename I> I& next(I &i) const { return graph->next(i); } |
---|
| 351 | |
---|
| 352 | template< typename It > It first() const { |
---|
[212] | 353 | It e; first(e); return e; } |
---|
[158] | 354 | |
---|
[174] | 355 | template< typename It > It first(const Node& v) const { |
---|
[212] | 356 | It e; first(e, v); return e; } |
---|
[158] | 357 | |
---|
[174] | 358 | Node head(const Edge& e) const { return graph->head(e); } |
---|
| 359 | Node tail(const Edge& e) const { return graph->tail(e); } |
---|
[158] | 360 | |
---|
| 361 | template<typename I> bool valid(const I& i) const |
---|
| 362 | { return graph->valid(i); } |
---|
| 363 | |
---|
| 364 | //template<typename I> void setInvalid(const I &i); |
---|
| 365 | //{ return graph->setInvalid(i); } |
---|
| 366 | |
---|
| 367 | int nodeNum() const { return graph->nodeNum(); } |
---|
| 368 | int edgeNum() const { return graph->edgeNum(); } |
---|
| 369 | |
---|
[174] | 370 | // template<typename I> Node aNode(const I& e) const { |
---|
[158] | 371 | // return graph->aNode(e); } |
---|
[174] | 372 | // template<typename I> Node bNode(const I& e) const { |
---|
[158] | 373 | // return graph->bNode(e); } |
---|
| 374 | |
---|
[174] | 375 | Node addNode() const { return graph->addNode(); } |
---|
| 376 | Edge addEdge(const Node& tail, const Node& head) const { |
---|
[158] | 377 | return graph->addEdge(tail, head); } |
---|
| 378 | |
---|
| 379 | template<typename I> void erase(const I& i) const { graph->erase(i); } |
---|
| 380 | |
---|
| 381 | void clear() const { graph->clear(); } |
---|
| 382 | |
---|
| 383 | template<typename T> class NodeMap : public Graph::NodeMap<T> { |
---|
| 384 | public: |
---|
| 385 | NodeMap(const UndirGraphWrapper<Graph>& _G) : |
---|
| 386 | Graph::NodeMap<T>(_G.getGraph()) { } |
---|
| 387 | NodeMap(const UndirGraphWrapper<Graph>& _G, T a) : |
---|
| 388 | Graph::NodeMap<T>(_G.getGraph(), a) { } |
---|
| 389 | }; |
---|
[168] | 390 | |
---|
[158] | 391 | template<typename T> class EdgeMap : public Graph::EdgeMap<T> { |
---|
| 392 | public: |
---|
| 393 | EdgeMap(const UndirGraphWrapper<Graph>& _G) : |
---|
| 394 | Graph::EdgeMap<T>(_G.getGraph()) { } |
---|
| 395 | EdgeMap(const UndirGraphWrapper<Graph>& _G, T a) : |
---|
| 396 | Graph::EdgeMap<T>(_G.getGraph(), a) { } |
---|
| 397 | }; |
---|
| 398 | }; |
---|
| 399 | |
---|
| 400 | |
---|
| 401 | |
---|
[155] | 402 | // template<typename Graph> |
---|
| 403 | // class SymGraphWrapper |
---|
| 404 | // { |
---|
| 405 | // Graph* graph; |
---|
[76] | 406 | |
---|
[155] | 407 | // public: |
---|
| 408 | // typedef Graph BaseGraph; |
---|
| 409 | |
---|
[174] | 410 | // typedef typename Graph::Node Node; |
---|
| 411 | // typedef typename Graph::Edge Edge; |
---|
| 412 | |
---|
[155] | 413 | // typedef typename Graph::NodeIt NodeIt; |
---|
| 414 | |
---|
| 415 | // //FIXME tag-ekkel megcsinalni, hogy abbol csinaljon |
---|
| 416 | // //iranyitatlant, ami van |
---|
| 417 | // //mert csak 1 dolgot lehet be typedef-elni |
---|
| 418 | // typedef typename Graph::OutEdgeIt SymEdgeIt; |
---|
| 419 | // //typedef typename Graph::InEdgeIt SymEdgeIt; |
---|
| 420 | // //typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 421 | // typedef typename Graph::EdgeIt EdgeIt; |
---|
[155] | 422 | |
---|
| 423 | // int nodeNum() const { return graph->nodeNum(); } |
---|
| 424 | // int edgeNum() const { return graph->edgeNum(); } |
---|
| 425 | |
---|
[212] | 426 | // template<typename I> I& first(I& i) const { return graph->first(i); } |
---|
| 427 | // template<typename I, typename P> I& first(I& i, const P& p) const { |
---|
| 428 | // return graph->first(i, p); } |
---|
[155] | 429 | // //template<typename I> I next(const I i); { return graph->goNext(i); } |
---|
| 430 | // //template<typename I> I &goNext(I &i); { return graph->goNext(i); } |
---|
| 431 | |
---|
| 432 | // template< typename It > It first() const { |
---|
[212] | 433 | // It e; first(e); return e; } |
---|
[155] | 434 | |
---|
[174] | 435 | // template< typename It > It first(Node v) const { |
---|
[212] | 436 | // It e; first(e, v); return e; } |
---|
[155] | 437 | |
---|
[174] | 438 | // Node head(const Edge& e) const { return graph->head(e); } |
---|
| 439 | // Node tail(const Edge& e) const { return graph->tail(e); } |
---|
[155] | 440 | |
---|
[174] | 441 | // template<typename I> Node aNode(const I& e) const { |
---|
[155] | 442 | // return graph->aNode(e); } |
---|
[174] | 443 | // template<typename I> Node bNode(const I& e) const { |
---|
[155] | 444 | // return graph->bNode(e); } |
---|
| 445 | |
---|
| 446 | // //template<typename I> bool valid(const I i); |
---|
| 447 | // //{ return graph->valid(i); } |
---|
| 448 | |
---|
| 449 | // //template<typename I> void setInvalid(const I &i); |
---|
| 450 | // //{ return graph->setInvalid(i); } |
---|
| 451 | |
---|
[174] | 452 | // Node addNode() { return graph->addNode(); } |
---|
| 453 | // Edge addEdge(const Node& tail, const Node& head) { |
---|
[155] | 454 | // return graph->addEdge(tail, head); } |
---|
| 455 | |
---|
| 456 | // template<typename I> void erase(const I& i) { graph->erase(i); } |
---|
| 457 | |
---|
| 458 | // void clear() { graph->clear(); } |
---|
| 459 | |
---|
| 460 | // template<typename T> class NodeMap : public Graph::NodeMap<T> { }; |
---|
| 461 | // template<typename T> class EdgeMap : public Graph::EdgeMap<T> { }; |
---|
| 462 | |
---|
| 463 | // void setGraph(Graph& _graph) { graph = &_graph; } |
---|
| 464 | // Graph& getGraph() { return (*graph); } |
---|
| 465 | |
---|
| 466 | // //SymGraphWrapper() : graph(0) { } |
---|
| 467 | // SymGraphWrapper(Graph& _graph) : graph(&_graph) { } |
---|
| 468 | // }; |
---|
| 469 | |
---|
| 470 | |
---|
| 471 | template<typename Graph, typename Number, typename FlowMap, typename CapacityMap> |
---|
| 472 | class ResGraphWrapper { |
---|
[76] | 473 | public: |
---|
| 474 | typedef Graph BaseGraph; |
---|
[174] | 475 | typedef typename Graph::Node Node; |
---|
[155] | 476 | typedef typename Graph::NodeIt NodeIt; |
---|
| 477 | private: |
---|
| 478 | typedef typename Graph::OutEdgeIt OldOutEdgeIt; |
---|
| 479 | typedef typename Graph::InEdgeIt OldInEdgeIt; |
---|
[199] | 480 | protected: |
---|
[174] | 481 | const Graph* graph; |
---|
[155] | 482 | FlowMap* flow; |
---|
| 483 | const CapacityMap* capacity; |
---|
| 484 | public: |
---|
[168] | 485 | |
---|
[155] | 486 | ResGraphWrapper(const Graph& _G, FlowMap& _flow, |
---|
| 487 | const CapacityMap& _capacity) : |
---|
[174] | 488 | graph(&_G), flow(&_flow), capacity(&_capacity) { } |
---|
[168] | 489 | |
---|
| 490 | void setGraph(const Graph& _graph) { graph = &_graph; } |
---|
[174] | 491 | const Graph& getGraph() const { return (*graph); } |
---|
[168] | 492 | |
---|
[174] | 493 | class Edge; |
---|
[155] | 494 | class OutEdgeIt; |
---|
[174] | 495 | friend class Edge; |
---|
[155] | 496 | friend class OutEdgeIt; |
---|
[76] | 497 | |
---|
[174] | 498 | class Edge { |
---|
[155] | 499 | friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>; |
---|
| 500 | protected: |
---|
[168] | 501 | bool out_or_in; //true, iff out |
---|
[155] | 502 | OldOutEdgeIt out; |
---|
| 503 | OldInEdgeIt in; |
---|
| 504 | public: |
---|
[174] | 505 | Edge() : out_or_in(true) { } |
---|
| 506 | Edge(const Invalid& i) : out_or_in(false), out(), in(i) { } |
---|
[168] | 507 | // bool valid() const { |
---|
| 508 | // return out_or_in && out.valid() || in.valid(); } |
---|
[174] | 509 | friend bool operator==(const Edge& u, const Edge& v) { |
---|
| 510 | if (v.out_or_in) |
---|
| 511 | return (u.out_or_in && u.out==v.out); |
---|
| 512 | else |
---|
| 513 | return (!u.out_or_in && u.in==v.in); |
---|
| 514 | } |
---|
| 515 | friend bool operator!=(const Edge& u, const Edge& v) { |
---|
| 516 | if (v.out_or_in) |
---|
| 517 | return (!u.out_or_in || u.out!=v.out); |
---|
| 518 | else |
---|
| 519 | return (u.out_or_in || u.in!=v.in); |
---|
| 520 | } |
---|
[155] | 521 | }; |
---|
| 522 | |
---|
| 523 | |
---|
[174] | 524 | class OutEdgeIt : public Edge { |
---|
[155] | 525 | friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>; |
---|
| 526 | public: |
---|
| 527 | OutEdgeIt() { } |
---|
[168] | 528 | //FIXME |
---|
[174] | 529 | OutEdgeIt(const Edge& e) : Edge(e) { } |
---|
| 530 | OutEdgeIt(const Invalid& i) : Edge(i) { } |
---|
[155] | 531 | private: |
---|
[174] | 532 | OutEdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG, Node v) : Edge() { |
---|
[212] | 533 | resG.graph->first(out, v); |
---|
[174] | 534 | while( resG.graph->valid(out) && !(resG.free(out)>0) ) { resG.graph->next(out); } |
---|
| 535 | if (!resG.graph->valid(out)) { |
---|
[155] | 536 | out_or_in=0; |
---|
[212] | 537 | resG.graph->first(in, v); |
---|
[174] | 538 | while( resG.graph->valid(in) && !(resG.free(in)>0) ) { resG.graph->next(in); } |
---|
[155] | 539 | } |
---|
| 540 | } |
---|
[168] | 541 | // public: |
---|
| 542 | // OutEdgeIt& operator++() { |
---|
| 543 | // if (out_or_in) { |
---|
[174] | 544 | // Node v=/*resG->*/G->aNode(out); |
---|
[168] | 545 | // ++out; |
---|
[174] | 546 | // while( out.valid() && !(Edge::free()>0) ) { ++out; } |
---|
[168] | 547 | // if (!out.valid()) { |
---|
| 548 | // out_or_in=0; |
---|
[212] | 549 | // G->first(in, v); |
---|
[174] | 550 | // while( in.valid() && !(Edge::free()>0) ) { ++in; } |
---|
[168] | 551 | // } |
---|
| 552 | // } else { |
---|
| 553 | // ++in; |
---|
[174] | 554 | // while( in.valid() && !(Edge::free()>0) ) { ++in; } |
---|
[168] | 555 | // } |
---|
| 556 | // return *this; |
---|
| 557 | // } |
---|
[155] | 558 | }; |
---|
| 559 | |
---|
[174] | 560 | class EdgeIt : public Edge { |
---|
[155] | 561 | friend class ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>; |
---|
[174] | 562 | typename Graph::NodeIt v; |
---|
[155] | 563 | public: |
---|
[174] | 564 | EdgeIt() { } |
---|
| 565 | //EdgeIt(const EdgeIt& e) : Edge(e), v(e.v) { } |
---|
| 566 | EdgeIt(const Invalid& i) : Edge(i) { } |
---|
| 567 | EdgeIt(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& resG) : Edge() { |
---|
[212] | 568 | resG.graph->first(v); |
---|
| 569 | if (resG.graph->valid(v)) resG.graph->first(out, v); else out=OldOutEdgeIt(INVALID); |
---|
[174] | 570 | while (resG.graph->valid(out) && !(resG.free(out)>0) ) { resG.graph->next(out); } |
---|
| 571 | while (resG.graph->valid(v) && !resG.graph->valid(out)) { |
---|
| 572 | resG.graph->next(v); |
---|
[212] | 573 | if (resG.graph->valid(v)) resG.graph->first(out, v); |
---|
[174] | 574 | while (resG.graph->valid(out) && !(resG.free(out)>0) ) { resG.graph->next(out); } |
---|
[155] | 575 | } |
---|
[174] | 576 | if (!resG.graph->valid(out)) { |
---|
[155] | 577 | out_or_in=0; |
---|
[212] | 578 | resG.graph->first(v); |
---|
| 579 | if (resG.graph->valid(v)) resG.graph->first(in, v); else in=OldInEdgeIt(INVALID); |
---|
[174] | 580 | while (resG.graph->valid(in) && !(resG.free(in)>0) ) { resG.graph->next(in); } |
---|
| 581 | while (resG.graph->valid(v) && !resG.graph->valid(in)) { |
---|
| 582 | resG.graph->next(v); |
---|
[212] | 583 | if (resG.graph->valid(v)) resG.graph->first(in, v); |
---|
[174] | 584 | while (resG.graph->valid(in) && !(resG.free(in)>0) ) { resG.graph->next(in); } |
---|
[155] | 585 | } |
---|
| 586 | } |
---|
| 587 | } |
---|
[174] | 588 | // EdgeIt& operator++() { |
---|
[168] | 589 | // if (out_or_in) { |
---|
| 590 | // ++out; |
---|
[174] | 591 | // while (out.valid() && !(Edge::free()>0) ) { ++out; } |
---|
[168] | 592 | // while (v.valid() && !out.valid()) { |
---|
| 593 | // ++v; |
---|
[212] | 594 | // if (v.valid()) G->first(out, v); |
---|
[174] | 595 | // while (out.valid() && !(Edge::free()>0) ) { ++out; } |
---|
[168] | 596 | // } |
---|
| 597 | // if (!out.valid()) { |
---|
| 598 | // out_or_in=0; |
---|
[212] | 599 | // G->first(v); |
---|
| 600 | // if (v.valid()) G->first(in, v); else in=OldInEdgeIt(); |
---|
[174] | 601 | // while (in.valid() && !(Edge::free()>0) ) { ++in; } |
---|
[168] | 602 | // while (v.valid() && !in.valid()) { |
---|
| 603 | // ++v; |
---|
[212] | 604 | // if (v.valid()) G->first(in, v); |
---|
[174] | 605 | // while (in.valid() && !(Edge::free()>0) ) { ++in; } |
---|
[168] | 606 | // } |
---|
| 607 | // } |
---|
| 608 | // } else { |
---|
| 609 | // ++in; |
---|
[174] | 610 | // while (in.valid() && !(Edge::free()>0) ) { ++in; } |
---|
[168] | 611 | // while (v.valid() && !in.valid()) { |
---|
| 612 | // ++v; |
---|
[212] | 613 | // if (v.valid()) G->first(in, v); |
---|
[174] | 614 | // while (in.valid() && !(Edge::free()>0) ) { ++in; } |
---|
[168] | 615 | // } |
---|
| 616 | // } |
---|
| 617 | // return *this; |
---|
| 618 | // } |
---|
[155] | 619 | }; |
---|
| 620 | |
---|
[212] | 621 | NodeIt& first(NodeIt& v) const { return graph->first(v); } |
---|
| 622 | OutEdgeIt& first(OutEdgeIt& e, Node v) const { |
---|
[168] | 623 | e=OutEdgeIt(*this, v); |
---|
[174] | 624 | return e; |
---|
[155] | 625 | } |
---|
[212] | 626 | EdgeIt& first(EdgeIt& e) const { |
---|
[174] | 627 | e=EdgeIt(*this); |
---|
| 628 | return e; |
---|
[155] | 629 | } |
---|
| 630 | |
---|
[174] | 631 | NodeIt& next(NodeIt& n) const { return graph->next(n); } |
---|
[155] | 632 | |
---|
| 633 | OutEdgeIt& next(OutEdgeIt& e) const { |
---|
| 634 | if (e.out_or_in) { |
---|
[174] | 635 | Node v=graph->aNode(e.out); |
---|
| 636 | graph->next(e.out); |
---|
| 637 | while( graph->valid(e.out) && !(free(e.out)>0) ) { graph->next(e.out); } |
---|
| 638 | if (!graph->valid(e.out)) { |
---|
[155] | 639 | e.out_or_in=0; |
---|
[212] | 640 | graph->first(e.in, v); |
---|
[174] | 641 | while( graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); } |
---|
[155] | 642 | } |
---|
| 643 | } else { |
---|
[174] | 644 | graph->next(e.in); |
---|
| 645 | while( graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); } |
---|
[155] | 646 | } |
---|
| 647 | return e; |
---|
| 648 | } |
---|
| 649 | |
---|
[174] | 650 | EdgeIt& next(EdgeIt& e) const { |
---|
[155] | 651 | if (e.out_or_in) { |
---|
[174] | 652 | graph->next(e.out); |
---|
| 653 | while (graph->valid(e.out) && !(free(e.out)>0) ) { graph->next(e.out); } |
---|
| 654 | while (graph->valid(e.v) && !graph->valid(e.out)) { |
---|
| 655 | graph->next(e.v); |
---|
[212] | 656 | if (graph->valid(e.v)) graph->first(e.out, e.v); |
---|
[174] | 657 | while (graph->valid(e.out) && !(free(e.out)>0) ) { graph->next(e.out); } |
---|
[155] | 658 | } |
---|
[174] | 659 | if (!graph->valid(e.out)) { |
---|
[155] | 660 | e.out_or_in=0; |
---|
[212] | 661 | graph->first(e.v); |
---|
| 662 | if (graph->valid(e.v)) graph->first(e.in, e.v); else e.in=OldInEdgeIt(INVALID); |
---|
[174] | 663 | while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); } |
---|
| 664 | while (graph->valid(e.v) && !graph->valid(e.in)) { |
---|
| 665 | graph->next(e.v); |
---|
[212] | 666 | if (graph->valid(e.v)) graph->first(e.in, e.v); |
---|
[174] | 667 | while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); } |
---|
[155] | 668 | } |
---|
| 669 | } |
---|
| 670 | } else { |
---|
[174] | 671 | graph->next(e.in); |
---|
| 672 | while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); } |
---|
| 673 | while (graph->valid(e.v) && !graph->valid(e.in)) { |
---|
| 674 | graph->next(e.v); |
---|
[212] | 675 | if (graph->valid(e.v)) graph->first(e.in, e.v); |
---|
[174] | 676 | while (graph->valid(e.in) && !(free(e.in)>0) ) { graph->next(e.in); } |
---|
[155] | 677 | } |
---|
| 678 | } |
---|
| 679 | return e; |
---|
| 680 | } |
---|
[76] | 681 | |
---|
| 682 | |
---|
[155] | 683 | template< typename It > |
---|
| 684 | It first() const { |
---|
| 685 | It e; |
---|
[212] | 686 | first(e); |
---|
[155] | 687 | return e; |
---|
| 688 | } |
---|
[76] | 689 | |
---|
[155] | 690 | template< typename It > |
---|
[174] | 691 | It first(Node v) const { |
---|
[155] | 692 | It e; |
---|
[212] | 693 | first(e, v); |
---|
[155] | 694 | return e; |
---|
| 695 | } |
---|
[76] | 696 | |
---|
[174] | 697 | Node tail(Edge e) const { |
---|
| 698 | return ((e.out_or_in) ? graph->aNode(e.out) : graph->aNode(e.in)); } |
---|
| 699 | Node head(Edge e) const { |
---|
| 700 | return ((e.out_or_in) ? graph->bNode(e.out) : graph->bNode(e.in)); } |
---|
[76] | 701 | |
---|
[174] | 702 | Node aNode(OutEdgeIt e) const { |
---|
| 703 | return ((e.out_or_in) ? graph->aNode(e.out) : graph->aNode(e.in)); } |
---|
| 704 | Node bNode(OutEdgeIt e) const { |
---|
| 705 | return ((e.out_or_in) ? graph->bNode(e.out) : graph->bNode(e.in)); } |
---|
[76] | 706 | |
---|
[174] | 707 | int id(Node v) const { return graph->id(v); } |
---|
[155] | 708 | |
---|
[174] | 709 | bool valid(Node n) const { return graph->valid(n); } |
---|
| 710 | bool valid(Edge e) const { |
---|
| 711 | return e.out_or_in ? graph->valid(e.out) : graph->valid(e.in); } |
---|
[155] | 712 | |
---|
[174] | 713 | void augment(const Edge& e, Number a) const { |
---|
[168] | 714 | if (e.out_or_in) |
---|
| 715 | flow->set(e.out, flow->get(e.out)+a); |
---|
| 716 | else |
---|
| 717 | flow->set(e.in, flow->get(e.in)-a); |
---|
| 718 | } |
---|
| 719 | |
---|
[174] | 720 | Number free(const Edge& e) const { |
---|
[168] | 721 | if (e.out_or_in) |
---|
| 722 | return (capacity->get(e.out)-flow->get(e.out)); |
---|
| 723 | else |
---|
| 724 | return (flow->get(e.in)); |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | Number free(OldOutEdgeIt out) const { |
---|
| 728 | return (capacity->get(out)-flow->get(out)); |
---|
| 729 | } |
---|
| 730 | |
---|
| 731 | Number free(OldInEdgeIt in) const { |
---|
| 732 | return (flow->get(in)); |
---|
| 733 | } |
---|
| 734 | |
---|
[155] | 735 | template<typename T> class NodeMap : public Graph::NodeMap<T> { |
---|
| 736 | public: |
---|
| 737 | NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) |
---|
[158] | 738 | : Graph::NodeMap<T>(_G.getGraph()) { } |
---|
[155] | 739 | NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, |
---|
[158] | 740 | T a) : Graph::NodeMap<T>(_G.getGraph(), a) { } |
---|
[155] | 741 | }; |
---|
| 742 | |
---|
| 743 | // template <typename T> |
---|
| 744 | // class NodeMap { |
---|
| 745 | // typename Graph::NodeMap<T> node_map; |
---|
| 746 | // public: |
---|
[174] | 747 | // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(*(_G.graph)) { } |
---|
| 748 | // NodeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : node_map(*(_G.graph), a) { } |
---|
| 749 | // void set(Node nit, T a) { node_map.set(nit, a); } |
---|
| 750 | // T get(Node nit) const { return node_map.get(nit); } |
---|
[155] | 751 | // }; |
---|
| 752 | |
---|
| 753 | template <typename T> |
---|
| 754 | class EdgeMap { |
---|
| 755 | typename Graph::EdgeMap<T> forward_map, backward_map; |
---|
| 756 | public: |
---|
[158] | 757 | EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : forward_map(_G.getGraph()), backward_map(_G.getGraph()) { } |
---|
| 758 | EdgeMap(const ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : forward_map(_G.getGraph(), a), backward_map(_G.getGraph(), a) { } |
---|
[174] | 759 | void set(Edge e, T a) { |
---|
[155] | 760 | if (e.out_or_in) |
---|
| 761 | forward_map.set(e.out, a); |
---|
| 762 | else |
---|
| 763 | backward_map.set(e.in, a); |
---|
| 764 | } |
---|
[174] | 765 | T get(Edge e) { |
---|
[155] | 766 | if (e.out_or_in) |
---|
| 767 | return forward_map.get(e.out); |
---|
| 768 | else |
---|
| 769 | return backward_map.get(e.in); |
---|
| 770 | } |
---|
| 771 | }; |
---|
[168] | 772 | }; |
---|
| 773 | |
---|
| 774 | template<typename Graph, typename Number, typename FlowMap, typename CapacityMap> |
---|
| 775 | class ErasingResGraphWrapper : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap> { |
---|
| 776 | protected: |
---|
| 777 | ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt> first_out_edges; |
---|
| 778 | //ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<int> dist; |
---|
| 779 | public: |
---|
| 780 | ErasingResGraphWrapper(const Graph& _G, FlowMap& _flow, |
---|
| 781 | const CapacityMap& _capacity) : |
---|
| 782 | ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>(_G, _flow, _capacity), |
---|
| 783 | first_out_edges(*this) /*, dist(*this)*/ { |
---|
[174] | 784 | for(NodeIt n=this->template first<NodeIt>(); this->valid(n); this->next(n)) { |
---|
[168] | 785 | OutEdgeIt e; |
---|
[212] | 786 | ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(e, n); |
---|
[168] | 787 | first_out_edges.set(n, e); |
---|
| 788 | } |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | //void setGraph(Graph& _graph) { graph = &_graph; } |
---|
| 792 | //Graph& getGraph() const { return (*graph); } |
---|
| 793 | |
---|
| 794 | //TrivGraphWrapper() : graph(0) { } |
---|
| 795 | //ErasingResGraphWrapper(Graph& _graph) : graph(&_graph) { } |
---|
| 796 | |
---|
| 797 | //typedef Graph BaseGraph; |
---|
| 798 | |
---|
[174] | 799 | //typedef typename Graph::Node Node; |
---|
[168] | 800 | //typedef typename Graph::NodeIt NodeIt; |
---|
| 801 | |
---|
[174] | 802 | //typedef typename Graph::Edge Edge; |
---|
[168] | 803 | //typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 804 | //typedef typename Graph::InEdgeIt InEdgeIt; |
---|
| 805 | //typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 806 | //typedef typename Graph::EdgeIt EdgeIt; |
---|
[168] | 807 | |
---|
[174] | 808 | typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Node Node; |
---|
[168] | 809 | typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeIt NodeIt; |
---|
| 810 | |
---|
[174] | 811 | typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Edge Edge; |
---|
[168] | 812 | typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt OutEdgeIt; |
---|
| 813 | //typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::InEdgeIt InEdgeIt; |
---|
| 814 | //typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 815 | //typedef typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeIt EdgeIt; |
---|
[168] | 816 | |
---|
[212] | 817 | NodeIt& first(NodeIt& n) const { |
---|
| 818 | return ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(n); |
---|
[168] | 819 | } |
---|
| 820 | |
---|
[212] | 821 | OutEdgeIt& first(OutEdgeIt& e, const Node& n) const { |
---|
[168] | 822 | e=first_out_edges.get(n); |
---|
| 823 | return e; |
---|
| 824 | } |
---|
| 825 | |
---|
[212] | 826 | //ROSSZ template<typename I> I& first(I& i) const { return first(i); } |
---|
| 827 | //ROSSZ template<typename I, typename P> I& first(I& i, const P& p) const { |
---|
| 828 | // return first(i, p); } |
---|
[168] | 829 | |
---|
| 830 | //template<typename I> I getNext(const I& i) const { |
---|
| 831 | // return graph->getNext(i); } |
---|
| 832 | //template<typename I> I& next(I &i) const { return graph->next(i); } |
---|
| 833 | |
---|
| 834 | template< typename It > It first() const { |
---|
[212] | 835 | It e; first(e); return e; } |
---|
[168] | 836 | |
---|
[174] | 837 | template< typename It > It first(const Node& v) const { |
---|
[212] | 838 | It e; first(e, v); return e; } |
---|
[168] | 839 | |
---|
[174] | 840 | //Node head(const Edge& e) const { return graph->head(e); } |
---|
| 841 | //Node tail(const Edge& e) const { return graph->tail(e); } |
---|
[168] | 842 | |
---|
| 843 | //template<typename I> bool valid(const I& i) const |
---|
| 844 | // { return graph->valid(i); } |
---|
| 845 | |
---|
| 846 | //int nodeNum() const { return graph->nodeNum(); } |
---|
| 847 | //int edgeNum() const { return graph->edgeNum(); } |
---|
| 848 | |
---|
[174] | 849 | //template<typename I> Node aNode(const I& e) const { |
---|
[168] | 850 | // return graph->aNode(e); } |
---|
[174] | 851 | //template<typename I> Node bNode(const I& e) const { |
---|
[168] | 852 | // return graph->bNode(e); } |
---|
| 853 | |
---|
[174] | 854 | //Node addNode() const { return graph->addNode(); } |
---|
| 855 | //Edge addEdge(const Node& tail, const Node& head) const { |
---|
[168] | 856 | // return graph->addEdge(tail, head); } |
---|
| 857 | |
---|
| 858 | //void erase(const OutEdgeIt& e) { |
---|
| 859 | // first_out_edge(this->tail(e))=e; |
---|
| 860 | //} |
---|
[174] | 861 | void erase(const Edge& e) { |
---|
[168] | 862 | OutEdgeIt f(e); |
---|
| 863 | next(f); |
---|
| 864 | first_out_edges.set(this->tail(e), f); |
---|
| 865 | } |
---|
| 866 | //template<typename I> void erase(const I& i) const { graph->erase(i); } |
---|
| 867 | |
---|
| 868 | //void clear() const { graph->clear(); } |
---|
| 869 | |
---|
| 870 | template<typename T> class NodeMap : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T> { |
---|
| 871 | public: |
---|
| 872 | NodeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : |
---|
| 873 | ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/) { } |
---|
| 874 | NodeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : |
---|
| 875 | ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/, a) { } |
---|
| 876 | }; |
---|
| 877 | |
---|
| 878 | template<typename T> class EdgeMap : public ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T> { |
---|
| 879 | public: |
---|
| 880 | EdgeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G) : |
---|
| 881 | ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/) { } |
---|
| 882 | EdgeMap(const ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>& _G, T a) : |
---|
| 883 | ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/, a) { } |
---|
| 884 | }; |
---|
| 885 | }; |
---|
| 886 | |
---|
| 887 | template<typename GraphWrapper> |
---|
| 888 | class FilterGraphWrapper { |
---|
| 889 | }; |
---|
| 890 | |
---|
| 891 | template<typename Graph, typename Number, typename FlowMap, typename CapacityMap> |
---|
| 892 | class FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> { |
---|
| 893 | |
---|
| 894 | //Graph* graph; |
---|
| 895 | |
---|
| 896 | public: |
---|
| 897 | //typedef Graph BaseGraph; |
---|
| 898 | |
---|
[174] | 899 | typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Node Node; |
---|
[168] | 900 | typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeIt NodeIt; |
---|
| 901 | |
---|
[174] | 902 | typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::Edge Edge; |
---|
[168] | 903 | typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt OutEdgeIt; |
---|
| 904 | //typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::InEdgeIt InEdgeIt; |
---|
| 905 | //typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 906 | typedef typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeIt EdgeIt; |
---|
[168] | 907 | |
---|
| 908 | //FilterGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<typename ResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt> first_out_edges; |
---|
| 909 | |
---|
| 910 | public: |
---|
| 911 | FilterGraphWrapper(const Graph& _G, FlowMap& _flow, |
---|
| 912 | const CapacityMap& _capacity) : |
---|
[199] | 913 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>(_G, _flow, _capacity), dist(*this, graph->nodeNum()) { |
---|
[168] | 914 | } |
---|
| 915 | |
---|
[212] | 916 | OutEdgeIt& first(OutEdgeIt& e, const Node& n) const { |
---|
| 917 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(e, n); |
---|
[199] | 918 | while (valid(e) && (dist.get(tail(e))/*+1!=*/>=dist.get(head(e)))) |
---|
[168] | 919 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e); |
---|
| 920 | return e; |
---|
| 921 | } |
---|
| 922 | |
---|
[174] | 923 | NodeIt& next(NodeIt& e) const { |
---|
[168] | 924 | return ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e); |
---|
| 925 | } |
---|
| 926 | |
---|
| 927 | OutEdgeIt& next(OutEdgeIt& e) const { |
---|
| 928 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e); |
---|
[199] | 929 | while (valid(e) && (dist.get(tail(e))/*+1!*/>=dist.get(head(e)))) |
---|
[168] | 930 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(e); |
---|
| 931 | return e; |
---|
| 932 | } |
---|
| 933 | |
---|
[212] | 934 | NodeIt& first(NodeIt& n) const { |
---|
| 935 | return ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::first(n); |
---|
[168] | 936 | } |
---|
| 937 | |
---|
[174] | 938 | void erase(const Edge& e) { |
---|
[168] | 939 | OutEdgeIt f(e); |
---|
| 940 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(f); |
---|
[199] | 941 | while (valid(f) && (dist.get(tail(f))/*+1!=*/>=dist.get(head(f)))) |
---|
[168] | 942 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::next(f); |
---|
| 943 | first_out_edges.set(this->tail(e), f); |
---|
| 944 | } |
---|
| 945 | |
---|
| 946 | //TrivGraphWrapper() : graph(0) { } |
---|
| 947 | //TrivGraphWrapper(Graph& _graph) : graph(&_graph) { } |
---|
| 948 | |
---|
| 949 | //void setGraph(Graph& _graph) { graph = &_graph; } |
---|
| 950 | //Graph& getGraph() const { return (*graph); } |
---|
| 951 | |
---|
[212] | 952 | //template<typename I> I& first(I& i) const { return graph->first(i); } |
---|
| 953 | //template<typename I, typename P> I& first(I& i, const P& p) const { |
---|
| 954 | // return graph->first(i, p); } |
---|
[168] | 955 | |
---|
| 956 | //template<typename I> I getNext(const I& i) const { |
---|
| 957 | // return graph->getNext(i); } |
---|
| 958 | //template<typename I> I& next(I &i) const { return graph->next(i); } |
---|
| 959 | |
---|
| 960 | template< typename It > It first() const { |
---|
[212] | 961 | It e; first(e); return e; } |
---|
[168] | 962 | |
---|
[174] | 963 | template< typename It > It first(const Node& v) const { |
---|
[212] | 964 | It e; first(e, v); return e; } |
---|
[168] | 965 | |
---|
[174] | 966 | //Node head(const Edge& e) const { return graph->head(e); } |
---|
| 967 | //Node tail(const Edge& e) const { return graph->tail(e); } |
---|
[168] | 968 | |
---|
| 969 | //template<typename I> bool valid(const I& i) const |
---|
| 970 | // { return graph->valid(i); } |
---|
| 971 | |
---|
| 972 | //template<typename I> void setInvalid(const I &i); |
---|
| 973 | //{ return graph->setInvalid(i); } |
---|
| 974 | |
---|
| 975 | //int nodeNum() const { return graph->nodeNum(); } |
---|
| 976 | //int edgeNum() const { return graph->edgeNum(); } |
---|
| 977 | |
---|
[174] | 978 | //template<typename I> Node aNode(const I& e) const { |
---|
[168] | 979 | // return graph->aNode(e); } |
---|
[174] | 980 | //template<typename I> Node bNode(const I& e) const { |
---|
[168] | 981 | // return graph->bNode(e); } |
---|
| 982 | |
---|
[174] | 983 | //Node addNode() const { return graph->addNode(); } |
---|
| 984 | //Edge addEdge(const Node& tail, const Node& head) const { |
---|
[168] | 985 | // return graph->addEdge(tail, head); } |
---|
| 986 | |
---|
| 987 | //template<typename I> void erase(const I& i) const { graph->erase(i); } |
---|
| 988 | |
---|
| 989 | //void clear() const { graph->clear(); } |
---|
| 990 | |
---|
| 991 | template<typename T> class NodeMap : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T> { |
---|
| 992 | public: |
---|
| 993 | NodeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G) : |
---|
| 994 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/) { } |
---|
| 995 | NodeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G, T a) : |
---|
| 996 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<T>(_G /*_G.getGraph()*/, a) { } |
---|
| 997 | }; |
---|
| 998 | |
---|
| 999 | template<typename T> class EdgeMap : public ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T> { |
---|
| 1000 | public: |
---|
| 1001 | EdgeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G) : |
---|
| 1002 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/) { } |
---|
| 1003 | EdgeMap(const FilterGraphWrapper<ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> >& _G, T a) : |
---|
| 1004 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::EdgeMap<T>(_G /*_G.getGraph()*/, a) { } |
---|
| 1005 | }; |
---|
| 1006 | |
---|
| 1007 | public: |
---|
| 1008 | ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<int> dist; |
---|
[155] | 1009 | |
---|
[76] | 1010 | }; |
---|
| 1011 | |
---|
| 1012 | |
---|
[148] | 1013 | |
---|
| 1014 | // // FIXME: comparison should be made better!!! |
---|
| 1015 | // template<typename Graph, typename T, typename LowerMap, typename FlowMap, typename UpperMap> |
---|
| 1016 | // class ResGraphWrapper |
---|
| 1017 | // { |
---|
| 1018 | // Graph* graph; |
---|
[76] | 1019 | |
---|
[148] | 1020 | // public: |
---|
| 1021 | // typedef Graph BaseGraph; |
---|
[76] | 1022 | |
---|
[174] | 1023 | // typedef typename Graph::Node Node; |
---|
| 1024 | // typedef typename Graph::Edge Edge; |
---|
| 1025 | |
---|
[148] | 1026 | // typedef typename Graph::NodeIt NodeIt; |
---|
[76] | 1027 | |
---|
[148] | 1028 | // class OutEdgeIt { |
---|
| 1029 | // public: |
---|
[174] | 1030 | // //Graph::Node n; |
---|
[148] | 1031 | // bool out_or_in; |
---|
| 1032 | // typename Graph::OutEdgeIt o; |
---|
| 1033 | // typename Graph::InEdgeIt i; |
---|
| 1034 | // }; |
---|
| 1035 | // class InEdgeIt { |
---|
| 1036 | // public: |
---|
[174] | 1037 | // //Graph::Node n; |
---|
[148] | 1038 | // bool out_or_in; |
---|
| 1039 | // typename Graph::OutEdgeIt o; |
---|
| 1040 | // typename Graph::InEdgeIt i; |
---|
| 1041 | // }; |
---|
| 1042 | // typedef typename Graph::SymEdgeIt SymEdgeIt; |
---|
[174] | 1043 | // typedef typename Graph::EdgeIt EdgeIt; |
---|
[76] | 1044 | |
---|
[148] | 1045 | // int nodeNum() const { return graph->nodeNum(); } |
---|
| 1046 | // int edgeNum() const { return graph->edgeNum(); } |
---|
[76] | 1047 | |
---|
[212] | 1048 | // Node& first(Node& n) const { return graph->first(n); } |
---|
[76] | 1049 | |
---|
[174] | 1050 | // // Edge and SymEdge is missing!!!! |
---|
| 1051 | // // Edge <-> In/OutEdgeIt conversion is missing!!!! |
---|
[76] | 1052 | |
---|
[148] | 1053 | // //FIXME |
---|
[212] | 1054 | // OutEdgeIt& first(OutEdgeIt& e, const Node& n) const |
---|
[148] | 1055 | // { |
---|
| 1056 | // e.n=n; |
---|
[212] | 1057 | // graph->first(e.o,n); |
---|
[148] | 1058 | // while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o)) |
---|
| 1059 | // graph->goNext(e.o); |
---|
| 1060 | // if(!graph->valid(e.o)) { |
---|
[212] | 1061 | // graph->first(e.i,n); |
---|
[148] | 1062 | // while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i)) |
---|
| 1063 | // graph->goNext(e.i); |
---|
| 1064 | // } |
---|
| 1065 | // return e; |
---|
| 1066 | // } |
---|
| 1067 | // /* |
---|
| 1068 | // OutEdgeIt &goNext(OutEdgeIt &e) |
---|
| 1069 | // { |
---|
| 1070 | // if(graph->valid(e.o)) { |
---|
| 1071 | // while(graph->valid(e.o) && fmap.get(e.o)>=himap.get(e.o)) |
---|
| 1072 | // graph->goNext(e.o); |
---|
| 1073 | // if(graph->valid(e.o)) return e; |
---|
[212] | 1074 | // else graph->first(e.i,e.n); |
---|
[148] | 1075 | // } |
---|
| 1076 | // else { |
---|
| 1077 | // while(graph->valid(e.i) && fmap.get(e.i)<=lomap.get(e.i)) |
---|
| 1078 | // graph->goNext(e.i); |
---|
| 1079 | // return e; |
---|
| 1080 | // } |
---|
| 1081 | // } |
---|
| 1082 | // OutEdgeIt Next(const OutEdgeIt &e) {OutEdgeIt t(e); return goNext(t);} |
---|
| 1083 | // */ |
---|
| 1084 | // //bool valid(const OutEdgeIt e) { return graph->valid(e.o)||graph->valid(e.i);} |
---|
[76] | 1085 | |
---|
[148] | 1086 | // //FIXME |
---|
[212] | 1087 | // InEdgeIt& first(InEdgeIt& e, const Node& n) const |
---|
[148] | 1088 | // { |
---|
| 1089 | // e.n=n; |
---|
[212] | 1090 | // graph->first(e.i,n); |
---|
[148] | 1091 | // while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i)) |
---|
| 1092 | // graph->goNext(e.i); |
---|
| 1093 | // if(!graph->valid(e.i)) { |
---|
[212] | 1094 | // graph->first(e.o,n); |
---|
[148] | 1095 | // while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o)) |
---|
| 1096 | // graph->goNext(e.o); |
---|
| 1097 | // } |
---|
| 1098 | // return e; |
---|
| 1099 | // } |
---|
| 1100 | // /* |
---|
| 1101 | // InEdgeIt &goNext(InEdgeIt &e) |
---|
| 1102 | // { |
---|
| 1103 | // if(graph->valid(e.i)) { |
---|
| 1104 | // while(graph->valid(e.i) && fmap.get(e.i)>=himap.get(e.i)) |
---|
| 1105 | // graph->goNext(e.i); |
---|
| 1106 | // if(graph->valid(e.i)) return e; |
---|
[212] | 1107 | // else graph->first(e.o,e.n); |
---|
[148] | 1108 | // } |
---|
| 1109 | // else { |
---|
| 1110 | // while(graph->valid(e.o) && fmap.get(e.o)<=lomap.get(e.o)) |
---|
| 1111 | // graph->goNext(e.o); |
---|
| 1112 | // return e; |
---|
| 1113 | // } |
---|
| 1114 | // } |
---|
| 1115 | // InEdgeIt Next(const InEdgeIt &e) {InEdgeIt t(e); return goNext(t);} |
---|
| 1116 | // */ |
---|
| 1117 | // //bool valid(const InEdgeIt e) { return graph->valid(e.i)||graph->valid(e.o);} |
---|
[76] | 1118 | |
---|
[148] | 1119 | // //template<typename I> I &goNext(I &i); { return graph->goNext(i); } |
---|
| 1120 | // //template<typename I> I next(const I i); { return graph->goNext(i); } |
---|
[76] | 1121 | |
---|
[148] | 1122 | // template< typename It > It first() const { |
---|
[212] | 1123 | // It e; first(e); return e; } |
---|
[76] | 1124 | |
---|
[174] | 1125 | // template< typename It > It first(Node v) const { |
---|
[212] | 1126 | // It e; first(e, v); return e; } |
---|
[76] | 1127 | |
---|
[174] | 1128 | // Node head(const Edge& e) const { return graph->head(e); } |
---|
| 1129 | // Node tail(const Edge& e) const { return graph->tail(e); } |
---|
[76] | 1130 | |
---|
[174] | 1131 | // template<typename I> Node aNode(const I& e) const { |
---|
[148] | 1132 | // return graph->aNode(e); } |
---|
[174] | 1133 | // template<typename I> Node bNode(const I& e) const { |
---|
[148] | 1134 | // return graph->bNode(e); } |
---|
[76] | 1135 | |
---|
[148] | 1136 | // //template<typename I> bool valid(const I i); |
---|
| 1137 | // //{ return graph->valid(i); } |
---|
[76] | 1138 | |
---|
[148] | 1139 | // //template<typename I> void setInvalid(const I &i); |
---|
| 1140 | // //{ return graph->setInvalid(i); } |
---|
[76] | 1141 | |
---|
[174] | 1142 | // Node addNode() { return graph->addNode(); } |
---|
| 1143 | // Edge addEdge(const Node& tail, const Node& head) { |
---|
[148] | 1144 | // return graph->addEdge(tail, head); } |
---|
[76] | 1145 | |
---|
[148] | 1146 | // template<typename I> void erase(const I& i) { graph->erase(i); } |
---|
[76] | 1147 | |
---|
[148] | 1148 | // void clear() { graph->clear(); } |
---|
[76] | 1149 | |
---|
[148] | 1150 | // template<typename S> class NodeMap : public Graph::NodeMap<S> { }; |
---|
| 1151 | // template<typename S> class EdgeMap : public Graph::EdgeMap<S> { }; |
---|
[76] | 1152 | |
---|
[148] | 1153 | // void setGraph(Graph& _graph) { graph = &_graph; } |
---|
| 1154 | // Graph& getGraph() { return (*graph); } |
---|
[76] | 1155 | |
---|
[148] | 1156 | // //ResGraphWrapper() : graph(0) { } |
---|
| 1157 | // ResGraphWrapper(Graph& _graph) : graph(&_graph) { } |
---|
| 1158 | // }; |
---|
[76] | 1159 | |
---|
[105] | 1160 | } //namespace hugo |
---|
[76] | 1161 | |
---|
| 1162 | #endif //GRAPH_WRAPPER_H |
---|
| 1163 | |
---|