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