alpar@395: // -*- mode:C++ -*- alpar@395: alpar@405: #ifndef HUGO_LIST_GRAPH_H alpar@405: #define HUGO_LIST_GRAPH_H alpar@395: klao@491: ///\ingroup graphs alpar@395: ///\file alpar@405: ///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes. alpar@395: alpar@395: #include alpar@395: #include alpar@395: ladanyi@542: #include alpar@395: alpar@395: namespace hugo { alpar@395: alpar@406: /// \addtogroup graphs alpar@406: /// @{ alpar@406: alpar@397: class SymListGraph; alpar@395: alpar@401: ///A list graph class. alpar@395: alpar@397: ///This is a simple and fast erasable graph implementation. alpar@397: /// alpar@395: ///It conforms to the graph interface documented under alpar@395: ///the description of \ref GraphSkeleton. alpar@395: ///\sa \ref GraphSkeleton. alpar@397: class ListGraph { alpar@395: alpar@397: //Nodes are double linked. alpar@397: //The free nodes are only single linked using the "next" field. alpar@395: struct NodeT alpar@395: { alpar@397: int first_in,first_out; alpar@397: int prev, next; alpar@397: // NodeT() {} alpar@395: }; alpar@397: //Edges are double linked. alpar@397: //The free edges are only single linked using the "next_in" field. alpar@395: struct EdgeT alpar@395: { alpar@397: int head, tail; alpar@397: int prev_in, prev_out; alpar@397: int next_in, next_out; alpar@395: //FIXME: is this necessary? alpar@397: // EdgeT() : next_in(-1), next_out(-1) prev_in(-1), prev_out(-1) {} alpar@395: }; alpar@395: alpar@395: std::vector nodes; alpar@397: //The first node alpar@397: int first_node; alpar@397: //The first free node alpar@397: int first_free_node; alpar@395: std::vector edges; alpar@397: //The first free edge alpar@397: int first_free_edge; alpar@395: alpar@397: protected: alpar@395: alpar@395: template class DynMapBase alpar@395: { alpar@395: protected: alpar@397: const ListGraph* G; alpar@395: public: alpar@515: virtual void add(const Key k) = 0; alpar@515: virtual void erase(const Key k) = 0; alpar@397: DynMapBase(const ListGraph &_G) : G(&_G) {} alpar@395: virtual ~DynMapBase() {} alpar@397: friend class ListGraph; alpar@395: }; alpar@395: alpar@395: public: alpar@395: template class EdgeMap; alpar@400: template class NodeMap; alpar@397: 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: public: alpar@395: alpar@397: ListGraph() : nodes(), first_node(-1), alpar@397: first_free_node(-1), edges(), first_free_edge(-1) {} alpar@397: ListGraph(const ListGraph &_g) : nodes(_g.nodes), first_node(_g.first_node), alpar@397: first_free_node(_g.first_free_node), alpar@397: edges(_g.edges), alpar@397: first_free_edge(_g.first_free_edge) {} alpar@395: alpar@397: ~ListGraph() 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@397: it.n=nodes[it.n].next; 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@397: EdgeIt& next(EdgeIt& it) const { alpar@397: if(edges[it.n].next_in!=-1) { alpar@397: it.n=edges[it.n].next_in; alpar@397: } alpar@397: else { alpar@397: int n; alpar@397: for(n=nodes[edges[it.n].head].next; alpar@397: n!=-1 && nodes[n].first_in == -1; alpar@397: n = nodes[n].next) ; alpar@397: it.n = (n==-1)?-1:nodes[n].first_in; alpar@397: } alpar@397: return it; alpar@397: } 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@397: /// Adds a new node to the graph. alpar@397: alpar@397: /// \todo It adds the nodes in a reversed order. alpar@397: /// (i.e. the lastly added node becomes the first.) alpar@395: Node addNode() { alpar@397: int n; alpar@397: alpar@397: if(first_free_node==-1) alpar@397: { alpar@397: n = nodes.size(); alpar@397: nodes.push_back(NodeT()); alpar@397: } alpar@397: else { alpar@397: n = first_free_node; alpar@397: first_free_node = nodes[n].next; alpar@397: } alpar@397: alpar@397: nodes[n].next = first_node; alpar@397: if(first_node != -1) nodes[first_node].prev = n; alpar@397: first_node = n; alpar@397: nodes[n].prev = -1; alpar@397: alpar@397: nodes[n].first_in = nodes[n].first_out = -1; alpar@397: alpar@397: Node nn; nn.n=n; alpar@395: alpar@397: //Update dynamic maps alpar@395: for(std::vector * >::iterator i=dyn_node_maps.begin(); alpar@397: i!=dyn_node_maps.end(); ++i) (**i).add(nn); alpar@395: alpar@397: return nn; alpar@395: } alpar@395: alpar@395: Edge addEdge(Node u, Node v) { alpar@397: int n; alpar@397: alpar@397: if(first_free_edge==-1) alpar@397: { alpar@397: n = edges.size(); alpar@397: edges.push_back(EdgeT()); alpar@397: } alpar@397: else { alpar@397: n = first_free_edge; alpar@397: first_free_edge = edges[n].next_in; alpar@397: } alpar@397: alpar@397: edges[n].tail = u.n; edges[n].head = v.n; alpar@395: alpar@397: edges[n].next_out = nodes[u.n].first_out; alpar@397: if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n; alpar@397: edges[n].next_in = nodes[v.n].first_in; alpar@397: if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n; alpar@397: edges[n].prev_in = edges[n].prev_out = -1; alpar@397: alpar@397: nodes[u.n].first_out = nodes[v.n].first_in = n; alpar@397: alpar@397: Edge e; e.n=n; alpar@397: alpar@397: //Update dynamic maps 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@397: private: alpar@397: void eraseEdge(int n) { alpar@397: alpar@397: if(edges[n].next_in!=-1) alpar@397: edges[edges[n].next_in].prev_in = edges[n].prev_in; alpar@397: if(edges[n].prev_in!=-1) alpar@397: edges[edges[n].prev_in].next_in = edges[n].next_in; alpar@397: else nodes[edges[n].head].first_in = edges[n].next_in; alpar@397: alpar@397: if(edges[n].next_out!=-1) alpar@397: edges[edges[n].next_out].prev_out = edges[n].prev_out; alpar@397: if(edges[n].prev_out!=-1) alpar@397: edges[edges[n].prev_out].next_out = edges[n].next_out; alpar@397: else nodes[edges[n].tail].first_out = edges[n].next_out; alpar@397: alpar@397: edges[n].next_in = first_free_edge; alpar@397: first_free_edge = -1; alpar@397: alpar@397: //Update dynamic maps alpar@397: Edge e; e.n=n; alpar@397: for(std::vector * >::iterator i=dyn_edge_maps.begin(); alpar@397: i!=dyn_edge_maps.end(); ++i) (**i).erase(e); alpar@397: } alpar@397: alpar@397: public: alpar@397: alpar@397: void erase(Node nn) { alpar@397: int n=nn.n; alpar@397: alpar@397: int m; alpar@397: while((m=nodes[n].first_in)!=-1) eraseEdge(m); alpar@397: while((m=nodes[n].first_out)!=-1) eraseEdge(m); alpar@397: alpar@397: if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; alpar@397: if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; alpar@397: else first_node = nodes[n].next; alpar@397: alpar@397: nodes[n].next = first_free_node; alpar@397: first_free_node = n; alpar@397: alpar@397: //Update dynamic maps alpar@397: for(std::vector * >::iterator i=dyn_node_maps.begin(); alpar@397: i!=dyn_node_maps.end(); ++i) (**i).erase(nn); alpar@397: } alpar@397: alpar@397: void erase(Edge e) { eraseEdge(e.n); } alpar@397: alpar@397: ///\bug Dynamic maps must be updated! alpar@397: /// alpar@397: void clear() { alpar@397: nodes.clear();edges.clear(); alpar@397: first_node=first_free_node=first_free_edge=-1; alpar@397: } alpar@395: alpar@395: class Node { alpar@397: friend class ListGraph; alpar@395: template friend class NodeMap; alpar@400: 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@397: friend int ListGraph::id(Node v) const; alpar@395: Node(int nn) {n=nn;} alpar@395: public: alpar@395: Node() {} alpar@503: Node (Invalid) { 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 NodeIt. alpar@579: NodeIt(const ListGraph& G, const Node &n) : Node(n) { } alpar@395: }; alpar@395: alpar@395: class Edge { alpar@397: friend class ListGraph; alpar@395: template friend class EdgeMap; alpar@395: alpar@397: //template friend class SymListGraph::SymEdgeMap; alpar@397: //friend Edge SymListGraph::opposite(Edge) const; alpar@395: alpar@395: friend class Node; alpar@395: friend class NodeIt; alpar@395: protected: alpar@395: int n; alpar@397: friend int ListGraph::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@397: NodeMap(const ListGraph &_G) : alpar@395: DynMapBase(_G), container(_G.maxNodeId()) alpar@395: { alpar@395: G->dyn_node_maps.push_back(this); alpar@395: } alpar@397: NodeMap(const ListGraph &_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@590: DynMapBase(*m.G), container(m.container.size()) alpar@590: 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@531: std::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@579: protected: alpar@395: std::vector container; alpar@395: alpar@395: public: alpar@395: typedef T ValueType; alpar@395: typedef Edge KeyType; alpar@395: alpar@397: EdgeMap(const ListGraph &_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@397: EdgeMap(const ListGraph &_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@503: G->dyn_edge_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@590: DynMapBase(*m.G), container(m.container.size()) alpar@395: { alpar@503: G->dyn_edge_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@531: std::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@397: ///\ref SymListGraph::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@397: /// alpar@397: ///Here erase(Edge) deletes a pair of edges. alpar@397: /// alpar@397: ///\todo this date structure need some reconsiderations. Maybe it alpar@397: ///should be implemented independently from ListGraph. alpar@395: alpar@397: class SymListGraph : public ListGraph alpar@395: { alpar@395: public: alpar@395: template class SymEdgeMap; alpar@395: template friend class SymEdgeMap; alpar@395: alpar@397: SymListGraph() : ListGraph() { } alpar@397: SymListGraph(const ListGraph &_g) : ListGraph(_g) { } alpar@397: ///Adds a pair of oppositely directed edges to the graph. alpar@395: Edge addEdge(Node u, Node v) alpar@395: { alpar@397: Edge e = ListGraph::addEdge(u,v); alpar@397: ListGraph::addEdge(v,u); alpar@395: return e; alpar@395: } alpar@395: alpar@397: void erase(Node n) { ListGraph::erase(n); } 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@397: ///Removes a pair of oppositely directed edges to the graph. alpar@397: void erase(Edge e) { alpar@397: ListGraph::erase(opposite(e)); alpar@397: ListGraph::erase(e); alpar@397: } alpar@397: 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@397: SymEdgeMap(const SymListGraph &_G) : alpar@395: DynMapBase(_G), container(_G.maxEdgeId()/2) alpar@395: { alpar@397: static_cast(G)->dyn_edge_maps.push_back(this); alpar@395: } alpar@397: SymEdgeMap(const SymListGraph &_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@590: DynMapBase(*m.G), container(m.container.size()) 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@397: for(i=static_cast(G)->dyn_edge_maps.begin(); alpar@397: 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@397: *i=static_cast(G)->dyn_edge_maps.back(); alpar@397: 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@531: std::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@400: alpar@401: ///A graph class containing only nodes. alpar@400: alpar@401: ///This class implements a graph structure without edges. alpar@401: ///The most useful application of this class is to be the node set of an alpar@401: ///\ref EdgeSet class. alpar@400: /// alpar@400: ///It conforms to the graph interface documented under alpar@401: ///the description of \ref GraphSkeleton with the exception that you cannot alpar@401: ///add (or delete) edges. The usual edge iterators are exists, but they are alpar@401: ///always \ref INVALID. alpar@401: ///\sa \ref GraphSkeleton alpar@508: ///\sa \ref EdgeSet alpar@400: class NodeSet { alpar@400: alpar@400: //Nodes are double linked. alpar@400: //The free nodes are only single linked using the "next" field. alpar@400: struct NodeT alpar@400: { alpar@400: int first_in,first_out; alpar@400: int prev, next; alpar@400: // NodeT() {} alpar@400: }; alpar@400: alpar@400: std::vector nodes; alpar@400: //The first node alpar@400: int first_node; alpar@400: //The first free node alpar@400: int first_free_node; alpar@400: alpar@400: protected: alpar@400: alpar@400: template class DynMapBase alpar@400: { alpar@400: protected: alpar@400: const NodeSet* G; alpar@400: public: alpar@515: virtual void add(const Key k) = 0; alpar@515: virtual void erase(const Key k) = 0; alpar@400: DynMapBase(const NodeSet &_G) : G(&_G) {} alpar@400: virtual ~DynMapBase() {} alpar@400: friend class NodeSet; alpar@400: }; alpar@400: alpar@400: public: alpar@400: template class EdgeMap; alpar@400: template class NodeMap; alpar@400: alpar@400: class Node; alpar@400: class Edge; alpar@400: alpar@400: // protected: alpar@400: // HELPME: alpar@400: protected: alpar@400: ///\bug It must be public because of SymEdgeMap. alpar@400: /// alpar@400: mutable std::vector * > dyn_node_maps; alpar@400: //mutable std::vector * > dyn_edge_maps; alpar@400: alpar@400: public: alpar@400: alpar@400: class NodeIt; alpar@400: class EdgeIt; alpar@400: class OutEdgeIt; alpar@400: class InEdgeIt; alpar@400: alpar@400: template class NodeMap; alpar@400: template class EdgeMap; alpar@400: alpar@400: public: alpar@400: alpar@408: ///Default constructor alpar@400: NodeSet() : nodes(), first_node(-1), alpar@400: first_free_node(-1) {} alpar@408: ///Copy constructor alpar@400: NodeSet(const NodeSet &_g) : nodes(_g.nodes), first_node(_g.first_node), alpar@400: first_free_node(_g.first_free_node) {} alpar@400: alpar@400: ~NodeSet() alpar@400: { alpar@400: for(std::vector * >::iterator i=dyn_node_maps.begin(); alpar@400: i!=dyn_node_maps.end(); ++i) (**i).G=NULL; alpar@400: //for(std::vector * >::iterator i=dyn_edge_maps.begin(); alpar@400: // i!=dyn_edge_maps.end(); ++i) (**i).G=NULL; alpar@400: } alpar@400: alpar@400: int nodeNum() const { return nodes.size(); } //FIXME: What is this? alpar@400: int edgeNum() const { return 0; } //FIXME: What is this? alpar@400: alpar@400: ///\bug This function does something different than alpar@400: ///its name would suggests... alpar@400: int maxNodeId() const { return nodes.size(); } //FIXME: What is this? alpar@400: ///\bug This function does something different than alpar@400: ///its name would suggests... alpar@400: int maxEdgeId() const { return 0; } //FIXME: What is this? alpar@400: alpar@400: Node tail(Edge e) const { return INVALID; } alpar@400: Node head(Edge e) const { return INVALID; } alpar@400: alpar@400: Node aNode(OutEdgeIt e) const { return INVALID; } alpar@400: Node aNode(InEdgeIt e) const { return INVALID; } alpar@400: alpar@400: Node bNode(OutEdgeIt e) const { return INVALID; } alpar@400: Node bNode(InEdgeIt e) const { return INVALID; } alpar@400: alpar@400: NodeIt& first(NodeIt& v) const { alpar@400: v=NodeIt(*this); return v; } alpar@400: EdgeIt& first(EdgeIt& e) const { alpar@400: e=EdgeIt(*this); return e; } alpar@400: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { alpar@400: e=OutEdgeIt(*this,v); return e; } alpar@400: InEdgeIt& first(InEdgeIt& e, const Node v) const { alpar@400: e=InEdgeIt(*this,v); return e; } alpar@400: alpar@400: // template< typename It > alpar@400: // It first() const { It e; first(e); return e; } alpar@400: alpar@400: // template< typename It > alpar@400: // It first(Node v) const { It e; first(e,v); return e; } alpar@400: alpar@400: bool valid(Edge e) const { return false; } alpar@400: bool valid(Node n) const { return n.n!=-1; } alpar@400: alpar@400: void setInvalid(Edge &e) { } alpar@400: void setInvalid(Node &n) { n.n=-1; } alpar@400: alpar@400: template It getNext(It it) const alpar@400: { It tmp(it); return next(tmp); } alpar@400: alpar@400: NodeIt& next(NodeIt& it) const { alpar@400: it.n=nodes[it.n].next; alpar@400: return it; alpar@400: } alpar@400: OutEdgeIt& next(OutEdgeIt& it) const { return it; } alpar@400: InEdgeIt& next(InEdgeIt& it) const { return it; } alpar@400: EdgeIt& next(EdgeIt& it) const { return it; } alpar@400: alpar@400: int id(Node v) const { return v.n; } alpar@400: int id(Edge e) const { return -1; } alpar@400: alpar@400: /// Adds a new node to the graph. alpar@400: alpar@400: /// \todo It adds the nodes in a reversed order. alpar@400: /// (i.e. the lastly added node becomes the first.) alpar@400: Node addNode() { alpar@400: int n; alpar@400: alpar@400: if(first_free_node==-1) alpar@400: { alpar@400: n = nodes.size(); alpar@400: nodes.push_back(NodeT()); alpar@400: } alpar@400: else { alpar@400: n = first_free_node; alpar@400: first_free_node = nodes[n].next; alpar@400: } alpar@400: alpar@400: nodes[n].next = first_node; alpar@400: if(first_node != -1) nodes[first_node].prev = n; alpar@400: first_node = n; alpar@400: nodes[n].prev = -1; alpar@400: alpar@400: nodes[n].first_in = nodes[n].first_out = -1; alpar@400: alpar@400: Node nn; nn.n=n; alpar@400: alpar@400: //Update dynamic maps alpar@400: for(std::vector * >::iterator i=dyn_node_maps.begin(); alpar@400: i!=dyn_node_maps.end(); ++i) (**i).add(nn); alpar@400: alpar@400: return nn; alpar@400: } alpar@400: alpar@400: void erase(Node nn) { alpar@400: int n=nn.n; alpar@400: alpar@400: if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev; alpar@400: if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next; alpar@400: else first_node = nodes[n].next; alpar@400: alpar@400: nodes[n].next = first_free_node; alpar@400: first_free_node = n; alpar@400: alpar@400: //Update dynamic maps alpar@400: for(std::vector * >::iterator i=dyn_node_maps.begin(); alpar@400: i!=dyn_node_maps.end(); ++i) (**i).erase(nn); alpar@400: } alpar@400: alpar@400: ///\bug Dynamic maps must be updated! alpar@400: /// alpar@400: void clear() { alpar@400: nodes.clear(); alpar@400: first_node = first_free_node = -1; alpar@400: } alpar@400: alpar@400: class Node { alpar@400: friend class NodeSet; alpar@400: template friend class NodeMap; alpar@400: alpar@400: friend class Edge; alpar@400: friend class OutEdgeIt; alpar@400: friend class InEdgeIt; alpar@400: alpar@400: protected: alpar@400: int n; alpar@400: friend int NodeSet::id(Node v) const; alpar@400: Node(int nn) {n=nn;} alpar@400: public: alpar@400: Node() {} alpar@400: Node (Invalid i) { n=-1; } alpar@400: bool operator==(const Node i) const {return n==i.n;} alpar@400: bool operator!=(const Node i) const {return n!=i.n;} alpar@400: bool operator<(const Node i) const {return n NodeIt. alpar@579: NodeIt(const NodeSet& G, const Node &n) : Node(n) { } alpar@579: alpar@400: }; alpar@400: alpar@400: class Edge { alpar@400: //friend class NodeSet; alpar@400: //template friend class EdgeMap; alpar@400: alpar@400: //template friend class SymNodeSet::SymEdgeMap; alpar@400: //friend Edge SymNodeSet::opposite(Edge) const; alpar@400: alpar@400: // friend class Node; alpar@400: // friend class NodeIt; alpar@400: protected: alpar@400: //friend int NodeSet::id(Edge e) const; alpar@400: // Edge(int nn) {} alpar@400: public: alpar@400: Edge() { } alpar@400: Edge (Invalid) { } alpar@400: bool operator==(const Edge i) const {return true;} alpar@400: bool operator!=(const Edge i) const {return false;} alpar@400: bool operator<(const Edge i) const {return false;} alpar@400: ///\bug This is a workaround until somebody tells me how to alpar@400: ///make class \c SymNodeSet::SymEdgeMap friend of Edge alpar@400: // int idref() {return -1;} alpar@400: // int idref() const {return -1;} alpar@400: }; alpar@400: alpar@400: class EdgeIt : public Edge { alpar@400: //friend class NodeSet; alpar@400: public: alpar@400: EdgeIt(const NodeSet& G) : Edge() { } alpar@400: EdgeIt (Invalid i) : Edge(i) { } alpar@400: EdgeIt() : Edge() { } alpar@400: ///\bug This is a workaround until somebody tells me how to alpar@400: ///make class \c SymNodeSet::SymEdgeMap friend of Edge alpar@400: // int idref() {return -1;} alpar@400: }; alpar@400: alpar@400: class OutEdgeIt : public Edge { alpar@400: friend class NodeSet; alpar@400: public: alpar@400: OutEdgeIt() : Edge() { } alpar@400: OutEdgeIt (Invalid i) : Edge(i) { } alpar@400: OutEdgeIt(const NodeSet& G,const Node v) : Edge() {} alpar@400: }; alpar@400: alpar@400: class InEdgeIt : public Edge { alpar@400: friend class NodeSet; alpar@400: public: alpar@400: InEdgeIt() : Edge() { } alpar@400: InEdgeIt (Invalid i) : Edge(i) { } alpar@400: InEdgeIt(const NodeSet& G,Node v) :Edge() {} alpar@400: }; alpar@400: alpar@400: template class NodeMap : public DynMapBase alpar@400: { alpar@400: std::vector container; alpar@400: alpar@400: public: alpar@400: typedef T ValueType; alpar@400: typedef Node KeyType; alpar@400: alpar@400: NodeMap(const NodeSet &_G) : alpar@400: DynMapBase(_G), container(_G.maxNodeId()) alpar@400: { alpar@400: G->dyn_node_maps.push_back(this); alpar@400: } alpar@400: NodeMap(const NodeSet &_G,const T &t) : alpar@400: DynMapBase(_G), container(_G.maxNodeId(),t) alpar@400: { alpar@400: G->dyn_node_maps.push_back(this); alpar@400: } alpar@400: alpar@400: NodeMap(const NodeMap &m) : alpar@400: DynMapBase(*m.G), container(m.container) alpar@400: { alpar@400: G->dyn_node_maps.push_back(this); alpar@400: } alpar@400: alpar@400: template friend class NodeMap; alpar@400: alpar@400: ///\todo It can copy between different types. alpar@400: /// alpar@400: template NodeMap(const NodeMap &m) : alpar@590: DynMapBase(*m.G), container(m.container.size()) alpar@400: { alpar@400: G->dyn_node_maps.push_back(this); alpar@400: typename std::vector::const_iterator i; alpar@400: for(typename std::vector::const_iterator i=m.container.begin(); alpar@400: i!=m.container.end(); alpar@400: i++) alpar@400: container.push_back(*i); alpar@400: } alpar@400: ~NodeMap() alpar@400: { alpar@400: if(G) { alpar@400: std::vector* >::iterator i; alpar@400: for(i=G->dyn_node_maps.begin(); alpar@400: i!=G->dyn_node_maps.end() && *i!=this; ++i) ; alpar@400: //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow... alpar@400: //A better way to do that: (Is this really important?) alpar@400: if(*i==this) { alpar@400: *i=G->dyn_node_maps.back(); alpar@400: G->dyn_node_maps.pop_back(); alpar@400: } alpar@400: } alpar@400: } alpar@400: alpar@400: void add(const Node k) alpar@400: { alpar@400: if(k.n>=int(container.size())) container.resize(k.n+1); alpar@400: } alpar@400: alpar@400: void erase(const Node) { } alpar@400: alpar@400: void set(Node n, T a) { container[n.n]=a; } alpar@400: //'T& operator[](Node n)' would be wrong here alpar@400: typename std::vector::reference alpar@400: operator[](Node n) { return container[n.n]; } alpar@400: //'const T& operator[](Node n)' would be wrong here alpar@400: typename std::vector::const_reference alpar@400: operator[](Node n) const { return container[n.n]; } alpar@400: alpar@400: ///\warning There is no safety check at all! alpar@400: ///Using operator = between maps attached to different graph may alpar@400: ///cause serious problem. alpar@400: ///\todo Is this really so? alpar@400: ///\todo It can copy between different types. alpar@400: const NodeMap& operator=(const NodeMap &m) alpar@400: { alpar@400: container = m.container; alpar@400: return *this; alpar@400: } alpar@400: template alpar@400: const NodeMap& operator=(const NodeMap &m) alpar@400: { alpar@531: std::copy(m.container.begin(), m.container.end(), container.begin()); alpar@400: return *this; alpar@400: } alpar@400: alpar@400: void update() {} //Useless for Dynamic Maps alpar@400: void update(T a) {} //Useless for Dynamic Maps alpar@400: }; alpar@400: alpar@400: template class EdgeMap alpar@400: { alpar@400: public: alpar@400: typedef T ValueType; alpar@400: typedef Edge KeyType; alpar@400: alpar@400: EdgeMap(const NodeSet &) { } alpar@400: EdgeMap(const NodeSet &,const T &) { } alpar@400: EdgeMap(const EdgeMap &) { } alpar@400: // template friend class EdgeMap; alpar@400: alpar@400: ///\todo It can copy between different types. alpar@400: /// alpar@400: template EdgeMap(const EdgeMap &) { } alpar@400: ~EdgeMap() { } alpar@400: alpar@400: void add(const Edge ) { } alpar@400: void erase(const Edge) { } alpar@400: alpar@400: void set(Edge, T) { } alpar@400: //T get(Edge n) const { return container[n.n]; } alpar@400: ValueType &operator[](Edge) { return *((T*)(NULL)); } alpar@400: const ValueType &operator[](Edge) const { return *((T*)(NULL)); } alpar@400: alpar@400: const EdgeMap& operator=(const EdgeMap &) { return *this; } alpar@400: alpar@400: template alpar@400: const EdgeMap& operator=(const EdgeMap &m) { return *this; } alpar@400: alpar@400: void update() {} alpar@400: void update(T a) {} alpar@400: }; alpar@400: }; alpar@400: alpar@400: alpar@400: alpar@401: ///Graph structure using a node set of another graph. alpar@401: alpar@401: ///This structure can be used to establish another graph over a node set alpar@401: /// of an existing one. The node iterator will go through the nodes of the alpar@401: /// original graph, and the NodeMap's of both graphs will convert to alpar@401: /// each other. alpar@401: /// alpar@404: ///\warning Adding or deleting nodes from the graph is not safe if an alpar@404: ///\ref EdgeSet is currently attached to it! alpar@404: /// alpar@404: ///\todo Make it possible to add/delete edges from the base graph alpar@404: ///(and from \ref EdgeSet, as well) alpar@404: /// alpar@401: ///\param GG The type of the graph which shares its node set with this class. alpar@401: ///Its interface must conform with \ref GraphSkeleton. alpar@400: /// alpar@400: ///It conforms to the graph interface documented under alpar@400: ///the description of \ref GraphSkeleton. alpar@400: ///\sa \ref GraphSkeleton. alpar@401: ///\sa \ref NodeSet. alpar@400: template alpar@400: class EdgeSet { alpar@400: alpar@400: typedef GG NodeGraphType; alpar@400: alpar@400: NodeGraphType &G; alpar@400: alpar@515: public: alpar@400: class Node; alpar@531: int id(Node v) const; alpar@531: alpar@531: class Node : public NodeGraphType::Node { alpar@531: friend class EdgeSet; alpar@531: // template friend class NodeMap; alpar@531: alpar@531: friend class Edge; alpar@531: friend class OutEdgeIt; alpar@531: friend class InEdgeIt; alpar@531: friend class SymEdge; alpar@531: alpar@531: public: alpar@531: friend int EdgeSet::id(Node v) const; alpar@531: // Node(int nn) {n=nn;} alpar@531: public: alpar@531: Node() : NodeGraphType::Node() {} alpar@531: Node (Invalid i) : NodeGraphType::Node(i) {} alpar@531: Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {} alpar@531: }; alpar@531: alpar@531: class NodeIt : public NodeGraphType::NodeIt { alpar@531: friend class EdgeSet; alpar@531: public: alpar@531: NodeIt() : NodeGraphType::NodeIt() { } alpar@531: NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {} alpar@531: NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { } alpar@531: NodeIt(const typename NodeGraphType::NodeIt &n) alpar@531: : NodeGraphType::NodeIt(n) {} alpar@579: ///\todo Undocumented conversion Node -\> NodeIt. alpar@579: NodeIt(const EdgeSet& _G, const Node &n) alpar@579: : NodeGraphType::NodeIt(_G.G,n) { } alpar@579: alpar@531: operator Node() { return Node(*this);} alpar@531: }; alpar@515: alpar@515: private: alpar@400: //Edges are double linked. alpar@400: //The free edges are only single linked using the "next_in" field. alpar@400: struct NodeT alpar@400: { alpar@400: int first_in,first_out; alpar@400: NodeT() : first_in(-1), first_out(-1) { } alpar@400: }; alpar@400: alpar@400: struct EdgeT alpar@400: { alpar@400: Node head, tail; alpar@400: int prev_in, prev_out; alpar@400: int next_in, next_out; alpar@400: }; alpar@400: alpar@400: alpar@515: typename NodeGraphType::template NodeMap nodes; alpar@400: alpar@400: std::vector edges; alpar@400: //The first free edge alpar@400: int first_free_edge; alpar@400: alpar@400: protected: alpar@400: alpar@400: template class DynMapBase alpar@400: { alpar@400: protected: alpar@400: const EdgeSet* G; alpar@400: public: alpar@515: virtual void add(const Key k) = 0; alpar@515: virtual void erase(const Key k) = 0; alpar@400: DynMapBase(const EdgeSet &_G) : G(&_G) {} alpar@400: virtual ~DynMapBase() {} alpar@400: friend class EdgeSet; alpar@400: }; alpar@400: alpar@400: public: alpar@400: //template class NodeMap; alpar@400: template class EdgeMap; alpar@400: alpar@400: class Node; alpar@400: class Edge; alpar@400: alpar@400: // protected: alpar@400: // HELPME: alpar@400: protected: alpar@400: // mutable std::vector * > dyn_node_maps; alpar@400: ///\bug It must be public because of SymEdgeMap. alpar@400: /// alpar@400: mutable std::vector * > dyn_edge_maps; alpar@400: alpar@400: public: alpar@400: alpar@400: class NodeIt; alpar@400: class EdgeIt; alpar@400: class OutEdgeIt; alpar@400: class InEdgeIt; alpar@400: alpar@400: template class NodeMap; alpar@400: template class EdgeMap; alpar@400: alpar@400: public: alpar@400: alpar@408: ///Constructor alpar@408: alpar@408: ///Construates a new graph based on the nodeset of an existing one. alpar@408: ///\param _G the base graph. alpar@408: ///\todo It looks like a copy constructor, but it isn't. alpar@401: EdgeSet(NodeGraphType &_G) : G(_G), alpar@401: nodes(_G), edges(), alpar@401: first_free_edge(-1) { } alpar@408: ///Copy constructor alpar@408: alpar@408: ///Makes a copy of an EdgeSet. alpar@408: ///It will be based on the same graph. alpar@400: EdgeSet(const EdgeSet &_g) : G(_g.G), nodes(_g.G), edges(_g.edges), alpar@401: first_free_edge(_g.first_free_edge) { } alpar@400: alpar@400: ~EdgeSet() alpar@400: { alpar@400: // for(std::vector * >::iterator i=dyn_node_maps.begin(); alpar@400: // i!=dyn_node_maps.end(); ++i) (**i).G=NULL; alpar@400: for(typename std::vector * >::iterator alpar@400: i=dyn_edge_maps.begin(); alpar@400: i!=dyn_edge_maps.end(); ++i) (**i).G=NULL; alpar@400: } alpar@400: alpar@400: int nodeNum() const { return G.nodeNum(); } //FIXME: What is this? alpar@400: int edgeNum() const { return edges.size(); } //FIXME: What is this? alpar@400: alpar@400: ///\bug This function does something different than alpar@400: ///its name would suggests... alpar@400: int maxNodeId() const { return G.maxNodeId(); } //FIXME: What is this? alpar@400: ///\bug This function does something different than alpar@400: ///its name would suggests... alpar@400: int maxEdgeId() const { return edges.size(); } //FIXME: What is this? alpar@400: alpar@400: Node tail(Edge e) const { return edges[e.n].tail; } alpar@400: Node head(Edge e) const { return edges[e.n].head; } alpar@400: alpar@400: Node aNode(OutEdgeIt e) const { return edges[e.n].tail; } alpar@400: Node aNode(InEdgeIt e) const { return edges[e.n].head; } alpar@400: alpar@400: Node bNode(OutEdgeIt e) const { return edges[e.n].head; } alpar@400: Node bNode(InEdgeIt e) const { return edges[e.n].tail; } alpar@400: alpar@400: NodeIt& first(NodeIt& v) const { alpar@400: v=NodeIt(*this); return v; } alpar@400: EdgeIt& first(EdgeIt& e) const { alpar@400: e=EdgeIt(*this); return e; } alpar@400: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { alpar@400: e=OutEdgeIt(*this,v); return e; } alpar@400: InEdgeIt& first(InEdgeIt& e, const Node v) const { alpar@400: e=InEdgeIt(*this,v); return e; } alpar@400: alpar@400: // template< typename It > alpar@400: // It first() const { It e; first(e); return e; } alpar@400: alpar@400: // template< typename It > alpar@400: // It first(Node v) const { It e; first(e,v); return e; } alpar@400: alpar@400: bool valid(Edge e) const { return e.n!=-1; } alpar@400: bool valid(Node n) const { return G.valid(n); } alpar@400: alpar@400: void setInvalid(Edge &e) { e.n=-1; } alpar@400: void setInvalid(Node &n) { G.setInvalid(n); } alpar@400: alpar@400: template It getNext(It it) const alpar@400: { It tmp(it); return next(tmp); } alpar@400: alpar@400: NodeIt& next(NodeIt& it) const { G.next(it); return it; } alpar@400: OutEdgeIt& next(OutEdgeIt& it) const alpar@400: { it.n=edges[it.n].next_out; return it; } alpar@400: InEdgeIt& next(InEdgeIt& it) const alpar@400: { it.n=edges[it.n].next_in; return it; } alpar@400: EdgeIt& next(EdgeIt& it) const { alpar@400: if(edges[it.n].next_in!=-1) { alpar@400: it.n=edges[it.n].next_in; alpar@400: } alpar@400: else { alpar@579: NodeIt n(*this,edges[it.n].head); alpar@579: for(n=next(n); alpar@503: valid(n) && nodes[n].first_in == -1; alpar@503: next(n)) ; alpar@503: it.n = (valid(n))?-1:nodes[n].first_in; alpar@400: } alpar@400: return it; alpar@400: } alpar@400: alpar@400: int id(Edge e) const { return e.n; } alpar@400: alpar@400: /// Adds a new node to the graph. alpar@579: Node addNode() { return G.addNode(); } alpar@400: alpar@400: Edge addEdge(Node u, Node v) { alpar@400: int n; alpar@400: alpar@400: if(first_free_edge==-1) alpar@400: { alpar@400: n = edges.size(); alpar@400: edges.push_back(EdgeT()); alpar@400: } alpar@400: else { alpar@400: n = first_free_edge; alpar@400: first_free_edge = edges[n].next_in; alpar@400: } alpar@400: alpar@401: edges[n].tail = u; edges[n].head = v; alpar@400: alpar@401: edges[n].next_out = nodes[u].first_out; alpar@401: if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n; alpar@401: edges[n].next_in = nodes[v].first_in; alpar@401: if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n; alpar@400: edges[n].prev_in = edges[n].prev_out = -1; alpar@400: alpar@401: nodes[u].first_out = nodes[v].first_in = n; alpar@400: alpar@400: Edge e; e.n=n; alpar@400: alpar@400: //Update dynamic maps alpar@400: for(typename std::vector * >::iterator alpar@400: i=dyn_edge_maps.begin(); alpar@400: i!=dyn_edge_maps.end(); ++i) (**i).add(e); alpar@400: alpar@400: return e; alpar@400: } alpar@400: alpar@400: private: alpar@400: void eraseEdge(int n) { alpar@400: alpar@400: if(edges[n].next_in!=-1) alpar@400: edges[edges[n].next_in].prev_in = edges[n].prev_in; alpar@400: if(edges[n].prev_in!=-1) alpar@400: edges[edges[n].prev_in].next_in = edges[n].next_in; alpar@400: else nodes[edges[n].head].first_in = edges[n].next_in; alpar@400: alpar@400: if(edges[n].next_out!=-1) alpar@400: edges[edges[n].next_out].prev_out = edges[n].prev_out; alpar@400: if(edges[n].prev_out!=-1) alpar@400: edges[edges[n].prev_out].next_out = edges[n].next_out; alpar@400: else nodes[edges[n].tail].first_out = edges[n].next_out; alpar@400: alpar@400: edges[n].next_in = first_free_edge; alpar@400: first_free_edge = -1; alpar@400: alpar@400: //Update dynamic maps alpar@400: Edge e; e.n=n; alpar@400: for(typename std::vector * >::iterator alpar@400: i=dyn_edge_maps.begin(); alpar@400: i!=dyn_edge_maps.end(); ++i) (**i).erase(e); alpar@400: } alpar@400: alpar@400: public: alpar@400: alpar@400: // void erase(Node nn) { alpar@400: // int n=nn.n; alpar@400: // int m; alpar@400: // while((m=nodes[n].first_in)!=-1) eraseEdge(m); alpar@400: // while((m=nodes[n].first_out)!=-1) eraseEdge(m); alpar@400: // } alpar@400: alpar@400: void erase(Edge e) { eraseEdge(e.n); } alpar@400: alpar@579: ///Clear all edges. (Doesn't clear the nodes!) alpar@579: void clear() { alpar@579: edges.clear(); alpar@579: first_free_edge=-1; alpar@579: } alpar@579: alpar@579: alpar@400: // //\bug Dynamic maps must be updated! alpar@400: // // alpar@400: // void clear() { alpar@400: // nodes.clear();edges.clear(); alpar@400: // first_node=first_free_node=first_free_edge=-1; alpar@400: // } alpar@400: alpar@579: public: alpar@579: template class EdgeMap; alpar@579: alpar@579: /// alpar@400: class Edge { alpar@579: public: alpar@400: friend class EdgeSet; alpar@400: template friend class EdgeMap; alpar@400: alpar@400: friend class Node; alpar@400: friend class NodeIt; alpar@579: public: alpar@579: ///\bug It shoud be at least protected alpar@579: /// alpar@579: int n; alpar@400: protected: alpar@400: friend int EdgeSet::id(Edge e) const; alpar@400: alpar@400: Edge(int nn) {n=nn;} alpar@400: public: alpar@400: Edge() { } alpar@400: Edge (Invalid) { n=-1; } alpar@400: bool operator==(const Edge i) const {return n==i.n;} alpar@400: bool operator!=(const Edge i) const {return n!=i.n;} alpar@400: bool operator<(const Edge i) const {return n friend class EdgeMap; alpar@579: alpar@579: alpar@400: public: alpar@400: EdgeIt(const EdgeSet& G) : Edge() { alpar@503: // typename NodeGraphType::Node m; alpar@503: NodeIt m; alpar@400: for(G.first(m); alpar@503: G.valid(m) && G.nodes[m].first_in == -1; G.next(m)); alpar@515: //AJJAJ! This is a non sense!!!!!!! alpar@515: this->n = G.valid(m)?-1:G.nodes[m].first_in; alpar@400: } alpar@400: EdgeIt (Invalid i) : Edge(i) { } alpar@400: EdgeIt() : Edge() { } alpar@400: ///\bug This is a workaround until somebody tells me how to alpar@400: ///make class \c SymEdgeSet::SymEdgeMap friend of Edge alpar@515: int &idref() {return this->n;} alpar@400: }; alpar@400: alpar@400: class OutEdgeIt : public Edge { alpar@400: friend class EdgeSet; alpar@400: public: alpar@400: OutEdgeIt() : Edge() { } alpar@400: OutEdgeIt (Invalid i) : Edge(i) { } alpar@400: alpar@579: OutEdgeIt(const EdgeSet& G,const Node v) : Edge(G.nodes[v].first_out) { } alpar@400: }; alpar@400: alpar@400: class InEdgeIt : public Edge { alpar@400: friend class EdgeSet; alpar@400: public: alpar@400: InEdgeIt() : Edge() { } alpar@400: InEdgeIt (Invalid i) : Edge(i) { } alpar@579: InEdgeIt(const EdgeSet& G,Node v) :Edge(G.nodes[v].first_in) { } alpar@400: }; alpar@400: alpar@515: template class NodeMap : alpar@515: public NodeGraphType::template NodeMap alpar@400: { alpar@579: //This is a must, the constructors need it. alpar@579: typedef typename NodeGraphType::template NodeMap ParentNodeMap; alpar@400: public: alpar@579: NodeMap(const EdgeSet &_G) : ParentNodeMap(_G.G) { } alpar@579: NodeMap(const EdgeSet &_G,const T &t) : ParentNodeMap(_G.G,t) { } alpar@400: //It is unnecessary alpar@515: NodeMap(const typename NodeGraphType::template NodeMap &m) : alpar@579: ParentNodeMap(m) { } alpar@400: alpar@400: ///\todo It can copy between different types. alpar@400: /// alpar@401: template alpar@515: NodeMap(const typename NodeGraphType::template NodeMap &m) alpar@579: : ParentNodeMap(m) { } alpar@400: }; alpar@400: alpar@579: /// alpar@400: template class EdgeMap : public DynMapBase alpar@400: { alpar@579: protected: alpar@579: public: alpar@579: ///\bug It should be at least protected alpar@579: /// alpar@400: std::vector container; alpar@400: alpar@400: public: alpar@400: typedef T ValueType; alpar@400: typedef Edge KeyType; alpar@400: alpar@400: EdgeMap(const EdgeSet &_G) : alpar@400: DynMapBase(_G), container(_G.maxEdgeId()) alpar@400: { alpar@400: //FIXME: What if there are empty Id's? alpar@400: //FIXME: Can I use 'this' in a constructor? alpar@400: G->dyn_edge_maps.push_back(this); alpar@400: } alpar@400: EdgeMap(const EdgeSet &_G,const T &t) : alpar@400: DynMapBase(_G), container(_G.maxEdgeId(),t) alpar@400: { alpar@400: G->dyn_edge_maps.push_back(this); alpar@400: } alpar@400: EdgeMap(const EdgeMap &m) : alpar@400: DynMapBase(*m.G), container(m.container) alpar@400: { alpar@503: G->dyn_edge_maps.push_back(this); alpar@400: } alpar@400: alpar@400: template friend class EdgeMap; alpar@400: alpar@400: ///\todo It can copy between different types. alpar@400: /// alpar@400: template EdgeMap(const EdgeMap &m) : alpar@590: DynMapBase(*m.G), container(m.container.size()) alpar@400: { alpar@503: G->dyn_edge_maps.push_back(this); alpar@400: typename std::vector::const_iterator i; alpar@400: for(typename std::vector::const_iterator i=m.container.begin(); alpar@400: i!=m.container.end(); alpar@400: i++) alpar@400: container.push_back(*i); alpar@400: } alpar@400: ~EdgeMap() alpar@400: { alpar@400: if(G) { alpar@400: typename std::vector* >::iterator i; alpar@400: for(i=G->dyn_edge_maps.begin(); alpar@400: i!=G->dyn_edge_maps.end() && *i!=this; ++i) ; alpar@400: //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow... alpar@400: //A better way to do that: (Is this really important?) alpar@400: if(*i==this) { alpar@400: *i=G->dyn_edge_maps.back(); alpar@400: G->dyn_edge_maps.pop_back(); alpar@400: } alpar@400: } alpar@400: } alpar@400: alpar@400: void add(const Edge k) alpar@400: { alpar@400: if(k.n>=int(container.size())) container.resize(k.n+1); alpar@400: } alpar@400: void erase(const Edge) { } alpar@400: alpar@579: ///\bug This doesn't work. Why? alpar@579: /// void set(Edge n, T a) { container[n.n]=a; } alpar@579: void set(Edge n, T a) { container[G->id(n)]=a; } alpar@400: //T get(Edge n) const { return container[n.n]; } alpar@400: typename std::vector::reference alpar@579: ///\bug This doesn't work. Why? alpar@579: /// operator[](Edge n) { return container[n.n]; } alpar@579: operator[](Edge n) { return container[G->id(n)]; } alpar@400: typename std::vector::const_reference alpar@579: ///\bug This doesn't work. Why? alpar@579: /// operator[](Edge n) const { return container[n.n]; } alpar@579: operator[](Edge n) const { return container[G->id(n)]; } alpar@400: alpar@400: ///\warning There is no safety check at all! alpar@400: ///Using operator = between maps attached to different graph may alpar@400: ///cause serious problem. alpar@400: ///\todo Is this really so? alpar@400: ///\todo It can copy between different types. alpar@400: const EdgeMap& operator=(const EdgeMap &m) alpar@400: { alpar@400: container = m.container; alpar@400: return *this; alpar@400: } alpar@579: alpar@579: template friend class EdgeMap; alpar@579: alpar@400: template alpar@400: const EdgeMap& operator=(const EdgeMap &m) alpar@400: { alpar@531: std::copy(m.container.begin(), m.container.end(), container.begin()); alpar@400: return *this; alpar@400: } alpar@400: alpar@400: void update() {} //Useless for DynMaps alpar@400: void update(T a) {} //Useless for DynMaps alpar@400: }; alpar@400: alpar@400: }; alpar@406: alpar@579: template alpar@579: inline int EdgeSet::id(Node v) const { return G.id(v); } alpar@531: alpar@406: /// @} alpar@406: alpar@395: } //namespace hugo alpar@395: alpar@405: #endif //HUGO_LIST_GRAPH_H