src/hugo/smart_graph.h
author hegyi
Tue, 07 Sep 2004 13:55:35 +0000
changeset 815 468c9ec86928
parent 798 6d1abeb62dd3
child 822 88226d9fe821
permissions -rw-r--r--
(none)
     1 // -*- mode:C++ -*-
     2 
     3 #ifndef HUGO_SMART_GRAPH_H
     4 #define HUGO_SMART_GRAPH_H
     5 
     6 ///\ingroup graphs
     7 ///\file
     8 ///\brief SmartGraph and SymSmartGraph classes.
     9 
    10 #include <vector>
    11 #include <climits>
    12 
    13 #include <hugo/invalid.h>
    14 
    15 #include <hugo/default_map_factory.h>
    16 #include <hugo/sym_map_factory.h>
    17 #include <hugo/map_registry.h>
    18 
    19 #include <hugo/map_defines.h>
    20 
    21 namespace hugo {
    22 
    23 /// \addtogroup graphs
    24 /// @{
    25 //  class SymSmartGraph;
    26 
    27   ///A smart graph class.
    28 
    29   ///This is a simple and fast graph implementation.
    30   ///It is also quite memory efficient, but at the price
    31   ///that <b> it does not support node and edge deletion</b>.
    32   ///It conforms to the graph interface documented under
    33   ///the description of \ref GraphSkeleton.
    34   ///\sa \ref GraphSkeleton.
    35   ///
    36   ///\todo Some member functions could be \c static.
    37   ///
    38   ///\todo A possibly useful functionality: a function saveState() would
    39   ///give back a data sturcture X and then the function restoreState(X)
    40   ///would remove the nodes and edges added after the call of saveState().
    41   ///Of course it should be used as a stack. (Maybe X is not necessary.)
    42   ///
    43   ///\author Alpar Juttner
    44   class SmartGraph {
    45 
    46     struct NodeT 
    47     {
    48       int first_in,first_out;      
    49       NodeT() : first_in(-1), first_out(-1) {}
    50     };
    51     struct EdgeT 
    52     {
    53       int head, tail, next_in, next_out;      
    54       //FIXME: is this necessary?
    55       EdgeT() : next_in(-1), next_out(-1) {}  
    56     };
    57 
    58     std::vector<NodeT> nodes;
    59 
    60     std::vector<EdgeT> edges;
    61     
    62     
    63   public:
    64 
    65     typedef SmartGraph Graph;
    66 
    67     class Node;
    68     class Edge;
    69 
    70     class NodeIt;
    71     class EdgeIt;
    72     class OutEdgeIt;
    73     class InEdgeIt;
    74     
    75     CREATE_MAP_REGISTRIES;
    76     CREATE_MAPS(DefaultMapFactory);
    77     
    78   public:
    79 
    80     SmartGraph() : nodes(), edges() { }
    81     SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
    82     
    83     ///Number of nodes.
    84     int nodeNum() const { return nodes.size(); }
    85     ///Number of edges.
    86     int edgeNum() const { return edges.size(); }
    87 
    88     /// Maximum node ID.
    89     
    90     /// Maximum node ID.
    91     ///\sa id(Node)
    92     int maxNodeId() const { return nodes.size()-1; }
    93     /// Maximum edge ID.
    94     
    95     /// Maximum edge ID.
    96     ///\sa id(Edge)
    97     int maxEdgeId() const { return edges.size()-1; }
    98 
    99     Node tail(Edge e) const { return edges[e.n].tail; }
   100     Node head(Edge e) const { return edges[e.n].head; }
   101 
   102     NodeIt& first(NodeIt& v) const { 
   103       v=NodeIt(*this); return v; }
   104     EdgeIt& first(EdgeIt& e) const { 
   105       e=EdgeIt(*this); return e; }
   106     OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
   107       e=OutEdgeIt(*this,v); return e; }
   108     InEdgeIt& first(InEdgeIt& e, const Node v) const { 
   109       e=InEdgeIt(*this,v); return e; }
   110 
   111     /// Node ID.
   112     
   113     /// The ID of a valid Node is a nonnegative integer not greater than
   114     /// \ref maxNodeId(). The range of the ID's is not surely continuous
   115     /// and the greatest node ID can be actually less then \ref maxNodeId().
   116     ///
   117     /// The ID of the \ref INVALID node is -1.
   118     ///\return The ID of the node \c v. 
   119     static int id(Node v) { return v.n; }
   120     /// Edge ID.
   121     
   122     /// The ID of a valid Edge is a nonnegative integer not greater than
   123     /// \ref maxEdgeId(). The range of the ID's is not surely continuous
   124     /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   125     ///
   126     /// The ID of the \ref INVALID edge is -1.
   127     ///\return The ID of the edge \c e. 
   128     static int id(Edge e) { return e.n; }
   129 
   130     Node addNode() {
   131       Node n; n.n=nodes.size();
   132       nodes.push_back(NodeT()); //FIXME: Hmmm...
   133 
   134       
   135       node_maps.add(n);
   136       return n;
   137     }
   138     
   139     Edge addEdge(Node u, Node v) {
   140       Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
   141       edges[e.n].tail=u.n; edges[e.n].head=v.n;
   142       edges[e.n].next_out=nodes[u.n].first_out;
   143       edges[e.n].next_in=nodes[v.n].first_in;
   144       nodes[u.n].first_out=nodes[v.n].first_in=e.n;
   145 
   146       edge_maps.add(e);
   147 
   148       return e;
   149     }
   150 
   151     /// Finds an edge between two nodes.
   152 
   153     /// Finds an edge from node \c u to node \c v.
   154     ///
   155     /// If \c prev is \ref INVALID (this is the default value), then
   156     /// It finds the first edge from \c u to \c v. Otherwise it looks for
   157     /// the next edge from \c u to \c v after \c prev.
   158     /// \return The found edge or INVALID if there is no such an edge.
   159     Edge findEdge(Node u,Node v, Edge prev = INVALID) 
   160     {
   161       int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
   162       while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
   163       prev.n=e;
   164       return prev;
   165     }
   166     
   167     void clear() {
   168       edge_maps.clear();
   169       edges.clear();
   170       node_maps.clear();
   171       nodes.clear();
   172     }
   173 
   174     class Node {
   175       friend class SmartGraph;
   176       template <typename T> friend class NodeMap;
   177       
   178       friend class Edge;
   179       friend class OutEdgeIt;
   180       friend class InEdgeIt;
   181       friend class SymEdge;
   182 
   183     protected:
   184       int n;
   185       friend int SmartGraph::id(Node v); 
   186       Node(int nn) {n=nn;}
   187     public:
   188       Node() {}
   189       Node (Invalid) { n=-1; }
   190       bool operator==(const Node i) const {return n==i.n;}
   191       bool operator!=(const Node i) const {return n!=i.n;}
   192       bool operator<(const Node i) const {return n<i.n;}
   193       //      ///Validity check
   194       //      operator bool() { return n!=-1; }
   195     };
   196     
   197     class NodeIt : public Node {
   198       const SmartGraph *G;
   199       friend class SmartGraph;
   200     public:
   201       NodeIt() : Node() { }
   202       NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
   203       NodeIt(Invalid i) : Node(i) { }
   204       NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
   205       NodeIt &operator++() {
   206 	n=(n+2)%(G->nodes.size()+1)-1; 
   207 	return *this; 
   208       }
   209 //       ///Validity check
   210 //       operator bool() { return Node::operator bool(); }      
   211     };
   212 
   213     class Edge {
   214       friend class SmartGraph;
   215       template <typename T> friend class EdgeMap;
   216 
   217       //template <typename T> friend class SymSmartGraph::SymEdgeMap;      
   218       //friend Edge SymSmartGraph::opposite(Edge) const;
   219       
   220       friend class Node;
   221       friend class NodeIt;
   222     protected:
   223       int n;
   224       friend int SmartGraph::id(Edge e);
   225 
   226     public:
   227       /// An Edge with id \c n.
   228 
   229       /// \bug It should be
   230       /// obtained by a member function of the Graph.
   231       Edge(int nn) {n=nn;}
   232       Edge() { }
   233       Edge (Invalid) { n=-1; }
   234       bool operator==(const Edge i) const {return n==i.n;}
   235       bool operator!=(const Edge i) const {return n!=i.n;}
   236       bool operator<(const Edge i) const {return n<i.n;}
   237       ///\bug This is a workaround until somebody tells me how to
   238       ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
   239       int &idref() {return n;}
   240       const int &idref() const {return n;} 
   241 //       ///Validity check
   242 //       operator bool() { return n!=-1; }
   243    };
   244     
   245     class EdgeIt : public Edge {
   246       const SmartGraph *G;
   247       friend class SmartGraph;
   248     public:
   249       EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
   250       EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
   251       EdgeIt (Invalid i) : Edge(i) { }
   252       EdgeIt() : Edge() { }
   253       ///\bug This is a workaround until somebody tells me how to
   254       ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
   255       int &idref() {return n;}
   256       EdgeIt &operator++() { --n; return *this; }
   257 //       ///Validity check
   258 //       operator bool() { return Edge::operator bool(); }      
   259     };
   260     
   261     class OutEdgeIt : public Edge {
   262       const SmartGraph *G;
   263       friend class SmartGraph;
   264     public: 
   265       OutEdgeIt() : Edge() { }
   266       OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
   267       OutEdgeIt (Invalid i) : Edge(i) { }
   268 
   269       OutEdgeIt(const SmartGraph& _G,const Node v)
   270 	: Edge(_G.nodes[v.n].first_out), G(&_G) {}
   271       OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
   272 //       ///Validity check
   273 //       operator bool() { return Edge::operator bool(); }      
   274     };
   275     
   276     class InEdgeIt : public Edge {
   277       const SmartGraph *G;
   278       friend class SmartGraph;
   279     public: 
   280       InEdgeIt() : Edge() { }
   281       InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
   282       InEdgeIt (Invalid i) : Edge(i) { }
   283       InEdgeIt(const SmartGraph& _G,Node v)
   284 	: Edge(_G.nodes[v.n].first_in), G(&_G) { }
   285       InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
   286 //       ///Validity check
   287 //       operator bool() { return Edge::operator bool(); }      
   288     };
   289 
   290   };
   291 
   292   ///Graph for bidirectional edges.
   293 
   294   ///The purpose of this graph structure is to handle graphs
   295   ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
   296   ///of oppositely directed edges.
   297   ///There is a new edge map type called
   298   ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
   299   ///that complements this
   300   ///feature by
   301   ///storing shared values for the edge pairs. The usual
   302   ///\ref GraphSkeleton::EdgeMap "EdgeMap"
   303   ///can be used
   304   ///as well.
   305   ///
   306   ///The oppositely directed edge can also be obtained easily
   307   ///using \ref opposite.
   308   ///\warning It shares the similarity with \ref SmartGraph that
   309   ///it is not possible to delete edges or nodes from the graph.
   310   //\sa \ref SmartGraph.
   311 
   312   class SymSmartGraph : public SmartGraph
   313   {
   314   public:
   315     typedef SymSmartGraph Graph;
   316 
   317     KEEP_NODE_MAP(SmartGraph);
   318     KEEP_EDGE_MAP(SmartGraph);
   319 
   320     CREATE_SYM_EDGE_MAP_REGISTRY;
   321     CREATE_SYM_EDGE_MAP_FACTORY(DefaultMapFactory);
   322     IMPORT_SYM_EDGE_MAP(SymEdgeMapFactory);
   323 
   324     SymSmartGraph() : SmartGraph() { }
   325     SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
   326     ///Adds a pair of oppositely directed edges to the graph.
   327     Edge addEdge(Node u, Node v)
   328     {
   329       Edge e = SmartGraph::addEdge(u,v);
   330       Edge f = SmartGraph::addEdge(v,u);
   331       sym_edge_maps.add(e);
   332       sym_edge_maps.add(f);
   333       return e;
   334     }
   335 
   336     ///The oppositely directed edge.
   337 
   338     ///Returns the oppositely directed
   339     ///pair of the edge \c e.
   340     static Edge opposite(Edge e)
   341     {
   342       Edge f;
   343       f.idref() = e.idref() - 2*(e.idref()%2) + 1;
   344       return f;
   345     }
   346     
   347 
   348   };
   349   
   350   /// @}  
   351 } //namespace hugo
   352 
   353 
   354 
   355 
   356 #endif //HUGO_SMART_GRAPH_H