2 #ifndef HUGO_LIST_GRAPH_H
3 #define HUGO_LIST_GRAPH_H
10 #include "map_registry.h"
11 #include "array_map_factory.h"
13 #include "map_defines.h"
17 template <typename It>
20 for( ; it.valid(); ++it) { ++i; }
36 typedef ListGraph Graph;
39 CREATE_MAPS(ArrayMapFactory)
48 node_item* _first_node;
49 node_item* _last_node;
52 friend class ListGraph;
53 template <typename T> friend class NodeMap;
59 friend class OutEdgeIt;
60 friend class InEdgeIt;
61 friend class SymEdgeIt;
62 friend std::ostream& operator<<(std::ostream& os, const Node& i);
63 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
66 edge_item* _first_out_edge;
67 edge_item* _last_out_edge;
68 edge_item* _first_in_edge;
69 edge_item* _last_in_edge;
70 node_item* _next_node;
71 node_item* _prev_node;
77 friend class ListGraph;
78 template <typename T> friend class EdgeMap;
84 friend class OutEdgeIt;
85 friend class InEdgeIt;
86 friend class SymEdgeIt;
87 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
100 node_item* _add_node() {
101 node_item* p=new node_item;
103 p->_first_out_edge=0;
107 p->_prev_node=_last_node;
109 if (_last_node) _last_node->_next_node=p;
111 if (!_first_node) _first_node=p;
117 edge_item* _add_edge(node_item* _tail, node_item* _head) {
118 edge_item* e=new edge_item;
123 e->_prev_out=_tail->_last_out_edge;
124 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
125 _tail->_last_out_edge=e;
126 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
129 e->_prev_in=_head->_last_in_edge;
130 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
131 _head->_last_in_edge=e;
132 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
139 //deletes a node which has no out edge and no in edge
140 void _delete_node(node_item* v) {
141 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
142 _last_node=v->_prev_node;
143 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
144 _first_node=v->_next_node;
150 void _delete_edge(edge_item* e) {
151 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
152 (e->_tail)->_last_out_edge=e->_prev_out;
153 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
154 (e->_tail)->_first_out_edge=e->_next_out;
155 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
156 (e->_head)->_last_in_edge=e->_prev_in;
157 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
158 (e->_head)->_first_in_edge=e->_next_in;
164 void _set_tail(edge_item* e, node_item* _tail) {
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;
172 e->_prev_out=_tail->_last_out_edge;
173 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
174 _tail->_last_out_edge=e;
175 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
179 void _set_head(edge_item* e, node_item* _head) {
180 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
181 (e->_head)->_last_in_edge=e->_prev_in;
182 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
183 (e->_head)->_first_in_edge=e->_next_in;
187 e->_prev_in=_head->_last_in_edge;
188 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
189 _head->_last_in_edge=e;
190 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
196 /* default constructor */
198 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0){ }
201 while (first<NodeIt>().valid()) erase(first<NodeIt>());
204 int nodeNum() const { return _node_num; }
205 int edgeNum() const { return _edge_num; }
207 /* functions to construct iterators from the graph, or from each other */
209 //NodeIt firstNode() const { return NodeIt(*this); }
210 //EdgeIt firstEdge() const { return EdgeIt(*this); }
212 //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
213 //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
214 //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
215 Node tail(Edge e) const { return e.tailNode(); }
216 Node head(Edge e) const { return e.headNode(); }
218 Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
219 Node aNode(const InEdgeIt& e) const { return e.aNode(); }
220 Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
222 Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
223 Node bNode(const InEdgeIt& e) const { return e.bNode(); }
224 Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
226 //Node invalid_node() { return Node(); }
227 //Edge invalid_edge() { return Edge(); }
228 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
229 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
230 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
232 /* same methods in other style */
233 /* for experimental purpose */
235 NodeIt& /*getF*/first(NodeIt& v) const {
236 v=NodeIt(*this); return v; }
237 EdgeIt& /*getF*/first(EdgeIt& e) const {
238 e=EdgeIt(*this); return e; }
239 OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const {
240 e=OutEdgeIt(*this, v); return e; }
241 InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const {
242 e=InEdgeIt(*this, v); return e; }
243 SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const {
244 e=SymEdgeIt(*this, v); return e; }
245 //void getTail(Node& n, const Edge& e) const { n=tail(e); }
246 //void getHead(Node& n, const Edge& e) const { n=head(e); }
248 //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
249 //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
250 //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
251 //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
252 //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
253 //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
254 //void get_invalid(Node& n) { n=Node(); }
255 //void get_invalid(Edge& e) { e=Edge(); }
256 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
257 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
258 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
260 template< typename It >
267 template< typename It >
268 It first(Node v) const {
274 bool valid(Node n) const { return n.valid(); }
275 bool valid(Edge e) const { return e.valid(); }
277 template <typename It> It getNext(It it) const {
278 It tmp(it); return next(tmp); }
279 template <typename It> It& next(It& it) const { return ++it; }
282 /* for getting id's of graph objects */
283 /* these are important for the implementation of property vectors */
285 int id(Node v) const { return v.node->id; }
286 int id(Edge e) const { return e.edge->id; }
288 /* adding nodes and edges */
291 Node n = _add_node();
295 Edge addEdge(Node u, Node v) {
296 Edge e = _add_edge(u.node, v.node);
303 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
304 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
305 _delete_node(i.node);
310 _delete_edge(e.edge);
314 while (first<NodeIt>().valid()) erase(first<NodeIt>());
317 void setTail(Edge e, Node tail) {
318 _set_tail(e.edge, tail.node);
321 void setHead(Edge e, Node head) {
322 _set_head(e.edge, head.node);
325 /* stream operations, for testing purpose */
327 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
328 os << i.node->id; return os;
330 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
331 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
336 friend class ListGraph;
337 template <typename T> friend class NodeMap;
340 friend class OutEdgeIt;
341 friend class InEdgeIt;
342 friend class SymEdgeIt;
343 //public: //FIXME: It is required by op= of NodeIt
347 friend int ListGraph::id(Node v) const;
349 Node() /*: node(0)*/ { }
350 Node(const Invalid&) : node(0) { }
352 Node(node_item* _node) : node(_node) { }
353 bool valid() const { return (node); }
355 //void makeInvalid() { node=0; }
356 friend bool operator==(Node u, Node v) { return v.node==u.node; }
357 friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
358 friend std::ostream& operator<<(std::ostream& os, const Node& i);
361 class NodeIt : public Node {
362 friend class ListGraph;
364 public: //for everybody but marci
365 NodeIt(const ListGraph& G) : Node(G._first_node) { }
367 NodeIt() : Node() { }
368 NodeIt(const Invalid& i) : Node(i) { }
370 NodeIt(node_item* v) : Node(v) { }
371 NodeIt& operator++() { node=node->_next_node; return *this; }
373 // NodeIt& operator=(const Node& e)
374 // { node=e.node; return *this; }
378 friend class ListGraph;
379 template <typename T> friend class EdgeMap;
385 friend int ListGraph::id(Edge e) const;
387 Edge() /*: edge(0)*/ { }
388 Edge(const Invalid&) : edge(0) { }
391 Edge(edge_item* _edge) : edge(_edge) { }
392 bool valid() const { return (edge); }
394 //void makeInvalid() { edge=0; }
395 friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
396 friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
398 Node tailNode() const { return Node(edge->_tail); }
399 Node headNode() const { return Node(edge->_head); }
401 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
404 class EdgeIt : public Edge {
405 friend class ListGraph;
408 EdgeIt(const ListGraph& G) {
409 node_item* v=G._first_node;
410 if (v) edge=v->_first_out_edge; else edge=0;
411 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
414 EdgeIt() : Edge() { }
415 EdgeIt(const Invalid& i) : Edge(i) { }
417 EdgeIt(edge_item* _e) : Edge(_e) { }
418 EdgeIt& operator++() {
419 node_item* v=edge->_tail;
420 edge=edge->_next_out;
421 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
426 class OutEdgeIt : public Edge {
427 friend class ListGraph;
430 protected: //for alpar
431 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
433 OutEdgeIt() : Edge()/*, v(0)*/ { }
434 OutEdgeIt(const Invalid& i) : Edge(i) { }
435 OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
437 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
439 Node aNode() const { return Node(edge->_tail); }
440 Node bNode() const { return Node(edge->_head); }
443 class InEdgeIt : public Edge {
444 friend class ListGraph;
447 protected: //for alpar
448 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
450 InEdgeIt() : Edge()/*, v(0)*/ { }
451 InEdgeIt(const Invalid& i) : Edge(i) { }
452 InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
454 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
456 Node aNode() const { return Node(edge->_head); }
457 Node bNode() const { return Node(edge->_tail); }
460 class SymEdgeIt : public Edge {
461 friend class ListGraph;
462 bool out_or_in; //1 iff out, 0 iff in
466 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
468 edge=_v.node->_first_out_edge;
469 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
472 SymEdgeIt() : Edge() /*, v(0)*/ { }
473 SymEdgeIt(const Invalid& i) : Edge(i) { }
474 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
476 edge=_v.node->_first_out_edge;
477 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
480 SymEdgeIt& operator++() {
482 node_item* v=edge->_tail;
483 edge=edge->_next_out;
484 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
492 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
494 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
499 // template< typename T >
500 // T ListGraph::first() const {
501 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
506 // ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
507 // return firstNode();
511 // ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
512 // return firstEdge();
515 // template< typename T >
516 // T ListGraph::first(ListGraph::Node v) const {
517 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl;
522 // ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
523 // return firstOutEdge(v);
527 // ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
528 // return firstInEdge(v);
532 // ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
533 // return firstSymEdge(v);
539 #endif //HUGO_LIST_GRAPH_H