ResGraphWrapper running time comparison test.
2 #ifndef HUGO_LIST_GRAPH_H
3 #define HUGO_LIST_GRAPH_H
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) { }
237 while (this->valid(first(n))) erase(n);
238 //while (first<NodeIt>().valid()) erase(first<NodeIt>());
241 int nodeNum() const { return _node_num; }
242 int edgeNum() const { return _edge_num; }
244 /* functions to construct iterators from the graph, or from each other */
246 //NodeIt firstNode() const { return NodeIt(*this); }
247 //EdgeIt firstEdge() const { return EdgeIt(*this); }
249 //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
250 //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
251 //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
252 Node tail(Edge e) const { return e.tailNode(); }
253 Node head(Edge e) const { return e.headNode(); }
255 Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
256 Node aNode(const InEdgeIt& e) const { return e.aNode(); }
257 Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
259 Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
260 Node bNode(const InEdgeIt& e) const { return e.bNode(); }
261 Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
263 //Node invalid_node() { return Node(); }
264 //Edge invalid_edge() { return Edge(); }
265 //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
266 //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
267 //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
269 /* same methods in other style */
270 /* for experimental purpose */
272 NodeIt& first(NodeIt& v) const {
273 v=NodeIt(*this); return v; }
274 EdgeIt& first(EdgeIt& e) const {
275 e=EdgeIt(*this); return e; }
276 OutEdgeIt& first(OutEdgeIt& e, Node v) const {
277 e=OutEdgeIt(*this, v); return e; }
278 InEdgeIt& first(InEdgeIt& e, Node v) const {
279 e=InEdgeIt(*this, v); return e; }
280 SymEdgeIt& first(SymEdgeIt& e, Node v) const {
281 e=SymEdgeIt(*this, v); return e; }
282 //void getTail(Node& n, const Edge& e) const { n=tail(e); }
283 //void getHead(Node& n, const Edge& e) const { n=head(e); }
285 //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
286 //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
287 //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
288 //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
289 //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
290 //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
291 //void get_invalid(Node& n) { n=Node(); }
292 //void get_invalid(Edge& e) { e=Edge(); }
293 //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
294 //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
295 //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
297 // template< typename It >
298 // It first() const {
304 // template< typename It >
305 // It first(Node v) const {
311 bool valid(Node n) const { return n.valid(); }
312 bool valid(Edge e) const { return e.valid(); }
314 // template <typename It> It getNext(It it) const {
315 // It tmp(it); next(tmp); return tmp; }
316 // NodeIt& next(NodeIt& it) const { return ++it; }
317 // EdgeIt& next(EdgeIt& it) const { return ++it; }
318 // OutEdgeIt& next(OutEdgeIt& it) const { return ++it; }
319 // InEdgeIt& next(InEdgeIt& it) const { return ++it; }
320 // SymEdgeIt& next(SymEdgeIt& it) const { return ++it; }
321 // template <typename It> It& next(It& it) const { return ++it; }
322 template <typename It> It& next(It& it) const { ++it; return it; }
325 /* for getting id's of graph objects */
326 /* these are important for the implementation of property vectors */
328 int id(Node v) const { return v.node->id; }
329 int id(Edge e) const { return e.edge->id; }
331 /* adding nodes and edges */
333 Node addNode() { return Node(_add_node()); }
334 Edge addEdge(Node u, Node v) {
335 return Edge(_add_edge(u.node, v.node));
341 while (this->valid(first(e, i))) erase(e);
345 while (this->valid(first(e, i))) erase(e);
347 //while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
348 //while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
349 _delete_node(i.node);
352 void erase(Edge e) { _delete_edge(e.edge); }
356 while (this->valid(first(e))) erase(e);
357 //while (first<NodeIt>().valid()) erase(first<NodeIt>());
360 void setTail(Edge e, Node tail) {
361 _set_tail(e.edge, tail.node);
364 void setHead(Edge e, Node head) {
365 _set_head(e.edge, head.node);
368 /* stream operations, for testing purpose */
370 friend std::ostream& operator<<(std::ostream& os, const Node& i) {
371 os << i.node->id; return os;
373 friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
374 os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
379 friend class ListGraph;
380 template <typename T> friend class NodeMap;
383 friend class OutEdgeIt;
384 friend class InEdgeIt;
385 friend class SymEdgeIt;
386 //public: //FIXME: It is required by op= of NodeIt
390 friend int ListGraph::id(Node v) const;
392 Node() /*: node(0)*/ { }
393 Node(const Invalid&) : node(0) { }
395 Node(node_item* _node) : node(_node) { }
396 bool valid() const { return (node); }
398 //void makeInvalid() { node=0; }
399 friend bool operator==(Node u, Node v) { return v.node==u.node; }
400 friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
401 friend std::ostream& operator<<(std::ostream& os, const Node& i);
404 class NodeIt : public Node {
405 friend class ListGraph;
407 public: //for everybody but marci
408 NodeIt(const ListGraph& G) : Node(G._first_node) { }
410 NodeIt() : Node() { }
411 NodeIt(const Invalid& i) : Node(i) { }
413 NodeIt(node_item* v) : Node(v) { }
414 NodeIt& operator++() { node=node->_next_node; return *this; }
416 // NodeIt& operator=(const Node& e)
417 // { node=e.node; return *this; }
421 friend class ListGraph;
422 template <typename T> friend class EdgeMap;
428 friend int ListGraph::id(Edge e) const;
430 Edge() /*: edge(0)*/ { }
431 Edge(const Invalid&) : edge(0) { }
434 Edge(edge_item* _edge) : edge(_edge) { }
435 bool valid() const { return (edge); }
437 //void makeInvalid() { edge=0; }
438 friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
439 friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
441 Node tailNode() const { return Node(edge->_tail); }
442 Node headNode() const { return Node(edge->_head); }
444 friend std::ostream& operator<<(std::ostream& os, const Edge& i);
447 class EdgeIt : public Edge {
448 friend class ListGraph;
451 EdgeIt(const ListGraph& G) {
452 node_item* v=G._first_node;
453 if (v) edge=v->_first_out_edge; else edge=0;
454 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
457 EdgeIt() : Edge() { }
458 EdgeIt(const Invalid& i) : Edge(i) { }
460 EdgeIt(edge_item* _e) : Edge(_e) { }
461 EdgeIt& operator++() {
462 node_item* v=edge->_tail;
463 edge=edge->_next_out;
464 while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
469 class OutEdgeIt : public Edge {
470 friend class ListGraph;
473 protected: //for alpar
474 OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
476 OutEdgeIt() : Edge()/*, v(0)*/ { }
477 OutEdgeIt(const Invalid& i) : Edge(i) { }
478 OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
480 OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
482 Node aNode() const { return Node(edge->_tail); }
483 Node bNode() const { return Node(edge->_head); }
486 class InEdgeIt : public Edge {
487 friend class ListGraph;
490 protected: //for alpar
491 InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
493 InEdgeIt() : Edge()/*, v(0)*/ { }
494 InEdgeIt(const Invalid& i) : Edge(i) { }
495 InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
497 InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
499 Node aNode() const { return Node(edge->_head); }
500 Node bNode() const { return Node(edge->_tail); }
503 class SymEdgeIt : public Edge {
504 friend class ListGraph;
505 bool out_or_in; //1 iff out, 0 iff in
508 protected: //for alpar
509 SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
511 edge=_v.node->_first_out_edge;
512 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
515 SymEdgeIt() : Edge() /*, v(0)*/ { }
516 SymEdgeIt(const Invalid& i) : Edge(i) { }
517 SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
519 edge=_v.node->_first_out_edge;
520 if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
523 SymEdgeIt& operator++() {
525 node_item* v=edge->_tail;
526 edge=edge->_next_out;
527 if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
535 return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
537 return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
541 class UndirListGraph : public ListGraph {
543 typedef SymEdgeIt OutEdgeIt;
544 typedef SymEdgeIt InEdgeIt;
549 #endif //HUGO_LIST_GRAPH_H