COIN-OR::LEMON - Graph Library

source: lemon/lemon/concepts/graph_components.h @ 581:6d3a9eec82b4

Last change on this file since 581:6d3a9eec82b4 was 581:6d3a9eec82b4, checked in by Alpar Juttner <alpar@…>, 15 years ago

Merge

File size: 45.7 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2009
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 graph components.
22
23
24#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H
25#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H
26
27#include <lemon/core.h>
28#include <lemon/concepts/maps.h>
29
30#include <lemon/bits/alteration_notifier.h>
31
32namespace lemon {
33  namespace concepts {
34
35    /// \brief Skeleton class for graph Node and Arc types
36    ///
37    /// This class describes the interface of Node and Arc (and Edge
38    /// in undirected graphs) subtypes of graph types.
39    ///
40    /// \note This class is a template class so that we can use it to
41    /// create graph skeleton classes. The reason for this is than Node
42    /// and Arc types should \em not derive from the same base class.
43    /// For Node you should instantiate it with character 'n' and for Arc
44    /// with 'a'.
45
46#ifndef DOXYGEN
47    template <char _selector = '0'>
48#endif
49    class GraphItem {
50    public:
51      /// \brief Default constructor.
52      ///
53      /// \warning The default constructor is not required to set
54      /// the item to some well-defined value. So you should consider it
55      /// as uninitialized.
56      GraphItem() {}
57      /// \brief Copy constructor.
58      ///
59      /// Copy constructor.
60      ///
61      GraphItem(const GraphItem &) {}
62      /// \brief Invalid constructor \& conversion.
63      ///
64      /// This constructor initializes the item to be invalid.
65      /// \sa Invalid for more details.
66      GraphItem(Invalid) {}
67      /// \brief Assign operator for nodes.
68      ///
69      /// The nodes are assignable.
70      ///
71      GraphItem& operator=(GraphItem const&) { return *this; }
72      /// \brief Equality operator.
73      ///
74      /// Two iterators are equal if and only if they represents the
75      /// same node in the graph or both are invalid.
76      bool operator==(GraphItem) const { return false; }
77      /// \brief Inequality operator.
78      ///
79      /// \sa operator==(const Node& n)
80      ///
81      bool operator!=(GraphItem) const { return false; }
82
83      /// \brief Artificial ordering operator.
84      ///
85      /// To allow the use of graph descriptors as key type in std::map or
86      /// similar associative container we require this.
87      ///
88      /// \note This operator only have to define some strict ordering of
89      /// the items; this order has nothing to do with the iteration
90      /// ordering of the items.
91      bool operator<(GraphItem) const { return false; }
92
93      template<typename _GraphItem>
94      struct Constraints {
95        void constraints() {
96          _GraphItem i1;
97          _GraphItem i2 = i1;
98          _GraphItem i3 = INVALID;
99
100          i1 = i2 = i3;
101
102          bool b;
103          //          b = (ia == ib) && (ia != ib) && (ia < ib);
104          b = (ia == ib) && (ia != ib);
105          b = (ia == INVALID) && (ib != INVALID);
106          b = (ia < ib);
107        }
108
109        const _GraphItem &ia;
110        const _GraphItem &ib;
111      };
112    };
113
114    /// \brief An empty base directed graph class.
115    ///
116    /// This class provides the minimal set of features needed for a
117    /// directed graph structure. All digraph concepts have to
118    /// conform to this base directed graph. It just provides types
119    /// for nodes and arcs and functions to get the source and the
120    /// target of the arcs.
121    class BaseDigraphComponent {
122    public:
123
124      typedef BaseDigraphComponent Digraph;
125
126      /// \brief Node class of the digraph.
127      ///
128      /// This class represents the Nodes of the digraph.
129      ///
130      typedef GraphItem<'n'> Node;
131
132      /// \brief Arc class of the digraph.
133      ///
134      /// This class represents the Arcs of the digraph.
135      ///
136      typedef GraphItem<'e'> Arc;
137
138      /// \brief Gives back the target node of an arc.
139      ///
140      /// Gives back the target node of an arc.
141      ///
142      Node target(const Arc&) const { return INVALID;}
143
144      /// \brief Gives back the source node of an arc.
145      ///
146      /// Gives back the source node of an arc.
147      ///
148      Node source(const Arc&) const { return INVALID;}
149
150      /// \brief Gives back the opposite node on the given arc.
151      ///
152      /// Gives back the opposite node on the given arc.
153      Node oppositeNode(const Node&, const Arc&) const {
154        return INVALID;
155      }
156
157      template <typename _Digraph>
158      struct Constraints {
159        typedef typename _Digraph::Node Node;
160        typedef typename _Digraph::Arc Arc;
161
162        void constraints() {
163          checkConcept<GraphItem<'n'>, Node>();
164          checkConcept<GraphItem<'a'>, Arc>();
165          {
166            Node n;
167            Arc e(INVALID);
168            n = digraph.source(e);
169            n = digraph.target(e);
170            n = digraph.oppositeNode(n, e);
171          }
172        }
173
174        const _Digraph& digraph;
175      };
176    };
177
178    /// \brief An empty base undirected graph class.
179    ///
180    /// This class provides the minimal set of features needed for an
181    /// undirected graph structure. All undirected graph concepts have
182    /// to conform to this base graph. It just provides types for
183    /// nodes, arcs and edges and functions to get the
184    /// source and the target of the arcs and edges,
185    /// conversion from arcs to edges and function to get
186    /// both direction of the edges.
187    class BaseGraphComponent : public BaseDigraphComponent {
188    public:
189      typedef BaseDigraphComponent::Node Node;
190      typedef BaseDigraphComponent::Arc Arc;
191      /// \brief Undirected arc class of the graph.
192      ///
193      /// This class represents the edges of the graph.
194      /// The undirected graphs can be used as a directed graph which
195      /// for each arc contains the opposite arc too so the graph is
196      /// bidirected. The edge represents two opposite
197      /// directed arcs.
198      class Edge : public GraphItem<'u'> {
199      public:
200        typedef GraphItem<'u'> Parent;
201        /// \brief Default constructor.
202        ///
203        /// \warning The default constructor is not required to set
204        /// the item to some well-defined value. So you should consider it
205        /// as uninitialized.
206        Edge() {}
207        /// \brief Copy constructor.
208        ///
209        /// Copy constructor.
210        ///
211        Edge(const Edge &) : Parent() {}
212        /// \brief Invalid constructor \& conversion.
213        ///
214        /// This constructor initializes the item to be invalid.
215        /// \sa Invalid for more details.
216        Edge(Invalid) {}
217        /// \brief Converter from arc to edge.
218        ///
219        /// Besides the core graph item functionality each arc should
220        /// be convertible to the represented edge.
221        Edge(const Arc&) {}
222        /// \brief Assign arc to edge.
223        ///
224        /// Besides the core graph item functionality each arc should
225        /// be convertible to the represented edge.
226        Edge& operator=(const Arc&) { return *this; }
227      };
228
229      /// \brief Returns the direction of the arc.
230      ///
231      /// Returns the direction of the arc. Each arc represents an
232      /// edge with a direction. It gives back the
233      /// direction.
234      bool direction(const Arc&) const { return true; }
235
236      /// \brief Returns the directed arc.
237      ///
238      /// Returns the directed arc from its direction and the
239      /// represented edge.
240      Arc direct(const Edge&, bool) const { return INVALID;}
241
242      /// \brief Returns the directed arc.
243      ///
244      /// Returns the directed arc from its source and the
245      /// represented edge.
246      Arc direct(const Edge&, const Node&) const { return INVALID;}
247
248      /// \brief Returns the opposite arc.
249      ///
250      /// Returns the opposite arc. It is the arc representing the
251      /// same edge and has opposite direction.
252      Arc oppositeArc(const Arc&) const { return INVALID;}
253
254      /// \brief Gives back one ending of an edge.
255      ///
256      /// Gives back one ending of an edge.
257      Node u(const Edge&) const { return INVALID;}
258
259      /// \brief Gives back the other ending of an edge.
260      ///
261      /// Gives back the other ending of an edge.
262      Node v(const Edge&) const { return INVALID;}
263
264      template <typename _Graph>
265      struct Constraints {
266        typedef typename _Graph::Node Node;
267        typedef typename _Graph::Arc Arc;
268        typedef typename _Graph::Edge Edge;
269
270        void constraints() {
271          checkConcept<BaseDigraphComponent, _Graph>();
272          checkConcept<GraphItem<'u'>, Edge>();
273          {
274            Node n;
275            Edge ue(INVALID);
276            Arc e;
277            n = graph.u(ue);
278            n = graph.v(ue);
279            e = graph.direct(ue, true);
280            e = graph.direct(ue, n);
281            e = graph.oppositeArc(e);
282            ue = e;
283            bool d = graph.direction(e);
284            ignore_unused_variable_warning(d);
285          }
286        }
287
288        const _Graph& graph;
289      };
290
291    };
292
293    /// \brief An empty idable base digraph class.
294    ///
295    /// This class provides beside the core digraph features
296    /// core id functions for the digraph structure.
297    /// The most of the base digraphs should conform to this concept.
298    /// The id's are unique and immutable.
299    template <typename _Base = BaseDigraphComponent>
300    class IDableDigraphComponent : public _Base {
301    public:
302
303      typedef _Base Base;
304      typedef typename Base::Node Node;
305      typedef typename Base::Arc Arc;
306
307      /// \brief Gives back an unique integer id for the Node.
308      ///
309      /// Gives back an unique integer id for the Node.
310      ///
311      int id(const Node&) const { return -1;}
312
313      /// \brief Gives back the node by the unique id.
314      ///
315      /// Gives back the node by the unique id.
316      /// If the digraph does not contain node with the given id
317      /// then the result of the function is undetermined.
318      Node nodeFromId(int) const { return INVALID;}
319
320      /// \brief Gives back an unique integer id for the Arc.
321      ///
322      /// Gives back an unique integer id for the Arc.
323      ///
324      int id(const Arc&) const { return -1;}
325
326      /// \brief Gives back the arc by the unique id.
327      ///
328      /// Gives back the arc by the unique id.
329      /// If the digraph does not contain arc with the given id
330      /// then the result of the function is undetermined.
331      Arc arcFromId(int) const { return INVALID;}
332
333      /// \brief Gives back an integer greater or equal to the maximum
334      /// Node id.
335      ///
336      /// Gives back an integer greater or equal to the maximum Node
337      /// id.
338      int maxNodeId() const { return -1;}
339
340      /// \brief Gives back an integer greater or equal to the maximum
341      /// Arc id.
342      ///
343      /// Gives back an integer greater or equal to the maximum Arc
344      /// id.
345      int maxArcId() const { return -1;}
346
347      template <typename _Digraph>
348      struct Constraints {
349
350        void constraints() {
351          checkConcept<Base, _Digraph >();
352          typename _Digraph::Node node;
353          int nid = digraph.id(node);
354          nid = digraph.id(node);
355          node = digraph.nodeFromId(nid);
356          typename _Digraph::Arc arc;
357          int eid = digraph.id(arc);
358          eid = digraph.id(arc);
359          arc = digraph.arcFromId(eid);
360
361          nid = digraph.maxNodeId();
362          ignore_unused_variable_warning(nid);
363          eid = digraph.maxArcId();
364          ignore_unused_variable_warning(eid);
365        }
366
367        const _Digraph& digraph;
368      };
369    };
370
371    /// \brief An empty idable base undirected graph class.
372    ///
373    /// This class provides beside the core undirected graph features
374    /// core id functions for the undirected graph structure.  The
375    /// most of the base undirected graphs should conform to this
376    /// concept.  The id's are unique and immutable.
377    template <typename _Base = BaseGraphComponent>
378    class IDableGraphComponent : public IDableDigraphComponent<_Base> {
379    public:
380
381      typedef _Base Base;
382      typedef typename Base::Edge Edge;
383
384      using IDableDigraphComponent<_Base>::id;
385
386      /// \brief Gives back an unique integer id for the Edge.
387      ///
388      /// Gives back an unique integer id for the Edge.
389      ///
390      int id(const Edge&) const { return -1;}
391
392      /// \brief Gives back the edge by the unique id.
393      ///
394      /// Gives back the edge by the unique id.  If the
395      /// graph does not contain arc with the given id then the
396      /// result of the function is undetermined.
397      Edge edgeFromId(int) const { return INVALID;}
398
399      /// \brief Gives back an integer greater or equal to the maximum
400      /// Edge id.
401      ///
402      /// Gives back an integer greater or equal to the maximum Edge
403      /// id.
404      int maxEdgeId() const { return -1;}
405
406      template <typename _Graph>
407      struct Constraints {
408
409        void constraints() {
410          checkConcept<Base, _Graph >();
411          checkConcept<IDableDigraphComponent<Base>, _Graph >();
412          typename _Graph::Edge edge;
413          int ueid = graph.id(edge);
414          ueid = graph.id(edge);
415          edge = graph.edgeFromId(ueid);
416          ueid = graph.maxEdgeId();
417          ignore_unused_variable_warning(ueid);
418        }
419
420        const _Graph& graph;
421      };
422    };
423
424    /// \brief Skeleton class for graph NodeIt and ArcIt
425    ///
426    /// Skeleton class for graph NodeIt and ArcIt.
427    ///
428    template <typename _Graph, typename _Item>
429    class GraphItemIt : public _Item {
430    public:
431      /// \brief Default constructor.
432      ///
433      /// @warning The default constructor sets the iterator
434      /// to an undefined value.
435      GraphItemIt() {}
436      /// \brief Copy constructor.
437      ///
438      /// Copy constructor.
439      ///
440      GraphItemIt(const GraphItemIt& ) {}
441      /// \brief Sets the iterator to the first item.
442      ///
443      /// Sets the iterator to the first item of \c the graph.
444      ///
445      explicit GraphItemIt(const _Graph&) {}
446      /// \brief Invalid constructor \& conversion.
447      ///
448      /// This constructor initializes the item to be invalid.
449      /// \sa Invalid for more details.
450      GraphItemIt(Invalid) {}
451      /// \brief Assign operator for items.
452      ///
453      /// The items are assignable.
454      ///
455      GraphItemIt& operator=(const GraphItemIt&) { return *this; }
456      /// \brief Next item.
457      ///
458      /// Assign the iterator to the next item.
459      ///
460      GraphItemIt& operator++() { return *this; }
461      /// \brief Equality operator
462      ///
463      /// Two iterators are equal if and only if they point to the
464      /// same object or both are invalid.
465      bool operator==(const GraphItemIt&) const { return true;}
466      /// \brief Inequality operator
467      ///
468      /// \sa operator==(Node n)
469      ///
470      bool operator!=(const GraphItemIt&) const { return true;}
471
472      template<typename _GraphItemIt>
473      struct Constraints {
474        void constraints() {
475          _GraphItemIt it1(g);
476          _GraphItemIt it2;
477
478          it2 = ++it1;
479          ++it2 = it1;
480          ++(++it1);
481
482          _Item bi = it1;
483          bi = it2;
484        }
485        _Graph& g;
486      };
487    };
488
489    /// \brief Skeleton class for graph InArcIt and OutArcIt
490    ///
491    /// \note Because InArcIt and OutArcIt may not inherit from the same
492    /// base class, the _selector is a additional template parameter. For
493    /// InArcIt you should instantiate it with character 'i' and for
494    /// OutArcIt with 'o'.
495    template <typename _Graph,
496              typename _Item = typename _Graph::Arc,
497              typename _Base = typename _Graph::Node,
498              char _selector = '0'>
499    class GraphIncIt : public _Item {
500    public:
501      /// \brief Default constructor.
502      ///
503      /// @warning The default constructor sets the iterator
504      /// to an undefined value.
505      GraphIncIt() {}
506      /// \brief Copy constructor.
507      ///
508      /// Copy constructor.
509      ///
510      GraphIncIt(GraphIncIt const& gi) : _Item(gi) {}
511      /// \brief Sets the iterator to the first arc incoming into or outgoing
512      /// from the node.
513      ///
514      /// Sets the iterator to the first arc incoming into or outgoing
515      /// from the node.
516      ///
517      explicit GraphIncIt(const _Graph&, const _Base&) {}
518      /// \brief Invalid constructor \& conversion.
519      ///
520      /// This constructor initializes the item to be invalid.
521      /// \sa Invalid for more details.
522      GraphIncIt(Invalid) {}
523      /// \brief Assign operator for iterators.
524      ///
525      /// The iterators are assignable.
526      ///
527      GraphIncIt& operator=(GraphIncIt const&) { return *this; }
528      /// \brief Next item.
529      ///
530      /// Assign the iterator to the next item.
531      ///
532      GraphIncIt& operator++() { return *this; }
533
534      /// \brief Equality operator
535      ///
536      /// Two iterators are equal if and only if they point to the
537      /// same object or both are invalid.
538      bool operator==(const GraphIncIt&) const { return true;}
539
540      /// \brief Inequality operator
541      ///
542      /// \sa operator==(Node n)
543      ///
544      bool operator!=(const GraphIncIt&) const { return true;}
545
546      template <typename _GraphIncIt>
547      struct Constraints {
548        void constraints() {
549          checkConcept<GraphItem<_selector>, _GraphIncIt>();
550          _GraphIncIt it1(graph, node);
551          _GraphIncIt it2;
552
553          it2 = ++it1;
554          ++it2 = it1;
555          ++(++it1);
556          _Item e = it1;
557          e = it2;
558
559        }
560
561        _Item arc;
562        _Base node;
563        _Graph graph;
564        _GraphIncIt it;
565      };
566    };
567
568
569    /// \brief An empty iterable digraph class.
570    ///
571    /// This class provides beside the core digraph features
572    /// iterator based iterable interface for the digraph structure.
573    /// This concept is part of the Digraph concept.
574    template <typename _Base = BaseDigraphComponent>
575    class IterableDigraphComponent : public _Base {
576
577    public:
578
579      typedef _Base Base;
580      typedef typename Base::Node Node;
581      typedef typename Base::Arc Arc;
582
583      typedef IterableDigraphComponent Digraph;
584
585      /// \name Base iteration
586      ///
587      /// This interface provides functions for iteration on digraph items
588      ///
589      /// @{
590
591      /// \brief Gives back the first node in the iterating order.
592      ///
593      /// Gives back the first node in the iterating order.
594      ///
595      void first(Node&) const {}
596
597      /// \brief Gives back the next node in the iterating order.
598      ///
599      /// Gives back the next node in the iterating order.
600      ///
601      void next(Node&) const {}
602
603      /// \brief Gives back the first arc in the iterating order.
604      ///
605      /// Gives back the first arc in the iterating order.
606      ///
607      void first(Arc&) const {}
608
609      /// \brief Gives back the next arc in the iterating order.
610      ///
611      /// Gives back the next arc in the iterating order.
612      ///
613      void next(Arc&) const {}
614
615
616      /// \brief Gives back the first of the arcs point to the given
617      /// node.
618      ///
619      /// Gives back the first of the arcs point to the given node.
620      ///
621      void firstIn(Arc&, const Node&) const {}
622
623      /// \brief Gives back the next of the arcs points to the given
624      /// node.
625      ///
626      /// Gives back the next of the arcs points to the given node.
627      ///
628      void nextIn(Arc&) const {}
629
630      /// \brief Gives back the first of the arcs start from the
631      /// given node.
632      ///
633      /// Gives back the first of the arcs start from the given node.
634      ///
635      void firstOut(Arc&, const Node&) const {}
636
637      /// \brief Gives back the next of the arcs start from the given
638      /// node.
639      ///
640      /// Gives back the next of the arcs start from the given node.
641      ///
642      void nextOut(Arc&) const {}
643
644      /// @}
645
646      /// \name Class based iteration
647      ///
648      /// This interface provides functions for iteration on digraph items
649      ///
650      /// @{
651
652      /// \brief This iterator goes through each node.
653      ///
654      /// This iterator goes through each node.
655      ///
656      typedef GraphItemIt<Digraph, Node> NodeIt;
657
658      /// \brief This iterator goes through each node.
659      ///
660      /// This iterator goes through each node.
661      ///
662      typedef GraphItemIt<Digraph, Arc> ArcIt;
663
664      /// \brief This iterator goes trough the incoming arcs of a node.
665      ///
666      /// This iterator goes trough the \e inccoming arcs of a certain node
667      /// of a digraph.
668      typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt;
669
670      /// \brief This iterator goes trough the outgoing arcs of a node.
671      ///
672      /// This iterator goes trough the \e outgoing arcs of a certain node
673      /// of a digraph.
674      typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt;
675
676      /// \brief The base node of the iterator.
677      ///
678      /// Gives back the base node of the iterator.
679      /// It is always the target of the pointed arc.
680      Node baseNode(const InArcIt&) const { return INVALID; }
681
682      /// \brief The running node of the iterator.
683      ///
684      /// Gives back the running node of the iterator.
685      /// It is always the source of the pointed arc.
686      Node runningNode(const InArcIt&) const { return INVALID; }
687
688      /// \brief The base node of the iterator.
689      ///
690      /// Gives back the base node of the iterator.
691      /// It is always the source of the pointed arc.
692      Node baseNode(const OutArcIt&) const { return INVALID; }
693
694      /// \brief The running node of the iterator.
695      ///
696      /// Gives back the running node of the iterator.
697      /// It is always the target of the pointed arc.
698      Node runningNode(const OutArcIt&) const { return INVALID; }
699
700      /// @}
701
702      template <typename _Digraph>
703      struct Constraints {
704        void constraints() {
705          checkConcept<Base, _Digraph>();
706
707          {
708            typename _Digraph::Node node(INVALID);
709            typename _Digraph::Arc arc(INVALID);
710            {
711              digraph.first(node);
712              digraph.next(node);
713            }
714            {
715              digraph.first(arc);
716              digraph.next(arc);
717            }
718            {
719              digraph.firstIn(arc, node);
720              digraph.nextIn(arc);
721            }
722            {
723              digraph.firstOut(arc, node);
724              digraph.nextOut(arc);
725            }
726          }
727
728          {
729            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>,
730              typename _Digraph::ArcIt >();
731            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>,
732              typename _Digraph::NodeIt >();
733            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
734              typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>();
735            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
736              typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>();
737
738            typename _Digraph::Node n;
739            typename _Digraph::InArcIt ieit(INVALID);
740            typename _Digraph::OutArcIt oeit(INVALID);
741            n = digraph.baseNode(ieit);
742            n = digraph.runningNode(ieit);
743            n = digraph.baseNode(oeit);
744            n = digraph.runningNode(oeit);
745            ignore_unused_variable_warning(n);
746          }
747        }
748
749        const _Digraph& digraph;
750
751      };
752    };
753
754    /// \brief An empty iterable undirected graph class.
755    ///
756    /// This class provides beside the core graph features iterator
757    /// based iterable interface for the undirected graph structure.
758    /// This concept is part of the Graph concept.
759    template <typename _Base = BaseGraphComponent>
760    class IterableGraphComponent : public IterableDigraphComponent<_Base> {
761    public:
762
763      typedef _Base Base;
764      typedef typename Base::Node Node;
765      typedef typename Base::Arc Arc;
766      typedef typename Base::Edge Edge;
767
768
769      typedef IterableGraphComponent Graph;
770
771      /// \name Base iteration
772      ///
773      /// This interface provides functions for iteration on graph items
774      /// @{
775
776      using IterableDigraphComponent<_Base>::first;
777      using IterableDigraphComponent<_Base>::next;
778
779      /// \brief Gives back the first edge in the iterating
780      /// order.
781      ///
782      /// Gives back the first edge in the iterating order.
783      ///
784      void first(Edge&) const {}
785
786      /// \brief Gives back the next edge in the iterating
787      /// order.
788      ///
789      /// Gives back the next edge in the iterating order.
790      ///
791      void next(Edge&) const {}
792
793
794      /// \brief Gives back the first of the edges from the
795      /// given node.
796      ///
797      /// Gives back the first of the edges from the given
798      /// node. The bool parameter gives back that direction which
799      /// gives a good direction of the edge so the source of the
800      /// directed arc is the given node.
801      void firstInc(Edge&, bool&, const Node&) const {}
802
803      /// \brief Gives back the next of the edges from the
804      /// given node.
805      ///
806      /// Gives back the next of the edges from the given
807      /// node. The bool parameter should be used as the \c firstInc()
808      /// use it.
809      void nextInc(Edge&, bool&) const {}
810
811      using IterableDigraphComponent<_Base>::baseNode;
812      using IterableDigraphComponent<_Base>::runningNode;
813
814      /// @}
815
816      /// \name Class based iteration
817      ///
818      /// This interface provides functions for iteration on graph items
819      ///
820      /// @{
821
822      /// \brief This iterator goes through each node.
823      ///
824      /// This iterator goes through each node.
825      typedef GraphItemIt<Graph, Edge> EdgeIt;
826      /// \brief This iterator goes trough the incident arcs of a
827      /// node.
828      ///
829      /// This iterator goes trough the incident arcs of a certain
830      /// node of a graph.
831      typedef GraphIncIt<Graph, Edge, Node, 'u'> IncEdgeIt;
832      /// \brief The base node of the iterator.
833      ///
834      /// Gives back the base node of the iterator.
835      Node baseNode(const IncEdgeIt&) const { return INVALID; }
836
837      /// \brief The running node of the iterator.
838      ///
839      /// Gives back the running node of the iterator.
840      Node runningNode(const IncEdgeIt&) const { return INVALID; }
841
842      /// @}
843
844      template <typename _Graph>
845      struct Constraints {
846        void constraints() {
847          checkConcept<IterableDigraphComponent<Base>, _Graph>();
848
849          {
850            typename _Graph::Node node(INVALID);
851            typename _Graph::Edge edge(INVALID);
852            bool dir;
853            {
854              graph.first(edge);
855              graph.next(edge);
856            }
857            {
858              graph.firstInc(edge, dir, node);
859              graph.nextInc(edge, dir);
860            }
861
862          }
863
864          {
865            checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>,
866              typename _Graph::EdgeIt >();
867            checkConcept<GraphIncIt<_Graph, typename _Graph::Edge,
868              typename _Graph::Node, 'u'>, typename _Graph::IncEdgeIt>();
869
870            typename _Graph::Node n;
871            typename _Graph::IncEdgeIt ueit(INVALID);
872            n = graph.baseNode(ueit);
873            n = graph.runningNode(ueit);
874          }
875        }
876
877        const _Graph& graph;
878
879      };
880    };
881
882    /// \brief An empty alteration notifier digraph class.
883    ///
884    /// This class provides beside the core digraph features alteration
885    /// notifier interface for the digraph structure.  This implements
886    /// an observer-notifier pattern for each digraph item. More
887    /// obsevers can be registered into the notifier and whenever an
888    /// alteration occured in the digraph all the observers will
889    /// notified about it.
890    template <typename _Base = BaseDigraphComponent>
891    class AlterableDigraphComponent : public _Base {
892    public:
893
894      typedef _Base Base;
895      typedef typename Base::Node Node;
896      typedef typename Base::Arc Arc;
897
898
899      /// The node observer registry.
900      typedef AlterationNotifier<AlterableDigraphComponent, Node>
901      NodeNotifier;
902      /// The arc observer registry.
903      typedef AlterationNotifier<AlterableDigraphComponent, Arc>
904      ArcNotifier;
905
906      /// \brief Gives back the node alteration notifier.
907      ///
908      /// Gives back the node alteration notifier.
909      NodeNotifier& notifier(Node) const {
910        return NodeNotifier();
911      }
912
913      /// \brief Gives back the arc alteration notifier.
914      ///
915      /// Gives back the arc alteration notifier.
916      ArcNotifier& notifier(Arc) const {
917        return ArcNotifier();
918      }
919
920      template <typename _Digraph>
921      struct Constraints {
922        void constraints() {
923          checkConcept<Base, _Digraph>();
924          typename _Digraph::NodeNotifier& nn
925            = digraph.notifier(typename _Digraph::Node());
926
927          typename _Digraph::ArcNotifier& en
928            = digraph.notifier(typename _Digraph::Arc());
929
930          ignore_unused_variable_warning(nn);
931          ignore_unused_variable_warning(en);
932        }
933
934        const _Digraph& digraph;
935
936      };
937
938    };
939
940    /// \brief An empty alteration notifier undirected graph class.
941    ///
942    /// This class provides beside the core graph features alteration
943    /// notifier interface for the graph structure.  This implements
944    /// an observer-notifier pattern for each graph item. More
945    /// obsevers can be registered into the notifier and whenever an
946    /// alteration occured in the graph all the observers will
947    /// notified about it.
948    template <typename _Base = BaseGraphComponent>
949    class AlterableGraphComponent : public AlterableDigraphComponent<_Base> {
950    public:
951
952      typedef _Base Base;
953      typedef typename Base::Edge Edge;
954
955
956      /// The arc observer registry.
957      typedef AlterationNotifier<AlterableGraphComponent, Edge>
958      EdgeNotifier;
959
960      /// \brief Gives back the arc alteration notifier.
961      ///
962      /// Gives back the arc alteration notifier.
963      EdgeNotifier& notifier(Edge) const {
964        return EdgeNotifier();
965      }
966
967      template <typename _Graph>
968      struct Constraints {
969        void constraints() {
970          checkConcept<AlterableGraphComponent<Base>, _Graph>();
971          typename _Graph::EdgeNotifier& uen
972            = graph.notifier(typename _Graph::Edge());
973          ignore_unused_variable_warning(uen);
974        }
975
976        const _Graph& graph;
977
978      };
979
980    };
981
982    /// \brief Class describing the concept of graph maps
983    ///
984    /// This class describes the common interface of the graph maps
985    /// (NodeMap, ArcMap), that is maps that can be used to
986    /// associate data to graph descriptors (nodes or arcs).
987    template <typename _Graph, typename _Item, typename _Value>
988    class GraphMap : public ReadWriteMap<_Item, _Value> {
989    public:
990
991      typedef ReadWriteMap<_Item, _Value> Parent;
992
993      /// The graph type of the map.
994      typedef _Graph Graph;
995      /// The key type of the map.
996      typedef _Item Key;
997      /// The value type of the map.
998      typedef _Value Value;
999
1000      /// \brief Construct a new map.
1001      ///
1002      /// Construct a new map for the graph.
1003      explicit GraphMap(const Graph&) {}
1004      /// \brief Construct a new map with default value.
1005      ///
1006      /// Construct a new map for the graph and initalise the values.
1007      GraphMap(const Graph&, const Value&) {}
1008
1009    private:
1010      /// \brief Copy constructor.
1011      ///
1012      /// Copy Constructor.
1013      GraphMap(const GraphMap&) : Parent() {}
1014
1015      /// \brief Assign operator.
1016      ///
1017      /// Assign operator. It does not mofify the underlying graph,
1018      /// it just iterates on the current item set and set the  map
1019      /// with the value returned by the assigned map.
1020      template <typename CMap>
1021      GraphMap& operator=(const CMap&) {
1022        checkConcept<ReadMap<Key, Value>, CMap>();
1023        return *this;
1024      }
1025
1026    public:
1027      template<typename _Map>
1028      struct Constraints {
1029        void constraints() {
1030          checkConcept<ReadWriteMap<Key, Value>, _Map >();
1031          // Construction with a graph parameter
1032          _Map a(g);
1033          // Constructor with a graph and a default value parameter
1034          _Map a2(g,t);
1035          // Copy constructor.
1036          // _Map b(c);
1037
1038          // ReadMap<Key, Value> cmap;
1039          // b = cmap;
1040
1041          ignore_unused_variable_warning(a);
1042          ignore_unused_variable_warning(a2);
1043          // ignore_unused_variable_warning(b);
1044        }
1045
1046        const _Map &c;
1047        const Graph &g;
1048        const typename GraphMap::Value &t;
1049      };
1050
1051    };
1052
1053    /// \brief An empty mappable digraph class.
1054    ///
1055    /// This class provides beside the core digraph features
1056    /// map interface for the digraph structure.
1057    /// This concept is part of the Digraph concept.
1058    template <typename _Base = BaseDigraphComponent>
1059    class MappableDigraphComponent : public _Base  {
1060    public:
1061
1062      typedef _Base Base;
1063      typedef typename Base::Node Node;
1064      typedef typename Base::Arc Arc;
1065
1066      typedef MappableDigraphComponent Digraph;
1067
1068      /// \brief ReadWrite map of the nodes.
1069      ///
1070      /// ReadWrite map of the nodes.
1071      ///
1072      template <typename _Value>
1073      class NodeMap : public GraphMap<Digraph, Node, _Value> {
1074      public:
1075        typedef GraphMap<MappableDigraphComponent, Node, _Value> Parent;
1076
1077        /// \brief Construct a new map.
1078        ///
1079        /// Construct a new map for the digraph.
1080        explicit NodeMap(const MappableDigraphComponent& digraph)
1081          : Parent(digraph) {}
1082
1083        /// \brief Construct a new map with default value.
1084        ///
1085        /// Construct a new map for the digraph and initalise the values.
1086        NodeMap(const MappableDigraphComponent& digraph, const _Value& value)
1087          : Parent(digraph, value) {}
1088
1089      private:
1090        /// \brief Copy constructor.
1091        ///
1092        /// Copy Constructor.
1093        NodeMap(const NodeMap& nm) : Parent(nm) {}
1094
1095        /// \brief Assign operator.
1096        ///
1097        /// Assign operator.
1098        template <typename CMap>
1099        NodeMap& operator=(const CMap&) {
1100          checkConcept<ReadMap<Node, _Value>, CMap>();
1101          return *this;
1102        }
1103
1104      };
1105
1106      /// \brief ReadWrite map of the arcs.
1107      ///
1108      /// ReadWrite map of the arcs.
1109      ///
1110      template <typename _Value>
1111      class ArcMap : public GraphMap<Digraph, Arc, _Value> {
1112      public:
1113        typedef GraphMap<MappableDigraphComponent, Arc, _Value> Parent;
1114
1115        /// \brief Construct a new map.
1116        ///
1117        /// Construct a new map for the digraph.
1118        explicit ArcMap(const MappableDigraphComponent& digraph)
1119          : Parent(digraph) {}
1120
1121        /// \brief Construct a new map with default value.
1122        ///
1123        /// Construct a new map for the digraph and initalise the values.
1124        ArcMap(const MappableDigraphComponent& digraph, const _Value& value)
1125          : Parent(digraph, value) {}
1126
1127      private:
1128        /// \brief Copy constructor.
1129        ///
1130        /// Copy Constructor.
1131        ArcMap(const ArcMap& nm) : Parent(nm) {}
1132
1133        /// \brief Assign operator.
1134        ///
1135        /// Assign operator.
1136        template <typename CMap>
1137        ArcMap& operator=(const CMap&) {
1138          checkConcept<ReadMap<Arc, _Value>, CMap>();
1139          return *this;
1140        }
1141
1142      };
1143
1144
1145      template <typename _Digraph>
1146      struct Constraints {
1147
1148        struct Dummy {
1149          int value;
1150          Dummy() : value(0) {}
1151          Dummy(int _v) : value(_v) {}
1152        };
1153
1154        void constraints() {
1155          checkConcept<Base, _Digraph>();
1156          { // int map test
1157            typedef typename _Digraph::template NodeMap<int> IntNodeMap;
1158            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>,
1159              IntNodeMap >();
1160          } { // bool map test
1161            typedef typename _Digraph::template NodeMap<bool> BoolNodeMap;
1162            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>,
1163              BoolNodeMap >();
1164          } { // Dummy map test
1165            typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap;
1166            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>,
1167              DummyNodeMap >();
1168          }
1169
1170          { // int map test
1171            typedef typename _Digraph::template ArcMap<int> IntArcMap;
1172            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>,
1173              IntArcMap >();
1174          } { // bool map test
1175            typedef typename _Digraph::template ArcMap<bool> BoolArcMap;
1176            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>,
1177              BoolArcMap >();
1178          } { // Dummy map test
1179            typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap;
1180            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>,
1181              DummyArcMap >();
1182          }
1183        }
1184
1185        _Digraph& digraph;
1186      };
1187    };
1188
1189    /// \brief An empty mappable base bipartite graph class.
1190    ///
1191    /// This class provides beside the core graph features
1192    /// map interface for the graph structure.
1193    /// This concept is part of the Graph concept.
1194    template <typename _Base = BaseGraphComponent>
1195    class MappableGraphComponent : public MappableDigraphComponent<_Base>  {
1196    public:
1197
1198      typedef _Base Base;
1199      typedef typename Base::Edge Edge;
1200
1201      typedef MappableGraphComponent Graph;
1202
1203      /// \brief ReadWrite map of the edges.
1204      ///
1205      /// ReadWrite map of the edges.
1206      ///
1207      template <typename _Value>
1208      class EdgeMap : public GraphMap<Graph, Edge, _Value> {
1209      public:
1210        typedef GraphMap<MappableGraphComponent, Edge, _Value> Parent;
1211
1212        /// \brief Construct a new map.
1213        ///
1214        /// Construct a new map for the graph.
1215        explicit EdgeMap(const MappableGraphComponent& graph)
1216          : Parent(graph) {}
1217
1218        /// \brief Construct a new map with default value.
1219        ///
1220        /// Construct a new map for the graph and initalise the values.
1221        EdgeMap(const MappableGraphComponent& graph, const _Value& value)
1222          : Parent(graph, value) {}
1223
1224      private:
1225        /// \brief Copy constructor.
1226        ///
1227        /// Copy Constructor.
1228        EdgeMap(const EdgeMap& nm) : Parent(nm) {}
1229
1230        /// \brief Assign operator.
1231        ///
1232        /// Assign operator.
1233        template <typename CMap>
1234        EdgeMap& operator=(const CMap&) {
1235          checkConcept<ReadMap<Edge, _Value>, CMap>();
1236          return *this;
1237        }
1238
1239      };
1240
1241
1242      template <typename _Graph>
1243      struct Constraints {
1244
1245        struct Dummy {
1246          int value;
1247          Dummy() : value(0) {}
1248          Dummy(int _v) : value(_v) {}
1249        };
1250
1251        void constraints() {
1252          checkConcept<MappableGraphComponent<Base>, _Graph>();
1253
1254          { // int map test
1255            typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
1256            checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
1257              IntEdgeMap >();
1258          } { // bool map test
1259            typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
1260            checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
1261              BoolEdgeMap >();
1262          } { // Dummy map test
1263            typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap;
1264            checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>,
1265              DummyEdgeMap >();
1266          }
1267        }
1268
1269        _Graph& graph;
1270      };
1271    };
1272
1273    /// \brief An empty extendable digraph class.
1274    ///
1275    /// This class provides beside the core digraph features digraph
1276    /// extendable interface for the digraph structure.  The main
1277    /// difference between the base and this interface is that the
1278    /// digraph alterations should handled already on this level.
1279    template <typename _Base = BaseDigraphComponent>
1280    class ExtendableDigraphComponent : public _Base {
1281    public:
1282      typedef _Base Base;
1283
1284      typedef typename _Base::Node Node;
1285      typedef typename _Base::Arc Arc;
1286
1287      /// \brief Adds a new node to the digraph.
1288      ///
1289      /// Adds a new node to the digraph.
1290      ///
1291      Node addNode() {
1292        return INVALID;
1293      }
1294
1295      /// \brief Adds a new arc connects the given two nodes.
1296      ///
1297      /// Adds a new arc connects the the given two nodes.
1298      Arc addArc(const Node&, const Node&) {
1299        return INVALID;
1300      }
1301
1302      template <typename _Digraph>
1303      struct Constraints {
1304        void constraints() {
1305          checkConcept<Base, _Digraph>();
1306          typename _Digraph::Node node_a, node_b;
1307          node_a = digraph.addNode();
1308          node_b = digraph.addNode();
1309          typename _Digraph::Arc arc;
1310          arc = digraph.addArc(node_a, node_b);
1311        }
1312
1313        _Digraph& digraph;
1314      };
1315    };
1316
1317    /// \brief An empty extendable base undirected graph class.
1318    ///
1319    /// This class provides beside the core undirected graph features
1320    /// core undircted graph extend interface for the graph structure.
1321    /// The main difference between the base and this interface is
1322    /// that the graph alterations should handled already on this
1323    /// level.
1324    template <typename _Base = BaseGraphComponent>
1325    class ExtendableGraphComponent : public _Base {
1326    public:
1327
1328      typedef _Base Base;
1329      typedef typename _Base::Node Node;
1330      typedef typename _Base::Edge Edge;
1331
1332      /// \brief Adds a new node to the graph.
1333      ///
1334      /// Adds a new node to the graph.
1335      ///
1336      Node addNode() {
1337        return INVALID;
1338      }
1339
1340      /// \brief Adds a new arc connects the given two nodes.
1341      ///
1342      /// Adds a new arc connects the the given two nodes.
1343      Edge addArc(const Node&, const Node&) {
1344        return INVALID;
1345      }
1346
1347      template <typename _Graph>
1348      struct Constraints {
1349        void constraints() {
1350          checkConcept<Base, _Graph>();
1351          typename _Graph::Node node_a, node_b;
1352          node_a = graph.addNode();
1353          node_b = graph.addNode();
1354          typename _Graph::Edge edge;
1355          edge = graph.addEdge(node_a, node_b);
1356        }
1357
1358        _Graph& graph;
1359      };
1360    };
1361
1362    /// \brief An empty erasable digraph class.
1363    ///
1364    /// This class provides beside the core digraph features core erase
1365    /// functions for the digraph structure. The main difference between
1366    /// the base and this interface is that the digraph alterations
1367    /// should handled already on this level.
1368    template <typename _Base = BaseDigraphComponent>
1369    class ErasableDigraphComponent : public _Base {
1370    public:
1371
1372      typedef _Base Base;
1373      typedef typename Base::Node Node;
1374      typedef typename Base::Arc Arc;
1375
1376      /// \brief Erase a node from the digraph.
1377      ///
1378      /// Erase a node from the digraph. This function should
1379      /// erase all arcs connecting to the node.
1380      void erase(const Node&) {}
1381
1382      /// \brief Erase an arc from the digraph.
1383      ///
1384      /// Erase an arc from the digraph.
1385      ///
1386      void erase(const Arc&) {}
1387
1388      template <typename _Digraph>
1389      struct Constraints {
1390        void constraints() {
1391          checkConcept<Base, _Digraph>();
1392          typename _Digraph::Node node;
1393          digraph.erase(node);
1394          typename _Digraph::Arc arc;
1395          digraph.erase(arc);
1396        }
1397
1398        _Digraph& digraph;
1399      };
1400    };
1401
1402    /// \brief An empty erasable base undirected graph class.
1403    ///
1404    /// This class provides beside the core undirected graph features
1405    /// core erase functions for the undirceted graph structure. The
1406    /// main difference between the base and this interface is that
1407    /// the graph alterations should handled already on this level.
1408    template <typename _Base = BaseGraphComponent>
1409    class ErasableGraphComponent : public _Base {
1410    public:
1411
1412      typedef _Base Base;
1413      typedef typename Base::Node Node;
1414      typedef typename Base::Edge Edge;
1415
1416      /// \brief Erase a node from the graph.
1417      ///
1418      /// Erase a node from the graph. This function should erase
1419      /// arcs connecting to the node.
1420      void erase(const Node&) {}
1421
1422      /// \brief Erase an arc from the graph.
1423      ///
1424      /// Erase an arc from the graph.
1425      ///
1426      void erase(const Edge&) {}
1427
1428      template <typename _Graph>
1429      struct Constraints {
1430        void constraints() {
1431          checkConcept<Base, _Graph>();
1432          typename _Graph::Node node;
1433          graph.erase(node);
1434          typename _Graph::Edge edge;
1435          graph.erase(edge);
1436        }
1437
1438        _Graph& graph;
1439      };
1440    };
1441
1442    /// \brief An empty clearable base digraph class.
1443    ///
1444    /// This class provides beside the core digraph features core clear
1445    /// functions for the digraph structure. The main difference between
1446    /// the base and this interface is that the digraph alterations
1447    /// should handled already on this level.
1448    template <typename _Base = BaseDigraphComponent>
1449    class ClearableDigraphComponent : public _Base {
1450    public:
1451
1452      typedef _Base Base;
1453
1454      /// \brief Erase all nodes and arcs from the digraph.
1455      ///
1456      /// Erase all nodes and arcs from the digraph.
1457      ///
1458      void clear() {}
1459
1460      template <typename _Digraph>
1461      struct Constraints {
1462        void constraints() {
1463          checkConcept<Base, _Digraph>();
1464          digraph.clear();
1465        }
1466
1467        _Digraph digraph;
1468      };
1469    };
1470
1471    /// \brief An empty clearable base undirected graph class.
1472    ///
1473    /// This class provides beside the core undirected graph features
1474    /// core clear functions for the undirected graph structure. The
1475    /// main difference between the base and this interface is that
1476    /// the graph alterations should handled already on this level.
1477    template <typename _Base = BaseGraphComponent>
1478    class ClearableGraphComponent : public ClearableDigraphComponent<_Base> {
1479    public:
1480
1481      typedef _Base Base;
1482
1483      template <typename _Graph>
1484      struct Constraints {
1485        void constraints() {
1486          checkConcept<ClearableGraphComponent<Base>, _Graph>();
1487        }
1488
1489        _Graph graph;
1490      };
1491    };
1492
1493  }
1494
1495}
1496
1497#endif
Note: See TracBrowser for help on using the repository browser.