lemon/concept/graph.h
author deba
Wed, 01 Mar 2006 10:17:25 +0000
changeset 1990 15fb7a4ea6be
parent 1956 a055123339d5
child 1993 2115143eceea
permissions -rw-r--r--
Some classes assumed that the GraphMaps should be inherited
from an ObserverBase. These classes parents replaced with
DefaultMap which cause that the graph maps should not be
inherited from the ObserverBase.
     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/invalid.h>
    27 #include <lemon/utility.h>
    28 #include <lemon/concept/maps.h>
    29 #include <lemon/concept_check.h>
    30 #include <lemon/concept/graph_component.h>
    31 
    32 namespace lemon {
    33   namespace concept {
    34 
    35     
    36     /**************** The full-featured graph concepts ****************/
    37 
    38 
    39     // \brief Modular static graph class.
    40     //     
    41     // It should be the same as the \c StaticGraph class.
    42     class _StaticGraph 
    43       :  virtual public BaseGraphComponent,
    44          public IterableGraphComponent, public MappableGraphComponent {
    45     public:
    46 
    47       typedef BaseGraphComponent::Node Node;
    48       typedef BaseGraphComponent::Edge Edge;
    49 
    50       template <typename _Graph>
    51       struct Constraints {
    52         void constraints() {
    53           checkConcept<IterableGraphComponent, _Graph>();
    54           checkConcept<MappableGraphComponent, _Graph>();
    55         }
    56       };
    57     };
    58 
    59     // \brief Modular extendable graph class.
    60     //     
    61     // It should be the same as the \c ExtendableGraph class.
    62     class _ExtendableGraph 
    63       :  virtual public BaseGraphComponent, public _StaticGraph,
    64          public ExtendableGraphComponent, public ClearableGraphComponent {
    65     public:
    66       typedef BaseGraphComponent::Node Node;
    67       typedef BaseGraphComponent::Edge Edge;
    68 
    69       template <typename _Graph>
    70       struct Constraints {
    71         void constraints() {
    72           checkConcept<_StaticGraph, _Graph >();
    73           checkConcept<ExtendableGraphComponent, _Graph >();
    74           checkConcept<ClearableGraphComponent, _Graph >();
    75         }
    76       };
    77     };
    78 
    79     // \brief Modular erasable graph class.
    80     //     
    81     // It should be the same as the \c ErasableGraph class.
    82     class _ErasableGraph 
    83       :  virtual public BaseGraphComponent, public _ExtendableGraph,
    84          public ErasableGraphComponent {
    85     public:
    86       typedef BaseGraphComponent::Node Node;
    87       typedef BaseGraphComponent::Edge Edge;
    88 
    89       template <typename _Graph>
    90       struct Constraints {
    91         void constraints() {
    92           checkConcept<_ExtendableGraph, _Graph >();
    93           checkConcept<ErasableGraphComponent, _Graph >();
    94         }
    95       };
    96     };
    97 
    98     /// \addtogroup graph_concepts
    99     /// @{
   100 
   101     /// An empty static graph class.
   102   
   103     /// This class provides all the common features of a graph structure,
   104     /// however completely without implementations and real data structures
   105     /// behind the interface.
   106     /// All graph algorithms should compile with this class, but it will not
   107     /// run properly, of course.
   108     ///
   109     /// It can be used for checking the interface compatibility,
   110     /// or it can serve as a skeleton of a new graph structure.
   111     /// 
   112     /// Also, you will find here the full documentation of a certain graph
   113     /// feature, the documentation of a real graph imlementation
   114     /// like @ref ListGraph or
   115     /// @ref SmartGraph will just refer to this structure.
   116     ///
   117     /// \todo A pages describing the concept of concept description would
   118     /// be nice.
   119     class StaticGraph
   120     {
   121     public:
   122       ///\e
   123 
   124       /// Defalult constructor.
   125 
   126       /// Defalult constructor.
   127       ///
   128       StaticGraph() { }
   129       ///Copy consructor.
   130 
   131 //       ///\todo It is not clear, what we expect from a copy constructor.
   132 //       ///E.g. How to assign the nodes/edges to each other? What about maps?
   133 //       StaticGraph(const StaticGraph& g) { }
   134 
   135       /// The base type of node iterators, 
   136       /// or in other words, the trivial node iterator.
   137 
   138       /// This is the base type of each node iterator,
   139       /// thus each kind of node iterator converts to this.
   140       /// More precisely each kind of node iterator should be inherited 
   141       /// from the trivial node iterator.
   142       class Node {
   143       public:
   144         /// Default constructor
   145 
   146         /// @warning The default constructor sets the iterator
   147         /// to an undefined value.
   148         Node() { }
   149         /// Copy constructor.
   150 
   151         /// Copy constructor.
   152         ///
   153         Node(const Node&) { }
   154 
   155         /// Invalid constructor \& conversion.
   156 
   157         /// This constructor initializes the iterator to be invalid.
   158         /// \sa Invalid for more details.
   159         Node(Invalid) { }
   160         /// Equality operator
   161 
   162         /// Two iterators are equal if and only if they point to the
   163         /// same object or both are invalid.
   164         bool operator==(Node) const { return true; }
   165 
   166         /// Inequality operator
   167         
   168         /// \sa operator==(Node n)
   169         ///
   170         bool operator!=(Node) const { return true; }
   171 
   172 	/// Artificial ordering operator.
   173 	
   174 	/// To allow the use of graph descriptors as key type in std::map or
   175 	/// similar associative container we require this.
   176 	///
   177 	/// \note This operator only have to define some strict ordering of
   178 	/// the items; this order has nothing to do with the iteration
   179 	/// ordering of the items.
   180 	///
   181 	/// \bug This is a technical requirement. Do we really need this?
   182 	bool operator<(Node) const { return false; }
   183 
   184       };
   185     
   186       /// This iterator goes through each node.
   187 
   188       /// This iterator goes through each node.
   189       /// Its usage is quite simple, for example you can count the number
   190       /// of nodes in graph \c g of type \c Graph like this:
   191       ///\code
   192       /// int count=0;
   193       /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
   194       ///\endcode
   195       class NodeIt : public Node {
   196       public:
   197         /// Default constructor
   198 
   199         /// @warning The default constructor sets the iterator
   200         /// to an undefined value.
   201         NodeIt() { }
   202         /// Copy constructor.
   203         
   204         /// Copy constructor.
   205         ///
   206         NodeIt(const NodeIt& n) : Node(n) { }
   207         /// Invalid constructor \& conversion.
   208 
   209         /// Initialize the iterator to be invalid.
   210         /// \sa Invalid for more details.
   211         NodeIt(Invalid) { }
   212         /// Sets the iterator to the first node.
   213 
   214         /// Sets the iterator to the first node of \c g.
   215         ///
   216         NodeIt(const StaticGraph&) { }
   217         /// Node -> NodeIt conversion.
   218 
   219         /// Sets the iterator to the node of \c the graph pointed by 
   220 	/// the trivial iterator.
   221         /// This feature necessitates that each time we 
   222         /// iterate the edge-set, the iteration order is the same.
   223         NodeIt(const StaticGraph&, const Node&) { }
   224         /// Next node.
   225 
   226         /// Assign the iterator to the next node.
   227         ///
   228         NodeIt& operator++() { return *this; }
   229       };
   230     
   231     
   232       /// The base type of the edge iterators.
   233 
   234       /// The base type of the edge iterators.
   235       ///
   236       class Edge {
   237       public:
   238         /// Default constructor
   239 
   240         /// @warning The default constructor sets the iterator
   241         /// to an undefined value.
   242         Edge() { }
   243         /// Copy constructor.
   244 
   245         /// Copy constructor.
   246         ///
   247         Edge(const Edge&) { }
   248         /// Initialize the iterator to be invalid.
   249 
   250         /// Initialize the iterator to be invalid.
   251         ///
   252         Edge(Invalid) { }
   253         /// Equality operator
   254 
   255         /// Two iterators are equal if and only if they point to the
   256         /// same object or both are invalid.
   257         bool operator==(Edge) const { return true; }
   258         /// Inequality operator
   259 
   260         /// \sa operator==(Edge n)
   261         ///
   262         bool operator!=(Edge) const { return true; }
   263 
   264 	/// Artificial ordering operator.
   265 	
   266 	/// To allow the use of graph descriptors as key type in std::map or
   267 	/// similar associative container we require this.
   268 	///
   269 	/// \note This operator only have to define some strict ordering of
   270 	/// the items; this order has nothing to do with the iteration
   271 	/// ordering of the items.
   272 	///
   273 	/// \bug This is a technical requirement. Do we really need this?
   274 	bool operator<(Edge) const { return false; }
   275       };
   276     
   277       /// This iterator goes trough the outgoing edges of a node.
   278 
   279       /// This iterator goes trough the \e outgoing edges of a certain node
   280       /// of a graph.
   281       /// Its usage is quite simple, for example you can count the number
   282       /// of outgoing edges of a node \c n
   283       /// in graph \c g of type \c Graph as follows.
   284       ///\code
   285       /// int count=0;
   286       /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   287       ///\endcode
   288     
   289       class OutEdgeIt : public Edge {
   290       public:
   291         /// Default constructor
   292 
   293         /// @warning The default constructor sets the iterator
   294         /// to an undefined value.
   295         OutEdgeIt() { }
   296         /// Copy constructor.
   297 
   298         /// Copy constructor.
   299         ///
   300         OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
   301         /// Initialize the iterator to be invalid.
   302 
   303         /// Initialize the iterator to be invalid.
   304         ///
   305         OutEdgeIt(Invalid) { }
   306         /// This constructor sets the iterator to the first outgoing edge.
   307     
   308         /// This constructor sets the iterator to the first outgoing edge of
   309         /// the node.
   310         OutEdgeIt(const StaticGraph&, const Node&) { }
   311         /// Edge -> OutEdgeIt conversion
   312 
   313         /// Sets the iterator to the value of the trivial iterator.
   314 	/// This feature necessitates that each time we 
   315         /// iterate the edge-set, the iteration order is the same.
   316         OutEdgeIt(const StaticGraph&, const Edge&) { }
   317         ///Next outgoing edge
   318         
   319         /// Assign the iterator to the next 
   320         /// outgoing edge of the corresponding node.
   321         OutEdgeIt& operator++() { return *this; }
   322       };
   323 
   324       /// This iterator goes trough the incoming edges of a node.
   325 
   326       /// This iterator goes trough the \e incoming edges of a certain node
   327       /// of a graph.
   328       /// Its usage is quite simple, for example you can count the number
   329       /// of outgoing edges of a node \c n
   330       /// in graph \c g of type \c Graph as follows.
   331       ///\code
   332       /// int count=0;
   333       /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   334       ///\endcode
   335 
   336       class InEdgeIt : public Edge {
   337       public:
   338         /// Default constructor
   339 
   340         /// @warning The default constructor sets the iterator
   341         /// to an undefined value.
   342         InEdgeIt() { }
   343         /// Copy constructor.
   344 
   345         /// Copy constructor.
   346         ///
   347         InEdgeIt(const InEdgeIt& e) : Edge(e) { }
   348         /// Initialize the iterator to be invalid.
   349 
   350         /// Initialize the iterator to be invalid.
   351         ///
   352         InEdgeIt(Invalid) { }
   353         /// This constructor sets the iterator to first incoming edge.
   354     
   355         /// This constructor set the iterator to the first incoming edge of
   356         /// the node.
   357         InEdgeIt(const StaticGraph&, const Node&) { }
   358         /// Edge -> InEdgeIt conversion
   359 
   360         /// Sets the iterator to the value of the trivial iterator \c e.
   361         /// This feature necessitates that each time we 
   362         /// iterate the edge-set, the iteration order is the same.
   363         InEdgeIt(const StaticGraph&, const Edge&) { }
   364         /// Next incoming edge
   365 
   366         /// Assign the iterator to the next inedge of the corresponding node.
   367         ///
   368         InEdgeIt& operator++() { return *this; }
   369       };
   370       /// This iterator goes through each edge.
   371 
   372       /// This iterator goes through each edge of a graph.
   373       /// Its usage is quite simple, for example you can count the number
   374       /// of edges in a graph \c g of type \c Graph as follows:
   375       ///\code
   376       /// int count=0;
   377       /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
   378       ///\endcode
   379       class EdgeIt : public Edge {
   380       public:
   381         /// Default constructor
   382 
   383         /// @warning The default constructor sets the iterator
   384         /// to an undefined value.
   385         EdgeIt() { }
   386         /// Copy constructor.
   387 
   388         /// Copy constructor.
   389         ///
   390         EdgeIt(const EdgeIt& e) : Edge(e) { }
   391         /// Initialize the iterator to be invalid.
   392 
   393         /// Initialize the iterator to be invalid.
   394         ///
   395         EdgeIt(Invalid) { }
   396         /// This constructor sets the iterator to the first edge.
   397     
   398         /// This constructor sets the iterator to the first edge of \c g.
   399         ///@param g the graph
   400         EdgeIt(const StaticGraph& g) { ignore_unused_variable_warning(g); }
   401         /// Edge -> EdgeIt conversion
   402 
   403         /// Sets the iterator to the value of the trivial iterator \c e.
   404         /// This feature necessitates that each time we 
   405         /// iterate the edge-set, the iteration order is the same.
   406         EdgeIt(const StaticGraph&, const Edge&) { } 
   407         ///Next edge
   408         
   409         /// Assign the iterator to the next edge.
   410         EdgeIt& operator++() { return *this; }
   411       };
   412       ///Gives back the target node of an edge.
   413 
   414       ///Gives back the target node of an edge.
   415       ///
   416       Node target(Edge) const { return INVALID; }
   417       ///Gives back the source node of an edge.
   418 
   419       ///Gives back the source node of an edge.
   420       ///
   421       Node source(Edge) const { return INVALID; }
   422 
   423 //       /// Gives back the first Node in the iterating order.
   424       
   425 //       /// Gives back the first Node in the iterating order.
   426 //       ///     
   427       void first(Node&) const {}
   428 
   429 //       /// Gives back the next Node in the iterating order.
   430       
   431 //       /// Gives back the next Node in the iterating order.
   432 //       ///     
   433       void next(Node&) const {}
   434 
   435 //       /// Gives back the first Edge in the iterating order.
   436       
   437 //       /// Gives back the first Edge in the iterating order.
   438 //       ///     
   439       void first(Edge&) const {}
   440 //       /// Gives back the next Edge in the iterating order.
   441       
   442 //       /// Gives back the next Edge in the iterating order.
   443 //       ///     
   444       void next(Edge&) const {}
   445 
   446 
   447 //       /// Gives back the first of the Edges point to the given Node.
   448       
   449 //       /// Gives back the first of the Edges point to the given Node.
   450 //       ///     
   451       void firstIn(Edge&, const Node&) const {}
   452 
   453 //       /// Gives back the next of the Edges points to the given Node.
   454 
   455 
   456 //       /// Gives back the next of the Edges points to the given Node.
   457 //       ///
   458       void nextIn(Edge&) const {}
   459 
   460 //       /// Gives back the first of the Edges start from the given Node.
   461       
   462 //       /// Gives back the first of the Edges start from the given Node.
   463 //       ///     
   464       void firstOut(Edge&, const Node&) const {}
   465 
   466 //       /// Gives back the next of the Edges start from the given Node.
   467       
   468 //       /// Gives back the next of the Edges start from the given Node.
   469 //       ///     
   470       void nextOut(Edge&) const {}
   471 
   472       /// \brief The base node of the iterator.
   473       ///
   474       /// Gives back the base node of the iterator.
   475       /// It is always the target of the pointed edge.
   476       Node baseNode(const InEdgeIt&) const { return INVALID; }
   477 
   478       /// \brief The running node of the iterator.
   479       ///
   480       /// Gives back the running node of the iterator.
   481       /// It is always the source of the pointed edge.
   482       Node runningNode(const InEdgeIt&) const { return INVALID; }
   483 
   484       /// \brief The base node of the iterator.
   485       ///
   486       /// Gives back the base node of the iterator.
   487       /// It is always the source of the pointed edge.
   488       Node baseNode(const OutEdgeIt&) const { return INVALID; }
   489 
   490       /// \brief The running node of the iterator.
   491       ///
   492       /// Gives back the running node of the iterator.
   493       /// It is always the target of the pointed edge.
   494       Node runningNode(const OutEdgeIt&) const { return INVALID; }
   495 
   496       /// \brief The opposite node on the given edge.
   497       ///
   498       /// Gives back the opposite node on the given edge.
   499       Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
   500 
   501       /// \brief Read write map of the nodes to type \c T.
   502       /// 
   503       /// ReadWrite map of the nodes to type \c T.
   504       /// \sa Reference
   505       /// \warning Making maps that can handle bool type (NodeMap<bool>)
   506       /// needs some extra attention!
   507       /// \todo Wrong documentation
   508       template<class T> 
   509       class NodeMap : public ReadWriteMap< Node, T >
   510       {
   511       public:
   512 
   513         ///\e
   514         NodeMap(const StaticGraph&) { }
   515         ///\e
   516         NodeMap(const StaticGraph&, T) { }
   517 
   518         ///Copy constructor
   519         NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
   520         ///Assignment operator
   521         NodeMap& operator=(const NodeMap&) { return *this; }
   522         // \todo fix this concept
   523       };
   524 
   525       /// \brief Read write map of the edges to type \c T.
   526       ///
   527       /// Reference map of the edges to type \c T.
   528       /// \sa Reference
   529       /// \warning Making maps that can handle bool type (EdgeMap<bool>)
   530       /// needs some extra attention!
   531       /// \todo Wrong documentation
   532       template<class T> 
   533       class EdgeMap : public ReadWriteMap<Edge,T>
   534       {
   535       public:
   536 
   537         ///\e
   538         EdgeMap(const StaticGraph&) { }
   539         ///\e
   540         EdgeMap(const StaticGraph&, T) { }
   541         ///Copy constructor
   542         EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
   543         ///Assignment operator
   544         EdgeMap& operator=(const EdgeMap&) { return *this; }
   545         // \todo fix this concept    
   546       };
   547 
   548       template <typename _Graph>
   549       struct Constraints : public _StaticGraph::Constraints<_Graph> {};
   550 
   551     };
   552 
   553     /// An empty non-static graph class.
   554     
   555     /// This class provides everything that \ref StaticGraph does.
   556     /// Additionally it enables building graphs from scratch.
   557     class ExtendableGraph : public StaticGraph
   558     {
   559     public:
   560       /// Defalult constructor.
   561 
   562       /// Defalult constructor.
   563       ///
   564       ExtendableGraph() { }
   565       ///Add a new node to the graph.
   566 
   567       /// \return the new node.
   568       ///
   569       Node addNode() { return INVALID; }
   570       ///Add a new edge to the graph.
   571 
   572       ///Add a new edge to the graph with source node \c s
   573       ///and target node \c t.
   574       ///\return the new edge.
   575       Edge addEdge(Node, Node) { return INVALID; }
   576     
   577       /// Resets the graph.
   578 
   579       /// This function deletes all edges and nodes of the graph.
   580       /// It also frees the memory allocated to store them.
   581       /// \todo It might belong to \ref ErasableGraph.
   582       void clear() { }
   583 
   584       template <typename _Graph>
   585       struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
   586 
   587     };
   588 
   589     /// An empty erasable graph class.
   590   
   591     /// This class is an extension of \ref ExtendableGraph. It makes it
   592     /// possible to erase edges or nodes.
   593     class ErasableGraph : public ExtendableGraph
   594     {
   595     public:
   596       /// Defalult constructor.
   597 
   598       /// Defalult constructor.
   599       ///
   600       ErasableGraph() { }
   601       /// Deletes a node.
   602 
   603       /// Deletes node \c n node.
   604       ///
   605       void erase(Node) { }
   606       /// Deletes an edge.
   607 
   608       /// Deletes edge \c e edge.
   609       ///
   610       void erase(Edge) { }
   611 
   612       template <typename _Graph>
   613       struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
   614 
   615     };
   616     
   617     // @}
   618   } //namespace concept  
   619 } //namespace lemon
   620 
   621 
   622 
   623 #endif // LEMON_CONCEPT_GRAPH_H