COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/deba/test_graph.h @ 686:fc8a3393e0d9

Last change on this file since 686:fc8a3393e0d9 was 676:7ec5e7e6c7b4, checked in by Alpar Juttner, 21 years ago

c++ setting for emacs
Empty commands for doxygen

File size: 15.9 KB
RevLine 
[262]1// -*- c++ -*-
2#ifndef HUGO_LIST_GRAPH_H
3#define HUGO_LIST_GRAPH_H
4
5#include <iostream>
6#include <vector>
7
[377]8#include "invalid.h"
[262]9
[674]10#include "map_registry.h"
11#include "array_map_factory.h"
12
13#include "map_defines.h"
[340]14
[262]15namespace hugo {
16
17  template <typename It>
18  int count(It it) {
19    int i=0;
20    for( ; it.valid(); ++it) { ++i; }
21    return i;
22  }
23
24  class ListGraph {
25    class node_item;
26    class edge_item;
27  public:
28    class Node;
29    class NodeIt;
30    class Edge;
31    class EdgeIt;
32    class OutEdgeIt;
33    class InEdgeIt;
34    class SymEdgeIt;
35   
[674]36    typedef ListGraph Graph;
[676]37   
38    CREATE_MAP_REGISTRIES;
39    CREATE_MAPS(ArrayMapFactory);
40   
[262]41  private:
[676]42   
[262]43    int node_id;
44    int edge_id;
45    int _node_num;
46    int _edge_num;
[676]47   
[262]48    node_item* _first_node;
49    node_item* _last_node;
50
51    class node_item {
52      friend class ListGraph;
53      template <typename T> friend class NodeMap;
54     
55      friend class Node;
56      friend class NodeIt;
57      friend class Edge;
58      friend class EdgeIt;
59      friend class OutEdgeIt;
60      friend class InEdgeIt;
61      friend class SymEdgeIt;
62      friend std::ostream& operator<<(std::ostream& os, const Node& i);
63      friend std::ostream& operator<<(std::ostream& os, const Edge& i);
64      //ListGraph* G;
65      int id;
66      edge_item* _first_out_edge;
67      edge_item* _last_out_edge;
68      edge_item* _first_in_edge;
69      edge_item* _last_in_edge;
70      node_item* _next_node;
71      node_item* _prev_node;
72    public:
73      node_item() { }
74    };
75
76    class edge_item {
77      friend class ListGraph;
78      template <typename T> friend class EdgeMap;
79
80      friend class Node;
81      friend class NodeIt;
82      friend class Edge;
83      friend class EdgeIt;
84      friend class OutEdgeIt;
85      friend class InEdgeIt;
86      friend class SymEdgeIt;
87      friend std::ostream& operator<<(std::ostream& os, const Edge& i);
88      //ListGraph* G;
89      int id;
90      node_item* _tail;
91      node_item* _head;
92      edge_item* _next_out;
93      edge_item* _prev_out;
94      edge_item* _next_in;
95      edge_item* _prev_in;
96    public:
97      edge_item() { }
98    };
99
100    node_item* _add_node() {
101      node_item* p=new node_item;
102      p->id=node_id++;
103      p->_first_out_edge=0;
104      p->_last_out_edge=0;
105      p->_first_in_edge=0;
106      p->_last_in_edge=0;
107      p->_prev_node=_last_node;
108      p->_next_node=0;
109      if (_last_node) _last_node->_next_node=p;
110      _last_node=p;
111      if (!_first_node) _first_node=p;
112
113      ++_node_num;
114      return p;
115    }
116
117    edge_item* _add_edge(node_item* _tail, node_item* _head) {
118      edge_item* e=new edge_item;
119      e->id=edge_id++;
120      e->_tail=_tail;
121      e->_head=_head;
122     
123      e->_prev_out=_tail->_last_out_edge;
124      if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
125      _tail->_last_out_edge=e;
126      if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
127      e->_next_out=0;
128 
129      e->_prev_in=_head->_last_in_edge;
130      if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
131      _head->_last_in_edge=e;
132      if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
133      e->_next_in=0;
134
135      ++_edge_num;
136      return e;
137    }
138
139    //deletes a node which has no out edge and no in edge
140    void _delete_node(node_item* v) {
141      if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
142        _last_node=v->_prev_node;
143      if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
144        _first_node=v->_next_node;
145
146      delete v;
147      --_node_num;
148    }
149
150    void _delete_edge(edge_item* e) {
151      if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
152        (e->_tail)->_last_out_edge=e->_prev_out;
153      if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
154        (e->_tail)->_first_out_edge=e->_next_out;
155      if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
156        (e->_head)->_last_in_edge=e->_prev_in;
157      if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
158        (e->_head)->_first_in_edge=e->_next_in;
159
160      delete e;
161      --_edge_num;
162    }
163
164    void _set_tail(edge_item* e, node_item* _tail) {
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     
170      e->_tail=_tail;
171     
172      e->_prev_out=_tail->_last_out_edge;
173      if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
174      _tail->_last_out_edge=e;
175      if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
176      e->_next_out=0;
177    }
178
179    void _set_head(edge_item* e, node_item* _head) {
180      if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
181        (e->_head)->_last_in_edge=e->_prev_in;
182      if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
183        (e->_head)->_first_in_edge=e->_next_in;
184     
185      e->_head=_head;
186     
187      e->_prev_in=_head->_last_in_edge;
188      if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
189      _head->_last_in_edge=e;
190      if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
191      e->_next_in=0;
192    }
193
194  public:
195
196    /* default constructor */
197
[571]198    ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0){ }
[262]199   
200    ~ListGraph() {
201      while (first<NodeIt>().valid()) erase(first<NodeIt>());
202    }
203
204    int nodeNum() const { return _node_num; }
205    int edgeNum() const { return _edge_num; }
206
207    /* functions to construct iterators from the graph, or from each other */
208
209    //NodeIt firstNode() const { return NodeIt(*this); }
210    //EdgeIt firstEdge() const { return EdgeIt(*this); }
211   
212    //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
213    //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
214    //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
215    Node tail(Edge e) const { return e.tailNode(); }
216    Node head(Edge e) const { return e.headNode(); }
217
218    Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
219    Node aNode(const InEdgeIt& e) const { return e.aNode(); }
220    Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
221
222    Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
223    Node bNode(const InEdgeIt& e) const { return e.bNode(); }
224    Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
225
226    //Node invalid_node() { return Node(); }
227    //Edge invalid_edge() { return Edge(); }
228    //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
229    //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
230    //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
231
232    /* same methods in other style */
233    /* for experimental purpose */
234
235    NodeIt& /*getF*/first(NodeIt& v) const {
236      v=NodeIt(*this); return v; }
237    EdgeIt& /*getF*/first(EdgeIt& e) const {
238      e=EdgeIt(*this); return e; }
239    OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const {
240      e=OutEdgeIt(*this, v); return e; }
241    InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const {
242      e=InEdgeIt(*this, v); return e; }
243    SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const {
244      e=SymEdgeIt(*this, v); return e; }
245    //void getTail(Node& n, const Edge& e) const { n=tail(e); }
246    //void getHead(Node& n, const Edge& e) const { n=head(e); }
247
248    //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
249    //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
250    //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
251    //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
252    //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
253    //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
254    //void get_invalid(Node& n) { n=Node(); }
255    //void get_invalid(Edge& e) { e=Edge(); }
256    //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
257    //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
258    //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
259
260    template< typename It >
261    It first() const {
262      It e;
263      /*getF*/first(e);
264      return e;
265    }
266
267    template< typename It >
268    It first(Node v) const {
269      It e;
270      /*getF*/first(e, v);
271      return e;
272    }
273
274    bool valid(Node n) const { return n.valid(); }
275    bool valid(Edge e) const { return e.valid(); }
276   
277    template <typename It> It getNext(It it) const {
278      It tmp(it); return next(tmp); }
279    template <typename It> It& next(It& it) const { return ++it; }
280   
281
282    /* for getting id's of graph objects */
283    /* these are important for the implementation of property vectors */
284
285    int id(Node v) const { return v.node->id; }
286    int id(Edge e) const { return e.edge->id; }
287
288    /* adding nodes and edges */
289
[340]290    Node addNode() {
[627]291      Node n = _add_node();
292      node_maps.add(n);
293      return n;
294    }
[262]295    Edge addEdge(Node u, Node v) {
[627]296      Edge e = _add_edge(u.node, v.node);
297      edge_maps.add(e);
[340]298      return e;
[262]299    }
300
301    void erase(Node i) {
[627]302      node_maps.erase(i);
[262]303      while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
304      while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
305      _delete_node(i.node);
306    }
307 
[340]308    void erase(Edge e) {
[627]309      edge_maps.erase(e);
310      _delete_edge(e.edge);
311    }
[262]312
313    void clear() {
314      while (first<NodeIt>().valid()) erase(first<NodeIt>());
315    }
316
317    void setTail(Edge e, Node tail) {
318      _set_tail(e.edge, tail.node);
319    }
320
321    void setHead(Edge e, Node head) {
322      _set_head(e.edge, head.node);
323    }
324
325    /* stream operations, for testing purpose */
326
327    friend std::ostream& operator<<(std::ostream& os, const Node& i) {
328      os << i.node->id; return os;
329    }
330    friend std::ostream& operator<<(std::ostream& os, const Edge& i) {
331      os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
332      return os;
333    }
334
335    class Node {
336      friend class ListGraph;
337      template <typename T> friend class NodeMap;
338
339      friend class Edge;
340      friend class OutEdgeIt;
341      friend class InEdgeIt;
342      friend class SymEdgeIt;
343      //public:  //FIXME: It is required by op= of NodeIt
344    protected:
345      node_item* node;
346    protected:
347      friend int ListGraph::id(Node v) const;
348    public:
349      Node() /*: node(0)*/ { }
350      Node(const Invalid&) : node(0) { }
351    protected:
352      Node(node_item* _node) : node(_node) { }
353      bool valid() const { return (node); }
354    public:
355      //void makeInvalid() { node=0; }
356      friend bool operator==(Node u, Node v) { return v.node==u.node; }
357      friend bool operator!=(Node u, Node v) { return v.node!=u.node; }
358      friend std::ostream& operator<<(std::ostream& os, const Node& i);
359    };
360   
361    class NodeIt : public Node {
362      friend class ListGraph;
363      //protected:
364    public: //for everybody but marci
365      NodeIt(const ListGraph& G) : Node(G._first_node) { }
366    public:
367      NodeIt() : Node() { }
368      NodeIt(const Invalid& i) : Node(i) { }
369    protected:
370      NodeIt(node_item* v) : Node(v) { }
371      NodeIt& operator++() { node=node->_next_node; return *this; }
372      //FIXME::
373      //      NodeIt& operator=(const Node& e)
374      //      { node=e.node; return *this; }
375    };
376
377    class Edge {
378      friend class ListGraph;
379      template <typename T> friend class EdgeMap;
380     
381      friend class Node;
382      friend class NodeIt;
383    protected:
384      edge_item* edge;
385      friend int ListGraph::id(Edge e) const;
386    public:
387      Edge() /*: edge(0)*/ { }
388      Edge(const Invalid&) : edge(0) { }
389      //Edge() { }
390    protected:
391      Edge(edge_item* _edge) : edge(_edge) { }
392      bool valid() const { return (edge); }
393    public:
394      //void makeInvalid() { edge=0; }
395      friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; }
396      friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; }
397    protected:
398      Node tailNode() const { return Node(edge->_tail); }
399      Node headNode() const { return Node(edge->_head); }
400    public:
401      friend std::ostream& operator<<(std::ostream& os, const Edge& i);
402    };
403   
404    class EdgeIt : public Edge {
405      friend class ListGraph;
406      //protected:
407    public: //for alpar
408      EdgeIt(const ListGraph& G) {
409        node_item* v=G._first_node;
410        if (v) edge=v->_first_out_edge; else edge=0;
411        while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
412      }
413    public:
414      EdgeIt() : Edge() { }
415      EdgeIt(const Invalid& i) : Edge(i) { }
416    protected:
417      EdgeIt(edge_item* _e) : Edge(_e) { }
418      EdgeIt& operator++() {
419        node_item* v=edge->_tail;
420        edge=edge->_next_out;
421        while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
422        return *this;
423      }
424    };
425   
426    class OutEdgeIt : public Edge {
427      friend class ListGraph;
428      //node_item* v;
429      //protected:
430    protected: //for alpar
431      OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
432    public:
433      OutEdgeIt() : Edge()/*, v(0)*/ { }
434      OutEdgeIt(const Invalid& i) : Edge(i) { }
435      OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
436    protected:
437      OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
438    protected:
439      Node aNode() const { return Node(edge->_tail); }
440      Node bNode() const { return Node(edge->_head); }
441    };
442   
443    class InEdgeIt : public Edge {
444      friend class ListGraph;
445      //node_item* v;
446      //protected:
447    protected: //for alpar
448      InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
449    public:
450      InEdgeIt() : Edge()/*, v(0)*/ { }
451      InEdgeIt(const Invalid& i) : Edge(i) { }
452      InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
453    protected:
454      InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
455    protected:
456      Node aNode() const { return Node(edge->_head); }
457      Node bNode() const { return Node(edge->_tail); }
458    };
459
460    class SymEdgeIt : public Edge {
461      friend class ListGraph;
462      bool out_or_in; //1 iff out, 0 iff in
463      //node_item* v;
464      //protected:
465    public: //for alpar
466      SymEdgeIt(const Node& _v) /*: v(_v.node)*/ {
467        out_or_in=1;
468        edge=_v.node->_first_out_edge;
469        if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
470      }
471    public:
472      SymEdgeIt() : Edge() /*, v(0)*/ { }
473      SymEdgeIt(const Invalid& i) : Edge(i) { }
474      SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ {
475        out_or_in=1;
476        edge=_v.node->_first_out_edge;
477        if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
478      }
479    protected:
480      SymEdgeIt& operator++() {
481        if (out_or_in) {
482          node_item* v=edge->_tail;
483          edge=edge->_next_out;
484          if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
485        } else {
486          edge=edge->_next_in;
487        }
488        return *this;
489      }
490    protected:
491      Node aNode() const {
492        return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
493      Node bNode() const {
494        return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
495    };
496
497  };
498
[627]499  //   template< typename T >
500  //   T ListGraph::first() const {
501  //     std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
502  //     return T();
503  //   }
[262]504
[627]505  //   template<>
506  //   ListGraph::NodeIt ListGraph::first<ListGraph::NodeIt>() const {
507  //     return firstNode();
508  //   }
[262]509
[627]510  //   template<>
511  //   ListGraph::EdgeIt ListGraph::first<ListGraph::EdgeIt>() const {
512  //     return firstEdge();
513  //   }
[262]514
[627]515  //   template< typename T >
516  //   T ListGraph::first(ListGraph::Node v) const {
517  //     std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::Node);" << std::endl;
518  //     return T();
519  //   }
[262]520
[627]521  //   template<>
522  //   ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::Node v) const {
523  //     return firstOutEdge(v);
524  //   }
[262]525
[627]526  //   template<>
527  //   ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::Node v) const {
528  //     return firstInEdge(v);
529  //   }
[262]530
[627]531  //   template<>
532  //   ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::Node v) const {
533  //     return firstSymEdge(v);
534  //   }
[262]535
536
537} //namespace hugo
538
539#endif //HUGO_LIST_GRAPH_H
Note: See TracBrowser for help on using the repository browser.