Docs are now divided into modules.
2 #ifndef HUGO_LIST_GRAPH_H
3 #define HUGO_LIST_GRAPH_H
10 #include "vector_map.h"
14 template <typename It>
17 for( ; it.valid(); ++it) { ++i; }
33 // template <typename T> class NodeMap;
34 // template <typename T> class EdgeMap;
36 // template <typename T> friend class NodeMap;
37 // template <typename T> friend class EdgeMap;
41 typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
42 NodeMapRegistry node_maps;
44 typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
45 EdgeMapRegistry edge_maps;
51 class NodeMap : public VectorMap<ListGraph, Node, NodeIt, T> {
53 NodeMap(ListGraph& g) : VectorMap<ListGraph, Node, NodeIt, T>(g.node_maps) {}
57 class EdgeMap : public VectorMap<ListGraph, Edge, EdgeIt, T> {
59 EdgeMap(ListGraph& g) : VectorMap<ListGraph, Edge, EdgeIt, T>(g.edge_maps) {}
68 node_item* _first_node;
69 node_item* _last_node;
72 friend class ListGraph;
73 template <typename T> friend class NodeMap;
79 friend class OutEdgeIt;
80 friend class InEdgeIt;
81 friend class SymEdgeIt;
82 friend std::ostream& operator<<(std::ostream& os, const Node& i);
83 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
86 edge_item* _first_out_edge;
87 edge_item* _last_out_edge;
88 edge_item* _first_in_edge;
89 edge_item* _last_in_edge;
90 node_item* _next_node;
91 node_item* _prev_node;
97 friend class ListGraph;
98 template <typename T> friend class EdgeMap;
104 friend class OutEdgeIt;
105 friend class InEdgeIt;
106 friend class SymEdgeIt;
107 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
112 edge_item* _next_out;
113 edge_item* _prev_out;
120 node_item* _add_node() {
121 node_item* p=new node_item;
123 p->_first_out_edge=0;
127 p->_prev_node=_last_node;
129 if (_last_node) _last_node->_next_node=p;
131 if (!_first_node) _first_node=p;
137 edge_item* _add_edge(node_item* _tail, node_item* _head) {
138 edge_item* e=new edge_item;
143 e->_prev_out=_tail->_last_out_edge;
144 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
145 _tail->_last_out_edge=e;
146 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
149 e->_prev_in=_head->_last_in_edge;
150 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
151 _head->_last_in_edge=e;
152 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
159 //deletes a node which has no out edge and no in edge
160 void _delete_node(node_item* v) {
161 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
162 _last_node=v->_prev_node;
163 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
164 _first_node=v->_next_node;
170 void _delete_edge(edge_item* e) {
171 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
172 (e->_tail)->_last_out_edge=e->_prev_out;
173 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
174 (e->_tail)->_first_out_edge=e->_next_out;
175 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
176 (e->_head)->_last_in_edge=e->_prev_in;
177 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
178 (e->_head)->_first_in_edge=e->_next_in;
184 void _set_tail(edge_item* e, node_item* _tail) {
185 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
186 (e->_tail)->_last_out_edge=e->_prev_out;
187 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
188 (e->_tail)->_first_out_edge=e->_next_out;
192 e->_prev_out=_tail->_last_out_edge;
193 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
194 _tail->_last_out_edge=e;
195 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
199 void _set_head(edge_item* e, node_item* _head) {
200 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
201 (e->_head)->_last_in_edge=e->_prev_in;
202 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
203 (e->_head)->_first_in_edge=e->_next_in;
207 e->_prev_in=_head->_last_in_edge;
208 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
209 _head->_last_in_edge=e;
210 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
216 /* default constructor */
218 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0),
219 edge_maps(*this), node_maps(*this) { }
222 while (first<NodeIt>().valid()) erase(first<NodeIt>());
225 int nodeNum() const { return _node_num; }
226 int edgeNum() const { return _edge_num; }
228 /* functions to construct iterators from the graph, or from each other */
230 //NodeIt firstNode() const { return NodeIt(*this); }
231 //EdgeIt firstEdge() const { return EdgeIt(*this); }
233 //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
234 //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
235 //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
236 Node tail(Edge e) const { return e.tailNode(); }
237 Node head(Edge e) const { return e.headNode(); }
239 Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
240 Node aNode(const InEdgeIt& e) const { return e.aNode(); }
241 Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
243 Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
244 Node bNode(const InEdgeIt& e) const { return e.bNode(); }
245 Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
247 //Node invalid_node() { return Node(); }
248 //Edge invalid_edge() { return Edge(); }
249 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
250 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
251 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
253 /* same methods in other style */
254 /* for experimental purpose */
256 NodeIt& /*getF*/first(NodeIt& v) const {
257 v=NodeIt(*this); return v; }
258 EdgeIt& /*getF*/first(EdgeIt& e) const {
259 e=EdgeIt(*this); return e; }
260 OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const {
261 e=OutEdgeIt(*this, v); return e; }
262 InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const {
263 e=InEdgeIt(*this, v); return e; }
264 SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const {
265 e=SymEdgeIt(*this, v); return e; }
266 //void getTail(Node& n, const Edge& e) const { n=tail(e); }
267 //void getHead(Node& n, const Edge& e) const { n=head(e); }
269 //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
270 //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
271 //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
272 //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
273 //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
274 //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
275 //void get_invalid(Node& n) { n=Node(); }
276 //void get_invalid(Edge& e) { e=Edge(); }
277 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
278 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
279 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
281 template< typename It >
288 template< typename It >
289 It first(Node v) const {
295 bool valid(Node n) const { return n.valid(); }
296 bool valid(Edge e) const { return e.valid(); }
298 template <typename It> It getNext(It it) const {
299 It tmp(it); return next(tmp); }
300 template <typename It> It& next(It& it) const { return ++it; }
303 /* for getting id's of graph objects */
304 /* these are important for the implementation of property vectors */
306 int id(Node v) const { return v.node->id; }
307 int id(Edge e) const { return e.edge->id; }
309 /* adding nodes and edges */
312 Node n = _add_node();
316 Edge addEdge(Node u, Node v) {
317 Edge e = _add_edge(u.node, v.node);
324 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
325 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
326 _delete_node(i.node);
331 _delete_edge(e.edge);
335 while (first<NodeIt>().valid()) erase(first<NodeIt>());
338 void setTail(Edge e, Node tail) {
339 _set_tail(e.edge, tail.node);
342 void setHead(Edge e, Node head) {
343 _set_head(e.edge, head.node);
346 /* stream operations, for testing purpose */
348 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
349 os << i.node->id; return os;
351 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
352 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
357 friend class ListGraph;
358 template <typename T> friend class NodeMap;
361 friend class OutEdgeIt;
362 friend class InEdgeIt;
363 friend class SymEdgeIt;
364 //public: //FIXME: It is required by op= of NodeIt
368 friend int ListGraph::id(Node v) const;
370 Node() /*: node(0)*/ { }
371 Node(const Invalid&) : node(0) { }
373 Node(node_item* _node) : node(_node) { }
374 bool valid() const { return (node); }
376 //void makeInvalid() { node=0; }
377 friend bool operator==(Node u, Node v) { return v.node==u.node; }
378 friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
379 friend std::ostream& operator<<(std::ostream& os, const Node& i);
382 class NodeIt : public Node {
383 friend class ListGraph;
385 public: //for everybody but marci
386 NodeIt(const ListGraph& G) : Node(G._first_node) { }
388 NodeIt() : Node() { }
389 NodeIt(const Invalid& i) : Node(i) { }
391 NodeIt(node_item* v) : Node(v) { }
392 NodeIt& operator++() { node=node->_next_node; return *this; }
394 // NodeIt& operator=(const Node& e)
395 // { node=e.node; return *this; }
399 friend class ListGraph;
400 template <typename T> friend class EdgeMap;
406 friend int ListGraph::id(Edge e) const;
408 Edge() /*: edge(0)*/ { }
409 Edge(const Invalid&) : edge(0) { }
412 Edge(edge_item* _edge) : edge(_edge) { }
413 bool valid() const { return (edge); }
415 //void makeInvalid() { edge=0; }
416 friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
417 friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
419 Node tailNode() const { return Node(edge->_tail); }
420 Node headNode() const { return Node(edge->_head); }
422 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
425 class EdgeIt : public Edge {
426 friend class ListGraph;
429 EdgeIt(const ListGraph& G) {
430 node_item* v=G._first_node;
431 if (v) edge=v->_first_out_edge; else edge=0;
432 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
435 EdgeIt() : Edge() { }
436 EdgeIt(const Invalid& i) : Edge(i) { }
438 EdgeIt(edge_item* _e) : Edge(_e) { }
439 EdgeIt& operator++() {
440 node_item* v=edge->_tail;
441 edge=edge->_next_out;
442 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
447 class OutEdgeIt : public Edge {
448 friend class ListGraph;
451 protected: //for alpar
452 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
454 OutEdgeIt() : Edge()/*, v(0)*/ { }
455 OutEdgeIt(const Invalid& i) : Edge(i) { }
456 OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
458 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
460 Node aNode() const { return Node(edge->_tail); }
461 Node bNode() const { return Node(edge->_head); }
464 class InEdgeIt : public Edge {
465 friend class ListGraph;
468 protected: //for alpar
469 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
471 InEdgeIt() : Edge()/*, v(0)*/ { }
472 InEdgeIt(const Invalid& i) : Edge(i) { }
473 InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
475 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
477 Node aNode() const { return Node(edge->_head); }
478 Node bNode() const { return Node(edge->_tail); }
481 class SymEdgeIt : public Edge {
482 friend class ListGraph;
483 bool out_or_in; //1 iff out, 0 iff in
487 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
489 edge=_v.node->_first_out_edge;
490 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
493 SymEdgeIt() : Edge() /*, v(0)*/ { }
494 SymEdgeIt(const Invalid& i) : Edge(i) { }
495 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
497 edge=_v.node->_first_out_edge;
498 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
501 SymEdgeIt& operator++() {
503 node_item* v=edge->_tail;
504 edge=edge->_next_out;
505 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
513 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
515 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
520 // template< typename T >
521 // T ListGraph::first() const {
522 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
527 // ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
528 // return firstNode();
532 // ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
533 // return firstEdge();
536 // template< typename T >
537 // T ListGraph::first(ListGraph::Node v) const {
538 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl;
543 // ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
544 // return firstOutEdge(v);
548 // ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
549 // return firstInEdge(v);
553 // ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
554 // return firstSymEdge(v);
560 #endif //HUGO_LIST_GRAPH_H