lemon/concept/graph.h
author alpar
Wed, 04 Jan 2006 13:31:59 +0000
changeset 1875 98698b69a902
parent 1760 f18e8ca73a8f
child 1909 2d806130e700
permissions -rw-r--r--
Happy new year to LEMON
     1 /* -*- C++ -*-
     2  * lemon/concept/graph.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2006 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 
    45       typedef False UndirTag;
    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       ///\todo undocumented
   125       ///
   126       typedef False UndirTag;
   127 
   128       /// Defalult constructor.
   129 
   130       /// Defalult constructor.
   131       ///
   132       StaticGraph() { }
   133       ///Copy consructor.
   134 
   135 //       ///\todo It is not clear, what we expect from a copy constructor.
   136 //       ///E.g. How to assign the nodes/edges to each other? What about maps?
   137 //       StaticGraph(const StaticGraph& g) { }
   138 
   139       /// The base type of node iterators, 
   140       /// or in other words, the trivial node iterator.
   141 
   142       /// This is the base type of each node iterator,
   143       /// thus each kind of node iterator converts to this.
   144       /// More precisely each kind of node iterator should be inherited 
   145       /// from the trivial node iterator.
   146       class Node {
   147       public:
   148         /// Default constructor
   149 
   150         /// @warning The default constructor sets the iterator
   151         /// to an undefined value.
   152         Node() { }
   153         /// Copy constructor.
   154 
   155         /// Copy constructor.
   156         ///
   157         Node(const Node&) { }
   158 
   159         /// Invalid constructor \& conversion.
   160 
   161         /// This constructor initializes the iterator to be invalid.
   162         /// \sa Invalid for more details.
   163         Node(Invalid) { }
   164         /// Equality operator
   165 
   166         /// Two iterators are equal if and only if they point to the
   167         /// same object or both are invalid.
   168         bool operator==(Node) const { return true; }
   169 
   170         /// Inequality operator
   171         
   172         /// \sa operator==(Node n)
   173         ///
   174         bool operator!=(Node) const { return true; }
   175 
   176 	/// Artificial ordering operator.
   177 	
   178 	/// To allow the use of graph descriptors as key type in std::map or
   179 	/// similar associative container we require this.
   180 	///
   181 	/// \note This operator only have to define some strict ordering of
   182 	/// the items; this order has nothing to do with the iteration
   183 	/// ordering of the items.
   184 	///
   185 	/// \bug This is a technical requirement. Do we really need this?
   186 	bool operator<(Node) const { return false; }
   187 
   188       };
   189     
   190       /// This iterator goes through each node.
   191 
   192       /// This iterator goes through each node.
   193       /// Its usage is quite simple, for example you can count the number
   194       /// of nodes in graph \c g of type \c Graph like this:
   195       /// \code
   196       /// int count=0;
   197       /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
   198       /// \endcode
   199       class NodeIt : public Node {
   200       public:
   201         /// Default constructor
   202 
   203         /// @warning The default constructor sets the iterator
   204         /// to an undefined value.
   205         NodeIt() { }
   206         /// Copy constructor.
   207         
   208         /// Copy constructor.
   209         ///
   210         NodeIt(const NodeIt& n) : Node(n) { }
   211         /// Invalid constructor \& conversion.
   212 
   213         /// Initialize the iterator to be invalid.
   214         /// \sa Invalid for more details.
   215         NodeIt(Invalid) { }
   216         /// Sets the iterator to the first node.
   217 
   218         /// Sets the iterator to the first node of \c g.
   219         ///
   220         NodeIt(const StaticGraph&) { }
   221         /// Node -> NodeIt conversion.
   222 
   223         /// Sets the iterator to the node of \c the graph pointed by 
   224 	/// the trivial iterator.
   225         /// This feature necessitates that each time we 
   226         /// iterate the edge-set, the iteration order is the same.
   227         NodeIt(const StaticGraph&, const Node&) { }
   228         /// Next node.
   229 
   230         /// Assign the iterator to the next node.
   231         ///
   232         NodeIt& operator++() { return *this; }
   233       };
   234     
   235     
   236       /// The base type of the edge iterators.
   237 
   238       /// The base type of the edge iterators.
   239       ///
   240       class Edge {
   241       public:
   242         /// Default constructor
   243 
   244         /// @warning The default constructor sets the iterator
   245         /// to an undefined value.
   246         Edge() { }
   247         /// Copy constructor.
   248 
   249         /// Copy constructor.
   250         ///
   251         Edge(const Edge&) { }
   252         /// Initialize the iterator to be invalid.
   253 
   254         /// Initialize the iterator to be invalid.
   255         ///
   256         Edge(Invalid) { }
   257         /// Equality operator
   258 
   259         /// Two iterators are equal if and only if they point to the
   260         /// same object or both are invalid.
   261         bool operator==(Edge) const { return true; }
   262         /// Inequality operator
   263 
   264         /// \sa operator==(Edge n)
   265         ///
   266         bool operator!=(Edge) const { return true; }
   267 
   268 	/// Artificial ordering operator.
   269 	
   270 	/// To allow the use of graph descriptors as key type in std::map or
   271 	/// similar associative container we require this.
   272 	///
   273 	/// \note This operator only have to define some strict ordering of
   274 	/// the items; this order has nothing to do with the iteration
   275 	/// ordering of the items.
   276 	///
   277 	/// \bug This is a technical requirement. Do we really need this?
   278 	bool operator<(Edge) const { return false; }
   279       };
   280     
   281       /// This iterator goes trough the outgoing edges of a node.
   282 
   283       /// This iterator goes trough the \e outgoing edges of a certain node
   284       /// of a graph.
   285       /// Its usage is quite simple, for example you can count the number
   286       /// of outgoing edges of a node \c n
   287       /// in graph \c g of type \c Graph as follows.
   288       /// \code
   289       /// int count=0;
   290       /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   291       /// \endcode
   292     
   293       class OutEdgeIt : public Edge {
   294       public:
   295         /// Default constructor
   296 
   297         /// @warning The default constructor sets the iterator
   298         /// to an undefined value.
   299         OutEdgeIt() { }
   300         /// Copy constructor.
   301 
   302         /// Copy constructor.
   303         ///
   304         OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
   305         /// Initialize the iterator to be invalid.
   306 
   307         /// Initialize the iterator to be invalid.
   308         ///
   309         OutEdgeIt(Invalid) { }
   310         /// This constructor sets the iterator to the first outgoing edge.
   311     
   312         /// This constructor sets the iterator to the first outgoing edge of
   313         /// the node.
   314         OutEdgeIt(const StaticGraph&, const Node&) { }
   315         /// Edge -> OutEdgeIt conversion
   316 
   317         /// Sets the iterator to the value of the trivial iterator.
   318 	/// This feature necessitates that each time we 
   319         /// iterate the edge-set, the iteration order is the same.
   320         OutEdgeIt(const StaticGraph&, const Edge&) { }
   321         ///Next outgoing edge
   322         
   323         /// Assign the iterator to the next 
   324         /// outgoing edge of the corresponding node.
   325         OutEdgeIt& operator++() { return *this; }
   326       };
   327 
   328       /// This iterator goes trough the incoming edges of a node.
   329 
   330       /// This iterator goes trough the \e incoming edges of a certain node
   331       /// of a graph.
   332       /// Its usage is quite simple, for example you can count the number
   333       /// of outgoing edges of a node \c n
   334       /// in graph \c g of type \c Graph as follows.
   335       /// \code
   336       /// int count=0;
   337       /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   338       /// \endcode
   339 
   340       class InEdgeIt : public Edge {
   341       public:
   342         /// Default constructor
   343 
   344         /// @warning The default constructor sets the iterator
   345         /// to an undefined value.
   346         InEdgeIt() { }
   347         /// Copy constructor.
   348 
   349         /// Copy constructor.
   350         ///
   351         InEdgeIt(const InEdgeIt& e) : Edge(e) { }
   352         /// Initialize the iterator to be invalid.
   353 
   354         /// Initialize the iterator to be invalid.
   355         ///
   356         InEdgeIt(Invalid) { }
   357         /// This constructor sets the iterator to first incoming edge.
   358     
   359         /// This constructor set the iterator to the first incoming edge of
   360         /// the node.
   361         InEdgeIt(const StaticGraph&, const Node&) { }
   362         /// Edge -> InEdgeIt conversion
   363 
   364         /// Sets the iterator to the value of the trivial iterator \c e.
   365         /// This feature necessitates that each time we 
   366         /// iterate the edge-set, the iteration order is the same.
   367         InEdgeIt(const StaticGraph&, const Edge&) { }
   368         /// Next incoming edge
   369 
   370         /// Assign the iterator to the next inedge of the corresponding node.
   371         ///
   372         InEdgeIt& operator++() { return *this; }
   373       };
   374       /// This iterator goes through each edge.
   375 
   376       /// This iterator goes through each edge of a graph.
   377       /// Its usage is quite simple, for example you can count the number
   378       /// of edges in a graph \c g of type \c Graph as follows:
   379       /// \code
   380       /// int count=0;
   381       /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
   382       /// \endcode
   383       class EdgeIt : public Edge {
   384       public:
   385         /// Default constructor
   386 
   387         /// @warning The default constructor sets the iterator
   388         /// to an undefined value.
   389         EdgeIt() { }
   390         /// Copy constructor.
   391 
   392         /// Copy constructor.
   393         ///
   394         EdgeIt(const EdgeIt& e) : Edge(e) { }
   395         /// Initialize the iterator to be invalid.
   396 
   397         /// Initialize the iterator to be invalid.
   398         ///
   399         EdgeIt(Invalid) { }
   400         /// This constructor sets the iterator to the first edge.
   401     
   402         /// This constructor sets the iterator to the first edge of \c g.
   403         ///@param g the graph
   404         EdgeIt(const StaticGraph& g) { ignore_unused_variable_warning(g); }
   405         /// Edge -> EdgeIt conversion
   406 
   407         /// Sets the iterator to the value of the trivial iterator \c e.
   408         /// This feature necessitates that each time we 
   409         /// iterate the edge-set, the iteration order is the same.
   410         EdgeIt(const StaticGraph&, const Edge&) { } 
   411         ///Next edge
   412         
   413         /// Assign the iterator to the next edge.
   414         EdgeIt& operator++() { return *this; }
   415       };
   416       ///Gives back the target node of an edge.
   417 
   418       ///Gives back the target node of an edge.
   419       ///
   420       Node target(Edge) const { return INVALID; }
   421       ///Gives back the source node of an edge.
   422 
   423       ///Gives back the source node of an edge.
   424       ///
   425       Node source(Edge) const { return INVALID; }
   426 
   427 //       /// Gives back the first Node in the iterating order.
   428       
   429 //       /// Gives back the first Node in the iterating order.
   430 //       ///     
   431       void first(Node&) const {}
   432 
   433 //       /// Gives back the next Node in the iterating order.
   434       
   435 //       /// Gives back the next Node in the iterating order.
   436 //       ///     
   437       void next(Node&) const {}
   438 
   439 //       /// Gives back the first Edge in the iterating order.
   440       
   441 //       /// Gives back the first Edge in the iterating order.
   442 //       ///     
   443       void first(Edge&) const {}
   444 //       /// Gives back the next Edge in the iterating order.
   445       
   446 //       /// Gives back the next Edge in the iterating order.
   447 //       ///     
   448       void next(Edge&) const {}
   449 
   450 
   451 //       /// Gives back the first of the Edges point to the given Node.
   452       
   453 //       /// Gives back the first of the Edges point to the given Node.
   454 //       ///     
   455       void firstIn(Edge&, const Node&) const {}
   456 
   457 //       /// Gives back the next of the Edges points to the given Node.
   458 
   459 
   460 //       /// Gives back the next of the Edges points to the given Node.
   461 //       ///
   462       void nextIn(Edge&) const {}
   463 
   464 //       /// Gives back the first of the Edges start from the given Node.
   465       
   466 //       /// Gives back the first of the Edges start from the given Node.
   467 //       ///     
   468       void firstOut(Edge&, const Node&) const {}
   469 
   470 //       /// Gives back the next of the Edges start from the given Node.
   471       
   472 //       /// Gives back the next of the Edges start from the given Node.
   473 //       ///     
   474       void nextOut(Edge&) const {}
   475 
   476       /// \brief The base node of the iterator.
   477       ///
   478       /// Gives back the base node of the iterator.
   479       /// It is always the target of the pointed edge.
   480       Node baseNode(const InEdgeIt&) const { return INVALID; }
   481 
   482       /// \brief The running node of the iterator.
   483       ///
   484       /// Gives back the running node of the iterator.
   485       /// It is always the source of the pointed edge.
   486       Node runningNode(const InEdgeIt&) const { return INVALID; }
   487 
   488       /// \brief The base node of the iterator.
   489       ///
   490       /// Gives back the base node of the iterator.
   491       /// It is always the source of the pointed edge.
   492       Node baseNode(const OutEdgeIt&) const { return INVALID; }
   493 
   494       /// \brief The running node of the iterator.
   495       ///
   496       /// Gives back the running node of the iterator.
   497       /// It is always the target of the pointed edge.
   498       Node runningNode(const OutEdgeIt&) const { return INVALID; }
   499 
   500       /// \brief The opposite node on the given edge.
   501       ///
   502       /// Gives back the opposite node on the given edge.
   503       Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
   504 
   505       /// \brief Read write map of the nodes to type \c T.
   506       /// 
   507       /// ReadWrite map of the nodes to type \c T.
   508       /// \sa Reference
   509       /// \warning Making maps that can handle bool type (NodeMap<bool>)
   510       /// needs some extra attention!
   511       /// \todo Wrong documentation
   512       template<class T> 
   513       class NodeMap : public ReadWriteMap< Node, T >
   514       {
   515       public:
   516 
   517         ///\e
   518         NodeMap(const StaticGraph&) { }
   519         ///\e
   520         NodeMap(const StaticGraph&, T) { }
   521 
   522         ///Copy constructor
   523         NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
   524         ///Assignment operator
   525         NodeMap& operator=(const NodeMap&) { return *this; }
   526         // \todo fix this concept
   527       };
   528 
   529       /// \brief Read write map of the edges to type \c T.
   530       ///
   531       /// Reference map of the edges to type \c T.
   532       /// \sa Reference
   533       /// \warning Making maps that can handle bool type (EdgeMap<bool>)
   534       /// needs some extra attention!
   535       /// \todo Wrong documentation
   536       template<class T> 
   537       class EdgeMap : public ReadWriteMap<Edge,T>
   538       {
   539       public:
   540 
   541         ///\e
   542         EdgeMap(const StaticGraph&) { }
   543         ///\e
   544         EdgeMap(const StaticGraph&, T) { }
   545         ///Copy constructor
   546         EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
   547         ///Assignment operator
   548         EdgeMap& operator=(const EdgeMap&) { return *this; }
   549         // \todo fix this concept    
   550       };
   551 
   552       template <typename _Graph>
   553       struct Constraints : public _StaticGraph::Constraints<_Graph> {};
   554 
   555     };
   556 
   557     /// An empty non-static graph class.
   558     
   559     /// This class provides everything that \ref StaticGraph does.
   560     /// Additionally it enables building graphs from scratch.
   561     class ExtendableGraph : public StaticGraph
   562     {
   563     public:
   564       /// Defalult constructor.
   565 
   566       /// Defalult constructor.
   567       ///
   568       ExtendableGraph() { }
   569       ///Add a new node to the graph.
   570 
   571       /// \return the new node.
   572       ///
   573       Node addNode() { return INVALID; }
   574       ///Add a new edge to the graph.
   575 
   576       ///Add a new edge to the graph with source node \c s
   577       ///and target node \c t.
   578       ///\return the new edge.
   579       Edge addEdge(Node, Node) { return INVALID; }
   580     
   581       /// Resets the graph.
   582 
   583       /// This function deletes all edges and nodes of the graph.
   584       /// It also frees the memory allocated to store them.
   585       /// \todo It might belong to \ref ErasableGraph.
   586       void clear() { }
   587 
   588       template <typename _Graph>
   589       struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
   590 
   591     };
   592 
   593     /// An empty erasable graph class.
   594   
   595     /// This class is an extension of \ref ExtendableGraph. It makes it
   596     /// possible to erase edges or nodes.
   597     class ErasableGraph : public ExtendableGraph
   598     {
   599     public:
   600       /// Defalult constructor.
   601 
   602       /// Defalult constructor.
   603       ///
   604       ErasableGraph() { }
   605       /// Deletes a node.
   606 
   607       /// Deletes node \c n node.
   608       ///
   609       void erase(Node) { }
   610       /// Deletes an edge.
   611 
   612       /// Deletes edge \c e edge.
   613       ///
   614       void erase(Edge) { }
   615 
   616       template <typename _Graph>
   617       struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
   618 
   619     };
   620     
   621     // @}
   622   } //namespace concept  
   623 } //namespace lemon
   624 
   625 
   626 
   627 #endif // LEMON_CONCEPT_GRAPH_H