3 #ifndef HUGO_SMART_GRAPH_H
 
     4 #define HUGO_SMART_GRAPH_H
 
     8 ///\brief SmartGraph and SymSmartGraph classes.
 
    17 /// \addtogroup graphs
 
    21   ///A smart graph class.
 
    23   ///This is a simple and fast graph implementation.
 
    24   ///It is also quite memory efficient, but at the price
 
    25   ///that <b> it does not support node and edge deletion</b>.
 
    26   ///It conforms to the graph interface documented under
 
    27   ///the description of \ref GraphSkeleton.
 
    28   ///\sa \ref GraphSkeleton.
 
    30   ///\todo Some member functions could be \c static.
 
    35       int first_in,first_out;      
 
    36       NodeT() : first_in(-1), first_out(-1) {}
 
    40       int head, tail, next_in, next_out;      
 
    41       //FIXME: is this necessary?
 
    42       EdgeT() : next_in(-1), next_out(-1) {}  
 
    45     std::vector<NodeT> nodes;
 
    47     std::vector<EdgeT> edges;
 
    51     template <typename Key> class DynMapBase
 
    56       virtual void add(const Key k) = 0;
 
    57       virtual void erase(const Key k) = 0;
 
    58       DynMapBase(const SmartGraph &_G) : G(&_G) {}
 
    59       virtual ~DynMapBase() {}
 
    60       friend class SmartGraph;
 
    64     template <typename T> class EdgeMap;
 
    65     template <typename T> class EdgeMap;
 
    73     ///\bug It must be public because of SymEdgeMap.
 
    75     mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
 
    76     ///\bug It must be public because of SymEdgeMap.
 
    78     mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
 
    87     template <typename T> class NodeMap;
 
    88     template <typename T> class EdgeMap;
 
    92     SmartGraph() : nodes(), edges() { }
 
    93     SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
 
    97       for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
 
    98 	  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
 
    99       for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
 
   100 	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
 
   103     int nodeNum() const { return nodes.size(); }  //FIXME: What is this?
 
   104     int edgeNum() const { return edges.size(); }  //FIXME: What is this?
 
   106     ///\bug This function does something different than
 
   107     ///its name would suggests...
 
   108     int maxNodeId() const { return nodes.size(); }  //FIXME: What is this?
 
   109     ///\bug This function does something different than
 
   110     ///its name would suggests...
 
   111     int maxEdgeId() const { return edges.size(); }  //FIXME: What is this?
 
   113     Node tail(Edge e) const { return edges[e.n].tail; }
 
   114     Node head(Edge e) const { return edges[e.n].head; }
 
   116     Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
 
   117     Node aNode(InEdgeIt e) const { return edges[e.n].head; }
 
   119     Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
 
   120     Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
 
   122     NodeIt& first(NodeIt& v) const { 
 
   123       v=NodeIt(*this); return v; }
 
   124     EdgeIt& first(EdgeIt& e) const { 
 
   125       e=EdgeIt(*this); return e; }
 
   126     OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
 
   127       e=OutEdgeIt(*this,v); return e; }
 
   128     InEdgeIt& first(InEdgeIt& e, const Node v) const { 
 
   129       e=InEdgeIt(*this,v); return e; }
 
   131 //     template< typename It >
 
   132 //     It first() const { It e; first(e); return e; }
 
   134 //     template< typename It >
 
   135 //     It first(Node v) const { It e; first(e,v); return e; }
 
   137     bool valid(Edge e) const { return e.n!=-1; }
 
   138     bool valid(Node n) const { return n.n!=-1; }
 
   140     void setInvalid(Edge &e) { e.n=-1; }
 
   141     void setInvalid(Node &n) { n.n=-1; }
 
   143     template <typename It> It getNext(It it) const
 
   144     { It tmp(it); return next(tmp); }
 
   146     NodeIt& next(NodeIt& it) const { 
 
   147       it.n=(it.n+2)%(nodes.size()+1)-1; 
 
   150     OutEdgeIt& next(OutEdgeIt& it) const
 
   151     { it.n=edges[it.n].next_out; return it; }
 
   152     InEdgeIt& next(InEdgeIt& it) const
 
   153     { it.n=edges[it.n].next_in; return it; }
 
   154     EdgeIt& next(EdgeIt& it) const { --it.n; return it; }
 
   156     int id(Node v) const { return v.n; }
 
   157     int id(Edge e) const { return e.n; }
 
   160       Node n; n.n=nodes.size();
 
   161       nodes.push_back(NodeT()); //FIXME: Hmmm...
 
   163       for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
 
   164 	  i!=dyn_node_maps.end(); ++i) (**i).add(n);
 
   169     Edge addEdge(Node u, Node v) {
 
   170       Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
 
   171       edges[e.n].tail=u.n; edges[e.n].head=v.n;
 
   172       edges[e.n].next_out=nodes[u.n].first_out;
 
   173       edges[e.n].next_in=nodes[v.n].first_in;
 
   174       nodes[u.n].first_out=nodes[v.n].first_in=e.n;
 
   176       for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
 
   177 	  i!=dyn_edge_maps.end(); ++i) (**i).add(e);
 
   182     void clear() {nodes.clear();edges.clear();}
 
   185       friend class SmartGraph;
 
   186       template <typename T> friend class NodeMap;
 
   189       friend class OutEdgeIt;
 
   190       friend class InEdgeIt;
 
   191       friend class SymEdge;
 
   195       friend int SmartGraph::id(Node v) const; 
 
   199       Node (Invalid i) { n=-1; }
 
   200       bool operator==(const Node i) const {return n==i.n;}
 
   201       bool operator!=(const Node i) const {return n!=i.n;}
 
   202       bool operator<(const Node i) const {return n<i.n;}
 
   205     class NodeIt : public Node {
 
   206       friend class SmartGraph;
 
   208       NodeIt() : Node() { }
 
   209       NodeIt(Invalid i) : Node(i) { }
 
   210       NodeIt(const SmartGraph& G) : Node(G.nodes.size()?0:-1) { }
 
   214       friend class SmartGraph;
 
   215       template <typename T> friend class EdgeMap;
 
   217       //template <typename T> friend class SymSmartGraph::SymEdgeMap;      
 
   218       //friend Edge SymSmartGraph::opposite(Edge) const;
 
   224       friend int SmartGraph::id(Edge e) const;
 
   229       Edge (Invalid) { n=-1; }
 
   230       bool operator==(const Edge i) const {return n==i.n;}
 
   231       bool operator!=(const Edge i) const {return n!=i.n;}
 
   232       bool operator<(const Edge i) const {return n<i.n;}
 
   233       ///\bug This is a workaround until somebody tells me how to
 
   234       ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
 
   235       int &idref() {return n;}
 
   236       const int &idref() const {return n;}
 
   239     class EdgeIt : public Edge {
 
   240       friend class SmartGraph;
 
   242       EdgeIt(const SmartGraph& G) : Edge(G.edges.size()-1) { }
 
   243       EdgeIt (Invalid i) : Edge(i) { }
 
   244       EdgeIt() : Edge() { }
 
   245       ///\bug This is a workaround until somebody tells me how to
 
   246       ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
 
   247       int &idref() {return n;}
 
   250     class OutEdgeIt : public Edge {
 
   251       friend class SmartGraph;
 
   253       OutEdgeIt() : Edge() { }
 
   254       OutEdgeIt (Invalid i) : Edge(i) { }
 
   256       OutEdgeIt(const SmartGraph& G,const Node v)
 
   257 	: Edge(G.nodes[v.n].first_out) {}
 
   260     class InEdgeIt : public Edge {
 
   261       friend class SmartGraph;
 
   263       InEdgeIt() : Edge() { }
 
   264       InEdgeIt (Invalid i) : Edge(i) { }
 
   265       InEdgeIt(const SmartGraph& G,Node v) :Edge(G.nodes[v.n].first_in){}
 
   268     template <typename T> class NodeMap : public DynMapBase<Node>
 
   270       std::vector<T> container;
 
   274       typedef Node KeyType;
 
   276       NodeMap(const SmartGraph &_G) :
 
   277 	DynMapBase<Node>(_G), container(_G.maxNodeId())
 
   279 	G->dyn_node_maps.push_back(this);
 
   281       NodeMap(const SmartGraph &_G,const T &t) :
 
   282 	DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
 
   284 	G->dyn_node_maps.push_back(this);
 
   287       NodeMap(const NodeMap<T> &m) :
 
   288  	DynMapBase<Node>(*m.G), container(m.container)
 
   290  	G->dyn_node_maps.push_back(this);
 
   293       template<typename TT> friend class NodeMap;
 
   295       ///\todo It can copy between different types.
 
   297       template<typename TT> NodeMap(const NodeMap<TT> &m) :
 
   298 	DynMapBase<Node>(*m.G)
 
   300 	G->dyn_node_maps.push_back(this);
 
   301 	typename std::vector<TT>::const_iterator i;
 
   302 	for(typename std::vector<TT>::const_iterator i=m.container.begin();
 
   303 	    i!=m.container.end();
 
   305 	  container.push_back(*i);
 
   310 	  std::vector<DynMapBase<Node>* >::iterator i;
 
   311 	  for(i=G->dyn_node_maps.begin();
 
   312 	      i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
 
   313 	  //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
 
   314 	  //A better way to do that: (Is this really important?)
 
   316 	    *i=G->dyn_node_maps.back();
 
   317 	    G->dyn_node_maps.pop_back();
 
   322       void add(const Node k) 
 
   324 	if(k.n>=int(container.size())) container.resize(k.n+1);
 
   327       void erase(const Node) { }
 
   329       void set(Node n, T a) { container[n.n]=a; }
 
   330       //'T& operator[](Node n)' would be wrong here
 
   331       typename std::vector<T>::reference
 
   332       operator[](Node n) { return container[n.n]; }
 
   333       //'const T& operator[](Node n)' would be wrong here
 
   334       typename std::vector<T>::const_reference 
 
   335       operator[](Node n) const { return container[n.n]; }
 
   337       ///\warning There is no safety check at all!
 
   338       ///Using operator = between maps attached to different graph may
 
   339       ///cause serious problem.
 
   340       ///\todo Is this really so?
 
   341       ///\todo It can copy between different types.
 
   342       const NodeMap<T>& operator=(const NodeMap<T> &m)
 
   344 	container = m.container;
 
   347       template<typename TT>
 
   348       const NodeMap<T>& operator=(const NodeMap<TT> &m)
 
   350 	copy(m.container.begin(), m.container.end(), container.begin());
 
   354       void update() {}    //Useless for Dynamic Maps
 
   355       void update(T a) {}  //Useless for Dynamic Maps
 
   358     template <typename T> class EdgeMap : public DynMapBase<Edge>
 
   360       std::vector<T> container;
 
   364       typedef Edge KeyType;
 
   366       EdgeMap(const SmartGraph &_G) :
 
   367 	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
 
   369 	//FIXME: What if there are empty Id's?
 
   370 	//FIXME: Can I use 'this' in a constructor?
 
   371 	G->dyn_edge_maps.push_back(this);
 
   373       EdgeMap(const SmartGraph &_G,const T &t) :
 
   374 	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
 
   376 	G->dyn_edge_maps.push_back(this);
 
   378       EdgeMap(const EdgeMap<T> &m) :
 
   379  	DynMapBase<Edge>(*m.G), container(m.container)
 
   381  	G->dyn_node_maps.push_back(this);
 
   384       template<typename TT> friend class EdgeMap;
 
   386       ///\todo It can copy between different types.
 
   388       template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
 
   389 	DynMapBase<Edge>(*m.G)
 
   391 	G->dyn_node_maps.push_back(this);
 
   392 	typename std::vector<TT>::const_iterator i;
 
   393 	for(typename std::vector<TT>::const_iterator i=m.container.begin();
 
   394 	    i!=m.container.end();
 
   396 	  container.push_back(*i);
 
   401 	  std::vector<DynMapBase<Edge>* >::iterator i;
 
   402 	  for(i=G->dyn_edge_maps.begin();
 
   403 	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
 
   404 	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
 
   405 	  //A better way to do that: (Is this really important?)
 
   407 	    *i=G->dyn_edge_maps.back();
 
   408 	    G->dyn_edge_maps.pop_back();
 
   413       void add(const Edge k) 
 
   415 	if(k.n>=int(container.size())) container.resize(k.n+1);
 
   417       void erase(const Edge) { }
 
   419       void set(Edge n, T a) { container[n.n]=a; }
 
   420       //T get(Edge n) const { return container[n.n]; }
 
   421       typename std::vector<T>::reference
 
   422       operator[](Edge n) { return container[n.n]; }
 
   423       typename std::vector<T>::const_reference
 
   424       operator[](Edge n) const { return container[n.n]; }
 
   426       ///\warning There is no safety check at all!
 
   427       ///Using operator = between maps attached to different graph may
 
   428       ///cause serious problem.
 
   429       ///\todo Is this really so?
 
   430       ///\todo It can copy between different types.
 
   431       const EdgeMap<T>& operator=(const EdgeMap<T> &m)
 
   433 	container = m.container;
 
   436       template<typename TT>
 
   437       const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
 
   439 	copy(m.container.begin(), m.container.end(), container.begin());
 
   443       void update() {}    //Useless for DynMaps
 
   444       void update(T a) {}  //Useless for DynMaps
 
   449   ///Graph for bidirectional edges.
 
   451   ///The purpose of this graph structure is to handle graphs
 
   452   ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
 
   453   ///of oppositely directed edges.
 
   454   ///There is a new edge map type called
 
   455   ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
 
   456   ///that complements this
 
   458   ///storing shared values for the edge pairs. The usual
 
   459   ///\ref GraphSkeleton::EdgeMap "EdgeMap"
 
   463   ///The oppositely directed edge can also be obtained easily
 
   464   ///using \ref opposite.
 
   465   ///\warning It shares the similarity with \ref SmartGraph that
 
   466   ///it is not possible to delete edges or nodes from the graph.
 
   467   //\sa \ref SmartGraph.
 
   469   class SymSmartGraph : public SmartGraph
 
   472     template<typename T> class SymEdgeMap;
 
   473     template<typename T> friend class SymEdgeMap;
 
   475     SymSmartGraph() : SmartGraph() { }
 
   476     SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
 
   477     ///Adds a pair of oppositely directed edges to the graph.
 
   478     Edge addEdge(Node u, Node v)
 
   480       Edge e = SmartGraph::addEdge(u,v);
 
   481       SmartGraph::addEdge(v,u);
 
   485     ///The oppositely directed edge.
 
   487     ///Returns the oppositely directed
 
   488     ///pair of the edge \c e.
 
   489     Edge opposite(Edge e) const
 
   492       f.idref() = e.idref() - 2*(e.idref()%2) + 1;
 
   496     ///Common data storage for the edge pairs.
 
   498     ///This map makes it possible to store data shared by the oppositely
 
   499     ///directed pairs of edges.
 
   500     template <typename T> class SymEdgeMap : public DynMapBase<Edge>
 
   502       std::vector<T> container;
 
   506       typedef Edge KeyType;
 
   508       SymEdgeMap(const SymSmartGraph &_G) :
 
   509 	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
 
   511 	static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.push_back(this);
 
   513       SymEdgeMap(const SymSmartGraph &_G,const T &t) :
 
   514 	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
 
   516 	G->dyn_edge_maps.push_back(this);
 
   519       SymEdgeMap(const SymEdgeMap<T> &m) :
 
   520  	DynMapBase<SymEdge>(*m.G), container(m.container)
 
   522  	G->dyn_node_maps.push_back(this);
 
   525       //      template<typename TT> friend class SymEdgeMap;
 
   527       ///\todo It can copy between different types.
 
   530       template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m) :
 
   531 	DynMapBase<SymEdge>(*m.G)
 
   533 	G->dyn_node_maps.push_back(this);
 
   534 	typename std::vector<TT>::const_iterator i;
 
   535 	for(typename std::vector<TT>::const_iterator i=m.container.begin();
 
   536 	    i!=m.container.end();
 
   538 	  container.push_back(*i);
 
   544 	  std::vector<DynMapBase<Edge>* >::iterator i;
 
   545 	  for(i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.begin();
 
   546 	      i!=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.end()
 
   548 	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
 
   549 	  //A better way to do that: (Is this really important?)
 
   551 	    *i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.back();
 
   552 	    static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.pop_back();
 
   557       void add(const Edge k) 
 
   559 	if(!k.idref()%2&&k.idref()/2>=int(container.size()))
 
   560 	  container.resize(k.idref()/2+1);
 
   562       void erase(const Edge k) { }
 
   564       void set(Edge n, T a) { container[n.idref()/2]=a; }
 
   565       //T get(Edge n) const { return container[n.idref()/2]; }
 
   566       typename std::vector<T>::reference
 
   567       operator[](Edge n) { return container[n.idref()/2]; }
 
   568       typename std::vector<T>::const_reference
 
   569       operator[](Edge n) const { return container[n.idref()/2]; }
 
   571       ///\warning There is no safety check at all!
 
   572       ///Using operator = between maps attached to different graph may
 
   573       ///cause serious problem.
 
   574       ///\todo Is this really so?
 
   575       ///\todo It can copy between different types.
 
   576       const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
 
   578 	container = m.container;
 
   581       template<typename TT>
 
   582       const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
 
   584 	copy(m.container.begin(), m.container.end(), container.begin());
 
   588       void update() {}    //Useless for DynMaps
 
   589       void update(T a) {}  //Useless for DynMaps
 
   602 #endif //SMART_GRAPH_H