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