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