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