COIN-OR::LEMON - Graph Library

source: lemon/lemon/concepts/digraph.h @ 1336:0759d974de81

Last change on this file since 1336:0759d974de81 was 1336:0759d974de81, checked in by Gabor Gevay <ggab90@…>, 10 years ago

STL style iterators (#325)

For

  • graph types,
  • graph adaptors,
  • paths,
  • iterable maps,
  • LP rows/cols and
  • active nodes is BellmanFord?
File size: 18.2 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[57]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[57]4 *
[1270]5 * Copyright (C) 2003-2013
[57]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
[576]19#ifndef LEMON_CONCEPTS_DIGRAPH_H
20#define LEMON_CONCEPTS_DIGRAPH_H
[57]21
22///\ingroup graph_concepts
23///\file
24///\brief The concept of directed graphs.
25
[220]26#include <lemon/core.h>
[57]27#include <lemon/concepts/maps.h>
28#include <lemon/concept_check.h>
29#include <lemon/concepts/graph_components.h>
[1336]30#include <lemon/bits/stl_iterators.h>
[57]31
32namespace lemon {
33  namespace concepts {
34
35    /// \ingroup graph_concepts
36    ///
37    /// \brief Class describing the concept of directed graphs.
38    ///
[781]39    /// This class describes the common interface of all directed
40    /// graphs (digraphs).
[57]41    ///
[781]42    /// Like all concept classes, it only provides an interface
43    /// without any sensible implementation. So any general algorithm for
44    /// directed graphs should compile with this class, but it will not
45    /// run properly, of course.
46    /// An actual digraph implementation like \ref ListDigraph or
47    /// \ref SmartDigraph may have additional functionality.
[57]48    ///
[781]49    /// \sa Graph
[57]50    class Digraph {
51    private:
[781]52      /// Diraphs are \e not copy constructible. Use DigraphCopy instead.
53      Digraph(const Digraph &) {}
54      /// \brief Assignment of a digraph to another one is \e not allowed.
55      /// Use DigraphCopy instead.
56      void operator=(const Digraph &) {}
[209]57
[781]58    public:
59      /// Default constructor.
60      Digraph() { }
[209]61
[781]62      /// The node type of the digraph
[57]63
64      /// This class identifies a node of the digraph. It also serves
65      /// as a base class of the node iterators,
[781]66      /// thus they convert to this type.
[57]67      class Node {
68      public:
69        /// Default constructor
70
[781]71        /// Default constructor.
72        /// \warning It sets the object to an undefined value.
[57]73        Node() { }
74        /// Copy constructor.
75
76        /// Copy constructor.
77        ///
78        Node(const Node&) { }
79
[781]80        /// %Invalid constructor \& conversion.
[57]81
[781]82        /// Initializes the object to be invalid.
[57]83        /// \sa Invalid for more details.
84        Node(Invalid) { }
85        /// Equality operator
86
[781]87        /// Equality operator.
88        ///
[57]89        /// Two iterators are equal if and only if they point to the
[781]90        /// same object or both are \c INVALID.
[57]91        bool operator==(Node) const { return true; }
92
93        /// Inequality operator
[209]94
[781]95        /// Inequality operator.
[57]96        bool operator!=(Node) const { return true; }
97
[209]98        /// Artificial ordering operator.
99
[781]100        /// Artificial ordering operator.
[209]101        ///
[781]102        /// \note This operator only has to define some strict ordering of
103        /// the nodes; this order has nothing to do with the iteration
104        /// ordering of the nodes.
[209]105        bool operator<(Node) const { return false; }
[57]106      };
[209]107
[781]108      /// Iterator class for the nodes.
[57]109
[781]110      /// This iterator goes through each node of the digraph.
[833]111      /// Its usage is quite simple, for example, you can count the number
[781]112      /// of nodes in a digraph \c g of type \c %Digraph like this:
[57]113      ///\code
114      /// int count=0;
115      /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
116      ///\endcode
117      class NodeIt : public Node {
118      public:
119        /// Default constructor
120
[781]121        /// Default constructor.
122        /// \warning It sets the iterator to an undefined value.
[57]123        NodeIt() { }
124        /// Copy constructor.
[209]125
[57]126        /// Copy constructor.
127        ///
128        NodeIt(const NodeIt& n) : Node(n) { }
[781]129        /// %Invalid constructor \& conversion.
[57]130
[781]131        /// Initializes the iterator to be invalid.
[57]132        /// \sa Invalid for more details.
133        NodeIt(Invalid) { }
134        /// Sets the iterator to the first node.
135
[781]136        /// Sets the iterator to the first node of the given digraph.
[57]137        ///
[781]138        explicit NodeIt(const Digraph&) { }
139        /// Sets the iterator to the given node.
[57]140
[781]141        /// Sets the iterator to the given node of the given digraph.
142        ///
[57]143        NodeIt(const Digraph&, const Node&) { }
144        /// Next node.
145
146        /// Assign the iterator to the next node.
147        ///
148        NodeIt& operator++() { return *this; }
149      };
[209]150
[1336]151      /// \brief Gets the collection of the nodes of the digraph.
152      ///
153      /// This function can be used for iterating on
154      /// the nodes of the digraph. It returns a wrapped NodeIt, which looks
155      /// like an STL container (by having begin() and end())
156      /// which you can use in range-based for loops, STL algorithms, etc.
157      /// For example you can write:
158      ///\code
159      /// ListDigraph g;
160      /// for(auto v: g.nodes())
161      ///   doSomething(v);
162      ///
163      /// //Using an STL algorithm:
164      /// copy(g.nodes().begin(), g.nodes().end(), vect.begin());
165      ///\endcode
166      LemonRangeWrapper1<NodeIt, Digraph> nodes() const {
167        return LemonRangeWrapper1<NodeIt, Digraph>(*this);
168      }
169
[209]170
[781]171      /// The arc type of the digraph
[57]172
173      /// This class identifies an arc of the digraph. It also serves
174      /// as a base class of the arc iterators,
175      /// thus they will convert to this type.
176      class Arc {
177      public:
178        /// Default constructor
179
[781]180        /// Default constructor.
181        /// \warning It sets the object to an undefined value.
[57]182        Arc() { }
183        /// Copy constructor.
184
185        /// Copy constructor.
186        ///
187        Arc(const Arc&) { }
[781]188        /// %Invalid constructor \& conversion.
[57]189
[781]190        /// Initializes the object to be invalid.
191        /// \sa Invalid for more details.
[57]192        Arc(Invalid) { }
193        /// Equality operator
194
[781]195        /// Equality operator.
196        ///
[57]197        /// Two iterators are equal if and only if they point to the
[781]198        /// same object or both are \c INVALID.
[57]199        bool operator==(Arc) const { return true; }
200        /// Inequality operator
201
[781]202        /// Inequality operator.
[57]203        bool operator!=(Arc) const { return true; }
204
[209]205        /// Artificial ordering operator.
206
[781]207        /// Artificial ordering operator.
[209]208        ///
[781]209        /// \note This operator only has to define some strict ordering of
210        /// the arcs; this order has nothing to do with the iteration
211        /// ordering of the arcs.
[209]212        bool operator<(Arc) const { return false; }
[57]213      };
[209]214
[781]215      /// Iterator class for the outgoing arcs of a node.
[57]216
217      /// This iterator goes trough the \e outgoing arcs of a certain node
218      /// of a digraph.
[833]219      /// Its usage is quite simple, for example, you can count the number
[57]220      /// of outgoing arcs of a node \c n
[781]221      /// in a digraph \c g of type \c %Digraph as follows.
[57]222      ///\code
223      /// int count=0;
[781]224      /// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count;
[57]225      ///\endcode
226      class OutArcIt : public Arc {
227      public:
228        /// Default constructor
229
[781]230        /// Default constructor.
231        /// \warning It sets the iterator to an undefined value.
[57]232        OutArcIt() { }
233        /// Copy constructor.
234
235        /// Copy constructor.
236        ///
237        OutArcIt(const OutArcIt& e) : Arc(e) { }
[781]238        /// %Invalid constructor \& conversion.
[57]239
[781]240        /// Initializes the iterator to be invalid.
241        /// \sa Invalid for more details.
242        OutArcIt(Invalid) { }
243        /// Sets the iterator to the first outgoing arc.
244
245        /// Sets the iterator to the first outgoing arc of the given node.
[57]246        ///
[781]247        OutArcIt(const Digraph&, const Node&) { }
248        /// Sets the iterator to the given arc.
[209]249
[781]250        /// Sets the iterator to the given arc of the given digraph.
251        ///
[57]252        OutArcIt(const Digraph&, const Arc&) { }
[781]253        /// Next outgoing arc
[209]254
255        /// Assign the iterator to the next
[57]256        /// outgoing arc of the corresponding node.
257        OutArcIt& operator++() { return *this; }
258      };
259
[1336]260      /// \brief Gets the collection of the outgoing arcs of a certain node
261      /// of the digraph.
262      ///
263      /// This function can be used for iterating on the
264      /// outgoing arcs of a certain node of the digraph. It returns a wrapped
265      /// OutArcIt, which looks like an STL container
266      /// (by having begin() and end()) which you can use in range-based
267      /// for loops, STL algorithms, etc.
268      /// For example if g is a Digraph and u is a node, you can write:
269      ///\code
270      /// for(auto a: g.outArcs(u))
271      ///   doSomething(a);
272      ///
273      /// //Using an STL algorithm:
274      /// copy(g.outArcs(u).begin(), g.outArcs(u).end(), vect.begin());
275      ///\endcode
276      LemonRangeWrapper2<OutArcIt, Digraph, Node> outArcs(const Node& u) const {
277        return LemonRangeWrapper2<OutArcIt, Digraph, Node>(*this, u);
278      }
279
280
[781]281      /// Iterator class for the incoming arcs of a node.
[57]282
283      /// This iterator goes trough the \e incoming arcs of a certain node
284      /// of a digraph.
[833]285      /// Its usage is quite simple, for example, you can count the number
[781]286      /// of incoming arcs of a node \c n
287      /// in a digraph \c g of type \c %Digraph as follows.
[57]288      ///\code
289      /// int count=0;
[781]290      /// for(Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count;
[57]291      ///\endcode
292      class InArcIt : public Arc {
293      public:
294        /// Default constructor
295
[781]296        /// Default constructor.
297        /// \warning It sets the iterator to an undefined value.
[57]298        InArcIt() { }
299        /// Copy constructor.
300
301        /// Copy constructor.
302        ///
303        InArcIt(const InArcIt& e) : Arc(e) { }
[781]304        /// %Invalid constructor \& conversion.
[57]305
[781]306        /// Initializes the iterator to be invalid.
307        /// \sa Invalid for more details.
308        InArcIt(Invalid) { }
309        /// Sets the iterator to the first incoming arc.
310
311        /// Sets the iterator to the first incoming arc of the given node.
[57]312        ///
[781]313        InArcIt(const Digraph&, const Node&) { }
314        /// Sets the iterator to the given arc.
[209]315
[781]316        /// Sets the iterator to the given arc of the given digraph.
317        ///
[57]318        InArcIt(const Digraph&, const Arc&) { }
319        /// Next incoming arc
320
[781]321        /// Assign the iterator to the next
322        /// incoming arc of the corresponding node.
[57]323        InArcIt& operator++() { return *this; }
324      };
325
[1336]326      /// \brief Gets the collection of the incoming arcs of a certain node
327      /// of the digraph.
328      ///
329      /// This function can be used for iterating on the
330      /// incoming arcs of a certain node of the digraph. It returns a wrapped
331      /// InArcIt, which looks like an STL container
332      /// (by having begin() and end()) which you can use in range-based
333      /// for loops, STL algorithms, etc.
334      /// For example if g is a Digraph and u is a node, you can write:
335      ///\code
336      /// for(auto a: g.inArcs(u))
337      ///   doSomething(a);
338      ///
339      /// //Using an STL algorithm:
340      /// copy(g.inArcs(u).begin(), g.inArcs(u).end(), vect.begin());
341      ///\endcode
342      LemonRangeWrapper2<InArcIt, Digraph, Node> inArcs(const Node& u) const {
343        return LemonRangeWrapper2<InArcIt, Digraph, Node>(*this, u);
344      }
345
346
[781]347      /// Iterator class for the arcs.
348
349      /// This iterator goes through each arc of the digraph.
[833]350      /// Its usage is quite simple, for example, you can count the number
[781]351      /// of arcs in a digraph \c g of type \c %Digraph as follows:
[57]352      ///\code
353      /// int count=0;
[781]354      /// for(Digraph::ArcIt a(g); a!=INVALID; ++a) ++count;
[57]355      ///\endcode
356      class ArcIt : public Arc {
357      public:
358        /// Default constructor
359
[781]360        /// Default constructor.
361        /// \warning It sets the iterator to an undefined value.
[57]362        ArcIt() { }
363        /// Copy constructor.
364
365        /// Copy constructor.
366        ///
367        ArcIt(const ArcIt& e) : Arc(e) { }
[781]368        /// %Invalid constructor \& conversion.
[57]369
[781]370        /// Initializes the iterator to be invalid.
371        /// \sa Invalid for more details.
372        ArcIt(Invalid) { }
373        /// Sets the iterator to the first arc.
374
375        /// Sets the iterator to the first arc of the given digraph.
[57]376        ///
[1271]377        explicit ArcIt(const Digraph& g) {
378          ::lemon::ignore_unused_variable_warning(g);
379        }
[781]380        /// Sets the iterator to the given arc.
[209]381
[781]382        /// Sets the iterator to the given arc of the given digraph.
383        ///
[209]384        ArcIt(const Digraph&, const Arc&) { }
[781]385        /// Next arc
[209]386
[57]387        /// Assign the iterator to the next arc.
[781]388        ///
[57]389        ArcIt& operator++() { return *this; }
390      };
391
[1336]392      /// \brief Gets the collection of the arcs of the digraph.
393      ///
394      /// This function can be used for iterating on the
395      /// arcs of the digraph. It returns a wrapped
396      /// ArcIt, which looks like an STL container
397      /// (by having begin() and end()) which you can use in range-based
398      /// for loops, STL algorithms, etc.
399      /// For example you can write:
400      ///\code
401      /// ListDigraph g;
402      /// for(auto a: g.arcs())
403      ///   doSomething(a);
404      ///
405      /// //Using an STL algorithm:
406      /// copy(g.arcs().begin(), g.arcs().end(), vect.begin());
407      ///\endcode
408      LemonRangeWrapper1<ArcIt, Digraph> arcs() const {
409        return LemonRangeWrapper1<ArcIt, Digraph>(*this);
410      }
411
412
[781]413      /// \brief The source node of the arc.
[57]414      ///
[781]415      /// Returns the source node of the given arc.
[57]416      Node source(Arc) const { return INVALID; }
417
[781]418      /// \brief The target node of the arc.
419      ///
420      /// Returns the target node of the given arc.
421      Node target(Arc) const { return INVALID; }
422
423      /// \brief The ID of the node.
424      ///
425      /// Returns the ID of the given node.
[209]426      int id(Node) const { return -1; }
[61]427
[781]428      /// \brief The ID of the arc.
429      ///
430      /// Returns the ID of the given arc.
[209]431      int id(Arc) const { return -1; }
[61]432
[781]433      /// \brief The node with the given ID.
[61]434      ///
[781]435      /// Returns the node with the given ID.
436      /// \pre The argument should be a valid node ID in the digraph.
[209]437      Node nodeFromId(int) const { return INVALID; }
[61]438
[781]439      /// \brief The arc with the given ID.
[61]440      ///
[781]441      /// Returns the arc with the given ID.
442      /// \pre The argument should be a valid arc ID in the digraph.
[209]443      Arc arcFromId(int) const { return INVALID; }
[61]444
[781]445      /// \brief An upper bound on the node IDs.
446      ///
447      /// Returns an upper bound on the node IDs.
[209]448      int maxNodeId() const { return -1; }
[61]449
[781]450      /// \brief An upper bound on the arc IDs.
451      ///
452      /// Returns an upper bound on the arc IDs.
[209]453      int maxArcId() const { return -1; }
[61]454
[57]455      void first(Node&) const {}
456      void next(Node&) const {}
457
458      void first(Arc&) const {}
459      void next(Arc&) const {}
460
461
462      void firstIn(Arc&, const Node&) const {}
463      void nextIn(Arc&) const {}
464
465      void firstOut(Arc&, const Node&) const {}
466      void nextOut(Arc&) const {}
467
[61]468      // The second parameter is dummy.
469      Node fromId(int, Node) const { return INVALID; }
470      // The second parameter is dummy.
471      Arc fromId(int, Arc) const { return INVALID; }
472
473      // Dummy parameter.
[209]474      int maxId(Node) const { return -1; }
[61]475      // Dummy parameter.
[209]476      int maxId(Arc) const { return -1; }
[61]477
[781]478      /// \brief The opposite node on the arc.
479      ///
480      /// Returns the opposite node on the given arc.
481      Node oppositeNode(Node, Arc) const { return INVALID; }
482
[57]483      /// \brief The base node of the iterator.
484      ///
[781]485      /// Returns the base node of the given outgoing arc iterator
486      /// (i.e. the source node of the corresponding arc).
487      Node baseNode(OutArcIt) const { return INVALID; }
[57]488
489      /// \brief The running node of the iterator.
490      ///
[781]491      /// Returns the running node of the given outgoing arc iterator
492      /// (i.e. the target node of the corresponding arc).
493      Node runningNode(OutArcIt) const { return INVALID; }
[57]494
495      /// \brief The base node of the iterator.
496      ///
[1217]497      /// Returns the base node of the given incoming arc iterator
[781]498      /// (i.e. the target node of the corresponding arc).
499      Node baseNode(InArcIt) const { return INVALID; }
[57]500
501      /// \brief The running node of the iterator.
502      ///
[1217]503      /// Returns the running node of the given incoming arc iterator
[781]504      /// (i.e. the source node of the corresponding arc).
505      Node runningNode(InArcIt) const { return INVALID; }
[57]506
[781]507      /// \brief Standard graph map type for the nodes.
[57]508      ///
[781]509      /// Standard graph map type for the nodes.
510      /// It conforms to the ReferenceMap concept.
[209]511      template<class T>
[627]512      class NodeMap : public ReferenceMap<Node, T, T&, const T&> {
[57]513      public:
514
[781]515        /// Constructor
516        explicit NodeMap(const Digraph&) { }
517        /// Constructor with given initial value
[57]518        NodeMap(const Digraph&, T) { }
519
[263]520      private:
[57]521        ///Copy constructor
[956]522        NodeMap(const NodeMap& nm) :
[627]523          ReferenceMap<Node, T, T&, const T&>(nm) { }
[57]524        ///Assignment operator
525        template <typename CMap>
[209]526        NodeMap& operator=(const CMap&) {
[57]527          checkConcept<ReadMap<Node, T>, CMap>();
[209]528          return *this;
[57]529        }
530      };
531
[781]532      /// \brief Standard graph map type for the arcs.
[57]533      ///
[781]534      /// Standard graph map type for the arcs.
535      /// It conforms to the ReferenceMap concept.
[209]536      template<class T>
[627]537      class ArcMap : public ReferenceMap<Arc, T, T&, const T&> {
[57]538      public:
539
[781]540        /// Constructor
541        explicit ArcMap(const Digraph&) { }
542        /// Constructor with given initial value
[57]543        ArcMap(const Digraph&, T) { }
[781]544
[263]545      private:
[57]546        ///Copy constructor
[627]547        ArcMap(const ArcMap& em) :
548          ReferenceMap<Arc, T, T&, const T&>(em) { }
[57]549        ///Assignment operator
550        template <typename CMap>
[209]551        ArcMap& operator=(const CMap&) {
[57]552          checkConcept<ReadMap<Arc, T>, CMap>();
[209]553          return *this;
[57]554        }
555      };
556
[125]557      template <typename _Digraph>
[57]558      struct Constraints {
559        void constraints() {
[627]560          checkConcept<BaseDigraphComponent, _Digraph>();
[125]561          checkConcept<IterableDigraphComponent<>, _Digraph>();
[209]562          checkConcept<IDableDigraphComponent<>, _Digraph>();
[125]563          checkConcept<MappableDigraphComponent<>, _Digraph>();
[57]564        }
565      };
566
567    };
[209]568
569  } //namespace concepts
[57]570} //namespace lemon
571
572
573
[576]574#endif
Note: See TracBrowser for help on using the repository browser.