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); }
83 node_item* _first_node;
84 node_item* _last_node;
87 friend class ListGraph;
88 template <typename T> friend class NodeMap;
94 friend class OutEdgeIt;
95 friend class InEdgeIt;
96 friend class SymEdgeIt;
97 friend std::ostream& operator<<(std::ostream& os, const Node& i);
98 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
101 edge_item* _first_out_edge;
102 edge_item* _last_out_edge;
103 edge_item* _first_in_edge;
104 edge_item* _last_in_edge;
105 node_item* _next_node;
106 node_item* _prev_node;
112 friend class ListGraph;
113 template <typename T> friend class EdgeMap;
119 friend class OutEdgeIt;
120 friend class InEdgeIt;
121 friend class SymEdgeIt;
122 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
127 edge_item* _next_out;
128 edge_item* _prev_out;
135 node_item* _add_node() {
136 node_item* p=new node_item;
138 p->_first_out_edge=0;
142 p->_prev_node=_last_node;
144 if (_last_node) _last_node->_next_node=p;
146 if (!_first_node) _first_node=p;
152 edge_item* _add_edge(node_item* _tail, node_item* _head) {
153 edge_item* e=new edge_item;
158 e->_prev_out=_tail->_last_out_edge;
159 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
160 _tail->_last_out_edge=e;
161 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
164 e->_prev_in=_head->_last_in_edge;
165 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
166 _head->_last_in_edge=e;
167 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
174 //deletes a node which has no out edge and no in edge
175 void _delete_node(node_item* v) {
176 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
177 _last_node=v->_prev_node;
178 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
179 _first_node=v->_next_node;
185 void _delete_edge(edge_item* e) {
186 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
187 (e->_tail)->_last_out_edge=e->_prev_out;
188 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
189 (e->_tail)->_first_out_edge=e->_next_out;
190 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
191 (e->_head)->_last_in_edge=e->_prev_in;
192 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
193 (e->_head)->_first_in_edge=e->_next_in;
199 void _set_tail(edge_item* e, node_item* _tail) {
200 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
201 (e->_tail)->_last_out_edge=e->_prev_out;
202 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
203 (e->_tail)->_first_out_edge=e->_next_out;
207 e->_prev_out=_tail->_last_out_edge;
208 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
209 _tail->_last_out_edge=e;
210 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
214 void _set_head(edge_item* e, node_item* _head) {
215 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
216 (e->_head)->_last_in_edge=e->_prev_in;
217 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
218 (e->_head)->_first_in_edge=e->_next_in;
222 e->_prev_in=_head->_last_in_edge;
223 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
224 _head->_last_in_edge=e;
225 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
231 /* default constructor */
233 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
236 while (first<NodeIt>().valid()) erase(first<NodeIt>());
239 int nodeNum() const { return _node_num; }
240 int edgeNum() const { return _edge_num; }
242 /* functions to construct iterators from the graph, or from each other */
244 //NodeIt firstNode() const { return NodeIt(*this); }
245 //EdgeIt firstEdge() const { return EdgeIt(*this); }
247 //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
248 //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
249 //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
250 Node tail(Edge e) const { return e.tailNode(); }
251 Node head(Edge e) const { return e.headNode(); }
253 Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
254 Node aNode(const InEdgeIt& e) const { return e.aNode(); }
255 Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
257 Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
258 Node bNode(const InEdgeIt& e) const { return e.bNode(); }
259 Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
261 //Node invalid_node() { return Node(); }
262 //Edge invalid_edge() { return Edge(); }
263 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
264 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
265 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
267 /* same methods in other style */
268 /* for experimental purpose */
270 NodeIt& /*getF*/first(NodeIt& v) const {
271 v=NodeIt(*this); return v; }
272 EdgeIt& /*getF*/first(EdgeIt& e) const {
273 e=EdgeIt(*this); return e; }
274 OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const {
275 e=OutEdgeIt(*this, v); return e; }
276 InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const {
277 e=InEdgeIt(*this, v); return e; }
278 SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const {
279 e=SymEdgeIt(*this, v); return e; }
280 //void getTail(Node& n, const Edge& e) const { n=tail(e); }
281 //void getHead(Node& n, const Edge& e) const { n=head(e); }
283 //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
284 //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
285 //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
286 //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
287 //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
288 //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
289 //void get_invalid(Node& n) { n=Node(); }
290 //void get_invalid(Edge& e) { e=Edge(); }
291 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
292 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
293 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
295 template< typename It >
302 template< typename It >
303 It first(Node v) const {
309 bool valid(Node n) const { return n.valid(); }
310 bool valid(Edge e) const { return e.valid(); }
312 template <typename It> It getNext(It it) const {
313 It tmp(it); return next(tmp); }
314 template <typename It> It& next(It& it) const { return ++it; }
317 /* for getting id's of graph objects */
318 /* these are important for the implementation of property vectors */
320 int id(Node v) const { return v.node->id; }
321 int id(Edge e) const { return e.edge->id; }
323 /* adding nodes and edges */
325 Node addNode() { return Node(_add_node()); }
326 Edge addEdge(Node u, Node v) {
327 return Edge(_add_edge(u.node, v.node));
331 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
332 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
333 _delete_node(i.node);
336 void erase(Edge e) { _delete_edge(e.edge); }
339 while (first<NodeIt>().valid()) erase(first<NodeIt>());
342 void setTail(Edge e, Node tail) {
343 _set_tail(e.edge, tail.node);
346 void setHead(Edge e, Node head) {
347 _set_head(e.edge, head.node);
350 /* stream operations, for testing purpose */
352 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
353 os << i.node->id; return os;
355 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
356 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
361 friend class ListGraph;
362 template <typename T> friend class NodeMap;
365 friend class OutEdgeIt;
366 friend class InEdgeIt;
367 friend class SymEdgeIt;
368 //public: //FIXME: It is required by op= of NodeIt
372 friend int ListGraph::id(Node v) const;
374 Node() /*: node(0)*/ { }
375 Node(const Invalid&) : node(0) { }
377 Node(node_item* _node) : node(_node) { }
378 bool valid() const { return (node); }
380 //void makeInvalid() { node=0; }
381 friend bool operator==(Node u, Node v) { return v.node==u.node; }
382 friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
383 friend std::ostream& operator<<(std::ostream& os, const Node& i);
386 class NodeIt : public Node {
387 friend class ListGraph;
389 public: //for everybody but marci
390 NodeIt(const ListGraph& G) : Node(G._first_node) { }
392 NodeIt() : Node() { }
393 NodeIt(const Invalid& i) : Node(i) { }
395 NodeIt(node_item* v) : Node(v) { }
396 NodeIt& operator++() { node=node->_next_node; return *this; }
398 // NodeIt& operator=(const Node& e)
399 // { node=e.node; return *this; }
403 friend class ListGraph;
404 template <typename T> friend class EdgeMap;
410 friend int ListGraph::id(Edge e) const;
412 Edge() /*: edge(0)*/ { }
413 Edge(const Invalid&) : edge(0) { }
416 Edge(edge_item* _edge) : edge(_edge) { }
417 bool valid() const { return (edge); }
419 //void makeInvalid() { edge=0; }
420 friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
421 friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
423 Node tailNode() const { return Node(edge->_tail); }
424 Node headNode() const { return Node(edge->_head); }
426 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
429 class EdgeIt : public Edge {
430 friend class ListGraph;
433 EdgeIt(const ListGraph& G) {
434 node_item* v=G._first_node;
435 if (v) edge=v->_first_out_edge; else edge=0;
436 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
439 EdgeIt() : Edge() { }
440 EdgeIt(const Invalid& i) : Edge(i) { }
442 EdgeIt(edge_item* _e) : Edge(_e) { }
443 EdgeIt& operator++() {
444 node_item* v=edge->_tail;
445 edge=edge->_next_out;
446 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
451 class OutEdgeIt : public Edge {
452 friend class ListGraph;
455 protected: //for alpar
456 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
458 OutEdgeIt() : Edge()/*, v(0)*/ { }
459 OutEdgeIt(const Invalid& i) : Edge(i) { }
460 OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
462 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
464 Node aNode() const { return Node(edge->_tail); }
465 Node bNode() const { return Node(edge->_head); }
468 class InEdgeIt : public Edge {
469 friend class ListGraph;
472 protected: //for alpar
473 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
475 InEdgeIt() : Edge()/*, v(0)*/ { }
476 InEdgeIt(const Invalid& i) : Edge(i) { }
477 InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
479 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
481 Node aNode() const { return Node(edge->_head); }
482 Node bNode() const { return Node(edge->_tail); }
485 class SymEdgeIt : public Edge {
486 friend class ListGraph;
487 bool out_or_in; //1 iff out, 0 iff in
491 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
493 edge=_v.node->_first_out_edge;
494 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
497 SymEdgeIt() : Edge() /*, v(0)*/ { }
498 SymEdgeIt(const Invalid& i) : Edge(i) { }
499 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
501 edge=_v.node->_first_out_edge;
502 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
505 SymEdgeIt& operator++() {
507 node_item* v=edge->_tail;
508 edge=edge->_next_out;
509 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
517 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
519 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
524 // template< typename T >
525 // T ListGraph::first() const {
526 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
531 // ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
532 // return firstNode();
536 // ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
537 // return firstEdge();
540 // template< typename T >
541 // T ListGraph::first(ListGraph::Node v) const {
542 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl;
547 // ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
548 // return firstOutEdge(v);
552 // ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
553 // return firstInEdge(v);
557 // ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
558 // return firstSymEdge(v);
564 #endif //LIST_GRAPH_H