lemon/concepts/graph.h
author alpar
Sat, 03 Mar 2007 12:00:32 +0000
changeset 2388 c6d537888fe5
child 2391 14a343be7a5a
permissions -rw-r--r--
More doc.
alpar@2260
     1
/* -*- C++ -*-
alpar@2260
     2
 *
alpar@2260
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2260
     4
 *
alpar@2260
     5
 * Copyright (C) 2003-2006
alpar@2260
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2260
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2260
     8
 *
alpar@2260
     9
 * Permission to use, modify and distribute this software is granted
alpar@2260
    10
 * provided that this copyright notice appears in all copies. For
alpar@2260
    11
 * precise terms see the accompanying LICENSE file.
alpar@2260
    12
 *
alpar@2260
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2260
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2260
    15
 * purpose.
alpar@2260
    16
 *
alpar@2260
    17
 */
alpar@2260
    18
alpar@2260
    19
#ifndef LEMON_CONCEPT_GRAPH_H
alpar@2260
    20
#define LEMON_CONCEPT_GRAPH_H
alpar@2260
    21
alpar@2260
    22
///\ingroup graph_concepts
alpar@2260
    23
///\file
alpar@2260
    24
///\brief Declaration of Graph.
alpar@2260
    25
alpar@2260
    26
#include <lemon/bits/invalid.h>
alpar@2260
    27
#include <lemon/bits/utility.h>
alpar@2260
    28
#include <lemon/concepts/maps.h>
alpar@2260
    29
#include <lemon/concept_check.h>
alpar@2260
    30
#include <lemon/concepts/graph_components.h>
alpar@2260
    31
alpar@2260
    32
namespace lemon {
alpar@2260
    33
  namespace concepts {
alpar@2260
    34
alpar@2260
    35
    /// \addtogroup graph_concepts
alpar@2260
    36
    /// @{
alpar@2260
    37
alpar@2260
    38
    /// The directed graph concept
alpar@2260
    39
alpar@2260
    40
    /// This class describes the \ref concept "concept" of the
alpar@2260
    41
    /// immutable directed graphs.
alpar@2260
    42
    ///
alpar@2260
    43
    /// Note that actual graph implementation like @ref ListGraph or
alpar@2260
    44
    /// @ref SmartGraph may have several additional functionality.
alpar@2260
    45
    ///
alpar@2260
    46
    /// \sa concept
alpar@2260
    47
    class Graph {
alpar@2260
    48
    private:
alpar@2260
    49
      ///Graphs are \e not copy constructible. Use GraphCopy() instead.
alpar@2260
    50
      
alpar@2260
    51
      ///Graphs are \e not copy constructible. Use GraphCopy() instead.
alpar@2260
    52
      ///
alpar@2260
    53
      Graph(const Graph &) {};
alpar@2260
    54
      ///\brief Assignment of \ref Graph "Graph"s to another ones are
alpar@2260
    55
      ///\e not allowed. Use GraphCopy() instead.
alpar@2260
    56
      
alpar@2260
    57
      ///Assignment of \ref Graph "Graph"s to another ones are
alpar@2260
    58
      ///\e not allowed.  Use GraphCopy() instead.
alpar@2260
    59
alpar@2260
    60
      void operator=(const Graph &) {}
alpar@2260
    61
    public:
alpar@2260
    62
      ///\e
alpar@2260
    63
alpar@2260
    64
      /// Defalult constructor.
alpar@2260
    65
alpar@2260
    66
      /// Defalult constructor.
alpar@2260
    67
      ///
alpar@2260
    68
      Graph() { }
alpar@2260
    69
      /// Class for identifying a node of the graph
alpar@2260
    70
alpar@2260
    71
      /// This class identifies a node of the graph. It also serves
alpar@2260
    72
      /// as a base class of the node iterators,
alpar@2260
    73
      /// thus they will convert to this type.
alpar@2260
    74
      class Node {
alpar@2260
    75
      public:
alpar@2260
    76
        /// Default constructor
alpar@2260
    77
alpar@2260
    78
        /// @warning The default constructor sets the iterator
alpar@2260
    79
        /// to an undefined value.
alpar@2260
    80
        Node() { }
alpar@2260
    81
        /// Copy constructor.
alpar@2260
    82
alpar@2260
    83
        /// Copy constructor.
alpar@2260
    84
        ///
alpar@2260
    85
        Node(const Node&) { }
alpar@2260
    86
alpar@2260
    87
        /// Invalid constructor \& conversion.
alpar@2260
    88
alpar@2260
    89
        /// This constructor initializes the iterator to be invalid.
alpar@2260
    90
        /// \sa Invalid for more details.
alpar@2260
    91
        Node(Invalid) { }
alpar@2260
    92
        /// Equality operator
alpar@2260
    93
alpar@2260
    94
        /// Two iterators are equal if and only if they point to the
alpar@2260
    95
        /// same object or both are invalid.
alpar@2260
    96
        bool operator==(Node) const { return true; }
alpar@2260
    97
alpar@2260
    98
        /// Inequality operator
alpar@2260
    99
        
alpar@2260
   100
        /// \sa operator==(Node n)
alpar@2260
   101
        ///
alpar@2260
   102
        bool operator!=(Node) const { return true; }
alpar@2260
   103
alpar@2260
   104
	/// Artificial ordering operator.
alpar@2260
   105
	
alpar@2260
   106
	/// To allow the use of graph descriptors as key type in std::map or
alpar@2260
   107
	/// similar associative container we require this.
alpar@2260
   108
	///
alpar@2260
   109
	/// \note This operator only have to define some strict ordering of
alpar@2260
   110
	/// the items; this order has nothing to do with the iteration
alpar@2260
   111
	/// ordering of the items.
alpar@2260
   112
	bool operator<(Node) const { return false; }
alpar@2260
   113
alpar@2260
   114
      };
alpar@2260
   115
    
alpar@2260
   116
      /// This iterator goes through each node.
alpar@2260
   117
alpar@2260
   118
      /// This iterator goes through each node.
alpar@2260
   119
      /// Its usage is quite simple, for example you can count the number
alpar@2260
   120
      /// of nodes in graph \c g of type \c Graph like this:
alpar@2260
   121
      ///\code
alpar@2260
   122
      /// int count=0;
alpar@2260
   123
      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
alpar@2260
   124
      ///\endcode
alpar@2260
   125
      class NodeIt : public Node {
alpar@2260
   126
      public:
alpar@2260
   127
        /// Default constructor
alpar@2260
   128
alpar@2260
   129
        /// @warning The default constructor sets the iterator
alpar@2260
   130
        /// to an undefined value.
alpar@2260
   131
        NodeIt() { }
alpar@2260
   132
        /// Copy constructor.
alpar@2260
   133
        
alpar@2260
   134
        /// Copy constructor.
alpar@2260
   135
        ///
alpar@2260
   136
        NodeIt(const NodeIt& n) : Node(n) { }
alpar@2260
   137
        /// Invalid constructor \& conversion.
alpar@2260
   138
alpar@2260
   139
        /// Initialize the iterator to be invalid.
alpar@2260
   140
        /// \sa Invalid for more details.
alpar@2260
   141
        NodeIt(Invalid) { }
alpar@2260
   142
        /// Sets the iterator to the first node.
alpar@2260
   143
alpar@2260
   144
        /// Sets the iterator to the first node of \c g.
alpar@2260
   145
        ///
alpar@2260
   146
        NodeIt(const Graph&) { }
alpar@2260
   147
        /// Node -> NodeIt conversion.
alpar@2260
   148
alpar@2260
   149
        /// Sets the iterator to the node of \c the graph pointed by 
alpar@2260
   150
	/// the trivial iterator.
alpar@2260
   151
        /// This feature necessitates that each time we 
alpar@2260
   152
        /// iterate the edge-set, the iteration order is the same.
alpar@2260
   153
        NodeIt(const Graph&, const Node&) { }
alpar@2260
   154
        /// Next node.
alpar@2260
   155
alpar@2260
   156
        /// Assign the iterator to the next node.
alpar@2260
   157
        ///
alpar@2260
   158
        NodeIt& operator++() { return *this; }
alpar@2260
   159
      };
alpar@2260
   160
    
alpar@2260
   161
    
alpar@2260
   162
      /// Class for identifying an edge of the graph
alpar@2260
   163
alpar@2260
   164
      /// This class identifies an edge of the graph. It also serves
alpar@2260
   165
      /// as a base class of the edge iterators,
alpar@2260
   166
      /// thus they will convert to this type.
alpar@2260
   167
      class Edge {
alpar@2260
   168
      public:
alpar@2260
   169
        /// Default constructor
alpar@2260
   170
alpar@2260
   171
        /// @warning The default constructor sets the iterator
alpar@2260
   172
        /// to an undefined value.
alpar@2260
   173
        Edge() { }
alpar@2260
   174
        /// Copy constructor.
alpar@2260
   175
alpar@2260
   176
        /// Copy constructor.
alpar@2260
   177
        ///
alpar@2260
   178
        Edge(const Edge&) { }
alpar@2260
   179
        /// Initialize the iterator to be invalid.
alpar@2260
   180
alpar@2260
   181
        /// Initialize the iterator to be invalid.
alpar@2260
   182
        ///
alpar@2260
   183
        Edge(Invalid) { }
alpar@2260
   184
        /// Equality operator
alpar@2260
   185
alpar@2260
   186
        /// Two iterators are equal if and only if they point to the
alpar@2260
   187
        /// same object or both are invalid.
alpar@2260
   188
        bool operator==(Edge) const { return true; }
alpar@2260
   189
        /// Inequality operator
alpar@2260
   190
alpar@2260
   191
        /// \sa operator==(Edge n)
alpar@2260
   192
        ///
alpar@2260
   193
        bool operator!=(Edge) const { return true; }
alpar@2260
   194
alpar@2260
   195
	/// Artificial ordering operator.
alpar@2260
   196
	
alpar@2260
   197
	/// To allow the use of graph descriptors as key type in std::map or
alpar@2260
   198
	/// similar associative container we require this.
alpar@2260
   199
	///
alpar@2260
   200
	/// \note This operator only have to define some strict ordering of
alpar@2260
   201
	/// the items; this order has nothing to do with the iteration
alpar@2260
   202
	/// ordering of the items.
alpar@2260
   203
	bool operator<(Edge) const { return false; }
alpar@2260
   204
      };
alpar@2260
   205
    
alpar@2260
   206
      /// This iterator goes trough the outgoing edges of a node.
alpar@2260
   207
alpar@2260
   208
      /// This iterator goes trough the \e outgoing edges of a certain node
alpar@2260
   209
      /// of a graph.
alpar@2260
   210
      /// Its usage is quite simple, for example you can count the number
alpar@2260
   211
      /// of outgoing edges of a node \c n
alpar@2260
   212
      /// in graph \c g of type \c Graph as follows.
alpar@2260
   213
      ///\code
alpar@2260
   214
      /// int count=0;
alpar@2260
   215
      /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
alpar@2260
   216
      ///\endcode
alpar@2260
   217
    
alpar@2260
   218
      class OutEdgeIt : public Edge {
alpar@2260
   219
      public:
alpar@2260
   220
        /// Default constructor
alpar@2260
   221
alpar@2260
   222
        /// @warning The default constructor sets the iterator
alpar@2260
   223
        /// to an undefined value.
alpar@2260
   224
        OutEdgeIt() { }
alpar@2260
   225
        /// Copy constructor.
alpar@2260
   226
alpar@2260
   227
        /// Copy constructor.
alpar@2260
   228
        ///
alpar@2260
   229
        OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
alpar@2260
   230
        /// Initialize the iterator to be invalid.
alpar@2260
   231
alpar@2260
   232
        /// Initialize the iterator to be invalid.
alpar@2260
   233
        ///
alpar@2260
   234
        OutEdgeIt(Invalid) { }
alpar@2260
   235
        /// This constructor sets the iterator to the first outgoing edge.
alpar@2260
   236
    
alpar@2260
   237
        /// This constructor sets the iterator to the first outgoing edge of
alpar@2260
   238
        /// the node.
alpar@2260
   239
        OutEdgeIt(const Graph&, const Node&) { }
alpar@2260
   240
        /// Edge -> OutEdgeIt conversion
alpar@2260
   241
alpar@2260
   242
        /// Sets the iterator to the value of the trivial iterator.
alpar@2260
   243
	/// This feature necessitates that each time we 
alpar@2260
   244
        /// iterate the edge-set, the iteration order is the same.
alpar@2260
   245
        OutEdgeIt(const Graph&, const Edge&) { }
alpar@2260
   246
        ///Next outgoing edge
alpar@2260
   247
        
alpar@2260
   248
        /// Assign the iterator to the next 
alpar@2260
   249
        /// outgoing edge of the corresponding node.
alpar@2260
   250
        OutEdgeIt& operator++() { return *this; }
alpar@2260
   251
      };
alpar@2260
   252
alpar@2260
   253
      /// This iterator goes trough the incoming edges of a node.
alpar@2260
   254
alpar@2260
   255
      /// This iterator goes trough the \e incoming edges of a certain node
alpar@2260
   256
      /// of a graph.
alpar@2260
   257
      /// Its usage is quite simple, for example you can count the number
alpar@2260
   258
      /// of outgoing edges of a node \c n
alpar@2260
   259
      /// in graph \c g of type \c Graph as follows.
alpar@2260
   260
      ///\code
alpar@2260
   261
      /// int count=0;
alpar@2260
   262
      /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
alpar@2260
   263
      ///\endcode
alpar@2260
   264
alpar@2260
   265
      class InEdgeIt : public Edge {
alpar@2260
   266
      public:
alpar@2260
   267
        /// Default constructor
alpar@2260
   268
alpar@2260
   269
        /// @warning The default constructor sets the iterator
alpar@2260
   270
        /// to an undefined value.
alpar@2260
   271
        InEdgeIt() { }
alpar@2260
   272
        /// Copy constructor.
alpar@2260
   273
alpar@2260
   274
        /// Copy constructor.
alpar@2260
   275
        ///
alpar@2260
   276
        InEdgeIt(const InEdgeIt& e) : Edge(e) { }
alpar@2260
   277
        /// Initialize the iterator to be invalid.
alpar@2260
   278
alpar@2260
   279
        /// Initialize the iterator to be invalid.
alpar@2260
   280
        ///
alpar@2260
   281
        InEdgeIt(Invalid) { }
alpar@2260
   282
        /// This constructor sets the iterator to first incoming edge.
alpar@2260
   283
    
alpar@2260
   284
        /// This constructor set the iterator to the first incoming edge of
alpar@2260
   285
        /// the node.
alpar@2260
   286
        InEdgeIt(const Graph&, const Node&) { }
alpar@2260
   287
        /// Edge -> InEdgeIt conversion
alpar@2260
   288
alpar@2260
   289
        /// Sets the iterator to the value of the trivial iterator \c e.
alpar@2260
   290
        /// This feature necessitates that each time we 
alpar@2260
   291
        /// iterate the edge-set, the iteration order is the same.
alpar@2260
   292
        InEdgeIt(const Graph&, const Edge&) { }
alpar@2260
   293
        /// Next incoming edge
alpar@2260
   294
alpar@2260
   295
        /// Assign the iterator to the next inedge of the corresponding node.
alpar@2260
   296
        ///
alpar@2260
   297
        InEdgeIt& operator++() { return *this; }
alpar@2260
   298
      };
alpar@2260
   299
      /// This iterator goes through each edge.
alpar@2260
   300
alpar@2260
   301
      /// This iterator goes through each edge of a graph.
alpar@2260
   302
      /// Its usage is quite simple, for example you can count the number
alpar@2260
   303
      /// of edges in a graph \c g of type \c Graph as follows:
alpar@2260
   304
      ///\code
alpar@2260
   305
      /// int count=0;
alpar@2260
   306
      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
alpar@2260
   307
      ///\endcode
alpar@2260
   308
      class EdgeIt : public Edge {
alpar@2260
   309
      public:
alpar@2260
   310
        /// Default constructor
alpar@2260
   311
alpar@2260
   312
        /// @warning The default constructor sets the iterator
alpar@2260
   313
        /// to an undefined value.
alpar@2260
   314
        EdgeIt() { }
alpar@2260
   315
        /// Copy constructor.
alpar@2260
   316
alpar@2260
   317
        /// Copy constructor.
alpar@2260
   318
        ///
alpar@2260
   319
        EdgeIt(const EdgeIt& e) : Edge(e) { }
alpar@2260
   320
        /// Initialize the iterator to be invalid.
alpar@2260
   321
alpar@2260
   322
        /// Initialize the iterator to be invalid.
alpar@2260
   323
        ///
alpar@2260
   324
        EdgeIt(Invalid) { }
alpar@2260
   325
        /// This constructor sets the iterator to the first edge.
alpar@2260
   326
    
alpar@2260
   327
        /// This constructor sets the iterator to the first edge of \c g.
alpar@2260
   328
        ///@param g the graph
alpar@2260
   329
        EdgeIt(const Graph& g) { ignore_unused_variable_warning(g); }
alpar@2260
   330
        /// Edge -> EdgeIt conversion
alpar@2260
   331
alpar@2260
   332
        /// Sets the iterator to the value of the trivial iterator \c e.
alpar@2260
   333
        /// This feature necessitates that each time we 
alpar@2260
   334
        /// iterate the edge-set, the iteration order is the same.
alpar@2260
   335
        EdgeIt(const Graph&, const Edge&) { } 
alpar@2260
   336
        ///Next edge
alpar@2260
   337
        
alpar@2260
   338
        /// Assign the iterator to the next edge.
alpar@2260
   339
        EdgeIt& operator++() { return *this; }
alpar@2260
   340
      };
alpar@2260
   341
      ///Gives back the target node of an edge.
alpar@2260
   342
alpar@2260
   343
      ///Gives back the target node of an edge.
alpar@2260
   344
      ///
alpar@2260
   345
      Node target(Edge) const { return INVALID; }
alpar@2260
   346
      ///Gives back the source node of an edge.
alpar@2260
   347
alpar@2260
   348
      ///Gives back the source node of an edge.
alpar@2260
   349
      ///
alpar@2260
   350
      Node source(Edge) const { return INVALID; }
alpar@2260
   351
alpar@2260
   352
      void first(Node&) const {}
alpar@2260
   353
      void next(Node&) const {}
alpar@2260
   354
alpar@2260
   355
      void first(Edge&) const {}
alpar@2260
   356
      void next(Edge&) const {}
alpar@2260
   357
alpar@2260
   358
alpar@2260
   359
      void firstIn(Edge&, const Node&) const {}
alpar@2260
   360
      void nextIn(Edge&) const {}
alpar@2260
   361
alpar@2260
   362
      void firstOut(Edge&, const Node&) const {}
alpar@2260
   363
      void nextOut(Edge&) const {}
alpar@2260
   364
alpar@2260
   365
      /// \brief The base node of the iterator.
alpar@2260
   366
      ///
alpar@2260
   367
      /// Gives back the base node of the iterator.
alpar@2260
   368
      /// It is always the target of the pointed edge.
alpar@2260
   369
      Node baseNode(const InEdgeIt&) const { return INVALID; }
alpar@2260
   370
alpar@2260
   371
      /// \brief The running node of the iterator.
alpar@2260
   372
      ///
alpar@2260
   373
      /// Gives back the running node of the iterator.
alpar@2260
   374
      /// It is always the source of the pointed edge.
alpar@2260
   375
      Node runningNode(const InEdgeIt&) const { return INVALID; }
alpar@2260
   376
alpar@2260
   377
      /// \brief The base node of the iterator.
alpar@2260
   378
      ///
alpar@2260
   379
      /// Gives back the base node of the iterator.
alpar@2260
   380
      /// It is always the source of the pointed edge.
alpar@2260
   381
      Node baseNode(const OutEdgeIt&) const { return INVALID; }
alpar@2260
   382
alpar@2260
   383
      /// \brief The running node of the iterator.
alpar@2260
   384
      ///
alpar@2260
   385
      /// Gives back the running node of the iterator.
alpar@2260
   386
      /// It is always the target of the pointed edge.
alpar@2260
   387
      Node runningNode(const OutEdgeIt&) const { return INVALID; }
alpar@2260
   388
alpar@2260
   389
      /// \brief The opposite node on the given edge.
alpar@2260
   390
      ///
alpar@2260
   391
      /// Gives back the opposite node on the given edge.
alpar@2260
   392
      Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
alpar@2260
   393
alpar@2260
   394
      /// \brief Read write map of the nodes to type \c T.
alpar@2260
   395
      /// 
alpar@2260
   396
      /// ReadWrite map of the nodes to type \c T.
alpar@2260
   397
      /// \sa Reference
alpar@2260
   398
      template<class T> 
alpar@2260
   399
      class NodeMap : public ReadWriteMap< Node, T > {
alpar@2260
   400
      public:
alpar@2260
   401
alpar@2260
   402
        ///\e
alpar@2260
   403
        NodeMap(const Graph&) { }
alpar@2260
   404
        ///\e
alpar@2260
   405
        NodeMap(const Graph&, T) { }
alpar@2260
   406
alpar@2260
   407
        ///Copy constructor
alpar@2260
   408
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
alpar@2260
   409
        ///Assignment operator
alpar@2260
   410
        template <typename CMap>
alpar@2260
   411
        NodeMap& operator=(const CMap&) { 
alpar@2260
   412
          checkConcept<ReadMap<Node, T>, CMap>();
alpar@2260
   413
          return *this; 
alpar@2260
   414
        }
alpar@2260
   415
      };
alpar@2260
   416
alpar@2260
   417
      /// \brief Read write map of the edges to type \c T.
alpar@2260
   418
      ///
alpar@2260
   419
      /// Reference map of the edges to type \c T.
alpar@2260
   420
      /// \sa Reference
alpar@2260
   421
      template<class T> 
alpar@2260
   422
      class EdgeMap : public ReadWriteMap<Edge,T> {
alpar@2260
   423
      public:
alpar@2260
   424
alpar@2260
   425
        ///\e
alpar@2260
   426
        EdgeMap(const Graph&) { }
alpar@2260
   427
        ///\e
alpar@2260
   428
        EdgeMap(const Graph&, T) { }
alpar@2260
   429
        ///Copy constructor
alpar@2260
   430
        EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
alpar@2260
   431
        ///Assignment operator
alpar@2260
   432
        template <typename CMap>
alpar@2260
   433
        EdgeMap& operator=(const CMap&) { 
alpar@2260
   434
          checkConcept<ReadMap<Edge, T>, CMap>();
alpar@2260
   435
          return *this; 
alpar@2260
   436
        }
alpar@2260
   437
      };
alpar@2260
   438
alpar@2260
   439
      template <typename RGraph>
alpar@2260
   440
      struct Constraints {
alpar@2260
   441
        void constraints() {
alpar@2260
   442
          checkConcept<IterableGraphComponent<>, Graph>();
alpar@2260
   443
          checkConcept<MappableGraphComponent<>, Graph>();
alpar@2260
   444
        }
alpar@2260
   445
      };
alpar@2260
   446
alpar@2260
   447
    };
alpar@2260
   448
    
alpar@2260
   449
    // @}
alpar@2260
   450
  } //namespace concepts  
alpar@2260
   451
} //namespace lemon
alpar@2260
   452
alpar@2260
   453
alpar@2260
   454
alpar@2260
   455
#endif // LEMON_CONCEPT_GRAPH_H