.
11 for( ; it.valid(); ++it) { ++i; }
29 template <typename T> class NodeMap;
30 template <typename T> class EdgeMap;
34 template <typename T> friend class NodeMap;
35 template <typename T> friend class EdgeMap;
40 std::vector<T> container;
43 typedef NodeIt KeyType;
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(NodeIt nit, T a) { container[G.id(nit)]=a; }
48 T get(NodeIt nit) const { return container[G.id(nit)]; }
49 void resize() { container.resize(G.node_id); }
50 void resize(T a) { container.resize(G.node_id, a); }
56 std::vector<T> container;
59 typedef EdgeIt KeyType;
60 EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
61 EdgeMap(const ListGraph& _G, T a) :
62 G(_G), container(G.edge_id, a) { }
63 void set(EdgeIt eit, T a) { container[G.id(eit)]=a; }
64 T get(EdgeIt eit) const { return container[G.id(eit)]; }
65 void resize() { container.resize(G.edge_id); }
66 void resize(T a) { container.resize(G.edge_id, a); }
74 node_item* _first_node;
75 node_item* _last_node;
78 friend class ListGraph;
80 friend class EachNodeIt;
82 friend class EachEdgeIt;
83 friend class OutEdgeIt;
84 friend class InEdgeIt;
85 friend class SymEdgeIt;
86 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
87 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
90 edge_item* _first_out_edge;
91 edge_item* _last_out_edge;
92 edge_item* _first_in_edge;
93 edge_item* _last_in_edge;
94 node_item* _next_node;
95 node_item* _prev_node;
101 friend class ListGraph;
103 friend class EachNodeIt;
105 friend class EachEdgeIt;
106 friend class OutEdgeIt;
107 friend class InEdgeIt;
108 friend class SymEdgeIt;
109 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
114 edge_item* _next_out;
115 edge_item* _prev_out;
122 node_item* _add_node() {
123 node_item* p=new node_item;
125 p->_first_out_edge=0;
129 p->_prev_node=_last_node;
131 if (_last_node) _last_node->_next_node=p;
133 if (!_first_node) _first_node=p;
139 edge_item* _add_edge(node_item* _tail, node_item* _head) {
140 edge_item* e=new edge_item;
145 e->_prev_out=_tail->_last_out_edge;
146 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
147 _tail->_last_out_edge=e;
148 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
151 e->_prev_in=_head->_last_in_edge;
152 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
153 _head->_last_in_edge=e;
154 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
161 //deletes a node which has no out edge and no in edge
162 void _delete_node(node_item* v) {
163 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
164 _last_node=v->_prev_node;
165 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
166 _first_node=v->_next_node;
172 void _delete_edge(edge_item* e) {
173 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
174 (e->_tail)->_last_out_edge=e->_prev_out;
175 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
176 (e->_tail)->_first_out_edge=e->_next_out;
177 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
178 (e->_head)->_last_in_edge=e->_prev_in;
179 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
180 (e->_head)->_first_in_edge=e->_next_in;
186 void _set_tail(edge_item* e, node_item* _tail) {
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;
194 e->_prev_out=_tail->_last_out_edge;
195 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
196 _tail->_last_out_edge=e;
197 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
201 void _set_head(edge_item* e, node_item* _head) {
202 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
203 (e->_head)->_last_in_edge=e->_prev_in;
204 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
205 (e->_head)->_first_in_edge=e->_next_in;
209 e->_prev_in=_head->_last_in_edge;
210 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
211 _head->_last_in_edge=e;
212 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
218 /* default constructor */
220 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
223 while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
226 int nodeNum() const { return _node_num; }
227 int edgeNum() const { return _edge_num; }
229 /* functions to construct iterators from the graph, or from each other */
231 //EachNodeIt firstNode() const { return EachNodeIt(*this); }
232 //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); }
234 //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); }
235 //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); }
236 //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); }
237 NodeIt tail(EdgeIt e) const { return e.tailNode(); }
238 NodeIt head(EdgeIt e) const { return e.headNode(); }
240 NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); }
241 NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); }
242 NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
244 NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); }
245 NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); }
246 NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
248 //NodeIt invalid_node() { return NodeIt(); }
249 //EdgeIt invalid_edge() { return EdgeIt(); }
250 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
251 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
252 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
254 /* same methods in other style */
255 /* for experimental purpose */
257 void getFirst(EachNodeIt& v) const { v=EachNodeIt(*this); }
258 void getFirst(EachEdgeIt& e) const { e=EachEdgeIt(*this); }
259 void getFirst(OutEdgeIt& e, NodeIt v) const { e=OutEdgeIt(v); }
260 void getFirst(InEdgeIt& e, NodeIt v) const { e=InEdgeIt(v); }
261 void getFirst(SymEdgeIt& e, NodeIt v) const { e=SymEdgeIt(v); }
262 //void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); }
263 //void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); }
265 //void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); }
266 //void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); }
267 //void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); }
268 //void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); }
269 //void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); }
270 //void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); }
271 //void get_invalid(NodeIt& n) { n=NodeIt(); }
272 //void get_invalid(EdgeIt& e) { e=EdgeIt(); }
273 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
274 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
275 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
277 template< typename It >
284 template< typename It >
285 It first(NodeIt v) const {
291 /* for getting id's of graph objects */
292 /* these are important for the implementation of property vectors */
294 int id(NodeIt v) const { return v.node->id; }
295 int id(EdgeIt e) const { return e.edge->id; }
297 /* adding nodes and edges */
299 NodeIt addNode() { return NodeIt(_add_node()); }
300 EdgeIt addEdge(NodeIt u, NodeIt v) {
301 return EdgeIt(_add_edge(u.node, v.node));
304 void erase(NodeIt i) {
305 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
306 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
307 _delete_node(i.node);
310 void erase(EdgeIt e) { _delete_edge(e.edge); }
313 while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
316 void setTail(EdgeIt e, NodeIt tail) {
317 _set_tail(e.edge, tail.node);
320 void setHead(EdgeIt e, NodeIt head) {
321 _set_head(e.edge, head.node);
324 /* stream operations, for testing purpose */
326 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) {
327 os << i.node->id; return os;
329 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) {
330 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
335 friend class ListGraph;
338 friend class OutEdgeIt;
339 friend class InEdgeIt;
340 friend class SymEdgeIt;
343 friend int ListGraph::id(NodeIt v) const;
345 NodeIt() : node(0) { }
346 NodeIt(node_item* _node) : node(_node) { }
347 bool valid() const { return (node!=0); }
348 //void makeInvalid() { node=0; }
349 friend bool operator==(const NodeIt& u, const NodeIt& v) {
350 return v.node==u.node;
352 friend bool operator!=(const NodeIt& u, const NodeIt& v) {
353 return v.node!=u.node;
355 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
358 class EachNodeIt : public NodeIt {
359 friend class ListGraph;
361 EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { }
363 EachNodeIt() : NodeIt() { }
364 EachNodeIt(node_item* v) : NodeIt(v) { }
365 EachNodeIt& operator++() { node=node->_next_node; return *this; }
369 friend class ListGraph;
372 friend class EachNodeIt;
375 friend int ListGraph::id(EdgeIt e) const;
377 EdgeIt() : edge(0) { }
379 EdgeIt(edge_item* _edge) : edge(_edge) { }
380 bool valid() const { return (edge!=0); }
381 //void makeInvalid() { edge=0; }
382 friend bool operator==(const EdgeIt& u, const EdgeIt& v) {
383 return v.edge==u.edge;
385 friend bool operator!=(const EdgeIt& u, const EdgeIt& v) {
386 return v.edge!=u.edge;
389 NodeIt tailNode() const { return NodeIt(edge->_tail); }
390 NodeIt headNode() const { return NodeIt(edge->_head); }
392 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
395 class EachEdgeIt : public EdgeIt {
396 friend class ListGraph;
398 EachEdgeIt(const ListGraph& G) {
399 node_item* v=G._first_node;
400 if (v) edge=v->_first_out_edge; else edge=0;
401 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
404 EachEdgeIt() : EdgeIt() { }
405 EachEdgeIt(edge_item* _e) : EdgeIt(_e) { }
406 EachEdgeIt& operator++() {
407 node_item* v=edge->_tail;
408 edge=edge->_next_out;
409 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
414 class OutEdgeIt : public EdgeIt {
415 friend class ListGraph;
418 OutEdgeIt(const NodeIt& _v) : v(_v.node) { edge=v->_first_out_edge; }
420 OutEdgeIt() : EdgeIt(), v(0) { }
421 OutEdgeIt(const ListGraph& G, NodeIt _v) : v(_v.node) { edge=v->_first_out_edge; }
422 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
424 NodeIt aNode() const { return NodeIt(v); }
425 NodeIt bNode() const {
426 return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
429 class InEdgeIt : public EdgeIt {
430 friend class ListGraph;
433 InEdgeIt(const NodeIt& _v) : v(_v.node) { edge=v->_first_in_edge; }
435 InEdgeIt() : EdgeIt(), v(0) { }
436 InEdgeIt(const ListGraph& G, NodeIt _v) : v(_v.node) { edge=v->_first_in_edge; }
437 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
439 NodeIt aNode() const { return NodeIt(v); }
440 NodeIt bNode() const {
441 return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
444 class SymEdgeIt : public EdgeIt {
445 friend class ListGraph;
446 bool out_or_in; //1 iff out, 0 iff in
449 SymEdgeIt(const NodeIt& _v) : v(_v.node) {
451 edge=v->_first_out_edge;
452 if (!edge) { edge=v->_first_in_edge; out_or_in=0; }
455 SymEdgeIt() : EdgeIt(), v(0) { }
456 SymEdgeIt(const ListGraph& G, NodeIt _v) : v(_v.node) {
458 edge=v->_first_out_edge;
459 if (!edge) { edge=v->_first_in_edge; out_or_in=0; }
461 SymEdgeIt& operator++() {
463 edge=edge->_next_out;
464 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
471 NodeIt aNode() const { return NodeIt(v); }
472 NodeIt bNode() const {
473 return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
479 template< typename T >
480 T ListGraph::first() const {
481 std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
486 ListGraph::EachNodeIt ListGraph::first<ListGraph::EachNodeIt>() const {
491 ListGraph::EachEdgeIt ListGraph::first<ListGraph::EachEdgeIt>() const {
495 template< typename T >
496 T ListGraph::first(ListGraph::NodeIt v) const {
497 std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::NodeIt);" << std::endl;
502 ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::NodeIt v) const {
503 return firstOutEdge(v);
507 ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::NodeIt v) const {
508 return firstInEdge(v);
512 ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::NodeIt v) const {
513 return firstSymEdge(v);
520 #endif //LIST_GRAPH_HH