[189] | 1 | // -*- c++ -*- |
---|
| 2 | #ifndef HUGO_LEDA_GRAPH_WRAPPER_H |
---|
| 3 | #define HUGO_LEDA_GRAPH_WRAPPER_H |
---|
| 4 | |
---|
| 5 | #include <LEDA/graph.h> |
---|
| 6 | #include <LEDA/node_array.h> |
---|
| 7 | #include <LEDA/edge_array.h> |
---|
| 8 | #include <LEDA/node_map.h> |
---|
| 9 | #include <LEDA/edge_map.h> |
---|
| 10 | //#include <LEDA/graph_alg.h> |
---|
| 11 | //#include <LEDA/dimacs.h> |
---|
| 12 | |
---|
| 13 | //#if defined(LEDA_NAMESPACE) |
---|
| 14 | //using namespace leda; |
---|
| 15 | //#endif |
---|
| 16 | |
---|
[616] | 17 | #include <hugo/invalid.h> |
---|
[189] | 18 | |
---|
| 19 | namespace hugo { |
---|
| 20 | |
---|
[617] | 21 | /// \brief A graph wrapper structure for wrapping LEDA graphs in HUGO. |
---|
| 22 | /// |
---|
| 23 | /// This graph wrapper class wraps LEDA graphs and LEDA parametrized graphs |
---|
| 24 | /// to satisfy HUGO graph concepts. |
---|
| 25 | /// Then the generic HUGOlib algorithms and wrappers can be used |
---|
[409] | 26 | /// with LEDA graphs. |
---|
[617] | 27 | /// \ingroup gwrapper |
---|
[189] | 28 | template<typename Graph> |
---|
| 29 | class LedaGraphWrapper |
---|
| 30 | { |
---|
[473] | 31 | protected: |
---|
[617] | 32 | Graph* l_graph; |
---|
| 33 | LedaGraphWrapper() : l_graph(0) { } |
---|
| 34 | void setGraph(Graph& _l_graph) { l_graph=&_l_graph; } |
---|
[189] | 35 | public: |
---|
| 36 | |
---|
| 37 | //LedaGraphWrapper() { } |
---|
[617] | 38 | LedaGraphWrapper(Graph& _l_graph) : l_graph(&_l_graph) { } |
---|
| 39 | LedaGraphWrapper(const LedaGraphWrapper &G) : l_graph(G.l_graph) { } |
---|
[189] | 40 | |
---|
| 41 | template <typename T> class NodeMap; |
---|
| 42 | template <typename T> class EdgeMap; |
---|
| 43 | |
---|
[461] | 44 | class Node; |
---|
| 45 | class NodeIt; |
---|
| 46 | class Edge; |
---|
| 47 | class EdgeIt; |
---|
| 48 | class OutEdgeIt; |
---|
| 49 | class InEdgeIt; |
---|
| 50 | |
---|
[189] | 51 | /// The base type of the node iterators. |
---|
| 52 | class Node { |
---|
[461] | 53 | friend class LedaGraphWrapper<Graph>; |
---|
[189] | 54 | //friend class Edge; |
---|
| 55 | friend class EdgeIt; |
---|
| 56 | friend class InEdgeIt; |
---|
| 57 | friend class OutEdgeIt; |
---|
| 58 | protected: |
---|
| 59 | template <typename T> friend class NodeMap; |
---|
[617] | 60 | leda_node l_n; |
---|
[446] | 61 | public: //FIXME |
---|
[617] | 62 | Node(leda_node _l_n) : l_n(_l_n) { } |
---|
[189] | 63 | public: |
---|
| 64 | /// @warning The default constructor sets the iterator |
---|
| 65 | /// to an undefined value. |
---|
| 66 | Node() {} //FIXME |
---|
| 67 | /// Initialize the iterator to be invalid |
---|
[617] | 68 | Node(Invalid) : l_n(0) { } |
---|
[189] | 69 | //Node(const Node &) {} |
---|
[617] | 70 | bool operator==(Node n) const { return l_n==n.l_n; } //FIXME |
---|
| 71 | bool operator!=(Node n) const { return l_n!=n.l_n; } //FIXME |
---|
| 72 | operator leda_node () { return l_n; } |
---|
[189] | 73 | }; |
---|
| 74 | |
---|
| 75 | /// This iterator goes through each node. |
---|
| 76 | class NodeIt : public Node { |
---|
| 77 | public: |
---|
| 78 | /// @warning The default constructor sets the iterator |
---|
| 79 | /// to an undefined value. |
---|
| 80 | NodeIt() {} //FIXME |
---|
| 81 | /// Initialize the iterator to be invalid |
---|
| 82 | NodeIt(Invalid i) : Node(i) {} |
---|
| 83 | /// Sets the iterator to the first node of \c G. |
---|
[617] | 84 | NodeIt(const LedaGraphWrapper &G) : Node(G.l_graph->first_node()) { } |
---|
[189] | 85 | //NodeIt(const NodeIt &) {} //FIXME |
---|
| 86 | }; |
---|
| 87 | |
---|
| 88 | /// The base type of the edge iterators. |
---|
| 89 | class Edge { |
---|
| 90 | friend class LedaGraphWrapper; |
---|
| 91 | protected: |
---|
| 92 | template <typename T> friend class EdgeMap; |
---|
[617] | 93 | leda_edge l_e; |
---|
[446] | 94 | public: //FIXME |
---|
[617] | 95 | Edge(leda_edge _l_e) : l_e(_l_e) { } |
---|
[189] | 96 | public: |
---|
| 97 | /// @warning The default constructor sets the iterator |
---|
| 98 | /// to an undefined value. |
---|
| 99 | Edge() {} //FIXME |
---|
| 100 | /// Initialize the iterator to be invalid |
---|
[617] | 101 | Edge(Invalid) : l_e(0) {} |
---|
[189] | 102 | //Edge(const Edge &) {} |
---|
[617] | 103 | bool operator==(Edge e) const { return l_e==e.l_e; } //FIXME |
---|
| 104 | bool operator!=(Edge e) const { return l_e!=e.l_e; } //FIXME |
---|
| 105 | operator leda_edge () { return l_e; } |
---|
[189] | 106 | }; |
---|
| 107 | |
---|
| 108 | /// This iterator goes trought the outgoing edges of a certain graph. |
---|
| 109 | |
---|
| 110 | class OutEdgeIt : public Edge { |
---|
| 111 | public: |
---|
| 112 | /// @warning The default constructor sets the iterator |
---|
| 113 | /// to an undefined value. |
---|
| 114 | OutEdgeIt() {} |
---|
| 115 | /// Initialize the iterator to be invalid |
---|
| 116 | OutEdgeIt(Invalid i) : Edge(i) {} |
---|
| 117 | /// This constructor sets the iterator to first outgoing edge. |
---|
| 118 | |
---|
| 119 | /// This constructor set the iterator to the first outgoing edge of |
---|
| 120 | /// node |
---|
| 121 | ///@param n the node |
---|
| 122 | ///@param G the graph |
---|
[617] | 123 | OutEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_adj_edge(n.l_n)) { } |
---|
[189] | 124 | }; |
---|
| 125 | |
---|
| 126 | class InEdgeIt : public Edge { |
---|
| 127 | public: |
---|
| 128 | /// @warning The default constructor sets the iterator |
---|
| 129 | /// to an undefined value. |
---|
| 130 | InEdgeIt() {} |
---|
| 131 | /// Initialize the iterator to be invalid |
---|
| 132 | InEdgeIt(Invalid i) : Edge(i) {} |
---|
[617] | 133 | InEdgeIt(const LedaGraphWrapper & G, Node n) : Edge(G.l_graph->first_in_edge(n.l_n)) { } |
---|
[189] | 134 | }; |
---|
| 135 | |
---|
| 136 | // class SymEdgeIt : public Edge {}; |
---|
| 137 | class EdgeIt : public Edge { |
---|
| 138 | public: |
---|
| 139 | /// @warning The default constructor sets the iterator |
---|
| 140 | /// to an undefined value. |
---|
| 141 | EdgeIt() {} |
---|
| 142 | /// Initialize the iterator to be invalid |
---|
| 143 | EdgeIt(Invalid i) : Edge(i) {} |
---|
[617] | 144 | EdgeIt(const LedaGraphWrapper & G) : Edge(G.l_graph->first_edge()) { } |
---|
[189] | 145 | }; |
---|
| 146 | |
---|
| 147 | /// First node of the graph. |
---|
| 148 | |
---|
| 149 | /// \post \c i and the return value will be the first node. |
---|
| 150 | /// |
---|
| 151 | NodeIt &first(NodeIt &i) const { i=NodeIt(*this); return i; } |
---|
| 152 | |
---|
| 153 | /// The first outgoing edge. |
---|
| 154 | InEdgeIt &first(InEdgeIt &i, Node n) const { |
---|
| 155 | i=InEdgeIt(*this, n); |
---|
| 156 | return i; |
---|
| 157 | } |
---|
| 158 | /// The first incoming edge. |
---|
| 159 | OutEdgeIt &first(OutEdgeIt &i, Node n) const { |
---|
| 160 | i=OutEdgeIt(*this, n); |
---|
| 161 | return i; |
---|
| 162 | } |
---|
| 163 | // SymEdgeIt &first(SymEdgeIt &, Node) const { return i;} |
---|
| 164 | /// The first edge of the Graph. |
---|
| 165 | EdgeIt &first(EdgeIt &i) const { |
---|
| 166 | i=EdgeIt(*this); |
---|
| 167 | return i; } |
---|
| 168 | |
---|
| 169 | // Node getNext(Node) const {} |
---|
| 170 | // InEdgeIt getNext(InEdgeIt) const {} |
---|
| 171 | // OutEdgeIt getNext(OutEdgeIt) const {} |
---|
| 172 | // //SymEdgeIt getNext(SymEdgeIt) const {} |
---|
| 173 | // EdgeIt getNext(EdgeIt) const {} |
---|
| 174 | |
---|
| 175 | /// Go to the next node. |
---|
| 176 | NodeIt &next(NodeIt &i) const { |
---|
[617] | 177 | i.l_n=l_graph->succ_node(i.l_n); |
---|
[189] | 178 | return i; |
---|
| 179 | } |
---|
| 180 | /// Go to the next incoming edge. |
---|
| 181 | InEdgeIt &next(InEdgeIt &i) const { |
---|
[617] | 182 | i.l_e=l_graph->in_succ(i.l_e); |
---|
[189] | 183 | return i; |
---|
| 184 | } |
---|
| 185 | /// Go to the next outgoing edge. |
---|
| 186 | OutEdgeIt &next(OutEdgeIt &i) const { |
---|
[617] | 187 | i.l_e=l_graph->adj_succ(i.l_e); |
---|
[189] | 188 | return i; |
---|
| 189 | } |
---|
| 190 | //SymEdgeIt &next(SymEdgeIt &) const {} |
---|
| 191 | /// Go to the next edge. |
---|
| 192 | EdgeIt &next(EdgeIt &i) const { |
---|
[617] | 193 | i.l_e=l_graph->succ_edge(i.l_e); |
---|
[189] | 194 | return i; |
---|
| 195 | } |
---|
| 196 | |
---|
[409] | 197 | // template< typename It > |
---|
| 198 | // It first() const { |
---|
| 199 | // It e; |
---|
| 200 | // first(e); |
---|
| 201 | // return e; |
---|
| 202 | // } |
---|
[189] | 203 | |
---|
[409] | 204 | // template< typename It > |
---|
| 205 | // It first(Node v) const { |
---|
| 206 | // It e; |
---|
| 207 | // first(e, v); |
---|
| 208 | // return e; |
---|
| 209 | // } |
---|
[189] | 210 | |
---|
| 211 | ///Gives back the head node of an edge. |
---|
| 212 | Node head(Edge e) const { |
---|
[617] | 213 | return Node(l_graph->target(e.l_e)); |
---|
[189] | 214 | } |
---|
| 215 | ///Gives back the tail node of an edge. |
---|
| 216 | Node tail(Edge e) const { |
---|
[617] | 217 | return Node(l_graph->source(e.l_e)); |
---|
[189] | 218 | } |
---|
| 219 | |
---|
| 220 | Node aNode(InEdgeIt e) const { return head(e); } |
---|
| 221 | Node aNode(OutEdgeIt e) const { return tail(e); } |
---|
| 222 | // Node aNode(SymEdgeIt) const {} |
---|
| 223 | |
---|
| 224 | Node bNode(InEdgeIt e) const { return tail(e); } |
---|
| 225 | Node bNode(OutEdgeIt e) const { return head(e); } |
---|
| 226 | // Node bNode(SymEdgeIt) const {} |
---|
| 227 | |
---|
| 228 | /// Checks if a node iterator is valid |
---|
[617] | 229 | bool valid(Node n) const { return n.l_n; } |
---|
[189] | 230 | /// Checks if an edge iterator is valid |
---|
[617] | 231 | bool valid(Edge e) const { return e.l_e; } |
---|
[189] | 232 | |
---|
| 233 | ///Gives back the \e id of a node. |
---|
[617] | 234 | int id(Node n) const { return n.l_n->id(); } |
---|
[189] | 235 | ///Gives back the \e id of an edge. |
---|
[617] | 236 | int id(Edge e) const { return e.l_e->id(); } |
---|
[189] | 237 | |
---|
| 238 | //void setInvalid(Node &) const {}; |
---|
| 239 | //void setInvalid(Edge &) const {}; |
---|
| 240 | |
---|
[617] | 241 | Node addNode() const { return Node(l_graph->new_node()); } |
---|
[189] | 242 | Edge addEdge(Node tail, Node head) const { |
---|
[617] | 243 | return Edge(l_graph->new_edge(tail.l_n, head.l_n)); |
---|
[189] | 244 | } |
---|
| 245 | |
---|
[617] | 246 | void erase(Node n) const { l_graph->del_node(n.l_n); } |
---|
| 247 | void erase(Edge e) const { l_graph->del_edge(e.l_e); } |
---|
[189] | 248 | |
---|
[617] | 249 | void clear() const { l_graph->clear(); } |
---|
[189] | 250 | |
---|
[617] | 251 | int nodeNum() const { return l_graph->number_of_nodes(); } |
---|
| 252 | int edgeNum() const { return l_graph->number_of_edges(); } |
---|
[189] | 253 | |
---|
| 254 | ///Read/write map from the nodes to type \c T. |
---|
| 255 | template<typename T> class NodeMap |
---|
| 256 | { |
---|
| 257 | leda_node_map<T> leda_stuff; |
---|
| 258 | public: |
---|
| 259 | typedef T ValueType; |
---|
| 260 | typedef Node KeyType; |
---|
| 261 | |
---|
[617] | 262 | NodeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {} |
---|
| 263 | NodeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {} |
---|
[189] | 264 | |
---|
[617] | 265 | void set(Node i, T t) { leda_stuff[i.l_n]=t; } |
---|
| 266 | T get(Node i) const { return leda_stuff[i.l_n]; } //FIXME: Is it necessary |
---|
| 267 | T &operator[](Node i) { return leda_stuff[i.l_n]; } |
---|
| 268 | const T &operator[](Node i) const { return leda_stuff[i.l_n]; } |
---|
[189] | 269 | |
---|
| 270 | void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ } |
---|
[617] | 271 | //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary |
---|
[189] | 272 | }; |
---|
| 273 | |
---|
| 274 | ///Read/write map from the edges to type \c T. |
---|
| 275 | template<typename T> class EdgeMap |
---|
| 276 | { |
---|
| 277 | leda_edge_map<T> leda_stuff; |
---|
| 278 | public: |
---|
| 279 | typedef T ValueType; |
---|
| 280 | typedef Edge KeyType; |
---|
| 281 | |
---|
[617] | 282 | EdgeMap(const LedaGraphWrapper &G) : leda_stuff(*(G.l_graph)) {} |
---|
| 283 | EdgeMap(const LedaGraphWrapper &G, T t) : leda_stuff(*(G.l_graph), t) {} |
---|
[189] | 284 | |
---|
[617] | 285 | void set(Edge i, T t) { leda_stuff[i.l_e]=t; } |
---|
| 286 | T get(Edge i) const { return leda_stuff[i.l_e]; } //FIXME: Is it necessary |
---|
| 287 | T &operator[](Edge i) { return leda_stuff[i.l_e]; } |
---|
| 288 | const T &operator[](Edge i) const { return leda_stuff[i.l_e]; } |
---|
[189] | 289 | |
---|
| 290 | void update() { /*leda_stuff.init(leda_stuff.get_graph());*/ } |
---|
[617] | 291 | //void update(T a) { leda_stuff.init(leda_stuff.get_graph()/**(G.l_graph)*/, a); } //FIXME: Is it necessary |
---|
[189] | 292 | }; |
---|
| 293 | |
---|
| 294 | }; |
---|
| 295 | |
---|
[617] | 296 | |
---|
| 297 | /// \brief LEDA graph template. |
---|
| 298 | /// |
---|
| 299 | /// This graph stucture uses LEDA graphs for physical storage. |
---|
| 300 | /// \ingroup graphs |
---|
[473] | 301 | template<typename Graph> |
---|
| 302 | class LedaGraph : public LedaGraphWrapper<Graph> { |
---|
| 303 | typedef LedaGraphWrapper<Graph> Parent; |
---|
| 304 | protected: |
---|
| 305 | Graph gr; |
---|
| 306 | public: |
---|
| 307 | LedaGraph() { |
---|
| 308 | Parent::setGraph(gr); |
---|
| 309 | } |
---|
| 310 | }; |
---|
| 311 | |
---|
[189] | 312 | } //namespace hugo |
---|
| 313 | |
---|
| 314 | #endif // HUGO_LEDA_GRAPH_WRAPPER_H |
---|