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