src/work/peter/edgepathgraph.h
author alpar
Mon, 21 Mar 2005 11:40:08 +0000
changeset 1233 f3d856bf1ebf
parent 986 e997802b855c
permissions -rw-r--r--
Several serious bugs fixed
hegyi@677
     1
// -*- c++ -*-
alpar@921
     2
#ifndef LEMON_NET_GRAPH_H
alpar@921
     3
#define LEMON_NET_GRAPH_H
hegyi@677
     4
hegyi@677
     5
///\file
hegyi@677
     6
///\brief Declaration of EdgePathGraph.
hegyi@677
     7
alpar@921
     8
#include <lemon/invalid.h>
alpar@921
     9
#include <lemon/maps.h>
hegyi@677
    10
alpar@921
    11
/// The namespace of LEMON
alpar@921
    12
namespace lemon {
hegyi@677
    13
hegyi@677
    14
  // @defgroup empty_graph The EdgePathGraph 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 all the 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 an edge in one layer can be a complete path in a nother
hegyi@677
    22
  /// layer.
hegyi@677
    23
hegyi@677
    24
  template <typename P, class Gact, class Gsub>
hegyi@677
    25
  class EdgePathGraph
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
    /// The layer on which the edges in this layer can represent paths.
hegyi@677
    35
    Gsub * sublayer;
hegyi@677
    36
hegyi@677
    37
hegyi@677
    38
    /// Map of nodes that represent the nodes of this layer in the sublayer
hegyi@677
    39
    typename Gact::template NodeMap<typename Gsub::Node *> projection;
hegyi@677
    40
hegyi@677
    41
hegyi@677
    42
    /// Map of routes that are represented by some edges in this layer
hegyi@677
    43
    typename Gact::template EdgeMap<P *> edgepath;
hegyi@677
    44
hegyi@677
    45
hegyi@677
    46
    /// Defalult constructor.
hegyi@677
    47
    /// We don't need any extra lines, because the actuallayer
hegyi@677
    48
    /// variable has run its constructor, when we have created this class
hegyi@677
    49
    /// So only the two maps has to be initialised here.
hegyi@677
    50
    EdgePathGraph() : projection(actuallayer), edgepath(actuallayer)
hegyi@677
    51
    {
hegyi@677
    52
    }
hegyi@677
    53
hegyi@677
    54
hegyi@677
    55
    ///Copy consructor.
hegyi@677
    56
    EdgePathGraph(const EdgePathGraph<P, Gact, Gsub> & EPG ) : actuallayer(EPG.actuallayer) , edgepath(actuallayer), projection(actuallayer)
hegyi@677
    57
    {
hegyi@677
    58
    }
hegyi@677
    59
hegyi@677
    60
hegyi@677
    61
    /// Map adder
hegyi@677
    62
hegyi@677
    63
    /// This function gets two edgemaps. One belongs to the actual layer and the
hegyi@677
    64
    /// other belongs to the sublayer.
hegyi@677
    65
    /// The function iterates through all of the edges in the edgemap belonging to the actual layer.
hegyi@677
    66
    /// It gets the value that belongs to the actual edge, and adds it to the value of each edge in the
hegyi@677
    67
    /// path represented by itself in the edgemap that belongs to the sublayer.
hegyi@677
    68
    
hegyi@677
    69
    template <typename T1, typename T2> void addMap (typename Gact::EdgeMap<T1> & actmap, typename Gsub::EdgeMap<T2> & submap)
hegyi@677
    70
    {
hegyi@677
    71
      for(EdgeIt e(actuallayer);actuallayer.valid(e);actuallayer.next(e))
hegyi@677
    72
      {
hegyi@677
    73
	typedef typename P::EdgeIt PEdgeIt;
hegyi@677
    74
	PEdgeIt f;
hegyi@677
    75
alpar@986
    76
	//dep//cout << "Edge " << id(source(e)) << " - " << id(target(e)) << " in actual layer is";
hegyi@677
    77
	T1 incr=actmap[e];
hegyi@677
    78
	//cout << incr << endl;
hegyi@677
    79
hegyi@677
    80
        if(edgepath[e])
hegyi@677
    81
	{
hegyi@677
    82
	  //dep//cout << endl << "Path";
hegyi@677
    83
	  for(edgepath[e]->first(f); edgepath[e]->valid(f); edgepath[e]->next(f))
hegyi@677
    84
	  {
alpar@986
    85
	    //dep//cout << " " << sublayer->id(sublayer->source(f)) << "-" << sublayer->id(sublayer->target(f));
hegyi@677
    86
	    submap[f]+=incr;
hegyi@677
    87
	  }
alpar@986
    88
	  //dep////cout << EPGr2.id(EPGr2.target(f)) << endl;
hegyi@677
    89
	  //dep//cout << endl;
hegyi@677
    90
	}
hegyi@677
    91
	else
hegyi@677
    92
	{
hegyi@677
    93
	  //dep//cout << " itself." <<endl;
hegyi@677
    94
	}
hegyi@677
    95
      }  
hegyi@677
    96
hegyi@677
    97
    };
hegyi@677
    98
hegyi@677
    99
hegyi@677
   100
    /// Describe
hegyi@677
   101
    /// This function walks thorugh the edges of the actual layer
hegyi@677
   102
    /// and displays the path represented by the actual edge.
hegyi@677
   103
    void describe ()
hegyi@677
   104
    {
hegyi@677
   105
      for(EdgeIt e(actuallayer);actuallayer.valid(e);actuallayer.next(e))
hegyi@677
   106
      {
hegyi@677
   107
	typedef typename P::EdgeIt PEdgeIt;
hegyi@677
   108
	PEdgeIt f;
hegyi@677
   109
alpar@986
   110
	cout << "Edge " << id(source(e)) << " - " << id(target(e)) << " in actual layer is";
hegyi@677
   111
        if(edgepath[e])
hegyi@677
   112
	{
hegyi@677
   113
	  cout << endl << "Path";
hegyi@677
   114
	  for(edgepath[e]->first(f); edgepath[e]->valid(f); edgepath[e]->next(f))
hegyi@677
   115
	  {
alpar@986
   116
	    cout << " " << sublayer->id(sublayer->source(f)) << "-" << sublayer->id(sublayer->target(f));
hegyi@677
   117
	  }
alpar@986
   118
	  //cout << EPGr2.id(EPGr2.target(f)) << endl;
hegyi@677
   119
	  cout << endl;
hegyi@677
   120
	}
hegyi@677
   121
	else
hegyi@677
   122
	{
hegyi@677
   123
	  cout << " itself." <<endl;
hegyi@677
   124
	}
hegyi@677
   125
      }  
hegyi@677
   126
hegyi@677
   127
    };
hegyi@677
   128
hegyi@677
   129
hegyi@677
   130
hegyi@677
   131
hegyi@677
   132
    /// The base type of the node iterators.
hegyi@677
   133
hegyi@677
   134
    /// This is the base type of each node iterators,
hegyi@677
   135
    /// thus each kind of node iterator will convert to this.
hegyi@677
   136
    /// The Node type of the EdgePathGraph is the Node type of the actual layer.
hegyi@677
   137
    typedef typename Gact::Node Node;
hegyi@677
   138
hegyi@677
   139
    
hegyi@677
   140
    /// This iterator goes through each node.
hegyi@677
   141
hegyi@677
   142
    /// Its usage is quite simple, for example you can count the number
hegyi@677
   143
    /// of nodes in graph \c G of type \c Graph like this:
hegyi@677
   144
    /// \code
hegyi@677
   145
    ///int count=0;
hegyi@677
   146
    ///for(Graph::NodeIt n(G);G.valid(n);G.next(n)) count++;
hegyi@677
   147
    /// \endcode
hegyi@677
   148
    /// The NodeIt type of the EdgePathGraph is the NodeIt type of the actual layer.
hegyi@677
   149
    typedef typename Gact::NodeIt NodeIt;
hegyi@677
   150
    
hegyi@677
   151
    
hegyi@677
   152
    /// The base type of the edge iterators.
hegyi@677
   153
    /// The Edge type of the EdgePathGraph is the Edge type of the actual layer.
hegyi@677
   154
    typedef typename  Gact::Edge Edge;
hegyi@677
   155
hegyi@677
   156
    
hegyi@677
   157
    /// This iterator goes trough the outgoing edges of a node.
hegyi@677
   158
hegyi@677
   159
    /// This iterator goes trough the \e outgoing edges of a certain node
hegyi@677
   160
    /// of a graph.
hegyi@677
   161
    /// Its usage is quite simple, for example you can count the number
hegyi@677
   162
    /// of outgoing edges of a node \c n
hegyi@677
   163
    /// in graph \c G of type \c Graph as follows.
hegyi@677
   164
    /// \code
hegyi@677
   165
    ///int count=0;
hegyi@677
   166
    ///for(Graph::OutEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
hegyi@677
   167
    /// \endcode
hegyi@677
   168
    /// The OutEdgeIt type of the EdgePathGraph is the OutEdgeIt type of the actual layer.
hegyi@677
   169
    typedef typename Gact::OutEdgeIt OutEdgeIt;
hegyi@677
   170
hegyi@677
   171
hegyi@677
   172
    /// This iterator goes trough the incoming edges of a node.
hegyi@677
   173
hegyi@677
   174
    /// This iterator goes trough the \e incoming edges of a certain node
hegyi@677
   175
    /// of a graph.
hegyi@677
   176
    /// Its usage is quite simple, for example you can count the number
hegyi@677
   177
    /// of outgoing edges of a node \c n
hegyi@677
   178
    /// in graph \c G of type \c Graph as follows.
hegyi@677
   179
    /// \code
hegyi@677
   180
    ///int count=0;
hegyi@677
   181
    ///for(Graph::InEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
hegyi@677
   182
    /// \endcode
hegyi@677
   183
    /// The InEdgeIt type of the EdgePathGraph is the InEdgeIt type of the actual layer.
hegyi@677
   184
    typedef typename Gact::InEdgeIt InEdgeIt;
hegyi@677
   185
hegyi@677
   186
hegyi@677
   187
    /// This iterator goes through each edge.
hegyi@677
   188
hegyi@677
   189
    /// This iterator goes through each edge of a graph.
hegyi@677
   190
    /// Its usage is quite simple, for example you can count the number
hegyi@677
   191
    /// of edges in a graph \c G of type \c Graph as follows:
hegyi@677
   192
    /// \code
hegyi@677
   193
    ///int count=0;
hegyi@677
   194
    ///for(Graph::EdgeIt e(G);G.valid(e);G.next(e)) count++;
hegyi@677
   195
    /// \endcode
hegyi@677
   196
    /// The EdgeIt type of the EdgePathGraph is the EdgeIt type of the actual layer.
hegyi@677
   197
    typedef typename Gact::EdgeIt EdgeIt;
hegyi@677
   198
hegyi@677
   199
hegyi@677
   200
    /// First node of the graph.
hegyi@677
   201
hegyi@677
   202
    /// \retval i the first node.
hegyi@677
   203
    /// \return the first node.
hegyi@677
   204
    typename Gact::NodeIt &first(typename Gact::NodeIt &i) const { return actuallayer.first(i);}
hegyi@677
   205
hegyi@677
   206
hegyi@677
   207
    /// The first incoming edge.
hegyi@677
   208
    typename Gact::InEdgeIt &first(typename Gact::InEdgeIt &i, typename Gact::Node) const { return actuallayer.first(i);}
hegyi@677
   209
hegyi@677
   210
hegyi@677
   211
    /// The first outgoing edge.
hegyi@677
   212
    typename Gact::OutEdgeIt &first(typename Gact::OutEdgeIt &i, typename Gact::Node) const { return actuallayer.first(i);}
hegyi@677
   213
hegyi@677
   214
hegyi@677
   215
    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
hegyi@677
   216
    /// The first edge of the Graph.
hegyi@677
   217
    typename Gact::EdgeIt &first(typename Gact::EdgeIt &i) const { return actuallayer.first(i);}
hegyi@677
   218
hegyi@677
   219
hegyi@677
   220
//     Node getNext(Node) const {}
hegyi@677
   221
//     InEdgeIt getNext(InEdgeIt) const {}
hegyi@677
   222
//     OutEdgeIt getNext(OutEdgeIt) const {}
hegyi@677
   223
//     //SymEdgeIt getNext(SymEdgeIt) const {}
hegyi@677
   224
//     EdgeIt getNext(EdgeIt) const {}
hegyi@677
   225
hegyi@677
   226
hegyi@677
   227
    /// Go to the next node.
hegyi@677
   228
    typename Gact::NodeIt &next(typename Gact::NodeIt &i) const { return actuallayer.next(i);}
hegyi@677
   229
    /// Go to the next incoming edge.
hegyi@677
   230
    typename Gact::InEdgeIt &next(typename Gact::InEdgeIt &i) const { return actuallayer.next(i);}
hegyi@677
   231
    /// Go to the next outgoing edge.
hegyi@677
   232
    typename Gact::OutEdgeIt &next(typename Gact::OutEdgeIt &i) const { return actuallayer.next(i);}
hegyi@677
   233
    //SymEdgeIt &next(SymEdgeIt &) const {}
hegyi@677
   234
    /// Go to the next edge.
hegyi@677
   235
    typename Gact::EdgeIt &next(typename Gact::EdgeIt &i) const { return actuallayer.next(i);}
hegyi@677
   236
alpar@986
   237
    ///Gives back the target node of an edge.
alpar@986
   238
    typename Gact::Node target(typename Gact::Edge edge) const { return actuallayer.target(edge); }
alpar@986
   239
    ///Gives back the source node of an edge.
alpar@986
   240
    typename Gact::Node source(typename Gact::Edge edge) const { return actuallayer.source(edge); }
hegyi@677
   241
  
hegyi@677
   242
    //   Node aNode(InEdgeIt) const {}
hegyi@677
   243
    //   Node aNode(OutEdgeIt) const {}
hegyi@677
   244
    //   Node aNode(SymEdgeIt) const {}
hegyi@677
   245
hegyi@677
   246
    //   Node bNode(InEdgeIt) const {}
hegyi@677
   247
    //   Node bNode(OutEdgeIt) const {}
hegyi@677
   248
    //   Node bNode(SymEdgeIt) const {}
hegyi@677
   249
hegyi@677
   250
    /// Checks if a node iterator is valid
hegyi@677
   251
hegyi@677
   252
    ///\todo Maybe, it would be better if iterator converted to
hegyi@677
   253
    ///bool directly, as Jacint prefers.
hegyi@677
   254
    bool valid(const typename Gact::Node& node) const { return actuallayer.valid(node);}
hegyi@677
   255
    /// Checks if an edge iterator is valid
hegyi@677
   256
hegyi@677
   257
    ///\todo Maybe, it would be better if iterator converted to
hegyi@677
   258
    ///bool directly, as Jacint prefers.
hegyi@677
   259
    bool valid(const typename Gact::Edge& edge) const { return actuallayer.valid(edge);}
hegyi@677
   260
hegyi@677
   261
    ///Gives back the \e id of a node.
hegyi@677
   262
hegyi@677
   263
    ///\warning Not all graph structures provide this feature.
hegyi@677
   264
    ///
hegyi@677
   265
    int id(const typename Gact::Node & node) const { return actuallayer.id(node);}
hegyi@677
   266
    ///Gives back the \e id of an edge.
hegyi@677
   267
hegyi@677
   268
    ///\warning Not all graph structures provide this feature.
hegyi@677
   269
    ///
hegyi@677
   270
    int id(const typename Gact::Edge & edge) const { return actuallayer.id(edge);}
hegyi@677
   271
hegyi@677
   272
    //void setInvalid(Node &) const {};
hegyi@677
   273
    //void setInvalid(Edge &) const {};
hegyi@677
   274
  
hegyi@677
   275
    ///Add a new node to the graph.
hegyi@677
   276
hegyi@677
   277
    /// \return the new node.
hegyi@677
   278
    ///
hegyi@677
   279
    typename Gact::Node addNode() { return actuallayer.addNode();}
hegyi@677
   280
    ///Add a new edge to the graph.
hegyi@677
   281
alpar@986
   282
    ///Add a new edge to the graph with source node \c source
alpar@986
   283
    ///and target node \c target.
hegyi@677
   284
    ///\return the new edge.
hegyi@677
   285
    typename Gact::Edge addEdge(typename Gact::Node node1, typename Gact::Node node2) { return actuallayer.addEdge(node1, node2);}
hegyi@677
   286
    
hegyi@677
   287
    /// Resets the graph.
hegyi@677
   288
hegyi@677
   289
    /// This function deletes all edges and nodes of the graph.
hegyi@677
   290
    /// It also frees the memory allocated to store them.
hegyi@677
   291
    void clear() {actuallayer.clear();}
hegyi@677
   292
hegyi@677
   293
    int nodeNum() const { return actuallayer.nodeNum();}
hegyi@677
   294
    int edgeNum() const { return actuallayer.edgeNum();}
hegyi@677
   295
hegyi@677
   296
    ///Read/write/reference map of the nodes to type \c T.
hegyi@677
   297
hegyi@677
   298
    ///Read/write/reference map of the nodes to type \c T.
alpar@880
   299
    /// \sa MemoryMap
hegyi@677
   300
    /// \todo We may need copy constructor
hegyi@677
   301
    /// \todo We may need conversion from other nodetype
hegyi@677
   302
    /// \todo We may need operator=
hegyi@677
   303
    /// \warning Making maps that can handle bool type (NodeMap<bool>)
hegyi@677
   304
    /// needs extra attention!
hegyi@677
   305
hegyi@677
   306
    template<class T> class NodeMap
hegyi@677
   307
    {
hegyi@677
   308
    public:
alpar@987
   309
      typedef T Value;
alpar@987
   310
      typedef Node Key;
hegyi@677
   311
hegyi@677
   312
      NodeMap(const EdgePathGraph &) {}
hegyi@677
   313
      NodeMap(const EdgePathGraph &, T) {}
hegyi@677
   314
hegyi@677
   315
      template<typename TT> NodeMap(const NodeMap<TT> &) {}
hegyi@677
   316
hegyi@677
   317
      /// Sets the value of a node.
hegyi@677
   318
hegyi@677
   319
      /// Sets the value associated with node \c i to the value \c t.
hegyi@677
   320
      ///
hegyi@677
   321
      void set(Node, T) {}
hegyi@677
   322
      // Gets the value of a node.
hegyi@677
   323
      //T get(Node i) const {return *(T*)0;}  //FIXME: Is it necessary?
hegyi@677
   324
      T &operator[](Node) {return *(T*)0;}
hegyi@677
   325
      const T &operator[](Node) const {return *(T*)0;}
hegyi@677
   326
hegyi@677
   327
      /// Updates the map if the graph has been changed
hegyi@677
   328
hegyi@677
   329
      /// \todo Do we need this?
hegyi@677
   330
      ///
hegyi@677
   331
      void update() {}
hegyi@677
   332
      void update(T a) {}   //FIXME: Is it necessary
hegyi@677
   333
    };
hegyi@677
   334
hegyi@677
   335
    ///Read/write/reference map of the edges to type \c T.
hegyi@677
   336
hegyi@677
   337
    ///Read/write/reference map of the edges to type \c T.
hegyi@677
   338
    ///It behaves exactly in the same way as \ref NodeMap.
hegyi@677
   339
    /// \sa NodeMap
alpar@880
   340
    /// \sa MemoryMap
hegyi@677
   341
    /// \todo We may need copy constructor
hegyi@677
   342
    /// \todo We may need conversion from other edgetype
hegyi@677
   343
    /// \todo We may need operator=
hegyi@677
   344
    template<class T> class EdgeMap
hegyi@677
   345
    {
hegyi@677
   346
    public:
alpar@987
   347
      typedef T Value;
alpar@987
   348
      typedef Edge Key;
hegyi@677
   349
hegyi@677
   350
      EdgeMap(const EdgePathGraph &) {}
hegyi@677
   351
      EdgeMap(const EdgePathGraph &, T ) {}
hegyi@677
   352
    
hegyi@677
   353
      ///\todo It can copy between different types.
hegyi@677
   354
      ///
hegyi@677
   355
      template<typename TT> EdgeMap(const EdgeMap<TT> &) {}
hegyi@677
   356
hegyi@677
   357
      void set(Edge, T) {}
hegyi@677
   358
      //T get(Edge) const {return *(T*)0;}
hegyi@677
   359
      T &operator[](Edge) {return *(T*)0;}
hegyi@677
   360
      const T &operator[](Edge) const {return *(T*)0;}
hegyi@677
   361
    
hegyi@677
   362
      void update() {}
hegyi@677
   363
      void update(T a) {}   //FIXME: Is it necessary
hegyi@677
   364
    };
hegyi@677
   365
  };
hegyi@677
   366
alpar@826
   367
  /// An empty erasable graph class.
hegyi@677
   368
  
alpar@826
   369
  /// This class provides all the common features of an \e erasable graph
hegyi@677
   370
  /// structure,
hegyi@677
   371
  /// however completely without implementations and real data structures
hegyi@677
   372
  /// behind the interface.
hegyi@677
   373
  /// All graph algorithms should compile with this class, but it will not
hegyi@677
   374
  /// run properly, of course.
hegyi@677
   375
  ///
hegyi@677
   376
  /// \todo This blabla could be replaced by a sepatate description about
alpar@880
   377
  /// s.
hegyi@677
   378
  ///
hegyi@677
   379
  /// It can be used for checking the interface compatibility,
hegyi@677
   380
  /// or it can serve as a skeleton of a new graph structure.
hegyi@677
   381
  /// 
hegyi@677
   382
  /// Also, you will find here the full documentation of a certain graph
hegyi@677
   383
  /// feature, the documentation of a real graph imlementation
hegyi@677
   384
  /// like @ref ListGraph or
hegyi@677
   385
  /// @ref SmartGraph will just refer to this structure.
hegyi@677
   386
  template <typename P, typename Gact, typename Gsub>
alpar@826
   387
  class ErasableEdgePathGraph : public EdgePathGraph<P, Gact, Gsub>
hegyi@677
   388
  {
hegyi@677
   389
  public:
hegyi@677
   390
    /// Deletes a node.
hegyi@677
   391
    void erase(typename Gact::Node n) {actuallayer.erase(n);}
hegyi@677
   392
    /// Deletes an edge.
hegyi@677
   393
    void erase(typename Gact::Edge e) {actuallayer.erase(e);}
hegyi@677
   394
hegyi@677
   395
    /// Defalult constructor.
alpar@826
   396
    ErasableEdgePathGraph() {}
hegyi@677
   397
    ///Copy consructor.
alpar@826
   398
    ErasableEdgePathGraph(const EdgePathGraph<P, Gact, Gsub> &EPG) {}
hegyi@677
   399
  };
hegyi@677
   400
hegyi@677
   401
  
hegyi@677
   402
  // @}
hegyi@677
   403
alpar@921
   404
} //namespace lemon
hegyi@677
   405
hegyi@677
   406
alpar@921
   407
#endif // LEMON_SKELETON_GRAPH_H