.
3 #ifndef HUGO_SMART_GRAPH_H
4 #define HUGO_SMART_GRAPH_H
15 ///A smart graph class.
17 ///This is a simple and fast graph implementation.
18 ///It is also quite memory efficient, but at the price
19 ///that <b> it does not support node and edge deletion</b>.
20 ///Apart from this it conforms to the graph interface documented under
21 ///the description of \ref GraphSkeleton.
22 ///\sa \ref GraphSkeleton.
27 int first_in,first_out;
28 NodeT() : first_in(-1), first_out(-1) {}
32 int head, tail, next_in, next_out;
33 //FIXME: is this necessary?
34 EdgeT() : next_in(-1), next_out(-1) {}
37 std::vector<NodeT> nodes;
39 std::vector<EdgeT> edges;
43 template <typename Key> class DynMapBase
48 virtual void add(const Key k) = NULL;
49 virtual void erase(const Key k) = NULL;
50 DynMapBase(const SmartGraph &_G) : G(&_G) {}
51 virtual ~DynMapBase() {}
52 friend class SmartGraph;
56 template <typename T> class EdgeMap;
57 template <typename T> class EdgeMap;
65 ///\bug It must be public because of SymEdgeMap.
67 mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
68 ///\bug It must be public because of SymEdgeMap.
70 mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
79 // class Node { int n; };
80 // class NodeIt : public Node { };
81 // class Edge { int n; };
82 // class EdgeIt : public Edge {};
83 // class OutEdgeIt : public Edge {};
84 // class InEdgeIt : public Edge {};
87 template <typename T> class NodeMap;
88 template <typename T> class EdgeMap;
92 /* default constructor */
94 SmartGraph() : nodes(), edges() { }
95 SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
99 for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
100 i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
101 for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
102 i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
105 int nodeNum() const { return nodes.size(); } //FIXME: What is this?
106 int edgeNum() const { return edges.size(); } //FIXME: What is this?
108 ///\bug This function does something different than
109 ///its name would suggests...
110 int maxNodeId() const { return nodes.size(); } //FIXME: What is this?
111 ///\bug This function does something different than
112 ///its name would suggests...
113 int maxEdgeId() const { return edges.size(); } //FIXME: What is this?
115 Node tail(Edge e) const { return edges[e.n].tail; }
116 Node head(Edge e) const { return edges[e.n].head; }
119 Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
120 Node aNode(InEdgeIt e) const { return edges[e.n].head; }
121 // //Node aNode(const SymEdge& e) const { return e.aNode(); }
124 Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
125 Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
126 // //Node bNode(const SymEdge& e) const { return e.bNode(); }
128 NodeIt& first(NodeIt& v) const {
129 v=NodeIt(*this); return v; }
130 EdgeIt& first(EdgeIt& e) const {
131 e=EdgeIt(*this); return e; }
132 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
133 e=OutEdgeIt(*this,v); return e; }
134 InEdgeIt& first(InEdgeIt& e, const Node v) const {
135 e=InEdgeIt(*this,v); return e; }
137 template< typename It >
138 It first() const { It e; first(e); return e; }
140 template< typename It >
141 It first(Node v) const { It e; first(e,v); return e; }
143 bool valid(Edge e) const { return e.n!=-1; }
144 bool valid(Node n) const { return n.n!=-1; }
146 void setInvalid(Edge &e) { e.n=-1; }
147 void setInvalid(Node &n) { n.n=-1; }
149 template <typename It> It getNext(It it) const
150 { It tmp(it); return next(tmp); }
152 NodeIt& next(NodeIt& it) const {
153 it.n=(it.n+2)%(nodes.size()+1)-1;
156 OutEdgeIt& next(OutEdgeIt& it) const
157 { it.n=edges[it.n].next_out; return it; }
158 InEdgeIt& next(InEdgeIt& it) const
159 { it.n=edges[it.n].next_in; return it; }
160 EdgeIt& next(EdgeIt& it) const { --it.n; return it; }
162 int id(Node v) const { return v.n; }
163 int id(Edge e) const { return e.n; }
166 Node n; n.n=nodes.size();
167 nodes.push_back(NodeT()); //FIXME: Hmmm...
169 for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
170 i!=dyn_node_maps.end(); ++i) (**i).add(n.n);
175 Edge addEdge(Node u, Node v) {
176 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
177 edges[e.n].tail=u.n; edges[e.n].head=v.n;
178 edges[e.n].next_out=nodes[u.n].first_out;
179 edges[e.n].next_in=nodes[v.n].first_in;
180 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
182 for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
183 i!=dyn_edge_maps.end(); ++i) (**i).add(e);
188 void clear() {nodes.clear();edges.clear();}
191 friend class SmartGraph;
192 template <typename T> friend class NodeMap;
195 friend class OutEdgeIt;
196 friend class InEdgeIt;
197 friend class SymEdge;
201 friend int SmartGraph::id(Node v) const;
205 Node (Invalid i) { n=-1; }
206 bool operator==(const Node i) const {return n==i.n;}
207 bool operator!=(const Node i) const {return n!=i.n;}
208 bool operator<(const Node i) const {return n<i.n;}
211 class NodeIt : public Node {
212 friend class SmartGraph;
214 NodeIt(const SmartGraph& G) : Node(G.nodes.size()?0:-1) { }
215 NodeIt() : Node() { }
219 friend class SmartGraph;
220 template <typename T> friend class EdgeMap;
222 //template <typename T> friend class SymSmartGraph::SymEdgeMap;
223 //friend Edge SymSmartGraph::opposite(Edge) const;
229 friend int SmartGraph::id(Edge e) const;
234 Edge (Invalid) { n=-1; }
235 bool operator==(const Edge i) const {return n==i.n;}
236 bool operator!=(const Edge i) const {return n!=i.n;}
237 bool operator<(const Edge i) const {return n<i.n;}
238 ///\bug This is a workaround until somebody tells me how to
239 ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
240 int &idref() {return n;}
241 const int &idref() const {return n;}
244 class EdgeIt : public Edge {
245 friend class SmartGraph;
247 EdgeIt(const SmartGraph& G) : Edge(G.edges.size()-1) { }
248 EdgeIt (Invalid i) : Edge(i) { }
249 EdgeIt() : Edge() { }
250 ///\bug This is a workaround until somebody tells me how to
251 ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
252 int &idref() {return n;}
255 class OutEdgeIt : public Edge {
256 friend class SmartGraph;
258 OutEdgeIt() : Edge() { }
259 OutEdgeIt (Invalid i) : Edge(i) { }
261 OutEdgeIt(const SmartGraph& G,const Node v)
262 : Edge(G.nodes[v.n].first_out) {}
265 class InEdgeIt : public Edge {
266 friend class SmartGraph;
268 InEdgeIt() : Edge() { }
269 InEdgeIt (Invalid i) : Edge(i) { }
270 InEdgeIt(const SmartGraph& G,Node v) :Edge(G.nodes[v.n].first_in){}
275 // // Static Maps are not necessary.
276 // template <typename T>
278 // const SmartGraph& G;
279 // std::vector<T> container;
281 // typedef T ValueType;
282 // typedef Node KeyType;
283 // NodeMap(const SmartGraph& _G) : G(_G), container(G.maxNodeId()) { }
284 // NodeMap(const SmartGraph& _G, T a) :
285 // G(_G), container(G.maxNodeId(), a) { }
286 // void set(Node n, T a) { container[n.n]=a; }
287 // T get(Node n) const { return container[n.n]; }
288 // T& operator[](Node n) { return container[n.n]; }
289 // const T& operator[](Node n) const { return container[n.n]; }
290 // void update() { container.resize(G.maxNodeId()); }
291 // void update(T a) { container.resize(G.maxNodeId(), a); }
294 // template <typename T>
296 // const SmartGraph& G;
297 // std::vector<T> container;
299 // typedef T ValueType;
300 // typedef Edge KeyType;
301 // EdgeMap(const SmartGraph& _G) : G(_G), container(G.maxEdgeId()) { }
302 // EdgeMap(const SmartGraph& _G, T a) :
303 // G(_G), container(G.maxEdgeId(), a) { }
304 // void set(Edge e, T a) { container[e.n]=a; }
305 // T get(Edge e) const { return container[e.n]; }
306 // T& operator[](Edge e) { return container[e.n]; }
307 // const T& operator[](Edge e) const { return container[e.n]; }
308 // void update() { container.resize(G.maxEdgeId()); }
309 // void update(T a) { container.resize(G.maxEdgeId(), a); }
312 template <typename T> class NodeMap : public DynMapBase<Node>
314 std::vector<T> container;
318 typedef Node KeyType;
320 NodeMap(const SmartGraph &_G) :
321 DynMapBase<Node>(_G), container(_G.maxNodeId())
323 G->dyn_node_maps.push_back(this);
325 NodeMap(const SmartGraph &_G,const T &t) :
326 DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
328 G->dyn_node_maps.push_back(this);
331 NodeMap(const NodeMap<T> &m) :
332 DynMapBase<Node>(*m.G), container(m.container)
334 G->dyn_node_maps.push_back(this);
337 template<typename TT> friend class NodeMap;
339 ///\todo It can copy between different types.
341 template<typename TT> NodeMap(const NodeMap<TT> &m) :
342 DynMapBase<Node>(*m.G)
344 G->dyn_node_maps.push_back(this);
345 typename std::vector<TT>::const_iterator i;
346 for(typename std::vector<TT>::const_iterator i=m.container.begin();
347 i!=m.container.end();
349 container.push_back(*i);
354 std::vector<DynMapBase<Node>* >::iterator i;
355 for(i=G->dyn_node_maps.begin();
356 i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
357 //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
358 //A better way to do that: (Is this really important?)
360 *i=G->dyn_node_maps.back();
361 G->dyn_node_maps.pop_back();
366 void add(const Node k)
368 if(k.n>=int(container.size())) container.resize(k.n+1);
371 void erase(const Node k) { }
373 void set(Node n, T a) { container[n.n]=a; }
374 T get(Node n) const { return container[n.n]; }
375 T& operator[](Node n) { return container[n.n]; }
376 const T& operator[](Node n) const { return container[n.n]; }
378 ///\warning There is no safety check at all!
379 ///Using operator = between maps attached to different graph may
380 ///cause serious problem.
381 ///\todo Is this really so?
382 ///\todo It can copy between different types.
383 const NodeMap<T>& operator=(const NodeMap<T> &m)
385 container = m.container;
388 template<typename TT>
389 const NodeMap<T>& operator=(const NodeMap<TT> &m)
391 copy(m.container.begin(), m.container.end(), container.begin());
395 void update() {} //Useless for DynMaps
396 void update(T a) {} //Useless for DynMaps
399 template <typename T> class EdgeMap : public DynMapBase<Edge>
401 std::vector<T> container;
405 typedef Edge KeyType;
407 EdgeMap(const SmartGraph &_G) :
408 DynMapBase<Edge>(_G), container(_G.maxEdgeId())
410 //FIXME: What if there are empty Id's?
411 //FIXME: Can I use 'this' in a constructor?
412 G->dyn_edge_maps.push_back(this);
414 EdgeMap(const SmartGraph &_G,const T &t) :
415 DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
417 G->dyn_edge_maps.push_back(this);
419 EdgeMap(const EdgeMap<T> &m) :
420 DynMapBase<Edge>(*m.G), container(m.container)
422 G->dyn_node_maps.push_back(this);
425 template<typename TT> friend class EdgeMap;
427 ///\todo It can copy between different types.
429 template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
430 DynMapBase<Edge>(*m.G)
432 G->dyn_node_maps.push_back(this);
433 typename std::vector<TT>::const_iterator i;
434 for(typename std::vector<TT>::const_iterator i=m.container.begin();
435 i!=m.container.end();
437 container.push_back(*i);
442 std::vector<DynMapBase<Edge>* >::iterator i;
443 for(i=G->dyn_edge_maps.begin();
444 i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
445 //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
446 //A better way to do that: (Is this really important?)
448 *i=G->dyn_edge_maps.back();
449 G->dyn_edge_maps.pop_back();
454 void add(const Edge k)
456 if(k.n>=int(container.size())) container.resize(k.n+1);
458 void erase(const Edge k) { }
460 void set(Edge n, T a) { container[n.n]=a; }
461 T get(Edge n) const { return container[n.n]; }
462 T& operator[](Edge n) { return container[n.n]; }
463 const T& operator[](Edge n) const { return container[n.n]; }
465 ///\warning There is no safety check at all!
466 ///Using operator = between maps attached to different graph may
467 ///cause serious problem.
468 ///\todo Is this really so?
469 ///\todo It can copy between different types.
470 const EdgeMap<T>& operator=(const EdgeMap<T> &m)
472 container = m.container;
475 template<typename TT>
476 const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
478 copy(m.container.begin(), m.container.end(), container.begin());
482 void update() {} //Useless for DynMaps
483 void update(T a) {} //Useless for DynMaps
488 ///Graph for bidirectional edges.
490 ///The purpose of this graph structure is to handle graphs
491 ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
492 ///of oppositely directed edges.
493 ///There is a new edge map type called
494 ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
495 ///that complements this
497 ///storing shared values for the edge pairs. The usual
498 ///\ref GraphSkeleton::EdgeMap "EdgeMap"
502 ///The oppositely directed edge can also be obtained easily
503 ///using \ref opposite.
504 ///\warning It shares the similarity with \ref SmartGraph that
505 ///it is not possible to delete edges or nodes from the graph.
506 //\sa \ref SmartGraph.
508 class SymSmartGraph : public SmartGraph
511 template<typename T> class SymEdgeMap;
512 template<typename T> friend class SymEdgeMap;
514 SymSmartGraph() : SmartGraph() { }
515 SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
516 Edge addEdge(Node u, Node v)
518 Edge e = SmartGraph::addEdge(u,v);
519 SmartGraph::addEdge(v,u);
523 ///The oppositely directed edge.
525 ///Returns the oppositely directed
526 ///pair of the edge \c e.
527 Edge opposite(Edge e) const
530 f.idref() = e.idref() - 2*(e.idref()%2) + 1;
534 ///Common data storage for the edge pairs.
536 ///This map makes it possible to store data shared by the oppositely
537 ///directed pairs of edges.
538 template <typename T> class SymEdgeMap : public DynMapBase<Edge>
540 std::vector<T> container;
544 typedef Edge KeyType;
546 SymEdgeMap(const SymSmartGraph &_G) :
547 DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
549 static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.push_back(this);
551 SymEdgeMap(const SymSmartGraph &_G,const T &t) :
552 DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
554 G->dyn_edge_maps.push_back(this);
557 SymEdgeMap(const SymEdgeMap<T> &m) :
558 DynMapBase<SymEdge>(*m.G), container(m.container)
560 G->dyn_node_maps.push_back(this);
563 // template<typename TT> friend class SymEdgeMap;
565 ///\todo It can copy between different types.
568 template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m) :
569 DynMapBase<SymEdge>(*m.G)
571 G->dyn_node_maps.push_back(this);
572 typename std::vector<TT>::const_iterator i;
573 for(typename std::vector<TT>::const_iterator i=m.container.begin();
574 i!=m.container.end();
576 container.push_back(*i);
582 std::vector<DynMapBase<Edge>* >::iterator i;
583 for(i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.begin();
584 i!=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.end()
586 //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
587 //A better way to do that: (Is this really important?)
589 *i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.back();
590 static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.pop_back();
595 void add(const Edge k)
597 if(!k.idref()%2&&k.idref()/2>=int(container.size()))
598 container.resize(k.idref()/2+1);
600 void erase(const Edge k) { }
602 void set(Edge n, T a) { container[n.idref()/2]=a; }
603 T get(Edge n) const { return container[n.idref()/2]; }
604 T& operator[](Edge n) { return container[n.idref()/2]; }
605 const T& operator[](Edge n) const { return container[n.idref()/2]; }
607 ///\warning There is no safety check at all!
608 ///Using operator = between maps attached to different graph may
609 ///cause serious problem.
610 ///\todo Is this really so?
611 ///\todo It can copy between different types.
612 const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
614 container = m.container;
617 template<typename TT>
618 const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
620 copy(m.container.begin(), m.container.end(), container.begin());
624 void update() {} //Useless for DynMaps
625 void update(T a) {} //Useless for DynMaps
637 #endif //SMART_GRAPH_H