src/work/peter/hierarchygraph.h
author alpar
Mon, 14 Jun 2004 08:35:10 +0000
changeset 678 a6bfd3346245
child 690 a0f95e1b17fc
permissions -rw-r--r--
New group for kruskal
Better links on the main page.
hegyi@677
     1
// -*- c++ -*-
hegyi@677
     2
#ifndef HUGO_NET_GRAPH_H
hegyi@677
     3
#define HUGO_NET_GRAPH_H
hegyi@677
     4
hegyi@677
     5
///\file
hegyi@677
     6
///\brief Declaration of HierarchyGraph.
hegyi@677
     7
hegyi@677
     8
#include <hugo/invalid.h>
hegyi@677
     9
#include <hugo/maps.h>
hegyi@677
    10
hegyi@677
    11
/// The namespace of HugoLib
hegyi@677
    12
namespace hugo {
hegyi@677
    13
hegyi@677
    14
  // @defgroup empty_graph The HierarchyGraph class
hegyi@677
    15
  // @{
hegyi@677
    16
hegyi@677
    17
  /// A graph class in that a simple edge can represent a path.
hegyi@677
    18
  
hegyi@677
    19
  /// This class provides common features of a graph structure
hegyi@677
    20
  /// that represents a network. You can handle with it layers. This
hegyi@677
    21
  /// means that a node in one layer can be a complete network in a nother
hegyi@677
    22
  /// layer.
hegyi@677
    23
hegyi@677
    24
  template <class Gact, class Gsub>
hegyi@677
    25
  class HierarchyGraph
hegyi@677
    26
  {
hegyi@677
    27
hegyi@677
    28
  public:
hegyi@677
    29
hegyi@677
    30
    /// The actual layer
hegyi@677
    31
    Gact actuallayer;
hegyi@677
    32
hegyi@677
    33
hegyi@677
    34
    /// Map of subnetworks that are represented by the nodes of this layer
hegyi@677
    35
    typename Gact::template NodeMap<Gsub> subnetwork;
hegyi@677
    36
hegyi@677
    37
hegyi@677
    38
hegyi@677
    39
    /// Defalult constructor.
hegyi@677
    40
    /// We don't need any extra lines, because the actuallayer
hegyi@677
    41
    /// variable has run its constructor, when we have created this class
hegyi@677
    42
    /// So only the two maps has to be initialised here.
hegyi@677
    43
    HierarchyGraph() : subnetwork(actuallayer)
hegyi@677
    44
    {
hegyi@677
    45
    }
hegyi@677
    46
hegyi@677
    47
hegyi@677
    48
    ///Copy consructor.
hegyi@677
    49
    HierarchyGraph(const HierarchyGraph<Gact, Gsub> & HG ) : actuallayer(HG.actuallayer), subnetwork(actuallayer)
hegyi@677
    50
    {
hegyi@677
    51
    }
hegyi@677
    52
hegyi@677
    53
 
hegyi@677
    54
    /// The base type of the node iterators.
hegyi@677
    55
hegyi@677
    56
    /// This is the base type of each node iterators,
hegyi@677
    57
    /// thus each kind of node iterator will convert to this.
hegyi@677
    58
    /// The Node type of the HierarchyGraph is the Node type of the actual layer.
hegyi@677
    59
    typedef typename Gact::Node Node;
hegyi@677
    60
hegyi@677
    61
    
hegyi@677
    62
    /// This iterator goes through each node.
hegyi@677
    63
hegyi@677
    64
    /// Its usage is quite simple, for example you can count the number
hegyi@677
    65
    /// of nodes in graph \c G of type \c Graph like this:
hegyi@677
    66
    /// \code
hegyi@677
    67
    ///int count=0;
hegyi@677
    68
    ///for(Graph::NodeIt n(G);G.valid(n);G.next(n)) count++;
hegyi@677
    69
    /// \endcode
hegyi@677
    70
    /// The NodeIt type of the HierarchyGraph is the NodeIt type of the actual layer.
hegyi@677
    71
    typedef typename Gact::NodeIt NodeIt;
hegyi@677
    72
    
hegyi@677
    73
    
hegyi@677
    74
    /// The base type of the edge iterators.
hegyi@677
    75
    /// The Edge type of the HierarchyGraph is the Edge type of the actual layer.
hegyi@677
    76
    typedef typename  Gact::Edge Edge;
hegyi@677
    77
hegyi@677
    78
    
hegyi@677
    79
    /// This iterator goes trough the outgoing edges of a node.
hegyi@677
    80
hegyi@677
    81
    /// This iterator goes trough the \e outgoing edges of a certain node
hegyi@677
    82
    /// of a graph.
hegyi@677
    83
    /// Its usage is quite simple, for example you can count the number
hegyi@677
    84
    /// of outgoing edges of a node \c n
hegyi@677
    85
    /// in graph \c G of type \c Graph as follows.
hegyi@677
    86
    /// \code
hegyi@677
    87
    ///int count=0;
hegyi@677
    88
    ///for(Graph::OutEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
hegyi@677
    89
    /// \endcode
hegyi@677
    90
    /// The OutEdgeIt type of the HierarchyGraph is the OutEdgeIt type of the actual layer.
hegyi@677
    91
    typedef typename Gact::OutEdgeIt OutEdgeIt;
hegyi@677
    92
hegyi@677
    93
hegyi@677
    94
    /// This iterator goes trough the incoming edges of a node.
hegyi@677
    95
hegyi@677
    96
    /// This iterator goes trough the \e incoming edges of a certain node
hegyi@677
    97
    /// of a graph.
hegyi@677
    98
    /// Its usage is quite simple, for example you can count the number
hegyi@677
    99
    /// of outgoing edges of a node \c n
hegyi@677
   100
    /// in graph \c G of type \c Graph as follows.
hegyi@677
   101
    /// \code
hegyi@677
   102
    ///int count=0;
hegyi@677
   103
    ///for(Graph::InEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
hegyi@677
   104
    /// \endcode
hegyi@677
   105
    /// The InEdgeIt type of the HierarchyGraph is the InEdgeIt type of the actual layer.
hegyi@677
   106
    typedef typename Gact::InEdgeIt InEdgeIt;
hegyi@677
   107
hegyi@677
   108
hegyi@677
   109
    /// This iterator goes through each edge.
hegyi@677
   110
hegyi@677
   111
    /// This iterator goes through each edge of a graph.
hegyi@677
   112
    /// Its usage is quite simple, for example you can count the number
hegyi@677
   113
    /// of edges in a graph \c G of type \c Graph as follows:
hegyi@677
   114
    /// \code
hegyi@677
   115
    ///int count=0;
hegyi@677
   116
    ///for(Graph::EdgeIt e(G);G.valid(e);G.next(e)) count++;
hegyi@677
   117
    /// \endcode
hegyi@677
   118
    /// The EdgeIt type of the HierarchyGraph is the EdgeIt type of the actual layer.
hegyi@677
   119
    typedef typename Gact::EdgeIt EdgeIt;
hegyi@677
   120
hegyi@677
   121
hegyi@677
   122
    /// First node of the graph.
hegyi@677
   123
hegyi@677
   124
    /// \retval i the first node.
hegyi@677
   125
    /// \return the first node.
hegyi@677
   126
    typename Gact::NodeIt &first(typename Gact::NodeIt &i) const { return actuallayer.first(i);}
hegyi@677
   127
hegyi@677
   128
hegyi@677
   129
    /// The first incoming edge.
hegyi@677
   130
    typename Gact::InEdgeIt &first(typename Gact::InEdgeIt &i, typename Gact::Node) const { return actuallayer.first(i);}
hegyi@677
   131
hegyi@677
   132
hegyi@677
   133
    /// The first outgoing edge.
hegyi@677
   134
    typename Gact::OutEdgeIt &first(typename Gact::OutEdgeIt &i, typename Gact::Node) const { return actuallayer.first(i);}
hegyi@677
   135
hegyi@677
   136
hegyi@677
   137
    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
hegyi@677
   138
    /// The first edge of the Graph.
hegyi@677
   139
    typename Gact::EdgeIt &first(typename Gact::EdgeIt &i) const { return actuallayer.first(i);}
hegyi@677
   140
hegyi@677
   141
hegyi@677
   142
//     Node getNext(Node) const {}
hegyi@677
   143
//     InEdgeIt getNext(InEdgeIt) const {}
hegyi@677
   144
//     OutEdgeIt getNext(OutEdgeIt) const {}
hegyi@677
   145
//     //SymEdgeIt getNext(SymEdgeIt) const {}
hegyi@677
   146
//     EdgeIt getNext(EdgeIt) const {}
hegyi@677
   147
hegyi@677
   148
hegyi@677
   149
    /// Go to the next node.
hegyi@677
   150
    typename Gact::NodeIt &next(typename Gact::NodeIt &i) const { return actuallayer.next(i);}
hegyi@677
   151
    /// Go to the next incoming edge.
hegyi@677
   152
    typename Gact::InEdgeIt &next(typename Gact::InEdgeIt &i) const { return actuallayer.next(i);}
hegyi@677
   153
    /// Go to the next outgoing edge.
hegyi@677
   154
    typename Gact::OutEdgeIt &next(typename Gact::OutEdgeIt &i) const { return actuallayer.next(i);}
hegyi@677
   155
    //SymEdgeIt &next(SymEdgeIt &) const {}
hegyi@677
   156
    /// Go to the next edge.
hegyi@677
   157
    typename Gact::EdgeIt &next(typename Gact::EdgeIt &i) const { return actuallayer.next(i);}
hegyi@677
   158
hegyi@677
   159
    ///Gives back the head node of an edge.
hegyi@677
   160
    typename Gact::Node head(typename Gact::Edge edge) const { return actuallayer.head(edge); }
hegyi@677
   161
    ///Gives back the tail node of an edge.
hegyi@677
   162
    typename Gact::Node tail(typename Gact::Edge edge) const { return actuallayer.tail(edge); }
hegyi@677
   163
  
hegyi@677
   164
    //   Node aNode(InEdgeIt) const {}
hegyi@677
   165
    //   Node aNode(OutEdgeIt) const {}
hegyi@677
   166
    //   Node aNode(SymEdgeIt) const {}
hegyi@677
   167
hegyi@677
   168
    //   Node bNode(InEdgeIt) const {}
hegyi@677
   169
    //   Node bNode(OutEdgeIt) const {}
hegyi@677
   170
    //   Node bNode(SymEdgeIt) const {}
hegyi@677
   171
hegyi@677
   172
    /// Checks if a node iterator is valid
hegyi@677
   173
hegyi@677
   174
    ///\todo Maybe, it would be better if iterator converted to
hegyi@677
   175
    ///bool directly, as Jacint prefers.
hegyi@677
   176
    bool valid(const typename Gact::Node& node) const { return actuallayer.valid(node);}
hegyi@677
   177
    /// Checks if an edge iterator is valid
hegyi@677
   178
hegyi@677
   179
    ///\todo Maybe, it would be better if iterator converted to
hegyi@677
   180
    ///bool directly, as Jacint prefers.
hegyi@677
   181
    bool valid(const typename Gact::Edge& edge) const { return actuallayer.valid(edge);}
hegyi@677
   182
hegyi@677
   183
    ///Gives back the \e id of a node.
hegyi@677
   184
hegyi@677
   185
    ///\warning Not all graph structures provide this feature.
hegyi@677
   186
    ///
hegyi@677
   187
    int id(const typename Gact::Node & node) const { return actuallayer.id(node);}
hegyi@677
   188
    ///Gives back the \e id of an edge.
hegyi@677
   189
hegyi@677
   190
    ///\warning Not all graph structures provide this feature.
hegyi@677
   191
    ///
hegyi@677
   192
    int id(const typename Gact::Edge & edge) const { return actuallayer.id(edge);}
hegyi@677
   193
hegyi@677
   194
    //void setInvalid(Node &) const {};
hegyi@677
   195
    //void setInvalid(Edge &) const {};
hegyi@677
   196
  
hegyi@677
   197
    ///Add a new node to the graph.
hegyi@677
   198
hegyi@677
   199
    /// \return the new node.
hegyi@677
   200
    ///
hegyi@677
   201
    typename Gact::Node addNode() { return actuallayer.addNode();}
hegyi@677
   202
    ///Add a new edge to the graph.
hegyi@677
   203
hegyi@677
   204
    ///Add a new edge to the graph with tail node \c tail
hegyi@677
   205
    ///and head node \c head.
hegyi@677
   206
    ///\return the new edge.
hegyi@677
   207
    typename Gact::Edge addEdge(typename Gact::Node node1, typename Gact::Node node2) { return actuallayer.addEdge(node1, node2);}
hegyi@677
   208
    
hegyi@677
   209
    /// Resets the graph.
hegyi@677
   210
hegyi@677
   211
    /// This function deletes all edges and nodes of the graph.
hegyi@677
   212
    /// It also frees the memory allocated to store them.
hegyi@677
   213
    void clear() {actuallayer.clear();}
hegyi@677
   214
hegyi@677
   215
    int nodeNum() const { return actuallayer.nodeNum();}
hegyi@677
   216
    int edgeNum() const { return actuallayer.edgeNum();}
hegyi@677
   217
hegyi@677
   218
    ///Read/write/reference map of the nodes to type \c T.
hegyi@677
   219
hegyi@677
   220
    ///Read/write/reference map of the nodes to type \c T.
hegyi@677
   221
    /// \sa MemoryMapSkeleton
hegyi@677
   222
    /// \todo We may need copy constructor
hegyi@677
   223
    /// \todo We may need conversion from other nodetype
hegyi@677
   224
    /// \todo We may need operator=
hegyi@677
   225
    /// \warning Making maps that can handle bool type (NodeMap<bool>)
hegyi@677
   226
    /// needs extra attention!
hegyi@677
   227
hegyi@677
   228
    template<class T> class NodeMap
hegyi@677
   229
    {
hegyi@677
   230
    public:
hegyi@677
   231
      typedef T ValueType;
hegyi@677
   232
      typedef Node KeyType;
hegyi@677
   233
hegyi@677
   234
      NodeMap(const HierarchyGraph &) {}
hegyi@677
   235
      NodeMap(const HierarchyGraph &, T) {}
hegyi@677
   236
hegyi@677
   237
      template<typename TT> NodeMap(const NodeMap<TT> &) {}
hegyi@677
   238
hegyi@677
   239
      /// Sets the value of a node.
hegyi@677
   240
hegyi@677
   241
      /// Sets the value associated with node \c i to the value \c t.
hegyi@677
   242
      ///
hegyi@677
   243
      void set(Node, T) {}
hegyi@677
   244
      // Gets the value of a node.
hegyi@677
   245
      //T get(Node i) const {return *(T*)0;}  //FIXME: Is it necessary?
hegyi@677
   246
      T &operator[](Node) {return *(T*)0;}
hegyi@677
   247
      const T &operator[](Node) const {return *(T*)0;}
hegyi@677
   248
hegyi@677
   249
      /// Updates the map if the graph has been changed
hegyi@677
   250
hegyi@677
   251
      /// \todo Do we need this?
hegyi@677
   252
      ///
hegyi@677
   253
      void update() {}
hegyi@677
   254
      void update(T a) {}   //FIXME: Is it necessary
hegyi@677
   255
    };
hegyi@677
   256
hegyi@677
   257
    ///Read/write/reference map of the edges to type \c T.
hegyi@677
   258
hegyi@677
   259
    ///Read/write/reference map of the edges to type \c T.
hegyi@677
   260
    ///It behaves exactly in the same way as \ref NodeMap.
hegyi@677
   261
    /// \sa NodeMap
hegyi@677
   262
    /// \sa MemoryMapSkeleton
hegyi@677
   263
    /// \todo We may need copy constructor
hegyi@677
   264
    /// \todo We may need conversion from other edgetype
hegyi@677
   265
    /// \todo We may need operator=
hegyi@677
   266
    template<class T> class EdgeMap
hegyi@677
   267
    {
hegyi@677
   268
    public:
hegyi@677
   269
      typedef T ValueType;
hegyi@677
   270
      typedef Edge KeyType;
hegyi@677
   271
hegyi@677
   272
      EdgeMap(const HierarchyGraph &) {}
hegyi@677
   273
      EdgeMap(const HierarchyGraph &, T ) {}
hegyi@677
   274
    
hegyi@677
   275
      ///\todo It can copy between different types.
hegyi@677
   276
      ///
hegyi@677
   277
      template<typename TT> EdgeMap(const EdgeMap<TT> &) {}
hegyi@677
   278
hegyi@677
   279
      void set(Edge, T) {}
hegyi@677
   280
      //T get(Edge) const {return *(T*)0;}
hegyi@677
   281
      T &operator[](Edge) {return *(T*)0;}
hegyi@677
   282
      const T &operator[](Edge) const {return *(T*)0;}
hegyi@677
   283
    
hegyi@677
   284
      void update() {}
hegyi@677
   285
      void update(T a) {}   //FIXME: Is it necessary
hegyi@677
   286
    };
hegyi@677
   287
  };
hegyi@677
   288
hegyi@677
   289
  /// An empty eraseable graph class.
hegyi@677
   290
  
hegyi@677
   291
  /// This class provides all the common features of an \e eraseable graph
hegyi@677
   292
  /// structure,
hegyi@677
   293
  /// however completely without implementations and real data structures
hegyi@677
   294
  /// behind the interface.
hegyi@677
   295
  /// All graph algorithms should compile with this class, but it will not
hegyi@677
   296
  /// run properly, of course.
hegyi@677
   297
  ///
hegyi@677
   298
  /// \todo This blabla could be replaced by a sepatate description about
hegyi@677
   299
  /// Skeletons.
hegyi@677
   300
  ///
hegyi@677
   301
  /// It can be used for checking the interface compatibility,
hegyi@677
   302
  /// or it can serve as a skeleton of a new graph structure.
hegyi@677
   303
  /// 
hegyi@677
   304
  /// Also, you will find here the full documentation of a certain graph
hegyi@677
   305
  /// feature, the documentation of a real graph imlementation
hegyi@677
   306
  /// like @ref ListGraph or
hegyi@677
   307
  /// @ref SmartGraph will just refer to this structure.
hegyi@677
   308
  template <typename Gact, typename Gsub>
hegyi@677
   309
  class EraseableHierarchyGraph : public HierarchyGraph<Gact, Gsub>
hegyi@677
   310
  {
hegyi@677
   311
  public:
hegyi@677
   312
    /// Deletes a node.
hegyi@677
   313
    void erase(typename Gact::Node n) {actuallayer.erase(n);}
hegyi@677
   314
    /// Deletes an edge.
hegyi@677
   315
    void erase(typename Gact::Edge e) {actuallayer.erase(e);}
hegyi@677
   316
hegyi@677
   317
    /// Defalult constructor.
hegyi@677
   318
    EraseableHierarchyGraph() {}
hegyi@677
   319
    ///Copy consructor.
hegyi@677
   320
    EraseableHierarchyGraph(const HierarchyGraph<Gact, Gsub> &EPG) {}
hegyi@677
   321
  };
hegyi@677
   322
hegyi@677
   323
  
hegyi@677
   324
  // @}
hegyi@677
   325
hegyi@677
   326
} //namespace hugo
hegyi@677
   327
hegyi@677
   328
hegyi@677
   329
#endif // HUGO_SKELETON_GRAPH_H