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@440:  * Copyright (C) 2003-2009
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@529: #ifndef LEMON_CONCEPTS_DIGRAPH_H
deba@529: #define LEMON_CONCEPTS_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:     ///
kpeter@734:     /// This class describes the common interface of all directed
kpeter@734:     /// graphs (digraphs).
deba@57:     ///
kpeter@734:     /// Like all concept classes, it only provides an interface
kpeter@734:     /// without any sensible implementation. So any general algorithm for
kpeter@734:     /// directed graphs should compile with this class, but it will not
kpeter@734:     /// run properly, of course.
kpeter@734:     /// An actual digraph implementation like \ref ListDigraph or
kpeter@734:     /// \ref SmartDigraph may have additional functionality.
deba@57:     ///
kpeter@734:     /// \sa Graph
deba@57:     class Digraph {
deba@57:     private:
kpeter@734:       /// Diraphs are \e not copy constructible. Use DigraphCopy instead.
kpeter@734:       Digraph(const Digraph &) {}
kpeter@734:       /// \brief Assignment of a digraph to another one is \e not allowed.
kpeter@734:       /// Use DigraphCopy instead.
kpeter@734:       void operator=(const Digraph &) {}
alpar@209: 
kpeter@734:     public:
kpeter@734:       /// Default constructor.
kpeter@734:       Digraph() { }
alpar@209: 
kpeter@734:       /// The node type 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,
kpeter@734:       /// thus they convert to this type.
deba@57:       class Node {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
kpeter@734:         /// Default constructor.
kpeter@734:         /// \warning It sets the object 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: 
kpeter@734:         /// %Invalid constructor \& conversion.
deba@57: 
kpeter@734:         /// Initializes the object to be invalid.
deba@57:         /// \sa Invalid for more details.
deba@57:         Node(Invalid) { }
deba@57:         /// Equality operator
deba@57: 
kpeter@734:         /// Equality operator.
kpeter@734:         ///
deba@57:         /// Two iterators are equal if and only if they point to the
kpeter@734:         /// same object or both are \c INVALID.
deba@57:         bool operator==(Node) const { return true; }
deba@57: 
deba@57:         /// Inequality operator
alpar@209: 
kpeter@734:         /// Inequality operator.
deba@57:         bool operator!=(Node) const { return true; }
deba@57: 
alpar@209:         /// Artificial ordering operator.
alpar@209: 
kpeter@734:         /// Artificial ordering operator.
alpar@209:         ///
kpeter@734:         /// \note This operator only has to define some strict ordering of
kpeter@734:         /// the nodes; this order has nothing to do with the iteration
kpeter@734:         /// ordering of the nodes.
alpar@209:         bool operator<(Node) const { return false; }
deba@57:       };
alpar@209: 
kpeter@734:       /// Iterator class for the nodes.
deba@57: 
kpeter@734:       /// This iterator goes through each node of the digraph.
kpeter@786:       /// Its usage is quite simple, for example, you can count the number
kpeter@734:       /// of nodes in a 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: 
kpeter@734:         /// Default constructor.
kpeter@734:         /// \warning It sets the iterator 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) { }
kpeter@734:         /// %Invalid constructor \& conversion.
deba@57: 
kpeter@734:         /// Initializes 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: 
kpeter@734:         /// Sets the iterator to the first node of the given digraph.
deba@57:         ///
kpeter@734:         explicit NodeIt(const Digraph&) { }
kpeter@734:         /// Sets the iterator to the given node.
deba@57: 
kpeter@734:         /// Sets the iterator to the given node of the given digraph.
kpeter@734:         ///
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: 
kpeter@734:       /// The arc type 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: 
kpeter@734:         /// Default constructor.
kpeter@734:         /// \warning It sets the object to an undefined value.
deba@57:         Arc() { }
deba@57:         /// Copy constructor.
deba@57: 
deba@57:         /// Copy constructor.
deba@57:         ///
deba@57:         Arc(const Arc&) { }
kpeter@734:         /// %Invalid constructor \& conversion.
deba@57: 
kpeter@734:         /// Initializes the object to be invalid.
kpeter@734:         /// \sa Invalid for more details.
deba@57:         Arc(Invalid) { }
deba@57:         /// Equality operator
deba@57: 
kpeter@734:         /// Equality operator.
kpeter@734:         ///
deba@57:         /// Two iterators are equal if and only if they point to the
kpeter@734:         /// same object or both are \c INVALID.
deba@57:         bool operator==(Arc) const { return true; }
deba@57:         /// Inequality operator
deba@57: 
kpeter@734:         /// Inequality operator.
deba@57:         bool operator!=(Arc) const { return true; }
deba@57: 
alpar@209:         /// Artificial ordering operator.
alpar@209: 
kpeter@734:         /// Artificial ordering operator.
alpar@209:         ///
kpeter@734:         /// \note This operator only has to define some strict ordering of
kpeter@734:         /// the arcs; this order has nothing to do with the iteration
kpeter@734:         /// ordering of the arcs.
alpar@209:         bool operator<(Arc) const { return false; }
deba@57:       };
alpar@209: 
kpeter@734:       /// Iterator class for 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.
kpeter@786:       /// Its usage is quite simple, for example, you can count the number
deba@57:       /// of outgoing arcs of a node \c n
kpeter@734:       /// in a digraph \c g of type \c %Digraph as follows.
deba@57:       ///\code
deba@57:       /// int count=0;
kpeter@734:       /// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count;
deba@57:       ///\endcode
deba@57:       class OutArcIt : public Arc {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
kpeter@734:         /// Default constructor.
kpeter@734:         /// \warning It sets the iterator 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) { }
kpeter@734:         /// %Invalid constructor \& conversion.
deba@57: 
kpeter@734:         /// Initializes the iterator to be invalid.
kpeter@734:         /// \sa Invalid for more details.
kpeter@734:         OutArcIt(Invalid) { }
kpeter@734:         /// Sets the iterator to the first outgoing arc.
kpeter@734: 
kpeter@734:         /// Sets the iterator to the first outgoing arc of the given node.
deba@57:         ///
kpeter@734:         OutArcIt(const Digraph&, const Node&) { }
kpeter@734:         /// Sets the iterator to the given arc.
alpar@209: 
kpeter@734:         /// Sets the iterator to the given arc of the given digraph.
kpeter@734:         ///
deba@57:         OutArcIt(const Digraph&, const Arc&) { }
kpeter@734:         /// 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: 
kpeter@734:       /// Iterator class for 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.
kpeter@786:       /// Its usage is quite simple, for example, you can count the number
kpeter@734:       /// of incoming arcs of a node \c n
kpeter@734:       /// in a digraph \c g of type \c %Digraph as follows.
deba@57:       ///\code
deba@57:       /// int count=0;
kpeter@734:       /// for(Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count;
deba@57:       ///\endcode
deba@57:       class InArcIt : public Arc {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
kpeter@734:         /// Default constructor.
kpeter@734:         /// \warning It sets the iterator 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) { }
kpeter@734:         /// %Invalid constructor \& conversion.
deba@57: 
kpeter@734:         /// Initializes the iterator to be invalid.
kpeter@734:         /// \sa Invalid for more details.
kpeter@734:         InArcIt(Invalid) { }
kpeter@734:         /// Sets the iterator to the first incoming arc.
kpeter@734: 
kpeter@734:         /// Sets the iterator to the first incoming arc of the given node.
deba@57:         ///
kpeter@734:         InArcIt(const Digraph&, const Node&) { }
kpeter@734:         /// Sets the iterator to the given arc.
alpar@209: 
kpeter@734:         /// Sets the iterator to the given arc of the given digraph.
kpeter@734:         ///
deba@57:         InArcIt(const Digraph&, const Arc&) { }
deba@57:         /// Next incoming arc
deba@57: 
kpeter@734:         /// Assign the iterator to the next
kpeter@734:         /// incoming arc of the corresponding node.
deba@57:         InArcIt& operator++() { return *this; }
deba@57:       };
deba@57: 
kpeter@734:       /// Iterator class for the arcs.
kpeter@734: 
kpeter@734:       /// This iterator goes through each arc of the digraph.
kpeter@786:       /// Its usage is quite simple, for example, you can count the number
kpeter@734:       /// of arcs in a digraph \c g of type \c %Digraph as follows:
deba@57:       ///\code
deba@57:       /// int count=0;
kpeter@734:       /// for(Digraph::ArcIt a(g); a!=INVALID; ++a) ++count;
deba@57:       ///\endcode
deba@57:       class ArcIt : public Arc {
deba@57:       public:
deba@57:         /// Default constructor
deba@57: 
kpeter@734:         /// Default constructor.
kpeter@734:         /// \warning It sets the iterator 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) { }
kpeter@734:         /// %Invalid constructor \& conversion.
deba@57: 
kpeter@734:         /// Initializes the iterator to be invalid.
kpeter@734:         /// \sa Invalid for more details.
kpeter@734:         ArcIt(Invalid) { }
kpeter@734:         /// Sets the iterator to the first arc.
kpeter@734: 
kpeter@734:         /// Sets the iterator to the first arc of the given digraph.
deba@57:         ///
kpeter@734:         explicit ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
kpeter@734:         /// Sets the iterator to the given arc.
alpar@209: 
kpeter@734:         /// Sets the iterator to the given arc of the given digraph.
kpeter@734:         ///
alpar@209:         ArcIt(const Digraph&, const Arc&) { }
kpeter@734:         /// Next arc
alpar@209: 
deba@57:         /// Assign the iterator to the next arc.
kpeter@734:         ///
deba@57:         ArcIt& operator++() { return *this; }
deba@57:       };
deba@57: 
kpeter@734:       /// \brief The source node of the arc.
deba@57:       ///
kpeter@734:       /// Returns the source node of the given arc.
deba@57:       Node source(Arc) const { return INVALID; }
deba@57: 
kpeter@734:       /// \brief The target node of the arc.
kpeter@734:       ///
kpeter@734:       /// Returns the target node of the given arc.
kpeter@734:       Node target(Arc) const { return INVALID; }
kpeter@734: 
kpeter@734:       /// \brief The ID of the node.
kpeter@734:       ///
kpeter@734:       /// Returns the ID of the given node.
alpar@209:       int id(Node) const { return -1; }
deba@61: 
kpeter@734:       /// \brief The ID of the arc.
kpeter@734:       ///
kpeter@734:       /// Returns the ID of the given arc.
alpar@209:       int id(Arc) const { return -1; }
deba@61: 
kpeter@734:       /// \brief The node with the given ID.
deba@61:       ///
kpeter@734:       /// Returns the node with the given ID.
kpeter@734:       /// \pre The argument should be a valid node ID in the digraph.
alpar@209:       Node nodeFromId(int) const { return INVALID; }
deba@61: 
kpeter@734:       /// \brief The arc with the given ID.
deba@61:       ///
kpeter@734:       /// Returns the arc with the given ID.
kpeter@734:       /// \pre The argument should be a valid arc ID in the digraph.
alpar@209:       Arc arcFromId(int) const { return INVALID; }
deba@61: 
kpeter@734:       /// \brief An upper bound on the node IDs.
kpeter@734:       ///
kpeter@734:       /// Returns an upper bound on the node IDs.
alpar@209:       int maxNodeId() const { return -1; }
deba@61: 
kpeter@734:       /// \brief An upper bound on the arc IDs.
kpeter@734:       ///
kpeter@734:       /// 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: 
kpeter@734:       /// \brief The opposite node on the arc.
kpeter@734:       ///
kpeter@734:       /// Returns the opposite node on the given arc.
kpeter@734:       Node oppositeNode(Node, Arc) const { return INVALID; }
kpeter@734: 
deba@57:       /// \brief The base node of the iterator.
deba@57:       ///
kpeter@734:       /// Returns the base node of the given outgoing arc iterator
kpeter@734:       /// (i.e. the source node of the corresponding arc).
kpeter@734:       Node baseNode(OutArcIt) const { return INVALID; }
deba@57: 
deba@57:       /// \brief The running node of the iterator.
deba@57:       ///
kpeter@734:       /// Returns the running node of the given outgoing arc iterator
kpeter@734:       /// (i.e. the target node of the corresponding arc).
kpeter@734:       Node runningNode(OutArcIt) const { return INVALID; }
deba@57: 
deba@57:       /// \brief The base node of the iterator.
deba@57:       ///
kpeter@734:       /// Returns the base node of the given incomming arc iterator
kpeter@734:       /// (i.e. the target node of the corresponding arc).
kpeter@734:       Node baseNode(InArcIt) const { return INVALID; }
deba@57: 
deba@57:       /// \brief The running node of the iterator.
deba@57:       ///
kpeter@734:       /// Returns the running node of the given incomming arc iterator
kpeter@734:       /// (i.e. the source node of the corresponding arc).
kpeter@734:       Node runningNode(InArcIt) const { return INVALID; }
deba@57: 
kpeter@734:       /// \brief Standard graph map type for the nodes.
deba@57:       ///
kpeter@734:       /// Standard graph map type for the nodes.
kpeter@734:       /// It conforms to the ReferenceMap concept.
alpar@209:       template<class T>
kpeter@580:       class NodeMap : public ReferenceMap<Node, T, T&, const T&> {
deba@57:       public:
deba@57: 
kpeter@734:         /// Constructor
kpeter@734:         explicit NodeMap(const Digraph&) { }
kpeter@734:         /// Constructor with given initial value
deba@57:         NodeMap(const Digraph&, T) { }
deba@57: 
kpeter@263:       private:
deba@57:         ///Copy constructor
kpeter@580:         NodeMap(const NodeMap& nm) : 
kpeter@580:           ReferenceMap<Node, T, T&, const 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: 
kpeter@734:       /// \brief Standard graph map type for the arcs.
deba@57:       ///
kpeter@734:       /// Standard graph map type for the arcs.
kpeter@734:       /// It conforms to the ReferenceMap concept.
alpar@209:       template<class T>
kpeter@580:       class ArcMap : public ReferenceMap<Arc, T, T&, const T&> {
deba@57:       public:
deba@57: 
kpeter@734:         /// Constructor
kpeter@734:         explicit ArcMap(const Digraph&) { }
kpeter@734:         /// Constructor with given initial value
deba@57:         ArcMap(const Digraph&, T) { }
kpeter@734: 
kpeter@263:       private:
deba@57:         ///Copy constructor
kpeter@580:         ArcMap(const ArcMap& em) :
kpeter@580:           ReferenceMap<Arc, T, T&, const 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() {
kpeter@580:           checkConcept<BaseDigraphComponent, _Digraph>();
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@529: #endif