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 T& operator[](Node n) { return container[/*G.id(n)*/n.node->id]; }
49 const T& operator[](Node n) const {
50 return container[/*G.id(n)*/n.node->id];
52 void update() { container.resize(G.node_id); }
53 void update(T a) { container.resize(G.node_id, a); }
59 std::vector<T> container;
63 EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
64 EdgeMap(const ListGraph& _G, T a) :
65 G(_G), container(G.edge_id, a) { }
66 void set(Edge e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
67 T get(Edge e) const { return container[/*G.id(e)*/e.edge->id]; }
68 T& operator[](Edge e) { return container[/*G.id(e)*/e.edge->id]; }
69 const T& operator[](Edge e) const {
70 return container[/*G.id(e)*/e.edge->id];
72 void update() { container.resize(G.edge_id); }
73 void update(T a) { container.resize(G.edge_id, a); }
81 node_item* _first_node;
82 node_item* _last_node;
85 friend class ListGraph;
86 template <typename T> friend class NodeMap;
92 friend class OutEdgeIt;
93 friend class InEdgeIt;
94 friend class SymEdgeIt;
95 friend std::ostream& operator<<(std::ostream& os, const Node& i);
96 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
99 edge_item* _first_out_edge;
100 edge_item* _last_out_edge;
101 edge_item* _first_in_edge;
102 edge_item* _last_in_edge;
103 node_item* _next_node;
104 node_item* _prev_node;
110 friend class ListGraph;
111 template <typename T> friend class EdgeMap;
117 friend class OutEdgeIt;
118 friend class InEdgeIt;
119 friend class SymEdgeIt;
120 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
125 edge_item* _next_out;
126 edge_item* _prev_out;
133 node_item* _add_node() {
134 node_item* p=new node_item;
136 p->_first_out_edge=0;
140 p->_prev_node=_last_node;
142 if (_last_node) _last_node->_next_node=p;
144 if (!_first_node) _first_node=p;
150 edge_item* _add_edge(node_item* _tail, node_item* _head) {
151 edge_item* e=new edge_item;
156 e->_prev_out=_tail->_last_out_edge;
157 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
158 _tail->_last_out_edge=e;
159 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
162 e->_prev_in=_head->_last_in_edge;
163 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
164 _head->_last_in_edge=e;
165 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
172 //deletes a node which has no out edge and no in edge
173 void _delete_node(node_item* v) {
174 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
175 _last_node=v->_prev_node;
176 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
177 _first_node=v->_next_node;
183 void _delete_edge(edge_item* e) {
184 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
185 (e->_tail)->_last_out_edge=e->_prev_out;
186 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
187 (e->_tail)->_first_out_edge=e->_next_out;
188 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
189 (e->_head)->_last_in_edge=e->_prev_in;
190 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
191 (e->_head)->_first_in_edge=e->_next_in;
197 void _set_tail(edge_item* e, node_item* _tail) {
198 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
199 (e->_tail)->_last_out_edge=e->_prev_out;
200 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
201 (e->_tail)->_first_out_edge=e->_next_out;
205 e->_prev_out=_tail->_last_out_edge;
206 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
207 _tail->_last_out_edge=e;
208 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
212 void _set_head(edge_item* e, node_item* _head) {
213 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
214 (e->_head)->_last_in_edge=e->_prev_in;
215 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
216 (e->_head)->_first_in_edge=e->_next_in;
220 e->_prev_in=_head->_last_in_edge;
221 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
222 _head->_last_in_edge=e;
223 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
229 /* default constructor */
231 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
234 while (first<NodeIt>().valid()) erase(first<NodeIt>());
237 int nodeNum() const { return _node_num; }
238 int edgeNum() const { return _edge_num; }
240 /* functions to construct iterators from the graph, or from each other */
242 //NodeIt firstNode() const { return NodeIt(*this); }
243 //EdgeIt firstEdge() const { return EdgeIt(*this); }
245 //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
246 //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
247 //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
248 Node tail(Edge e) const { return e.tailNode(); }
249 Node head(Edge e) const { return e.headNode(); }
251 Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
252 Node aNode(const InEdgeIt& e) const { return e.aNode(); }
253 Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
255 Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
256 Node bNode(const InEdgeIt& e) const { return e.bNode(); }
257 Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
259 //Node invalid_node() { return Node(); }
260 //Edge invalid_edge() { return Edge(); }
261 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
262 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
263 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
265 /* same methods in other style */
266 /* for experimental purpose */
268 NodeIt& /*getF*/first(NodeIt& v) const {
269 v=NodeIt(*this); return v; }
270 EdgeIt& /*getF*/first(EdgeIt& e) const {
271 e=EdgeIt(*this); return e; }
272 OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const {
273 e=OutEdgeIt(*this, v); return e; }
274 InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const {
275 e=InEdgeIt(*this, v); return e; }
276 SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const {
277 e=SymEdgeIt(*this, v); return e; }
278 //void getTail(Node& n, const Edge& e) const { n=tail(e); }
279 //void getHead(Node& n, const Edge& e) const { n=head(e); }
281 //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
282 //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
283 //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
284 //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
285 //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
286 //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
287 //void get_invalid(Node& n) { n=Node(); }
288 //void get_invalid(Edge& e) { e=Edge(); }
289 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
290 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
291 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
293 template< typename It >
300 template< typename It >
301 It first(Node v) const {
307 bool valid(Node n) const { return n.valid(); }
308 bool valid(Edge e) const { return e.valid(); }
310 template <typename It> It getNext(It it) const {
311 It tmp(it); return next(tmp); }
312 template <typename It> It& next(It& it) const { return ++it; }
315 /* for getting id's of graph objects */
316 /* these are important for the implementation of property vectors */
318 int id(Node v) const { return v.node->id; }
319 int id(Edge e) const { return e.edge->id; }
321 /* adding nodes and edges */
323 Node addNode() { return Node(_add_node()); }
324 Edge addEdge(Node u, Node v) {
325 return Edge(_add_edge(u.node, v.node));
329 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
330 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
331 _delete_node(i.node);
334 void erase(Edge e) { _delete_edge(e.edge); }
337 while (first<NodeIt>().valid()) erase(first<NodeIt>());
340 void setTail(Edge e, Node tail) {
341 _set_tail(e.edge, tail.node);
344 void setHead(Edge e, Node head) {
345 _set_head(e.edge, head.node);
348 /* stream operations, for testing purpose */
350 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
351 os << i.node->id; return os;
353 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
354 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
359 friend class ListGraph;
360 template <typename T> friend class NodeMap;
363 friend class OutEdgeIt;
364 friend class InEdgeIt;
365 friend class SymEdgeIt;
366 //public: //FIXME: It is required by op= of NodeIt
370 friend int ListGraph::id(Node v) const;
372 Node() /*: node(0)*/ { }
373 Node(const Invalid&) : node(0) { }
375 Node(node_item* _node) : node(_node) { }
376 bool valid() const { return (node); }
378 //void makeInvalid() { node=0; }
379 friend bool operator==(Node u, Node v) { return v.node==u.node; }
380 friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
381 friend std::ostream& operator<<(std::ostream& os, const Node& i);
384 class NodeIt : public Node {
385 friend class ListGraph;
387 public: //for everybody but marci
388 NodeIt(const ListGraph& G) : Node(G._first_node) { }
390 NodeIt() : Node() { }
391 NodeIt(const Invalid& i) : Node(i) { }
393 NodeIt(node_item* v) : Node(v) { }
394 NodeIt& operator++() { node=node->_next_node; return *this; }
396 // NodeIt& operator=(const Node& e)
397 // { node=e.node; return *this; }
401 friend class ListGraph;
402 template <typename T> friend class EdgeMap;
408 friend int ListGraph::id(Edge e) const;
410 Edge() /*: edge(0)*/ { }
411 Edge(const Invalid&) : edge(0) { }
414 Edge(edge_item* _edge) : edge(_edge) { }
415 bool valid() const { return (edge); }
417 //void makeInvalid() { edge=0; }
418 friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
419 friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
421 Node tailNode() const { return Node(edge->_tail); }
422 Node headNode() const { return Node(edge->_head); }
424 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
427 class EdgeIt : public Edge {
428 friend class ListGraph;
431 EdgeIt(const ListGraph& G) {
432 node_item* v=G._first_node;
433 if (v) edge=v->_first_out_edge; else edge=0;
434 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
437 EdgeIt() : Edge() { }
438 EdgeIt(const Invalid& i) : Edge(i) { }
440 EdgeIt(edge_item* _e) : Edge(_e) { }
441 EdgeIt& operator++() {
442 node_item* v=edge->_tail;
443 edge=edge->_next_out;
444 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
449 class OutEdgeIt : public Edge {
450 friend class ListGraph;
453 protected: //for alpar
454 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
456 OutEdgeIt() : Edge()/*, v(0)*/ { }
457 OutEdgeIt(const Invalid& i) : Edge(i) { }
458 OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
460 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
462 Node aNode() const { return Node(edge->_tail); }
463 Node bNode() const { return Node(edge->_head); }
466 class InEdgeIt : public Edge {
467 friend class ListGraph;
470 protected: //for alpar
471 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
473 InEdgeIt() : Edge()/*, v(0)*/ { }
474 InEdgeIt(const Invalid& i) : Edge(i) { }
475 InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
477 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
479 Node aNode() const { return Node(edge->_head); }
480 Node bNode() const { return Node(edge->_tail); }
483 class SymEdgeIt : public Edge {
484 friend class ListGraph;
485 bool out_or_in; //1 iff out, 0 iff in
489 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
491 edge=_v.node->_first_out_edge;
492 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
495 SymEdgeIt() : Edge() /*, v(0)*/ { }
496 SymEdgeIt(const Invalid& i) : Edge(i) { }
497 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
499 edge=_v.node->_first_out_edge;
500 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
503 SymEdgeIt& operator++() {
505 node_item* v=edge->_tail;
506 edge=edge->_next_out;
507 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
515 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
517 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
522 // template< typename T >
523 // T ListGraph::first() const {
524 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
529 // ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
530 // return firstNode();
534 // ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
535 // return firstEdge();
538 // template< typename T >
539 // T ListGraph::first(ListGraph::Node v) const {
540 // std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl;
545 // ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
546 // return firstOutEdge(v);
550 // ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
551 // return firstInEdge(v);
555 // ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
556 // return firstSymEdge(v);
562 #endif //LIST_GRAPH_H