src/include/skeletons/graph.h
author klao
Sat, 03 Apr 2004 18:21:25 +0000
changeset 282 7f85e99502db
parent 242 b255f25ad394
child 298 315d826faa8f
permissions -rw-r--r--
Bit more elaborated map concepts
marci@174
     1
// -*- c++ -*-
alpar@183
     2
#ifndef HUGO_EMPTYGRAPH_H
alpar@183
     3
#define HUGO_EMPTYGRAPH_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:
alpar@147
    34
    
alpar@163
    35
    /// The base type of the node iterators.
alpar@182
    36
alpar@186
    37
    /// This is the base type of each node iterators,
alpar@182
    38
    /// thus each kind of node iterator will convert to this.
alpar@163
    39
    class Node {
alpar@163
    40
    public:
alpar@163
    41
      /// @warning The default constructor sets the iterator
alpar@163
    42
      /// to an undefined value.
alpar@163
    43
      Node() {}   //FIXME
alpar@182
    44
      /// Invalid constructor \& conversion.
alpar@182
    45
alpar@182
    46
      /// This constructor initializes the iterator to be invalid.
alpar@182
    47
      /// \sa Invalid for more details.
alpar@182
    48
marci@174
    49
      Node(Invalid) {}
alpar@182
    50
      //Node(const Node &) {}
alpar@182
    51
alpar@182
    52
      /// Two iterators are equal if and only if they point to the
alpar@182
    53
      /// same object or both are invalid.
alpar@182
    54
      bool operator==(Node n) const { return true; }
alpar@182
    55
alpar@182
    56
      /// \sa \ref operator==(Node n)
alpar@182
    57
      ///
alpar@182
    58
      bool operator!=(Node n) const { return true; }
alpar@182
    59
alpar@182
    60
      bool operator<(Node n) const { return true; }
alpar@163
    61
    };
alpar@147
    62
    
alpar@163
    63
    /// This iterator goes through each node.
alpar@186
    64
alpar@186
    65
    /// This iterator goes through each node.
alpar@186
    66
    /// Its usage is quite simple, for example you can count the number
alpar@186
    67
    /// of nodes in graph \c G of type \c Graph like this:
alpar@186
    68
    /// \code
alpar@186
    69
    ///int count=0;
alpar@186
    70
    ///for(Graph::NodeIt n(G);G.valid(n);G.next(n)) count++;
alpar@186
    71
    /// \endcode
alpar@163
    72
    class NodeIt : public Node {
alpar@163
    73
    public:
alpar@163
    74
      /// @warning The default constructor sets the iterator
alpar@163
    75
      /// to an undefined value.
alpar@163
    76
      NodeIt() {} //FIXME
alpar@182
    77
      /// Invalid constructor \& conversion.
alpar@182
    78
alpar@163
    79
      /// Initialize the iterator to be invalid
alpar@182
    80
      /// \sa Invalid for more details.
marci@174
    81
      NodeIt(Invalid) {}
alpar@163
    82
      /// Sets the iterator to the first node of \c G.
alpar@182
    83
      NodeIt(const GraphSkeleton &G) {}
alpar@182
    84
      /// @warning The default constructor sets the iterator
alpar@182
    85
      /// to an undefined value.
alpar@182
    86
      NodeIt(const NodeIt &) {}
alpar@163
    87
    };
alpar@163
    88
    
alpar@163
    89
    
alpar@163
    90
    /// The base type of the edge iterators.
alpar@163
    91
    class Edge {
alpar@163
    92
    public:
alpar@163
    93
      /// @warning The default constructor sets the iterator
alpar@163
    94
      /// to an undefined value.
alpar@163
    95
      Edge() {}   //FIXME
alpar@163
    96
      /// Initialize the iterator to be invalid
marci@174
    97
      Edge(Invalid) {}
alpar@182
    98
      /// Two iterators are equal if and only if they point to the
alpar@182
    99
      /// same object or both are invalid.
alpar@182
   100
      bool operator==(Edge n) const { return true; }
alpar@182
   101
      bool operator!=(Edge n) const { return true; }
alpar@182
   102
      bool operator<(Edge n) const { return true; }
alpar@163
   103
    };
alpar@163
   104
    
alpar@242
   105
    /// This iterator goes trough the outgoing edges of a node.
alpar@186
   106
alpar@242
   107
    /// This iterator goes trough the \e outgoing edges of a certain node
alpar@186
   108
    /// of a graph.
alpar@186
   109
    /// Its usage is quite simple, for example you can count the number
alpar@186
   110
    /// of outgoing edges of a node \c n
alpar@186
   111
    /// in graph \c G of type \c Graph as follows.
alpar@186
   112
    /// \code
alpar@186
   113
    ///int count=0;
alpar@186
   114
    ///for(Graph::OutEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
alpar@186
   115
    /// \endcode
alpar@163
   116
    
alpar@163
   117
    class OutEdgeIt : public Edge {
alpar@163
   118
    public:
alpar@163
   119
      /// @warning The default constructor sets the iterator
alpar@163
   120
      /// to an undefined value.
alpar@163
   121
      OutEdgeIt() {}
alpar@163
   122
      /// Initialize the iterator to be invalid
marci@174
   123
      OutEdgeIt(Invalid) {}
alpar@163
   124
      /// This constructor sets the iterator to first outgoing edge.
alpar@163
   125
    
alpar@163
   126
      /// This constructor set the iterator to the first outgoing edge of
alpar@163
   127
      /// node
alpar@163
   128
      ///@param n the node
alpar@163
   129
      ///@param G the graph
alpar@182
   130
      OutEdgeIt(const GraphSkeleton & G, Node n) {}
alpar@163
   131
    };
alpar@163
   132
alpar@242
   133
    /// This iterator goes trough the incoming edges of a node.
alpar@186
   134
alpar@242
   135
    /// This iterator goes trough the \e incoming edges of a certain node
alpar@186
   136
    /// of a graph.
alpar@186
   137
    /// Its usage is quite simple, for example you can count the number
alpar@186
   138
    /// of outgoing edges of a node \c n
alpar@186
   139
    /// in graph \c G of type \c Graph as follows.
alpar@186
   140
    /// \code
alpar@186
   141
    ///int count=0;
alpar@186
   142
    ///for(Graph::InEdgeIt e(G,n);G.valid(e);G.next(e)) count++;
alpar@186
   143
    /// \endcode
alpar@186
   144
alpar@163
   145
    class InEdgeIt : public Edge {
alpar@163
   146
    public:
alpar@163
   147
      /// @warning The default constructor sets the iterator
alpar@163
   148
      /// to an undefined value.
alpar@163
   149
      InEdgeIt() {}
alpar@163
   150
      /// Initialize the iterator to be invalid
marci@174
   151
      InEdgeIt(Invalid) {}
alpar@182
   152
      InEdgeIt(const GraphSkeleton &, Node) {}    
alpar@163
   153
    };
alpar@163
   154
    //  class SymEdgeIt : public Edge {};
alpar@186
   155
alpar@186
   156
    /// This iterator goes through each edge.
alpar@186
   157
alpar@186
   158
    /// This iterator goes through each edge of a graph.
alpar@186
   159
    /// Its usage is quite simple, for example you can count the number
alpar@186
   160
    /// of edges in a graph \c G of type \c Graph as follows:
alpar@186
   161
    /// \code
alpar@186
   162
    ///int count=0;
alpar@186
   163
    ///for(Graph::EdgeIt e(G);G.valid(e);G.next(e)) count++;
alpar@186
   164
    /// \endcode
alpar@163
   165
    class EdgeIt : public Edge {
alpar@163
   166
    public:
alpar@163
   167
      /// @warning The default constructor sets the iterator
alpar@163
   168
      /// to an undefined value.
alpar@163
   169
      EdgeIt() {}
alpar@163
   170
      /// Initialize the iterator to be invalid
marci@174
   171
      EdgeIt(Invalid) {}
alpar@182
   172
      EdgeIt(const GraphSkeleton &) {}
alpar@163
   173
    };
alpar@163
   174
alpar@163
   175
    /// First node of the graph.
alpar@163
   176
alpar@163
   177
    /// \post \c i and the return value will be the first node.
alpar@163
   178
    ///
alpar@163
   179
    NodeIt &first(NodeIt &i) const { return i;}
alpar@163
   180
alpar@242
   181
    /// The first incoming edge.
alpar@242
   182
    InEdgeIt &first(InEdgeIt &i, Node n) const { return i;}
alpar@163
   183
    /// The first outgoing edge.
alpar@163
   184
    OutEdgeIt &first(OutEdgeIt &i, Node n) const { return i;}
alpar@163
   185
    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
alpar@163
   186
    /// The first edge of the Graph.
alpar@163
   187
    EdgeIt &first(EdgeIt &i) const { return i;}
alpar@163
   188
alpar@163
   189
//     Node getNext(Node) const {}
alpar@163
   190
//     InEdgeIt getNext(InEdgeIt) const {}
alpar@163
   191
//     OutEdgeIt getNext(OutEdgeIt) const {}
alpar@163
   192
//     //SymEdgeIt getNext(SymEdgeIt) const {}
alpar@163
   193
//     EdgeIt getNext(EdgeIt) const {}
alpar@163
   194
alpar@163
   195
    /// Go to the next node.
marci@178
   196
    NodeIt &next(NodeIt &i) const { return i;}
alpar@163
   197
    /// Go to the next incoming edge.
alpar@163
   198
    InEdgeIt &next(InEdgeIt &i) const { return i;}
alpar@163
   199
    /// Go to the next outgoing edge.
alpar@163
   200
    OutEdgeIt &next(OutEdgeIt &i) const { return i;}
alpar@163
   201
    //SymEdgeIt &next(SymEdgeIt &) const {}
alpar@163
   202
    /// Go to the next edge.
alpar@163
   203
    EdgeIt &next(EdgeIt &i) const { return i;}
alpar@163
   204
alpar@163
   205
    ///Gives back the head node of an edge.
alpar@163
   206
    Node head(Edge) const { return INVALID; }
alpar@163
   207
    ///Gives back the tail node of an edge.
alpar@163
   208
    Node tail(Edge) const { return INVALID; }
alpar@52
   209
  
alpar@163
   210
    //   Node aNode(InEdgeIt) const {}
alpar@163
   211
    //   Node aNode(OutEdgeIt) const {}
alpar@163
   212
    //   Node aNode(SymEdgeIt) const {}
alpar@163
   213
alpar@163
   214
    //   Node bNode(InEdgeIt) const {}
alpar@163
   215
    //   Node bNode(OutEdgeIt) const {}
alpar@163
   216
    //   Node bNode(SymEdgeIt) const {}
alpar@163
   217
alpar@163
   218
    /// Checks if a node iterator is valid
alpar@186
   219
alpar@186
   220
    ///\todo Maybe, it would be better if iterator converted to
alpar@186
   221
    ///bool directly, as Jacint prefers.
marci@174
   222
    bool valid(const Node) const { return true;}
alpar@163
   223
    /// Checks if an edge iterator is valid
alpar@186
   224
alpar@186
   225
    ///\todo Maybe, it would be better if iterator converted to
alpar@186
   226
    ///bool directly, as Jacint prefers.
marci@174
   227
    bool valid(const Edge) const { return true;}
alpar@163
   228
alpar@163
   229
    ///Gives back the \e id of a node.
alpar@182
   230
alpar@242
   231
    ///\warning Not all graph structures provide this feature.
alpar@182
   232
    ///
marci@174
   233
    int id(const Node) const { return 0;}
alpar@163
   234
    ///Gives back the \e id of an edge.
alpar@182
   235
alpar@242
   236
    ///\warning Not all graph structures provide this feature.
alpar@182
   237
    ///
marci@174
   238
    int id(const Edge) const { return 0;}
alpar@163
   239
alpar@163
   240
    //void setInvalid(Node &) const {};
alpar@163
   241
    //void setInvalid(Edge &) const {};
alpar@163
   242
  
alpar@182
   243
    ///Add a new node to the graph.
alpar@182
   244
alpar@182
   245
    /// \return the new node.
alpar@186
   246
    ///
alpar@163
   247
    Node addNode() { return INVALID;}
alpar@182
   248
    ///Add a new edge to the graph.
alpar@182
   249
alpar@182
   250
    ///Add a new edge to the graph with tail node \c tail
alpar@182
   251
    ///and head node \c head.
alpar@182
   252
    ///\return the new edge.
alpar@163
   253
    Edge addEdge(Node tail, Node head) { return INVALID;}
alpar@163
   254
    
alpar@242
   255
    /// Resets the graph.
alpar@182
   256
alpar@182
   257
    /// This function deletes all edges and nodes of the graph.
alpar@182
   258
    /// It also frees the memory allocated to store them.
alpar@163
   259
    void clear() {}
alpar@163
   260
marci@179
   261
    int nodeNum() const { return 0;}
marci@179
   262
    int edgeNum() const { return 0;}
alpar@242
   263
 
alpar@242
   264
    /// Defalult constructor.
alpar@182
   265
    GraphSkeleton() {}
alpar@242
   266
    ///Copy consructor.
alpar@182
   267
    GraphSkeleton(const GraphSkeleton &G) {}
alpar@163
   268
  
alpar@242
   269
 
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
alpar@183
   393
#endif // HUGO_EMPTYGRAPH_H