11 for( ; it.valid(); ++it) { ++i; }
29 template <typename ValueType> class NodeMap;
30 template <typename ValueType> class EdgeMap;
34 template <typename ValueType> friend class NodeMap;
35 template <typename ValueType> friend class EdgeMap;
37 template <typename ValueType>
40 std::vector<ValueType> container;
42 NodeMap(const ListGraph& _G) : G(_G), container(_G.node_id) { }
43 NodeMap(const ListGraph& _G, const ValueType a) :
44 G(_G), container(_G.node_id, a) { }
45 void set(const NodeIt nit, const ValueType a) { container[G.id(nit)]=a; }
46 ValueType get(const NodeIt nit) const { return container[G.id(nit)]; }
49 template <typename ValueType>
52 std::vector<ValueType> container;
54 EdgeMap(const ListGraph& _G) : G(_G), container(_G.edge_id) { }
55 EdgeMap(const ListGraph& _G, const ValueType a) :
56 G(_G), container(_G.edge_id, a) { }
57 void set(const EdgeIt eit, const ValueType a) { container[G.id(eit)]=a; }
58 ValueType get(const EdgeIt eit) const { return container[G.id(eit)]; }
66 node_item* _first_node;
67 node_item* _last_node;
70 friend class ListGraph;
72 friend class EachNodeIt;
74 friend class EachEdgeIt;
75 friend class OutEdgeIt;
76 friend class InEdgeIt;
77 friend class SymEdgeIt;
78 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
79 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
82 edge_item* _first_out_edge;
83 edge_item* _last_out_edge;
84 edge_item* _first_in_edge;
85 edge_item* _last_in_edge;
86 node_item* _next_node;
87 node_item* _prev_node;
93 friend class ListGraph;
95 friend class EachNodeIt;
97 friend class EachEdgeIt;
98 friend class OutEdgeIt;
99 friend class InEdgeIt;
100 friend class SymEdgeIt;
101 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
106 edge_item* _next_out;
107 edge_item* _prev_out;
114 node_item* _add_node() {
115 node_item* p=new node_item;
117 p->_first_out_edge=0;
121 p->_prev_node=_last_node;
123 if (_last_node) _last_node->_next_node=p;
125 if (!_first_node) _first_node=p;
131 edge_item* _add_edge(node_item* _tail, node_item* _head) {
132 edge_item* e=new edge_item;
137 e->_prev_out=_tail->_last_out_edge;
138 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
139 _tail->_last_out_edge=e;
140 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
143 e->_prev_in=_head->_last_in_edge;
144 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
145 _head->_last_in_edge=e;
146 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
153 //deletes a node which has no out edge and no in edge
154 void _delete_node(node_item* v) {
155 if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
156 _last_node=v->_prev_node;
157 if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
158 _first_node=v->_next_node;
164 void _delete_edge(edge_item* e) {
165 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
166 (e->_tail)->_last_out_edge=e->_prev_out;
167 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
168 (e->_tail)->_first_out_edge=e->_next_out;
169 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
170 (e->_head)->_last_in_edge=e->_prev_in;
171 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
172 (e->_head)->_first_in_edge=e->_next_in;
178 void _set_tail(edge_item* e, node_item* _tail) {
179 if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
180 (e->_tail)->_last_out_edge=e->_prev_out;
181 if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
182 (e->_tail)->_first_out_edge=e->_next_out;
186 e->_prev_out=_tail->_last_out_edge;
187 if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
188 _tail->_last_out_edge=e;
189 if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
193 void _set_head(edge_item* e, node_item* _head) {
194 if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
195 (e->_head)->_last_in_edge=e->_prev_in;
196 if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
197 (e->_head)->_first_in_edge=e->_next_in;
201 e->_prev_in=_head->_last_in_edge;
202 if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
203 _head->_last_in_edge=e;
204 if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
210 /* default constructor */
212 ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
215 while (first<EachNodeIt>().valid()) deleteNode(first<EachNodeIt>());
218 int nodeNum() const { return _node_num; }
219 int edgeNum() const { return _edge_num; }
221 /* functions to construct iterators from the graph, or from each other */
223 //EachNodeIt firstNode() const { return EachNodeIt(*this); }
224 //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); }
226 //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); }
227 //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); }
228 //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); }
229 NodeIt tail(const EdgeIt e) const { return e.tailNode(); }
230 NodeIt head(const EdgeIt e) const { return e.headNode(); }
232 NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); }
233 NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); }
234 NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
236 NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); }
237 NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); }
238 NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
240 //NodeIt invalid_node() { return NodeIt(); }
241 //EdgeIt invalid_edge() { return EdgeIt(); }
242 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
243 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
244 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
246 /* same methods in other style */
247 /* for experimental purpose */
249 void getFirst(EachNodeIt& v) const { v=EachNodeIt(*this); }
250 void getFirst(EachEdgeIt& e) const { e=EachEdgeIt(*this); }
251 void getFirst(OutEdgeIt& e, const NodeIt& v) const { e=OutEdgeIt(v); }
252 void getFirst(InEdgeIt& e, const NodeIt& v) const { e=InEdgeIt(v); }
253 void getFirst(SymEdgeIt& e, const NodeIt& v) const { e=SymEdgeIt(v); }
254 void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); }
255 void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); }
257 void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); }
258 void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); }
259 void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); }
260 void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); }
261 void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); }
262 void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); }
263 //void get_invalid(NodeIt& n) { n=NodeIt(); }
264 //void get_invalid(EdgeIt& e) { e=EdgeIt(); }
265 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
266 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
267 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
269 template< typename It >
276 template< typename It >
277 It first(const NodeIt v) const {
283 /* for getting id's of graph objects */
284 /* these are important for the implementation of property vectors */
286 int id(const NodeIt v) const { return v.node->id; }
287 int id(const EdgeIt e) const { return e.edge->id; }
289 /* adding nodes and edges */
291 NodeIt addNode() { return NodeIt(_add_node()); }
292 EdgeIt addEdge(const NodeIt u, const NodeIt v) {
293 return EdgeIt(_add_edge(u.node, v.node));
296 void deleteNode(const NodeIt i) {
297 while (first<OutEdgeIt>(i).valid()) deleteEdge(first<OutEdgeIt>(i));
298 while (first<InEdgeIt>(i).valid()) deleteEdge(first<InEdgeIt>(i));
299 _delete_node(i.node);
302 void deleteEdge(const EdgeIt e) { _delete_edge(e.edge); }
304 void setTail(const EdgeIt e, const NodeIt tail) {
305 _set_tail(e.edge, tail.node);
308 void setHead(const EdgeIt e, const NodeIt head) {
309 _set_head(e.edge, head.node);
312 /* stream operations, for testing purpose */
314 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) {
315 os << i.node->id; return os;
317 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) {
318 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
323 friend class ListGraph;
326 friend class OutEdgeIt;
327 friend class InEdgeIt;
328 friend class SymEdgeIt;
331 friend int ListGraph::id(const NodeIt v) const;
333 NodeIt() : node(0) { }
334 NodeIt(node_item* _node) : node(_node) { }
335 bool valid() const { return (node!=0); }
336 //void makeInvalid() { node=0; }
337 friend bool operator==(const NodeIt& u, const NodeIt& v) {
338 return v.node==u.node;
340 friend bool operator!=(const NodeIt& u, const NodeIt& v) {
341 return v.node!=u.node;
343 friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
346 class EachNodeIt : public NodeIt {
347 friend class ListGraph;
349 EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { }
351 EachNodeIt() : NodeIt() { }
352 EachNodeIt(node_item* v) : NodeIt(v) { }
353 EachNodeIt& operator++() { node=node->_next_node; return *this; }
357 friend class ListGraph;
360 friend class EachNodeIt;
363 friend int ListGraph::id(const EdgeIt e) const;
365 EdgeIt() : edge(0) { }
367 EdgeIt(edge_item* _edge) : edge(_edge) { }
368 bool valid() const { return (edge!=0); }
369 //void makeInvalid() { edge=0; }
370 friend bool operator==(const EdgeIt& u, const EdgeIt& v) {
371 return v.edge==u.edge;
373 friend bool operator!=(const EdgeIt& u, const EdgeIt& v) {
374 return v.edge!=u.edge;
377 NodeIt tailNode() const { return NodeIt(edge->_tail); }
378 NodeIt headNode() const { return NodeIt(edge->_head); }
380 friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
383 class EachEdgeIt : public EdgeIt {
384 friend class ListGraph;
386 EachEdgeIt(const ListGraph& G) {
387 node_item* v=G._first_node;
388 if (v) edge=v->_first_out_edge; else edge=0;
389 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
392 EachEdgeIt() : EdgeIt() { }
393 EachEdgeIt(edge_item* _e) : EdgeIt(_e) { }
394 EachEdgeIt& operator++() {
395 node_item* v=edge->_tail;
396 edge=edge->_next_out;
397 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
402 class OutEdgeIt : public EdgeIt {
403 friend class ListGraph;
406 OutEdgeIt(const NodeIt& _v) : v(_v.node) { edge=v->_first_out_edge; }
408 OutEdgeIt() : EdgeIt(), v(0) { }
409 OutEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { edge=v->_first_out_edge; }
410 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
412 NodeIt aNode() const { return NodeIt(v); }
413 NodeIt bNode() const {
414 return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
417 class InEdgeIt : public EdgeIt {
418 friend class ListGraph;
421 InEdgeIt(const NodeIt& _v) : v(_v.node) { edge=v->_first_in_edge; }
423 InEdgeIt() : EdgeIt(), v(0) { }
424 InEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { edge=v->_first_in_edge; }
425 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
427 NodeIt aNode() const { return NodeIt(v); }
428 NodeIt bNode() const {
429 return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
432 class SymEdgeIt : public EdgeIt {
433 friend class ListGraph;
434 bool out_or_in; //1 iff out, 0 iff in
437 SymEdgeIt(const NodeIt& _v) : v(_v.node) {
439 edge=v->_first_out_edge;
440 if (!edge) { edge=v->_first_in_edge; out_or_in=0; }
443 SymEdgeIt() : EdgeIt(), v(0) { }
444 SymEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) {
446 edge=v->_first_out_edge;
447 if (!edge) { edge=v->_first_in_edge; out_or_in=0; }
449 SymEdgeIt& operator++() {
451 edge=edge->_next_out;
452 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
459 NodeIt aNode() const { return NodeIt(v); }
460 NodeIt bNode() const {
461 return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
467 template< typename T >
468 T ListGraph::first() const {
469 std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
474 ListGraph::EachNodeIt ListGraph::first<ListGraph::EachNodeIt>() const {
479 ListGraph::EachEdgeIt ListGraph::first<ListGraph::EachEdgeIt>() const {
483 template< typename T >
484 T ListGraph::first(ListGraph::NodeIt v) const {
485 std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::NodeIt);" << std::endl;
490 ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::NodeIt v) const {
491 return firstOutEdge(v);
495 ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::NodeIt v) const {
496 return firstInEdge(v);
500 ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::NodeIt v) const {
501 return firstSymEdge(v);
508 #endif //LIST_GRAPH_HH