alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
deba@57:  *
alpar@107:  * Copyright (C) 2003-2008
deba@57:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57:  *
deba@57:  * Permission to use, modify and distribute this software is granted
deba@57:  * provided that this copyright notice appears in all copies. For
deba@57:  * precise terms see the accompanying LICENSE file.
deba@57:  *
deba@57:  * This software is provided "AS IS" with no warranty of any kind,
deba@57:  * express or implied, and with no claim as to its suitability for any
deba@57:  * purpose.
deba@57:  *
deba@57:  */
deba@57: 
deba@57: #ifndef LEMON_CONCEPT_DIGRAPH_H
deba@57: #define LEMON_CONCEPT_DIGRAPH_H
deba@57: 
deba@57: ///\ingroup graph_concepts
deba@57: ///\file
deba@57: ///\brief The concept of directed graphs.
deba@57: 
deba@220: #include <lemon/core.h>
deba@57: #include <lemon/concepts/maps.h>
deba@57: #include <lemon/concept_check.h>
deba@57: #include <lemon/concepts/graph_components.h>
deba@57: 
deba@57: namespace lemon {
deba@57:   namespace concepts {
deba@57: 
deba@57:     /// \ingroup graph_concepts
deba@57:     ///
deba@57:     /// \brief Class describing the concept of directed graphs.
deba@57:     ///
deba@57:     /// This class describes the \ref concept "concept" of the
deba@57:     /// immutable directed digraphs.
deba@57:     ///
deba@57:     /// Note that actual digraph implementation like @ref ListDigraph or
deba@57:     /// @ref SmartDigraph may have several additional functionality.
deba@57:     ///
deba@57:     /// \sa concept
deba@57:     class Digraph {
deba@57:     private:
deba@57:       ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
alpar@209: 
deba@57:       ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
deba@57:       ///
deba@57:       Digraph(const Digraph &) {};
deba@57:       ///\brief Assignment of \ref Digraph "Digraph"s to another ones are
deba@57:       ///\e not allowed. Use DigraphCopy() instead.
alpar@209: 
deba@57:       ///Assignment of \ref Digraph "Digraph"s to another ones are
deba@57:       ///\e not allowed.  Use DigraphCopy() instead.
deba@57: 
deba@57:       void operator=(const Digraph &) {}
deba@57:     public:
deba@57:       ///\e
deba@57: 
deba@57:       /// Defalult constructor.
deba@57: 
deba@57:       /// Defalult constructor.
deba@57:       ///
deba@57:       Digraph() { }
deba@57:       /// Class for identifying a node of the digraph
deba@57: 
deba@57:       /// This class identifies a node of the digraph. It also serves
deba@57:       /// as a base class of the node iterators,
deba@57:       /// thus they will convert to this type.
deba@57:       class Node {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
deba@57:         /// @warning The default constructor sets the iterator
deba@57:         /// to an undefined value.
deba@57:         Node() { }
deba@57:         /// Copy constructor.
deba@57: 
deba@57:         /// Copy constructor.
deba@57:         ///
deba@57:         Node(const Node&) { }
deba@57: 
deba@57:         /// Invalid constructor \& conversion.
deba@57: 
deba@57:         /// This constructor initializes the iterator to be invalid.
deba@57:         /// \sa Invalid for more details.
deba@57:         Node(Invalid) { }
deba@57:         /// Equality operator
deba@57: 
deba@57:         /// Two iterators are equal if and only if they point to the
deba@57:         /// same object or both are invalid.
deba@57:         bool operator==(Node) const { return true; }
deba@57: 
deba@57:         /// Inequality operator
alpar@209: 
deba@57:         /// \sa operator==(Node n)
deba@57:         ///
deba@57:         bool operator!=(Node) const { return true; }
deba@57: 
alpar@209:         /// Artificial ordering operator.
alpar@209: 
alpar@209:         /// To allow the use of digraph descriptors as key type in std::map or
alpar@209:         /// similar associative container we require this.
alpar@209:         ///
alpar@209:         /// \note This operator only have to define some strict ordering of
alpar@209:         /// the items; this order has nothing to do with the iteration
alpar@209:         /// ordering of the items.
alpar@209:         bool operator<(Node) const { return false; }
deba@57: 
deba@57:       };
alpar@209: 
deba@57:       /// This iterator goes through each node.
deba@57: 
deba@57:       /// This iterator goes through each node.
deba@57:       /// Its usage is quite simple, for example you can count the number
deba@57:       /// of nodes in digraph \c g of type \c Digraph like this:
deba@57:       ///\code
deba@57:       /// int count=0;
deba@57:       /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
deba@57:       ///\endcode
deba@57:       class NodeIt : public Node {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
deba@57:         /// @warning The default constructor sets the iterator
deba@57:         /// to an undefined value.
deba@57:         NodeIt() { }
deba@57:         /// Copy constructor.
alpar@209: 
deba@57:         /// Copy constructor.
deba@57:         ///
deba@57:         NodeIt(const NodeIt& n) : Node(n) { }
deba@57:         /// Invalid constructor \& conversion.
deba@57: 
deba@57:         /// Initialize the iterator to be invalid.
deba@57:         /// \sa Invalid for more details.
deba@57:         NodeIt(Invalid) { }
deba@57:         /// Sets the iterator to the first node.
deba@57: 
deba@57:         /// Sets the iterator to the first node of \c g.
deba@57:         ///
deba@57:         NodeIt(const Digraph&) { }
deba@57:         /// Node -> NodeIt conversion.
deba@57: 
alpar@209:         /// Sets the iterator to the node of \c the digraph pointed by
alpar@209:         /// the trivial iterator.
alpar@209:         /// This feature necessitates that each time we
deba@57:         /// iterate the arc-set, the iteration order is the same.
deba@57:         NodeIt(const Digraph&, const Node&) { }
deba@57:         /// Next node.
deba@57: 
deba@57:         /// Assign the iterator to the next node.
deba@57:         ///
deba@57:         NodeIt& operator++() { return *this; }
deba@57:       };
alpar@209: 
alpar@209: 
deba@57:       /// Class for identifying an arc of the digraph
deba@57: 
deba@57:       /// This class identifies an arc of the digraph. It also serves
deba@57:       /// as a base class of the arc iterators,
deba@57:       /// thus they will convert to this type.
deba@57:       class Arc {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
deba@57:         /// @warning The default constructor sets the iterator
deba@57:         /// to an undefined value.
deba@57:         Arc() { }
deba@57:         /// Copy constructor.
deba@57: 
deba@57:         /// Copy constructor.
deba@57:         ///
deba@57:         Arc(const Arc&) { }
deba@57:         /// Initialize the iterator to be invalid.
deba@57: 
deba@57:         /// Initialize the iterator to be invalid.
deba@57:         ///
deba@57:         Arc(Invalid) { }
deba@57:         /// Equality operator
deba@57: 
deba@57:         /// Two iterators are equal if and only if they point to the
deba@57:         /// same object or both are invalid.
deba@57:         bool operator==(Arc) const { return true; }
deba@57:         /// Inequality operator
deba@57: 
deba@57:         /// \sa operator==(Arc n)
deba@57:         ///
deba@57:         bool operator!=(Arc) const { return true; }
deba@57: 
alpar@209:         /// Artificial ordering operator.
alpar@209: 
alpar@209:         /// To allow the use of digraph descriptors as key type in std::map or
alpar@209:         /// similar associative container we require this.
alpar@209:         ///
alpar@209:         /// \note This operator only have to define some strict ordering of
alpar@209:         /// the items; this order has nothing to do with the iteration
alpar@209:         /// ordering of the items.
alpar@209:         bool operator<(Arc) const { return false; }
deba@57:       };
alpar@209: 
deba@57:       /// This iterator goes trough the outgoing arcs of a node.
deba@57: 
deba@57:       /// This iterator goes trough the \e outgoing arcs of a certain node
deba@57:       /// of a digraph.
deba@57:       /// Its usage is quite simple, for example you can count the number
deba@57:       /// of outgoing arcs of a node \c n
deba@57:       /// in digraph \c g of type \c Digraph as follows.
deba@57:       ///\code
deba@57:       /// int count=0;
deba@57:       /// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
deba@57:       ///\endcode
alpar@209: 
deba@57:       class OutArcIt : public Arc {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
deba@57:         /// @warning The default constructor sets the iterator
deba@57:         /// to an undefined value.
deba@57:         OutArcIt() { }
deba@57:         /// Copy constructor.
deba@57: 
deba@57:         /// Copy constructor.
deba@57:         ///
deba@57:         OutArcIt(const OutArcIt& e) : Arc(e) { }
deba@57:         /// Initialize the iterator to be invalid.
deba@57: 
deba@57:         /// Initialize the iterator to be invalid.
deba@57:         ///
deba@57:         OutArcIt(Invalid) { }
deba@57:         /// This constructor sets the iterator to the first outgoing arc.
alpar@209: 
deba@57:         /// This constructor sets the iterator to the first outgoing arc of
deba@57:         /// the node.
deba@57:         OutArcIt(const Digraph&, const Node&) { }
deba@57:         /// Arc -> OutArcIt conversion
deba@57: 
deba@57:         /// Sets the iterator to the value of the trivial iterator.
alpar@209:         /// This feature necessitates that each time we
deba@57:         /// iterate the arc-set, the iteration order is the same.
deba@57:         OutArcIt(const Digraph&, const Arc&) { }
deba@57:         ///Next outgoing arc
alpar@209: 
alpar@209:         /// Assign the iterator to the next
deba@57:         /// outgoing arc of the corresponding node.
deba@57:         OutArcIt& operator++() { return *this; }
deba@57:       };
deba@57: 
deba@57:       /// This iterator goes trough the incoming arcs of a node.
deba@57: 
deba@57:       /// This iterator goes trough the \e incoming arcs of a certain node
deba@57:       /// of a digraph.
deba@57:       /// Its usage is quite simple, for example you can count the number
deba@57:       /// of outgoing arcs of a node \c n
deba@57:       /// in digraph \c g of type \c Digraph as follows.
deba@57:       ///\code
deba@57:       /// int count=0;
deba@57:       /// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
deba@57:       ///\endcode
deba@57: 
deba@57:       class InArcIt : public Arc {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
deba@57:         /// @warning The default constructor sets the iterator
deba@57:         /// to an undefined value.
deba@57:         InArcIt() { }
deba@57:         /// Copy constructor.
deba@57: 
deba@57:         /// Copy constructor.
deba@57:         ///
deba@57:         InArcIt(const InArcIt& e) : Arc(e) { }
deba@57:         /// Initialize the iterator to be invalid.
deba@57: 
deba@57:         /// Initialize the iterator to be invalid.
deba@57:         ///
deba@57:         InArcIt(Invalid) { }
deba@57:         /// This constructor sets the iterator to first incoming arc.
alpar@209: 
deba@57:         /// This constructor set the iterator to the first incoming arc of
deba@57:         /// the node.
deba@57:         InArcIt(const Digraph&, const Node&) { }
deba@57:         /// Arc -> InArcIt conversion
deba@57: 
deba@57:         /// Sets the iterator to the value of the trivial iterator \c e.
alpar@209:         /// This feature necessitates that each time we
deba@57:         /// iterate the arc-set, the iteration order is the same.
deba@57:         InArcIt(const Digraph&, const Arc&) { }
deba@57:         /// Next incoming arc
deba@57: 
deba@57:         /// Assign the iterator to the next inarc of the corresponding node.
deba@57:         ///
deba@57:         InArcIt& operator++() { return *this; }
deba@57:       };
deba@57:       /// This iterator goes through each arc.
deba@57: 
deba@57:       /// This iterator goes through each arc of a digraph.
deba@57:       /// Its usage is quite simple, for example you can count the number
deba@57:       /// of arcs in a digraph \c g of type \c Digraph as follows:
deba@57:       ///\code
deba@57:       /// int count=0;
deba@57:       /// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count;
deba@57:       ///\endcode
deba@57:       class ArcIt : public Arc {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
deba@57:         /// @warning The default constructor sets the iterator
deba@57:         /// to an undefined value.
deba@57:         ArcIt() { }
deba@57:         /// Copy constructor.
deba@57: 
deba@57:         /// Copy constructor.
deba@57:         ///
deba@57:         ArcIt(const ArcIt& e) : Arc(e) { }
deba@57:         /// Initialize the iterator to be invalid.
deba@57: 
deba@57:         /// Initialize the iterator to be invalid.
deba@57:         ///
deba@57:         ArcIt(Invalid) { }
deba@57:         /// This constructor sets the iterator to the first arc.
alpar@209: 
deba@57:         /// This constructor sets the iterator to the first arc of \c g.
deba@57:         ///@param g the digraph
deba@57:         ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
deba@57:         /// Arc -> ArcIt conversion
deba@57: 
deba@57:         /// Sets the iterator to the value of the trivial iterator \c e.
alpar@209:         /// This feature necessitates that each time we
deba@57:         /// iterate the arc-set, the iteration order is the same.
alpar@209:         ArcIt(const Digraph&, const Arc&) { }
deba@57:         ///Next arc
alpar@209: 
deba@57:         /// Assign the iterator to the next arc.
deba@57:         ArcIt& operator++() { return *this; }
deba@57:       };
deba@57:       ///Gives back the target node of an arc.
deba@57: 
deba@57:       ///Gives back the target node of an arc.
deba@57:       ///
deba@57:       Node target(Arc) const { return INVALID; }
deba@57:       ///Gives back the source node of an arc.
deba@57: 
deba@57:       ///Gives back the source node of an arc.
deba@57:       ///
deba@57:       Node source(Arc) const { return INVALID; }
deba@57: 
deba@61:       /// \brief Returns the ID of the node.
alpar@209:       int id(Node) const { return -1; }
deba@61: 
deba@61:       /// \brief Returns the ID of the arc.
alpar@209:       int id(Arc) const { return -1; }
deba@61: 
deba@61:       /// \brief Returns the node with the given ID.
deba@61:       ///
deba@61:       /// \pre The argument should be a valid node ID in the graph.
alpar@209:       Node nodeFromId(int) const { return INVALID; }
deba@61: 
deba@61:       /// \brief Returns the arc with the given ID.
deba@61:       ///
deba@61:       /// \pre The argument should be a valid arc ID in the graph.
alpar@209:       Arc arcFromId(int) const { return INVALID; }
deba@61: 
deba@61:       /// \brief Returns an upper bound on the node IDs.
alpar@209:       int maxNodeId() const { return -1; }
deba@61: 
deba@61:       /// \brief Returns an upper bound on the arc IDs.
alpar@209:       int maxArcId() const { return -1; }
deba@61: 
deba@57:       void first(Node&) const {}
deba@57:       void next(Node&) const {}
deba@57: 
deba@57:       void first(Arc&) const {}
deba@57:       void next(Arc&) const {}
deba@57: 
deba@57: 
deba@57:       void firstIn(Arc&, const Node&) const {}
deba@57:       void nextIn(Arc&) const {}
deba@57: 
deba@57:       void firstOut(Arc&, const Node&) const {}
deba@57:       void nextOut(Arc&) const {}
deba@57: 
deba@61:       // The second parameter is dummy.
deba@61:       Node fromId(int, Node) const { return INVALID; }
deba@61:       // The second parameter is dummy.
deba@61:       Arc fromId(int, Arc) const { return INVALID; }
deba@61: 
deba@61:       // Dummy parameter.
alpar@209:       int maxId(Node) const { return -1; }
deba@61:       // Dummy parameter.
alpar@209:       int maxId(Arc) const { return -1; }
deba@61: 
deba@57:       /// \brief The base node of the iterator.
deba@57:       ///
deba@57:       /// Gives back the base node of the iterator.
deba@57:       /// It is always the target of the pointed arc.
deba@57:       Node baseNode(const InArcIt&) const { return INVALID; }
deba@57: 
deba@57:       /// \brief The running node of the iterator.
deba@57:       ///
deba@57:       /// Gives back the running node of the iterator.
deba@57:       /// It is always the source of the pointed arc.
deba@57:       Node runningNode(const InArcIt&) const { return INVALID; }
deba@57: 
deba@57:       /// \brief The base node of the iterator.
deba@57:       ///
deba@57:       /// Gives back the base node of the iterator.
deba@57:       /// It is always the source of the pointed arc.
deba@57:       Node baseNode(const OutArcIt&) const { return INVALID; }
deba@57: 
deba@57:       /// \brief The running node of the iterator.
deba@57:       ///
deba@57:       /// Gives back the running node of the iterator.
deba@57:       /// It is always the target of the pointed arc.
deba@57:       Node runningNode(const OutArcIt&) const { return INVALID; }
deba@57: 
deba@57:       /// \brief The opposite node on the given arc.
deba@57:       ///
deba@57:       /// Gives back the opposite node on the given arc.
deba@57:       Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
deba@57: 
deba@57:       /// \brief Read write map of the nodes to type \c T.
alpar@209:       ///
deba@57:       /// ReadWrite map of the nodes to type \c T.
deba@57:       /// \sa Reference
alpar@209:       template<class T>
deba@57:       class NodeMap : public ReadWriteMap< Node, T > {
deba@57:       public:
deba@57: 
deba@57:         ///\e
deba@57:         NodeMap(const Digraph&) { }
deba@57:         ///\e
deba@57:         NodeMap(const Digraph&, T) { }
deba@57: 
kpeter@263:       private:
deba@57:         ///Copy constructor
deba@57:         NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
deba@57:         ///Assignment operator
deba@57:         template <typename CMap>
alpar@209:         NodeMap& operator=(const CMap&) {
deba@57:           checkConcept<ReadMap<Node, T>, CMap>();
alpar@209:           return *this;
deba@57:         }
deba@57:       };
deba@57: 
deba@57:       /// \brief Read write map of the arcs to type \c T.
deba@57:       ///
deba@57:       /// Reference map of the arcs to type \c T.
deba@57:       /// \sa Reference
alpar@209:       template<class T>
deba@57:       class ArcMap : public ReadWriteMap<Arc,T> {
deba@57:       public:
deba@57: 
deba@57:         ///\e
deba@57:         ArcMap(const Digraph&) { }
deba@57:         ///\e
deba@57:         ArcMap(const Digraph&, T) { }
kpeter@263:       private:
deba@57:         ///Copy constructor
deba@57:         ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
deba@57:         ///Assignment operator
deba@57:         template <typename CMap>
alpar@209:         ArcMap& operator=(const CMap&) {
deba@57:           checkConcept<ReadMap<Arc, T>, CMap>();
alpar@209:           return *this;
deba@57:         }
deba@57:       };
deba@57: 
deba@125:       template <typename _Digraph>
deba@57:       struct Constraints {
deba@57:         void constraints() {
deba@125:           checkConcept<IterableDigraphComponent<>, _Digraph>();
alpar@209:           checkConcept<IDableDigraphComponent<>, _Digraph>();
deba@125:           checkConcept<MappableDigraphComponent<>, _Digraph>();
deba@57:         }
deba@57:       };
deba@57: 
deba@57:     };
alpar@209: 
alpar@209:   } //namespace concepts
deba@57: } //namespace lemon
deba@57: 
deba@57: 
deba@57: 
deba@57: #endif // LEMON_CONCEPT_DIGRAPH_H