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