alpar@105: // -*- mode:C++ -*- alpar@105: alpar@185: #ifndef HUGO_SMART_GRAPH_H alpar@185: #define HUGO_SMART_GRAPH_H alpar@104: klao@491: ///\ingroup graphs alpar@242: ///\file alpar@242: ///\brief SmartGraph and SymSmartGraph classes. alpar@242: alpar@104: #include alpar@129: #include alpar@104: ladanyi@542: #include 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 it does not support node and edge deletion. 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@456: ///\author Alpar Juttner 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 nodes; alpar@129: alpar@104: std::vector edges; alpar@104: alpar@185: protected: alpar@185: alpar@108: template 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 class EdgeMap; alpar@590: template class NodeMap; 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 * > dyn_node_maps; alpar@185: ///\bug It must be public because of SymEdgeMap. alpar@185: /// alpar@164: mutable std::vector * > dyn_edge_maps; alpar@108: alpar@108: public: alpar@108: alpar@503: alpar@164: class NodeIt; alpar@164: class EdgeIt; alpar@104: class OutEdgeIt; alpar@104: class InEdgeIt; alpar@104: alpar@105: template class NodeMap; alpar@104: template 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 * >::iterator i=dyn_node_maps.begin(); alpar@108: i!=dyn_node_maps.end(); ++i) (**i).G=NULL; alpar@164: for(std::vector * >::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@503: ///\deprecated Use alpar@503: ///\code alpar@503: /// e=INVALID; alpar@503: ///\endcode alpar@503: ///instead. alpar@164: void setInvalid(Edge &e) { e.n=-1; } alpar@503: ///\deprecated Use alpar@503: ///\code alpar@503: /// e=INVALID; alpar@503: ///\endcode alpar@503: ///instead. alpar@164: void setInvalid(Node &n) { n.n=-1; } alpar@129: alpar@157: template 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 * >::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 * >::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 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@503: Node (Invalid) { 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 NodeIt. alpar@579: NodeIt(const SmartGraph& G, const Node &n) : Node(n) { } alpar@104: }; alpar@104: alpar@164: class Edge { alpar@104: friend class SmartGraph; alpar@104: template friend class EdgeMap; alpar@185: alpar@185: //template 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@706: public: alpar@706: /// An Edge with id \c n. alpar@706: alpar@706: /// \bug It should be alpar@706: /// obtained by a member function of the Graph. alpar@164: Edge(int nn) {n=nn;} 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 class NodeMap : public DynMapBase alpar@108: { alpar@108: std::vector 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(_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(_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 &m) : alpar@185: DynMapBase(*m.G), container(m.container) alpar@185: { alpar@185: G->dyn_node_maps.push_back(this); alpar@185: } alpar@185: alpar@185: template friend class NodeMap; alpar@185: alpar@185: ///\todo It can copy between different types. alpar@590: ///\todo We could use 'copy' alpar@185: template NodeMap(const NodeMap &m) : alpar@590: DynMapBase(*m.G), container(m.container.size()) alpar@185: { alpar@185: G->dyn_node_maps.push_back(this); alpar@185: typename std::vector::const_iterator i; alpar@185: for(typename std::vector::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* >::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::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::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& operator=(const NodeMap &m) alpar@185: { alpar@185: container = m.container; alpar@185: return *this; alpar@185: } alpar@185: template alpar@185: const NodeMap& operator=(const NodeMap &m) alpar@185: { alpar@531: std::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 class EdgeMap : public DynMapBase alpar@108: { alpar@108: std::vector 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(_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(_G), container(_G.maxEdgeId(),t) alpar@185: { alpar@185: G->dyn_edge_maps.push_back(this); alpar@185: } alpar@185: EdgeMap(const EdgeMap &m) : alpar@185: DynMapBase(*m.G), container(m.container) alpar@185: { alpar@503: G->dyn_edge_maps.push_back(this); alpar@185: } alpar@185: alpar@185: template friend class EdgeMap; alpar@185: alpar@185: ///\todo It can copy between different types. alpar@590: template EdgeMap(const EdgeMap &m) alpar@590: : DynMapBase(*m.G), container(m.container.size()) alpar@185: { alpar@503: G->dyn_edge_maps.push_back(this); alpar@185: typename std::vector::const_iterator i; alpar@185: for(typename std::vector::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* >::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::reference alpar@215: operator[](Edge n) { return container[n.n]; } alpar@215: typename std::vector::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& operator=(const EdgeMap &m) alpar@185: { alpar@185: container = m.container; alpar@185: return *this; alpar@185: } alpar@185: template alpar@185: const EdgeMap& operator=(const EdgeMap &m) alpar@185: { alpar@531: std::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 class SymEdgeMap; alpar@186: template 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 class SymEdgeMap : public DynMapBase alpar@185: { alpar@185: std::vector 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(_G), container(_G.maxEdgeId()/2) alpar@185: { alpar@186: static_cast(G)->dyn_edge_maps.push_back(this); alpar@185: } alpar@186: SymEdgeMap(const SymSmartGraph &_G,const T &t) : alpar@185: DynMapBase(_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 &m) : alpar@185: DynMapBase(*m.G), container(m.container) alpar@185: { alpar@185: G->dyn_node_maps.push_back(this); alpar@185: } alpar@185: alpar@185: // template friend class SymEdgeMap; alpar@185: alpar@185: ///\todo It can copy between different types. alpar@185: /// alpar@185: alpar@590: template SymEdgeMap(const SymEdgeMap &m) alpar@590: : DynMapBase(*m.G), container(m.container.size()) alpar@185: { alpar@185: G->dyn_node_maps.push_back(this); alpar@185: typename std::vector::const_iterator i; alpar@185: for(typename std::vector::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* >::iterator i; alpar@186: for(i=static_cast(G)->dyn_edge_maps.begin(); alpar@186: i!=static_cast(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(G)->dyn_edge_maps.back(); alpar@186: static_cast(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::reference alpar@215: operator[](Edge n) { return container[n.idref()/2]; } alpar@215: typename std::vector::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& operator=(const SymEdgeMap &m) alpar@185: { alpar@185: container = m.container; alpar@185: return *this; alpar@185: } alpar@185: template alpar@185: const SymEdgeMap& operator=(const SymEdgeMap &m) alpar@185: { alpar@531: std::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@590: #endif //HUGO_SMART_GRAPH_H