lemon/concept/ugraph.h
author alpar
Wed, 12 Jul 2006 11:05:00 +0000
changeset 2132 783b1d583be3
parent 2121 09a07a851506
child 2163 bef3457be038
permissions -rw-r--r--
(Hopefully) finish privatizing the copy constr. and operator= in
the graph concept, ListGraph and SmartGraph.
     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 ///\ingroup graph_concepts
    20 ///\file
    21 ///\brief The concept of the undirected graphs.
    22 
    23 
    24 #ifndef LEMON_CONCEPT_UGRAPH_H
    25 #define LEMON_CONCEPT_UGRAPH_H
    26 
    27 #include <lemon/concept/graph_components.h>
    28 #include <lemon/concept/graph.h>
    29 #include <lemon/bits/utility.h>
    30 
    31 namespace lemon {
    32   namespace concept {
    33 
    34     /// \addtogroup graph_concepts
    35     /// @{
    36 
    37 
    38     /// Class describing the concept of Undirected Graphs.
    39 
    40     /// This class describes the common interface of all Undirected
    41     /// Graphs.
    42     ///
    43     /// As all concept describing classes it provides only interface
    44     /// without any sensible implementation. So any algorithm for
    45     /// undirected graph should compile with this class, but it will not
    46     /// run properly, of couse.
    47     ///
    48     /// In LEMON undirected graphs also fulfill the concept of directed
    49     /// graphs (\ref lemon::concept::Graph "Graph Concept"). For
    50     /// explanation of this and more see also the page \ref graphs,
    51     /// a tutorial about graphs.
    52     ///
    53     /// You can assume that all undirected graph can be handled
    54     /// as a directed graph. This way it is fully conform
    55     /// to the Graph concept.
    56 
    57     class UGraph {
    58     public:
    59       ///\e
    60 
    61       ///\todo undocumented
    62       ///
    63       typedef True UndirectedTag;
    64 
    65       /// \brief The base type of node iterators, 
    66       /// or in other words, the trivial node iterator.
    67       ///
    68       /// This is the base type of each node iterator,
    69       /// thus each kind of node iterator converts to this.
    70       /// More precisely each kind of node iterator should be inherited 
    71       /// from the trivial node iterator.
    72       class Node {
    73       public:
    74         /// Default constructor
    75 
    76         /// @warning The default constructor sets the iterator
    77         /// to an undefined value.
    78         Node() { }
    79         /// Copy constructor.
    80 
    81         /// Copy constructor.
    82         ///
    83         Node(const Node&) { }
    84 
    85         /// Invalid constructor \& conversion.
    86 
    87         /// This constructor initializes the iterator to be invalid.
    88         /// \sa Invalid for more details.
    89         Node(Invalid) { }
    90         /// Equality operator
    91 
    92         /// Two iterators are equal if and only if they point to the
    93         /// same object or both are invalid.
    94         bool operator==(Node) const { return true; }
    95 
    96         /// Inequality operator
    97         
    98         /// \sa operator==(Node n)
    99         ///
   100         bool operator!=(Node) const { return true; }
   101 
   102 	/// Artificial ordering operator.
   103 	
   104 	/// To allow the use of graph descriptors as key type in std::map or
   105 	/// similar associative container we require this.
   106 	///
   107 	/// \note This operator only have to define some strict ordering of
   108 	/// the items; this order has nothing to do with the iteration
   109 	/// ordering of the items.
   110 	bool operator<(Node) const { return false; }
   111 
   112       };
   113     
   114       /// This iterator goes through each node.
   115 
   116       /// This iterator goes through each node.
   117       /// Its usage is quite simple, for example you can count the number
   118       /// of nodes in graph \c g of type \c Graph like this:
   119       ///\code
   120       /// int count=0;
   121       /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
   122       ///\endcode
   123       class NodeIt : public Node {
   124       public:
   125         /// Default constructor
   126 
   127         /// @warning The default constructor sets the iterator
   128         /// to an undefined value.
   129         NodeIt() { }
   130         /// Copy constructor.
   131         
   132         /// Copy constructor.
   133         ///
   134         NodeIt(const NodeIt& n) : Node(n) { }
   135         /// Invalid constructor \& conversion.
   136 
   137         /// Initialize the iterator to be invalid.
   138         /// \sa Invalid for more details.
   139         NodeIt(Invalid) { }
   140         /// Sets the iterator to the first node.
   141 
   142         /// Sets the iterator to the first node of \c g.
   143         ///
   144         NodeIt(const UGraph&) { }
   145         /// Node -> NodeIt conversion.
   146 
   147         /// Sets the iterator to the node of \c the graph pointed by 
   148 	/// the trivial iterator.
   149         /// This feature necessitates that each time we 
   150         /// iterate the edge-set, the iteration order is the same.
   151         NodeIt(const UGraph&, const Node&) { }
   152         /// Next node.
   153 
   154         /// Assign the iterator to the next node.
   155         ///
   156         NodeIt& operator++() { return *this; }
   157       };
   158     
   159     
   160       /// The base type of the undirected edge iterators.
   161 
   162       /// The base type of the undirected edge iterators.
   163       ///
   164       class UEdge {
   165       public:
   166         /// Default constructor
   167 
   168         /// @warning The default constructor sets the iterator
   169         /// to an undefined value.
   170         UEdge() { }
   171         /// Copy constructor.
   172 
   173         /// Copy constructor.
   174         ///
   175         UEdge(const UEdge&) { }
   176         /// Initialize the iterator to be invalid.
   177 
   178         /// Initialize the iterator to be invalid.
   179         ///
   180         UEdge(Invalid) { }
   181         /// Equality operator
   182 
   183         /// Two iterators are equal if and only if they point to the
   184         /// same object or both are invalid.
   185         bool operator==(UEdge) const { return true; }
   186         /// Inequality operator
   187 
   188         /// \sa operator==(UEdge n)
   189         ///
   190         bool operator!=(UEdge) const { return true; }
   191 
   192 	/// Artificial ordering operator.
   193 	
   194 	/// To allow the use of graph descriptors as key type in std::map or
   195 	/// similar associative container we require this.
   196 	///
   197 	/// \note This operator only have to define some strict ordering of
   198 	/// the items; this order has nothing to do with the iteration
   199 	/// ordering of the items.
   200 	bool operator<(UEdge) const { return false; }
   201       };
   202 
   203       /// This iterator goes through each undirected edge.
   204 
   205       /// This iterator goes through each undirected edge of a graph.
   206       /// Its usage is quite simple, for example you can count the number
   207       /// of undirected edges in a graph \c g of type \c Graph as follows:
   208       ///\code
   209       /// int count=0;
   210       /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
   211       ///\endcode
   212       class UEdgeIt : public UEdge {
   213       public:
   214         /// Default constructor
   215 
   216         /// @warning The default constructor sets the iterator
   217         /// to an undefined value.
   218         UEdgeIt() { }
   219         /// Copy constructor.
   220 
   221         /// Copy constructor.
   222         ///
   223         UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
   224         /// Initialize the iterator to be invalid.
   225 
   226         /// Initialize the iterator to be invalid.
   227         ///
   228         UEdgeIt(Invalid) { }
   229         /// This constructor sets the iterator to the first undirected edge.
   230     
   231         /// This constructor sets the iterator to the first undirected edge.
   232         UEdgeIt(const UGraph&) { }
   233         /// UEdge -> UEdgeIt conversion
   234 
   235         /// Sets the iterator to the value of the trivial iterator.
   236         /// This feature necessitates that each time we
   237         /// iterate the undirected edge-set, the iteration order is the 
   238 	/// same.
   239         UEdgeIt(const UGraph&, const UEdge&) { } 
   240         /// Next undirected edge
   241         
   242         /// Assign the iterator to the next undirected edge.
   243         UEdgeIt& operator++() { return *this; }
   244       };
   245 
   246       /// \brief This iterator goes trough the incident undirected 
   247       /// edges of a node.
   248       ///
   249       /// This iterator goes trough the incident undirected edges
   250       /// of a certain node of a graph. You should assume that the 
   251       /// loop edges will be iterated twice.
   252       /// 
   253       /// Its usage is quite simple, for example you can compute the
   254       /// degree (i.e. count the number of incident edges of a node \c n
   255       /// in graph \c g of type \c Graph as follows. 
   256       ///
   257       ///\code
   258       /// int count=0;
   259       /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   260       ///\endcode
   261       class IncEdgeIt : public UEdge {
   262       public:
   263         /// Default constructor
   264 
   265         /// @warning The default constructor sets the iterator
   266         /// to an undefined value.
   267         IncEdgeIt() { }
   268         /// Copy constructor.
   269 
   270         /// Copy constructor.
   271         ///
   272         IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
   273         /// Initialize the iterator to be invalid.
   274 
   275         /// Initialize the iterator to be invalid.
   276         ///
   277         IncEdgeIt(Invalid) { }
   278         /// This constructor sets the iterator to first incident edge.
   279     
   280         /// This constructor set the iterator to the first incident edge of
   281         /// the node.
   282         IncEdgeIt(const UGraph&, const Node&) { }
   283         /// UEdge -> IncEdgeIt conversion
   284 
   285         /// Sets the iterator to the value of the trivial iterator \c e.
   286         /// This feature necessitates that each time we 
   287         /// iterate the edge-set, the iteration order is the same.
   288         IncEdgeIt(const UGraph&, const UEdge&) { }
   289         /// Next incident edge
   290 
   291         /// Assign the iterator to the next incident edge
   292 	/// of the corresponding node.
   293         IncEdgeIt& operator++() { return *this; }
   294       };
   295 
   296       /// The directed edge type.
   297 
   298       /// The directed edge type. It can be converted to the
   299       /// undirected edge.
   300       class Edge : public UEdge {
   301       public:
   302         /// Default constructor
   303 
   304         /// @warning The default constructor sets the iterator
   305         /// to an undefined value.
   306         Edge() { }
   307         /// Copy constructor.
   308 
   309         /// Copy constructor.
   310         ///
   311         Edge(const Edge& e) : UEdge(e) { }
   312         /// Initialize the iterator to be invalid.
   313 
   314         /// Initialize the iterator to be invalid.
   315         ///
   316         Edge(Invalid) { }
   317         /// Equality operator
   318 
   319         /// Two iterators are equal if and only if they point to the
   320         /// same object or both are invalid.
   321         bool operator==(Edge) const { return true; }
   322         /// Inequality operator
   323 
   324         /// \sa operator==(Edge n)
   325         ///
   326         bool operator!=(Edge) const { return true; }
   327 
   328 	/// Artificial ordering operator.
   329 	
   330 	/// To allow the use of graph descriptors as key type in std::map or
   331 	/// similar associative container we require this.
   332 	///
   333 	/// \note This operator only have to define some strict ordering of
   334 	/// the items; this order has nothing to do with the iteration
   335 	/// ordering of the items.
   336 	bool operator<(Edge) const { return false; }
   337 	
   338       }; 
   339       /// This iterator goes through each directed edge.
   340 
   341       /// This iterator goes through each edge of a graph.
   342       /// Its usage is quite simple, for example you can count the number
   343       /// of edges in a graph \c g of type \c Graph as follows:
   344       ///\code
   345       /// int count=0;
   346       /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
   347       ///\endcode
   348       class EdgeIt : public Edge {
   349       public:
   350         /// Default constructor
   351 
   352         /// @warning The default constructor sets the iterator
   353         /// to an undefined value.
   354         EdgeIt() { }
   355         /// Copy constructor.
   356 
   357         /// Copy constructor.
   358         ///
   359         EdgeIt(const EdgeIt& e) : Edge(e) { }
   360         /// Initialize the iterator to be invalid.
   361 
   362         /// Initialize the iterator to be invalid.
   363         ///
   364         EdgeIt(Invalid) { }
   365         /// This constructor sets the iterator to the first edge.
   366     
   367         /// This constructor sets the iterator to the first edge of \c g.
   368         ///@param g the graph
   369         EdgeIt(const UGraph &g) { ignore_unused_variable_warning(g); }
   370         /// Edge -> EdgeIt conversion
   371 
   372         /// Sets the iterator to the value of the trivial iterator \c e.
   373         /// This feature necessitates that each time we 
   374         /// iterate the edge-set, the iteration order is the same.
   375         EdgeIt(const UGraph&, const Edge&) { } 
   376         ///Next edge
   377         
   378         /// Assign the iterator to the next edge.
   379         EdgeIt& operator++() { return *this; }
   380       };
   381    
   382       /// This iterator goes trough the outgoing directed edges of a node.
   383 
   384       /// This iterator goes trough the \e outgoing edges of a certain node
   385       /// of a graph.
   386       /// Its usage is quite simple, for example you can count the number
   387       /// of outgoing edges of a node \c n
   388       /// in graph \c g of type \c Graph as follows.
   389       ///\code
   390       /// int count=0;
   391       /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   392       ///\endcode
   393     
   394       class OutEdgeIt : public Edge {
   395       public:
   396         /// Default constructor
   397 
   398         /// @warning The default constructor sets the iterator
   399         /// to an undefined value.
   400         OutEdgeIt() { }
   401         /// Copy constructor.
   402 
   403         /// Copy constructor.
   404         ///
   405         OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
   406         /// Initialize the iterator to be invalid.
   407 
   408         /// Initialize the iterator to be invalid.
   409         ///
   410         OutEdgeIt(Invalid) { }
   411         /// This constructor sets the iterator to the first outgoing edge.
   412     
   413         /// This constructor sets the iterator to the first outgoing edge of
   414         /// the node.
   415         ///@param n the node
   416         ///@param g the graph
   417         OutEdgeIt(const UGraph& n, const Node& g) {
   418 	  ignore_unused_variable_warning(n);
   419 	  ignore_unused_variable_warning(g);
   420 	}
   421         /// Edge -> OutEdgeIt conversion
   422 
   423         /// Sets the iterator to the value of the trivial iterator.
   424 	/// This feature necessitates that each time we 
   425         /// iterate the edge-set, the iteration order is the same.
   426         OutEdgeIt(const UGraph&, const Edge&) { }
   427         ///Next outgoing edge
   428         
   429         /// Assign the iterator to the next 
   430         /// outgoing edge of the corresponding node.
   431         OutEdgeIt& operator++() { return *this; }
   432       };
   433 
   434       /// This iterator goes trough the incoming directed edges of a node.
   435 
   436       /// This iterator goes trough the \e incoming edges of a certain node
   437       /// of a graph.
   438       /// Its usage is quite simple, for example you can count the number
   439       /// of outgoing edges of a node \c n
   440       /// in graph \c g of type \c Graph as follows.
   441       ///\code
   442       /// int count=0;
   443       /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   444       ///\endcode
   445 
   446       class InEdgeIt : public Edge {
   447       public:
   448         /// Default constructor
   449 
   450         /// @warning The default constructor sets the iterator
   451         /// to an undefined value.
   452         InEdgeIt() { }
   453         /// Copy constructor.
   454 
   455         /// Copy constructor.
   456         ///
   457         InEdgeIt(const InEdgeIt& e) : Edge(e) { }
   458         /// Initialize the iterator to be invalid.
   459 
   460         /// Initialize the iterator to be invalid.
   461         ///
   462         InEdgeIt(Invalid) { }
   463         /// This constructor sets the iterator to first incoming edge.
   464     
   465         /// This constructor set the iterator to the first incoming edge of
   466         /// the node.
   467         ///@param n the node
   468         ///@param g the graph
   469         InEdgeIt(const UGraph& g, const Node& n) { 
   470 	  ignore_unused_variable_warning(n);
   471 	  ignore_unused_variable_warning(g);
   472 	}
   473         /// Edge -> InEdgeIt conversion
   474 
   475         /// Sets the iterator to the value of the trivial iterator \c e.
   476         /// This feature necessitates that each time we 
   477         /// iterate the edge-set, the iteration order is the same.
   478         InEdgeIt(const UGraph&, const Edge&) { }
   479         /// Next incoming edge
   480 
   481         /// Assign the iterator to the next inedge of the corresponding node.
   482         ///
   483         InEdgeIt& operator++() { return *this; }
   484       };
   485 
   486       /// \brief Read write map of the nodes to type \c T.
   487       /// 
   488       /// ReadWrite map of the nodes to type \c T.
   489       /// \sa Reference
   490       /// \warning Making maps that can handle bool type (NodeMap<bool>)
   491       /// needs some extra attention!
   492       template<class T> 
   493       class NodeMap : public ReadWriteMap< Node, T >
   494       {
   495       public:
   496 
   497         ///\e
   498         NodeMap(const UGraph&) { }
   499         ///\e
   500         NodeMap(const UGraph&, T) { }
   501 
   502         ///Copy constructor
   503         NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
   504         ///Assignment operator
   505         template <typename CMap>
   506         NodeMap& operator=(const CMap&) { 
   507           checkConcept<ReadMap<Node, T>, CMap>();
   508           return *this; 
   509         }
   510       };
   511 
   512       /// \brief Read write map of the directed edges to type \c T.
   513       ///
   514       /// Reference map of the directed edges to type \c T.
   515       /// \sa Reference
   516       /// \warning Making maps that can handle bool type (EdgeMap<bool>)
   517       /// needs some extra attention!
   518       template<class T> 
   519       class EdgeMap : public ReadWriteMap<Edge,T>
   520       {
   521       public:
   522 
   523         ///\e
   524         EdgeMap(const UGraph&) { }
   525         ///\e
   526         EdgeMap(const UGraph&, T) { }
   527         ///Copy constructor
   528         EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
   529         ///Assignment operator
   530         template <typename CMap>
   531         EdgeMap& operator=(const CMap&) { 
   532           checkConcept<ReadMap<Edge, T>, CMap>();
   533           return *this; 
   534         }
   535       };
   536 
   537       /// Read write map of the undirected edges to type \c T.
   538 
   539       /// Reference map of the edges to type \c T.
   540       /// \sa Reference
   541       /// \warning Making maps that can handle bool type (UEdgeMap<bool>)
   542       /// needs some extra attention!
   543       template<class T> 
   544       class UEdgeMap : public ReadWriteMap<UEdge,T>
   545       {
   546       public:
   547 
   548         ///\e
   549         UEdgeMap(const UGraph&) { }
   550         ///\e
   551         UEdgeMap(const UGraph&, T) { }
   552         ///Copy constructor
   553         UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
   554         ///Assignment operator
   555         template <typename CMap>
   556         UEdgeMap& operator=(const CMap&) { 
   557           checkConcept<ReadMap<UEdge, T>, CMap>();
   558           return *this; 
   559         }
   560       };
   561 
   562       /// \brief Direct the given undirected edge.
   563       ///
   564       /// Direct the given undirected edge. The returned edge source
   565       /// will be the given edge.
   566       Edge direct(const UEdge&, const Node&) const {
   567 	return INVALID;
   568       }
   569 
   570       /// \brief Direct the given undirected edge.
   571       ///
   572       /// Direct the given undirected edge. The returned edge source
   573       /// will be the source of the undirected edge if the given bool
   574       /// is true.
   575       Edge direct(const UEdge&, bool) const {
   576 	return INVALID;
   577       }
   578 
   579       /// \brief Returns true if the edge has default orientation.
   580       ///
   581       /// Returns whether the given directed edge is same orientation as
   582       /// the corresponding undirected edge.
   583       bool direction(Edge) const { return true; }
   584 
   585       /// \brief Returns the opposite directed edge.
   586       ///
   587       /// Returns the opposite directed edge.
   588       Edge oppositeEdge(Edge) const { return INVALID; }
   589 
   590       /// \brief Opposite node on an edge
   591       ///
   592       /// \return the opposite of the given Node on the given Edge
   593       Node oppositeNode(Node, UEdge) const { return INVALID; }
   594 
   595       /// \brief First node of the undirected edge.
   596       ///
   597       /// \return the first node of the given UEdge.
   598       ///
   599       /// Naturally uectected edges don't have direction and thus
   600       /// don't have source and target node. But we use these two methods
   601       /// to query the two endnodes of the edge. The direction of the edge
   602       /// which arises this way is called the inherent direction of the
   603       /// undirected edge, and is used to define the "default" direction
   604       /// of the directed versions of the edges.
   605       /// \sa direction
   606       Node source(UEdge) const { return INVALID; }
   607 
   608       /// \brief Second node of the undirected edge.
   609       Node target(UEdge) const { return INVALID; }
   610 
   611       /// \brief Source node of the directed edge.
   612       Node source(Edge) const { return INVALID; }
   613 
   614       /// \brief Target node of the directed edge.
   615       Node target(Edge) const { return INVALID; }
   616 
   617       void first(Node&) const {}
   618       void next(Node&) const {}
   619 
   620       void first(UEdge&) const {}
   621       void next(UEdge&) const {}
   622 
   623       void first(Edge&) const {}
   624       void next(Edge&) const {}
   625 
   626       void firstOut(Edge&, Node) const {}
   627       void nextOut(Edge&) const {}
   628 
   629       void firstIn(Edge&, Node) const {}
   630       void nextIn(Edge&) const {}
   631 
   632 
   633       void firstInc(UEdge &, bool &, const Node &) const {}
   634       void nextInc(UEdge &, bool &) const {}
   635 
   636       /// \brief Base node of the iterator
   637       ///
   638       /// Returns the base node (the source in this case) of the iterator
   639       Node baseNode(OutEdgeIt e) const {
   640 	return source(e);
   641       }
   642       /// \brief Running node of the iterator
   643       ///
   644       /// Returns the running node (the target in this case) of the
   645       /// iterator
   646       Node runningNode(OutEdgeIt e) const {
   647 	return target(e);
   648       }
   649 
   650       /// \brief Base node of the iterator
   651       ///
   652       /// Returns the base node (the target in this case) of the iterator
   653       Node baseNode(InEdgeIt e) const {
   654 	return target(e);
   655       }
   656       /// \brief Running node of the iterator
   657       ///
   658       /// Returns the running node (the source in this case) of the
   659       /// iterator
   660       Node runningNode(InEdgeIt e) const {
   661 	return source(e);
   662       }
   663 
   664       /// \brief Base node of the iterator
   665       ///
   666       /// Returns the base node of the iterator
   667       Node baseNode(IncEdgeIt) const {
   668 	return INVALID;
   669       }
   670       
   671       /// \brief Running node of the iterator
   672       ///
   673       /// Returns the running node of the iterator
   674       Node runningNode(IncEdgeIt) const {
   675 	return INVALID;
   676       }
   677 
   678       template <typename Graph>
   679       struct Constraints {
   680 	void constraints() {
   681 	  checkConcept<BaseIterableUGraphComponent<>, Graph>();
   682 	  checkConcept<IterableUGraphComponent<>, Graph>();
   683 	  checkConcept<MappableUGraphComponent<>, Graph>();
   684 	}
   685       };
   686 
   687     };
   688 
   689     /// @}
   690 
   691   }
   692 
   693 }
   694 
   695 #endif