src/include/skeletons/graph.h
author alpar
Sat, 17 Apr 2004 13:15:53 +0000
changeset 348 b63ea19e502e
parent 320 190ecba15b33
child 403 4ade9002b3a4
permissions -rw-r--r--
A bool Edge Map with iterators that goes through the true or the false edges.
marci@174
     1
// -*- c++ -*-
marci@298
     2
#ifndef HUGO_GRAPH_H
marci@298
     3
#define HUGO_GRAPH_H
alpar@52
     4
alpar@242
     5
///\file
alpar@242
     6
///\brief Declaration of GraphSkeleton.
alpar@242
     7
alpar@163
     8
#include <invalid.h>
alpar@145
     9
alpar@163
    10
/// The namespace of HugoLib
alpar@163
    11
namespace hugo {
alpar@163
    12
alpar@182
    13
  // @defgroup empty_graph The GraphSkeleton class
alpar@163
    14
  // @{
alpar@163
    15
alpar@163
    16
  /// An empty graph class.
alpar@163
    17
  
alpar@186
    18
  /// This class provides all the common features of a graph structure,
alpar@186
    19
  /// however completely without implementations and real data structures
alpar@163
    20
  /// behind the interface.
alpar@163
    21
  /// All graph algorithms should compile with this class, but it will not
alpar@163
    22
  /// run properly, of course.
alpar@163
    23
  ///
alpar@163
    24
  /// It can be used for checking the interface compatibility,
alpar@163
    25
  /// or it can serve as a skeleton of a new graph structure.
alpar@165
    26
  /// 
alpar@165
    27
  /// Also, you will find here the full documentation of a certain graph
alpar@165
    28
  /// feature, the documentation of a real graph imlementation
alpar@165
    29
  /// like @ref ListGraph or
alpar@165
    30
  /// @ref SmartGraph will just refer to this structure.
alpar@182
    31
  class GraphSkeleton
alpar@163
    32
  {
alpar@147
    33
  public:
marci@320
    34
    /// Defalult constructor.
marci@320
    35
    GraphSkeleton() {}
marci@320
    36
    ///Copy consructor.
alpar@321
    37
alpar@321
    38
    ///\todo It is not clear, what we expect from a copy constructor.
alpar@321
    39
    ///E.g. How to assign the nodes/edges to each other? What about maps?
marci@320
    40
    GraphSkeleton(const GraphSkeleton &G) {}
marci@320
    41
alpar@163
    42
    /// The base type of the node iterators.
alpar@182
    43
alpar@186
    44
    /// This is the base type of each node iterators,
alpar@182
    45
    /// thus each kind of node iterator will convert to this.
alpar@163
    46
    class Node {
alpar@163
    47
    public:
alpar@163
    48
      /// @warning The default constructor sets the iterator
alpar@163
    49
      /// to an undefined value.
alpar@163
    50
      Node() {}   //FIXME
alpar@182
    51
      /// Invalid constructor \& conversion.
alpar@182
    52
alpar@182
    53
      /// This constructor initializes the iterator to be invalid.
alpar@182
    54
      /// \sa Invalid for more details.
alpar@182
    55
marci@174
    56
      Node(Invalid) {}
alpar@182
    57
      //Node(const Node &) {}
alpar@182
    58
alpar@182
    59
      /// Two iterators are equal if and only if they point to the
alpar@182
    60
      /// same object or both are invalid.
alpar@182
    61
      bool operator==(Node n) const { return true; }
alpar@182
    62
alpar@182
    63
      /// \sa \ref operator==(Node n)
alpar@182
    64
      ///
alpar@182
    65
      bool operator!=(Node n) const { return true; }
alpar@182
    66
alpar@182
    67
      bool operator<(Node n) const { return true; }
alpar@163
    68
    };
alpar@147
    69
    
alpar@163
    70
    /// This iterator goes through each node.
alpar@186
    71
alpar@186
    72
    /// This iterator goes through each node.
alpar@186
    73
    /// Its usage is quite simple, for example you can count the number
alpar@186
    74
    /// of nodes in graph \c G of type \c Graph like this:
alpar@186
    75
    /// \code
alpar@186
    76
    ///int count=0;
alpar@186
    77
    ///for(Graph::NodeIt n(G);G.valid(n);G.next(n)) count++;
alpar@186
    78
    /// \endcode
alpar@163
    79
    class NodeIt : public Node {
alpar@163
    80
    public:
alpar@163
    81
      /// @warning The default constructor sets the iterator
alpar@163
    82
      /// to an undefined value.
alpar@163
    83
      NodeIt() {} //FIXME
alpar@182
    84
      /// Invalid constructor \& conversion.
alpar@182
    85
alpar@163
    86
      /// Initialize the iterator to be invalid
alpar@182
    87
      /// \sa Invalid for more details.
marci@174
    88
      NodeIt(Invalid) {}
alpar@163
    89
      /// Sets the iterator to the first node of \c G.
alpar@182
    90
      NodeIt(const GraphSkeleton &G) {}
alpar@182
    91
      /// @warning The default constructor sets the iterator
alpar@182
    92
      /// to an undefined value.
alpar@182
    93
      NodeIt(const NodeIt &) {}
alpar@163
    94
    };
alpar@163
    95
    
alpar@163
    96
    
alpar@163
    97
    /// The base type of the edge iterators.
alpar@163
    98
    class Edge {
alpar@163
    99
    public:
alpar@163
   100
      /// @warning The default constructor sets the iterator
alpar@163
   101
      /// to an undefined value.
alpar@163
   102
      Edge() {}   //FIXME
alpar@163
   103
      /// Initialize the iterator to be invalid
marci@174
   104
      Edge(Invalid) {}
alpar@182
   105
      /// Two iterators are equal if and only if they point to the
alpar@182
   106
      /// same object or both are invalid.
alpar@182
   107
      bool operator==(Edge n) const { return true; }
alpar@182
   108
      bool operator!=(Edge n) const { return true; }
alpar@182
   109
      bool operator<(Edge n) const { return true; }
alpar@163
   110
    };
alpar@163
   111
    
alpar@242
   112
    /// This iterator goes trough the outgoing edges of a node.
alpar@186
   113
alpar@242
   114
    /// This iterator goes trough the \e outgoing edges of a certain node
alpar@186
   115
    /// of a graph.
alpar@186
   116
    /// Its usage is quite simple, for example you can count the number
alpar@186
   117
    /// of outgoing edges of a node \c n
alpar@186
   118
    /// in graph \c G of type \c Graph as follows.
alpar@186
   119
    /// \code
alpar@186
   120
    ///int count=0;
alpar@186
   121
    ///for(Graph::OutEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
alpar@186
   122
    /// \endcode
alpar@163
   123
    
alpar@163
   124
    class OutEdgeIt : public Edge {
alpar@163
   125
    public:
alpar@163
   126
      /// @warning The default constructor sets the iterator
alpar@163
   127
      /// to an undefined value.
alpar@163
   128
      OutEdgeIt() {}
alpar@163
   129
      /// Initialize the iterator to be invalid
marci@174
   130
      OutEdgeIt(Invalid) {}
alpar@163
   131
      /// This constructor sets the iterator to first outgoing edge.
alpar@163
   132
    
alpar@163
   133
      /// This constructor set the iterator to the first outgoing edge of
alpar@163
   134
      /// node
alpar@163
   135
      ///@param n the node
alpar@163
   136
      ///@param G the graph
alpar@182
   137
      OutEdgeIt(const GraphSkeleton & G, Node n) {}
alpar@163
   138
    };
alpar@163
   139
alpar@242
   140
    /// This iterator goes trough the incoming edges of a node.
alpar@186
   141
alpar@242
   142
    /// This iterator goes trough the \e incoming edges of a certain node
alpar@186
   143
    /// of a graph.
alpar@186
   144
    /// Its usage is quite simple, for example you can count the number
alpar@186
   145
    /// of outgoing edges of a node \c n
alpar@186
   146
    /// in graph \c G of type \c Graph as follows.
alpar@186
   147
    /// \code
alpar@186
   148
    ///int count=0;
alpar@186
   149
    ///for(Graph::InEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
alpar@186
   150
    /// \endcode
alpar@186
   151
alpar@163
   152
    class InEdgeIt : public Edge {
alpar@163
   153
    public:
alpar@163
   154
      /// @warning The default constructor sets the iterator
alpar@163
   155
      /// to an undefined value.
alpar@163
   156
      InEdgeIt() {}
alpar@163
   157
      /// Initialize the iterator to be invalid
marci@174
   158
      InEdgeIt(Invalid) {}
alpar@182
   159
      InEdgeIt(const GraphSkeleton &, Node) {}    
alpar@163
   160
    };
alpar@163
   161
    //  class SymEdgeIt : public Edge {};
alpar@186
   162
alpar@186
   163
    /// This iterator goes through each edge.
alpar@186
   164
alpar@186
   165
    /// This iterator goes through each edge of a graph.
alpar@186
   166
    /// Its usage is quite simple, for example you can count the number
alpar@186
   167
    /// of edges in a graph \c G of type \c Graph as follows:
alpar@186
   168
    /// \code
alpar@186
   169
    ///int count=0;
alpar@186
   170
    ///for(Graph::EdgeIt e(G);G.valid(e);G.next(e)) count++;
alpar@186
   171
    /// \endcode
alpar@163
   172
    class EdgeIt : public Edge {
alpar@163
   173
    public:
alpar@163
   174
      /// @warning The default constructor sets the iterator
alpar@163
   175
      /// to an undefined value.
alpar@163
   176
      EdgeIt() {}
alpar@163
   177
      /// Initialize the iterator to be invalid
marci@174
   178
      EdgeIt(Invalid) {}
alpar@182
   179
      EdgeIt(const GraphSkeleton &) {}
alpar@163
   180
    };
alpar@163
   181
alpar@163
   182
    /// First node of the graph.
alpar@163
   183
alpar@163
   184
    /// \post \c i and the return value will be the first node.
alpar@163
   185
    ///
alpar@163
   186
    NodeIt &first(NodeIt &i) const { return i;}
alpar@163
   187
alpar@242
   188
    /// The first incoming edge.
alpar@242
   189
    InEdgeIt &first(InEdgeIt &i, Node n) const { return i;}
alpar@163
   190
    /// The first outgoing edge.
alpar@163
   191
    OutEdgeIt &first(OutEdgeIt &i, Node n) const { return i;}
alpar@163
   192
    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
alpar@163
   193
    /// The first edge of the Graph.
alpar@163
   194
    EdgeIt &first(EdgeIt &i) const { return i;}
alpar@163
   195
alpar@163
   196
//     Node getNext(Node) const {}
alpar@163
   197
//     InEdgeIt getNext(InEdgeIt) const {}
alpar@163
   198
//     OutEdgeIt getNext(OutEdgeIt) const {}
alpar@163
   199
//     //SymEdgeIt getNext(SymEdgeIt) const {}
alpar@163
   200
//     EdgeIt getNext(EdgeIt) const {}
alpar@163
   201
alpar@163
   202
    /// Go to the next node.
marci@178
   203
    NodeIt &next(NodeIt &i) const { return i;}
alpar@163
   204
    /// Go to the next incoming edge.
alpar@163
   205
    InEdgeIt &next(InEdgeIt &i) const { return i;}
alpar@163
   206
    /// Go to the next outgoing edge.
alpar@163
   207
    OutEdgeIt &next(OutEdgeIt &i) const { return i;}
alpar@163
   208
    //SymEdgeIt &next(SymEdgeIt &) const {}
alpar@163
   209
    /// Go to the next edge.
alpar@163
   210
    EdgeIt &next(EdgeIt &i) const { return i;}
alpar@163
   211
alpar@163
   212
    ///Gives back the head node of an edge.
alpar@163
   213
    Node head(Edge) const { return INVALID; }
alpar@163
   214
    ///Gives back the tail node of an edge.
alpar@163
   215
    Node tail(Edge) const { return INVALID; }
alpar@52
   216
  
alpar@163
   217
    //   Node aNode(InEdgeIt) const {}
alpar@163
   218
    //   Node aNode(OutEdgeIt) const {}
alpar@163
   219
    //   Node aNode(SymEdgeIt) const {}
alpar@163
   220
alpar@163
   221
    //   Node bNode(InEdgeIt) const {}
alpar@163
   222
    //   Node bNode(OutEdgeIt) const {}
alpar@163
   223
    //   Node bNode(SymEdgeIt) const {}
alpar@163
   224
alpar@163
   225
    /// Checks if a node iterator is valid
alpar@186
   226
alpar@186
   227
    ///\todo Maybe, it would be better if iterator converted to
alpar@186
   228
    ///bool directly, as Jacint prefers.
marci@320
   229
    bool valid(const Node&) const { return true;}
alpar@163
   230
    /// Checks if an edge iterator is valid
alpar@186
   231
alpar@186
   232
    ///\todo Maybe, it would be better if iterator converted to
alpar@186
   233
    ///bool directly, as Jacint prefers.
marci@320
   234
    bool valid(const Edge&) const { return true;}
alpar@163
   235
alpar@163
   236
    ///Gives back the \e id of a node.
alpar@182
   237
alpar@242
   238
    ///\warning Not all graph structures provide this feature.
alpar@182
   239
    ///
marci@320
   240
    int id(const Node&) const { return 0;}
alpar@163
   241
    ///Gives back the \e id of an edge.
alpar@182
   242
alpar@242
   243
    ///\warning Not all graph structures provide this feature.
alpar@182
   244
    ///
marci@320
   245
    int id(const Edge&) const { return 0;}
alpar@163
   246
alpar@163
   247
    //void setInvalid(Node &) const {};
alpar@163
   248
    //void setInvalid(Edge &) const {};
alpar@163
   249
  
alpar@182
   250
    ///Add a new node to the graph.
alpar@182
   251
alpar@182
   252
    /// \return the new node.
alpar@186
   253
    ///
alpar@163
   254
    Node addNode() { return INVALID;}
alpar@182
   255
    ///Add a new edge to the graph.
alpar@182
   256
alpar@182
   257
    ///Add a new edge to the graph with tail node \c tail
alpar@182
   258
    ///and head node \c head.
alpar@182
   259
    ///\return the new edge.
alpar@163
   260
    Edge addEdge(Node tail, Node head) { return INVALID;}
alpar@163
   261
    
alpar@242
   262
    /// Resets the graph.
alpar@182
   263
alpar@182
   264
    /// This function deletes all edges and nodes of the graph.
alpar@182
   265
    /// It also frees the memory allocated to store them.
alpar@163
   266
    void clear() {}
alpar@163
   267
marci@179
   268
    int nodeNum() const { return 0;}
marci@179
   269
    int edgeNum() const { return 0;}
alpar@163
   270
alpar@186
   271
    ///Read/write/reference map of the nodes to type \c T.
alpar@182
   272
alpar@186
   273
    ///Read/write/reference map of the nodes to type \c T.
alpar@186
   274
    /// \sa MemoryMapSkeleton
alpar@182
   275
    /// \todo We may need copy constructor
alpar@182
   276
    /// \todo We may need conversion from other nodetype
alpar@182
   277
    /// \todo We may need operator=
alpar@216
   278
    /// \warning Making maps that can handle bool type (NodeMap<bool>)
alpar@216
   279
    /// needs extra attention!
alpar@182
   280
alpar@163
   281
    template<class T> class NodeMap
alpar@163
   282
    {
alpar@163
   283
    public:
alpar@163
   284
      typedef T ValueType;
alpar@163
   285
      typedef Node KeyType;
alpar@163
   286
alpar@182
   287
      NodeMap(const GraphSkeleton &G) {}
alpar@182
   288
      NodeMap(const GraphSkeleton &G, T t) {}
alpar@163
   289
alpar@182
   290
      template<typename TT> NodeMap(const NodeMap<TT> &m) {}
alpar@182
   291
alpar@182
   292
      /// Sets the value of a node.
alpar@182
   293
alpar@182
   294
      /// Sets the value associated with node \c i to the value \c t.
alpar@182
   295
      ///
alpar@163
   296
      void set(Node i, T t) {}
alpar@182
   297
      /// Gets the value of a node.
alpar@182
   298
      T get(Node i) const {return *(T*)0;}  //FIXME: Is it necessary
alpar@182
   299
      T &operator[](Node i) {return *(T*)0;}
alpar@182
   300
      const T &operator[](Node i) const {return *(T*)0;}
alpar@163
   301
alpar@182
   302
      /// Updates the map if the graph has been changed
alpar@182
   303
alpar@182
   304
      /// \todo Do we need this?
alpar@182
   305
      ///
alpar@163
   306
      void update() {}
alpar@163
   307
      void update(T a) {}   //FIXME: Is it necessary
alpar@163
   308
    };
alpar@163
   309
alpar@186
   310
    ///Read/write/reference map of the edges to type \c T.
alpar@182
   311
alpar@186
   312
    ///Read/write/reference map of the edges to type \c T.
alpar@186
   313
    ///It behaves exactly in the same way as \ref NodeMap.
alpar@186
   314
    /// \sa NodeMap
alpar@186
   315
    /// \sa MemoryMapSkeleton
alpar@186
   316
    /// \todo We may need copy constructor
alpar@186
   317
    /// \todo We may need conversion from other edgetype
alpar@186
   318
    /// \todo We may need operator=
alpar@163
   319
    template<class T> class EdgeMap
alpar@163
   320
    {
alpar@163
   321
    public:
alpar@163
   322
      typedef T ValueType;
alpar@163
   323
      typedef Edge KeyType;
alpar@163
   324
alpar@182
   325
      EdgeMap(const GraphSkeleton &G) {}
alpar@182
   326
      EdgeMap(const GraphSkeleton &G, T t) {}
alpar@163
   327
    
alpar@163
   328
      void set(Edge i, T t) {}
alpar@182
   329
      T get(Edge i) const {return *(T*)0;}
alpar@182
   330
      T &operator[](Edge i) {return *(T*)0;}
alpar@163
   331
    
alpar@163
   332
      void update() {}
alpar@163
   333
      void update(T a) {}   //FIXME: Is it necessary
alpar@163
   334
    };
alpar@147
   335
  };
alpar@52
   336
alpar@242
   337
  /// An empty eraseable graph class.
alpar@242
   338
  
alpar@242
   339
  /// This class provides all the common features of an \e eraseable graph
alpar@242
   340
  /// structure,
alpar@242
   341
  /// however completely without implementations and real data structures
alpar@242
   342
  /// behind the interface.
alpar@242
   343
  /// All graph algorithms should compile with this class, but it will not
alpar@242
   344
  /// run properly, of course.
alpar@242
   345
  ///
alpar@242
   346
  /// \todo This blabla could be replaced by a sepatate description about
alpar@242
   347
  /// Skeletons.
alpar@242
   348
  ///
alpar@242
   349
  /// It can be used for checking the interface compatibility,
alpar@242
   350
  /// or it can serve as a skeleton of a new graph structure.
alpar@242
   351
  /// 
alpar@242
   352
  /// Also, you will find here the full documentation of a certain graph
alpar@242
   353
  /// feature, the documentation of a real graph imlementation
alpar@242
   354
  /// like @ref ListGraph or
alpar@242
   355
  /// @ref SmartGraph will just refer to this structure.
alpar@242
   356
  class EraseableGraphSkeleton : public GraphSkeleton
alpar@242
   357
  {
alpar@242
   358
  public:
alpar@242
   359
    /// Deletes a node.
alpar@242
   360
    void erase(Node n) {}
alpar@242
   361
    /// Deletes an edge.
alpar@242
   362
    void erase(Edge e) {}
alpar@242
   363
alpar@242
   364
    /// Defalult constructor.
alpar@242
   365
    GraphSkeleton() {}
alpar@242
   366
    ///Copy consructor.
alpar@242
   367
    GraphSkeleton(const GraphSkeleton &G) {}
alpar@242
   368
  };
alpar@242
   369
alpar@242
   370
  
alpar@163
   371
  // @}
alpar@147
   372
marci@174
   373
} //namespace hugo
alpar@52
   374
alpar@145
   375
alpar@145
   376
alpar@182
   377
// class EmptyBipGraph : public Graph Skeleton
alpar@147
   378
// {
alpar@163
   379
//   class ANode {};
alpar@163
   380
//   class BNode {};
alpar@145
   381
alpar@163
   382
//   ANode &next(ANode &) {}
alpar@163
   383
//   BNode &next(BNode &) {}
alpar@145
   384
alpar@163
   385
//   ANode &getFirst(ANode &) const {}
alpar@163
   386
//   BNode &getFirst(BNode &) const {}
alpar@145
   387
alpar@147
   388
//   enum NodeClass { A = 0, B = 1 };
alpar@163
   389
//   NodeClass getClass(Node n) {}
alpar@147
   390
alpar@147
   391
// }
marci@174
   392
marci@298
   393
#endif // HUGO_GRAPH_H