src/hugo/full_graph.h
author jacint
Mon, 20 Sep 2004 08:05:59 +0000
changeset 886 23bcaa25c255
parent 822 88226d9fe821
child 897 ef09eee53b09
permissions -rw-r--r--
(none)
marci@606
     1
// -*- c++ -*-
alpar@591
     2
alpar@591
     3
#ifndef HUGO_FULL_GRAPH_H
alpar@591
     4
#define HUGO_FULL_GRAPH_H
alpar@591
     5
alpar@591
     6
///\ingroup graphs
alpar@591
     7
///\file
alpar@591
     8
///\brief FullGraph and SymFullGraph classes.
alpar@591
     9
alpar@591
    10
#include <vector>
deba@782
    11
#include <climits>
alpar@591
    12
alpar@591
    13
#include <hugo/invalid.h>
alpar@591
    14
deba@782
    15
#include <hugo/map_registry.h>
deba@822
    16
#include <hugo/default_map.h>
deba@822
    17
deba@822
    18
#include <hugo/map_defines.h>
deba@782
    19
alpar@591
    20
namespace hugo {
alpar@591
    21
alpar@591
    22
/// \addtogroup graphs
alpar@591
    23
/// @{
alpar@591
    24
alpar@591
    25
  ///A full graph class.
alpar@591
    26
alpar@591
    27
  ///This is a simple and fast directed full graph implementation.
marci@606
    28
  ///It is completely static, so you can neither add nor delete either
alpar@591
    29
  ///edges or nodes.
alpar@880
    30
  ///Thus it conforms to
alpar@880
    31
  ///the \ref skeleton::StaticGraph "StaticGraph" concept
alpar@880
    32
  ///\sa skeleton::StaticGraph.
alpar@752
    33
  ///\todo What about loops?
alpar@752
    34
  ///\todo Don't we need SymEdgeMap?
alpar@591
    35
  ///
alpar@591
    36
  ///\author Alpar Juttner
alpar@591
    37
  class FullGraph {
alpar@591
    38
    int NodeNum;
alpar@591
    39
    int EdgeNum;
alpar@591
    40
  public:
deba@782
    41
deba@782
    42
    typedef FullGraph Graph;
alpar@591
    43
alpar@591
    44
    class Node;
alpar@591
    45
    class Edge;
deba@782
    46
alpar@591
    47
    class NodeIt;
alpar@591
    48
    class EdgeIt;
alpar@591
    49
    class OutEdgeIt;
alpar@591
    50
    class InEdgeIt;
alpar@591
    51
    
deba@822
    52
deba@822
    53
    /// Creating map registries.
deba@782
    54
    CREATE_MAP_REGISTRIES;
deba@822
    55
    /// Creating node and edge maps.
deba@822
    56
    CREATE_MAPS(DefaultMap);
alpar@591
    57
    
alpar@591
    58
  public:
alpar@591
    59
alpar@591
    60
    ///Creates a full graph with \c n nodes.
alpar@591
    61
    FullGraph(int n) : NodeNum(n), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    62
    ///
alpar@591
    63
    FullGraph(const FullGraph &_g)
alpar@591
    64
      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    65
    
alpar@813
    66
    ///Number of nodes.
alpar@813
    67
    int nodeNum() const { return NodeNum; }
alpar@813
    68
    ///Number of edges.
alpar@813
    69
    int edgeNum() const { return EdgeNum; }
alpar@591
    70
alpar@813
    71
    /// Maximum node ID.
alpar@813
    72
    
alpar@813
    73
    /// Maximum node ID.
alpar@813
    74
    ///\sa id(Node)
alpar@813
    75
    int maxNodeId() const { return NodeNum-1; }
alpar@813
    76
    /// Maximum edge ID.
alpar@813
    77
    
alpar@813
    78
    /// Maximum edge ID.
alpar@813
    79
    ///\sa id(Edge)
alpar@813
    80
    int maxEdgeId() const { return EdgeNum-1; }
alpar@591
    81
alpar@591
    82
    Node tail(Edge e) const { return e.n%NodeNum; }
alpar@591
    83
    Node head(Edge e) const { return e.n/NodeNum; }
alpar@591
    84
alpar@591
    85
    NodeIt& first(NodeIt& v) const {
alpar@591
    86
      v=NodeIt(*this); return v; }
alpar@591
    87
    EdgeIt& first(EdgeIt& e) const { 
alpar@591
    88
      e=EdgeIt(*this); return e; }
alpar@591
    89
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@591
    90
      e=OutEdgeIt(*this,v); return e; }
alpar@591
    91
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@591
    92
      e=InEdgeIt(*this,v); return e; }
alpar@591
    93
alpar@813
    94
    /// Node ID.
alpar@813
    95
    
alpar@813
    96
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
    97
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
    98
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
    99
    ///
alpar@813
   100
    /// The ID of the \ref INVALID node is -1.
alpar@813
   101
    ///\return The ID of the node \c v. 
alpar@713
   102
    static int id(Node v) { return v.n; }
alpar@813
   103
    /// Edge ID.
alpar@813
   104
    
alpar@813
   105
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   106
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   107
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   108
    ///
alpar@813
   109
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   110
    ///\return The ID of the edge \c e. 
alpar@713
   111
    static int id(Edge e) { return e.n; }
alpar@591
   112
alpar@774
   113
    /// Finds an edge between two nodes.
alpar@774
   114
    
alpar@774
   115
    /// Finds an edge from node \c u to node \c v.
alpar@774
   116
    ///
alpar@774
   117
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   118
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   119
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   120
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   121
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   122
    {
deba@782
   123
      return prev.n == -1 ? Edge(*this, u.n, v.n) : INVALID;
alpar@774
   124
    }
alpar@774
   125
    
alpar@774
   126
      
alpar@591
   127
    class Node {
alpar@591
   128
      friend class FullGraph;
alpar@591
   129
      template <typename T> friend class NodeMap;
alpar@591
   130
alpar@591
   131
      friend class Edge;
alpar@591
   132
      friend class OutEdgeIt;
alpar@591
   133
      friend class InEdgeIt;
alpar@591
   134
      friend class SymEdge;
alpar@591
   135
alpar@591
   136
    protected:
alpar@591
   137
      int n;
alpar@722
   138
      friend int FullGraph::id(Node v); 
alpar@591
   139
      Node(int nn) {n=nn;}
alpar@591
   140
    public:
alpar@591
   141
      Node() {}
alpar@591
   142
      Node (Invalid) { n=-1; }
alpar@591
   143
      bool operator==(const Node i) const {return n==i.n;}
alpar@591
   144
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@591
   145
      bool operator<(const Node i) const {return n<i.n;}
alpar@591
   146
    };
alpar@591
   147
    
alpar@591
   148
    class NodeIt : public Node {
alpar@774
   149
      const FullGraph *G;
alpar@591
   150
      friend class FullGraph;
alpar@591
   151
    public:
alpar@591
   152
      NodeIt() : Node() { }
alpar@774
   153
      NodeIt(const FullGraph& _G,Node n) : Node(n), G(&_G) { }
alpar@591
   154
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   155
      NodeIt(const FullGraph& _G) : Node(_G.NodeNum?0:-1), G(&_G) { }
alpar@591
   156
      ///\todo Undocumented conversion Node -\> NodeIt.
alpar@774
   157
      NodeIt& operator++() { n=(n+2)%(G->NodeNum+1)-1;return *this; }
alpar@591
   158
    };
alpar@591
   159
alpar@591
   160
    class Edge {
alpar@591
   161
      friend class FullGraph;
alpar@591
   162
      template <typename T> friend class EdgeMap;
alpar@591
   163
      
alpar@591
   164
      friend class Node;
alpar@591
   165
      friend class NodeIt;
alpar@591
   166
    protected:
alpar@591
   167
      int n; //NodeNum*head+tail;
alpar@722
   168
      friend int FullGraph::id(Edge e);
alpar@591
   169
alpar@774
   170
      Edge(int nn) : n(nn) {}
alpar@774
   171
      Edge(const FullGraph &G, int tail, int head) : n(G.NodeNum*head+tail) {}
alpar@591
   172
    public:
alpar@591
   173
      Edge() { }
alpar@591
   174
      Edge (Invalid) { n=-1; }
alpar@591
   175
      bool operator==(const Edge i) const {return n==i.n;}
alpar@591
   176
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@591
   177
      bool operator<(const Edge i) const {return n<i.n;}
alpar@591
   178
      ///\bug This is a workaround until somebody tells me how to
alpar@591
   179
      ///make class \c SymFullGraph::SymEdgeMap friend of Edge
alpar@591
   180
      int &idref() {return n;}
alpar@591
   181
      const int &idref() const {return n;}
alpar@591
   182
    };
alpar@591
   183
    
alpar@591
   184
    class EdgeIt : public Edge {
alpar@591
   185
      friend class FullGraph;
alpar@591
   186
    public:
alpar@774
   187
      EdgeIt(const FullGraph& _G) : Edge(_G.EdgeNum-1) { }
alpar@774
   188
      EdgeIt(const FullGraph&, Edge e) : Edge(e) { }
alpar@591
   189
      EdgeIt (Invalid i) : Edge(i) { }
alpar@591
   190
      EdgeIt() : Edge() { }
alpar@774
   191
      EdgeIt& operator++() { --n; return *this; }
alpar@774
   192
alpar@591
   193
      ///\bug This is a workaround until somebody tells me how to
alpar@591
   194
      ///make class \c SymFullGraph::SymEdgeMap friend of Edge
alpar@591
   195
      int &idref() {return n;}
alpar@591
   196
    };
alpar@591
   197
    
alpar@591
   198
    class OutEdgeIt : public Edge {
alpar@774
   199
      const FullGraph *G;
alpar@591
   200
      friend class FullGraph;
alpar@591
   201
    public: 
alpar@591
   202
      OutEdgeIt() : Edge() { }
alpar@774
   203
      OutEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@591
   204
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@591
   205
alpar@774
   206
      OutEdgeIt(const FullGraph& _G,const Node v) : Edge(v.n), G(&_G) {}
alpar@774
   207
      
alpar@774
   208
      OutEdgeIt& operator++()
alpar@774
   209
      { n+=G->NodeNum; if(n>=G->EdgeNum) n=-1; return *this; }
alpar@774
   210
alpar@591
   211
    };
alpar@591
   212
    
alpar@591
   213
    class InEdgeIt : public Edge {
alpar@774
   214
      const FullGraph *G;
alpar@591
   215
      friend class FullGraph;
alpar@591
   216
    public: 
alpar@591
   217
      InEdgeIt() : Edge() { }
alpar@774
   218
      InEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@591
   219
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
   220
      InEdgeIt(const FullGraph& _G,Node v) : Edge(v.n*_G.NodeNum), G(&_G) {}
alpar@774
   221
      InEdgeIt& operator++()
alpar@774
   222
      { if(!((++n)%G->NodeNum)) n=-1; return *this; }
alpar@591
   223
    };
alpar@591
   224
alpar@591
   225
  };
alpar@591
   226
alpar@591
   227
  /// @}  
alpar@591
   228
alpar@591
   229
} //namespace hugo
alpar@591
   230
alpar@591
   231
alpar@591
   232
alpar@591
   233
alpar@591
   234
#endif //HUGO_FULL_GRAPH_H