[395] | 1 | // -*- mode:C++ -*- |
---|
| 2 | |
---|
[405] | 3 | #ifndef HUGO_LIST_GRAPH_H |
---|
| 4 | #define HUGO_LIST_GRAPH_H |
---|
[395] | 5 | |
---|
[491] | 6 | ///\ingroup graphs |
---|
[395] | 7 | ///\file |
---|
[405] | 8 | ///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes. |
---|
[395] | 9 | |
---|
| 10 | #include <vector> |
---|
[782] | 11 | #include <climits> |
---|
[395] | 12 | |
---|
[542] | 13 | #include <hugo/invalid.h> |
---|
[395] | 14 | |
---|
[782] | 15 | #include <hugo/map_registry.h> |
---|
| 16 | #include <hugo/array_map_factory.h> |
---|
| 17 | |
---|
| 18 | #include <hugo/sym_map_factory.h> |
---|
| 19 | |
---|
| 20 | #include <hugo/map_defines.h> |
---|
| 21 | |
---|
| 22 | |
---|
[395] | 23 | namespace hugo { |
---|
| 24 | |
---|
[406] | 25 | /// \addtogroup graphs |
---|
| 26 | /// @{ |
---|
| 27 | |
---|
[782] | 28 | // class SymListGraph; |
---|
[395] | 29 | |
---|
[401] | 30 | ///A list graph class. |
---|
[395] | 31 | |
---|
[397] | 32 | ///This is a simple and fast erasable graph implementation. |
---|
| 33 | /// |
---|
[395] | 34 | ///It conforms to the graph interface documented under |
---|
| 35 | ///the description of \ref GraphSkeleton. |
---|
| 36 | ///\sa \ref GraphSkeleton. |
---|
[397] | 37 | class ListGraph { |
---|
[395] | 38 | |
---|
[397] | 39 | //Nodes are double linked. |
---|
| 40 | //The free nodes are only single linked using the "next" field. |
---|
[395] | 41 | struct NodeT |
---|
| 42 | { |
---|
[397] | 43 | int first_in,first_out; |
---|
| 44 | int prev, next; |
---|
| 45 | // NodeT() {} |
---|
[395] | 46 | }; |
---|
[397] | 47 | //Edges are double linked. |
---|
| 48 | //The free edges are only single linked using the "next_in" field. |
---|
[395] | 49 | struct EdgeT |
---|
| 50 | { |
---|
[397] | 51 | int head, tail; |
---|
| 52 | int prev_in, prev_out; |
---|
| 53 | int next_in, next_out; |
---|
[395] | 54 | //FIXME: is this necessary? |
---|
[397] | 55 | // EdgeT() : next_in(-1), next_out(-1) prev_in(-1), prev_out(-1) {} |
---|
[395] | 56 | }; |
---|
| 57 | |
---|
| 58 | std::vector<NodeT> nodes; |
---|
[397] | 59 | //The first node |
---|
| 60 | int first_node; |
---|
| 61 | //The first free node |
---|
| 62 | int first_free_node; |
---|
[395] | 63 | std::vector<EdgeT> edges; |
---|
[397] | 64 | //The first free edge |
---|
| 65 | int first_free_edge; |
---|
[395] | 66 | |
---|
[782] | 67 | public: |
---|
[395] | 68 | |
---|
[782] | 69 | typedef ListGraph Graph; |
---|
[397] | 70 | |
---|
[395] | 71 | class Node; |
---|
| 72 | class Edge; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | public: |
---|
| 76 | |
---|
| 77 | class NodeIt; |
---|
| 78 | class EdgeIt; |
---|
| 79 | class OutEdgeIt; |
---|
| 80 | class InEdgeIt; |
---|
[782] | 81 | |
---|
| 82 | CREATE_MAP_REGISTRIES; |
---|
| 83 | CREATE_MAPS(ArrayMapFactory); |
---|
| 84 | |
---|
[395] | 85 | public: |
---|
| 86 | |
---|
[782] | 87 | ListGraph() |
---|
| 88 | : nodes(), first_node(-1), |
---|
| 89 | first_free_node(-1), edges(), first_free_edge(-1) {} |
---|
| 90 | |
---|
| 91 | ListGraph(const ListGraph &_g) |
---|
| 92 | : nodes(_g.nodes), first_node(_g.first_node), |
---|
| 93 | first_free_node(_g.first_free_node), edges(_g.edges), |
---|
| 94 | first_free_edge(_g.first_free_edge) {} |
---|
[395] | 95 | |
---|
| 96 | int nodeNum() const { return nodes.size(); } //FIXME: What is this? |
---|
| 97 | int edgeNum() const { return edges.size(); } //FIXME: What is this? |
---|
| 98 | |
---|
[695] | 99 | ///Set the expected number of edges |
---|
| 100 | |
---|
| 101 | ///With this function, it is possible to set the expected number of edges. |
---|
| 102 | ///The use of this fasten the building of the graph and makes |
---|
| 103 | ///it possible to avoid the superfluous memory allocation. |
---|
| 104 | void reserveEdge(int n) { edges.reserve(n); }; |
---|
| 105 | |
---|
[395] | 106 | ///\bug This function does something different than |
---|
| 107 | ///its name would suggests... |
---|
| 108 | int maxNodeId() const { return nodes.size(); } //FIXME: What is this? |
---|
| 109 | ///\bug This function does something different than |
---|
| 110 | ///its name would suggests... |
---|
| 111 | int maxEdgeId() const { return edges.size(); } //FIXME: What is this? |
---|
| 112 | |
---|
| 113 | Node tail(Edge e) const { return edges[e.n].tail; } |
---|
| 114 | Node head(Edge e) const { return edges[e.n].head; } |
---|
| 115 | |
---|
[713] | 116 | NodeIt& first(NodeIt& v) const { |
---|
[395] | 117 | v=NodeIt(*this); return v; } |
---|
[713] | 118 | EdgeIt& first(EdgeIt& e) const { |
---|
[395] | 119 | e=EdgeIt(*this); return e; } |
---|
[713] | 120 | OutEdgeIt& first(OutEdgeIt& e, const Node v) const { |
---|
[395] | 121 | e=OutEdgeIt(*this,v); return e; } |
---|
[713] | 122 | InEdgeIt& first(InEdgeIt& e, const Node v) const { |
---|
[395] | 123 | e=InEdgeIt(*this,v); return e; } |
---|
| 124 | |
---|
[713] | 125 | static int id(Node v) { return v.n; } |
---|
| 126 | static int id(Edge e) { return e.n; } |
---|
[395] | 127 | |
---|
[397] | 128 | /// Adds a new node to the graph. |
---|
| 129 | |
---|
| 130 | /// \todo It adds the nodes in a reversed order. |
---|
| 131 | /// (i.e. the lastly added node becomes the first.) |
---|
[395] | 132 | Node addNode() { |
---|
[397] | 133 | int n; |
---|
| 134 | |
---|
| 135 | if(first_free_node==-1) |
---|
| 136 | { |
---|
| 137 | n = nodes.size(); |
---|
| 138 | nodes.push_back(NodeT()); |
---|
| 139 | } |
---|
| 140 | else { |
---|
| 141 | n = first_free_node; |
---|
| 142 | first_free_node = nodes[n].next; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | nodes[n].next = first_node; |
---|
| 146 | if(first_node != -1) nodes[first_node].prev = n; |
---|
| 147 | first_node = n; |
---|
| 148 | nodes[n].prev = -1; |
---|
| 149 | |
---|
| 150 | nodes[n].first_in = nodes[n].first_out = -1; |
---|
| 151 | |
---|
| 152 | Node nn; nn.n=n; |
---|
[395] | 153 | |
---|
[397] | 154 | //Update dynamic maps |
---|
[782] | 155 | node_maps.add(nn); |
---|
[395] | 156 | |
---|
[397] | 157 | return nn; |
---|
[395] | 158 | } |
---|
| 159 | |
---|
| 160 | Edge addEdge(Node u, Node v) { |
---|
[397] | 161 | int n; |
---|
| 162 | |
---|
| 163 | if(first_free_edge==-1) |
---|
| 164 | { |
---|
| 165 | n = edges.size(); |
---|
| 166 | edges.push_back(EdgeT()); |
---|
| 167 | } |
---|
| 168 | else { |
---|
| 169 | n = first_free_edge; |
---|
| 170 | first_free_edge = edges[n].next_in; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | edges[n].tail = u.n; edges[n].head = v.n; |
---|
[395] | 174 | |
---|
[397] | 175 | edges[n].next_out = nodes[u.n].first_out; |
---|
| 176 | if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n; |
---|
| 177 | edges[n].next_in = nodes[v.n].first_in; |
---|
| 178 | if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n; |
---|
| 179 | edges[n].prev_in = edges[n].prev_out = -1; |
---|
| 180 | |
---|
| 181 | nodes[u.n].first_out = nodes[v.n].first_in = n; |
---|
| 182 | |
---|
| 183 | Edge e; e.n=n; |
---|
| 184 | |
---|
| 185 | //Update dynamic maps |
---|
[782] | 186 | edge_maps.add(e); |
---|
[395] | 187 | |
---|
| 188 | return e; |
---|
| 189 | } |
---|
[774] | 190 | |
---|
| 191 | /// Finds an edge between two nodes. |
---|
[395] | 192 | |
---|
[774] | 193 | /// Finds an edge from node \c u to node \c v. |
---|
| 194 | /// |
---|
| 195 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 196 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 197 | /// the next edge from \c u to \c v after \c prev. |
---|
| 198 | /// \return The found edge or INVALID if there is no such an edge. |
---|
| 199 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 200 | { |
---|
| 201 | int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; |
---|
| 202 | while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out; |
---|
| 203 | prev.n=e; |
---|
| 204 | return prev; |
---|
| 205 | } |
---|
| 206 | |
---|
[397] | 207 | private: |
---|
| 208 | void eraseEdge(int n) { |
---|
| 209 | |
---|
| 210 | if(edges[n].next_in!=-1) |
---|
| 211 | edges[edges[n].next_in].prev_in = edges[n].prev_in; |
---|
| 212 | if(edges[n].prev_in!=-1) |
---|
| 213 | edges[edges[n].prev_in].next_in = edges[n].next_in; |
---|
| 214 | else nodes[edges[n].head].first_in = edges[n].next_in; |
---|
| 215 | |
---|
| 216 | if(edges[n].next_out!=-1) |
---|
| 217 | edges[edges[n].next_out].prev_out = edges[n].prev_out; |
---|
| 218 | if(edges[n].prev_out!=-1) |
---|
| 219 | edges[edges[n].prev_out].next_out = edges[n].next_out; |
---|
| 220 | else nodes[edges[n].tail].first_out = edges[n].next_out; |
---|
| 221 | |
---|
| 222 | edges[n].next_in = first_free_edge; |
---|
[695] | 223 | first_free_edge = n; |
---|
[397] | 224 | |
---|
| 225 | //Update dynamic maps |
---|
| 226 | Edge e; e.n=n; |
---|
[782] | 227 | edge_maps.erase(e); |
---|
| 228 | |
---|
[397] | 229 | } |
---|
| 230 | |
---|
| 231 | public: |
---|
| 232 | |
---|
| 233 | void erase(Node nn) { |
---|
| 234 | int n=nn.n; |
---|
| 235 | |
---|
| 236 | int m; |
---|
| 237 | while((m=nodes[n].first_in)!=-1) eraseEdge(m); |
---|
| 238 | while((m=nodes[n].first_out)!=-1) eraseEdge(m); |
---|
| 239 | |
---|
| 240 | if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; |
---|
| 241 | if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; |
---|
| 242 | else first_node = nodes[n].next; |
---|
| 243 | |
---|
| 244 | nodes[n].next = first_free_node; |
---|
| 245 | first_free_node = n; |
---|
| 246 | |
---|
| 247 | //Update dynamic maps |
---|
[782] | 248 | node_maps.erase(nn); |
---|
| 249 | |
---|
[397] | 250 | } |
---|
| 251 | |
---|
| 252 | void erase(Edge e) { eraseEdge(e.n); } |
---|
| 253 | |
---|
| 254 | void clear() { |
---|
[782] | 255 | edge_maps.clear(); |
---|
| 256 | edges.clear(); |
---|
| 257 | node_maps.clear(); |
---|
| 258 | nodes.clear(); |
---|
[397] | 259 | first_node=first_free_node=first_free_edge=-1; |
---|
| 260 | } |
---|
[395] | 261 | |
---|
| 262 | class Node { |
---|
[397] | 263 | friend class ListGraph; |
---|
[395] | 264 | template <typename T> friend class NodeMap; |
---|
[400] | 265 | |
---|
[395] | 266 | friend class Edge; |
---|
| 267 | friend class OutEdgeIt; |
---|
| 268 | friend class InEdgeIt; |
---|
| 269 | friend class SymEdge; |
---|
| 270 | |
---|
| 271 | protected: |
---|
| 272 | int n; |
---|
[722] | 273 | friend int ListGraph::id(Node v); |
---|
[395] | 274 | Node(int nn) {n=nn;} |
---|
| 275 | public: |
---|
| 276 | Node() {} |
---|
[503] | 277 | Node (Invalid) { n=-1; } |
---|
[395] | 278 | bool operator==(const Node i) const {return n==i.n;} |
---|
| 279 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
| 280 | bool operator<(const Node i) const {return n<i.n;} |
---|
[774] | 281 | // ///Validity check |
---|
| 282 | // operator bool() { return n!=-1; } |
---|
[395] | 283 | }; |
---|
| 284 | |
---|
| 285 | class NodeIt : public Node { |
---|
[774] | 286 | const ListGraph *G; |
---|
[397] | 287 | friend class ListGraph; |
---|
[395] | 288 | public: |
---|
[400] | 289 | NodeIt() : Node() { } |
---|
| 290 | NodeIt(Invalid i) : Node(i) { } |
---|
[774] | 291 | NodeIt(const ListGraph& _G) : Node(_G.first_node), G(&_G) { } |
---|
[579] | 292 | ///\todo Undocumented conversion Node -\> NodeIt. |
---|
[774] | 293 | NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { } |
---|
| 294 | NodeIt &operator++() { |
---|
| 295 | n=G->nodes[n].next; |
---|
| 296 | return *this; |
---|
| 297 | } |
---|
| 298 | // ///Validity check |
---|
| 299 | // operator bool() { return Node::operator bool(); } |
---|
[395] | 300 | }; |
---|
| 301 | |
---|
| 302 | class Edge { |
---|
[397] | 303 | friend class ListGraph; |
---|
[395] | 304 | template <typename T> friend class EdgeMap; |
---|
| 305 | |
---|
[397] | 306 | //template <typename T> friend class SymListGraph::SymEdgeMap; |
---|
| 307 | //friend Edge SymListGraph::opposite(Edge) const; |
---|
[395] | 308 | |
---|
| 309 | friend class Node; |
---|
| 310 | friend class NodeIt; |
---|
| 311 | protected: |
---|
| 312 | int n; |
---|
[722] | 313 | friend int ListGraph::id(Edge e); |
---|
[395] | 314 | |
---|
[706] | 315 | public: |
---|
| 316 | /// An Edge with id \c n. |
---|
| 317 | |
---|
| 318 | /// \bug It should be |
---|
| 319 | /// obtained by a member function of the Graph. |
---|
[395] | 320 | Edge(int nn) {n=nn;} |
---|
[706] | 321 | |
---|
[395] | 322 | Edge() { } |
---|
| 323 | Edge (Invalid) { n=-1; } |
---|
| 324 | bool operator==(const Edge i) const {return n==i.n;} |
---|
| 325 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
| 326 | bool operator<(const Edge i) const {return n<i.n;} |
---|
| 327 | ///\bug This is a workaround until somebody tells me how to |
---|
[397] | 328 | ///make class \c SymListGraph::SymEdgeMap friend of Edge |
---|
[395] | 329 | int &idref() {return n;} |
---|
[774] | 330 | const int &idref() const {return n;} |
---|
| 331 | // ///Validity check |
---|
| 332 | // operator bool() { return n!=-1; } |
---|
| 333 | }; |
---|
[395] | 334 | |
---|
| 335 | class EdgeIt : public Edge { |
---|
[774] | 336 | const ListGraph *G; |
---|
[397] | 337 | friend class ListGraph; |
---|
[395] | 338 | public: |
---|
[774] | 339 | EdgeIt(const ListGraph& _G) : Edge(), G(&_G) { |
---|
[397] | 340 | int m; |
---|
[774] | 341 | for(m=_G.first_node; |
---|
| 342 | m!=-1 && _G.nodes[m].first_in == -1; m = _G.nodes[m].next); |
---|
| 343 | n = (m==-1)?-1:_G.nodes[m].first_in; |
---|
[397] | 344 | } |
---|
[395] | 345 | EdgeIt (Invalid i) : Edge(i) { } |
---|
[774] | 346 | EdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
[395] | 347 | EdgeIt() : Edge() { } |
---|
| 348 | ///\bug This is a workaround until somebody tells me how to |
---|
[397] | 349 | ///make class \c SymListGraph::SymEdgeMap friend of Edge |
---|
[395] | 350 | int &idref() {return n;} |
---|
[774] | 351 | EdgeIt &operator++() { |
---|
| 352 | if(G->edges[n].next_in!=-1) n=G->edges[n].next_in; |
---|
| 353 | else { |
---|
| 354 | int nn; |
---|
| 355 | for(nn=G->nodes[G->edges[n].head].next; |
---|
| 356 | nn!=-1 && G->nodes[nn].first_in == -1; |
---|
| 357 | nn = G->nodes[nn].next) ; |
---|
| 358 | n = (nn==-1)?-1:G->nodes[nn].first_in; |
---|
| 359 | } |
---|
| 360 | return *this; |
---|
| 361 | } |
---|
| 362 | // ///Validity check |
---|
| 363 | // operator bool() { return Edge::operator bool(); } |
---|
[395] | 364 | }; |
---|
| 365 | |
---|
| 366 | class OutEdgeIt : public Edge { |
---|
[774] | 367 | const ListGraph *G; |
---|
[397] | 368 | friend class ListGraph; |
---|
[395] | 369 | public: |
---|
| 370 | OutEdgeIt() : Edge() { } |
---|
[774] | 371 | OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
[395] | 372 | OutEdgeIt (Invalid i) : Edge(i) { } |
---|
| 373 | |
---|
[774] | 374 | OutEdgeIt(const ListGraph& _G,const Node v) |
---|
| 375 | : Edge(_G.nodes[v.n].first_out), G(&_G) {} |
---|
| 376 | OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; } |
---|
| 377 | // ///Validity check |
---|
| 378 | // operator bool() { return Edge::operator bool(); } |
---|
[395] | 379 | }; |
---|
| 380 | |
---|
| 381 | class InEdgeIt : public Edge { |
---|
[774] | 382 | const ListGraph *G; |
---|
[397] | 383 | friend class ListGraph; |
---|
[395] | 384 | public: |
---|
| 385 | InEdgeIt() : Edge() { } |
---|
[774] | 386 | InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
[395] | 387 | InEdgeIt (Invalid i) : Edge(i) { } |
---|
[774] | 388 | InEdgeIt(const ListGraph& _G,Node v) |
---|
| 389 | : Edge(_G.nodes[v.n].first_in), G(&_G) { } |
---|
| 390 | InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; } |
---|
| 391 | // ///Validity check |
---|
| 392 | // operator bool() { return Edge::operator bool(); } |
---|
[395] | 393 | }; |
---|
| 394 | }; |
---|
| 395 | |
---|
| 396 | ///Graph for bidirectional edges. |
---|
| 397 | |
---|
| 398 | ///The purpose of this graph structure is to handle graphs |
---|
| 399 | ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair |
---|
| 400 | ///of oppositely directed edges. |
---|
| 401 | ///There is a new edge map type called |
---|
[397] | 402 | ///\ref SymListGraph::SymEdgeMap "SymEdgeMap" |
---|
[395] | 403 | ///that complements this |
---|
| 404 | ///feature by |
---|
| 405 | ///storing shared values for the edge pairs. The usual |
---|
| 406 | ///\ref GraphSkeleton::EdgeMap "EdgeMap" |
---|
| 407 | ///can be used |
---|
| 408 | ///as well. |
---|
| 409 | /// |
---|
| 410 | ///The oppositely directed edge can also be obtained easily |
---|
| 411 | ///using \ref opposite. |
---|
[397] | 412 | /// |
---|
| 413 | ///Here erase(Edge) deletes a pair of edges. |
---|
| 414 | /// |
---|
| 415 | ///\todo this date structure need some reconsiderations. Maybe it |
---|
| 416 | ///should be implemented independently from ListGraph. |
---|
[782] | 417 | |
---|
[397] | 418 | class SymListGraph : public ListGraph |
---|
[395] | 419 | { |
---|
| 420 | public: |
---|
[782] | 421 | |
---|
| 422 | typedef SymListGraph Graph; |
---|
| 423 | |
---|
| 424 | KEEP_NODE_MAP(ListGraph); |
---|
| 425 | KEEP_EDGE_MAP(ListGraph); |
---|
| 426 | |
---|
| 427 | CREATE_SYM_EDGE_MAP_REGISTRY; |
---|
| 428 | CREATE_SYM_EDGE_MAP_FACTORY(ArrayMapFactory); |
---|
| 429 | IMPORT_SYM_EDGE_MAP(SymEdgeMapFactory); |
---|
[395] | 430 | |
---|
[397] | 431 | SymListGraph() : ListGraph() { } |
---|
| 432 | SymListGraph(const ListGraph &_g) : ListGraph(_g) { } |
---|
| 433 | ///Adds a pair of oppositely directed edges to the graph. |
---|
[395] | 434 | Edge addEdge(Node u, Node v) |
---|
| 435 | { |
---|
[397] | 436 | Edge e = ListGraph::addEdge(u,v); |
---|
[782] | 437 | Edge f = ListGraph::addEdge(v,u); |
---|
| 438 | sym_edge_maps.add(e); |
---|
| 439 | sym_edge_maps.add(f); |
---|
| 440 | |
---|
[395] | 441 | return e; |
---|
| 442 | } |
---|
| 443 | |
---|
[782] | 444 | void erase(Node n) { ListGraph::erase(n);} |
---|
[395] | 445 | ///The oppositely directed edge. |
---|
| 446 | |
---|
| 447 | ///Returns the oppositely directed |
---|
| 448 | ///pair of the edge \c e. |
---|
[713] | 449 | static Edge opposite(Edge e) |
---|
[395] | 450 | { |
---|
| 451 | Edge f; |
---|
| 452 | f.idref() = e.idref() - 2*(e.idref()%2) + 1; |
---|
| 453 | return f; |
---|
| 454 | } |
---|
| 455 | |
---|
[397] | 456 | ///Removes a pair of oppositely directed edges to the graph. |
---|
| 457 | void erase(Edge e) { |
---|
[782] | 458 | Edge f = opposite(e); |
---|
| 459 | sym_edge_maps.erase(e); |
---|
| 460 | sym_edge_maps.erase(f); |
---|
| 461 | ListGraph::erase(f); |
---|
[397] | 462 | ListGraph::erase(e); |
---|
[782] | 463 | } |
---|
| 464 | }; |
---|
[395] | 465 | |
---|
[400] | 466 | |
---|
[401] | 467 | ///A graph class containing only nodes. |
---|
[400] | 468 | |
---|
[401] | 469 | ///This class implements a graph structure without edges. |
---|
| 470 | ///The most useful application of this class is to be the node set of an |
---|
| 471 | ///\ref EdgeSet class. |
---|
[400] | 472 | /// |
---|
| 473 | ///It conforms to the graph interface documented under |
---|
[401] | 474 | ///the description of \ref GraphSkeleton with the exception that you cannot |
---|
| 475 | ///add (or delete) edges. The usual edge iterators are exists, but they are |
---|
| 476 | ///always \ref INVALID. |
---|
| 477 | ///\sa \ref GraphSkeleton |
---|
[508] | 478 | ///\sa \ref EdgeSet |
---|
[400] | 479 | class NodeSet { |
---|
| 480 | |
---|
| 481 | //Nodes are double linked. |
---|
| 482 | //The free nodes are only single linked using the "next" field. |
---|
| 483 | struct NodeT |
---|
| 484 | { |
---|
| 485 | int first_in,first_out; |
---|
| 486 | int prev, next; |
---|
| 487 | // NodeT() {} |
---|
| 488 | }; |
---|
| 489 | |
---|
| 490 | std::vector<NodeT> nodes; |
---|
| 491 | //The first node |
---|
| 492 | int first_node; |
---|
| 493 | //The first free node |
---|
| 494 | int first_free_node; |
---|
| 495 | |
---|
| 496 | public: |
---|
[782] | 497 | |
---|
| 498 | typedef NodeSet Graph; |
---|
[400] | 499 | |
---|
| 500 | class Node; |
---|
| 501 | class Edge; |
---|
| 502 | |
---|
| 503 | public: |
---|
| 504 | |
---|
| 505 | class NodeIt; |
---|
| 506 | class EdgeIt; |
---|
| 507 | class OutEdgeIt; |
---|
| 508 | class InEdgeIt; |
---|
| 509 | |
---|
[782] | 510 | CREATE_MAP_REGISTRIES; |
---|
| 511 | CREATE_MAPS(ArrayMapFactory); |
---|
[400] | 512 | |
---|
| 513 | public: |
---|
| 514 | |
---|
[408] | 515 | ///Default constructor |
---|
[782] | 516 | NodeSet() |
---|
| 517 | : nodes(), first_node(-1), first_free_node(-1) {} |
---|
[408] | 518 | ///Copy constructor |
---|
[782] | 519 | NodeSet(const NodeSet &_g) |
---|
| 520 | : nodes(_g.nodes), first_node(_g.first_node), |
---|
| 521 | first_free_node(_g.first_free_node) {} |
---|
[400] | 522 | |
---|
| 523 | int nodeNum() const { return nodes.size(); } //FIXME: What is this? |
---|
| 524 | int edgeNum() const { return 0; } //FIXME: What is this? |
---|
| 525 | |
---|
| 526 | ///\bug This function does something different than |
---|
| 527 | ///its name would suggests... |
---|
| 528 | int maxNodeId() const { return nodes.size(); } //FIXME: What is this? |
---|
| 529 | ///\bug This function does something different than |
---|
| 530 | ///its name would suggests... |
---|
| 531 | int maxEdgeId() const { return 0; } //FIXME: What is this? |
---|
| 532 | |
---|
| 533 | Node tail(Edge e) const { return INVALID; } |
---|
| 534 | Node head(Edge e) const { return INVALID; } |
---|
| 535 | |
---|
| 536 | NodeIt& first(NodeIt& v) const { |
---|
| 537 | v=NodeIt(*this); return v; } |
---|
| 538 | EdgeIt& first(EdgeIt& e) const { |
---|
| 539 | e=EdgeIt(*this); return e; } |
---|
| 540 | OutEdgeIt& first(OutEdgeIt& e, const Node v) const { |
---|
| 541 | e=OutEdgeIt(*this,v); return e; } |
---|
| 542 | InEdgeIt& first(InEdgeIt& e, const Node v) const { |
---|
| 543 | e=InEdgeIt(*this,v); return e; } |
---|
| 544 | |
---|
| 545 | int id(Node v) const { return v.n; } |
---|
| 546 | int id(Edge e) const { return -1; } |
---|
| 547 | |
---|
| 548 | /// Adds a new node to the graph. |
---|
| 549 | |
---|
| 550 | /// \todo It adds the nodes in a reversed order. |
---|
| 551 | /// (i.e. the lastly added node becomes the first.) |
---|
| 552 | Node addNode() { |
---|
| 553 | int n; |
---|
| 554 | |
---|
| 555 | if(first_free_node==-1) |
---|
| 556 | { |
---|
| 557 | n = nodes.size(); |
---|
| 558 | nodes.push_back(NodeT()); |
---|
| 559 | } |
---|
| 560 | else { |
---|
| 561 | n = first_free_node; |
---|
| 562 | first_free_node = nodes[n].next; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | nodes[n].next = first_node; |
---|
| 566 | if(first_node != -1) nodes[first_node].prev = n; |
---|
| 567 | first_node = n; |
---|
| 568 | nodes[n].prev = -1; |
---|
| 569 | |
---|
| 570 | nodes[n].first_in = nodes[n].first_out = -1; |
---|
| 571 | |
---|
| 572 | Node nn; nn.n=n; |
---|
| 573 | |
---|
| 574 | //Update dynamic maps |
---|
[782] | 575 | node_maps.add(nn); |
---|
[400] | 576 | |
---|
| 577 | return nn; |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | void erase(Node nn) { |
---|
| 581 | int n=nn.n; |
---|
| 582 | |
---|
| 583 | if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; |
---|
| 584 | if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; |
---|
| 585 | else first_node = nodes[n].next; |
---|
| 586 | |
---|
| 587 | nodes[n].next = first_free_node; |
---|
| 588 | first_free_node = n; |
---|
| 589 | |
---|
| 590 | //Update dynamic maps |
---|
[782] | 591 | node_maps.erase(nn); |
---|
[400] | 592 | } |
---|
| 593 | |
---|
[774] | 594 | |
---|
| 595 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 596 | { |
---|
| 597 | return INVALID; |
---|
| 598 | } |
---|
| 599 | |
---|
[400] | 600 | void clear() { |
---|
[782] | 601 | node_maps.clear(); |
---|
[400] | 602 | nodes.clear(); |
---|
| 603 | first_node = first_free_node = -1; |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | class Node { |
---|
| 607 | friend class NodeSet; |
---|
| 608 | template <typename T> friend class NodeMap; |
---|
| 609 | |
---|
| 610 | friend class Edge; |
---|
| 611 | friend class OutEdgeIt; |
---|
| 612 | friend class InEdgeIt; |
---|
| 613 | |
---|
| 614 | protected: |
---|
| 615 | int n; |
---|
| 616 | friend int NodeSet::id(Node v) const; |
---|
| 617 | Node(int nn) {n=nn;} |
---|
| 618 | public: |
---|
| 619 | Node() {} |
---|
| 620 | Node (Invalid i) { n=-1; } |
---|
| 621 | bool operator==(const Node i) const {return n==i.n;} |
---|
| 622 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
| 623 | bool operator<(const Node i) const {return n<i.n;} |
---|
| 624 | }; |
---|
| 625 | |
---|
| 626 | class NodeIt : public Node { |
---|
[774] | 627 | const NodeSet *G; |
---|
[400] | 628 | friend class NodeSet; |
---|
| 629 | public: |
---|
[579] | 630 | NodeIt() : Node() { } |
---|
[774] | 631 | NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { } |
---|
[579] | 632 | NodeIt(Invalid i) : Node(i) { } |
---|
[774] | 633 | NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { } |
---|
| 634 | NodeIt &operator++() { |
---|
| 635 | n=G->nodes[n].next; |
---|
| 636 | return *this; |
---|
| 637 | } |
---|
[400] | 638 | }; |
---|
| 639 | |
---|
| 640 | class Edge { |
---|
| 641 | //friend class NodeSet; |
---|
| 642 | //template <typename T> friend class EdgeMap; |
---|
| 643 | |
---|
| 644 | //template <typename T> friend class SymNodeSet::SymEdgeMap; |
---|
| 645 | //friend Edge SymNodeSet::opposite(Edge) const; |
---|
| 646 | |
---|
| 647 | // friend class Node; |
---|
| 648 | // friend class NodeIt; |
---|
| 649 | protected: |
---|
| 650 | //friend int NodeSet::id(Edge e) const; |
---|
| 651 | // Edge(int nn) {} |
---|
| 652 | public: |
---|
| 653 | Edge() { } |
---|
| 654 | Edge (Invalid) { } |
---|
| 655 | bool operator==(const Edge i) const {return true;} |
---|
| 656 | bool operator!=(const Edge i) const {return false;} |
---|
| 657 | bool operator<(const Edge i) const {return false;} |
---|
| 658 | ///\bug This is a workaround until somebody tells me how to |
---|
| 659 | ///make class \c SymNodeSet::SymEdgeMap friend of Edge |
---|
| 660 | // int idref() {return -1;} |
---|
| 661 | // int idref() const {return -1;} |
---|
| 662 | }; |
---|
| 663 | |
---|
| 664 | class EdgeIt : public Edge { |
---|
| 665 | //friend class NodeSet; |
---|
| 666 | public: |
---|
| 667 | EdgeIt(const NodeSet& G) : Edge() { } |
---|
[774] | 668 | EdgeIt(const NodeSet&, Edge) : Edge() { } |
---|
[400] | 669 | EdgeIt (Invalid i) : Edge(i) { } |
---|
| 670 | EdgeIt() : Edge() { } |
---|
| 671 | ///\bug This is a workaround until somebody tells me how to |
---|
| 672 | ///make class \c SymNodeSet::SymEdgeMap friend of Edge |
---|
| 673 | // int idref() {return -1;} |
---|
[774] | 674 | EdgeIt operator++() { return INVALID; } |
---|
[400] | 675 | }; |
---|
| 676 | |
---|
| 677 | class OutEdgeIt : public Edge { |
---|
| 678 | friend class NodeSet; |
---|
| 679 | public: |
---|
| 680 | OutEdgeIt() : Edge() { } |
---|
[774] | 681 | OutEdgeIt(const NodeSet&, Edge) : Edge() { } |
---|
[400] | 682 | OutEdgeIt (Invalid i) : Edge(i) { } |
---|
| 683 | OutEdgeIt(const NodeSet& G,const Node v) : Edge() {} |
---|
[774] | 684 | OutEdgeIt operator++() { return INVALID; } |
---|
[400] | 685 | }; |
---|
| 686 | |
---|
| 687 | class InEdgeIt : public Edge { |
---|
| 688 | friend class NodeSet; |
---|
| 689 | public: |
---|
| 690 | InEdgeIt() : Edge() { } |
---|
[774] | 691 | InEdgeIt(const NodeSet&, Edge) : Edge() { } |
---|
[400] | 692 | InEdgeIt (Invalid i) : Edge(i) { } |
---|
| 693 | InEdgeIt(const NodeSet& G,Node v) :Edge() {} |
---|
[774] | 694 | InEdgeIt operator++() { return INVALID; } |
---|
[400] | 695 | }; |
---|
| 696 | |
---|
| 697 | }; |
---|
| 698 | |
---|
| 699 | |
---|
| 700 | |
---|
[401] | 701 | ///Graph structure using a node set of another graph. |
---|
| 702 | |
---|
| 703 | ///This structure can be used to establish another graph over a node set |
---|
| 704 | /// of an existing one. The node iterator will go through the nodes of the |
---|
| 705 | /// original graph, and the NodeMap's of both graphs will convert to |
---|
| 706 | /// each other. |
---|
| 707 | /// |
---|
[404] | 708 | ///\warning Adding or deleting nodes from the graph is not safe if an |
---|
| 709 | ///\ref EdgeSet is currently attached to it! |
---|
| 710 | /// |
---|
| 711 | ///\todo Make it possible to add/delete edges from the base graph |
---|
| 712 | ///(and from \ref EdgeSet, as well) |
---|
| 713 | /// |
---|
[401] | 714 | ///\param GG The type of the graph which shares its node set with this class. |
---|
| 715 | ///Its interface must conform with \ref GraphSkeleton. |
---|
[400] | 716 | /// |
---|
| 717 | ///It conforms to the graph interface documented under |
---|
| 718 | ///the description of \ref GraphSkeleton. |
---|
| 719 | ///\sa \ref GraphSkeleton. |
---|
[401] | 720 | ///\sa \ref NodeSet. |
---|
[400] | 721 | template<typename GG> |
---|
| 722 | class EdgeSet { |
---|
| 723 | |
---|
| 724 | typedef GG NodeGraphType; |
---|
| 725 | |
---|
| 726 | NodeGraphType &G; |
---|
| 727 | |
---|
[515] | 728 | public: |
---|
[782] | 729 | |
---|
[400] | 730 | class Node; |
---|
[705] | 731 | class Edge; |
---|
| 732 | class OutEdgeIt; |
---|
| 733 | class InEdgeIt; |
---|
| 734 | class SymEdge; |
---|
[782] | 735 | |
---|
| 736 | typedef EdgeSet Graph; |
---|
| 737 | |
---|
[531] | 738 | int id(Node v) const; |
---|
| 739 | |
---|
| 740 | class Node : public NodeGraphType::Node { |
---|
| 741 | friend class EdgeSet; |
---|
| 742 | // template <typename T> friend class NodeMap; |
---|
| 743 | |
---|
| 744 | friend class Edge; |
---|
| 745 | friend class OutEdgeIt; |
---|
| 746 | friend class InEdgeIt; |
---|
| 747 | friend class SymEdge; |
---|
| 748 | |
---|
| 749 | public: |
---|
| 750 | friend int EdgeSet::id(Node v) const; |
---|
| 751 | // Node(int nn) {n=nn;} |
---|
| 752 | public: |
---|
| 753 | Node() : NodeGraphType::Node() {} |
---|
| 754 | Node (Invalid i) : NodeGraphType::Node(i) {} |
---|
| 755 | Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {} |
---|
| 756 | }; |
---|
| 757 | |
---|
| 758 | class NodeIt : public NodeGraphType::NodeIt { |
---|
| 759 | friend class EdgeSet; |
---|
| 760 | public: |
---|
| 761 | NodeIt() : NodeGraphType::NodeIt() { } |
---|
[774] | 762 | NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { } |
---|
[531] | 763 | NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {} |
---|
| 764 | NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { } |
---|
| 765 | NodeIt(const typename NodeGraphType::NodeIt &n) |
---|
| 766 | : NodeGraphType::NodeIt(n) {} |
---|
[579] | 767 | |
---|
[531] | 768 | operator Node() { return Node(*this);} |
---|
[774] | 769 | NodeIt &operator++() |
---|
| 770 | { this->NodeGraphType::NodeIt::operator++(); return *this;} |
---|
[531] | 771 | }; |
---|
[515] | 772 | |
---|
| 773 | private: |
---|
[400] | 774 | //Edges are double linked. |
---|
| 775 | //The free edges are only single linked using the "next_in" field. |
---|
| 776 | struct NodeT |
---|
| 777 | { |
---|
| 778 | int first_in,first_out; |
---|
| 779 | NodeT() : first_in(-1), first_out(-1) { } |
---|
| 780 | }; |
---|
| 781 | |
---|
| 782 | struct EdgeT |
---|
| 783 | { |
---|
| 784 | Node head, tail; |
---|
| 785 | int prev_in, prev_out; |
---|
| 786 | int next_in, next_out; |
---|
| 787 | }; |
---|
| 788 | |
---|
| 789 | |
---|
[515] | 790 | typename NodeGraphType::template NodeMap<NodeT> nodes; |
---|
[400] | 791 | |
---|
| 792 | std::vector<EdgeT> edges; |
---|
| 793 | //The first free edge |
---|
| 794 | int first_free_edge; |
---|
| 795 | |
---|
| 796 | public: |
---|
| 797 | |
---|
| 798 | class Node; |
---|
| 799 | class Edge; |
---|
| 800 | |
---|
| 801 | class NodeIt; |
---|
| 802 | class EdgeIt; |
---|
| 803 | class OutEdgeIt; |
---|
| 804 | class InEdgeIt; |
---|
[782] | 805 | |
---|
| 806 | |
---|
| 807 | CREATE_EDGE_MAP_REGISTRY; |
---|
| 808 | CREATE_EDGE_MAP_FACTORY(ArrayMapFactory); |
---|
| 809 | IMPORT_EDGE_MAP(EdgeMapFactory); |
---|
[400] | 810 | |
---|
| 811 | |
---|
| 812 | public: |
---|
| 813 | |
---|
[408] | 814 | ///Constructor |
---|
| 815 | |
---|
| 816 | ///Construates a new graph based on the nodeset of an existing one. |
---|
| 817 | ///\param _G the base graph. |
---|
| 818 | ///\todo It looks like a copy constructor, but it isn't. |
---|
[782] | 819 | EdgeSet(NodeGraphType &_G) |
---|
| 820 | : G(_G), nodes(_G), edges(), |
---|
| 821 | first_free_edge(-1) {} |
---|
[408] | 822 | ///Copy constructor |
---|
| 823 | |
---|
| 824 | ///Makes a copy of an EdgeSet. |
---|
| 825 | ///It will be based on the same graph. |
---|
[782] | 826 | EdgeSet(const EdgeSet &_g) |
---|
| 827 | : G(_g.G), nodes(_g.G), edges(_g.edges), |
---|
| 828 | first_free_edge(_g.first_free_edge) {} |
---|
[400] | 829 | |
---|
| 830 | int nodeNum() const { return G.nodeNum(); } //FIXME: What is this? |
---|
| 831 | int edgeNum() const { return edges.size(); } //FIXME: What is this? |
---|
| 832 | |
---|
| 833 | ///\bug This function does something different than |
---|
| 834 | ///its name would suggests... |
---|
| 835 | int maxNodeId() const { return G.maxNodeId(); } //FIXME: What is this? |
---|
| 836 | ///\bug This function does something different than |
---|
| 837 | ///its name would suggests... |
---|
| 838 | int maxEdgeId() const { return edges.size(); } //FIXME: What is this? |
---|
| 839 | |
---|
| 840 | Node tail(Edge e) const { return edges[e.n].tail; } |
---|
| 841 | Node head(Edge e) const { return edges[e.n].head; } |
---|
| 842 | |
---|
| 843 | NodeIt& first(NodeIt& v) const { |
---|
| 844 | v=NodeIt(*this); return v; } |
---|
| 845 | EdgeIt& first(EdgeIt& e) const { |
---|
| 846 | e=EdgeIt(*this); return e; } |
---|
| 847 | OutEdgeIt& first(OutEdgeIt& e, const Node v) const { |
---|
| 848 | e=OutEdgeIt(*this,v); return e; } |
---|
| 849 | InEdgeIt& first(InEdgeIt& e, const Node v) const { |
---|
| 850 | e=InEdgeIt(*this,v); return e; } |
---|
| 851 | |
---|
| 852 | int id(Edge e) const { return e.n; } |
---|
| 853 | |
---|
| 854 | /// Adds a new node to the graph. |
---|
[579] | 855 | Node addNode() { return G.addNode(); } |
---|
[400] | 856 | |
---|
| 857 | Edge addEdge(Node u, Node v) { |
---|
| 858 | int n; |
---|
| 859 | |
---|
| 860 | if(first_free_edge==-1) |
---|
| 861 | { |
---|
| 862 | n = edges.size(); |
---|
| 863 | edges.push_back(EdgeT()); |
---|
| 864 | } |
---|
| 865 | else { |
---|
| 866 | n = first_free_edge; |
---|
| 867 | first_free_edge = edges[n].next_in; |
---|
| 868 | } |
---|
| 869 | |
---|
[401] | 870 | edges[n].tail = u; edges[n].head = v; |
---|
[400] | 871 | |
---|
[401] | 872 | edges[n].next_out = nodes[u].first_out; |
---|
| 873 | if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n; |
---|
| 874 | edges[n].next_in = nodes[v].first_in; |
---|
| 875 | if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n; |
---|
[400] | 876 | edges[n].prev_in = edges[n].prev_out = -1; |
---|
| 877 | |
---|
[401] | 878 | nodes[u].first_out = nodes[v].first_in = n; |
---|
[400] | 879 | |
---|
| 880 | Edge e; e.n=n; |
---|
| 881 | |
---|
| 882 | //Update dynamic maps |
---|
[782] | 883 | edge_maps.add(e); |
---|
[400] | 884 | |
---|
| 885 | return e; |
---|
| 886 | } |
---|
| 887 | |
---|
[774] | 888 | /// Finds an edge between two nodes. |
---|
| 889 | |
---|
| 890 | /// Finds an edge from node \c u to node \c v. |
---|
| 891 | /// |
---|
| 892 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 893 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 894 | /// the next edge from \c u to \c v after \c prev. |
---|
| 895 | /// \return The found edge or INVALID if there is no such an edge. |
---|
| 896 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 897 | { |
---|
| 898 | int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out; |
---|
| 899 | while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out; |
---|
| 900 | prev.n=e; |
---|
| 901 | return prev; |
---|
| 902 | } |
---|
| 903 | |
---|
[400] | 904 | private: |
---|
| 905 | void eraseEdge(int n) { |
---|
| 906 | |
---|
| 907 | if(edges[n].next_in!=-1) |
---|
| 908 | edges[edges[n].next_in].prev_in = edges[n].prev_in; |
---|
| 909 | if(edges[n].prev_in!=-1) |
---|
| 910 | edges[edges[n].prev_in].next_in = edges[n].next_in; |
---|
| 911 | else nodes[edges[n].head].first_in = edges[n].next_in; |
---|
| 912 | |
---|
| 913 | if(edges[n].next_out!=-1) |
---|
| 914 | edges[edges[n].next_out].prev_out = edges[n].prev_out; |
---|
| 915 | if(edges[n].prev_out!=-1) |
---|
| 916 | edges[edges[n].prev_out].next_out = edges[n].next_out; |
---|
| 917 | else nodes[edges[n].tail].first_out = edges[n].next_out; |
---|
| 918 | |
---|
| 919 | edges[n].next_in = first_free_edge; |
---|
| 920 | first_free_edge = -1; |
---|
| 921 | |
---|
| 922 | //Update dynamic maps |
---|
[782] | 923 | Edge e; e.n = n; |
---|
| 924 | edge_maps.erase(e); |
---|
[400] | 925 | } |
---|
| 926 | |
---|
| 927 | public: |
---|
| 928 | |
---|
| 929 | // void erase(Node nn) { |
---|
| 930 | // int n=nn.n; |
---|
| 931 | // int m; |
---|
| 932 | // while((m=nodes[n].first_in)!=-1) eraseEdge(m); |
---|
| 933 | // while((m=nodes[n].first_out)!=-1) eraseEdge(m); |
---|
| 934 | // } |
---|
| 935 | |
---|
| 936 | void erase(Edge e) { eraseEdge(e.n); } |
---|
| 937 | |
---|
[579] | 938 | ///Clear all edges. (Doesn't clear the nodes!) |
---|
| 939 | void clear() { |
---|
[782] | 940 | edge_maps.clear(); |
---|
[579] | 941 | edges.clear(); |
---|
| 942 | first_free_edge=-1; |
---|
| 943 | } |
---|
| 944 | |
---|
| 945 | |
---|
[400] | 946 | class Edge { |
---|
[579] | 947 | public: |
---|
[400] | 948 | friend class EdgeSet; |
---|
| 949 | template <typename T> friend class EdgeMap; |
---|
| 950 | |
---|
| 951 | friend class Node; |
---|
| 952 | friend class NodeIt; |
---|
[579] | 953 | public: |
---|
[774] | 954 | ///\bug It should be at least protected |
---|
[579] | 955 | /// |
---|
| 956 | int n; |
---|
[400] | 957 | protected: |
---|
| 958 | friend int EdgeSet::id(Edge e) const; |
---|
| 959 | |
---|
| 960 | Edge(int nn) {n=nn;} |
---|
| 961 | public: |
---|
| 962 | Edge() { } |
---|
| 963 | Edge (Invalid) { n=-1; } |
---|
| 964 | bool operator==(const Edge i) const {return n==i.n;} |
---|
| 965 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
| 966 | bool operator<(const Edge i) const {return n<i.n;} |
---|
| 967 | ///\bug This is a workaround until somebody tells me how to |
---|
| 968 | ///make class \c SymEdgeSet::SymEdgeMap friend of Edge |
---|
| 969 | int &idref() {return n;} |
---|
| 970 | const int &idref() const {return n;} |
---|
| 971 | }; |
---|
| 972 | |
---|
| 973 | class EdgeIt : public Edge { |
---|
| 974 | friend class EdgeSet; |
---|
[579] | 975 | template <typename T> friend class EdgeMap; |
---|
| 976 | |
---|
[774] | 977 | const EdgeSet *G; |
---|
[400] | 978 | public: |
---|
[774] | 979 | EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) { |
---|
[503] | 980 | // typename NodeGraphType::Node m; |
---|
| 981 | NodeIt m; |
---|
[774] | 982 | for(G->first(m); |
---|
| 983 | m!=INVALID && G->nodes[m].first_in == -1; ++m); |
---|
| 984 | ///\bug AJJAJ! This is a non sense!!!!!!! |
---|
| 985 | this->n = m!=INVALID?-1:G->nodes[m].first_in; |
---|
[400] | 986 | } |
---|
[774] | 987 | EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } |
---|
[400] | 988 | EdgeIt (Invalid i) : Edge(i) { } |
---|
| 989 | EdgeIt() : Edge() { } |
---|
[774] | 990 | ///. |
---|
| 991 | |
---|
| 992 | ///\bug UNIMPLEMENTED!!!!! |
---|
| 993 | // |
---|
| 994 | EdgeIt &operator++() { |
---|
| 995 | return *this; |
---|
| 996 | } |
---|
| 997 | ///\bug This is a workaround until somebody tells me how to |
---|
[400] | 998 | ///make class \c SymEdgeSet::SymEdgeMap friend of Edge |
---|
[515] | 999 | int &idref() {return this->n;} |
---|
[400] | 1000 | }; |
---|
| 1001 | |
---|
| 1002 | class OutEdgeIt : public Edge { |
---|
[774] | 1003 | const EdgeSet *G; |
---|
[400] | 1004 | friend class EdgeSet; |
---|
| 1005 | public: |
---|
| 1006 | OutEdgeIt() : Edge() { } |
---|
| 1007 | OutEdgeIt (Invalid i) : Edge(i) { } |
---|
[774] | 1008 | OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } |
---|
[400] | 1009 | |
---|
[774] | 1010 | OutEdgeIt(const EdgeSet& _G,const Node v) : |
---|
| 1011 | Edge(_G.nodes[v].first_out), G(&_G) { } |
---|
[782] | 1012 | OutEdgeIt &operator++() { n = G->edges[n].next_out; return *this; } |
---|
[400] | 1013 | }; |
---|
| 1014 | |
---|
| 1015 | class InEdgeIt : public Edge { |
---|
[774] | 1016 | const EdgeSet *G; |
---|
[400] | 1017 | friend class EdgeSet; |
---|
| 1018 | public: |
---|
| 1019 | InEdgeIt() : Edge() { } |
---|
| 1020 | InEdgeIt (Invalid i) : Edge(i) { } |
---|
[774] | 1021 | InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } |
---|
| 1022 | InEdgeIt(const EdgeSet& _G,Node v) |
---|
| 1023 | : Edge(_G.nodes[v].first_in), G(&_G) { } |
---|
| 1024 | InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; } |
---|
[400] | 1025 | }; |
---|
| 1026 | |
---|
[782] | 1027 | |
---|
| 1028 | template <typename V> class NodeMap |
---|
| 1029 | : public NodeGraphType::template NodeMap<V> |
---|
[400] | 1030 | { |
---|
[579] | 1031 | //This is a must, the constructors need it. |
---|
[782] | 1032 | typedef typename NodeGraphType::template NodeMap<V> MapImpl; |
---|
| 1033 | typedef V Value; |
---|
[400] | 1034 | public: |
---|
[782] | 1035 | NodeMap() : MapImpl() {} |
---|
| 1036 | |
---|
| 1037 | NodeMap(const EdgeSet& graph) |
---|
| 1038 | : MapImpl(graph.G) { } |
---|
[400] | 1039 | |
---|
[782] | 1040 | NodeMap(const EdgeSet& graph, const Value& value) |
---|
| 1041 | : MapImpl(graph.G, value) { } |
---|
[400] | 1042 | |
---|
[782] | 1043 | NodeMap(const NodeMap& copy) |
---|
| 1044 | : MapImpl(static_cast<const MapImpl&>(copy)) {} |
---|
[400] | 1045 | |
---|
[782] | 1046 | template<typename CMap> |
---|
| 1047 | NodeMap(const CMap& copy) |
---|
| 1048 | : MapImpl(copy) { } |
---|
| 1049 | |
---|
| 1050 | NodeMap& operator=(const NodeMap& copy) { |
---|
| 1051 | MapImpl::operator=(static_cast<const MapImpl&>(copy)); |
---|
| 1052 | return *this; |
---|
[400] | 1053 | } |
---|
| 1054 | |
---|
[782] | 1055 | template <typename CMap> |
---|
| 1056 | NodeMap& operator=(const CMap& copy) { |
---|
| 1057 | MapImpl::operator=(copy); |
---|
[400] | 1058 | return *this; |
---|
| 1059 | } |
---|
[579] | 1060 | |
---|
[400] | 1061 | }; |
---|
| 1062 | }; |
---|
[406] | 1063 | |
---|
[579] | 1064 | template<typename GG> |
---|
| 1065 | inline int EdgeSet<GG>::id(Node v) const { return G.id(v); } |
---|
[531] | 1066 | |
---|
[406] | 1067 | /// @} |
---|
| 1068 | |
---|
[395] | 1069 | } //namespace hugo |
---|
| 1070 | |
---|
[405] | 1071 | #endif //HUGO_LIST_GRAPH_H |
---|