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