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