I hope it works. The 'erase' functions hasn't been tested yet.
2 #ifndef HUGO_LIST_GRAPH_H
3 #define HUGO_LIST_GRAPH_H
12 template <typename It>
15 for( ; it.valid(); ++it) { ++i; }
30 template <typename T> class NodeMap;
31 template <typename T> class EdgeMap;
33 template <typename T> friend class NodeMap;
34 template <typename T> friend class EdgeMap;
39 std::vector<T> container;
43 NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { }
44 NodeMap(const ListGraph& _G, T a) :
45 G(_G), container(G.node_id, a) { }
46 void set(Node n, T a) { container[/*G.id(n)*/n.node->id]=a; }
47 // T get(Node n) const { return container[/*G.id(n)*/n.node->id]; }
48 typename std::vector<T>::reference operator[](Node n) {
49 return container[/*G.id(n)*/n.node->id]; }
50 typename std::vector<T>::const_reference operator[](Node n) const {
51 return container[/*G.id(n)*/n.node->id];
53 void update() { container.resize(G.node_id); }
54 void update(T a) { container.resize(G.node_id, a); }
60 std::vector<T> container;
64 EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
65 EdgeMap(const ListGraph& _G, T a) :
66 G(_G), container(G.edge_id, a) { }
67 void set(Edge e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
68 // T get(Edge e) const { return container[/*G.id(e)*/e.edge->id]; }
69 typename std::vector<T>::reference operator[](Edge e) {
70 return container[/*G.id(e)*/e.edge->id]; }
71 typename std::vector<T>::const_reference operator[](Edge e) const {
72 return container[/*G.id(e)*/e.edge->id];
74 void update() { container.resize(G.edge_id); }
75 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) { }
238 while (this->valid(first(n))) erase(n);
239 //while (first<NodeIt>().valid()) erase(first<NodeIt>());
242 int nodeNum() const { return _node_num; }
243 int edgeNum() const { return _edge_num; }
245 /* functions to construct iterators from the graph, or from each other */
247 //NodeIt firstNode() const { return NodeIt(*this); }
248 //EdgeIt firstEdge() const { return EdgeIt(*this); }
250 //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
251 //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
252 //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
253 Node tail(Edge e) const { return e.tailNode(); }
254 Node head(Edge e) const { return e.headNode(); }
256 Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
257 Node aNode(const InEdgeIt& e) const { return e.aNode(); }
258 Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
260 Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
261 Node bNode(const InEdgeIt& e) const { return e.bNode(); }
262 Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
264 //Node invalid_node() { return Node(); }
265 //Edge invalid_edge() { return Edge(); }
266 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
267 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
268 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
270 /* same methods in other style */
271 /* for experimental purpose */
273 NodeIt& first(NodeIt& v) const {
274 v=NodeIt(*this); return v; }
275 EdgeIt& first(EdgeIt& e) const {
276 e=EdgeIt(*this); return e; }
277 OutEdgeIt& first(OutEdgeIt& e, Node v) const {
278 e=OutEdgeIt(*this, v); return e; }
279 InEdgeIt& first(InEdgeIt& e, Node v) const {
280 e=InEdgeIt(*this, v); return e; }
281 SymEdgeIt& first(SymEdgeIt& e, Node v) const {
282 e=SymEdgeIt(*this, v); return e; }
283 //void getTail(Node& n, const Edge& e) const { n=tail(e); }
284 //void getHead(Node& n, const Edge& e) const { n=head(e); }
286 //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
287 //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
288 //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
289 //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
290 //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
291 //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
292 //void get_invalid(Node& n) { n=Node(); }
293 //void get_invalid(Edge& e) { e=Edge(); }
294 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
295 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
296 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
298 // template< typename It >
299 // It first() const {
305 // template< typename It >
306 // It first(Node v) const {
312 bool valid(Node n) const { return n.valid(); }
313 bool valid(Edge e) const { return e.valid(); }
315 // template <typename It> It getNext(It it) const {
316 // It tmp(it); next(tmp); return tmp; }
317 // NodeIt& next(NodeIt& it) const { return ++it; }
318 // EdgeIt& next(EdgeIt& it) const { return ++it; }
319 // OutEdgeIt& next(OutEdgeIt& it) const { return ++it; }
320 // InEdgeIt& next(InEdgeIt& it) const { return ++it; }
321 // SymEdgeIt& next(SymEdgeIt& it) const { return ++it; }
322 // template <typename It> It& next(It& it) const { return ++it; }
323 template <typename It> It& next(It& it) const { ++it; return it; }
326 /* for getting id's of graph objects */
327 /* these are important for the implementation of property vectors */
329 int id(Node v) const { return v.node->id; }
330 int id(Edge e) const { return e.edge->id; }
332 /* adding nodes and edges */
334 Node addNode() { return Node(_add_node()); }
335 Edge addEdge(Node u, Node v) {
336 return Edge(_add_edge(u.node, v.node));
342 while (this->valid(first(e, i))) erase(e);
346 while (this->valid(first(e, i))) erase(e);
348 //while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
349 //while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
350 _delete_node(i.node);
353 void erase(Edge e) { _delete_edge(e.edge); }
357 while (this->valid(first(e))) erase(e);
358 //while (first<NodeIt>().valid()) erase(first<NodeIt>());
361 void setTail(Edge e, Node tail) {
362 _set_tail(e.edge, tail.node);
365 void setHead(Edge e, Node head) {
366 _set_head(e.edge, head.node);
369 /* stream operations, for testing purpose */
371 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
372 os << i.node->id; return os;
374 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
375 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
380 friend class ListGraph;
381 template <typename T> friend class NodeMap;
384 friend class OutEdgeIt;
385 friend class InEdgeIt;
386 friend class SymEdgeIt;
387 //public: //FIXME: It is required by op= of NodeIt
391 friend int ListGraph::id(Node v) const;
393 Node() /*: node(0)*/ { }
394 Node(const Invalid&) : node(0) { }
396 Node(node_item* _node) : node(_node) { }
397 bool valid() const { return (node); }
399 //void makeInvalid() { node=0; }
400 friend bool operator==(Node u, Node v) { return v.node==u.node; }
401 friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
402 friend std::ostream& operator<<(std::ostream& os, const Node& i);
405 class NodeIt : public Node {
406 friend class ListGraph;
408 public: //for everybody but marci
409 NodeIt(const ListGraph& G) : Node(G._first_node) { }
411 NodeIt() : Node() { }
412 NodeIt(const Invalid& i) : Node(i) { }
414 NodeIt(node_item* v) : Node(v) { }
415 NodeIt& operator++() { node=node->_next_node; return *this; }
417 // NodeIt& operator=(const Node& e)
418 // { node=e.node; return *this; }
422 friend class ListGraph;
423 template <typename T> friend class EdgeMap;
429 friend int ListGraph::id(Edge e) const;
431 Edge() /*: edge(0)*/ { }
432 Edge(const Invalid&) : edge(0) { }
435 Edge(edge_item* _edge) : edge(_edge) { }
436 bool valid() const { return (edge); }
438 //void makeInvalid() { edge=0; }
439 friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
440 friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
442 Node tailNode() const { return Node(edge->_tail); }
443 Node headNode() const { return Node(edge->_head); }
445 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
448 class EdgeIt : public Edge {
449 friend class ListGraph;
452 EdgeIt(const ListGraph& G) {
453 node_item* v=G._first_node;
454 if (v) edge=v->_first_out_edge; else edge=0;
455 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
458 EdgeIt() : Edge() { }
459 EdgeIt(const Invalid& i) : Edge(i) { }
461 EdgeIt(edge_item* _e) : Edge(_e) { }
462 EdgeIt& operator++() {
463 node_item* v=edge->_tail;
464 edge=edge->_next_out;
465 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
470 class OutEdgeIt : public Edge {
471 friend class ListGraph;
474 protected: //for alpar
475 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
477 OutEdgeIt() : Edge()/*, v(0)*/ { }
478 OutEdgeIt(const Invalid& i) : Edge(i) { }
479 OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
481 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
483 Node aNode() const { return Node(edge->_tail); }
484 Node bNode() const { return Node(edge->_head); }
487 class InEdgeIt : public Edge {
488 friend class ListGraph;
491 protected: //for alpar
492 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
494 InEdgeIt() : Edge()/*, v(0)*/ { }
495 InEdgeIt(const Invalid& i) : Edge(i) { }
496 InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
498 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
500 Node aNode() const { return Node(edge->_head); }
501 Node bNode() const { return Node(edge->_tail); }
504 class SymEdgeIt : public Edge {
505 friend class ListGraph;
506 bool out_or_in; //1 iff out, 0 iff in
509 protected: //for alpar
510 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
512 edge=_v.node->_first_out_edge;
513 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
516 SymEdgeIt() : Edge() /*, v(0)*/ { }
517 SymEdgeIt(const Invalid& i) : Edge(i) { }
518 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
520 edge=_v.node->_first_out_edge;
521 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
524 SymEdgeIt& operator++() {
526 node_item* v=edge->_tail;
527 edge=edge->_next_out;
528 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
536 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
538 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
542 class UndirListGraph : public ListGraph {
544 typedef SymEdgeIt OutEdgeIt;
545 typedef SymEdgeIt InEdgeIt;
550 #endif //HUGO_LIST_GRAPH_H