[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[39] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[39] | 4 | * |
---|
[440] | 5 | * Copyright (C) 2003-2009 |
---|
[39] | 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 | |
---|
[57] | 19 | #ifndef LEMON_LIST_GRAPH_H |
---|
| 20 | #define LEMON_LIST_GRAPH_H |
---|
| 21 | |
---|
| 22 | ///\ingroup graphs |
---|
| 23 | ///\file |
---|
[735] | 24 | ///\brief ListDigraph and ListGraph classes. |
---|
[57] | 25 | |
---|
[220] | 26 | #include <lemon/core.h> |
---|
| 27 | #include <lemon/error.h> |
---|
[57] | 28 | #include <lemon/bits/graph_extender.h> |
---|
| 29 | |
---|
| 30 | #include <vector> |
---|
| 31 | #include <list> |
---|
| 32 | |
---|
| 33 | namespace lemon { |
---|
| 34 | |
---|
[738] | 35 | class ListDigraph; |
---|
| 36 | |
---|
[57] | 37 | class ListDigraphBase { |
---|
| 38 | |
---|
| 39 | protected: |
---|
| 40 | struct NodeT { |
---|
| 41 | int first_in, first_out; |
---|
| 42 | int prev, next; |
---|
| 43 | }; |
---|
[209] | 44 | |
---|
[57] | 45 | struct ArcT { |
---|
| 46 | int target, source; |
---|
| 47 | int prev_in, prev_out; |
---|
| 48 | int next_in, next_out; |
---|
| 49 | }; |
---|
| 50 | |
---|
| 51 | std::vector<NodeT> nodes; |
---|
| 52 | |
---|
| 53 | int first_node; |
---|
| 54 | |
---|
| 55 | int first_free_node; |
---|
| 56 | |
---|
| 57 | std::vector<ArcT> arcs; |
---|
| 58 | |
---|
| 59 | int first_free_arc; |
---|
[209] | 60 | |
---|
[57] | 61 | public: |
---|
[209] | 62 | |
---|
[57] | 63 | typedef ListDigraphBase Digraph; |
---|
[209] | 64 | |
---|
[57] | 65 | class Node { |
---|
| 66 | friend class ListDigraphBase; |
---|
[738] | 67 | friend class ListDigraph; |
---|
[57] | 68 | protected: |
---|
| 69 | |
---|
| 70 | int id; |
---|
| 71 | explicit Node(int pid) { id = pid;} |
---|
| 72 | |
---|
| 73 | public: |
---|
| 74 | Node() {} |
---|
| 75 | Node (Invalid) { id = -1; } |
---|
| 76 | bool operator==(const Node& node) const {return id == node.id;} |
---|
| 77 | bool operator!=(const Node& node) const {return id != node.id;} |
---|
| 78 | bool operator<(const Node& node) const {return id < node.id;} |
---|
| 79 | }; |
---|
| 80 | |
---|
| 81 | class Arc { |
---|
| 82 | friend class ListDigraphBase; |
---|
[738] | 83 | friend class ListDigraph; |
---|
[57] | 84 | protected: |
---|
| 85 | |
---|
| 86 | int id; |
---|
| 87 | explicit Arc(int pid) { id = pid;} |
---|
| 88 | |
---|
| 89 | public: |
---|
| 90 | Arc() {} |
---|
| 91 | Arc (Invalid) { id = -1; } |
---|
| 92 | bool operator==(const Arc& arc) const {return id == arc.id;} |
---|
| 93 | bool operator!=(const Arc& arc) const {return id != arc.id;} |
---|
| 94 | bool operator<(const Arc& arc) const {return id < arc.id;} |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | ListDigraphBase() |
---|
| 100 | : nodes(), first_node(-1), |
---|
[209] | 101 | first_free_node(-1), arcs(), first_free_arc(-1) {} |
---|
[57] | 102 | |
---|
[209] | 103 | |
---|
| 104 | int maxNodeId() const { return nodes.size()-1; } |
---|
[57] | 105 | int maxArcId() const { return arcs.size()-1; } |
---|
| 106 | |
---|
| 107 | Node source(Arc e) const { return Node(arcs[e.id].source); } |
---|
| 108 | Node target(Arc e) const { return Node(arcs[e.id].target); } |
---|
| 109 | |
---|
| 110 | |
---|
[209] | 111 | void first(Node& node) const { |
---|
[57] | 112 | node.id = first_node; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void next(Node& node) const { |
---|
| 116 | node.id = nodes[node.id].next; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
[209] | 120 | void first(Arc& arc) const { |
---|
[57] | 121 | int n; |
---|
[209] | 122 | for(n = first_node; |
---|
| 123 | n!=-1 && nodes[n].first_in == -1; |
---|
| 124 | n = nodes[n].next) {} |
---|
[73] | 125 | arc.id = (n == -1) ? -1 : nodes[n].first_in; |
---|
[57] | 126 | } |
---|
| 127 | |
---|
| 128 | void next(Arc& arc) const { |
---|
| 129 | if (arcs[arc.id].next_in != -1) { |
---|
[209] | 130 | arc.id = arcs[arc.id].next_in; |
---|
[57] | 131 | } else { |
---|
[209] | 132 | int n; |
---|
| 133 | for(n = nodes[arcs[arc.id].target].next; |
---|
| 134 | n!=-1 && nodes[n].first_in == -1; |
---|
| 135 | n = nodes[n].next) {} |
---|
| 136 | arc.id = (n == -1) ? -1 : nodes[n].first_in; |
---|
| 137 | } |
---|
[57] | 138 | } |
---|
| 139 | |
---|
| 140 | void firstOut(Arc &e, const Node& v) const { |
---|
| 141 | e.id = nodes[v.id].first_out; |
---|
| 142 | } |
---|
| 143 | void nextOut(Arc &e) const { |
---|
| 144 | e.id=arcs[e.id].next_out; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void firstIn(Arc &e, const Node& v) const { |
---|
| 148 | e.id = nodes[v.id].first_in; |
---|
| 149 | } |
---|
| 150 | void nextIn(Arc &e) const { |
---|
| 151 | e.id=arcs[e.id].next_in; |
---|
| 152 | } |
---|
| 153 | |
---|
[209] | 154 | |
---|
[57] | 155 | static int id(Node v) { return v.id; } |
---|
| 156 | static int id(Arc e) { return e.id; } |
---|
| 157 | |
---|
| 158 | static Node nodeFromId(int id) { return Node(id);} |
---|
| 159 | static Arc arcFromId(int id) { return Arc(id);} |
---|
| 160 | |
---|
[209] | 161 | bool valid(Node n) const { |
---|
| 162 | return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
---|
| 163 | nodes[n.id].prev != -2; |
---|
[149] | 164 | } |
---|
| 165 | |
---|
[209] | 166 | bool valid(Arc a) const { |
---|
| 167 | return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
---|
| 168 | arcs[a.id].prev_in != -2; |
---|
[149] | 169 | } |
---|
| 170 | |
---|
[209] | 171 | Node addNode() { |
---|
[57] | 172 | int n; |
---|
[209] | 173 | |
---|
[57] | 174 | if(first_free_node==-1) { |
---|
[209] | 175 | n = nodes.size(); |
---|
| 176 | nodes.push_back(NodeT()); |
---|
[57] | 177 | } else { |
---|
[209] | 178 | n = first_free_node; |
---|
| 179 | first_free_node = nodes[n].next; |
---|
[57] | 180 | } |
---|
[209] | 181 | |
---|
[57] | 182 | nodes[n].next = first_node; |
---|
| 183 | if(first_node != -1) nodes[first_node].prev = n; |
---|
| 184 | first_node = n; |
---|
| 185 | nodes[n].prev = -1; |
---|
[209] | 186 | |
---|
[57] | 187 | nodes[n].first_in = nodes[n].first_out = -1; |
---|
[209] | 188 | |
---|
[57] | 189 | return Node(n); |
---|
| 190 | } |
---|
[209] | 191 | |
---|
[57] | 192 | Arc addArc(Node u, Node v) { |
---|
[209] | 193 | int n; |
---|
[57] | 194 | |
---|
| 195 | if (first_free_arc == -1) { |
---|
[209] | 196 | n = arcs.size(); |
---|
| 197 | arcs.push_back(ArcT()); |
---|
[57] | 198 | } else { |
---|
[209] | 199 | n = first_free_arc; |
---|
| 200 | first_free_arc = arcs[n].next_in; |
---|
[57] | 201 | } |
---|
[209] | 202 | |
---|
| 203 | arcs[n].source = u.id; |
---|
[57] | 204 | arcs[n].target = v.id; |
---|
| 205 | |
---|
| 206 | arcs[n].next_out = nodes[u.id].first_out; |
---|
| 207 | if(nodes[u.id].first_out != -1) { |
---|
[209] | 208 | arcs[nodes[u.id].first_out].prev_out = n; |
---|
[57] | 209 | } |
---|
[209] | 210 | |
---|
[57] | 211 | arcs[n].next_in = nodes[v.id].first_in; |
---|
| 212 | if(nodes[v.id].first_in != -1) { |
---|
[209] | 213 | arcs[nodes[v.id].first_in].prev_in = n; |
---|
[57] | 214 | } |
---|
[209] | 215 | |
---|
[57] | 216 | arcs[n].prev_in = arcs[n].prev_out = -1; |
---|
[209] | 217 | |
---|
[57] | 218 | nodes[u.id].first_out = nodes[v.id].first_in = n; |
---|
| 219 | |
---|
| 220 | return Arc(n); |
---|
| 221 | } |
---|
[209] | 222 | |
---|
[57] | 223 | void erase(const Node& node) { |
---|
| 224 | int n = node.id; |
---|
[209] | 225 | |
---|
[57] | 226 | if(nodes[n].next != -1) { |
---|
[209] | 227 | nodes[nodes[n].next].prev = nodes[n].prev; |
---|
[57] | 228 | } |
---|
[209] | 229 | |
---|
[57] | 230 | if(nodes[n].prev != -1) { |
---|
[209] | 231 | nodes[nodes[n].prev].next = nodes[n].next; |
---|
[57] | 232 | } else { |
---|
[209] | 233 | first_node = nodes[n].next; |
---|
[57] | 234 | } |
---|
[209] | 235 | |
---|
[57] | 236 | nodes[n].next = first_free_node; |
---|
| 237 | first_free_node = n; |
---|
[149] | 238 | nodes[n].prev = -2; |
---|
[57] | 239 | |
---|
| 240 | } |
---|
[209] | 241 | |
---|
[57] | 242 | void erase(const Arc& arc) { |
---|
| 243 | int n = arc.id; |
---|
[209] | 244 | |
---|
[57] | 245 | if(arcs[n].next_in!=-1) { |
---|
[209] | 246 | arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
---|
[57] | 247 | } |
---|
| 248 | |
---|
| 249 | if(arcs[n].prev_in!=-1) { |
---|
[209] | 250 | arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
---|
[57] | 251 | } else { |
---|
[209] | 252 | nodes[arcs[n].target].first_in = arcs[n].next_in; |
---|
[57] | 253 | } |
---|
| 254 | |
---|
[209] | 255 | |
---|
[57] | 256 | if(arcs[n].next_out!=-1) { |
---|
[209] | 257 | arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
---|
| 258 | } |
---|
[57] | 259 | |
---|
| 260 | if(arcs[n].prev_out!=-1) { |
---|
[209] | 261 | arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
---|
[57] | 262 | } else { |
---|
[209] | 263 | nodes[arcs[n].source].first_out = arcs[n].next_out; |
---|
[57] | 264 | } |
---|
[209] | 265 | |
---|
[57] | 266 | arcs[n].next_in = first_free_arc; |
---|
[149] | 267 | first_free_arc = n; |
---|
| 268 | arcs[n].prev_in = -2; |
---|
[57] | 269 | } |
---|
| 270 | |
---|
| 271 | void clear() { |
---|
| 272 | arcs.clear(); |
---|
| 273 | nodes.clear(); |
---|
| 274 | first_node = first_free_node = first_free_arc = -1; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | protected: |
---|
[209] | 278 | void changeTarget(Arc e, Node n) |
---|
[57] | 279 | { |
---|
| 280 | if(arcs[e.id].next_in != -1) |
---|
[209] | 281 | arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in; |
---|
[57] | 282 | if(arcs[e.id].prev_in != -1) |
---|
[209] | 283 | arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in; |
---|
[57] | 284 | else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in; |
---|
| 285 | if (nodes[n.id].first_in != -1) { |
---|
[209] | 286 | arcs[nodes[n.id].first_in].prev_in = e.id; |
---|
[57] | 287 | } |
---|
| 288 | arcs[e.id].target = n.id; |
---|
| 289 | arcs[e.id].prev_in = -1; |
---|
| 290 | arcs[e.id].next_in = nodes[n.id].first_in; |
---|
| 291 | nodes[n.id].first_in = e.id; |
---|
| 292 | } |
---|
[209] | 293 | void changeSource(Arc e, Node n) |
---|
[57] | 294 | { |
---|
| 295 | if(arcs[e.id].next_out != -1) |
---|
[209] | 296 | arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
---|
[57] | 297 | if(arcs[e.id].prev_out != -1) |
---|
[209] | 298 | arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
---|
[57] | 299 | else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
---|
| 300 | if (nodes[n.id].first_out != -1) { |
---|
[209] | 301 | arcs[nodes[n.id].first_out].prev_out = e.id; |
---|
[57] | 302 | } |
---|
| 303 | arcs[e.id].source = n.id; |
---|
| 304 | arcs[e.id].prev_out = -1; |
---|
| 305 | arcs[e.id].next_out = nodes[n.id].first_out; |
---|
| 306 | nodes[n.id].first_out = e.id; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | }; |
---|
| 310 | |
---|
| 311 | typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
---|
| 312 | |
---|
[73] | 313 | /// \addtogroup graphs |
---|
[57] | 314 | /// @{ |
---|
| 315 | |
---|
[209] | 316 | ///A general directed graph structure. |
---|
[57] | 317 | |
---|
[735] | 318 | ///\ref ListDigraph is a versatile and fast directed graph |
---|
| 319 | ///implementation based on linked lists that are stored in |
---|
[209] | 320 | ///\c std::vector structures. |
---|
[57] | 321 | /// |
---|
[735] | 322 | ///This type fully conforms to the \ref concepts::Digraph "Digraph concept" |
---|
| 323 | ///and it also provides several useful additional functionalities. |
---|
| 324 | ///Most of its member functions and nested classes are documented |
---|
[73] | 325 | ///only in the concept class. |
---|
[57] | 326 | /// |
---|
[73] | 327 | ///\sa concepts::Digraph |
---|
[735] | 328 | ///\sa ListGraph |
---|
[57] | 329 | class ListDigraph : public ExtendedListDigraphBase { |
---|
[617] | 330 | typedef ExtendedListDigraphBase Parent; |
---|
| 331 | |
---|
[57] | 332 | private: |
---|
[735] | 333 | /// Digraphs are \e not copy constructible. Use DigraphCopy instead. |
---|
[57] | 334 | ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {}; |
---|
[735] | 335 | /// \brief Assignment of a digraph to another one is \e not allowed. |
---|
| 336 | /// Use DigraphCopy instead. |
---|
[57] | 337 | void operator=(const ListDigraph &) {} |
---|
| 338 | public: |
---|
| 339 | |
---|
| 340 | /// Constructor |
---|
[209] | 341 | |
---|
[57] | 342 | /// Constructor. |
---|
| 343 | /// |
---|
| 344 | ListDigraph() {} |
---|
| 345 | |
---|
| 346 | ///Add a new node to the digraph. |
---|
[209] | 347 | |
---|
[735] | 348 | ///This function adds a new node to the digraph. |
---|
[559] | 349 | ///\return The new node. |
---|
[57] | 350 | Node addNode() { return Parent::addNode(); } |
---|
| 351 | |
---|
| 352 | ///Add a new arc to the digraph. |
---|
[209] | 353 | |
---|
[735] | 354 | ///This function adds a new arc to the digraph with source node \c s |
---|
[57] | 355 | ///and target node \c t. |
---|
[559] | 356 | ///\return The new arc. |
---|
[735] | 357 | Arc addArc(Node s, Node t) { |
---|
[209] | 358 | return Parent::addArc(s, t); |
---|
[57] | 359 | } |
---|
| 360 | |
---|
[234] | 361 | ///\brief Erase a node from the digraph. |
---|
| 362 | /// |
---|
[735] | 363 | ///This function erases the given node from the digraph. |
---|
| 364 | void erase(Node n) { Parent::erase(n); } |
---|
[234] | 365 | |
---|
| 366 | ///\brief Erase an arc from the digraph. |
---|
| 367 | /// |
---|
[735] | 368 | ///This function erases the given arc from the digraph. |
---|
| 369 | void erase(Arc a) { Parent::erase(a); } |
---|
[234] | 370 | |
---|
[149] | 371 | /// Node validity check |
---|
| 372 | |
---|
[735] | 373 | /// This function gives back \c true if the given node is valid, |
---|
| 374 | /// i.e. it is a real node of the digraph. |
---|
[149] | 375 | /// |
---|
[735] | 376 | /// \warning A removed node could become valid again if new nodes are |
---|
| 377 | /// added to the digraph. |
---|
[149] | 378 | bool valid(Node n) const { return Parent::valid(n); } |
---|
| 379 | |
---|
| 380 | /// Arc validity check |
---|
| 381 | |
---|
[735] | 382 | /// This function gives back \c true if the given arc is valid, |
---|
| 383 | /// i.e. it is a real arc of the digraph. |
---|
[149] | 384 | /// |
---|
[735] | 385 | /// \warning A removed arc could become valid again if new arcs are |
---|
| 386 | /// added to the digraph. |
---|
[149] | 387 | bool valid(Arc a) const { return Parent::valid(a); } |
---|
| 388 | |
---|
[735] | 389 | /// Change the target node of an arc |
---|
[57] | 390 | |
---|
[735] | 391 | /// This function changes the target node of the given arc \c a to \c n. |
---|
[57] | 392 | /// |
---|
[735] | 393 | ///\note \c ArcIt and \c OutArcIt iterators referencing the changed |
---|
| 394 | ///arc remain valid, however \c InArcIt iterators are invalidated. |
---|
[73] | 395 | /// |
---|
[57] | 396 | ///\warning This functionality cannot be used together with the Snapshot |
---|
| 397 | ///feature. |
---|
[235] | 398 | void changeTarget(Arc a, Node n) { |
---|
| 399 | Parent::changeTarget(a,n); |
---|
[57] | 400 | } |
---|
[735] | 401 | /// Change the source node of an arc |
---|
[57] | 402 | |
---|
[735] | 403 | /// This function changes the source node of the given arc \c a to \c n. |
---|
[57] | 404 | /// |
---|
[735] | 405 | ///\note \c InArcIt iterators referencing the changed arc remain |
---|
| 406 | ///valid, however \c ArcIt and \c OutArcIt iterators are invalidated. |
---|
[73] | 407 | /// |
---|
[57] | 408 | ///\warning This functionality cannot be used together with the Snapshot |
---|
| 409 | ///feature. |
---|
[235] | 410 | void changeSource(Arc a, Node n) { |
---|
| 411 | Parent::changeSource(a,n); |
---|
[57] | 412 | } |
---|
| 413 | |
---|
[735] | 414 | /// Reverse the direction of an arc. |
---|
[57] | 415 | |
---|
[735] | 416 | /// This function reverses the direction of the given arc. |
---|
| 417 | ///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing |
---|
| 418 | ///the changed arc are invalidated. |
---|
[73] | 419 | /// |
---|
[57] | 420 | ///\warning This functionality cannot be used together with the Snapshot |
---|
| 421 | ///feature. |
---|
[735] | 422 | void reverseArc(Arc a) { |
---|
| 423 | Node t=target(a); |
---|
| 424 | changeTarget(a,source(a)); |
---|
| 425 | changeSource(a,t); |
---|
[57] | 426 | } |
---|
| 427 | |
---|
| 428 | ///Contract two nodes. |
---|
| 429 | |
---|
[735] | 430 | ///This function contracts the given two nodes. |
---|
| 431 | ///Node \c v is removed, but instead of deleting its |
---|
| 432 | ///incident arcs, they are joined to node \c u. |
---|
| 433 | ///If the last parameter \c r is \c true (this is the default value), |
---|
| 434 | ///then the newly created loops are removed. |
---|
[57] | 435 | /// |
---|
[735] | 436 | ///\note The moved arcs are joined to node \c u using changeSource() |
---|
| 437 | ///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are |
---|
| 438 | ///invalidated for the outgoing arcs of node \c v and \c InArcIt |
---|
| 439 | ///iterators are invalidated for the incomming arcs of \c v. |
---|
| 440 | ///Moreover all iterators referencing node \c v or the removed |
---|
| 441 | ///loops are also invalidated. Other iterators remain valid. |
---|
[73] | 442 | /// |
---|
[57] | 443 | ///\warning This functionality cannot be used together with the Snapshot |
---|
| 444 | ///feature. |
---|
[735] | 445 | void contract(Node u, Node v, bool r = true) |
---|
[57] | 446 | { |
---|
[735] | 447 | for(OutArcIt e(*this,v);e!=INVALID;) { |
---|
[209] | 448 | OutArcIt f=e; |
---|
| 449 | ++f; |
---|
[735] | 450 | if(r && target(e)==u) erase(e); |
---|
| 451 | else changeSource(e,u); |
---|
[209] | 452 | e=f; |
---|
[57] | 453 | } |
---|
[735] | 454 | for(InArcIt e(*this,v);e!=INVALID;) { |
---|
[209] | 455 | InArcIt f=e; |
---|
| 456 | ++f; |
---|
[735] | 457 | if(r && source(e)==u) erase(e); |
---|
| 458 | else changeTarget(e,u); |
---|
[209] | 459 | e=f; |
---|
[57] | 460 | } |
---|
[735] | 461 | erase(v); |
---|
[57] | 462 | } |
---|
| 463 | |
---|
| 464 | ///Split a node. |
---|
| 465 | |
---|
[735] | 466 | ///This function splits the given node. First, a new node is added |
---|
| 467 | ///to the digraph, then the source of each outgoing arc of node \c n |
---|
| 468 | ///is moved to this new node. |
---|
| 469 | ///If the second parameter \c connect is \c true (this is the default |
---|
| 470 | ///value), then a new arc from node \c n to the newly created node |
---|
| 471 | ///is also added. |
---|
[57] | 472 | ///\return The newly created node. |
---|
| 473 | /// |
---|
[738] | 474 | ///\note All iterators remain valid. |
---|
[57] | 475 | /// |
---|
[735] | 476 | ///\warning This functionality cannot be used together with the |
---|
[73] | 477 | ///Snapshot feature. |
---|
[57] | 478 | Node split(Node n, bool connect = true) { |
---|
| 479 | Node b = addNode(); |
---|
[738] | 480 | nodes[b.id].first_out=nodes[n.id].first_out; |
---|
| 481 | nodes[n.id].first_out=-1; |
---|
| 482 | for(int i=nodes[b.id].first_out; i!=-1; i=arcs[i].next_out) { |
---|
| 483 | arcs[i].source=b.id; |
---|
[57] | 484 | } |
---|
| 485 | if (connect) addArc(n,b); |
---|
| 486 | return b; |
---|
| 487 | } |
---|
[209] | 488 | |
---|
[57] | 489 | ///Split an arc. |
---|
| 490 | |
---|
[735] | 491 | ///This function splits the given arc. First, a new node \c v is |
---|
| 492 | ///added to the digraph, then the target node of the original arc |
---|
| 493 | ///is set to \c v. Finally, an arc from \c v to the original target |
---|
| 494 | ///is added. |
---|
| 495 | ///\return The newly created node. |
---|
[73] | 496 | /// |
---|
[735] | 497 | ///\note \c InArcIt iterators referencing the original arc are |
---|
| 498 | ///invalidated. Other iterators remain valid. |
---|
[73] | 499 | /// |
---|
| 500 | ///\warning This functionality cannot be used together with the |
---|
| 501 | ///Snapshot feature. |
---|
[735] | 502 | Node split(Arc a) { |
---|
| 503 | Node v = addNode(); |
---|
| 504 | addArc(v,target(a)); |
---|
| 505 | changeTarget(a,v); |
---|
| 506 | return v; |
---|
[57] | 507 | } |
---|
[209] | 508 | |
---|
[735] | 509 | ///Clear the digraph. |
---|
| 510 | |
---|
| 511 | ///This function erases all nodes and arcs from the digraph. |
---|
| 512 | /// |
---|
| 513 | void clear() { |
---|
| 514 | Parent::clear(); |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | /// Reserve memory for nodes. |
---|
| 518 | |
---|
| 519 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 520 | /// allocation: if you know that the digraph you want to build will |
---|
| 521 | /// be large (e.g. it will contain millions of nodes and/or arcs), |
---|
| 522 | /// then it is worth reserving space for this amount before starting |
---|
| 523 | /// to build the digraph. |
---|
| 524 | /// \sa reserveArc() |
---|
| 525 | void reserveNode(int n) { nodes.reserve(n); }; |
---|
| 526 | |
---|
| 527 | /// Reserve memory for arcs. |
---|
| 528 | |
---|
| 529 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 530 | /// allocation: if you know that the digraph you want to build will |
---|
| 531 | /// be large (e.g. it will contain millions of nodes and/or arcs), |
---|
| 532 | /// then it is worth reserving space for this amount before starting |
---|
| 533 | /// to build the digraph. |
---|
| 534 | /// \sa reserveNode() |
---|
| 535 | void reserveArc(int m) { arcs.reserve(m); }; |
---|
| 536 | |
---|
[57] | 537 | /// \brief Class to make a snapshot of the digraph and restore |
---|
[73] | 538 | /// it later. |
---|
[57] | 539 | /// |
---|
[73] | 540 | /// Class to make a snapshot of the digraph and restore it later. |
---|
[57] | 541 | /// |
---|
| 542 | /// The newly added nodes and arcs can be removed using the |
---|
| 543 | /// restore() function. |
---|
| 544 | /// |
---|
[735] | 545 | /// \note After a state is restored, you cannot restore a later state, |
---|
| 546 | /// i.e. you cannot add the removed nodes and arcs again using |
---|
| 547 | /// another Snapshot instance. |
---|
| 548 | /// |
---|
| 549 | /// \warning Node and arc deletions and other modifications (e.g. |
---|
| 550 | /// reversing, contracting, splitting arcs or nodes) cannot be |
---|
[209] | 551 | /// restored. These events invalidate the snapshot. |
---|
[735] | 552 | /// However the arcs and nodes that were added to the digraph after |
---|
| 553 | /// making the current snapshot can be removed without invalidating it. |
---|
[57] | 554 | class Snapshot { |
---|
| 555 | protected: |
---|
| 556 | |
---|
| 557 | typedef Parent::NodeNotifier NodeNotifier; |
---|
| 558 | |
---|
| 559 | class NodeObserverProxy : public NodeNotifier::ObserverBase { |
---|
| 560 | public: |
---|
| 561 | |
---|
| 562 | NodeObserverProxy(Snapshot& _snapshot) |
---|
| 563 | : snapshot(_snapshot) {} |
---|
| 564 | |
---|
| 565 | using NodeNotifier::ObserverBase::attach; |
---|
| 566 | using NodeNotifier::ObserverBase::detach; |
---|
| 567 | using NodeNotifier::ObserverBase::attached; |
---|
[209] | 568 | |
---|
[57] | 569 | protected: |
---|
[209] | 570 | |
---|
[57] | 571 | virtual void add(const Node& node) { |
---|
| 572 | snapshot.addNode(node); |
---|
| 573 | } |
---|
| 574 | virtual void add(const std::vector<Node>& nodes) { |
---|
| 575 | for (int i = nodes.size() - 1; i >= 0; ++i) { |
---|
| 576 | snapshot.addNode(nodes[i]); |
---|
| 577 | } |
---|
| 578 | } |
---|
| 579 | virtual void erase(const Node& node) { |
---|
| 580 | snapshot.eraseNode(node); |
---|
| 581 | } |
---|
| 582 | virtual void erase(const std::vector<Node>& nodes) { |
---|
| 583 | for (int i = 0; i < int(nodes.size()); ++i) { |
---|
| 584 | snapshot.eraseNode(nodes[i]); |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | virtual void build() { |
---|
| 588 | Node node; |
---|
| 589 | std::vector<Node> nodes; |
---|
[209] | 590 | for (notifier()->first(node); node != INVALID; |
---|
[57] | 591 | notifier()->next(node)) { |
---|
| 592 | nodes.push_back(node); |
---|
| 593 | } |
---|
| 594 | for (int i = nodes.size() - 1; i >= 0; --i) { |
---|
| 595 | snapshot.addNode(nodes[i]); |
---|
| 596 | } |
---|
| 597 | } |
---|
| 598 | virtual void clear() { |
---|
| 599 | Node node; |
---|
[209] | 600 | for (notifier()->first(node); node != INVALID; |
---|
[57] | 601 | notifier()->next(node)) { |
---|
| 602 | snapshot.eraseNode(node); |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | Snapshot& snapshot; |
---|
| 607 | }; |
---|
| 608 | |
---|
| 609 | class ArcObserverProxy : public ArcNotifier::ObserverBase { |
---|
| 610 | public: |
---|
| 611 | |
---|
| 612 | ArcObserverProxy(Snapshot& _snapshot) |
---|
| 613 | : snapshot(_snapshot) {} |
---|
| 614 | |
---|
| 615 | using ArcNotifier::ObserverBase::attach; |
---|
| 616 | using ArcNotifier::ObserverBase::detach; |
---|
| 617 | using ArcNotifier::ObserverBase::attached; |
---|
[209] | 618 | |
---|
[57] | 619 | protected: |
---|
| 620 | |
---|
| 621 | virtual void add(const Arc& arc) { |
---|
| 622 | snapshot.addArc(arc); |
---|
| 623 | } |
---|
| 624 | virtual void add(const std::vector<Arc>& arcs) { |
---|
| 625 | for (int i = arcs.size() - 1; i >= 0; ++i) { |
---|
| 626 | snapshot.addArc(arcs[i]); |
---|
| 627 | } |
---|
| 628 | } |
---|
| 629 | virtual void erase(const Arc& arc) { |
---|
| 630 | snapshot.eraseArc(arc); |
---|
| 631 | } |
---|
| 632 | virtual void erase(const std::vector<Arc>& arcs) { |
---|
| 633 | for (int i = 0; i < int(arcs.size()); ++i) { |
---|
| 634 | snapshot.eraseArc(arcs[i]); |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | virtual void build() { |
---|
| 638 | Arc arc; |
---|
| 639 | std::vector<Arc> arcs; |
---|
[209] | 640 | for (notifier()->first(arc); arc != INVALID; |
---|
[57] | 641 | notifier()->next(arc)) { |
---|
| 642 | arcs.push_back(arc); |
---|
| 643 | } |
---|
| 644 | for (int i = arcs.size() - 1; i >= 0; --i) { |
---|
| 645 | snapshot.addArc(arcs[i]); |
---|
| 646 | } |
---|
| 647 | } |
---|
| 648 | virtual void clear() { |
---|
| 649 | Arc arc; |
---|
[209] | 650 | for (notifier()->first(arc); arc != INVALID; |
---|
[57] | 651 | notifier()->next(arc)) { |
---|
| 652 | snapshot.eraseArc(arc); |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | Snapshot& snapshot; |
---|
| 657 | }; |
---|
[209] | 658 | |
---|
[57] | 659 | ListDigraph *digraph; |
---|
| 660 | |
---|
| 661 | NodeObserverProxy node_observer_proxy; |
---|
| 662 | ArcObserverProxy arc_observer_proxy; |
---|
| 663 | |
---|
| 664 | std::list<Node> added_nodes; |
---|
| 665 | std::list<Arc> added_arcs; |
---|
| 666 | |
---|
| 667 | |
---|
| 668 | void addNode(const Node& node) { |
---|
[209] | 669 | added_nodes.push_front(node); |
---|
[57] | 670 | } |
---|
| 671 | void eraseNode(const Node& node) { |
---|
[209] | 672 | std::list<Node>::iterator it = |
---|
[57] | 673 | std::find(added_nodes.begin(), added_nodes.end(), node); |
---|
| 674 | if (it == added_nodes.end()) { |
---|
| 675 | clear(); |
---|
| 676 | arc_observer_proxy.detach(); |
---|
| 677 | throw NodeNotifier::ImmediateDetach(); |
---|
| 678 | } else { |
---|
| 679 | added_nodes.erase(it); |
---|
| 680 | } |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | void addArc(const Arc& arc) { |
---|
[209] | 684 | added_arcs.push_front(arc); |
---|
[57] | 685 | } |
---|
| 686 | void eraseArc(const Arc& arc) { |
---|
[209] | 687 | std::list<Arc>::iterator it = |
---|
[57] | 688 | std::find(added_arcs.begin(), added_arcs.end(), arc); |
---|
| 689 | if (it == added_arcs.end()) { |
---|
| 690 | clear(); |
---|
[209] | 691 | node_observer_proxy.detach(); |
---|
[57] | 692 | throw ArcNotifier::ImmediateDetach(); |
---|
| 693 | } else { |
---|
| 694 | added_arcs.erase(it); |
---|
[209] | 695 | } |
---|
[57] | 696 | } |
---|
| 697 | |
---|
| 698 | void attach(ListDigraph &_digraph) { |
---|
[209] | 699 | digraph = &_digraph; |
---|
| 700 | node_observer_proxy.attach(digraph->notifier(Node())); |
---|
[57] | 701 | arc_observer_proxy.attach(digraph->notifier(Arc())); |
---|
| 702 | } |
---|
[209] | 703 | |
---|
[57] | 704 | void detach() { |
---|
[209] | 705 | node_observer_proxy.detach(); |
---|
| 706 | arc_observer_proxy.detach(); |
---|
[57] | 707 | } |
---|
| 708 | |
---|
| 709 | bool attached() const { |
---|
| 710 | return node_observer_proxy.attached(); |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | void clear() { |
---|
| 714 | added_nodes.clear(); |
---|
[209] | 715 | added_arcs.clear(); |
---|
[57] | 716 | } |
---|
| 717 | |
---|
| 718 | public: |
---|
| 719 | |
---|
| 720 | /// \brief Default constructor. |
---|
| 721 | /// |
---|
| 722 | /// Default constructor. |
---|
[735] | 723 | /// You have to call save() to actually make a snapshot. |
---|
[209] | 724 | Snapshot() |
---|
| 725 | : digraph(0), node_observer_proxy(*this), |
---|
[57] | 726 | arc_observer_proxy(*this) {} |
---|
[209] | 727 | |
---|
[57] | 728 | /// \brief Constructor that immediately makes a snapshot. |
---|
[209] | 729 | /// |
---|
[735] | 730 | /// This constructor immediately makes a snapshot of the given digraph. |
---|
| 731 | Snapshot(ListDigraph &gr) |
---|
[209] | 732 | : node_observer_proxy(*this), |
---|
[57] | 733 | arc_observer_proxy(*this) { |
---|
[735] | 734 | attach(gr); |
---|
[57] | 735 | } |
---|
[209] | 736 | |
---|
[57] | 737 | /// \brief Make a snapshot. |
---|
| 738 | /// |
---|
[735] | 739 | /// This function makes a snapshot of the given digraph. |
---|
| 740 | /// It can be called more than once. In case of a repeated |
---|
[57] | 741 | /// call, the previous snapshot gets lost. |
---|
[735] | 742 | void save(ListDigraph &gr) { |
---|
[57] | 743 | if (attached()) { |
---|
| 744 | detach(); |
---|
| 745 | clear(); |
---|
| 746 | } |
---|
[735] | 747 | attach(gr); |
---|
[57] | 748 | } |
---|
[209] | 749 | |
---|
[57] | 750 | /// \brief Undo the changes until the last snapshot. |
---|
[735] | 751 | /// |
---|
| 752 | /// This function undos the changes until the last snapshot |
---|
| 753 | /// created by save() or Snapshot(ListDigraph&). |
---|
[57] | 754 | void restore() { |
---|
[209] | 755 | detach(); |
---|
| 756 | for(std::list<Arc>::iterator it = added_arcs.begin(); |
---|
[57] | 757 | it != added_arcs.end(); ++it) { |
---|
[209] | 758 | digraph->erase(*it); |
---|
| 759 | } |
---|
| 760 | for(std::list<Node>::iterator it = added_nodes.begin(); |
---|
[57] | 761 | it != added_nodes.end(); ++it) { |
---|
[209] | 762 | digraph->erase(*it); |
---|
| 763 | } |
---|
[57] | 764 | clear(); |
---|
| 765 | } |
---|
| 766 | |
---|
[735] | 767 | /// \brief Returns \c true if the snapshot is valid. |
---|
[57] | 768 | /// |
---|
[735] | 769 | /// This function returns \c true if the snapshot is valid. |
---|
[57] | 770 | bool valid() const { |
---|
| 771 | return attached(); |
---|
| 772 | } |
---|
| 773 | }; |
---|
[209] | 774 | |
---|
[57] | 775 | }; |
---|
| 776 | |
---|
| 777 | ///@} |
---|
| 778 | |
---|
| 779 | class ListGraphBase { |
---|
| 780 | |
---|
| 781 | protected: |
---|
| 782 | |
---|
| 783 | struct NodeT { |
---|
| 784 | int first_out; |
---|
| 785 | int prev, next; |
---|
| 786 | }; |
---|
[209] | 787 | |
---|
[57] | 788 | struct ArcT { |
---|
| 789 | int target; |
---|
| 790 | int prev_out, next_out; |
---|
| 791 | }; |
---|
| 792 | |
---|
| 793 | std::vector<NodeT> nodes; |
---|
| 794 | |
---|
| 795 | int first_node; |
---|
| 796 | |
---|
| 797 | int first_free_node; |
---|
| 798 | |
---|
| 799 | std::vector<ArcT> arcs; |
---|
| 800 | |
---|
| 801 | int first_free_arc; |
---|
[209] | 802 | |
---|
[57] | 803 | public: |
---|
[209] | 804 | |
---|
[617] | 805 | typedef ListGraphBase Graph; |
---|
[57] | 806 | |
---|
| 807 | class Node { |
---|
| 808 | friend class ListGraphBase; |
---|
| 809 | protected: |
---|
| 810 | |
---|
| 811 | int id; |
---|
| 812 | explicit Node(int pid) { id = pid;} |
---|
| 813 | |
---|
| 814 | public: |
---|
| 815 | Node() {} |
---|
| 816 | Node (Invalid) { id = -1; } |
---|
| 817 | bool operator==(const Node& node) const {return id == node.id;} |
---|
| 818 | bool operator!=(const Node& node) const {return id != node.id;} |
---|
| 819 | bool operator<(const Node& node) const {return id < node.id;} |
---|
| 820 | }; |
---|
| 821 | |
---|
| 822 | class Edge { |
---|
| 823 | friend class ListGraphBase; |
---|
| 824 | protected: |
---|
| 825 | |
---|
| 826 | int id; |
---|
| 827 | explicit Edge(int pid) { id = pid;} |
---|
| 828 | |
---|
| 829 | public: |
---|
| 830 | Edge() {} |
---|
| 831 | Edge (Invalid) { id = -1; } |
---|
[73] | 832 | bool operator==(const Edge& edge) const {return id == edge.id;} |
---|
| 833 | bool operator!=(const Edge& edge) const {return id != edge.id;} |
---|
| 834 | bool operator<(const Edge& edge) const {return id < edge.id;} |
---|
[57] | 835 | }; |
---|
| 836 | |
---|
| 837 | class Arc { |
---|
| 838 | friend class ListGraphBase; |
---|
| 839 | protected: |
---|
| 840 | |
---|
| 841 | int id; |
---|
| 842 | explicit Arc(int pid) { id = pid;} |
---|
| 843 | |
---|
| 844 | public: |
---|
[329] | 845 | operator Edge() const { |
---|
| 846 | return id != -1 ? edgeFromId(id / 2) : INVALID; |
---|
[238] | 847 | } |
---|
[57] | 848 | |
---|
| 849 | Arc() {} |
---|
| 850 | Arc (Invalid) { id = -1; } |
---|
| 851 | bool operator==(const Arc& arc) const {return id == arc.id;} |
---|
| 852 | bool operator!=(const Arc& arc) const {return id != arc.id;} |
---|
| 853 | bool operator<(const Arc& arc) const {return id < arc.id;} |
---|
| 854 | }; |
---|
| 855 | |
---|
| 856 | ListGraphBase() |
---|
| 857 | : nodes(), first_node(-1), |
---|
[209] | 858 | first_free_node(-1), arcs(), first_free_arc(-1) {} |
---|
[57] | 859 | |
---|
[209] | 860 | |
---|
| 861 | int maxNodeId() const { return nodes.size()-1; } |
---|
[57] | 862 | int maxEdgeId() const { return arcs.size() / 2 - 1; } |
---|
| 863 | int maxArcId() const { return arcs.size()-1; } |
---|
| 864 | |
---|
| 865 | Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); } |
---|
| 866 | Node target(Arc e) const { return Node(arcs[e.id].target); } |
---|
| 867 | |
---|
| 868 | Node u(Edge e) const { return Node(arcs[2 * e.id].target); } |
---|
| 869 | Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); } |
---|
| 870 | |
---|
| 871 | static bool direction(Arc e) { |
---|
| 872 | return (e.id & 1) == 1; |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | static Arc direct(Edge e, bool d) { |
---|
| 876 | return Arc(e.id * 2 + (d ? 1 : 0)); |
---|
| 877 | } |
---|
| 878 | |
---|
[209] | 879 | void first(Node& node) const { |
---|
[57] | 880 | node.id = first_node; |
---|
| 881 | } |
---|
| 882 | |
---|
| 883 | void next(Node& node) const { |
---|
| 884 | node.id = nodes[node.id].next; |
---|
| 885 | } |
---|
| 886 | |
---|
[209] | 887 | void first(Arc& e) const { |
---|
[57] | 888 | int n = first_node; |
---|
| 889 | while (n != -1 && nodes[n].first_out == -1) { |
---|
| 890 | n = nodes[n].next; |
---|
| 891 | } |
---|
| 892 | e.id = (n == -1) ? -1 : nodes[n].first_out; |
---|
| 893 | } |
---|
| 894 | |
---|
| 895 | void next(Arc& e) const { |
---|
| 896 | if (arcs[e.id].next_out != -1) { |
---|
[209] | 897 | e.id = arcs[e.id].next_out; |
---|
[57] | 898 | } else { |
---|
[209] | 899 | int n = nodes[arcs[e.id ^ 1].target].next; |
---|
[57] | 900 | while(n != -1 && nodes[n].first_out == -1) { |
---|
| 901 | n = nodes[n].next; |
---|
| 902 | } |
---|
[209] | 903 | e.id = (n == -1) ? -1 : nodes[n].first_out; |
---|
| 904 | } |
---|
[57] | 905 | } |
---|
| 906 | |
---|
[209] | 907 | void first(Edge& e) const { |
---|
[57] | 908 | int n = first_node; |
---|
| 909 | while (n != -1) { |
---|
| 910 | e.id = nodes[n].first_out; |
---|
| 911 | while ((e.id & 1) != 1) { |
---|
| 912 | e.id = arcs[e.id].next_out; |
---|
| 913 | } |
---|
| 914 | if (e.id != -1) { |
---|
| 915 | e.id /= 2; |
---|
| 916 | return; |
---|
[209] | 917 | } |
---|
[57] | 918 | n = nodes[n].next; |
---|
| 919 | } |
---|
| 920 | e.id = -1; |
---|
| 921 | } |
---|
| 922 | |
---|
| 923 | void next(Edge& e) const { |
---|
| 924 | int n = arcs[e.id * 2].target; |
---|
| 925 | e.id = arcs[(e.id * 2) | 1].next_out; |
---|
| 926 | while ((e.id & 1) != 1) { |
---|
| 927 | e.id = arcs[e.id].next_out; |
---|
| 928 | } |
---|
| 929 | if (e.id != -1) { |
---|
| 930 | e.id /= 2; |
---|
| 931 | return; |
---|
[209] | 932 | } |
---|
[57] | 933 | n = nodes[n].next; |
---|
| 934 | while (n != -1) { |
---|
| 935 | e.id = nodes[n].first_out; |
---|
| 936 | while ((e.id & 1) != 1) { |
---|
| 937 | e.id = arcs[e.id].next_out; |
---|
| 938 | } |
---|
| 939 | if (e.id != -1) { |
---|
| 940 | e.id /= 2; |
---|
| 941 | return; |
---|
[209] | 942 | } |
---|
[57] | 943 | n = nodes[n].next; |
---|
| 944 | } |
---|
| 945 | e.id = -1; |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | void firstOut(Arc &e, const Node& v) const { |
---|
| 949 | e.id = nodes[v.id].first_out; |
---|
| 950 | } |
---|
| 951 | void nextOut(Arc &e) const { |
---|
| 952 | e.id = arcs[e.id].next_out; |
---|
| 953 | } |
---|
| 954 | |
---|
| 955 | void firstIn(Arc &e, const Node& v) const { |
---|
| 956 | e.id = ((nodes[v.id].first_out) ^ 1); |
---|
| 957 | if (e.id == -2) e.id = -1; |
---|
| 958 | } |
---|
| 959 | void nextIn(Arc &e) const { |
---|
| 960 | e.id = ((arcs[e.id ^ 1].next_out) ^ 1); |
---|
| 961 | if (e.id == -2) e.id = -1; |
---|
| 962 | } |
---|
| 963 | |
---|
| 964 | void firstInc(Edge &e, bool& d, const Node& v) const { |
---|
[73] | 965 | int a = nodes[v.id].first_out; |
---|
| 966 | if (a != -1 ) { |
---|
| 967 | e.id = a / 2; |
---|
| 968 | d = ((a & 1) == 1); |
---|
[57] | 969 | } else { |
---|
| 970 | e.id = -1; |
---|
| 971 | d = true; |
---|
| 972 | } |
---|
| 973 | } |
---|
| 974 | void nextInc(Edge &e, bool& d) const { |
---|
[73] | 975 | int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out); |
---|
| 976 | if (a != -1 ) { |
---|
| 977 | e.id = a / 2; |
---|
| 978 | d = ((a & 1) == 1); |
---|
[57] | 979 | } else { |
---|
| 980 | e.id = -1; |
---|
| 981 | d = true; |
---|
| 982 | } |
---|
| 983 | } |
---|
[209] | 984 | |
---|
[57] | 985 | static int id(Node v) { return v.id; } |
---|
| 986 | static int id(Arc e) { return e.id; } |
---|
| 987 | static int id(Edge e) { return e.id; } |
---|
| 988 | |
---|
| 989 | static Node nodeFromId(int id) { return Node(id);} |
---|
| 990 | static Arc arcFromId(int id) { return Arc(id);} |
---|
| 991 | static Edge edgeFromId(int id) { return Edge(id);} |
---|
| 992 | |
---|
[209] | 993 | bool valid(Node n) const { |
---|
| 994 | return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
---|
| 995 | nodes[n.id].prev != -2; |
---|
[149] | 996 | } |
---|
| 997 | |
---|
[209] | 998 | bool valid(Arc a) const { |
---|
| 999 | return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
---|
| 1000 | arcs[a.id].prev_out != -2; |
---|
[149] | 1001 | } |
---|
| 1002 | |
---|
[209] | 1003 | bool valid(Edge e) const { |
---|
| 1004 | return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) && |
---|
| 1005 | arcs[2 * e.id].prev_out != -2; |
---|
[149] | 1006 | } |
---|
| 1007 | |
---|
[209] | 1008 | Node addNode() { |
---|
[57] | 1009 | int n; |
---|
[209] | 1010 | |
---|
[57] | 1011 | if(first_free_node==-1) { |
---|
[209] | 1012 | n = nodes.size(); |
---|
| 1013 | nodes.push_back(NodeT()); |
---|
[57] | 1014 | } else { |
---|
[209] | 1015 | n = first_free_node; |
---|
| 1016 | first_free_node = nodes[n].next; |
---|
[57] | 1017 | } |
---|
[209] | 1018 | |
---|
[57] | 1019 | nodes[n].next = first_node; |
---|
| 1020 | if (first_node != -1) nodes[first_node].prev = n; |
---|
| 1021 | first_node = n; |
---|
| 1022 | nodes[n].prev = -1; |
---|
[209] | 1023 | |
---|
[57] | 1024 | nodes[n].first_out = -1; |
---|
[209] | 1025 | |
---|
[57] | 1026 | return Node(n); |
---|
| 1027 | } |
---|
[209] | 1028 | |
---|
[57] | 1029 | Edge addEdge(Node u, Node v) { |
---|
[209] | 1030 | int n; |
---|
[57] | 1031 | |
---|
| 1032 | if (first_free_arc == -1) { |
---|
[209] | 1033 | n = arcs.size(); |
---|
| 1034 | arcs.push_back(ArcT()); |
---|
| 1035 | arcs.push_back(ArcT()); |
---|
[57] | 1036 | } else { |
---|
[209] | 1037 | n = first_free_arc; |
---|
| 1038 | first_free_arc = arcs[n].next_out; |
---|
[57] | 1039 | } |
---|
[209] | 1040 | |
---|
[57] | 1041 | arcs[n].target = u.id; |
---|
| 1042 | arcs[n | 1].target = v.id; |
---|
| 1043 | |
---|
| 1044 | arcs[n].next_out = nodes[v.id].first_out; |
---|
| 1045 | if (nodes[v.id].first_out != -1) { |
---|
[209] | 1046 | arcs[nodes[v.id].first_out].prev_out = n; |
---|
| 1047 | } |
---|
[57] | 1048 | arcs[n].prev_out = -1; |
---|
| 1049 | nodes[v.id].first_out = n; |
---|
[209] | 1050 | |
---|
[57] | 1051 | arcs[n | 1].next_out = nodes[u.id].first_out; |
---|
| 1052 | if (nodes[u.id].first_out != -1) { |
---|
[209] | 1053 | arcs[nodes[u.id].first_out].prev_out = (n | 1); |
---|
[57] | 1054 | } |
---|
[209] | 1055 | arcs[n | 1].prev_out = -1; |
---|
[57] | 1056 | nodes[u.id].first_out = (n | 1); |
---|
| 1057 | |
---|
| 1058 | return Edge(n / 2); |
---|
| 1059 | } |
---|
[209] | 1060 | |
---|
[57] | 1061 | void erase(const Node& node) { |
---|
| 1062 | int n = node.id; |
---|
[209] | 1063 | |
---|
[57] | 1064 | if(nodes[n].next != -1) { |
---|
[209] | 1065 | nodes[nodes[n].next].prev = nodes[n].prev; |
---|
[57] | 1066 | } |
---|
[209] | 1067 | |
---|
[57] | 1068 | if(nodes[n].prev != -1) { |
---|
[209] | 1069 | nodes[nodes[n].prev].next = nodes[n].next; |
---|
[57] | 1070 | } else { |
---|
[209] | 1071 | first_node = nodes[n].next; |
---|
[57] | 1072 | } |
---|
[209] | 1073 | |
---|
[57] | 1074 | nodes[n].next = first_free_node; |
---|
| 1075 | first_free_node = n; |
---|
[149] | 1076 | nodes[n].prev = -2; |
---|
[57] | 1077 | } |
---|
[209] | 1078 | |
---|
[73] | 1079 | void erase(const Edge& edge) { |
---|
| 1080 | int n = edge.id * 2; |
---|
[209] | 1081 | |
---|
[57] | 1082 | if (arcs[n].next_out != -1) { |
---|
[209] | 1083 | arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
---|
| 1084 | } |
---|
[57] | 1085 | |
---|
| 1086 | if (arcs[n].prev_out != -1) { |
---|
[209] | 1087 | arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
---|
[57] | 1088 | } else { |
---|
[209] | 1089 | nodes[arcs[n | 1].target].first_out = arcs[n].next_out; |
---|
[57] | 1090 | } |
---|
| 1091 | |
---|
| 1092 | if (arcs[n | 1].next_out != -1) { |
---|
[209] | 1093 | arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
---|
| 1094 | } |
---|
[57] | 1095 | |
---|
| 1096 | if (arcs[n | 1].prev_out != -1) { |
---|
[209] | 1097 | arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
---|
[57] | 1098 | } else { |
---|
[209] | 1099 | nodes[arcs[n].target].first_out = arcs[n | 1].next_out; |
---|
[57] | 1100 | } |
---|
[209] | 1101 | |
---|
[57] | 1102 | arcs[n].next_out = first_free_arc; |
---|
[209] | 1103 | first_free_arc = n; |
---|
[149] | 1104 | arcs[n].prev_out = -2; |
---|
| 1105 | arcs[n | 1].prev_out = -2; |
---|
[57] | 1106 | |
---|
| 1107 | } |
---|
| 1108 | |
---|
| 1109 | void clear() { |
---|
| 1110 | arcs.clear(); |
---|
| 1111 | nodes.clear(); |
---|
| 1112 | first_node = first_free_node = first_free_arc = -1; |
---|
| 1113 | } |
---|
| 1114 | |
---|
| 1115 | protected: |
---|
| 1116 | |
---|
[235] | 1117 | void changeV(Edge e, Node n) { |
---|
[57] | 1118 | if(arcs[2 * e.id].next_out != -1) { |
---|
[209] | 1119 | arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out; |
---|
[57] | 1120 | } |
---|
| 1121 | if(arcs[2 * e.id].prev_out != -1) { |
---|
[209] | 1122 | arcs[arcs[2 * e.id].prev_out].next_out = |
---|
[57] | 1123 | arcs[2 * e.id].next_out; |
---|
| 1124 | } else { |
---|
[209] | 1125 | nodes[arcs[(2 * e.id) | 1].target].first_out = |
---|
[57] | 1126 | arcs[2 * e.id].next_out; |
---|
| 1127 | } |
---|
| 1128 | |
---|
| 1129 | if (nodes[n.id].first_out != -1) { |
---|
[209] | 1130 | arcs[nodes[n.id].first_out].prev_out = 2 * e.id; |
---|
[57] | 1131 | } |
---|
| 1132 | arcs[(2 * e.id) | 1].target = n.id; |
---|
| 1133 | arcs[2 * e.id].prev_out = -1; |
---|
| 1134 | arcs[2 * e.id].next_out = nodes[n.id].first_out; |
---|
| 1135 | nodes[n.id].first_out = 2 * e.id; |
---|
| 1136 | } |
---|
| 1137 | |
---|
[235] | 1138 | void changeU(Edge e, Node n) { |
---|
[57] | 1139 | if(arcs[(2 * e.id) | 1].next_out != -1) { |
---|
[209] | 1140 | arcs[arcs[(2 * e.id) | 1].next_out].prev_out = |
---|
[57] | 1141 | arcs[(2 * e.id) | 1].prev_out; |
---|
| 1142 | } |
---|
| 1143 | if(arcs[(2 * e.id) | 1].prev_out != -1) { |
---|
[209] | 1144 | arcs[arcs[(2 * e.id) | 1].prev_out].next_out = |
---|
[57] | 1145 | arcs[(2 * e.id) | 1].next_out; |
---|
| 1146 | } else { |
---|
[209] | 1147 | nodes[arcs[2 * e.id].target].first_out = |
---|
[57] | 1148 | arcs[(2 * e.id) | 1].next_out; |
---|
| 1149 | } |
---|
| 1150 | |
---|
| 1151 | if (nodes[n.id].first_out != -1) { |
---|
[209] | 1152 | arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
---|
[57] | 1153 | } |
---|
| 1154 | arcs[2 * e.id].target = n.id; |
---|
| 1155 | arcs[(2 * e.id) | 1].prev_out = -1; |
---|
| 1156 | arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out; |
---|
| 1157 | nodes[n.id].first_out = ((2 * e.id) | 1); |
---|
| 1158 | } |
---|
| 1159 | |
---|
| 1160 | }; |
---|
| 1161 | |
---|
| 1162 | typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
---|
| 1163 | |
---|
| 1164 | |
---|
[73] | 1165 | /// \addtogroup graphs |
---|
[57] | 1166 | /// @{ |
---|
| 1167 | |
---|
[73] | 1168 | ///A general undirected graph structure. |
---|
[57] | 1169 | |
---|
[735] | 1170 | ///\ref ListGraph is a versatile and fast undirected graph |
---|
| 1171 | ///implementation based on linked lists that are stored in |
---|
[209] | 1172 | ///\c std::vector structures. |
---|
[57] | 1173 | /// |
---|
[735] | 1174 | ///This type fully conforms to the \ref concepts::Graph "Graph concept" |
---|
| 1175 | ///and it also provides several useful additional functionalities. |
---|
| 1176 | ///Most of its member functions and nested classes are documented |
---|
[73] | 1177 | ///only in the concept class. |
---|
| 1178 | /// |
---|
| 1179 | ///\sa concepts::Graph |
---|
[735] | 1180 | ///\sa ListDigraph |
---|
[57] | 1181 | class ListGraph : public ExtendedListGraphBase { |
---|
[617] | 1182 | typedef ExtendedListGraphBase Parent; |
---|
| 1183 | |
---|
[57] | 1184 | private: |
---|
[735] | 1185 | /// Graphs are \e not copy constructible. Use GraphCopy instead. |
---|
[57] | 1186 | ListGraph(const ListGraph &) :ExtendedListGraphBase() {}; |
---|
[735] | 1187 | /// \brief Assignment of a graph to another one is \e not allowed. |
---|
| 1188 | /// Use GraphCopy instead. |
---|
[57] | 1189 | void operator=(const ListGraph &) {} |
---|
| 1190 | public: |
---|
| 1191 | /// Constructor |
---|
[209] | 1192 | |
---|
[57] | 1193 | /// Constructor. |
---|
| 1194 | /// |
---|
| 1195 | ListGraph() {} |
---|
| 1196 | |
---|
[73] | 1197 | typedef Parent::OutArcIt IncEdgeIt; |
---|
[57] | 1198 | |
---|
[73] | 1199 | /// \brief Add a new node to the graph. |
---|
[57] | 1200 | /// |
---|
[735] | 1201 | /// This function adds a new node to the graph. |
---|
[559] | 1202 | /// \return The new node. |
---|
[57] | 1203 | Node addNode() { return Parent::addNode(); } |
---|
| 1204 | |
---|
[73] | 1205 | /// \brief Add a new edge to the graph. |
---|
[57] | 1206 | /// |
---|
[735] | 1207 | /// This function adds a new edge to the graph between nodes |
---|
| 1208 | /// \c u and \c v with inherent orientation from node \c u to |
---|
| 1209 | /// node \c v. |
---|
[559] | 1210 | /// \return The new edge. |
---|
[735] | 1211 | Edge addEdge(Node u, Node v) { |
---|
| 1212 | return Parent::addEdge(u, v); |
---|
[57] | 1213 | } |
---|
[234] | 1214 | |
---|
[735] | 1215 | ///\brief Erase a node from the graph. |
---|
[234] | 1216 | /// |
---|
[735] | 1217 | /// This function erases the given node from the graph. |
---|
| 1218 | void erase(Node n) { Parent::erase(n); } |
---|
| 1219 | |
---|
| 1220 | ///\brief Erase an edge from the graph. |
---|
[234] | 1221 | /// |
---|
[735] | 1222 | /// This function erases the given edge from the graph. |
---|
| 1223 | void erase(Edge e) { Parent::erase(e); } |
---|
[149] | 1224 | /// Node validity check |
---|
| 1225 | |
---|
[735] | 1226 | /// This function gives back \c true if the given node is valid, |
---|
| 1227 | /// i.e. it is a real node of the graph. |
---|
[149] | 1228 | /// |
---|
[735] | 1229 | /// \warning A removed node could become valid again if new nodes are |
---|
[149] | 1230 | /// added to the graph. |
---|
| 1231 | bool valid(Node n) const { return Parent::valid(n); } |
---|
[735] | 1232 | /// Edge validity check |
---|
| 1233 | |
---|
| 1234 | /// This function gives back \c true if the given edge is valid, |
---|
| 1235 | /// i.e. it is a real edge of the graph. |
---|
| 1236 | /// |
---|
| 1237 | /// \warning A removed edge could become valid again if new edges are |
---|
| 1238 | /// added to the graph. |
---|
| 1239 | bool valid(Edge e) const { return Parent::valid(e); } |
---|
[149] | 1240 | /// Arc validity check |
---|
| 1241 | |
---|
[735] | 1242 | /// This function gives back \c true if the given arc is valid, |
---|
| 1243 | /// i.e. it is a real arc of the graph. |
---|
[149] | 1244 | /// |
---|
[735] | 1245 | /// \warning A removed arc could become valid again if new edges are |
---|
[149] | 1246 | /// added to the graph. |
---|
| 1247 | bool valid(Arc a) const { return Parent::valid(a); } |
---|
| 1248 | |
---|
[735] | 1249 | /// \brief Change the first node of an edge. |
---|
[149] | 1250 | /// |
---|
[735] | 1251 | /// This function changes the first node of the given edge \c e to \c n. |
---|
[57] | 1252 | /// |
---|
[735] | 1253 | ///\note \c EdgeIt and \c ArcIt iterators referencing the |
---|
| 1254 | ///changed edge are invalidated and all other iterators whose |
---|
| 1255 | ///base node is the changed node are also invalidated. |
---|
[73] | 1256 | /// |
---|
| 1257 | ///\warning This functionality cannot be used together with the |
---|
| 1258 | ///Snapshot feature. |
---|
[235] | 1259 | void changeU(Edge e, Node n) { |
---|
| 1260 | Parent::changeU(e,n); |
---|
[209] | 1261 | } |
---|
[735] | 1262 | /// \brief Change the second node of an edge. |
---|
[57] | 1263 | /// |
---|
[735] | 1264 | /// This function changes the second node of the given edge \c e to \c n. |
---|
[57] | 1265 | /// |
---|
[735] | 1266 | ///\note \c EdgeIt iterators referencing the changed edge remain |
---|
| 1267 | ///valid, however \c ArcIt iterators referencing the changed edge and |
---|
| 1268 | ///all other iterators whose base node is the changed node are also |
---|
| 1269 | ///invalidated. |
---|
[73] | 1270 | /// |
---|
| 1271 | ///\warning This functionality cannot be used together with the |
---|
| 1272 | ///Snapshot feature. |
---|
[235] | 1273 | void changeV(Edge e, Node n) { |
---|
| 1274 | Parent::changeV(e,n); |
---|
[57] | 1275 | } |
---|
[735] | 1276 | |
---|
[57] | 1277 | /// \brief Contract two nodes. |
---|
| 1278 | /// |
---|
[735] | 1279 | /// This function contracts the given two nodes. |
---|
| 1280 | /// Node \c b is removed, but instead of deleting |
---|
| 1281 | /// its incident edges, they are joined to node \c a. |
---|
| 1282 | /// If the last parameter \c r is \c true (this is the default value), |
---|
| 1283 | /// then the newly created loops are removed. |
---|
[57] | 1284 | /// |
---|
[735] | 1285 | /// \note The moved edges are joined to node \c a using changeU() |
---|
| 1286 | /// or changeV(), thus all edge and arc iterators whose base node is |
---|
| 1287 | /// \c b are invalidated. |
---|
| 1288 | /// Moreover all iterators referencing node \c b or the removed |
---|
| 1289 | /// loops are also invalidated. Other iterators remain valid. |
---|
[73] | 1290 | /// |
---|
| 1291 | ///\warning This functionality cannot be used together with the |
---|
| 1292 | ///Snapshot feature. |
---|
[57] | 1293 | void contract(Node a, Node b, bool r = true) { |
---|
[73] | 1294 | for(IncEdgeIt e(*this, b); e!=INVALID;) { |
---|
[209] | 1295 | IncEdgeIt f = e; ++f; |
---|
| 1296 | if (r && runningNode(e) == a) { |
---|
| 1297 | erase(e); |
---|
[235] | 1298 | } else if (u(e) == b) { |
---|
| 1299 | changeU(e, a); |
---|
[209] | 1300 | } else { |
---|
[235] | 1301 | changeV(e, a); |
---|
[209] | 1302 | } |
---|
| 1303 | e = f; |
---|
[57] | 1304 | } |
---|
| 1305 | erase(b); |
---|
| 1306 | } |
---|
| 1307 | |
---|
[735] | 1308 | ///Clear the graph. |
---|
| 1309 | |
---|
| 1310 | ///This function erases all nodes and arcs from the graph. |
---|
| 1311 | /// |
---|
| 1312 | void clear() { |
---|
| 1313 | Parent::clear(); |
---|
| 1314 | } |
---|
[57] | 1315 | |
---|
[736] | 1316 | /// Reserve memory for nodes. |
---|
| 1317 | |
---|
| 1318 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 1319 | /// allocation: if you know that the graph you want to build will |
---|
| 1320 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 1321 | /// then it is worth reserving space for this amount before starting |
---|
| 1322 | /// to build the graph. |
---|
| 1323 | /// \sa reserveEdge() |
---|
| 1324 | void reserveNode(int n) { nodes.reserve(n); }; |
---|
| 1325 | |
---|
| 1326 | /// Reserve memory for edges. |
---|
| 1327 | |
---|
| 1328 | /// Using this function, it is possible to avoid superfluous memory |
---|
| 1329 | /// allocation: if you know that the graph you want to build will |
---|
| 1330 | /// be large (e.g. it will contain millions of nodes and/or edges), |
---|
| 1331 | /// then it is worth reserving space for this amount before starting |
---|
| 1332 | /// to build the graph. |
---|
| 1333 | /// \sa reserveNode() |
---|
| 1334 | void reserveEdge(int m) { arcs.reserve(2 * m); }; |
---|
| 1335 | |
---|
[73] | 1336 | /// \brief Class to make a snapshot of the graph and restore |
---|
| 1337 | /// it later. |
---|
[57] | 1338 | /// |
---|
[73] | 1339 | /// Class to make a snapshot of the graph and restore it later. |
---|
[57] | 1340 | /// |
---|
| 1341 | /// The newly added nodes and edges can be removed |
---|
| 1342 | /// using the restore() function. |
---|
| 1343 | /// |
---|
[735] | 1344 | /// \note After a state is restored, you cannot restore a later state, |
---|
| 1345 | /// i.e. you cannot add the removed nodes and edges again using |
---|
| 1346 | /// another Snapshot instance. |
---|
| 1347 | /// |
---|
| 1348 | /// \warning Node and edge deletions and other modifications |
---|
| 1349 | /// (e.g. changing the end-nodes of edges or contracting nodes) |
---|
| 1350 | /// cannot be restored. These events invalidate the snapshot. |
---|
| 1351 | /// However the edges and nodes that were added to the graph after |
---|
| 1352 | /// making the current snapshot can be removed without invalidating it. |
---|
[57] | 1353 | class Snapshot { |
---|
| 1354 | protected: |
---|
| 1355 | |
---|
| 1356 | typedef Parent::NodeNotifier NodeNotifier; |
---|
| 1357 | |
---|
| 1358 | class NodeObserverProxy : public NodeNotifier::ObserverBase { |
---|
| 1359 | public: |
---|
| 1360 | |
---|
| 1361 | NodeObserverProxy(Snapshot& _snapshot) |
---|
| 1362 | : snapshot(_snapshot) {} |
---|
| 1363 | |
---|
| 1364 | using NodeNotifier::ObserverBase::attach; |
---|
| 1365 | using NodeNotifier::ObserverBase::detach; |
---|
| 1366 | using NodeNotifier::ObserverBase::attached; |
---|
[209] | 1367 | |
---|
[57] | 1368 | protected: |
---|
[209] | 1369 | |
---|
[57] | 1370 | virtual void add(const Node& node) { |
---|
| 1371 | snapshot.addNode(node); |
---|
| 1372 | } |
---|
| 1373 | virtual void add(const std::vector<Node>& nodes) { |
---|
| 1374 | for (int i = nodes.size() - 1; i >= 0; ++i) { |
---|
| 1375 | snapshot.addNode(nodes[i]); |
---|
| 1376 | } |
---|
| 1377 | } |
---|
| 1378 | virtual void erase(const Node& node) { |
---|
| 1379 | snapshot.eraseNode(node); |
---|
| 1380 | } |
---|
| 1381 | virtual void erase(const std::vector<Node>& nodes) { |
---|
| 1382 | for (int i = 0; i < int(nodes.size()); ++i) { |
---|
| 1383 | snapshot.eraseNode(nodes[i]); |
---|
| 1384 | } |
---|
| 1385 | } |
---|
| 1386 | virtual void build() { |
---|
| 1387 | Node node; |
---|
| 1388 | std::vector<Node> nodes; |
---|
[209] | 1389 | for (notifier()->first(node); node != INVALID; |
---|
[57] | 1390 | notifier()->next(node)) { |
---|
| 1391 | nodes.push_back(node); |
---|
| 1392 | } |
---|
| 1393 | for (int i = nodes.size() - 1; i >= 0; --i) { |
---|
| 1394 | snapshot.addNode(nodes[i]); |
---|
| 1395 | } |
---|
| 1396 | } |
---|
| 1397 | virtual void clear() { |
---|
| 1398 | Node node; |
---|
[209] | 1399 | for (notifier()->first(node); node != INVALID; |
---|
[57] | 1400 | notifier()->next(node)) { |
---|
| 1401 | snapshot.eraseNode(node); |
---|
| 1402 | } |
---|
| 1403 | } |
---|
| 1404 | |
---|
| 1405 | Snapshot& snapshot; |
---|
| 1406 | }; |
---|
| 1407 | |
---|
| 1408 | class EdgeObserverProxy : public EdgeNotifier::ObserverBase { |
---|
| 1409 | public: |
---|
| 1410 | |
---|
| 1411 | EdgeObserverProxy(Snapshot& _snapshot) |
---|
| 1412 | : snapshot(_snapshot) {} |
---|
| 1413 | |
---|
| 1414 | using EdgeNotifier::ObserverBase::attach; |
---|
| 1415 | using EdgeNotifier::ObserverBase::detach; |
---|
| 1416 | using EdgeNotifier::ObserverBase::attached; |
---|
[209] | 1417 | |
---|
[57] | 1418 | protected: |
---|
| 1419 | |
---|
[73] | 1420 | virtual void add(const Edge& edge) { |
---|
| 1421 | snapshot.addEdge(edge); |
---|
[57] | 1422 | } |
---|
[73] | 1423 | virtual void add(const std::vector<Edge>& edges) { |
---|
| 1424 | for (int i = edges.size() - 1; i >= 0; ++i) { |
---|
| 1425 | snapshot.addEdge(edges[i]); |
---|
[57] | 1426 | } |
---|
| 1427 | } |
---|
[73] | 1428 | virtual void erase(const Edge& edge) { |
---|
| 1429 | snapshot.eraseEdge(edge); |
---|
[57] | 1430 | } |
---|
[73] | 1431 | virtual void erase(const std::vector<Edge>& edges) { |
---|
| 1432 | for (int i = 0; i < int(edges.size()); ++i) { |
---|
| 1433 | snapshot.eraseEdge(edges[i]); |
---|
[57] | 1434 | } |
---|
| 1435 | } |
---|
| 1436 | virtual void build() { |
---|
[73] | 1437 | Edge edge; |
---|
| 1438 | std::vector<Edge> edges; |
---|
[209] | 1439 | for (notifier()->first(edge); edge != INVALID; |
---|
[73] | 1440 | notifier()->next(edge)) { |
---|
| 1441 | edges.push_back(edge); |
---|
[57] | 1442 | } |
---|
[73] | 1443 | for (int i = edges.size() - 1; i >= 0; --i) { |
---|
| 1444 | snapshot.addEdge(edges[i]); |
---|
[57] | 1445 | } |
---|
| 1446 | } |
---|
| 1447 | virtual void clear() { |
---|
[73] | 1448 | Edge edge; |
---|
[209] | 1449 | for (notifier()->first(edge); edge != INVALID; |
---|
[73] | 1450 | notifier()->next(edge)) { |
---|
| 1451 | snapshot.eraseEdge(edge); |
---|
[57] | 1452 | } |
---|
| 1453 | } |
---|
| 1454 | |
---|
| 1455 | Snapshot& snapshot; |
---|
| 1456 | }; |
---|
[73] | 1457 | |
---|
| 1458 | ListGraph *graph; |
---|
[57] | 1459 | |
---|
| 1460 | NodeObserverProxy node_observer_proxy; |
---|
[73] | 1461 | EdgeObserverProxy edge_observer_proxy; |
---|
[57] | 1462 | |
---|
| 1463 | std::list<Node> added_nodes; |
---|
[73] | 1464 | std::list<Edge> added_edges; |
---|
[57] | 1465 | |
---|
| 1466 | |
---|
| 1467 | void addNode(const Node& node) { |
---|
[209] | 1468 | added_nodes.push_front(node); |
---|
[57] | 1469 | } |
---|
| 1470 | void eraseNode(const Node& node) { |
---|
[209] | 1471 | std::list<Node>::iterator it = |
---|
[57] | 1472 | std::find(added_nodes.begin(), added_nodes.end(), node); |
---|
| 1473 | if (it == added_nodes.end()) { |
---|
| 1474 | clear(); |
---|
[73] | 1475 | edge_observer_proxy.detach(); |
---|
[57] | 1476 | throw NodeNotifier::ImmediateDetach(); |
---|
| 1477 | } else { |
---|
| 1478 | added_nodes.erase(it); |
---|
| 1479 | } |
---|
| 1480 | } |
---|
| 1481 | |
---|
[73] | 1482 | void addEdge(const Edge& edge) { |
---|
[209] | 1483 | added_edges.push_front(edge); |
---|
[57] | 1484 | } |
---|
[73] | 1485 | void eraseEdge(const Edge& edge) { |
---|
[209] | 1486 | std::list<Edge>::iterator it = |
---|
[73] | 1487 | std::find(added_edges.begin(), added_edges.end(), edge); |
---|
| 1488 | if (it == added_edges.end()) { |
---|
[57] | 1489 | clear(); |
---|
| 1490 | node_observer_proxy.detach(); |
---|
| 1491 | throw EdgeNotifier::ImmediateDetach(); |
---|
| 1492 | } else { |
---|
[73] | 1493 | added_edges.erase(it); |
---|
| 1494 | } |
---|
[57] | 1495 | } |
---|
| 1496 | |
---|
[73] | 1497 | void attach(ListGraph &_graph) { |
---|
[209] | 1498 | graph = &_graph; |
---|
| 1499 | node_observer_proxy.attach(graph->notifier(Node())); |
---|
[73] | 1500 | edge_observer_proxy.attach(graph->notifier(Edge())); |
---|
[57] | 1501 | } |
---|
[209] | 1502 | |
---|
[57] | 1503 | void detach() { |
---|
[209] | 1504 | node_observer_proxy.detach(); |
---|
| 1505 | edge_observer_proxy.detach(); |
---|
[57] | 1506 | } |
---|
| 1507 | |
---|
| 1508 | bool attached() const { |
---|
| 1509 | return node_observer_proxy.attached(); |
---|
| 1510 | } |
---|
| 1511 | |
---|
| 1512 | void clear() { |
---|
| 1513 | added_nodes.clear(); |
---|
[209] | 1514 | added_edges.clear(); |
---|
[57] | 1515 | } |
---|
| 1516 | |
---|
| 1517 | public: |
---|
| 1518 | |
---|
| 1519 | /// \brief Default constructor. |
---|
| 1520 | /// |
---|
| 1521 | /// Default constructor. |
---|
[735] | 1522 | /// You have to call save() to actually make a snapshot. |
---|
[209] | 1523 | Snapshot() |
---|
| 1524 | : graph(0), node_observer_proxy(*this), |
---|
[73] | 1525 | edge_observer_proxy(*this) {} |
---|
[209] | 1526 | |
---|
[57] | 1527 | /// \brief Constructor that immediately makes a snapshot. |
---|
[209] | 1528 | /// |
---|
[735] | 1529 | /// This constructor immediately makes a snapshot of the given graph. |
---|
| 1530 | Snapshot(ListGraph &gr) |
---|
[209] | 1531 | : node_observer_proxy(*this), |
---|
[73] | 1532 | edge_observer_proxy(*this) { |
---|
[735] | 1533 | attach(gr); |
---|
[57] | 1534 | } |
---|
[209] | 1535 | |
---|
[57] | 1536 | /// \brief Make a snapshot. |
---|
| 1537 | /// |
---|
[735] | 1538 | /// This function makes a snapshot of the given graph. |
---|
| 1539 | /// It can be called more than once. In case of a repeated |
---|
[57] | 1540 | /// call, the previous snapshot gets lost. |
---|
[735] | 1541 | void save(ListGraph &gr) { |
---|
[57] | 1542 | if (attached()) { |
---|
| 1543 | detach(); |
---|
| 1544 | clear(); |
---|
| 1545 | } |
---|
[735] | 1546 | attach(gr); |
---|
[57] | 1547 | } |
---|
[209] | 1548 | |
---|
[57] | 1549 | /// \brief Undo the changes until the last snapshot. |
---|
[735] | 1550 | /// |
---|
| 1551 | /// This function undos the changes until the last snapshot |
---|
| 1552 | /// created by save() or Snapshot(ListGraph&). |
---|
[57] | 1553 | void restore() { |
---|
[209] | 1554 | detach(); |
---|
| 1555 | for(std::list<Edge>::iterator it = added_edges.begin(); |
---|
[73] | 1556 | it != added_edges.end(); ++it) { |
---|
[209] | 1557 | graph->erase(*it); |
---|
| 1558 | } |
---|
| 1559 | for(std::list<Node>::iterator it = added_nodes.begin(); |
---|
[57] | 1560 | it != added_nodes.end(); ++it) { |
---|
[209] | 1561 | graph->erase(*it); |
---|
| 1562 | } |
---|
[57] | 1563 | clear(); |
---|
| 1564 | } |
---|
| 1565 | |
---|
[735] | 1566 | /// \brief Returns \c true if the snapshot is valid. |
---|
[57] | 1567 | /// |
---|
[735] | 1568 | /// This function returns \c true if the snapshot is valid. |
---|
[57] | 1569 | bool valid() const { |
---|
| 1570 | return attached(); |
---|
| 1571 | } |
---|
| 1572 | }; |
---|
| 1573 | }; |
---|
[209] | 1574 | |
---|
| 1575 | /// @} |
---|
[57] | 1576 | } //namespace lemon |
---|
[209] | 1577 | |
---|
[57] | 1578 | |
---|
| 1579 | #endif |
---|