src/work/marci/leda/leda_graph_wrapper.h
changeset 643 f8053cb51047
parent 616 31879aac4dc3
child 646 bd7a69231cf8
equal deleted inserted replaced
5:5ce14db04bd4 6:9cf5966cf2d0
    14 //using namespace leda;
    14 //using namespace leda;
    15 //#endif
    15 //#endif
    16 
    16 
    17 #include <hugo/invalid.h>
    17 #include <hugo/invalid.h>
    18 
    18 
    19 /// The namespace of HugoLib
       
    20 namespace hugo {
    19 namespace hugo {
    21 
    20 
    22   // @defgroup empty_graph The LedaGraphWrapper class
    21   /// \brief A graph wrapper structure for wrapping LEDA graphs in HUGO.
    23   // @{
    22   ///
    24 
    23   /// This graph wrapper class wraps LEDA graphs and LEDA parametrized graphs
    25   /// A graph wrapperstructure for wrapping LEDA graphs in HUGO.
    24   /// to satisfy HUGO graph concepts.
    26   
    25   /// Then the generic HUGOlib algorithms and wrappers can be used 
    27   /// This graph wrapper class wraps LEDA graph and LEDA parametrized graph
       
    28   /// and then the generic algorithms and wrappers of HUGO can be used 
       
    29   /// with LEDA graphs. 
    26   /// with LEDA graphs. 
    30   /// This class provides all the common features of a grapf structure,
    27   /// \ingroup gwrapper
    31   /// however completely without implementations or real data structures
       
    32   /// behind the interface.
       
    33   /// All graph algorithms should compile with this class, but it will not
       
    34   /// run properly, of course.
       
    35   ///
       
    36   /// It can be used for checking the interface compatibility,
       
    37   /// or it can serve as a skeleton of a new graph structure.
       
    38   /// 
       
    39   /// Also, you will find here the full documentation of a certain graph
       
    40   /// feature, the documentation of a real graph imlementation
       
    41   /// like @ref ListGraph or
       
    42   /// @ref SmartGraph will just refer to this structure.
       
    43   template<typename Graph>
    28   template<typename Graph>
    44   class LedaGraphWrapper
    29   class LedaGraphWrapper
    45   {
    30   {
    46   protected:
    31   protected:
    47     Graph* _graph;
    32     Graph* l_graph;
    48     LedaGraphWrapper() : _graph(0) { }
    33     LedaGraphWrapper() : l_graph(0) { }
    49     void setGraph(Graph& __graph) { _graph=&__graph; }
    34     void setGraph(Graph& _l_graph) { l_graph=&_l_graph; }
    50   public:
    35   public:
    51    
    36    
    52         //LedaGraphWrapper() { }
    37         //LedaGraphWrapper() { }
    53     LedaGraphWrapper(Graph& __graph) : _graph(&__graph) { }
    38     LedaGraphWrapper(Graph& _l_graph) : l_graph(&_l_graph) { }
    54     LedaGraphWrapper(const LedaGraphWrapper &G) : _graph(G._graph) { }
    39     LedaGraphWrapper(const LedaGraphWrapper &G) : l_graph(G.l_graph) { }
    55 
    40 
    56     template <typename T> class NodeMap;
    41     template <typename T> class NodeMap;
    57     template <typename T> class EdgeMap;
    42     template <typename T> class EdgeMap;
    58 
    43 
    59     class Node;
    44     class Node;
    70       friend class EdgeIt;
    55       friend class EdgeIt;
    71       friend class InEdgeIt;
    56       friend class InEdgeIt;
    72       friend class OutEdgeIt;
    57       friend class OutEdgeIt;
    73     protected:
    58     protected:
    74       template <typename T> friend class NodeMap;
    59       template <typename T> friend class NodeMap;
    75       leda_node _n;
    60       leda_node l_n;
    76     public: //FIXME
    61     public: //FIXME
    77       Node(leda_node __n) : _n(__n) { } 
    62       Node(leda_node _l_n) : l_n(_l_n) { } 
    78     public:
    63     public:
    79       /// @warning The default constructor sets the iterator
    64       /// @warning The default constructor sets the iterator
    80       /// to an undefined value.
    65       /// to an undefined value.
    81       Node() {}   //FIXME
    66       Node() {}   //FIXME
    82       /// Initialize the iterator to be invalid
    67       /// Initialize the iterator to be invalid
    83       Node(Invalid) : _n(0) { }
    68       Node(Invalid) : l_n(0) { }
    84       //Node(const Node &) {} 
    69       //Node(const Node &) {} 
    85       bool operator==(Node n) const { return _n==n._n; } //FIXME
    70       bool operator==(Node n) const { return l_n==n.l_n; } //FIXME
    86       bool operator!=(Node n) const { return _n!=n._n; } //FIXME
    71       bool operator!=(Node n) const { return l_n!=n.l_n; } //FIXME
    87       operator leda_node () { return _n; }
    72       operator leda_node () { return l_n; }
    88     };
    73     };
    89     
    74     
    90     /// This iterator goes through each node.
    75     /// This iterator goes through each node.
    91     class NodeIt : public Node {
    76     class NodeIt : public Node {
    92     public:
    77     public:
    94       /// to an undefined value.
    79       /// to an undefined value.
    95       NodeIt() {} //FIXME
    80       NodeIt() {} //FIXME
    96       /// Initialize the iterator to be invalid
    81       /// Initialize the iterator to be invalid
    97       NodeIt(Invalid i) : Node(i) {}
    82       NodeIt(Invalid i) : Node(i) {}
    98       /// Sets the iterator to the first node of \c G.
    83       /// Sets the iterator to the first node of \c G.
    99       NodeIt(const LedaGraphWrapper &G) : Node(G._graph->first_node()) { }
    84       NodeIt(const LedaGraphWrapper &G) : Node(G.l_graph->first_node()) { }
   100       //NodeIt(const NodeIt &) {} //FIXME
    85       //NodeIt(const NodeIt &) {} //FIXME
   101     };
    86     };
   102     
    87     
   103     /// The base type of the edge iterators.
    88     /// The base type of the edge iterators.
   104     class Edge {
    89     class Edge {
   105       friend class LedaGraphWrapper;
    90       friend class LedaGraphWrapper;
   106     protected:
    91     protected:
   107       template <typename T> friend class EdgeMap;
    92       template <typename T> friend class EdgeMap;
   108       leda_edge _e;
    93       leda_edge l_e;
   109     public: //FIXME
    94     public: //FIXME
   110       Edge(leda_edge __e) : _e(__e) { } 
    95       Edge(leda_edge _l_e) : l_e(_l_e) { } 
   111     public:
    96     public:
   112       /// @warning The default constructor sets the iterator
    97       /// @warning The default constructor sets the iterator
   113       /// to an undefined value.
    98       /// to an undefined value.
   114       Edge() {}   //FIXME
    99       Edge() {}   //FIXME
   115       /// Initialize the iterator to be invalid
   100       /// Initialize the iterator to be invalid
   116       Edge(Invalid) : _e(0) {}
   101       Edge(Invalid) : l_e(0) {}
   117       //Edge(const Edge &) {} 
   102       //Edge(const Edge &) {} 
   118       bool operator==(Edge e) const { return _e==e._e; } //FIXME
   103       bool operator==(Edge e) const { return l_e==e.l_e; } //FIXME
   119       bool operator!=(Edge e) const { return _e!=e._e; } //FIXME 
   104       bool operator!=(Edge e) const { return l_e!=e.l_e; } //FIXME 
   120       operator leda_edge () { return _e; }
   105       operator leda_edge () { return l_e; }
   121     };
   106     };
   122     
   107     
   123     /// This iterator goes trought the outgoing edges of a certain graph.
   108     /// This iterator goes trought the outgoing edges of a certain graph.
   124     
   109     
   125     class OutEdgeIt : public Edge {
   110     class OutEdgeIt : public Edge {
   133     
   118     
   134       /// This constructor set the iterator to the first outgoing edge of
   119       /// This constructor set the iterator to the first outgoing edge of
   135       /// node
   120       /// node
   136       ///@param n the node
   121       ///@param n the node
   137       ///@param G the graph
   122       ///@param G the graph
   138       OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G._graph->first_adj_edge(n._n)) { }
   123       OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_adj_edge(n.l_n)) { }
   139     };
   124     };
   140 
   125 
   141     class InEdgeIt : public Edge {
   126     class InEdgeIt : public Edge {
   142     public:
   127     public:
   143       /// @warning The default constructor sets the iterator
   128       /// @warning The default constructor sets the iterator
   144       /// to an undefined value.
   129       /// to an undefined value.
   145       InEdgeIt() {}
   130       InEdgeIt() {}
   146       /// Initialize the iterator to be invalid
   131       /// Initialize the iterator to be invalid
   147       InEdgeIt(Invalid i) : Edge(i) {}
   132       InEdgeIt(Invalid i) : Edge(i) {}
   148       InEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G._graph->first_in_edge(n._n)) { }
   133       InEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_in_edge(n.l_n)) { }
   149     };
   134     };
   150 
   135 
   151     //  class SymEdgeIt : public Edge {};
   136     //  class SymEdgeIt : public Edge {};
   152     class EdgeIt : public Edge {
   137     class EdgeIt : public Edge {
   153     public:
   138     public:
   154       /// @warning The default constructor sets the iterator
   139       /// @warning The default constructor sets the iterator
   155       /// to an undefined value.
   140       /// to an undefined value.
   156       EdgeIt() {}
   141       EdgeIt() {}
   157       /// Initialize the iterator to be invalid
   142       /// Initialize the iterator to be invalid
   158       EdgeIt(Invalid i) : Edge(i) {}
   143       EdgeIt(Invalid i) : Edge(i) {}
   159       EdgeIt(const LedaGraphWrapper & G) : Edge(G._graph->first_edge()) { }
   144       EdgeIt(const LedaGraphWrapper & G) : Edge(G.l_graph->first_edge()) { }
   160     };
   145     };
   161 
   146 
   162     /// First node of the graph.
   147     /// First node of the graph.
   163 
   148 
   164     /// \post \c i and the return value will be the first node.
   149     /// \post \c i and the return value will be the first node.
   187 //     //SymEdgeIt getNext(SymEdgeIt) const {}
   172 //     //SymEdgeIt getNext(SymEdgeIt) const {}
   188 //     EdgeIt getNext(EdgeIt) const {}
   173 //     EdgeIt getNext(EdgeIt) const {}
   189 
   174 
   190     /// Go to the next node.
   175     /// Go to the next node.
   191     NodeIt &next(NodeIt &i) const { 
   176     NodeIt &next(NodeIt &i) const { 
   192       i._n=_graph->succ_node(i._n); 
   177       i.l_n=l_graph->succ_node(i.l_n); 
   193       return i; 
   178       return i; 
   194     }
   179     }
   195     /// Go to the next incoming edge.
   180     /// Go to the next incoming edge.
   196     InEdgeIt &next(InEdgeIt &i) const { 
   181     InEdgeIt &next(InEdgeIt &i) const { 
   197       i._e=_graph->in_succ(i._e); 
   182       i.l_e=l_graph->in_succ(i.l_e); 
   198       return i;
   183       return i;
   199     }
   184     }
   200     /// Go to the next outgoing edge.
   185     /// Go to the next outgoing edge.
   201     OutEdgeIt &next(OutEdgeIt &i) const { 
   186     OutEdgeIt &next(OutEdgeIt &i) const { 
   202       i._e=_graph->adj_succ(i._e); 
   187       i.l_e=l_graph->adj_succ(i.l_e); 
   203       return i;
   188       return i;
   204     }
   189     }
   205     //SymEdgeIt &next(SymEdgeIt &) const {}
   190     //SymEdgeIt &next(SymEdgeIt &) const {}
   206     /// Go to the next edge.
   191     /// Go to the next edge.
   207     EdgeIt &next(EdgeIt &i) const {      
   192     EdgeIt &next(EdgeIt &i) const {      
   208       i._e=_graph->succ_edge(i._e); 
   193       i.l_e=l_graph->succ_edge(i.l_e); 
   209       return i; 
   194       return i; 
   210     }
   195     }
   211 
   196 
   212 //     template< typename It >
   197 //     template< typename It >
   213 //     It first() const { 
   198 //     It first() const { 
   223 //       return e; 
   208 //       return e; 
   224 //     }
   209 //     }
   225 
   210 
   226     ///Gives back the head node of an edge.
   211     ///Gives back the head node of an edge.
   227     Node head(Edge e) const { 
   212     Node head(Edge e) const { 
   228       return Node(_graph->target(e._e)); 
   213       return Node(l_graph->target(e.l_e)); 
   229     }
   214     }
   230     ///Gives back the tail node of an edge.
   215     ///Gives back the tail node of an edge.
   231     Node tail(Edge e) const { 
   216     Node tail(Edge e) const { 
   232       return Node(_graph->source(e._e)); 
   217       return Node(l_graph->source(e.l_e)); 
   233     }
   218     }
   234   
   219   
   235     Node aNode(InEdgeIt e) const { return head(e); }
   220     Node aNode(InEdgeIt e) const { return head(e); }
   236     Node aNode(OutEdgeIt e) const { return tail(e); }
   221     Node aNode(OutEdgeIt e) const { return tail(e); }
   237     //   Node aNode(SymEdgeIt) const {}
   222     //   Node aNode(SymEdgeIt) const {}
   239     Node bNode(InEdgeIt e) const { return tail(e); }
   224     Node bNode(InEdgeIt e) const { return tail(e); }
   240     Node bNode(OutEdgeIt e) const { return head(e); }
   225     Node bNode(OutEdgeIt e) const { return head(e); }
   241     //   Node bNode(SymEdgeIt) const {}
   226     //   Node bNode(SymEdgeIt) const {}
   242 
   227 
   243     /// Checks if a node iterator is valid
   228     /// Checks if a node iterator is valid
   244     bool valid(Node n) const { return n._n; }
   229     bool valid(Node n) const { return n.l_n; }
   245     /// Checks if an edge iterator is valid
   230     /// Checks if an edge iterator is valid
   246     bool valid(Edge e) const { return e._e; }
   231     bool valid(Edge e) const { return e.l_e; }
   247 
   232 
   248     ///Gives back the \e id of a node.
   233     ///Gives back the \e id of a node.
   249     int id(Node n) const { return n._n->id(); }
   234     int id(Node n) const { return n.l_n->id(); }
   250     ///Gives back the \e id of an edge.
   235     ///Gives back the \e id of an edge.
   251     int id(Edge e) const { return e._e->id(); }
   236     int id(Edge e) const { return e.l_e->id(); }
   252 
   237 
   253     //void setInvalid(Node &) const {};
   238     //void setInvalid(Node &) const {};
   254     //void setInvalid(Edge &) const {};
   239     //void setInvalid(Edge &) const {};
   255   
   240   
   256     Node addNode() const { return Node(_graph->new_node()); }
   241     Node addNode() const { return Node(l_graph->new_node()); }
   257     Edge addEdge(Node tail, Node head) const { 
   242     Edge addEdge(Node tail, Node head) const { 
   258       return Edge(_graph->new_edge(tail._n, head._n));
   243       return Edge(l_graph->new_edge(tail.l_n, head.l_n));
   259     }
   244     }
   260     
   245     
   261     void erase(Node n) const { _graph->del_node(n._n); }
   246     void erase(Node n) const { l_graph->del_node(n.l_n); }
   262     void erase(Edge e) const { _graph->del_edge(e._e); }
   247     void erase(Edge e) const { l_graph->del_edge(e.l_e); }
   263 
   248 
   264     void clear() const { _graph->clear(); }
   249     void clear() const { l_graph->clear(); }
   265 
   250 
   266     int nodeNum() const { return _graph->number_of_nodes(); }
   251     int nodeNum() const { return l_graph->number_of_nodes(); }
   267     int edgeNum() const { return _graph->number_of_edges(); }
   252     int edgeNum() const { return l_graph->number_of_edges(); }
   268 
   253 
   269     ///Read/write map from the nodes to type \c T.
   254     ///Read/write map from the nodes to type \c T.
   270     template<typename T> class NodeMap
   255     template<typename T> class NodeMap
   271     {
   256     {
   272       leda_node_map<T> leda_stuff;
   257       leda_node_map<T> leda_stuff;
   273     public:
   258     public:
   274       typedef T ValueType;
   259       typedef T ValueType;
   275       typedef Node KeyType;
   260       typedef Node KeyType;
   276 
   261 
   277       NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
   262       NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
   278       NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
   263       NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
   279 
   264 
   280       void set(Node i, T t) { leda_stuff[i._n]=t; }
   265       void set(Node i, T t) { leda_stuff[i.l_n]=t; }
   281       T get(Node i) const { return leda_stuff[i._n]; }  //FIXME: Is it necessary
   266       T get(Node i) const { return leda_stuff[i.l_n]; }  //FIXME: Is it necessary
   282       T &operator[](Node i) { return leda_stuff[i._n]; }
   267       T &operator[](Node i) { return leda_stuff[i.l_n]; }
   283       const T &operator[](Node i) const { return leda_stuff[i._n]; }
   268       const T &operator[](Node i) const { return leda_stuff[i.l_n]; }
   284 
   269 
   285       void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
   270       void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
   286       //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); }   //FIXME: Is it necessary
   271       //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); }   //FIXME: Is it necessary
   287     };
   272     };
   288 
   273 
   289     ///Read/write map from the edges to type \c T.
   274     ///Read/write map from the edges to type \c T.
   290     template<typename T> class EdgeMap
   275     template<typename T> class EdgeMap
   291     {
   276     {
   292       leda_edge_map<T> leda_stuff;
   277       leda_edge_map<T> leda_stuff;
   293     public:
   278     public:
   294       typedef T ValueType;
   279       typedef T ValueType;
   295       typedef Edge KeyType;
   280       typedef Edge KeyType;
   296 
   281 
   297       EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G._graph)) {}
   282       EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {}
   298       EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G._graph), t) {}
   283       EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {}
   299 
   284 
   300       void set(Edge i, T t) { leda_stuff[i._e]=t; }
   285       void set(Edge i, T t) { leda_stuff[i.l_e]=t; }
   301       T get(Edge i) const { return leda_stuff[i._e]; }  //FIXME: Is it necessary
   286       T get(Edge i) const { return leda_stuff[i.l_e]; }  //FIXME: Is it necessary
   302       T &operator[](Edge i) { return leda_stuff[i._e]; }
   287       T &operator[](Edge i) { return leda_stuff[i.l_e]; }
   303       const T &operator[](Edge i) const { return leda_stuff[i._e]; }
   288       const T &operator[](Edge i) const { return leda_stuff[i.l_e]; }
   304 
   289 
   305       void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
   290       void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ }
   306       //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G._graph)*/, a); }   //FIXME: Is it necessary
   291       //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); }   //FIXME: Is it necessary
   307     };
   292     };
   308 
   293 
   309   };
   294   };
   310 
   295 
       
   296 
       
   297   /// \brief LEDA graph template.
       
   298   ///
       
   299   /// This graph stucture uses LEDA graphs for physical storage.
       
   300   /// \ingroup graphs
   311   template<typename Graph>
   301   template<typename Graph>
   312   class LedaGraph : public LedaGraphWrapper<Graph> {
   302   class LedaGraph : public LedaGraphWrapper<Graph> {
   313     typedef LedaGraphWrapper<Graph> Parent;
   303     typedef LedaGraphWrapper<Graph> Parent;
   314   protected:
   304   protected:
   315     Graph gr;
   305     Graph gr;
   317     LedaGraph() { 
   307     LedaGraph() { 
   318       Parent::setGraph(gr); 
   308       Parent::setGraph(gr); 
   319     }
   309     }
   320   };
   310   };
   321 
   311 
   322   // @}
       
   323 
       
   324 } //namespace hugo
   312 } //namespace hugo
   325 
   313 
   326 
       
   327 
       
   328 // class EmptyBipGraph : public EmptyGraph
       
   329 // {
       
   330 //   class ANode {};
       
   331 //   class BNode {};
       
   332 
       
   333 //   ANode &next(ANode &) {}
       
   334 //   BNode &next(BNode &) {}
       
   335 
       
   336 //   ANode &getFirst(ANode &) const {}
       
   337 //   BNode &getFirst(BNode &) const {}
       
   338 
       
   339 //   enum NodeClass { A = 0, B = 1 };
       
   340 //   NodeClass getClass(Node n) {}
       
   341 
       
   342 // }
       
   343 
       
   344 #endif // HUGO_LEDA_GRAPH_WRAPPER_H
   314 #endif // HUGO_LEDA_GRAPH_WRAPPER_H