alpar@105: // -*- mode:C++ -*-
alpar@105: 
alpar@185: #ifndef HUGO_SMART_GRAPH_H
alpar@185: #define HUGO_SMART_GRAPH_H
alpar@104: 
alpar@407: ///ingroup graphs
alpar@242: ///\file
alpar@242: ///\brief SmartGraph and SymSmartGraph classes.
alpar@242: 
alpar@104: #include <vector>
alpar@129: #include <limits.h>
alpar@104: 
alpar@253: #include "invalid.h"
alpar@157: 
alpar@105: namespace hugo {
alpar@104: 
alpar@407: /// \addtogroup graphs
alpar@407: /// @{
alpar@185:   class SymSmartGraph;
alpar@185: 
alpar@186:   ///A smart graph class.
alpar@186: 
alpar@186:   ///This is a simple and fast graph implementation.
alpar@186:   ///It is also quite memory efficient, but at the price
alpar@186:   ///that <b> it does not support node and edge deletion</b>.
alpar@242:   ///It conforms to the graph interface documented under
alpar@186:   ///the description of \ref GraphSkeleton.
alpar@186:   ///\sa \ref GraphSkeleton.
alpar@402:   ///
alpar@402:   ///\todo Some member functions could be \c static.
alpar@104:   class SmartGraph {
alpar@104: 
alpar@104:     struct NodeT 
alpar@104:     {
alpar@104:       int first_in,first_out;      
alpar@157:       NodeT() : first_in(-1), first_out(-1) {}
alpar@104:     };
alpar@104:     struct EdgeT 
alpar@104:     {
alpar@104:       int head, tail, next_in, next_out;      
alpar@104:       //FIXME: is this necessary?
alpar@157:       EdgeT() : next_in(-1), next_out(-1) {}  
alpar@104:     };
alpar@104: 
alpar@104:     std::vector<NodeT> nodes;
alpar@129: 
alpar@104:     std::vector<EdgeT> edges;
alpar@104:     
alpar@185:     protected:
alpar@185:     
alpar@108:     template <typename Key> class DynMapBase
alpar@108:     {
alpar@108:     protected:
alpar@185:       const SmartGraph* G; 
alpar@108:     public:
marci@415:       virtual void add(const Key k) = 0;
marci@415:       virtual void erase(const Key k) = 0;
alpar@157:       DynMapBase(const SmartGraph &_G) : G(&_G) {}
alpar@108:       virtual ~DynMapBase() {}
alpar@108:       friend class SmartGraph;
alpar@108:     };
alpar@185:     
alpar@104:   public:
alpar@185:     template <typename T> class EdgeMap;
alpar@185:     template <typename T> class EdgeMap;
alpar@104: 
alpar@164:     class Node;
alpar@164:     class Edge;
alpar@108: 
alpar@185:     //  protected:
alpar@185:     // HELPME:
alpar@186:   protected:
alpar@185:     ///\bug It must be public because of SymEdgeMap.
alpar@185:     ///
alpar@164:     mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
alpar@185:     ///\bug It must be public because of SymEdgeMap.
alpar@185:     ///
alpar@164:     mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
alpar@108:     
alpar@108:   public:
alpar@108: 
alpar@164:     class NodeIt;
alpar@164:     class EdgeIt;
alpar@104:     class OutEdgeIt;
alpar@104:     class InEdgeIt;
alpar@104:     
alpar@105:     template <typename T> class NodeMap;
alpar@104:     template <typename T> class EdgeMap;
alpar@104:     
alpar@104:   public:
alpar@104: 
alpar@104:     SmartGraph() : nodes(), edges() { }
alpar@136:     SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
alpar@104:     
alpar@108:     ~SmartGraph()
alpar@108:     {
alpar@164:       for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@108: 	  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
alpar@164:       for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@108: 	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
alpar@108:     }
alpar@104: 
alpar@104:     int nodeNum() const { return nodes.size(); }  //FIXME: What is this?
alpar@104:     int edgeNum() const { return edges.size(); }  //FIXME: What is this?
alpar@104: 
alpar@186:     ///\bug This function does something different than
alpar@186:     ///its name would suggests...
alpar@108:     int maxNodeId() const { return nodes.size(); }  //FIXME: What is this?
alpar@186:     ///\bug This function does something different than
alpar@186:     ///its name would suggests...
alpar@108:     int maxEdgeId() const { return edges.size(); }  //FIXME: What is this?
alpar@108: 
alpar@164:     Node tail(Edge e) const { return edges[e.n].tail; }
alpar@164:     Node head(Edge e) const { return edges[e.n].head; }
alpar@104: 
marci@174:     Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
marci@174:     Node aNode(InEdgeIt e) const { return edges[e.n].head; }
alpar@104: 
marci@174:     Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
marci@174:     Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
alpar@104: 
alpar@164:     NodeIt& first(NodeIt& v) const { 
alpar@164:       v=NodeIt(*this); return v; }
alpar@164:     EdgeIt& first(EdgeIt& e) const { 
alpar@164:       e=EdgeIt(*this); return e; }
alpar@164:     OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@104:       e=OutEdgeIt(*this,v); return e; }
alpar@164:     InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@104:       e=InEdgeIt(*this,v); return e; }
alpar@104: 
marci@353: //     template< typename It >
marci@353: //     It first() const { It e; first(e); return e; }
alpar@104: 
marci@353: //     template< typename It >
marci@353: //     It first(Node v) const { It e; first(e,v); return e; }
alpar@104: 
alpar@164:     bool valid(Edge e) const { return e.n!=-1; }
alpar@164:     bool valid(Node n) const { return n.n!=-1; }
alpar@104:     
alpar@164:     void setInvalid(Edge &e) { e.n=-1; }
alpar@164:     void setInvalid(Node &n) { n.n=-1; }
alpar@129:     
alpar@157:     template <typename It> It getNext(It it) const
alpar@157:     { It tmp(it); return next(tmp); }
alpar@104: 
marci@174:     NodeIt& next(NodeIt& it) const { 
marci@174:       it.n=(it.n+2)%(nodes.size()+1)-1; 
marci@174:       return it; 
marci@174:     }
alpar@157:     OutEdgeIt& next(OutEdgeIt& it) const
alpar@104:     { it.n=edges[it.n].next_out; return it; }
alpar@157:     InEdgeIt& next(InEdgeIt& it) const
alpar@104:     { it.n=edges[it.n].next_in; return it; }
alpar@164:     EdgeIt& next(EdgeIt& it) const { --it.n; return it; }
alpar@104: 
alpar@164:     int id(Node v) const { return v.n; }
alpar@164:     int id(Edge e) const { return e.n; }
alpar@104: 
alpar@164:     Node addNode() {
alpar@164:       Node n; n.n=nodes.size();
alpar@104:       nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@108: 
alpar@164:       for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@398: 	  i!=dyn_node_maps.end(); ++i) (**i).add(n);
alpar@108: 
alpar@104:       return n;
alpar@104:     }
alpar@108:     
alpar@164:     Edge addEdge(Node u, Node v) {
alpar@164:       Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@104:       edges[e.n].tail=u.n; edges[e.n].head=v.n;
alpar@104:       edges[e.n].next_out=nodes[u.n].first_out;
alpar@104:       edges[e.n].next_in=nodes[v.n].first_in;
alpar@104:       nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108: 
alpar@164:       for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@157: 	  i!=dyn_edge_maps.end(); ++i) (**i).add(e);
alpar@108: 
alpar@104:       return e;
alpar@104:     }
alpar@104: 
alpar@104:     void clear() {nodes.clear();edges.clear();}
alpar@104: 
alpar@164:     class Node {
alpar@104:       friend class SmartGraph;
alpar@104:       template <typename T> friend class NodeMap;
alpar@104:       
alpar@164:       friend class Edge;
alpar@104:       friend class OutEdgeIt;
alpar@104:       friend class InEdgeIt;
alpar@164:       friend class SymEdge;
alpar@104: 
alpar@104:     protected:
alpar@104:       int n;
alpar@164:       friend int SmartGraph::id(Node v) const; 
alpar@164:       Node(int nn) {n=nn;}
alpar@104:     public:
alpar@164:       Node() {}
alpar@164:       Node (Invalid i) { n=-1; }
alpar@164:       bool operator==(const Node i) const {return n==i.n;}
alpar@164:       bool operator!=(const Node i) const {return n!=i.n;}
alpar@164:       bool operator<(const Node i) const {return n<i.n;}
alpar@104:     };
alpar@104:     
alpar@164:     class NodeIt : public Node {
alpar@104:       friend class SmartGraph;
alpar@104:     public:
alpar@402:       NodeIt() : Node() { }
alpar@402:       NodeIt(Invalid i) : Node(i) { }
alpar@164:       NodeIt(const SmartGraph& G) : Node(G.nodes.size()?0:-1) { }
alpar@104:     };
alpar@104: 
alpar@164:     class Edge {
alpar@104:       friend class SmartGraph;
alpar@104:       template <typename T> friend class EdgeMap;
alpar@185: 
alpar@185:       //template <typename T> friend class SymSmartGraph::SymEdgeMap;      
alpar@185:       //friend Edge SymSmartGraph::opposite(Edge) const;
alpar@104:       
alpar@164:       friend class Node;
alpar@104:       friend class NodeIt;
alpar@104:     protected:
alpar@104:       int n;
alpar@164:       friend int SmartGraph::id(Edge e) const;
alpar@157: 
alpar@164:       Edge(int nn) {n=nn;}
alpar@104:     public:
alpar@164:       Edge() { }
marci@174:       Edge (Invalid) { n=-1; }
alpar@164:       bool operator==(const Edge i) const {return n==i.n;}
alpar@164:       bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164:       bool operator<(const Edge i) const {return n<i.n;}
alpar@185:       ///\bug This is a workaround until somebody tells me how to
alpar@185:       ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
alpar@185:       int &idref() {return n;}
alpar@185:       const int &idref() const {return n;}
alpar@104:     };
alpar@104:     
alpar@164:     class EdgeIt : public Edge {
alpar@104:       friend class SmartGraph;
alpar@104:     public:
alpar@164:       EdgeIt(const SmartGraph& G) : Edge(G.edges.size()-1) { }
alpar@164:       EdgeIt (Invalid i) : Edge(i) { }
alpar@164:       EdgeIt() : Edge() { }
alpar@185:       ///\bug This is a workaround until somebody tells me how to
alpar@185:       ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
alpar@185:       int &idref() {return n;}
alpar@104:     };
alpar@104:     
alpar@164:     class OutEdgeIt : public Edge {
alpar@104:       friend class SmartGraph;
alpar@104:     public: 
alpar@164:       OutEdgeIt() : Edge() { }
alpar@164:       OutEdgeIt (Invalid i) : Edge(i) { }
alpar@157: 
alpar@164:       OutEdgeIt(const SmartGraph& G,const Node v)
alpar@164: 	: Edge(G.nodes[v.n].first_out) {}
alpar@104:     };
alpar@104:     
alpar@164:     class InEdgeIt : public Edge {
alpar@104:       friend class SmartGraph;
alpar@104:     public: 
alpar@164:       InEdgeIt() : Edge() { }
alpar@164:       InEdgeIt (Invalid i) : Edge(i) { }
alpar@164:       InEdgeIt(const SmartGraph& G,Node v) :Edge(G.nodes[v.n].first_in){}
alpar@104:     };
alpar@105: 
alpar@185:     template <typename T> class NodeMap : public DynMapBase<Node>
alpar@108:     {
alpar@108:       std::vector<T> container;
alpar@105: 
alpar@108:     public:
alpar@108:       typedef T ValueType;
alpar@164:       typedef Node KeyType;
alpar@105: 
alpar@185:       NodeMap(const SmartGraph &_G) :
alpar@164: 	DynMapBase<Node>(_G), container(_G.maxNodeId())
alpar@108:       {
alpar@108: 	G->dyn_node_maps.push_back(this);
alpar@108:       }
alpar@185:       NodeMap(const SmartGraph &_G,const T &t) :
alpar@185: 	DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
alpar@185:       {
alpar@185: 	G->dyn_node_maps.push_back(this);
alpar@185:       }
alpar@185:       
alpar@185:       NodeMap(const NodeMap<T> &m) :
alpar@185:  	DynMapBase<Node>(*m.G), container(m.container)
alpar@185:       {
alpar@185:  	G->dyn_node_maps.push_back(this);
alpar@185:       }
alpar@185: 
alpar@185:       template<typename TT> friend class NodeMap;
alpar@185:  
alpar@185:       ///\todo It can copy between different types.
alpar@185:       ///
alpar@185:       template<typename TT> NodeMap(const NodeMap<TT> &m) :
alpar@185: 	DynMapBase<Node>(*m.G)
alpar@185:       {
alpar@185: 	G->dyn_node_maps.push_back(this);
alpar@185: 	typename std::vector<TT>::const_iterator i;
alpar@185: 	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185: 	    i!=m.container.end();
alpar@185: 	    i++)
alpar@185: 	  container.push_back(*i);
alpar@185:       }
alpar@185:       ~NodeMap()
alpar@108:       {
alpar@108: 	if(G) {
alpar@164: 	  std::vector<DynMapBase<Node>* >::iterator i;
alpar@108: 	  for(i=G->dyn_node_maps.begin();
alpar@108: 	      i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
alpar@115: 	  //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
alpar@115: 	  //A better way to do that: (Is this really important?)
alpar@115: 	  if(*i==this) {
alpar@116: 	    *i=G->dyn_node_maps.back();
alpar@115: 	    G->dyn_node_maps.pop_back();
alpar@115: 	  }
alpar@108: 	}
alpar@108:       }
alpar@105: 
alpar@164:       void add(const Node k) 
alpar@108:       {
alpar@185: 	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@108:       }
alpar@177: 
alpar@215:       void erase(const Node) { }
alpar@108:       
alpar@164:       void set(Node n, T a) { container[n.n]=a; }
alpar@285:       //'T& operator[](Node n)' would be wrong here
alpar@215:       typename std::vector<T>::reference
alpar@215:       operator[](Node n) { return container[n.n]; }
alpar@285:       //'const T& operator[](Node n)' would be wrong here
alpar@215:       typename std::vector<T>::const_reference 
alpar@215:       operator[](Node n) const { return container[n.n]; }
alpar@108: 
alpar@185:       ///\warning There is no safety check at all!
alpar@185:       ///Using operator = between maps attached to different graph may
alpar@185:       ///cause serious problem.
alpar@185:       ///\todo Is this really so?
alpar@185:       ///\todo It can copy between different types.
alpar@185:       const NodeMap<T>& operator=(const NodeMap<T> &m)
alpar@185:       {
alpar@185: 	container = m.container;
alpar@185: 	return *this;
alpar@185:       }
alpar@185:       template<typename TT>
alpar@185:       const NodeMap<T>& operator=(const NodeMap<TT> &m)
alpar@185:       {
alpar@185: 	copy(m.container.begin(), m.container.end(), container.begin());
alpar@185: 	return *this;
alpar@185:       }
alpar@185:       
alpar@285:       void update() {}    //Useless for Dynamic Maps
alpar@285:       void update(T a) {}  //Useless for Dynamic Maps
alpar@108:     };
alpar@108:     
alpar@185:     template <typename T> class EdgeMap : public DynMapBase<Edge>
alpar@108:     {
alpar@108:       std::vector<T> container;
alpar@108: 
alpar@108:     public:
alpar@108:       typedef T ValueType;
alpar@164:       typedef Edge KeyType;
alpar@108: 
alpar@185:       EdgeMap(const SmartGraph &_G) :
alpar@164: 	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
alpar@108:       {
alpar@108: 	//FIXME: What if there are empty Id's?
alpar@115: 	//FIXME: Can I use 'this' in a constructor?
alpar@108: 	G->dyn_edge_maps.push_back(this);
alpar@108:       }
alpar@185:       EdgeMap(const SmartGraph &_G,const T &t) :
alpar@185: 	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
alpar@185:       {
alpar@185: 	G->dyn_edge_maps.push_back(this);
alpar@185:       } 
alpar@185:       EdgeMap(const EdgeMap<T> &m) :
alpar@185:  	DynMapBase<Edge>(*m.G), container(m.container)
alpar@185:       {
alpar@185:  	G->dyn_node_maps.push_back(this);
alpar@185:       }
alpar@185: 
alpar@185:       template<typename TT> friend class EdgeMap;
alpar@185: 
alpar@185:       ///\todo It can copy between different types.
alpar@185:       ///
alpar@185:       template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
alpar@185: 	DynMapBase<Edge>(*m.G)
alpar@185:       {
alpar@185: 	G->dyn_node_maps.push_back(this);
alpar@185: 	typename std::vector<TT>::const_iterator i;
alpar@185: 	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185: 	    i!=m.container.end();
alpar@185: 	    i++)
alpar@185: 	  container.push_back(*i);
alpar@185:       }
alpar@185:       ~EdgeMap()
alpar@108:       {
alpar@108: 	if(G) {
alpar@164: 	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@108: 	  for(i=G->dyn_edge_maps.begin();
alpar@108: 	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
alpar@115: 	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@115: 	  //A better way to do that: (Is this really important?)
alpar@115: 	  if(*i==this) {
alpar@116: 	    *i=G->dyn_edge_maps.back();
alpar@115: 	    G->dyn_edge_maps.pop_back();
alpar@115: 	  }
alpar@108: 	}
alpar@108:       }
alpar@115:       
alpar@164:       void add(const Edge k) 
alpar@108:       {
alpar@108: 	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@108:       }
alpar@215:       void erase(const Edge) { }
alpar@108:       
alpar@164:       void set(Edge n, T a) { container[n.n]=a; }
alpar@209:       //T get(Edge n) const { return container[n.n]; }
alpar@215:       typename std::vector<T>::reference
alpar@215:       operator[](Edge n) { return container[n.n]; }
alpar@215:       typename std::vector<T>::const_reference
alpar@215:       operator[](Edge n) const { return container[n.n]; }
alpar@108: 
alpar@185:       ///\warning There is no safety check at all!
alpar@185:       ///Using operator = between maps attached to different graph may
alpar@185:       ///cause serious problem.
alpar@185:       ///\todo Is this really so?
alpar@185:       ///\todo It can copy between different types.
alpar@185:       const EdgeMap<T>& operator=(const EdgeMap<T> &m)
alpar@185:       {
alpar@185: 	container = m.container;
alpar@185: 	return *this;
alpar@185:       }
alpar@185:       template<typename TT>
alpar@185:       const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
alpar@185:       {
alpar@185: 	copy(m.container.begin(), m.container.end(), container.begin());
alpar@185: 	return *this;
alpar@185:       }
alpar@185:       
alpar@108:       void update() {}    //Useless for DynMaps
alpar@108:       void update(T a) {}  //Useless for DynMaps
alpar@108:     };
alpar@185: 
alpar@104:   };
alpar@185: 
alpar@185:   ///Graph for bidirectional edges.
alpar@185: 
alpar@185:   ///The purpose of this graph structure is to handle graphs
alpar@185:   ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@186:   ///of oppositely directed edges.
alpar@186:   ///There is a new edge map type called
alpar@186:   ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
alpar@186:   ///that complements this
alpar@186:   ///feature by
alpar@186:   ///storing shared values for the edge pairs. The usual
alpar@186:   ///\ref GraphSkeleton::EdgeMap "EdgeMap"
alpar@186:   ///can be used
alpar@185:   ///as well.
alpar@185:   ///
alpar@186:   ///The oppositely directed edge can also be obtained easily
alpar@186:   ///using \ref opposite.
alpar@186:   ///\warning It shares the similarity with \ref SmartGraph that
alpar@186:   ///it is not possible to delete edges or nodes from the graph.
alpar@186:   //\sa \ref SmartGraph.
alpar@185: 
alpar@185:   class SymSmartGraph : public SmartGraph
alpar@185:   {
alpar@185:   public:
alpar@186:     template<typename T> class SymEdgeMap;
alpar@186:     template<typename T> friend class SymEdgeMap;
alpar@186: 
alpar@185:     SymSmartGraph() : SmartGraph() { }
alpar@185:     SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
alpar@398:     ///Adds a pair of oppositely directed edges to the graph.
alpar@185:     Edge addEdge(Node u, Node v)
alpar@185:     {
alpar@185:       Edge e = SmartGraph::addEdge(u,v);
alpar@185:       SmartGraph::addEdge(v,u);
alpar@185:       return e;
alpar@185:     }
alpar@185: 
alpar@186:     ///The oppositely directed edge.
alpar@186: 
alpar@186:     ///Returns the oppositely directed
alpar@186:     ///pair of the edge \c e.
alpar@185:     Edge opposite(Edge e) const
alpar@185:     {
alpar@185:       Edge f;
alpar@185:       f.idref() = e.idref() - 2*(e.idref()%2) + 1;
alpar@185:       return f;
alpar@185:     }
alpar@185:     
alpar@186:     ///Common data storage for the edge pairs.
alpar@186: 
alpar@186:     ///This map makes it possible to store data shared by the oppositely
alpar@186:     ///directed pairs of edges.
alpar@185:     template <typename T> class SymEdgeMap : public DynMapBase<Edge>
alpar@185:     {
alpar@185:       std::vector<T> container;
alpar@185:       
alpar@185:     public:
alpar@185:       typedef T ValueType;
alpar@185:       typedef Edge KeyType;
alpar@185: 
alpar@186:       SymEdgeMap(const SymSmartGraph &_G) :
alpar@185: 	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
alpar@185:       {
alpar@186: 	static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.push_back(this);
alpar@185:       }
alpar@186:       SymEdgeMap(const SymSmartGraph &_G,const T &t) :
alpar@185: 	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
alpar@185:       {
alpar@185: 	G->dyn_edge_maps.push_back(this);
alpar@185:       }
alpar@185: 
alpar@185:       SymEdgeMap(const SymEdgeMap<T> &m) :
alpar@185:  	DynMapBase<SymEdge>(*m.G), container(m.container)
alpar@185:       {
alpar@185:  	G->dyn_node_maps.push_back(this);
alpar@185:       }
alpar@185: 
alpar@185:       //      template<typename TT> friend class SymEdgeMap;
alpar@185: 
alpar@185:       ///\todo It can copy between different types.
alpar@185:       ///
alpar@185: 
alpar@185:       template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m) :
alpar@185: 	DynMapBase<SymEdge>(*m.G)
alpar@185:       {
alpar@185: 	G->dyn_node_maps.push_back(this);
alpar@185: 	typename std::vector<TT>::const_iterator i;
alpar@185: 	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185: 	    i!=m.container.end();
alpar@185: 	    i++)
alpar@185: 	  container.push_back(*i);
alpar@185:       }
alpar@185:  
alpar@185:       ~SymEdgeMap()
alpar@185:       {
alpar@185: 	if(G) {
alpar@185: 	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@186: 	  for(i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.begin();
alpar@186: 	      i!=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.end()
alpar@186: 		&& *i!=this; ++i) ;
alpar@185: 	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@185: 	  //A better way to do that: (Is this really important?)
alpar@185: 	  if(*i==this) {
alpar@186: 	    *i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.back();
alpar@186: 	    static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.pop_back();
alpar@185: 	  }
alpar@185: 	}
alpar@185:       }
alpar@185:       
alpar@185:       void add(const Edge k) 
alpar@185:       {
alpar@185: 	if(!k.idref()%2&&k.idref()/2>=int(container.size()))
alpar@185: 	  container.resize(k.idref()/2+1);
alpar@185:       }
alpar@185:       void erase(const Edge k) { }
alpar@185:       
alpar@185:       void set(Edge n, T a) { container[n.idref()/2]=a; }
alpar@209:       //T get(Edge n) const { return container[n.idref()/2]; }
alpar@215:       typename std::vector<T>::reference
alpar@215:       operator[](Edge n) { return container[n.idref()/2]; }
alpar@215:       typename std::vector<T>::const_reference
alpar@215:       operator[](Edge n) const { return container[n.idref()/2]; }
alpar@185: 
alpar@185:       ///\warning There is no safety check at all!
alpar@185:       ///Using operator = between maps attached to different graph may
alpar@185:       ///cause serious problem.
alpar@185:       ///\todo Is this really so?
alpar@185:       ///\todo It can copy between different types.
alpar@185:       const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
alpar@185:       {
alpar@185: 	container = m.container;
alpar@185: 	return *this;
alpar@185:       }
alpar@185:       template<typename TT>
alpar@185:       const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
alpar@185:       {
alpar@185: 	copy(m.container.begin(), m.container.end(), container.begin());
alpar@185: 	return *this;
alpar@185:       }
alpar@185:       
alpar@185:       void update() {}    //Useless for DynMaps
alpar@185:       void update(T a) {}  //Useless for DynMaps
alpar@185: 
alpar@185:     };
alpar@185: 
alpar@185:   };
alpar@185:   
alpar@407:   /// @}  
alpar@407: 
alpar@105: } //namespace hugo
alpar@104: 
alpar@157: 
alpar@157: 
alpar@157: 
alpar@104: #endif //SMART_GRAPH_H