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 n, T a) { container[/*G.id(n)*/n.node->id]=a; }
48 T get(NodeIt n) const { return container[/*G.id(n)*/n.node->id]; }
49 T& operator[](NodeIt n) { return container[/*G.id(n)*/n.node->id]; }
50 const T& operator[](NodeIt n) const { return container[/*G.id(n)*/n.node->id]; }
51 void resize() { container.resize(G.node_id); }
52 void resize(T a) { container.resize(G.node_id, a); }
58 std::vector<T> container;
61 typedef EdgeIt KeyType;
62 EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
63 EdgeMap(const ListGraph& _G, T a) :
64 G(_G), container(G.edge_id, a) { }
65 void set(EdgeIt e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
66 T get(EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; }
67 T& operator[](EdgeIt e) { return container[/*G.id(e)*/e.edge->id]; }
68 const T& operator[](EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; }
69 void resize() { container.resize(G.edge_id); }
70 void resize(T a) { container.resize(G.edge_id, a); }
78 node_item* _first_node;
79 node_item* _last_node;
82 friend class ListGraph;
83 template <typename T> friend class NodeMap;
86 friend class EachNodeIt;
88 friend class EachEdgeIt;
89 friend class OutEdgeIt;
90 friend class InEdgeIt;
91 friend class SymEdgeIt;
92 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
93 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
96 edge_item* _first_out_edge;
97 edge_item* _last_out_edge;
98 edge_item* _first_in_edge;
99 edge_item* _last_in_edge;
100 node_item* _next_node;
101 node_item* _prev_node;
107 friend class ListGraph;
108 template <typename T> friend class EdgeMap;
111 friend class EachNodeIt;
113 friend class EachEdgeIt;
114 friend class OutEdgeIt;
115 friend class InEdgeIt;
116 friend class SymEdgeIt;
117 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
122 edge_item* _next_out;
123 edge_item* _prev_out;
130 node_item* _add_node() {
131 node_item* p=new node_item;
133 p->_first_out_edge=0;
137 p->_prev_node=_last_node;
139 if (_last_node) _last_node->_next_node=p;
141 if (!_first_node) _first_node=p;
147 edge_item* _add_edge(node_item* _tail, node_item* _head) {
148 edge_item* e=new edge_item;
153 e->_prev_out=_tail->_last_out_edge;
154 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
155 _tail->_last_out_edge=e;
156 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
159 e->_prev_in=_head->_last_in_edge;
160 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
161 _head->_last_in_edge=e;
162 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
169 //deletes a node which has no out edge and no in edge
170 void _delete_node(node_item* v) {
171 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
172 _last_node=v->_prev_node;
173 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
174 _first_node=v->_next_node;
180 void _delete_edge(edge_item* e) {
181 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
182 (e->_tail)->_last_out_edge=e->_prev_out;
183 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
184 (e->_tail)->_first_out_edge=e->_next_out;
185 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
186 (e->_head)->_last_in_edge=e->_prev_in;
187 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
188 (e->_head)->_first_in_edge=e->_next_in;
194 void _set_tail(edge_item* e, node_item* _tail) {
195 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
196 (e->_tail)->_last_out_edge=e->_prev_out;
197 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
198 (e->_tail)->_first_out_edge=e->_next_out;
202 e->_prev_out=_tail->_last_out_edge;
203 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
204 _tail->_last_out_edge=e;
205 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
209 void _set_head(edge_item* e, node_item* _head) {
210 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
211 (e->_head)->_last_in_edge=e->_prev_in;
212 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
213 (e->_head)->_first_in_edge=e->_next_in;
217 e->_prev_in=_head->_last_in_edge;
218 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
219 _head->_last_in_edge=e;
220 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
226 /* default constructor */
228 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
231 while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
234 int nodeNum() const { return _node_num; }
235 int edgeNum() const { return _edge_num; }
237 /* functions to construct iterators from the graph, or from each other */
239 //EachNodeIt firstNode() const { return EachNodeIt(*this); }
240 //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); }
242 //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); }
243 //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); }
244 //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); }
245 NodeIt tail(EdgeIt e) const { return e.tailNode(); }
246 NodeIt head(EdgeIt e) const { return e.headNode(); }
248 NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); }
249 NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); }
250 NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
252 NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); }
253 NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); }
254 NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
256 //NodeIt invalid_node() { return NodeIt(); }
257 //EdgeIt invalid_edge() { return EdgeIt(); }
258 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
259 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
260 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
262 /* same methods in other style */
263 /* for experimental purpose */
265 EachNodeIt& getFirst(EachNodeIt& v) const {
266 v=EachNodeIt(*this); return v; }
267 EachEdgeIt& getFirst(EachEdgeIt& e) const {
268 e=EachEdgeIt(*this); return e; }
269 OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const {
270 e=OutEdgeIt(v); return e; }
271 InEdgeIt& getFirst(InEdgeIt& e, NodeIt v) const {
272 e=InEdgeIt(v); return e; }
273 SymEdgeIt& getFirst(SymEdgeIt& e, NodeIt v) const {
274 e=SymEdgeIt(v); return e; }
275 //void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); }
276 //void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); }
278 //void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); }
279 //void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); }
280 //void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); }
281 //void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); }
282 //void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); }
283 //void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); }
284 //void get_invalid(NodeIt& n) { n=NodeIt(); }
285 //void get_invalid(EdgeIt& e) { e=EdgeIt(); }
286 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
287 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
288 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
290 template< typename It >
297 template< typename It >
298 It first(NodeIt v) const {
304 /* for getting id's of graph objects */
305 /* these are important for the implementation of property vectors */
307 int id(NodeIt v) const { return v.node->id; }
308 int id(EdgeIt e) const { return e.edge->id; }
310 /* adding nodes and edges */
312 NodeIt addNode() { return NodeIt(_add_node()); }
313 EdgeIt addEdge(NodeIt u, NodeIt v) {
314 return EdgeIt(_add_edge(u.node, v.node));
317 void erase(NodeIt i) {
318 while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
319 while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
320 _delete_node(i.node);
323 void erase(EdgeIt e) { _delete_edge(e.edge); }
326 while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
329 void setTail(EdgeIt e, NodeIt tail) {
330 _set_tail(e.edge, tail.node);
333 void setHead(EdgeIt e, NodeIt head) {
334 _set_head(e.edge, head.node);
337 /* stream operations, for testing purpose */
339 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) {
340 os << i.node->id; return os;
342 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) {
343 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
348 friend class ListGraph;
349 template <typename T> friend class NodeMap;
352 friend class OutEdgeIt;
353 friend class InEdgeIt;
354 friend class SymEdgeIt;
357 friend int ListGraph::id(NodeIt v) const;
359 NodeIt() : node(0) { }
360 NodeIt(node_item* _node) : node(_node) { }
361 bool valid() const { return (node!=0); }
362 //void makeInvalid() { node=0; }
363 friend bool operator==(const NodeIt& u, const NodeIt& v) {
364 return v.node==u.node;
366 friend bool operator!=(const NodeIt& u, const NodeIt& v) {
367 return v.node!=u.node;
369 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
372 class EachNodeIt : public NodeIt {
373 friend class ListGraph;
375 EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { }
377 EachNodeIt() : NodeIt() { }
378 EachNodeIt(node_item* v) : NodeIt(v) { }
379 EachNodeIt& operator++() { node=node->_next_node; return *this; }
383 friend class ListGraph;
384 template <typename T> friend class EdgeMap;
387 friend class EachNodeIt;
390 friend int ListGraph::id(EdgeIt e) const;
392 EdgeIt() : edge(0) { }
394 EdgeIt(edge_item* _edge) : edge(_edge) { }
395 bool valid() const { return (edge!=0); }
396 //void makeInvalid() { edge=0; }
397 friend bool operator==(const EdgeIt& u, const EdgeIt& v) {
398 return v.edge==u.edge;
400 friend bool operator!=(const EdgeIt& u, const EdgeIt& v) {
401 return v.edge!=u.edge;
404 NodeIt tailNode() const { return NodeIt(edge->_tail); }
405 NodeIt headNode() const { return NodeIt(edge->_head); }
407 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
410 class EachEdgeIt : public EdgeIt {
411 friend class ListGraph;
413 EachEdgeIt(const ListGraph& G) {
414 node_item* v=G._first_node;
415 if (v) edge=v->_first_out_edge; else edge=0;
416 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
419 EachEdgeIt() : EdgeIt() { }
420 EachEdgeIt(edge_item* _e) : EdgeIt(_e) { }
421 EachEdgeIt& operator++() {
422 node_item* v=edge->_tail;
423 edge=edge->_next_out;
424 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
429 class OutEdgeIt : public EdgeIt {
430 friend class ListGraph;
433 OutEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
435 OutEdgeIt() : EdgeIt()/*, v(0)*/ { }
436 OutEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
437 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
439 NodeIt aNode() const { return NodeIt(edge->_tail); }
440 NodeIt bNode() const { return NodeIt(edge->_head); }
443 class InEdgeIt : public EdgeIt {
444 friend class ListGraph;
447 InEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
449 InEdgeIt() : EdgeIt()/*, v(0)*/ { }
450 InEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
451 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
453 NodeIt aNode() const { return NodeIt(edge->_head); }
454 NodeIt bNode() const { return NodeIt(edge->_tail); }
457 class SymEdgeIt : public EdgeIt {
458 friend class ListGraph;
459 bool out_or_in; //1 iff out, 0 iff in
462 SymEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ {
464 edge=_v.node->_first_out_edge;
465 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
468 SymEdgeIt() : EdgeIt() /*, v(0)*/ { }
469 SymEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ {
471 edge=_v.node->_first_out_edge;
472 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
474 SymEdgeIt& operator++() {
476 node_item* v=edge->_tail;
477 edge=edge->_next_out;
478 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
485 NodeIt aNode() const {
486 return (out_or_in) ? NodeIt(edge->_tail) : NodeIt(edge->_head); }
487 NodeIt bNode() const {
488 return (out_or_in) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
494 template< typename T >
495 T ListGraph::first() const {
496 std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
501 ListGraph::EachNodeIt ListGraph::first<ListGraph::EachNodeIt>() const {
506 ListGraph::EachEdgeIt ListGraph::first<ListGraph::EachEdgeIt>() const {
510 template< typename T >
511 T ListGraph::first(ListGraph::NodeIt v) const {
512 std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::NodeIt);" << std::endl;
517 ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::NodeIt v) const {
518 return firstOutEdge(v);
522 ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::NodeIt v) const {
523 return firstInEdge(v);
527 ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::NodeIt v) const {
528 return firstSymEdge(v);
535 #endif //LIST_GRAPH_HH