| 1 | // -*- c++ -*- |
|---|
| 2 | #ifndef HUGO_LIST_GRAPH_H |
|---|
| 3 | #define HUGO_LIST_GRAPH_H |
|---|
| 4 | |
|---|
| 5 | #include <iostream> |
|---|
| 6 | #include <vector> |
|---|
| 7 | |
|---|
| 8 | #include "invalid.h" |
|---|
| 9 | |
|---|
| 10 | #include "vector_map_factory.h" |
|---|
| 11 | |
|---|
| 12 | namespace hugo { |
|---|
| 13 | |
|---|
| 14 | template <typename It> |
|---|
| 15 | int count(It it) { |
|---|
| 16 | int i=0; |
|---|
| 17 | for( ; it.valid(); ++it) { ++i; } |
|---|
| 18 | return i; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | class ListGraph { |
|---|
| 22 | class node_item; |
|---|
| 23 | class edge_item; |
|---|
| 24 | public: |
|---|
| 25 | class Node; |
|---|
| 26 | class NodeIt; |
|---|
| 27 | class Edge; |
|---|
| 28 | class EdgeIt; |
|---|
| 29 | class OutEdgeIt; |
|---|
| 30 | class InEdgeIt; |
|---|
| 31 | class SymEdgeIt; |
|---|
| 32 | |
|---|
| 33 | // template <typename T> class NodeMap; |
|---|
| 34 | // template <typename T> class EdgeMap; |
|---|
| 35 | private: |
|---|
| 36 | // template <typename T> friend class NodeMap; |
|---|
| 37 | // template <typename T> friend class EdgeMap; |
|---|
| 38 | |
|---|
| 39 | private: |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | public: |
|---|
| 43 | |
|---|
| 44 | typedef MapBase<ListGraph, Node, NodeIt> NodeMapBase; |
|---|
| 45 | typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry; |
|---|
| 46 | typedef VectorMapFactory<ListGraph, Node, NodeIt> NodeMapFactory; |
|---|
| 47 | NodeMapRegistry node_maps; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | typedef MapBase<ListGraph, Edge, EdgeIt> EdgeMapBase; |
|---|
| 52 | typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry; |
|---|
| 53 | typedef VectorMapFactory<ListGraph, Edge, EdgeIt> EdgeMapFactory; |
|---|
| 54 | EdgeMapRegistry edge_maps; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | int node_id; |
|---|
| 58 | int edge_id; |
|---|
| 59 | int _node_num; |
|---|
| 60 | int _edge_num; |
|---|
| 61 | |
|---|
| 62 | node_item* _first_node; |
|---|
| 63 | node_item* _last_node; |
|---|
| 64 | |
|---|
| 65 | class node_item { |
|---|
| 66 | friend class ListGraph; |
|---|
| 67 | template <typename T> friend class NodeMap; |
|---|
| 68 | |
|---|
| 69 | friend class Node; |
|---|
| 70 | friend class NodeIt; |
|---|
| 71 | friend class Edge; |
|---|
| 72 | friend class EdgeIt; |
|---|
| 73 | friend class OutEdgeIt; |
|---|
| 74 | friend class InEdgeIt; |
|---|
| 75 | friend class SymEdgeIt; |
|---|
| 76 | friend std::ostream& operator<<(std::ostream& os, const Node& i); |
|---|
| 77 | friend std::ostream& operator<<(std::ostream& os, const Edge& i); |
|---|
| 78 | //ListGraph* G; |
|---|
| 79 | int id; |
|---|
| 80 | edge_item* _first_out_edge; |
|---|
| 81 | edge_item* _last_out_edge; |
|---|
| 82 | edge_item* _first_in_edge; |
|---|
| 83 | edge_item* _last_in_edge; |
|---|
| 84 | node_item* _next_node; |
|---|
| 85 | node_item* _prev_node; |
|---|
| 86 | public: |
|---|
| 87 | node_item() { } |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | class edge_item { |
|---|
| 91 | friend class ListGraph; |
|---|
| 92 | template <typename T> friend class EdgeMap; |
|---|
| 93 | |
|---|
| 94 | friend class Node; |
|---|
| 95 | friend class NodeIt; |
|---|
| 96 | friend class Edge; |
|---|
| 97 | friend class EdgeIt; |
|---|
| 98 | friend class OutEdgeIt; |
|---|
| 99 | friend class InEdgeIt; |
|---|
| 100 | friend class SymEdgeIt; |
|---|
| 101 | friend std::ostream& operator<<(std::ostream& os, const Edge& i); |
|---|
| 102 | //ListGraph* G; |
|---|
| 103 | int id; |
|---|
| 104 | node_item* _tail; |
|---|
| 105 | node_item* _head; |
|---|
| 106 | edge_item* _next_out; |
|---|
| 107 | edge_item* _prev_out; |
|---|
| 108 | edge_item* _next_in; |
|---|
| 109 | edge_item* _prev_in; |
|---|
| 110 | public: |
|---|
| 111 | edge_item() { } |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | node_item* _add_node() { |
|---|
| 115 | node_item* p=new node_item; |
|---|
| 116 | p->id=node_id++; |
|---|
| 117 | p->_first_out_edge=0; |
|---|
| 118 | p->_last_out_edge=0; |
|---|
| 119 | p->_first_in_edge=0; |
|---|
| 120 | p->_last_in_edge=0; |
|---|
| 121 | p->_prev_node=_last_node; |
|---|
| 122 | p->_next_node=0; |
|---|
| 123 | if (_last_node) _last_node->_next_node=p; |
|---|
| 124 | _last_node=p; |
|---|
| 125 | if (!_first_node) _first_node=p; |
|---|
| 126 | |
|---|
| 127 | ++_node_num; |
|---|
| 128 | return p; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | edge_item* _add_edge(node_item* _tail, node_item* _head) { |
|---|
| 132 | edge_item* e=new edge_item; |
|---|
| 133 | e->id=edge_id++; |
|---|
| 134 | e->_tail=_tail; |
|---|
| 135 | e->_head=_head; |
|---|
| 136 | |
|---|
| 137 | e->_prev_out=_tail->_last_out_edge; |
|---|
| 138 | if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; |
|---|
| 139 | _tail->_last_out_edge=e; |
|---|
| 140 | if (!_tail->_first_out_edge) _tail->_first_out_edge=e; |
|---|
| 141 | e->_next_out=0; |
|---|
| 142 | |
|---|
| 143 | e->_prev_in=_head->_last_in_edge; |
|---|
| 144 | if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; |
|---|
| 145 | _head->_last_in_edge=e; |
|---|
| 146 | if (!_head->_first_in_edge) { _head->_first_in_edge=e; } |
|---|
| 147 | e->_next_in=0; |
|---|
| 148 | |
|---|
| 149 | ++_edge_num; |
|---|
| 150 | return e; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | //deletes a node which has no out edge and no in edge |
|---|
| 154 | void _delete_node(node_item* v) { |
|---|
| 155 | if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else |
|---|
| 156 | _last_node=v->_prev_node; |
|---|
| 157 | if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else |
|---|
| 158 | _first_node=v->_next_node; |
|---|
| 159 | |
|---|
| 160 | delete v; |
|---|
| 161 | --_node_num; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | void _delete_edge(edge_item* e) { |
|---|
| 165 | if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else |
|---|
| 166 | (e->_tail)->_last_out_edge=e->_prev_out; |
|---|
| 167 | if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else |
|---|
| 168 | (e->_tail)->_first_out_edge=e->_next_out; |
|---|
| 169 | if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else |
|---|
| 170 | (e->_head)->_last_in_edge=e->_prev_in; |
|---|
| 171 | if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else |
|---|
| 172 | (e->_head)->_first_in_edge=e->_next_in; |
|---|
| 173 | |
|---|
| 174 | delete e; |
|---|
| 175 | --_edge_num; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | void _set_tail(edge_item* e, node_item* _tail) { |
|---|
| 179 | if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else |
|---|
| 180 | (e->_tail)->_last_out_edge=e->_prev_out; |
|---|
| 181 | if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else |
|---|
| 182 | (e->_tail)->_first_out_edge=e->_next_out; |
|---|
| 183 | |
|---|
| 184 | e->_tail=_tail; |
|---|
| 185 | |
|---|
| 186 | e->_prev_out=_tail->_last_out_edge; |
|---|
| 187 | if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; |
|---|
| 188 | _tail->_last_out_edge=e; |
|---|
| 189 | if (!_tail->_first_out_edge) _tail->_first_out_edge=e; |
|---|
| 190 | e->_next_out=0; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void _set_head(edge_item* e, node_item* _head) { |
|---|
| 194 | if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else |
|---|
| 195 | (e->_head)->_last_in_edge=e->_prev_in; |
|---|
| 196 | if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else |
|---|
| 197 | (e->_head)->_first_in_edge=e->_next_in; |
|---|
| 198 | |
|---|
| 199 | e->_head=_head; |
|---|
| 200 | |
|---|
| 201 | e->_prev_in=_head->_last_in_edge; |
|---|
| 202 | if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; |
|---|
| 203 | _head->_last_in_edge=e; |
|---|
| 204 | if (!_head->_first_in_edge) { _head->_first_in_edge=e; } |
|---|
| 205 | e->_next_in=0; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | public: |
|---|
| 209 | |
|---|
| 210 | /* default constructor */ |
|---|
| 211 | |
|---|
| 212 | ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0){ } |
|---|
| 213 | |
|---|
| 214 | ~ListGraph() { |
|---|
| 215 | while (first<NodeIt>().valid()) erase(first<NodeIt>()); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | int nodeNum() const { return _node_num; } |
|---|
| 219 | int edgeNum() const { return _edge_num; } |
|---|
| 220 | |
|---|
| 221 | /* functions to construct iterators from the graph, or from each other */ |
|---|
| 222 | |
|---|
| 223 | //NodeIt firstNode() const { return NodeIt(*this); } |
|---|
| 224 | //EdgeIt firstEdge() const { return EdgeIt(*this); } |
|---|
| 225 | |
|---|
| 226 | //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); } |
|---|
| 227 | //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); } |
|---|
| 228 | //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); } |
|---|
| 229 | Node tail(Edge e) const { return e.tailNode(); } |
|---|
| 230 | Node head(Edge e) const { return e.headNode(); } |
|---|
| 231 | |
|---|
| 232 | Node aNode(const OutEdgeIt& e) const { return e.aNode(); } |
|---|
| 233 | Node aNode(const InEdgeIt& e) const { return e.aNode(); } |
|---|
| 234 | Node aNode(const SymEdgeIt& e) const { return e.aNode(); } |
|---|
| 235 | |
|---|
| 236 | Node bNode(const OutEdgeIt& e) const { return e.bNode(); } |
|---|
| 237 | Node bNode(const InEdgeIt& e) const { return e.bNode(); } |
|---|
| 238 | Node bNode(const SymEdgeIt& e) const { return e.bNode(); } |
|---|
| 239 | |
|---|
| 240 | //Node invalid_node() { return Node(); } |
|---|
| 241 | //Edge invalid_edge() { return Edge(); } |
|---|
| 242 | //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); } |
|---|
| 243 | //InEdgeIt invalid_in_edge() { return InEdgeIt(); } |
|---|
| 244 | //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); } |
|---|
| 245 | |
|---|
| 246 | /* same methods in other style */ |
|---|
| 247 | /* for experimental purpose */ |
|---|
| 248 | |
|---|
| 249 | NodeIt& /*getF*/first(NodeIt& v) const { |
|---|
| 250 | v=NodeIt(*this); return v; } |
|---|
| 251 | EdgeIt& /*getF*/first(EdgeIt& e) const { |
|---|
| 252 | e=EdgeIt(*this); return e; } |
|---|
| 253 | OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const { |
|---|
| 254 | e=OutEdgeIt(*this, v); return e; } |
|---|
| 255 | InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const { |
|---|
| 256 | e=InEdgeIt(*this, v); return e; } |
|---|
| 257 | SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const { |
|---|
| 258 | e=SymEdgeIt(*this, v); return e; } |
|---|
| 259 | //void getTail(Node& n, const Edge& e) const { n=tail(e); } |
|---|
| 260 | //void getHead(Node& n, const Edge& e) const { n=head(e); } |
|---|
| 261 | |
|---|
| 262 | //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); } |
|---|
| 263 | //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); } |
|---|
| 264 | //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); } |
|---|
| 265 | //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); } |
|---|
| 266 | //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); } |
|---|
| 267 | //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); } |
|---|
| 268 | //void get_invalid(Node& n) { n=Node(); } |
|---|
| 269 | //void get_invalid(Edge& e) { e=Edge(); } |
|---|
| 270 | //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); } |
|---|
| 271 | //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); } |
|---|
| 272 | //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); } |
|---|
| 273 | |
|---|
| 274 | template< typename It > |
|---|
| 275 | It first() const { |
|---|
| 276 | It e; |
|---|
| 277 | /*getF*/first(e); |
|---|
| 278 | return e; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | template< typename It > |
|---|
| 282 | It first(Node v) const { |
|---|
| 283 | It e; |
|---|
| 284 | /*getF*/first(e, v); |
|---|
| 285 | return e; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | bool valid(Node n) const { return n.valid(); } |
|---|
| 289 | bool valid(Edge e) const { return e.valid(); } |
|---|
| 290 | |
|---|
| 291 | template <typename It> It getNext(It it) const { |
|---|
| 292 | It tmp(it); return next(tmp); } |
|---|
| 293 | template <typename It> It& next(It& it) const { return ++it; } |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | /* for getting id's of graph objects */ |
|---|
| 297 | /* these are important for the implementation of property vectors */ |
|---|
| 298 | |
|---|
| 299 | int id(Node v) const { return v.node->id; } |
|---|
| 300 | int id(Edge e) const { return e.edge->id; } |
|---|
| 301 | |
|---|
| 302 | /* adding nodes and edges */ |
|---|
| 303 | |
|---|
| 304 | Node addNode() { |
|---|
| 305 | Node n = _add_node(); |
|---|
| 306 | node_maps.add(n); |
|---|
| 307 | return n; |
|---|
| 308 | } |
|---|
| 309 | Edge addEdge(Node u, Node v) { |
|---|
| 310 | Edge e = _add_edge(u.node, v.node); |
|---|
| 311 | edge_maps.add(e); |
|---|
| 312 | return e; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | void erase(Node i) { |
|---|
| 316 | node_maps.erase(i); |
|---|
| 317 | while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i)); |
|---|
| 318 | while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i)); |
|---|
| 319 | _delete_node(i.node); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | void erase(Edge e) { |
|---|
| 323 | edge_maps.erase(e); |
|---|
| 324 | _delete_edge(e.edge); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | void clear() { |
|---|
| 328 | while (first<NodeIt>().valid()) erase(first<NodeIt>()); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | void setTail(Edge e, Node tail) { |
|---|
| 332 | _set_tail(e.edge, tail.node); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | void setHead(Edge e, Node head) { |
|---|
| 336 | _set_head(e.edge, head.node); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | /* stream operations, for testing purpose */ |
|---|
| 340 | |
|---|
| 341 | friend std::ostream& operator<<(std::ostream& os, const Node& i) { |
|---|
| 342 | os << i.node->id; return os; |
|---|
| 343 | } |
|---|
| 344 | friend std::ostream& operator<<(std::ostream& os, const Edge& i) { |
|---|
| 345 | os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; |
|---|
| 346 | return os; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | class Node { |
|---|
| 350 | friend class ListGraph; |
|---|
| 351 | template <typename T> friend class NodeMap; |
|---|
| 352 | |
|---|
| 353 | friend class Edge; |
|---|
| 354 | friend class OutEdgeIt; |
|---|
| 355 | friend class InEdgeIt; |
|---|
| 356 | friend class SymEdgeIt; |
|---|
| 357 | //public: //FIXME: It is required by op= of NodeIt |
|---|
| 358 | protected: |
|---|
| 359 | node_item* node; |
|---|
| 360 | protected: |
|---|
| 361 | friend int ListGraph::id(Node v) const; |
|---|
| 362 | public: |
|---|
| 363 | Node() /*: node(0)*/ { } |
|---|
| 364 | Node(const Invalid&) : node(0) { } |
|---|
| 365 | protected: |
|---|
| 366 | Node(node_item* _node) : node(_node) { } |
|---|
| 367 | bool valid() const { return (node); } |
|---|
| 368 | public: |
|---|
| 369 | //void makeInvalid() { node=0; } |
|---|
| 370 | friend bool operator==(Node u, Node v) { return v.node==u.node; } |
|---|
| 371 | friend bool operator!=(Node u, Node v) { return v.node!=u.node; } |
|---|
| 372 | friend std::ostream& operator<<(std::ostream& os, const Node& i); |
|---|
| 373 | }; |
|---|
| 374 | |
|---|
| 375 | class NodeIt : public Node { |
|---|
| 376 | friend class ListGraph; |
|---|
| 377 | //protected: |
|---|
| 378 | public: //for everybody but marci |
|---|
| 379 | NodeIt(const ListGraph& G) : Node(G._first_node) { } |
|---|
| 380 | public: |
|---|
| 381 | NodeIt() : Node() { } |
|---|
| 382 | NodeIt(const Invalid& i) : Node(i) { } |
|---|
| 383 | protected: |
|---|
| 384 | NodeIt(node_item* v) : Node(v) { } |
|---|
| 385 | NodeIt& operator++() { node=node->_next_node; return *this; } |
|---|
| 386 | //FIXME:: |
|---|
| 387 | // NodeIt& operator=(const Node& e) |
|---|
| 388 | // { node=e.node; return *this; } |
|---|
| 389 | }; |
|---|
| 390 | |
|---|
| 391 | class Edge { |
|---|
| 392 | friend class ListGraph; |
|---|
| 393 | template <typename T> friend class EdgeMap; |
|---|
| 394 | |
|---|
| 395 | friend class Node; |
|---|
| 396 | friend class NodeIt; |
|---|
| 397 | protected: |
|---|
| 398 | edge_item* edge; |
|---|
| 399 | friend int ListGraph::id(Edge e) const; |
|---|
| 400 | public: |
|---|
| 401 | Edge() /*: edge(0)*/ { } |
|---|
| 402 | Edge(const Invalid&) : edge(0) { } |
|---|
| 403 | //Edge() { } |
|---|
| 404 | protected: |
|---|
| 405 | Edge(edge_item* _edge) : edge(_edge) { } |
|---|
| 406 | bool valid() const { return (edge); } |
|---|
| 407 | public: |
|---|
| 408 | //void makeInvalid() { edge=0; } |
|---|
| 409 | friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; } |
|---|
| 410 | friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; } |
|---|
| 411 | protected: |
|---|
| 412 | Node tailNode() const { return Node(edge->_tail); } |
|---|
| 413 | Node headNode() const { return Node(edge->_head); } |
|---|
| 414 | public: |
|---|
| 415 | friend std::ostream& operator<<(std::ostream& os, const Edge& i); |
|---|
| 416 | }; |
|---|
| 417 | |
|---|
| 418 | class EdgeIt : public Edge { |
|---|
| 419 | friend class ListGraph; |
|---|
| 420 | //protected: |
|---|
| 421 | public: //for alpar |
|---|
| 422 | EdgeIt(const ListGraph& G) { |
|---|
| 423 | node_item* v=G._first_node; |
|---|
| 424 | if (v) edge=v->_first_out_edge; else edge=0; |
|---|
| 425 | while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } |
|---|
| 426 | } |
|---|
| 427 | public: |
|---|
| 428 | EdgeIt() : Edge() { } |
|---|
| 429 | EdgeIt(const Invalid& i) : Edge(i) { } |
|---|
| 430 | protected: |
|---|
| 431 | EdgeIt(edge_item* _e) : Edge(_e) { } |
|---|
| 432 | EdgeIt& operator++() { |
|---|
| 433 | node_item* v=edge->_tail; |
|---|
| 434 | edge=edge->_next_out; |
|---|
| 435 | while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } |
|---|
| 436 | return *this; |
|---|
| 437 | } |
|---|
| 438 | }; |
|---|
| 439 | |
|---|
| 440 | class OutEdgeIt : public Edge { |
|---|
| 441 | friend class ListGraph; |
|---|
| 442 | //node_item* v; |
|---|
| 443 | //protected: |
|---|
| 444 | protected: //for alpar |
|---|
| 445 | OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } |
|---|
| 446 | public: |
|---|
| 447 | OutEdgeIt() : Edge()/*, v(0)*/ { } |
|---|
| 448 | OutEdgeIt(const Invalid& i) : Edge(i) { } |
|---|
| 449 | OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } |
|---|
| 450 | protected: |
|---|
| 451 | OutEdgeIt& operator++() { edge=edge->_next_out; return *this; } |
|---|
| 452 | protected: |
|---|
| 453 | Node aNode() const { return Node(edge->_tail); } |
|---|
| 454 | Node bNode() const { return Node(edge->_head); } |
|---|
| 455 | }; |
|---|
| 456 | |
|---|
| 457 | class InEdgeIt : public Edge { |
|---|
| 458 | friend class ListGraph; |
|---|
| 459 | //node_item* v; |
|---|
| 460 | //protected: |
|---|
| 461 | protected: //for alpar |
|---|
| 462 | InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } |
|---|
| 463 | public: |
|---|
| 464 | InEdgeIt() : Edge()/*, v(0)*/ { } |
|---|
| 465 | InEdgeIt(const Invalid& i) : Edge(i) { } |
|---|
| 466 | InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } |
|---|
| 467 | protected: |
|---|
| 468 | InEdgeIt& operator++() { edge=edge->_next_in; return *this; } |
|---|
| 469 | protected: |
|---|
| 470 | Node aNode() const { return Node(edge->_head); } |
|---|
| 471 | Node bNode() const { return Node(edge->_tail); } |
|---|
| 472 | }; |
|---|
| 473 | |
|---|
| 474 | class SymEdgeIt : public Edge { |
|---|
| 475 | friend class ListGraph; |
|---|
| 476 | bool out_or_in; //1 iff out, 0 iff in |
|---|
| 477 | //node_item* v; |
|---|
| 478 | //protected: |
|---|
| 479 | public: //for alpar |
|---|
| 480 | SymEdgeIt(const Node& _v) /*: v(_v.node)*/ { |
|---|
| 481 | out_or_in=1; |
|---|
| 482 | edge=_v.node->_first_out_edge; |
|---|
| 483 | if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } |
|---|
| 484 | } |
|---|
| 485 | public: |
|---|
| 486 | SymEdgeIt() : Edge() /*, v(0)*/ { } |
|---|
| 487 | SymEdgeIt(const Invalid& i) : Edge(i) { } |
|---|
| 488 | SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { |
|---|
| 489 | out_or_in=1; |
|---|
| 490 | edge=_v.node->_first_out_edge; |
|---|
| 491 | if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } |
|---|
| 492 | } |
|---|
| 493 | protected: |
|---|
| 494 | SymEdgeIt& operator++() { |
|---|
| 495 | if (out_or_in) { |
|---|
| 496 | node_item* v=edge->_tail; |
|---|
| 497 | edge=edge->_next_out; |
|---|
| 498 | if (!edge) { out_or_in=0; edge=v->_first_in_edge; } |
|---|
| 499 | } else { |
|---|
| 500 | edge=edge->_next_in; |
|---|
| 501 | } |
|---|
| 502 | return *this; |
|---|
| 503 | } |
|---|
| 504 | protected: |
|---|
| 505 | Node aNode() const { |
|---|
| 506 | return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); } |
|---|
| 507 | Node bNode() const { |
|---|
| 508 | return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); } |
|---|
| 509 | }; |
|---|
| 510 | |
|---|
| 511 | }; |
|---|
| 512 | |
|---|
| 513 | // template< typename T > |
|---|
| 514 | // T ListGraph::first() const { |
|---|
| 515 | // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl; |
|---|
| 516 | // return T(); |
|---|
| 517 | // } |
|---|
| 518 | |
|---|
| 519 | // template<> |
|---|
| 520 | // ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const { |
|---|
| 521 | // return firstNode(); |
|---|
| 522 | // } |
|---|
| 523 | |
|---|
| 524 | // template<> |
|---|
| 525 | // ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const { |
|---|
| 526 | // return firstEdge(); |
|---|
| 527 | // } |
|---|
| 528 | |
|---|
| 529 | // template< typename T > |
|---|
| 530 | // T ListGraph::first(ListGraph::Node v) const { |
|---|
| 531 | // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl; |
|---|
| 532 | // return T(); |
|---|
| 533 | // } |
|---|
| 534 | |
|---|
| 535 | // template<> |
|---|
| 536 | // ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const { |
|---|
| 537 | // return firstOutEdge(v); |
|---|
| 538 | // } |
|---|
| 539 | |
|---|
| 540 | // template<> |
|---|
| 541 | // ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const { |
|---|
| 542 | // return firstInEdge(v); |
|---|
| 543 | // } |
|---|
| 544 | |
|---|
| 545 | // template<> |
|---|
| 546 | // ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const { |
|---|
| 547 | // return firstSymEdge(v); |
|---|
| 548 | // } |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | } //namespace hugo |
|---|
| 552 | |
|---|
| 553 | #endif //HUGO_LIST_GRAPH_H |
|---|