Changes in the documentation.
2 #ifndef HUGO_LIST_GRAPH_H
3 #define HUGO_LIST_GRAPH_H
10 #include "vector_map.h"
11 #include "edge_map_registry.h"
12 #include "node_map_registry.h"
13 #include "edge_map_base.h"
14 #include "node_map_base.h"
18 template <typename It>
21 for( ; it.valid(); ++it) { ++i; }
37 // template <typename T> class NodeMap;
38 // template <typename T> class EdgeMap;
40 // template <typename T> friend class NodeMap;
41 // template <typename T> friend class EdgeMap;
43 NodeMapRegistry<ListGraph, Node> node_maps;
46 class NodeMap : public VectorMap<ListGraph, Edge, T, EdgeMapBase> {};
48 EdgeMapRegistry<ListGraph, Edge> edge_maps;
51 class EdgeMap : public VectorMap<Graph, Node, T, NodeMapBase> {};
59 node_item* _first_node;
60 node_item* _last_node;
63 friend class ListGraph;
64 template <typename T> friend class NodeMap;
70 friend class OutEdgeIt;
71 friend class InEdgeIt;
72 friend class SymEdgeIt;
73 friend std::ostream& operator<<(std::ostream& os, const Node& i);
74 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
77 edge_item* _first_out_edge;
78 edge_item* _last_out_edge;
79 edge_item* _first_in_edge;
80 edge_item* _last_in_edge;
81 node_item* _next_node;
82 node_item* _prev_node;
88 friend class ListGraph;
89 template <typename T> friend class EdgeMap;
95 friend class OutEdgeIt;
96 friend class InEdgeIt;
97 friend class SymEdgeIt;
98 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
103 edge_item* _next_out;
104 edge_item* _prev_out;
111 node_item* _add_node() {
112 node_item* p=new node_item;
114 p->_first_out_edge=0;
118 p->_prev_node=_last_node;
120 if (_last_node) _last_node->_next_node=p;
122 if (!_first_node) _first_node=p;
128 edge_item* _add_edge(node_item* _tail, node_item* _head) {
129 edge_item* e=new edge_item;
134 e->_prev_out=_tail->_last_out_edge;
135 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
136 _tail->_last_out_edge=e;
137 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
140 e->_prev_in=_head->_last_in_edge;
141 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
142 _head->_last_in_edge=e;
143 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
150 //deletes a node which has no out edge and no in edge
151 void _delete_node(node_item* v) {
152 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
153 _last_node=v->_prev_node;
154 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
155 _first_node=v->_next_node;
161 void _delete_edge(edge_item* e) {
162 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
163 (e->_tail)->_last_out_edge=e->_prev_out;
164 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
165 (e->_tail)->_first_out_edge=e->_next_out;
166 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
167 (e->_head)->_last_in_edge=e->_prev_in;
168 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
169 (e->_head)->_first_in_edge=e->_next_in;
175 void _set_tail(edge_item* e, node_item* _tail) {
176 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
177 (e->_tail)->_last_out_edge=e->_prev_out;
178 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
179 (e->_tail)->_first_out_edge=e->_next_out;
183 e->_prev_out=_tail->_last_out_edge;
184 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
185 _tail->_last_out_edge=e;
186 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
190 void _set_head(edge_item* e, node_item* _head) {
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;
198 e->_prev_in=_head->_last_in_edge;
199 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
200 _head->_last_in_edge=e;
201 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
207 /* default constructor */
209 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
212 while (first<NodeIt>().valid()) erase(first<NodeIt>());
215 int nodeNum() const { return _node_num; }
216 int edgeNum() const { return _edge_num; }
218 /* functions to construct iterators from the graph, or from each other */
220 //NodeIt firstNode() const { return NodeIt(*this); }
221 //EdgeIt firstEdge() const { return EdgeIt(*this); }
223 //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
224 //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
225 //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
226 Node tail(Edge e) const { return e.tailNode(); }
227 Node head(Edge e) const { return e.headNode(); }
229 Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
230 Node aNode(const InEdgeIt& e) const { return e.aNode(); }
231 Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
233 Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
234 Node bNode(const InEdgeIt& e) const { return e.bNode(); }
235 Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
237 //Node invalid_node() { return Node(); }
238 //Edge invalid_edge() { return Edge(); }
239 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
240 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
241 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
243 /* same methods in other style */
244 /* for experimental purpose */
246 NodeIt& /*getF*/first(NodeIt& v) const {
247 v=NodeIt(*this); return v; }
248 EdgeIt& /*getF*/first(EdgeIt& e) const {
249 e=EdgeIt(*this); return e; }
250 OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const {
251 e=OutEdgeIt(*this, v); return e; }
252 InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const {
253 e=InEdgeIt(*this, v); return e; }
254 SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const {
255 e=SymEdgeIt(*this, v); return e; }
256 //void getTail(Node& n, const Edge& e) const { n=tail(e); }
257 //void getHead(Node& n, const Edge& e) const { n=head(e); }
259 //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
260 //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
261 //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
262 //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
263 //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
264 //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
265 //void get_invalid(Node& n) { n=Node(); }
266 //void get_invalid(Edge& e) { e=Edge(); }
267 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
268 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
269 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
271 template< typename It >
278 template< typename It >
279 It first(Node v) const {
285 bool valid(Node n) const { return n.valid(); }
286 bool valid(Edge e) const { return e.valid(); }
288 template <typename It> It getNext(It it) const {
289 It tmp(it); return next(tmp); }
290 template <typename It> It& next(It& it) const { return ++it; }
293 /* for getting id's of graph objects */
294 /* these are important for the implementation of property vectors */
296 int id(Node v) const { return v.node->id; }
297 int id(Edge e) const { return e.edge->id; }
299 /* adding nodes and edges */
302 Node n = _add_node();
306 Edge addEdge(Node u, Node v) {
307 Edge e = _add_edge(u.node, v.node);
314 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
315 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
316 _delete_node(i.node);
321 _delete_edge(e.edge);
325 while (first<NodeIt>().valid()) erase(first<NodeIt>());
328 void setTail(Edge e, Node tail) {
329 _set_tail(e.edge, tail.node);
332 void setHead(Edge e, Node head) {
333 _set_head(e.edge, head.node);
336 /* stream operations, for testing purpose */
338 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
339 os << i.node->id; return os;
341 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
342 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
347 friend class ListGraph;
348 template <typename T> friend class NodeMap;
351 friend class OutEdgeIt;
352 friend class InEdgeIt;
353 friend class SymEdgeIt;
354 //public: //FIXME: It is required by op= of NodeIt
358 friend int ListGraph::id(Node v) const;
360 Node() /*: node(0)*/ { }
361 Node(const Invalid&) : node(0) { }
363 Node(node_item* _node) : node(_node) { }
364 bool valid() const { return (node); }
366 //void makeInvalid() { node=0; }
367 friend bool operator==(Node u, Node v) { return v.node==u.node; }
368 friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
369 friend std::ostream& operator<<(std::ostream& os, const Node& i);
372 class NodeIt : public Node {
373 friend class ListGraph;
375 public: //for everybody but marci
376 NodeIt(const ListGraph& G) : Node(G._first_node) { }
378 NodeIt() : Node() { }
379 NodeIt(const Invalid& i) : Node(i) { }
381 NodeIt(node_item* v) : Node(v) { }
382 NodeIt& operator++() { node=node->_next_node; return *this; }
384 // NodeIt& operator=(const Node& e)
385 // { node=e.node; return *this; }
389 friend class ListGraph;
390 template <typename T> friend class EdgeMap;
396 friend int ListGraph::id(Edge e) const;
398 Edge() /*: edge(0)*/ { }
399 Edge(const Invalid&) : edge(0) { }
402 Edge(edge_item* _edge) : edge(_edge) { }
403 bool valid() const { return (edge); }
405 //void makeInvalid() { edge=0; }
406 friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
407 friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
409 Node tailNode() const { return Node(edge->_tail); }
410 Node headNode() const { return Node(edge->_head); }
412 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
415 class EdgeIt : public Edge {
416 friend class ListGraph;
419 EdgeIt(const ListGraph& G) {
420 node_item* v=G._first_node;
421 if (v) edge=v->_first_out_edge; else edge=0;
422 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
425 EdgeIt() : Edge() { }
426 EdgeIt(const Invalid& i) : Edge(i) { }
428 EdgeIt(edge_item* _e) : Edge(_e) { }
429 EdgeIt& operator++() {
430 node_item* v=edge->_tail;
431 edge=edge->_next_out;
432 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
437 class OutEdgeIt : public Edge {
438 friend class ListGraph;
441 protected: //for alpar
442 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
444 OutEdgeIt() : Edge()/*, v(0)*/ { }
445 OutEdgeIt(const Invalid& i) : Edge(i) { }
446 OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
448 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
450 Node aNode() const { return Node(edge->_tail); }
451 Node bNode() const { return Node(edge->_head); }
454 class InEdgeIt : public Edge {
455 friend class ListGraph;
458 protected: //for alpar
459 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
461 InEdgeIt() : Edge()/*, v(0)*/ { }
462 InEdgeIt(const Invalid& i) : Edge(i) { }
463 InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
465 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
467 Node aNode() const { return Node(edge->_head); }
468 Node bNode() const { return Node(edge->_tail); }
471 class SymEdgeIt : public Edge {
472 friend class ListGraph;
473 bool out_or_in; //1 iff out, 0 iff in
477 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
479 edge=_v.node->_first_out_edge;
480 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
483 SymEdgeIt() : Edge() /*, v(0)*/ { }
484 SymEdgeIt(const Invalid& i) : Edge(i) { }
485 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
487 edge=_v.node->_first_out_edge;
488 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
491 SymEdgeIt& operator++() {
493 node_item* v=edge->_tail;
494 edge=edge->_next_out;
495 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
503 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
505 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
510 // template< typename T >
511 // T ListGraph::first() const {
512 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
517 // ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
518 // return firstNode();
522 // ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
523 // return firstEdge();
526 // template< typename T >
527 // T ListGraph::first(ListGraph::Node v) const {
528 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl;
533 // ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
534 // return firstOutEdge(v);
538 // ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
539 // return firstInEdge(v);
543 // ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
544 // return firstSymEdge(v);
550 #endif //HUGO_LIST_GRAPH_H