src/hugo/skeletons/graph.h
author deba
Fri, 03 Sep 2004 15:32:03 +0000
changeset 799 3393abe30678
parent 774 4297098d9677
child 801 48638058e188
permissions -rw-r--r--
(none)
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@774
    38
      StaticGraphSkeleton() { }
alpar@732
    39
      ///Copy consructor.
alpar@163
    40
alpar@732
    41
      ///\todo It is not clear, what we expect from a copy constructor.
alpar@732
    42
      ///E.g. How to assign the nodes/edges to each other? What about maps?
alpar@774
    43
      StaticGraphSkeleton(const StaticGraphSkeleton& g) { }
alpar@732
    44
alpar@774
    45
      /// The base type of node iterators, 
alpar@774
    46
      /// or in other words, the trivial node iterator.
alpar@732
    47
alpar@774
    48
      /// This is the base type of each node iterator,
alpar@774
    49
      /// thus each kind of node iterator converts to this.
alpar@774
    50
      /// More precisely each kind of node iterator have to be inherited 
alpar@774
    51
      /// from the trivial node iterator.
alpar@732
    52
      class Node {
alpar@732
    53
      public:
alpar@732
    54
	/// @warning The default constructor sets the iterator
alpar@732
    55
	/// to an undefined value.
alpar@774
    56
	Node() { }
alpar@774
    57
	/// Copy constructor.
alpar@774
    58
	Node(const Node&) { }
alpar@732
    59
	/// Invalid constructor \& conversion.
alpar@732
    60
alpar@732
    61
	/// This constructor initializes the iterator to be invalid.
alpar@732
    62
	/// \sa Invalid for more details.
alpar@774
    63
	Node(Invalid) { }
alpar@732
    64
	/// Two iterators are equal if and only if they point to the
alpar@732
    65
	/// same object or both are invalid.
alpar@732
    66
	bool operator==(Node) const { return true; }
alpar@732
    67
alpar@732
    68
	/// \sa \ref operator==(Node n)
alpar@732
    69
	///
alpar@732
    70
	bool operator!=(Node) const { return true; }
alpar@732
    71
alpar@732
    72
	bool operator<(Node) const { return true; }
alpar@732
    73
      };
alpar@732
    74
    
alpar@732
    75
      /// This iterator goes through each node.
alpar@732
    76
alpar@732
    77
      /// This iterator goes through each node.
alpar@732
    78
      /// Its usage is quite simple, for example you can count the number
alpar@774
    79
      /// of nodes in graph \c g of type \c Graph like this:
alpar@732
    80
      /// \code
alpar@774
    81
      /// int count=0;
alpar@774
    82
      /// for (Graph::NodeIt n(g); g.valid(n); ++n) ++count;
alpar@732
    83
      /// \endcode
alpar@732
    84
      class NodeIt : public Node {
alpar@732
    85
      public:
alpar@732
    86
	/// @warning The default constructor sets the iterator
alpar@732
    87
	/// to an undefined value.
alpar@774
    88
	NodeIt() { }
alpar@774
    89
	/// Copy constructor.
alpar@774
    90
	NodeIt(const NodeIt&) { }
alpar@732
    91
	/// Invalid constructor \& conversion.
alpar@732
    92
alpar@774
    93
	/// Initialize the iterator to be invalid.
alpar@732
    94
	/// \sa Invalid for more details.
alpar@774
    95
	NodeIt(Invalid) { }
alpar@774
    96
	/// Sets the iterator to the first node of \c g.
alpar@774
    97
	NodeIt(const StaticGraphSkeleton& g) { }
alpar@774
    98
	/// Sets the iterator to the node of \c g pointed by the trivial 
alpar@774
    99
	/// iterator n. This feature necessitates that each time we 
alpar@774
   100
	/// iterate the node-set, the iteration order is the same.
alpar@774
   101
	NodeIt(const StaticGraphSkeleton& g, const Node& n) { }
alpar@774
   102
	/// Assign the iterator to the next node.
alpar@774
   103
	NodeIt& operator++() { return *this; }
alpar@732
   104
      };
alpar@732
   105
    
alpar@732
   106
    
alpar@732
   107
      /// The base type of the edge iterators.
alpar@732
   108
      class Edge {
alpar@732
   109
      public:
alpar@732
   110
	/// @warning The default constructor sets the iterator
alpar@732
   111
	/// to an undefined value.
alpar@774
   112
	Edge() { }
alpar@774
   113
	/// Copy constructor.
alpar@774
   114
	Edge(const Edge&) { }
alpar@774
   115
	/// Initialize the iterator to be invalid.
alpar@774
   116
	Edge(Invalid) { }
alpar@732
   117
	/// Two iterators are equal if and only if they point to the
alpar@732
   118
	/// same object or both are invalid.
alpar@732
   119
	bool operator==(Edge) const { return true; }
alpar@732
   120
	bool operator!=(Edge) const { return true; }
alpar@732
   121
	bool operator<(Edge) const { return true; }
alpar@732
   122
      };
alpar@732
   123
    
alpar@732
   124
      /// This iterator goes trough the outgoing edges of a node.
alpar@732
   125
alpar@732
   126
      /// This iterator goes trough the \e outgoing edges of a certain node
alpar@732
   127
      /// of a graph.
alpar@732
   128
      /// Its usage is quite simple, for example you can count the number
alpar@732
   129
      /// of outgoing edges of a node \c n
alpar@774
   130
      /// in graph \c g of type \c Graph as follows.
alpar@732
   131
      /// \code
alpar@774
   132
      /// int count=0;
alpar@774
   133
      /// for (Graph::OutEdgeIt e(g, n); g.valid(e); ++e) ++count;
alpar@732
   134
      /// \endcode
alpar@732
   135
    
alpar@732
   136
      class OutEdgeIt : public Edge {
alpar@732
   137
      public:
alpar@732
   138
	/// @warning The default constructor sets the iterator
alpar@732
   139
	/// to an undefined value.
alpar@774
   140
	OutEdgeIt() { }
alpar@774
   141
	/// Copy constructor.
alpar@774
   142
	OutEdgeIt(const OutEdgeIt&) { }
alpar@774
   143
	/// Initialize the iterator to be invalid.
alpar@774
   144
	OutEdgeIt(Invalid) { }
alpar@732
   145
	/// This constructor sets the iterator to first outgoing edge.
alpar@732
   146
    
alpar@732
   147
	/// This constructor set the iterator to the first outgoing edge of
alpar@732
   148
	/// node
alpar@732
   149
	///@param n the node
alpar@774
   150
	///@param g the graph
alpar@774
   151
	OutEdgeIt(const StaticGraphSkeleton& g, const Node& n) { }
alpar@774
   152
	/// Sets the iterator to the value of the trivial iterator \c e.
alpar@774
   153
	/// This feature necessitates that each time we 
alpar@774
   154
	/// iterate the edge-set, the iteration order is the same.
alpar@774
   155
	OutEdgeIt(const StaticGraphSkeleton& g, const Edge& e) { }
alpar@774
   156
	/// Assign the iterator to the next outedge of the corresponding node.
alpar@774
   157
	OutEdgeIt& operator++() { return *this; }
alpar@732
   158
      };
alpar@732
   159
alpar@732
   160
      /// This iterator goes trough the incoming edges of a node.
alpar@732
   161
alpar@732
   162
      /// This iterator goes trough the \e incoming edges of a certain node
alpar@732
   163
      /// of a graph.
alpar@732
   164
      /// Its usage is quite simple, for example you can count the number
alpar@732
   165
      /// of outgoing edges of a node \c n
alpar@774
   166
      /// in graph \c g of type \c Graph as follows.
alpar@732
   167
      /// \code
alpar@774
   168
      /// int count=0;
alpar@774
   169
      /// for(Graph::InEdgeIt e(g, n); g.valid(e); ++) ++count;
alpar@732
   170
      /// \endcode
alpar@732
   171
alpar@732
   172
      class InEdgeIt : public Edge {
alpar@732
   173
      public:
alpar@732
   174
	/// @warning The default constructor sets the iterator
alpar@732
   175
	/// to an undefined value.
alpar@774
   176
	InEdgeIt() { }
alpar@774
   177
	/// Copy constructor.
alpar@774
   178
	InEdgeIt(const InEdgeIt&) { }
alpar@774
   179
	/// Initialize the iterator to be invalid.
alpar@774
   180
	InEdgeIt(Invalid) { }
alpar@774
   181
	/// .
alpar@774
   182
	InEdgeIt(const StaticGraphSkeleton&, const Node&) { }
alpar@774
   183
	/// .
alpar@774
   184
	InEdgeIt(const StaticGraphSkeleton&, const Edge&) { }
alpar@774
   185
	/// Assign the iterator to the next inedge of the corresponding node.
alpar@774
   186
	InEdgeIt& operator++() { return *this; }
alpar@732
   187
      };
alpar@732
   188
      //  class SymEdgeIt : public Edge {};
alpar@732
   189
alpar@732
   190
      /// This iterator goes through each edge.
alpar@732
   191
alpar@732
   192
      /// This iterator goes through each edge of a graph.
alpar@732
   193
      /// Its usage is quite simple, for example you can count the number
alpar@774
   194
      /// of edges in a graph \c g of type \c Graph as follows:
alpar@732
   195
      /// \code
alpar@774
   196
      /// int count=0;
alpar@774
   197
      /// for(Graph::EdgeIt e(g); g.valid(e); ++e) ++count;
alpar@732
   198
      /// \endcode
alpar@732
   199
      class EdgeIt : public Edge {
alpar@732
   200
      public:
alpar@732
   201
	/// @warning The default constructor sets the iterator
alpar@732
   202
	/// to an undefined value.
alpar@774
   203
	EdgeIt() { }
alpar@774
   204
	/// Copy constructor.
alpar@774
   205
	EdgeIt(const EdgeIt&) { }
alpar@774
   206
	/// Initialize the iterator to be invalid.
alpar@774
   207
	EdgeIt(Invalid) { }
alpar@774
   208
	/// .
alpar@774
   209
	EdgeIt(const StaticGraphSkeleton&) { }
alpar@774
   210
	/// .
alpar@774
   211
	EdgeIt(const StaticGraphSkeleton&, const Edge&) { } 
alpar@774
   212
	EdgeIt& operator++() { return *this; }
alpar@732
   213
      };
alpar@732
   214
alpar@732
   215
      /// First node of the graph.
alpar@732
   216
alpar@732
   217
      /// \retval i the first node.
alpar@732
   218
      /// \return the first node.
alpar@732
   219
      ///
alpar@774
   220
      NodeIt& first(NodeIt& i) const { return i; }
alpar@732
   221
alpar@732
   222
      /// The first incoming edge.
alpar@774
   223
      InEdgeIt& first(InEdgeIt &i, Node) const { return i; }
alpar@732
   224
      /// The first outgoing edge.
alpar@774
   225
      OutEdgeIt& first(OutEdgeIt& i, Node) const { return i; }
alpar@774
   226
      //  SymEdgeIt& first(SymEdgeIt&, Node) const { return i; }
alpar@732
   227
      /// The first edge of the Graph.
alpar@774
   228
      EdgeIt& first(EdgeIt& i) const { return i; }
alpar@732
   229
alpar@732
   230
      //     Node getNext(Node) const {}
alpar@732
   231
      //     InEdgeIt getNext(InEdgeIt) const {}
alpar@732
   232
      //     OutEdgeIt getNext(OutEdgeIt) const {}
alpar@732
   233
      //     //SymEdgeIt getNext(SymEdgeIt) const {}
alpar@732
   234
      //     EdgeIt getNext(EdgeIt) const {}
alpar@732
   235
alpar@732
   236
      /// Go to the next node.
alpar@774
   237
      NodeIt& next(NodeIt& i) const { return i; }
alpar@732
   238
      /// Go to the next incoming edge.
alpar@774
   239
      InEdgeIt& next(InEdgeIt& i) const { return i; }
alpar@732
   240
      /// Go to the next outgoing edge.
alpar@774
   241
      OutEdgeIt& next(OutEdgeIt& i) const { return i; }
alpar@774
   242
      //SymEdgeIt& next(SymEdgeIt&) const { }
alpar@732
   243
      /// Go to the next edge.
alpar@774
   244
      EdgeIt& next(EdgeIt& i) const { return i; }
alpar@732
   245
alpar@732
   246
      ///Gives back the head node of an edge.
alpar@732
   247
      Node head(Edge) const { return INVALID; }
alpar@732
   248
      ///Gives back the tail node of an edge.
alpar@732
   249
      Node tail(Edge) const { return INVALID; }
alpar@163
   250
  
alpar@732
   251
      //   Node aNode(InEdgeIt) const {}
alpar@732
   252
      //   Node aNode(OutEdgeIt) const {}
alpar@732
   253
      //   Node aNode(SymEdgeIt) const {}
alpar@321
   254
alpar@732
   255
      //   Node bNode(InEdgeIt) const {}
alpar@732
   256
      //   Node bNode(OutEdgeIt) const {}
alpar@732
   257
      //   Node bNode(SymEdgeIt) const {}
marci@320
   258
alpar@732
   259
      /// Checks if a node iterator is valid
alpar@182
   260
alpar@732
   261
      ///\todo Maybe, it would be better if iterator converted to
alpar@732
   262
      ///bool directly, as Jacint prefers.
alpar@774
   263
      bool valid(const Node&) const { return true; }
alpar@732
   264
      /// Checks if an edge iterator is valid
alpar@182
   265
alpar@732
   266
      ///\todo Maybe, it would be better if iterator converted to
alpar@732
   267
      ///bool directly, as Jacint prefers.
alpar@774
   268
      bool valid(const Edge&) const { return true; }
alpar@182
   269
alpar@732
   270
      ///Gives back the \e id of a node.
alpar@182
   271
alpar@732
   272
      ///\warning Not all graph structures provide this feature.
alpar@732
   273
      ///
alpar@774
   274
      int id(const Node&) const { return 0; }
alpar@732
   275
      ///Gives back the \e id of an edge.
alpar@182
   276
alpar@732
   277
      ///\warning Not all graph structures provide this feature.
alpar@182
   278
      ///
alpar@774
   279
      int id(const Edge&) const { return 0; }
alpar@182
   280
alpar@732
   281
      /// Resets the graph.
alpar@732
   282
alpar@732
   283
      /// This function deletes all edges and nodes of the graph.
alpar@732
   284
      /// It also frees the memory allocated to store them.
alpar@774
   285
      void clear() { }
alpar@732
   286
alpar@774
   287
      int nodeNum() const { return 0; }
alpar@774
   288
      int edgeNum() const { return 0; }
alpar@732
   289
alpar@732
   290
alpar@732
   291
      ///Reference map of the nodes to type \c T.
alpar@732
   292
alpar@732
   293
      ///Reference map of the nodes to type \c T.
alpar@732
   294
      /// \sa ReferenceSkeleton
alpar@732
   295
      /// \warning Making maps that can handle bool type (NodeMap<bool>)
alpar@732
   296
      /// needs extra attention!
alpar@732
   297
alpar@732
   298
      template<class T> class NodeMap
alpar@732
   299
	: public ReferenceMap< Node, T >
alpar@732
   300
      {
alpar@732
   301
      public:
alpar@732
   302
alpar@774
   303
	NodeMap(const StaticGraphSkeleton&) { }
alpar@774
   304
	NodeMap(const StaticGraphSkeleton&, T) { }
alpar@732
   305
alpar@732
   306
	///Copy constructor
alpar@774
   307
	template<typename TT> NodeMap(const NodeMap<TT>&) { }
alpar@732
   308
	///Assignment operator
alpar@774
   309
	template<typename TT> NodeMap& operator=(const NodeMap<TT>&)
alpar@774
   310
	{ return *this; }
alpar@732
   311
      };
alpar@732
   312
alpar@732
   313
      ///Reference map of the edges to type \c T.
alpar@732
   314
alpar@732
   315
      ///Reference map of the edges to type \c T.
alpar@732
   316
      /// \sa ReferenceSkeleton
alpar@732
   317
      /// \warning Making maps that can handle bool type (EdgeMap<bool>)
alpar@732
   318
      /// needs extra attention!
alpar@732
   319
      template<class T> class EdgeMap
alpar@732
   320
	: public ReferenceMap<Edge,T>
alpar@732
   321
      {
alpar@732
   322
      public:
alpar@732
   323
	typedef T ValueType;
alpar@732
   324
	typedef Edge KeyType;
alpar@732
   325
alpar@774
   326
	EdgeMap(const StaticGraphSkeleton&) { }
alpar@774
   327
	EdgeMap(const StaticGraphSkeleton&, T) { }
alpar@147
   328
    
alpar@732
   329
	///Copy constructor
alpar@774
   330
	template<typename TT> EdgeMap(const EdgeMap<TT>&) { }
alpar@732
   331
	///Assignment operator
alpar@774
   332
	template<typename TT> EdgeMap &operator=(const EdgeMap<TT>&)
alpar@774
   333
	{ return *this; }
alpar@732
   334
      };
alpar@163
   335
    };
alpar@163
   336
alpar@186
   337
alpar@732
   338
  
alpar@732
   339
    /// An empty graph class.
alpar@186
   340
alpar@732
   341
    /// This class provides everything that \c StaticGraphSkeleton
alpar@732
   342
    /// with additional functionality which enables to build a
alpar@732
   343
    /// graph from scratch.
alpar@732
   344
    class GraphSkeleton : public StaticGraphSkeleton
alpar@732
   345
    {
alpar@163
   346
    public:
alpar@732
   347
      /// Defalult constructor.
alpar@774
   348
      GraphSkeleton() { }
alpar@732
   349
      ///Copy consructor.
alpar@186
   350
alpar@732
   351
      ///\todo It is not clear, what we expect from a copy constructor.
alpar@732
   352
      ///E.g. How to assign the nodes/edges to each other? What about maps?
alpar@774
   353
      GraphSkeleton(const GraphSkeleton&) { }
alpar@186
   354
alpar@732
   355
      ///Add a new node to the graph.
alpar@732
   356
alpar@732
   357
      /// \return the new node.
alpar@732
   358
      ///
alpar@774
   359
      Node addNode() { return INVALID; }
alpar@732
   360
      ///Add a new edge to the graph.
alpar@732
   361
alpar@732
   362
      ///Add a new edge to the graph with tail node \c tail
alpar@732
   363
      ///and head node \c head.
alpar@732
   364
      ///\return the new edge.
alpar@774
   365
      Edge addEdge(Node, Node) { return INVALID; }
alpar@732
   366
    
alpar@732
   367
      /// Resets the graph.
alpar@732
   368
alpar@732
   369
      /// This function deletes all edges and nodes of the graph.
alpar@732
   370
      /// It also frees the memory allocated to store them.
alpar@732
   371
      /// \todo It might belong to \c EraseableGraphSkeleton.
alpar@774
   372
      void clear() { }
alpar@163
   373
    };
alpar@163
   374
alpar@732
   375
    /// An empty eraseable graph class.
alpar@52
   376
  
alpar@732
   377
    /// This class is an extension of \c GraphSkeleton. It also makes it
alpar@732
   378
    /// possible to erase edges or nodes.
alpar@732
   379
    class EraseableGraphSkeleton : public GraphSkeleton
alpar@163
   380
    {
alpar@163
   381
    public:
alpar@732
   382
      /// Deletes a node.
alpar@774
   383
      void erase(Node n) { }
alpar@732
   384
      /// Deletes an edge.
alpar@774
   385
      void erase(Edge e) { }
alpar@163
   386
alpar@732
   387
      /// Defalult constructor.
alpar@774
   388
      EraseableGraphSkeleton() { }
alpar@732
   389
      ///Copy consructor.
alpar@774
   390
      EraseableGraphSkeleton(const GraphSkeleton&) { }
alpar@163
   391
    };
alpar@163
   392
alpar@732
   393
    // @}
alpar@732
   394
  } //namespace skeleton
alpar@242
   395
  
marci@174
   396
} //namespace hugo
alpar@52
   397
alpar@145
   398
alpar@145
   399
alpar@182
   400
// class EmptyBipGraph : public Graph Skeleton
alpar@147
   401
// {
alpar@163
   402
//   class ANode {};
alpar@163
   403
//   class BNode {};
alpar@145
   404
alpar@163
   405
//   ANode &next(ANode &) {}
alpar@163
   406
//   BNode &next(BNode &) {}
alpar@145
   407
alpar@163
   408
//   ANode &getFirst(ANode &) const {}
alpar@163
   409
//   BNode &getFirst(BNode &) const {}
alpar@145
   410
alpar@147
   411
//   enum NodeClass { A = 0, B = 1 };
alpar@163
   412
//   NodeClass getClass(Node n) {}
alpar@147
   413
alpar@147
   414
// }
marci@174
   415
alpar@503
   416
#endif // HUGO_SKELETON_GRAPH_H