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