2 #ifndef HUGO_LIST_GRAPH_H
3 #define HUGO_LIST_GRAPH_H
12 template <typename It>
15 for( ; it.valid(); ++it) { ++i; }
31 template <typename T> class NodeMap;
32 template <typename T> class EdgeMap;
34 template <typename T> friend class NodeMap;
35 template <typename T> friend class EdgeMap;
40 std::vector<T> container;
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];
54 void update() { container.resize(G.node_id); }
55 void update(T a) { container.resize(G.node_id, a); }
61 std::vector<T> container;
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];
75 void update() { container.resize(G.edge_id); }
76 void update(T a) { container.resize(G.edge_id, a); }
84 node_item* _first_node;
85 node_item* _last_node;
88 friend class ListGraph;
89 template <typename T> friend class NodeMap;
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);
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;
113 friend class ListGraph;
114 template <typename T> friend class EdgeMap;
120 friend class OutEdgeIt;
121 friend class InEdgeIt;
122 friend class SymEdgeIt;
123 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
128 edge_item* _next_out;
129 edge_item* _prev_out;
136 node_item* _add_node() {
137 node_item* p=new node_item;
139 p->_first_out_edge=0;
143 p->_prev_node=_last_node;
145 if (_last_node) _last_node->_next_node=p;
147 if (!_first_node) _first_node=p;
153 edge_item* _add_edge(node_item* _tail, node_item* _head) {
154 edge_item* e=new edge_item;
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;
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; }
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;
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;
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;
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;
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;
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; }
232 /* default constructor */
234 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
237 while (first<NodeIt>().valid()) erase(first<NodeIt>());
240 int nodeNum() const { return _node_num; }
241 int edgeNum() const { return _edge_num; }
243 /* functions to construct iterators from the graph, or from each other */
245 //NodeIt firstNode() const { return NodeIt(*this); }
246 //EdgeIt firstEdge() const { return EdgeIt(*this); }
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(); }
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(); }
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(); }
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(); }
268 /* same methods in other style */
269 /* for experimental purpose */
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); }
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(); }
296 template< typename It >
303 template< typename It >
304 It first(Node v) const {
310 bool valid(Node n) const { return n.valid(); }
311 bool valid(Edge e) const { return e.valid(); }
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; }
318 /* for getting id's of graph objects */
319 /* these are important for the implementation of property vectors */
321 int id(Node v) const { return v.node->id; }
322 int id(Edge e) const { return e.edge->id; }
324 /* adding nodes and edges */
326 Node addNode() { return Node(_add_node()); }
327 Edge addEdge(Node u, Node v) {
328 return Edge(_add_edge(u.node, v.node));
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);
337 void erase(Edge e) { _delete_edge(e.edge); }
340 while (first<NodeIt>().valid()) erase(first<NodeIt>());
343 void setTail(Edge e, Node tail) {
344 _set_tail(e.edge, tail.node);
347 void setHead(Edge e, Node head) {
348 _set_head(e.edge, head.node);
351 /* stream operations, for testing purpose */
353 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
354 os << i.node->id; return os;
356 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
357 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
362 friend class ListGraph;
363 template <typename T> friend class NodeMap;
366 friend class OutEdgeIt;
367 friend class InEdgeIt;
368 friend class SymEdgeIt;
369 //public: //FIXME: It is required by op= of NodeIt
373 friend int ListGraph::id(Node v) const;
375 Node() /*: node(0)*/ { }
376 Node(const Invalid&) : node(0) { }
378 Node(node_item* _node) : node(_node) { }
379 bool valid() const { return (node); }
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);
387 class NodeIt : public Node {
388 friend class ListGraph;
390 public: //for everybody but marci
391 NodeIt(const ListGraph& G) : Node(G._first_node) { }
393 NodeIt() : Node() { }
394 NodeIt(const Invalid& i) : Node(i) { }
396 NodeIt(node_item* v) : Node(v) { }
397 NodeIt& operator++() { node=node->_next_node; return *this; }
399 // NodeIt& operator=(const Node& e)
400 // { node=e.node; return *this; }
404 friend class ListGraph;
405 template <typename T> friend class EdgeMap;
411 friend int ListGraph::id(Edge e) const;
413 Edge() /*: edge(0)*/ { }
414 Edge(const Invalid&) : edge(0) { }
417 Edge(edge_item* _edge) : edge(_edge) { }
418 bool valid() const { return (edge); }
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; }
424 Node tailNode() const { return Node(edge->_tail); }
425 Node headNode() const { return Node(edge->_head); }
427 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
430 class EdgeIt : public Edge {
431 friend class ListGraph;
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; }
440 EdgeIt() : Edge() { }
441 EdgeIt(const Invalid& i) : Edge(i) { }
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; }
452 class OutEdgeIt : public Edge {
453 friend class ListGraph;
456 protected: //for alpar
457 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
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; }
463 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
465 Node aNode() const { return Node(edge->_tail); }
466 Node bNode() const { return Node(edge->_head); }
469 class InEdgeIt : public Edge {
470 friend class ListGraph;
473 protected: //for alpar
474 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
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; }
480 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
482 Node aNode() const { return Node(edge->_head); }
483 Node bNode() const { return Node(edge->_tail); }
486 class SymEdgeIt : public Edge {
487 friend class ListGraph;
488 bool out_or_in; //1 iff out, 0 iff in
492 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
494 edge=_v.node->_first_out_edge;
495 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
498 SymEdgeIt() : Edge() /*, v(0)*/ { }
499 SymEdgeIt(const Invalid& i) : Edge(i) { }
500 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
502 edge=_v.node->_first_out_edge;
503 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
506 SymEdgeIt& operator++() {
508 node_item* v=edge->_tail;
509 edge=edge->_next_out;
510 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
518 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
520 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
525 // template< typename T >
526 // T ListGraph::first() const {
527 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
532 // ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
533 // return firstNode();
537 // ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
538 // return firstEdge();
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;
548 // ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
549 // return firstOutEdge(v);
553 // ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
554 // return firstInEdge(v);
558 // ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
559 // return firstSymEdge(v);
565 #endif //HUGO_LIST_GRAPH_H