src/hugo/skeletons/graph.h
author alpar
Sun, 19 Sep 2004 12:24:48 +0000
changeset 881 a9f19f38970b
parent 880 9d0bfd35b97c
child 906 17f31d280385
permissions -rw-r--r--
Right (but still too short) documentation of the namespaces.
marci@174
     1
// -*- c++ -*-
alpar@503
     2
#ifndef HUGO_SKELETON_GRAPH_H
alpar@503
     3
#define HUGO_SKELETON_GRAPH_H
alpar@52
     4
alpar@794
     5
///\ingroup skeletons
alpar@242
     6
///\file
alpar@880
     7
///\brief Declaration of Graph.
alpar@242
     8
ladanyi@542
     9
#include <hugo/invalid.h>
alpar@732
    10
#include <hugo/skeletons/maps.h>
alpar@145
    11
alpar@163
    12
namespace hugo {
alpar@732
    13
  namespace skeleton {
alpar@732
    14
    
alpar@794
    15
    /// \addtogroup skeletons
alpar@794
    16
    /// @{
alpar@163
    17
alpar@732
    18
    /// An empty static graph class.
alpar@732
    19
  
alpar@732
    20
    /// This class provides all the common features of a graph structure,
alpar@732
    21
    /// however completely without implementations and real data structures
alpar@732
    22
    /// behind the interface.
alpar@732
    23
    /// All graph algorithms should compile with this class, but it will not
alpar@732
    24
    /// run properly, of course.
alpar@732
    25
    ///
alpar@732
    26
    /// It can be used for checking the interface compatibility,
alpar@732
    27
    /// or it can serve as a skeleton of a new graph structure.
alpar@732
    28
    /// 
alpar@732
    29
    /// Also, you will find here the full documentation of a certain graph
alpar@732
    30
    /// feature, the documentation of a real graph imlementation
alpar@732
    31
    /// like @ref ListGraph or
alpar@732
    32
    /// @ref SmartGraph will just refer to this structure.
alpar@880
    33
    class StaticGraph
alpar@732
    34
    {
alpar@732
    35
    public:
alpar@732
    36
      /// Defalult constructor.
alpar@801
    37
alpar@801
    38
      /// Defalult constructor.
alpar@801
    39
      ///
alpar@880
    40
      StaticGraph() { }
alpar@732
    41
      ///Copy consructor.
alpar@163
    42
alpar@801
    43
//       ///\todo It is not clear, what we expect from a copy constructor.
alpar@801
    44
//       ///E.g. How to assign the nodes/edges to each other? What about maps?
alpar@880
    45
//       StaticGraph(const StaticGraph& g) { }
alpar@732
    46
alpar@774
    47
      /// The base type of node iterators, 
alpar@774
    48
      /// or in other words, the trivial node iterator.
alpar@732
    49
alpar@774
    50
      /// This is the base type of each node iterator,
alpar@774
    51
      /// thus each kind of node iterator converts to this.
alpar@801
    52
      /// More precisely each kind of node iterator should be inherited 
alpar@774
    53
      /// from the trivial node iterator.
alpar@732
    54
      class Node {
alpar@732
    55
      public:
alpar@801
    56
	/// Default constructor
alpar@801
    57
alpar@732
    58
	/// @warning The default constructor sets the iterator
alpar@732
    59
	/// to an undefined value.
alpar@774
    60
	Node() { }
alpar@774
    61
	/// Copy constructor.
alpar@801
    62
alpar@801
    63
	/// Copy constructor.
alpar@801
    64
	///
alpar@774
    65
	Node(const Node&) { }
alpar@801
    66
alpar@732
    67
	/// Invalid constructor \& conversion.
alpar@732
    68
alpar@732
    69
	/// This constructor initializes the iterator to be invalid.
alpar@732
    70
	/// \sa Invalid for more details.
alpar@774
    71
	Node(Invalid) { }
alpar@801
    72
	/// Equality operator
alpar@801
    73
alpar@732
    74
	/// Two iterators are equal if and only if they point to the
alpar@732
    75
	/// same object or both are invalid.
alpar@732
    76
	bool operator==(Node) const { return true; }
alpar@732
    77
alpar@801
    78
	/// Inequality operator
alpar@801
    79
	
alpar@732
    80
	/// \sa \ref operator==(Node n)
alpar@732
    81
	///
alpar@732
    82
	bool operator!=(Node) const { return true; }
alpar@732
    83
alpar@801
    84
 	///Comparison operator.
alpar@801
    85
alpar@801
    86
	///This is a strict ordering between the nodes.
alpar@801
    87
	///
alpar@801
    88
	///This ordering can be different from the order in which NodeIt
alpar@801
    89
	///goes through the nodes.
alpar@801
    90
	///\todo Possibly we don't need it.
alpar@732
    91
	bool operator<(Node) const { return true; }
alpar@732
    92
      };
alpar@732
    93
    
alpar@732
    94
      /// This iterator goes through each node.
alpar@732
    95
alpar@732
    96
      /// This iterator goes through each node.
alpar@732
    97
      /// Its usage is quite simple, for example you can count the number
alpar@774
    98
      /// of nodes in graph \c g of type \c Graph like this:
alpar@732
    99
      /// \code
alpar@774
   100
      /// int count=0;
alpar@801
   101
      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
alpar@732
   102
      /// \endcode
alpar@732
   103
      class NodeIt : public Node {
alpar@732
   104
      public:
alpar@801
   105
	/// Default constructor
alpar@801
   106
alpar@732
   107
	/// @warning The default constructor sets the iterator
alpar@732
   108
	/// to an undefined value.
alpar@774
   109
	NodeIt() { }
alpar@774
   110
	/// Copy constructor.
alpar@801
   111
	
alpar@801
   112
	/// Copy constructor.
alpar@801
   113
	///
alpar@774
   114
	NodeIt(const NodeIt&) { }
alpar@732
   115
	/// Invalid constructor \& conversion.
alpar@732
   116
alpar@774
   117
	/// Initialize the iterator to be invalid.
alpar@732
   118
	/// \sa Invalid for more details.
alpar@774
   119
	NodeIt(Invalid) { }
alpar@801
   120
	/// Sets the iterator to the first node.
alpar@801
   121
alpar@774
   122
	/// Sets the iterator to the first node of \c g.
alpar@801
   123
	///
alpar@880
   124
	NodeIt(const StaticGraph& g) { }
alpar@801
   125
	/// Node -> NodeIt conversion.
alpar@801
   126
alpar@774
   127
	/// Sets the iterator to the node of \c g pointed by the trivial 
alpar@801
   128
	/// iterator n.
alpar@801
   129
	/// This feature necessitates that each time we 
alpar@801
   130
	/// iterate the edge-set, the iteration order is the same.
alpar@880
   131
	NodeIt(const StaticGraph& g, const Node& n) { }
alpar@801
   132
	/// Next node.
alpar@801
   133
alpar@774
   134
	/// Assign the iterator to the next node.
alpar@801
   135
	///
alpar@774
   136
	NodeIt& operator++() { return *this; }
alpar@732
   137
      };
alpar@732
   138
    
alpar@732
   139
    
alpar@732
   140
      /// The base type of the edge iterators.
alpar@801
   141
alpar@801
   142
      /// The base type of the edge iterators.
alpar@801
   143
      ///
alpar@732
   144
      class Edge {
alpar@732
   145
      public:
alpar@801
   146
	/// Default constructor
alpar@801
   147
alpar@732
   148
	/// @warning The default constructor sets the iterator
alpar@732
   149
	/// to an undefined value.
alpar@774
   150
	Edge() { }
alpar@774
   151
	/// Copy constructor.
alpar@801
   152
alpar@801
   153
	/// Copy constructor.
alpar@801
   154
	///
alpar@774
   155
	Edge(const Edge&) { }
alpar@774
   156
	/// Initialize the iterator to be invalid.
alpar@801
   157
alpar@801
   158
	/// Initialize the iterator to be invalid.
alpar@801
   159
	///
alpar@774
   160
	Edge(Invalid) { }
alpar@801
   161
	/// Equality operator
alpar@801
   162
alpar@732
   163
	/// Two iterators are equal if and only if they point to the
alpar@732
   164
	/// same object or both are invalid.
alpar@732
   165
	bool operator==(Edge) const { return true; }
alpar@801
   166
	/// Inequality operator
alpar@801
   167
alpar@801
   168
	/// \sa \ref operator==(Node n)
alpar@801
   169
	///
alpar@732
   170
	bool operator!=(Edge) const { return true; }
alpar@801
   171
 	///Comparison operator.
alpar@801
   172
alpar@801
   173
	///This is a strict ordering between the nodes.
alpar@801
   174
	///
alpar@801
   175
	///This ordering can be different from the order in which NodeIt
alpar@801
   176
	///goes through the nodes.
alpar@801
   177
	///\todo Possibly we don't need it.
alpar@801
   178
 	bool operator<(Edge) const { return true; }
alpar@732
   179
      };
alpar@732
   180
    
alpar@732
   181
      /// This iterator goes trough the outgoing edges of a node.
alpar@732
   182
alpar@732
   183
      /// This iterator goes trough the \e outgoing edges of a certain node
alpar@732
   184
      /// of a graph.
alpar@732
   185
      /// Its usage is quite simple, for example you can count the number
alpar@732
   186
      /// of outgoing edges of a node \c n
alpar@774
   187
      /// in graph \c g of type \c Graph as follows.
alpar@732
   188
      /// \code
alpar@774
   189
      /// int count=0;
alpar@801
   190
      /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
alpar@732
   191
      /// \endcode
alpar@732
   192
    
alpar@732
   193
      class OutEdgeIt : public Edge {
alpar@732
   194
      public:
alpar@801
   195
	/// Default constructor
alpar@801
   196
alpar@732
   197
	/// @warning The default constructor sets the iterator
alpar@732
   198
	/// to an undefined value.
alpar@774
   199
	OutEdgeIt() { }
alpar@774
   200
	/// Copy constructor.
alpar@801
   201
alpar@801
   202
	/// Copy constructor.
alpar@801
   203
	///
alpar@774
   204
	OutEdgeIt(const OutEdgeIt&) { }
alpar@774
   205
	/// Initialize the iterator to be invalid.
alpar@801
   206
alpar@801
   207
	/// Initialize the iterator to be invalid.
alpar@801
   208
	///
alpar@774
   209
	OutEdgeIt(Invalid) { }
alpar@732
   210
	/// This constructor sets the iterator to first outgoing edge.
alpar@732
   211
    
alpar@732
   212
	/// This constructor set the iterator to the first outgoing edge of
alpar@732
   213
	/// node
alpar@732
   214
	///@param n the node
alpar@774
   215
	///@param g the graph
alpar@880
   216
	OutEdgeIt(const StaticGraph& g, const Node& n) { }
alpar@801
   217
	/// Edge -> OutEdgeIt conversion
alpar@801
   218
alpar@774
   219
	/// Sets the iterator to the value of the trivial iterator \c e.
alpar@774
   220
	/// This feature necessitates that each time we 
alpar@774
   221
	/// iterate the edge-set, the iteration order is the same.
alpar@880
   222
	OutEdgeIt(const StaticGraph& g, const Edge& e) { }
alpar@801
   223
	///Next outgoing edge
alpar@801
   224
	
alpar@801
   225
	/// Assign the iterator to the next 
alpar@801
   226
	/// outgoing edge of the corresponding node.
alpar@774
   227
	OutEdgeIt& operator++() { return *this; }
alpar@732
   228
      };
alpar@732
   229
alpar@732
   230
      /// This iterator goes trough the incoming edges of a node.
alpar@732
   231
alpar@732
   232
      /// This iterator goes trough the \e incoming edges of a certain node
alpar@732
   233
      /// of a graph.
alpar@732
   234
      /// Its usage is quite simple, for example you can count the number
alpar@732
   235
      /// of outgoing edges of a node \c n
alpar@774
   236
      /// in graph \c g of type \c Graph as follows.
alpar@732
   237
      /// \code
alpar@774
   238
      /// int count=0;
alpar@801
   239
      /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
alpar@732
   240
      /// \endcode
alpar@732
   241
alpar@732
   242
      class InEdgeIt : public Edge {
alpar@732
   243
      public:
alpar@801
   244
	/// Default constructor
alpar@801
   245
alpar@732
   246
	/// @warning The default constructor sets the iterator
alpar@732
   247
	/// to an undefined value.
alpar@774
   248
	InEdgeIt() { }
alpar@774
   249
	/// Copy constructor.
alpar@801
   250
alpar@801
   251
	/// Copy constructor.
alpar@801
   252
	///
alpar@774
   253
	InEdgeIt(const InEdgeIt&) { }
alpar@774
   254
	/// Initialize the iterator to be invalid.
alpar@801
   255
alpar@801
   256
	/// Initialize the iterator to be invalid.
alpar@801
   257
	///
alpar@774
   258
	InEdgeIt(Invalid) { }
alpar@801
   259
	/// This constructor sets the iterator to first incoming edge.
alpar@801
   260
    
alpar@801
   261
	/// This constructor set the iterator to the first incoming edge of
alpar@801
   262
	/// node
alpar@801
   263
	///@param n the node
alpar@801
   264
	///@param g the graph
alpar@880
   265
	InEdgeIt(const StaticGraph& g, const Node& n) { }
alpar@801
   266
	/// Edge -> InEdgeIt conversion
alpar@801
   267
alpar@801
   268
	/// Sets the iterator to the value of the trivial iterator \c e.
alpar@801
   269
	/// This feature necessitates that each time we 
alpar@801
   270
	/// iterate the edge-set, the iteration order is the same.
alpar@880
   271
	InEdgeIt(const StaticGraph& g, const Edge& n) { }
alpar@801
   272
	/// Next incoming edge
alpar@801
   273
alpar@774
   274
	/// Assign the iterator to the next inedge of the corresponding node.
alpar@801
   275
	///
alpar@774
   276
	InEdgeIt& operator++() { return *this; }
alpar@732
   277
      };
alpar@732
   278
      /// This iterator goes through each edge.
alpar@732
   279
alpar@732
   280
      /// This iterator goes through each edge of a graph.
alpar@732
   281
      /// Its usage is quite simple, for example you can count the number
alpar@774
   282
      /// of edges in a graph \c g of type \c Graph as follows:
alpar@732
   283
      /// \code
alpar@774
   284
      /// int count=0;
alpar@801
   285
      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
alpar@732
   286
      /// \endcode
alpar@732
   287
      class EdgeIt : public Edge {
alpar@732
   288
      public:
alpar@801
   289
	/// Default constructor
alpar@801
   290
alpar@732
   291
	/// @warning The default constructor sets the iterator
alpar@732
   292
	/// to an undefined value.
alpar@774
   293
	EdgeIt() { }
alpar@774
   294
	/// Copy constructor.
alpar@801
   295
alpar@801
   296
	/// Copy constructor.
alpar@801
   297
	///
alpar@774
   298
	EdgeIt(const EdgeIt&) { }
alpar@774
   299
	/// Initialize the iterator to be invalid.
alpar@801
   300
alpar@801
   301
	/// Initialize the iterator to be invalid.
alpar@801
   302
	///
alpar@774
   303
	EdgeIt(Invalid) { }
alpar@801
   304
	/// This constructor sets the iterator to first edge.
alpar@801
   305
    
alpar@801
   306
	/// This constructor set the iterator to the first edge of
alpar@801
   307
	/// node
alpar@801
   308
	///@param g the graph
alpar@880
   309
	EdgeIt(const StaticGraph& g) { }
alpar@801
   310
	/// Edge -> EdgeIt conversion
alpar@801
   311
alpar@801
   312
	/// Sets the iterator to the value of the trivial iterator \c e.
alpar@801
   313
	/// This feature necessitates that each time we 
alpar@801
   314
	/// iterate the edge-set, the iteration order is the same.
alpar@880
   315
	EdgeIt(const StaticGraph&, const Edge&) { } 
alpar@801
   316
    	///Next edge
alpar@801
   317
	
alpar@801
   318
	/// Assign the iterator to the next 
alpar@801
   319
	/// edge of the corresponding node.
alpar@774
   320
	EdgeIt& operator++() { return *this; }
alpar@732
   321
      };
alpar@732
   322
alpar@732
   323
      /// First node of the graph.
alpar@732
   324
alpar@732
   325
      /// \retval i the first node.
alpar@732
   326
      /// \return the first node.
alpar@732
   327
      ///
alpar@774
   328
      NodeIt& first(NodeIt& i) const { return i; }
alpar@732
   329
alpar@732
   330
      /// The first incoming edge.
alpar@801
   331
alpar@801
   332
      /// The first incoming edge.
alpar@801
   333
      ///
alpar@774
   334
      InEdgeIt& first(InEdgeIt &i, Node) const { return i; }
alpar@732
   335
      /// The first outgoing edge.
alpar@801
   336
alpar@801
   337
      /// The first outgoing edge.
alpar@801
   338
      ///
alpar@774
   339
      OutEdgeIt& first(OutEdgeIt& i, Node) const { return i; }
alpar@732
   340
      /// The first edge of the Graph.
alpar@801
   341
alpar@801
   342
      /// The first edge of the Graph.
alpar@801
   343
      ///
alpar@774
   344
      EdgeIt& first(EdgeIt& i) const { return i; }
alpar@732
   345
alpar@801
   346
      ///Gives back the head node of an edge.
alpar@732
   347
alpar@732
   348
      ///Gives back the head node of an edge.
alpar@801
   349
      ///
alpar@732
   350
      Node head(Edge) const { return INVALID; }
alpar@732
   351
      ///Gives back the tail node of an edge.
alpar@801
   352
alpar@801
   353
      ///Gives back the tail node of an edge.
alpar@801
   354
      ///
alpar@732
   355
      Node tail(Edge) const { return INVALID; }
alpar@163
   356
  
alpar@732
   357
      ///Gives back the \e id of a node.
alpar@182
   358
alpar@732
   359
      ///\warning Not all graph structures provide this feature.
alpar@732
   360
      ///
alpar@801
   361
      ///\todo Should each graph provide \c id?
alpar@774
   362
      int id(const Node&) const { return 0; }
alpar@732
   363
      ///Gives back the \e id of an edge.
alpar@182
   364
alpar@732
   365
      ///\warning Not all graph structures provide this feature.
alpar@182
   366
      ///
alpar@801
   367
      ///\todo Should each graph provide \c id?
alpar@774
   368
      int id(const Edge&) const { return 0; }
alpar@182
   369
alpar@801
   370
      /// .
alpar@801
   371
      
alpar@880
   372
      ///\todo Should it be in the concept?
alpar@801
   373
      ///
alpar@774
   374
      int nodeNum() const { return 0; }
alpar@801
   375
      /// .
alpar@880
   376
alpar@880
   377
      ///\todo Should it be in the concept?
alpar@801
   378
      ///
alpar@774
   379
      int edgeNum() const { return 0; }
alpar@732
   380
alpar@732
   381
alpar@732
   382
      ///Reference map of the nodes to type \c T.
alpar@732
   383
alpar@880
   384
      /// \ingroup skeletons
alpar@732
   385
      ///Reference map of the nodes to type \c T.
alpar@880
   386
      /// \sa Reference
alpar@732
   387
      /// \warning Making maps that can handle bool type (NodeMap<bool>)
alpar@801
   388
      /// needs some extra attention!
alpar@880
   389
      template<class T> class NodeMap : public ReferenceMap< Node, T >
alpar@732
   390
      {
alpar@732
   391
      public:
alpar@732
   392
alpar@801
   393
	/// .
alpar@880
   394
	NodeMap(const StaticGraph&) { }
alpar@801
   395
	/// .
alpar@880
   396
	NodeMap(const StaticGraph&, T) { }
alpar@732
   397
alpar@732
   398
	///Copy constructor
alpar@774
   399
	template<typename TT> NodeMap(const NodeMap<TT>&) { }
alpar@732
   400
	///Assignment operator
alpar@774
   401
	template<typename TT> NodeMap& operator=(const NodeMap<TT>&)
alpar@774
   402
	{ return *this; }
alpar@732
   403
      };
alpar@732
   404
alpar@732
   405
      ///Reference map of the edges to type \c T.
alpar@732
   406
alpar@880
   407
      /// \ingroup skeletons
alpar@732
   408
      ///Reference map of the edges to type \c T.
alpar@880
   409
      /// \sa Reference
alpar@732
   410
      /// \warning Making maps that can handle bool type (EdgeMap<bool>)
alpar@801
   411
      /// needs some extra attention!
alpar@732
   412
      template<class T> class EdgeMap
alpar@732
   413
	: public ReferenceMap<Edge,T>
alpar@732
   414
      {
alpar@732
   415
      public:
alpar@732
   416
alpar@801
   417
	/// .
alpar@880
   418
	EdgeMap(const StaticGraph&) { }
alpar@801
   419
	/// .
alpar@880
   420
	EdgeMap(const StaticGraph&, T) { }
alpar@147
   421
    
alpar@732
   422
	///Copy constructor
alpar@774
   423
	template<typename TT> EdgeMap(const EdgeMap<TT>&) { }
alpar@732
   424
	///Assignment operator
alpar@774
   425
	template<typename TT> EdgeMap &operator=(const EdgeMap<TT>&)
alpar@774
   426
	{ return *this; }
alpar@732
   427
      };
alpar@163
   428
    };
alpar@163
   429
alpar@186
   430
alpar@732
   431
  
alpar@801
   432
    /// An empty non-static graph class.
alpar@186
   433
alpar@880
   434
    /// This class provides everything that \ref StaticGraph
alpar@732
   435
    /// with additional functionality which enables to build a
alpar@732
   436
    /// graph from scratch.
alpar@880
   437
    class ExtendableGraph : public StaticGraph
alpar@732
   438
    {
alpar@163
   439
    public:
alpar@732
   440
      /// Defalult constructor.
alpar@801
   441
alpar@801
   442
      /// Defalult constructor.
alpar@801
   443
      ///
alpar@880
   444
      ExtendableGraph() { }
alpar@732
   445
      ///Add a new node to the graph.
alpar@732
   446
alpar@732
   447
      /// \return the new node.
alpar@732
   448
      ///
alpar@774
   449
      Node addNode() { return INVALID; }
alpar@732
   450
      ///Add a new edge to the graph.
alpar@732
   451
alpar@880
   452
      ///Add a new edge to the graph with tail node \c t
alpar@880
   453
      ///and head node \c h.
alpar@732
   454
      ///\return the new edge.
alpar@880
   455
      Edge addEdge(Node h, Node t) { return INVALID; }
alpar@732
   456
    
alpar@732
   457
      /// Resets the graph.
alpar@732
   458
alpar@732
   459
      /// This function deletes all edges and nodes of the graph.
alpar@732
   460
      /// It also frees the memory allocated to store them.
alpar@880
   461
      /// \todo It might belong to \ref ErasableGraph.
alpar@774
   462
      void clear() { }
alpar@163
   463
    };
alpar@163
   464
alpar@826
   465
    /// An empty erasable graph class.
alpar@52
   466
  
alpar@880
   467
    /// This class is an extension of \ref ExtendableGraph. It also makes it
alpar@732
   468
    /// possible to erase edges or nodes.
alpar@880
   469
    class ErasableGraph : public ExtendableGraph
alpar@163
   470
    {
alpar@163
   471
    public:
alpar@801
   472
      /// Defalult constructor.
alpar@801
   473
alpar@801
   474
      /// Defalult constructor.
alpar@801
   475
      ///
alpar@880
   476
      ErasableGraph() { }
alpar@732
   477
      /// Deletes a node.
alpar@801
   478
alpar@801
   479
      /// Deletes node \c n node.
alpar@801
   480
      ///
alpar@774
   481
      void erase(Node n) { }
alpar@732
   482
      /// Deletes an edge.
alpar@801
   483
alpar@801
   484
      /// Deletes edge \c e edge.
alpar@801
   485
      ///
alpar@774
   486
      void erase(Edge e) { }
alpar@163
   487
    };
alpar@163
   488
alpar@732
   489
    // @}
alpar@801
   490
  } //namespace skeleton  
marci@174
   491
} //namespace hugo
alpar@52
   492
alpar@145
   493
alpar@145
   494
alpar@503
   495
#endif // HUGO_SKELETON_GRAPH_H