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 deba@782: #include alpar@395: ladanyi@542: #include alpar@395: deba@782: #include deba@782: #include deba@782: deba@782: #include deba@782: deba@782: #include deba@782: deba@782: alpar@395: namespace hugo { alpar@395: alpar@406: /// \addtogroup graphs alpar@406: /// @{ alpar@406: deba@782: // 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: deba@782: public: alpar@395: deba@782: typedef ListGraph Graph; alpar@397: alpar@395: class Node; alpar@395: class Edge; alpar@395: alpar@395: alpar@395: public: alpar@395: alpar@395: class NodeIt; alpar@395: class EdgeIt; alpar@395: class OutEdgeIt; alpar@395: class InEdgeIt; deba@782: deba@782: CREATE_MAP_REGISTRIES; deba@782: CREATE_MAPS(ArrayMapFactory); deba@782: alpar@395: public: alpar@395: deba@782: ListGraph() deba@782: : nodes(), first_node(-1), deba@782: first_free_node(-1), edges(), first_free_edge(-1) {} deba@782: deba@782: ListGraph(const ListGraph &_g) deba@782: : nodes(_g.nodes), first_node(_g.first_node), deba@782: first_free_node(_g.first_free_node), edges(_g.edges), deba@782: first_free_edge(_g.first_free_edge) {} 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@695: ///Set the expected number of edges alpar@695: alpar@695: ///With this function, it is possible to set the expected number of edges. alpar@695: ///The use of this fasten the building of the graph and makes alpar@695: ///it possible to avoid the superfluous memory allocation. alpar@695: void reserveEdge(int n) { edges.reserve(n); }; alpar@695: 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@713: NodeIt& first(NodeIt& v) const { alpar@395: v=NodeIt(*this); return v; } alpar@713: EdgeIt& first(EdgeIt& e) const { alpar@395: e=EdgeIt(*this); return e; } alpar@713: OutEdgeIt& first(OutEdgeIt& e, const Node v) const { alpar@395: e=OutEdgeIt(*this,v); return e; } alpar@713: InEdgeIt& first(InEdgeIt& e, const Node v) const { alpar@395: e=InEdgeIt(*this,v); return e; } alpar@395: alpar@713: static int id(Node v) { return v.n; } alpar@713: static int id(Edge e) { 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 deba@782: node_maps.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 deba@782: edge_maps.add(e); alpar@395: alpar@395: return e; alpar@395: } alpar@774: alpar@774: /// Finds an edge between two nodes. alpar@395: alpar@774: /// Finds an edge from node \c u to node \c v. alpar@774: /// alpar@774: /// If \c prev is \ref INVALID (this is the default value), then alpar@774: /// It finds the first edge from \c u to \c v. Otherwise it looks for alpar@774: /// the next edge from \c u to \c v after \c prev. alpar@774: /// \return The found edge or INVALID if there is no such an edge. alpar@774: Edge findEdge(Node u,Node v, Edge prev = INVALID) alpar@774: { alpar@774: int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; alpar@774: while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out; alpar@774: prev.n=e; alpar@774: return prev; alpar@774: } alpar@774: 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@695: first_free_edge = n; alpar@397: alpar@397: //Update dynamic maps alpar@397: Edge e; e.n=n; deba@782: edge_maps.erase(e); deba@782: 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 deba@782: node_maps.erase(nn); deba@782: alpar@397: } alpar@397: alpar@397: void erase(Edge e) { eraseEdge(e.n); } alpar@397: alpar@397: void clear() { deba@782: edge_maps.clear(); deba@782: edges.clear(); deba@782: node_maps.clear(); deba@782: nodes.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@722: friend int ListGraph::id(Node v); 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@774: NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { } alpar@774: NodeIt &operator++() { alpar@774: n=G->nodes[n].next; alpar@774: return *this; alpar@774: } alpar@774: // ///Validity check alpar@774: // operator bool() { return Node::operator bool(); } 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@722: friend int ListGraph::id(Edge e); alpar@395: 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@395: Edge(int nn) {n=nn;} alpar@706: 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 nedges[n].next_in!=-1) n=G->edges[n].next_in; alpar@774: else { alpar@774: int nn; alpar@774: for(nn=G->nodes[G->edges[n].head].next; alpar@774: nn!=-1 && G->nodes[nn].first_in == -1; alpar@774: nn = G->nodes[nn].next) ; alpar@774: n = (nn==-1)?-1:G->nodes[nn].first_in; alpar@774: } alpar@774: return *this; alpar@774: } alpar@774: // ///Validity check alpar@774: // operator bool() { return Edge::operator bool(); } alpar@395: }; alpar@395: alpar@395: class OutEdgeIt : public Edge { alpar@774: const ListGraph *G; alpar@397: friend class ListGraph; alpar@395: public: alpar@395: OutEdgeIt() : Edge() { } alpar@774: OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } alpar@395: OutEdgeIt (Invalid i) : Edge(i) { } alpar@395: alpar@774: OutEdgeIt(const ListGraph& _G,const Node v) alpar@774: : Edge(_G.nodes[v.n].first_out), G(&_G) {} alpar@774: OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; } alpar@774: // ///Validity check alpar@774: // operator bool() { return Edge::operator bool(); } alpar@395: }; alpar@395: alpar@395: class InEdgeIt : public Edge { alpar@774: const ListGraph *G; alpar@397: friend class ListGraph; alpar@395: public: alpar@395: InEdgeIt() : Edge() { } alpar@774: InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { } alpar@395: InEdgeIt (Invalid i) : Edge(i) { } alpar@774: InEdgeIt(const ListGraph& _G,Node v) alpar@774: : Edge(_G.nodes[v.n].first_in), G(&_G) { } alpar@774: InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; } alpar@774: // ///Validity check alpar@774: // operator bool() { return Edge::operator bool(); } 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. deba@782: alpar@397: class SymListGraph : public ListGraph alpar@395: { alpar@395: public: deba@782: deba@782: typedef SymListGraph Graph; deba@782: deba@782: KEEP_NODE_MAP(ListGraph); deba@782: KEEP_EDGE_MAP(ListGraph); deba@782: deba@782: CREATE_SYM_EDGE_MAP_REGISTRY; deba@782: CREATE_SYM_EDGE_MAP_FACTORY(ArrayMapFactory); deba@782: IMPORT_SYM_EDGE_MAP(SymEdgeMapFactory); 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); deba@782: Edge f = ListGraph::addEdge(v,u); deba@782: sym_edge_maps.add(e); deba@782: sym_edge_maps.add(f); deba@782: alpar@395: return e; alpar@395: } alpar@395: deba@782: 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@713: static Edge opposite(Edge e) 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) { deba@782: Edge f = opposite(e); deba@782: sym_edge_maps.erase(e); deba@782: sym_edge_maps.erase(f); deba@782: ListGraph::erase(f); alpar@397: ListGraph::erase(e); deba@782: } deba@782: }; 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: public: deba@782: deba@782: typedef NodeSet Graph; alpar@400: alpar@400: class Node; alpar@400: class Edge; 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: deba@782: CREATE_MAP_REGISTRIES; deba@782: CREATE_MAPS(ArrayMapFactory); alpar@400: alpar@400: public: alpar@400: alpar@408: ///Default constructor deba@782: NodeSet() deba@782: : nodes(), first_node(-1), first_free_node(-1) {} alpar@408: ///Copy constructor deba@782: NodeSet(const NodeSet &_g) deba@782: : nodes(_g.nodes), first_node(_g.first_node), deba@782: first_free_node(_g.first_free_node) {} 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: 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: 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 deba@782: node_maps.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 deba@782: node_maps.erase(nn); alpar@400: } alpar@400: alpar@774: alpar@774: Edge findEdge(Node u,Node v, Edge prev = INVALID) alpar@774: { alpar@774: return INVALID; alpar@774: } alpar@774: alpar@400: void clear() { deba@782: node_maps.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 nnodes[n].next; alpar@774: return *this; alpar@774: } 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@774: EdgeIt(const NodeSet&, Edge) : 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@774: EdgeIt operator++() { return INVALID; } alpar@400: }; alpar@400: alpar@400: class OutEdgeIt : public Edge { alpar@400: friend class NodeSet; alpar@400: public: alpar@400: OutEdgeIt() : Edge() { } alpar@774: OutEdgeIt(const NodeSet&, Edge) : Edge() { } alpar@400: OutEdgeIt (Invalid i) : Edge(i) { } alpar@400: OutEdgeIt(const NodeSet& G,const Node v) : Edge() {} alpar@774: OutEdgeIt operator++() { return INVALID; } alpar@400: }; alpar@400: alpar@400: class InEdgeIt : public Edge { alpar@400: friend class NodeSet; alpar@400: public: alpar@400: InEdgeIt() : Edge() { } alpar@774: InEdgeIt(const NodeSet&, Edge) : Edge() { } alpar@400: InEdgeIt (Invalid i) : Edge(i) { } alpar@400: InEdgeIt(const NodeSet& G,Node v) :Edge() {} alpar@774: InEdgeIt operator++() { return INVALID; } alpar@400: }; 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: deba@782: alpar@400: class Node; alpar@705: class Edge; alpar@705: class OutEdgeIt; alpar@705: class InEdgeIt; alpar@705: class SymEdge; deba@782: deba@782: typedef EdgeSet Graph; deba@782: 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@774: NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { } 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: alpar@531: operator Node() { return Node(*this);} alpar@774: NodeIt &operator++() alpar@774: { this->NodeGraphType::NodeIt::operator++(); return *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: public: alpar@400: alpar@400: class Node; alpar@400: class Edge; alpar@400: alpar@400: class NodeIt; alpar@400: class EdgeIt; alpar@400: class OutEdgeIt; alpar@400: class InEdgeIt; deba@782: deba@782: deba@782: CREATE_EDGE_MAP_REGISTRY; deba@782: CREATE_EDGE_MAP_FACTORY(ArrayMapFactory); deba@782: IMPORT_EDGE_MAP(EdgeMapFactory); alpar@400: 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. deba@782: EdgeSet(NodeGraphType &_G) deba@782: : G(_G), nodes(_G), edges(), deba@782: 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. deba@782: EdgeSet(const EdgeSet &_g) deba@782: : G(_g.G), nodes(_g.G), edges(_g.edges), deba@782: first_free_edge(_g.first_free_edge) {} 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: 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: 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 deba@782: edge_maps.add(e); alpar@400: alpar@400: return e; alpar@400: } alpar@400: alpar@774: /// Finds an edge between two nodes. alpar@774: alpar@774: /// Finds an edge from node \c u to node \c v. alpar@774: /// alpar@774: /// If \c prev is \ref INVALID (this is the default value), then alpar@774: /// It finds the first edge from \c u to \c v. Otherwise it looks for alpar@774: /// the next edge from \c u to \c v after \c prev. alpar@774: /// \return The found edge or INVALID if there is no such an edge. alpar@774: Edge findEdge(Node u,Node v, Edge prev = INVALID) alpar@774: { alpar@774: int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out; alpar@774: while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out; alpar@774: prev.n=e; alpar@774: return prev; alpar@774: } alpar@774: 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 deba@782: Edge e; e.n = n; deba@782: edge_maps.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() { deba@782: edge_maps.clear(); alpar@579: edges.clear(); alpar@579: first_free_edge=-1; alpar@579: } 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@774: ///\bug It should 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@774: const EdgeSet *G; alpar@400: public: alpar@774: EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) { alpar@503: // typename NodeGraphType::Node m; alpar@503: NodeIt m; alpar@774: for(G->first(m); alpar@774: m!=INVALID && G->nodes[m].first_in == -1; ++m); alpar@774: ///\bug AJJAJ! This is a non sense!!!!!!! alpar@774: this->n = m!=INVALID?-1:G->nodes[m].first_in; alpar@400: } alpar@774: EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } alpar@400: EdgeIt (Invalid i) : Edge(i) { } alpar@400: EdgeIt() : Edge() { } alpar@774: ///. alpar@774: alpar@774: ///\bug UNIMPLEMENTED!!!!! alpar@774: // alpar@774: EdgeIt &operator++() { alpar@774: return *this; alpar@774: } alpar@774: ///\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@774: const EdgeSet *G; alpar@400: friend class EdgeSet; alpar@400: public: alpar@400: OutEdgeIt() : Edge() { } alpar@400: OutEdgeIt (Invalid i) : Edge(i) { } alpar@774: OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } alpar@400: alpar@774: OutEdgeIt(const EdgeSet& _G,const Node v) : alpar@774: Edge(_G.nodes[v].first_out), G(&_G) { } deba@782: OutEdgeIt &operator++() { n = G->edges[n].next_out; return *this; } alpar@400: }; alpar@400: alpar@400: class InEdgeIt : public Edge { alpar@774: const EdgeSet *G; alpar@400: friend class EdgeSet; alpar@400: public: alpar@400: InEdgeIt() : Edge() { } alpar@400: InEdgeIt (Invalid i) : Edge(i) { } alpar@774: InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { } alpar@774: InEdgeIt(const EdgeSet& _G,Node v) alpar@774: : Edge(_G.nodes[v].first_in), G(&_G) { } alpar@774: InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; } alpar@400: }; alpar@400: deba@782: deba@782: template class NodeMap deba@782: : public NodeGraphType::template NodeMap alpar@400: { alpar@579: //This is a must, the constructors need it. deba@782: typedef typename NodeGraphType::template NodeMap MapImpl; deba@782: typedef V Value; alpar@400: public: deba@782: NodeMap() : MapImpl() {} deba@782: deba@782: NodeMap(const EdgeSet& graph) deba@782: : MapImpl(graph.G) { } alpar@400: deba@782: NodeMap(const EdgeSet& graph, const Value& value) deba@782: : MapImpl(graph.G, value) { } alpar@400: deba@782: NodeMap(const NodeMap& copy) deba@782: : MapImpl(static_cast(copy)) {} alpar@400: deba@782: template deba@782: NodeMap(const CMap& copy) deba@782: : MapImpl(copy) { } deba@782: deba@782: NodeMap& operator=(const NodeMap& copy) { deba@782: MapImpl::operator=(static_cast(copy)); deba@782: return *this; alpar@400: } alpar@400: deba@782: template deba@782: NodeMap& operator=(const CMap& copy) { deba@782: MapImpl::operator=(copy); alpar@400: return *this; alpar@400: } alpar@579: 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