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