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