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