src/hugo/full_graph.h
author marci
Thu, 02 Sep 2004 11:20:49 +0000
changeset 784 a48964a87141
parent 774 4297098d9677
child 798 6d1abeb62dd3
permissions -rw-r--r--
dimacs.h
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@782
    16
#include <hugo/array_map_factory.h>
deba@782
    17
alpar@591
    18
namespace hugo {
alpar@591
    19
alpar@591
    20
/// \addtogroup graphs
alpar@591
    21
/// @{
alpar@591
    22
alpar@591
    23
  ///A full graph class.
alpar@591
    24
alpar@591
    25
  ///This is a simple and fast directed full graph implementation.
marci@606
    26
  ///It is completely static, so you can neither add nor delete either
alpar@591
    27
  ///edges or nodes.
alpar@591
    28
  ///Otherwise it conforms to the graph interface documented under
alpar@591
    29
  ///the description of \ref GraphSkeleton.
alpar@591
    30
  ///\sa \ref GraphSkeleton.
alpar@752
    31
  ///\todo What about loops?
alpar@752
    32
  ///\todo Don't we need SymEdgeMap?
alpar@591
    33
  ///
alpar@591
    34
  ///\author Alpar Juttner
alpar@591
    35
  class FullGraph {
alpar@591
    36
    int NodeNum;
alpar@591
    37
    int EdgeNum;
alpar@591
    38
  public:
deba@782
    39
deba@782
    40
    typedef FullGraph Graph;
alpar@591
    41
alpar@591
    42
    class Node;
alpar@591
    43
    class Edge;
deba@782
    44
alpar@591
    45
    class NodeIt;
alpar@591
    46
    class EdgeIt;
alpar@591
    47
    class OutEdgeIt;
alpar@591
    48
    class InEdgeIt;
alpar@591
    49
    
deba@782
    50
    CREATE_MAP_REGISTRIES;
deba@782
    51
    CREATE_MAPS(ArrayMapFactory);
alpar@591
    52
    
alpar@591
    53
  public:
alpar@591
    54
alpar@591
    55
    ///Creates a full graph with \c n nodes.
alpar@591
    56
    FullGraph(int n) : NodeNum(n), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    57
    ///
alpar@591
    58
    FullGraph(const FullGraph &_g)
alpar@591
    59
      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    60
    
alpar@591
    61
    int nodeNum() const { return NodeNum; }  //FIXME: What is this?
alpar@591
    62
    int edgeNum() const { return EdgeNum; }  //FIXME: What is this?
alpar@591
    63
alpar@591
    64
    int maxNodeId() const { return NodeNum; }  //FIXME: What is this?
alpar@591
    65
    int maxEdgeId() const { return EdgeNum; }  //FIXME: What is this?
alpar@591
    66
alpar@591
    67
    Node tail(Edge e) const { return e.n%NodeNum; }
alpar@591
    68
    Node head(Edge e) const { return e.n/NodeNum; }
alpar@591
    69
alpar@591
    70
    NodeIt& first(NodeIt& v) const {
alpar@591
    71
      v=NodeIt(*this); return v; }
alpar@591
    72
    EdgeIt& first(EdgeIt& e) const { 
alpar@591
    73
      e=EdgeIt(*this); return e; }
alpar@591
    74
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@591
    75
      e=OutEdgeIt(*this,v); return e; }
alpar@591
    76
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@591
    77
      e=InEdgeIt(*this,v); return e; }
alpar@591
    78
alpar@713
    79
    static int id(Node v) { return v.n; }
alpar@713
    80
    static int id(Edge e) { return e.n; }
alpar@591
    81
alpar@774
    82
    /// Finds an edge between two nodes.
alpar@774
    83
    
alpar@774
    84
    /// Finds an edge from node \c u to node \c v.
alpar@774
    85
    ///
alpar@774
    86
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
    87
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
    88
    /// the next edge from \c u to \c v after \c prev.
alpar@774
    89
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
    90
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
    91
    {
deba@782
    92
      return prev.n == -1 ? Edge(*this, u.n, v.n) : INVALID;
alpar@774
    93
    }
alpar@774
    94
    
alpar@774
    95
      
alpar@591
    96
    class Node {
alpar@591
    97
      friend class FullGraph;
alpar@591
    98
      template <typename T> friend class NodeMap;
alpar@591
    99
alpar@591
   100
      friend class Edge;
alpar@591
   101
      friend class OutEdgeIt;
alpar@591
   102
      friend class InEdgeIt;
alpar@591
   103
      friend class SymEdge;
alpar@591
   104
alpar@591
   105
    protected:
alpar@591
   106
      int n;
alpar@722
   107
      friend int FullGraph::id(Node v); 
alpar@591
   108
      Node(int nn) {n=nn;}
alpar@591
   109
    public:
alpar@591
   110
      Node() {}
alpar@591
   111
      Node (Invalid) { n=-1; }
alpar@591
   112
      bool operator==(const Node i) const {return n==i.n;}
alpar@591
   113
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@591
   114
      bool operator<(const Node i) const {return n<i.n;}
alpar@591
   115
    };
alpar@591
   116
    
alpar@591
   117
    class NodeIt : public Node {
alpar@774
   118
      const FullGraph *G;
alpar@591
   119
      friend class FullGraph;
alpar@591
   120
    public:
alpar@591
   121
      NodeIt() : Node() { }
alpar@774
   122
      NodeIt(const FullGraph& _G,Node n) : Node(n), G(&_G) { }
alpar@591
   123
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   124
      NodeIt(const FullGraph& _G) : Node(_G.NodeNum?0:-1), G(&_G) { }
alpar@591
   125
      ///\todo Undocumented conversion Node -\> NodeIt.
alpar@774
   126
      NodeIt& operator++() { n=(n+2)%(G->NodeNum+1)-1;return *this; }
alpar@591
   127
    };
alpar@591
   128
alpar@591
   129
    class Edge {
alpar@591
   130
      friend class FullGraph;
alpar@591
   131
      template <typename T> friend class EdgeMap;
alpar@591
   132
      
alpar@591
   133
      friend class Node;
alpar@591
   134
      friend class NodeIt;
alpar@591
   135
    protected:
alpar@591
   136
      int n; //NodeNum*head+tail;
alpar@722
   137
      friend int FullGraph::id(Edge e);
alpar@591
   138
alpar@774
   139
      Edge(int nn) : n(nn) {}
alpar@774
   140
      Edge(const FullGraph &G, int tail, int head) : n(G.NodeNum*head+tail) {}
alpar@591
   141
    public:
alpar@591
   142
      Edge() { }
alpar@591
   143
      Edge (Invalid) { n=-1; }
alpar@591
   144
      bool operator==(const Edge i) const {return n==i.n;}
alpar@591
   145
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@591
   146
      bool operator<(const Edge i) const {return n<i.n;}
alpar@591
   147
      ///\bug This is a workaround until somebody tells me how to
alpar@591
   148
      ///make class \c SymFullGraph::SymEdgeMap friend of Edge
alpar@591
   149
      int &idref() {return n;}
alpar@591
   150
      const int &idref() const {return n;}
alpar@591
   151
    };
alpar@591
   152
    
alpar@591
   153
    class EdgeIt : public Edge {
alpar@591
   154
      friend class FullGraph;
alpar@591
   155
    public:
alpar@774
   156
      EdgeIt(const FullGraph& _G) : Edge(_G.EdgeNum-1) { }
alpar@774
   157
      EdgeIt(const FullGraph&, Edge e) : Edge(e) { }
alpar@591
   158
      EdgeIt (Invalid i) : Edge(i) { }
alpar@591
   159
      EdgeIt() : Edge() { }
alpar@774
   160
      EdgeIt& operator++() { --n; return *this; }
alpar@774
   161
alpar@591
   162
      ///\bug This is a workaround until somebody tells me how to
alpar@591
   163
      ///make class \c SymFullGraph::SymEdgeMap friend of Edge
alpar@591
   164
      int &idref() {return n;}
alpar@591
   165
    };
alpar@591
   166
    
alpar@591
   167
    class OutEdgeIt : public Edge {
alpar@774
   168
      const FullGraph *G;
alpar@591
   169
      friend class FullGraph;
alpar@591
   170
    public: 
alpar@591
   171
      OutEdgeIt() : Edge() { }
alpar@774
   172
      OutEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@591
   173
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@591
   174
alpar@774
   175
      OutEdgeIt(const FullGraph& _G,const Node v) : Edge(v.n), G(&_G) {}
alpar@774
   176
      
alpar@774
   177
      OutEdgeIt& operator++()
alpar@774
   178
      { n+=G->NodeNum; if(n>=G->EdgeNum) n=-1; return *this; }
alpar@774
   179
alpar@591
   180
    };
alpar@591
   181
    
alpar@591
   182
    class InEdgeIt : public Edge {
alpar@774
   183
      const FullGraph *G;
alpar@591
   184
      friend class FullGraph;
alpar@591
   185
    public: 
alpar@591
   186
      InEdgeIt() : Edge() { }
alpar@774
   187
      InEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@591
   188
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
   189
      InEdgeIt(const FullGraph& _G,Node v) : Edge(v.n*_G.NodeNum), G(&_G) {}
alpar@774
   190
      InEdgeIt& operator++()
alpar@774
   191
      { if(!((++n)%G->NodeNum)) n=-1; return *this; }
alpar@591
   192
    };
alpar@591
   193
alpar@591
   194
  };
alpar@591
   195
alpar@591
   196
  /// @}  
alpar@591
   197
alpar@591
   198
} //namespace hugo
alpar@591
   199
alpar@591
   200
alpar@591
   201
alpar@591
   202
alpar@591
   203
#endif //HUGO_FULL_GRAPH_H