Little beauty fault is corrected.
2 * lemon/concept/graph_component.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 ///\ingroup graph_concepts
19 ///\brief The graph components.
22 #ifndef LEMON_CONCEPT_GRAPH_COMPONENT_H
23 #define LEMON_CONCEPT_GRAPH_COMPONENT_H
25 #include <lemon/invalid.h>
26 #include <lemon/concept/maps.h>
28 #include <lemon/bits/alteration_notifier.h>
33 /// \addtogroup graph_concepts
36 /**************** Graph iterator concepts ****************/
38 /// Skeleton class for graph Node and Edge types
40 /// This class describes the interface of Node and Edge (and UndirEdge
41 /// in undirected graphs) subtypes of graph types.
43 /// \note This class is a template class so that we can use it to
44 /// create graph skeleton classes. The reason for this is than Node
45 /// and Edge types should \em not derive from the same base class.
46 /// For Node you should instantiate it with character 'n' and for Edge
50 template <char _selector = '0'>
54 /// Default constructor.
56 /// \warning The default constructor is not required to set
57 /// the item to some well-defined value. So you should consider it
64 GraphItem(GraphItem const&) {}
65 /// Invalid constructor \& conversion.
67 /// This constructor initializes the item to be invalid.
68 /// \sa Invalid for more details.
70 /// Assign operator for nodes.
72 /// The nodes are assignable.
74 GraphItem& operator=(GraphItem const&) { return *this; }
75 /// Equality operator.
77 /// Two iterators are equal if and only if they represents the
78 /// same node in the graph or both are invalid.
79 bool operator==(GraphItem) const { return false; }
80 /// Inequality operator.
82 /// \sa operator==(const Node& n)
84 bool operator!=(GraphItem) const { return false; }
86 /// Artificial ordering operator.
88 /// To allow the use of graph descriptors as key type in std::map or
89 /// similar associative container we require this.
91 /// \note This operator only have to define some strict ordering of
92 /// the items; this order has nothing to do with the iteration
93 /// ordering of the items.
95 /// \bug This is a technical requirement. Do we really need this?
96 bool operator<(GraphItem) const { return false; }
98 template<typename _GraphItem>
103 _GraphItem i3 = INVALID;
108 // b = (ia == ib) && (ia != ib) && (ia < ib);
109 b = (ia == ib) && (ia != ib);
110 b = (ia == INVALID) && (ib != INVALID);
114 const _GraphItem &ia;
115 const _GraphItem &ib;
119 /// A type describing the concept of graph node
121 /// This is an instantiation of \ref GraphItem which can be used as a
122 /// Node subtype in graph skeleton definitions
123 typedef GraphItem<'n'> GraphNode;
125 /// A type describing the concept of graph edge
127 /// This is an instantiation of \ref GraphItem which can be used as a
128 /// Edge subtype in graph skeleton definitions
129 typedef GraphItem<'e'> GraphEdge;
132 /**************** Basic features of graphs ****************/
134 /// An empty base graph class.
136 /// This class provides the minimal set of features needed for a graph
137 /// structure. All graph concepts have to be conform to this base
140 /// \bug This is not true. The minimal graph concept is the
141 /// BaseIterableGraphComponent.
143 class BaseGraphComponent {
146 typedef BaseGraphComponent Graph;
148 /// Node class of the graph.
150 /// This class represents the Nodes of the graph.
152 typedef GraphItem<'n'> Node;
154 /// Edge class of the graph.
156 /// This class represents the Edges of the graph.
158 typedef GraphItem<'e'> Edge;
160 ///Gives back the target node of an edge.
162 ///Gives back the target node of an edge.
164 Node target(const Edge&) const { return INVALID;}
166 ///Gives back the source node of an edge.
168 ///Gives back the source node of an edge.
170 Node source(const Edge&) const { return INVALID;}
173 template <typename _Graph>
175 typedef typename _Graph::Node Node;
176 typedef typename _Graph::Edge Edge;
179 checkConcept<GraphItem<'n'>, Node>();
180 checkConcept<GraphItem<'e'>, Edge>();
193 /// An empty iterable base graph class.
195 /// This class provides beside the core graph features
196 /// core iterable interface for the graph structure.
197 /// Most of the base graphs should be conform to this concept.
199 class BaseIterableGraphComponent : virtual public BaseGraphComponent {
202 typedef BaseGraphComponent::Node Node;
203 typedef BaseGraphComponent::Edge Edge;
205 /// Gives back the first Node in the iterating order.
207 /// Gives back the first Node in the iterating order.
209 void first(Node&) const {}
211 /// Gives back the next Node in the iterating order.
213 /// Gives back the next Node in the iterating order.
215 void next(Node&) const {}
217 /// Gives back the first Edge in the iterating order.
219 /// Gives back the first Edge in the iterating order.
221 void first(Edge&) const {}
222 /// Gives back the next Edge in the iterating order.
224 /// Gives back the next Edge in the iterating order.
226 void next(Edge&) const {}
229 /// Gives back the first of the Edges point to the given Node.
231 /// Gives back the first of the Edges point to the given Node.
233 void firstIn(Edge&, const Node&) const {}
235 /// Gives back the next of the Edges points to the given Node.
238 /// Gives back the next of the Edges points to the given Node.
240 void nextIn(Edge&) const {}
242 /// Gives back the first of the Edges start from the given Node.
244 /// Gives back the first of the Edges start from the given Node.
246 void firstOut(Edge&, const Node&) const {}
248 /// Gives back the next of the Edges start from the given Node.
250 /// Gives back the next of the Edges start from the given Node.
252 void nextOut(Edge&) const {}
255 template <typename _Graph>
259 checkConcept< BaseGraphComponent, _Graph >();
260 typename _Graph::Node node;
261 typename _Graph::Edge edge;
271 graph.firstIn(edge, node);
275 graph.firstOut(edge, node);
284 /// An empty idable base graph class.
286 /// This class provides beside the core graph features
287 /// core id functions for the graph structure.
288 /// The most of the base graphs should be conform to this concept.
289 /// The id's are unique and immutable.
290 class IDableGraphComponent : virtual public BaseGraphComponent {
293 typedef BaseGraphComponent::Node Node;
294 typedef BaseGraphComponent::Edge Edge;
296 /// Gives back an unique integer id for the Node.
298 /// Gives back an unique integer id for the Node.
300 int id(const Node&) const { return -1;}
302 /// \brief Gives back the node by the unique id.
304 /// Gives back the node by the unique id.
305 /// If the graph does not contain node with the given id
306 /// then the result of the function is undetermined.
307 Node fromId(int , Node) const { return INVALID;}
309 /// \brief Gives back an unique integer id for the Edge.
311 /// Gives back an unique integer id for the Edge.
313 int id(const Edge&) const { return -1;}
315 /// \brief Gives back the edge by the unique id.
317 /// Gives back the edge by the unique id.
318 /// If the graph does not contain edge with the given id
319 /// then the result of the function is undetermined.
320 Edge fromId(int, Edge) const { return INVALID;}
322 template <typename _Graph>
326 checkConcept< BaseGraphComponent, _Graph >();
327 typename _Graph::Node node;
328 int nid = graph.id(node);
329 nid = graph.id(node);
330 node = graph.fromId(nid, Node());
331 typename _Graph::Edge edge;
332 int eid = graph.id(edge);
333 eid = graph.id(edge);
334 edge = graph.fromId(eid, Edge());
342 /// An empty max-idable base graph class.
344 /// This class provides beside the core graph features
345 /// core max id functions for the graph structure.
346 /// The most of the base graphs should be conform to this concept.
347 /// The id's are unique and immutable.
348 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
351 /// Gives back an integer greater or equal to the maximum Node id.
353 /// Gives back an integer greater or equal to the maximum Node id.
355 int maxId(Node = INVALID) const { return -1;}
357 /// Gives back an integer greater or equal to the maximum Edge id.
359 /// Gives back an integer greater or equal to the maximum Edge id.
361 int maxId(Edge = INVALID) const { return -1;}
363 template <typename _Graph>
367 checkConcept<BaseGraphComponent, _Graph>();
368 int nid = graph.maxId(typename _Graph::Node());
369 ignore_unused_variable_warning(nid);
370 int eid = graph.maxId(typename _Graph::Edge());
371 ignore_unused_variable_warning(eid);
378 /// An empty extendable base graph class.
380 /// This class provides beside the core graph features
381 /// core graph extend interface for the graph structure.
382 /// The most of the base graphs should be conform to this concept.
383 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
386 typedef BaseGraphComponent::Node Node;
387 typedef BaseGraphComponent::Edge Edge;
389 /// Adds a new Node to the graph.
391 /// Adds a new Node to the graph.
397 /// Adds a new Edge connects the two Nodes to the graph.
399 /// Adds a new Edge connects the two Nodes to the graph.
401 Edge addEdge(const Node&, const Node&) {
405 template <typename _Graph>
408 checkConcept<BaseGraphComponent, _Graph >();
409 typename _Graph::Node node_a, node_b;
410 node_a = graph.addNode();
411 node_b = graph.addNode();
412 typename _Graph::Edge edge;
413 edge = graph.addEdge(node_a, node_b);
420 /// An empty erasable base graph class.
422 /// This class provides beside the core graph features
423 /// core erase functions for the graph structure.
424 /// The most of the base graphs should be conform to this concept.
425 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
428 typedef BaseGraphComponent::Node Node;
429 typedef BaseGraphComponent::Edge Edge;
431 /// Erase a Node from the graph.
433 /// Erase a Node from the graph. This function should not
434 /// erase edges connecting to the Node.
435 void erase(const Node&) {}
437 /// Erase an Edge from the graph.
439 /// Erase an Edge from the graph.
441 void erase(const Edge&) {}
443 template <typename _Graph>
446 checkConcept<BaseGraphComponent, _Graph>();
447 typename _Graph::Node node;
449 typename _Graph::Edge edge;
457 /// An empty clearable base graph class.
459 /// This class provides beside the core graph features
460 /// core clear functions for the graph structure.
461 /// The most of the base graphs should be conform to this concept.
462 class ClearableGraphComponent : virtual public BaseGraphComponent {
465 /// Erase all the Nodes and Edges from the graph.
467 /// Erase all the Nodes and Edges from the graph.
471 template <typename _Graph>
474 checkConcept<BaseGraphComponent, _Graph>();
483 /// Skeleton class for graph NodeIt and EdgeIt
485 /// Skeleton class for graph NodeIt and EdgeIt.
487 template <typename _Graph, typename _Item>
488 class GraphIterator : public _Item {
490 /// \todo Don't we need the Item type as typedef?
492 /// Default constructor.
494 /// @warning The default constructor sets the iterator
495 /// to an undefined value.
497 /// Copy constructor.
499 /// Copy constructor.
501 GraphIterator(GraphIterator const&) {}
502 /// Sets the iterator to the first item.
504 /// Sets the iterator to the first item of \c the graph.
506 explicit GraphIterator(const _Graph&) {}
507 /// Invalid constructor \& conversion.
509 /// This constructor initializes the item to be invalid.
510 /// \sa Invalid for more details.
511 GraphIterator(Invalid) {}
512 /// Assign operator for items.
514 /// The items are assignable.
516 GraphIterator& operator=(GraphIterator const&) { return *this; }
519 /// Assign the iterator to the next item.
521 GraphIterator& operator++() { return *this; }
522 // Node operator*() const { return INVALID; }
523 /// Equality operator
525 /// Two iterators are equal if and only if they point to the
526 /// same object or both are invalid.
527 bool operator==(const GraphIterator&) const { return true;}
528 /// Inequality operator
530 /// \sa operator==(Node n)
532 bool operator!=(const GraphIterator&) const { return true;}
534 template<typename _GraphIterator>
537 // checkConcept< Item, _GraphIterator >();
538 _GraphIterator it1(g);
540 /// \todo Do we need NodeIt(Node) kind of constructor?
541 // _GraphIterator it2(bj);
547 /// \bug This should be: is_base_and_derived<BaseItem, _GraphIterator>
555 /// Skeleton class for graph InEdgeIt and OutEdgeIt
557 /// \note Because InEdgeIt and OutEdgeIt may not inherit from the same
558 /// base class, the _selector is a additional template parameter. For
559 /// InEdgeIt you should instantiate it with character 'i' and for
560 /// OutEdgeIt with 'o'.
561 /// \todo Is this a good name for this concept?
562 template <typename Graph,
563 typename Edge = typename Graph::Edge,
564 char _selector = '0'>
565 class GraphIncIterator : public Edge {
567 /// Default constructor.
569 /// @warning The default constructor sets the iterator
570 /// to an undefined value.
571 GraphIncIterator() {}
572 /// Copy constructor.
574 /// Copy constructor.
576 GraphIncIterator(GraphIncIterator const& gi) :Edge(gi) {}
577 /// Sets the iterator to the first edge incoming into or outgoing
580 /// Sets the iterator to the first edge incoming into or outgoing
583 explicit GraphIncIterator(const Graph&, const typename Graph::Node&) {}
584 /// Invalid constructor \& conversion.
586 /// This constructor initializes the item to be invalid.
587 /// \sa Invalid for more details.
588 GraphIncIterator(Invalid) {}
589 /// Assign operator for nodes.
591 /// The nodes are assignable.
593 GraphIncIterator& operator=(GraphIncIterator const&) { return *this; }
596 /// Assign the iterator to the next node.
598 GraphIncIterator& operator++() { return *this; }
600 // Node operator*() const { return INVALID; }
602 /// Equality operator
604 /// Two iterators are equal if and only if they point to the
605 /// same object or both are invalid.
606 bool operator==(const GraphIncIterator&) const { return true;}
608 /// Inequality operator
610 /// \sa operator==(Node n)
612 bool operator!=(const GraphIncIterator&) const { return true;}
614 template <typename _GraphIncIterator>
616 typedef typename Graph::Node Node;
618 checkConcept<GraphItem<'e'>, _GraphIncIterator>();
619 _GraphIncIterator it1(graph, node);
620 /// \todo Do we need OutEdgeIt(Edge) kind of constructor?
621 // _GraphIncIterator it2(edge);
622 _GraphIncIterator it2;
633 void const_constraits() {
634 Node n = graph.baseNode(it);
635 n = graph.runningNode(it);
641 _GraphIncIterator it;
646 /// An empty iterable base graph class.
648 /// This class provides beside the core graph features
649 /// iterator based iterable interface for the graph structure.
650 /// This concept is part of the StaticGraphConcept.
651 class IterableGraphComponent : virtual public BaseGraphComponent {
655 typedef IterableGraphComponent Graph;
657 typedef BaseGraphComponent::Node Node;
658 typedef BaseGraphComponent::Edge Edge;
660 /// This iterator goes through each node.
662 /// This iterator goes through each node.
664 typedef GraphIterator<Graph, Node> NodeIt;
665 /// This iterator goes through each node.
667 /// This iterator goes through each node.
669 typedef GraphIterator<Graph, Edge> EdgeIt;
670 /// This iterator goes trough the incoming edges of a node.
672 /// This iterator goes trough the \e inccoming edges of a certain node
674 typedef GraphIncIterator<Graph, Edge, 'i'> InEdgeIt;
675 /// This iterator goes trough the outgoing edges of a node.
677 /// This iterator goes trough the \e outgoing edges of a certain node
679 typedef GraphIncIterator<Graph, Edge, 'o'> OutEdgeIt;
682 template <typename _Graph>
685 checkConcept< BaseGraphComponent, _Graph>();
687 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>,
688 typename _Graph::EdgeIt >();
689 checkConcept<GraphIterator<_Graph, typename _Graph::Node>,
690 typename _Graph::NodeIt >();
691 checkConcept<GraphIncIterator<_Graph>, typename _Graph::InEdgeIt >();
692 checkConcept<GraphIncIterator<_Graph>, typename _Graph::OutEdgeIt >();
696 /// An empty alteration notifier base graph class.
698 /// This class provides beside the core graph features
699 /// alteration notifier interface for the graph structure.
700 /// This is an observer-notifier pattern. More Obsevers can
701 /// be registered into the notifier and whenever an alteration
702 /// occured in the graph all the observers will notified about it.
703 class AlterableGraphComponent : virtual public BaseGraphComponent {
706 /// The edge observer registry.
707 typedef AlterationNotifier<Edge> EdgeNotifier;
708 /// The node observer registry.
709 typedef AlterationNotifier<Node> NodeNotifier;
711 /// \brief Gives back the edge alteration notifier.
713 /// Gives back the edge alteration notifier.
714 EdgeNotifier getNotifier(Edge) const {
715 return EdgeNotifier();
718 /// \brief Gives back the node alteration notifier.
720 /// Gives back the node alteration notifier.
721 NodeNotifier getNotifier(Node) const {
722 return NodeNotifier();
728 /// Class describing the concept of graph maps
730 /// This class describes the common interface of the graph maps
731 /// (NodeMap, EdgeMap), that is \ref maps-pages "maps" which can be used to
732 /// associate data to graph descriptors (nodes or edges).
733 template <typename Graph, typename Item, typename _Value>
734 class GraphMap : public ReadWriteMap<Item, _Value> {
738 /// \brief Construct a new map.
740 /// Construct a new map for the graph.
741 explicit GraphMap(const Graph&) {}
742 /// \brief Construct a new map with default value.
744 /// Construct a new map for the graph and initalise the values.
745 GraphMap(const Graph&, const _Value&) {}
746 /// \brief Copy constructor.
748 /// Copy Constructor.
749 GraphMap(const GraphMap& gm) :ReadWriteMap<Item, _Value>(gm) {}
751 /// \brief Assign operator.
754 GraphMap& operator=(const GraphMap&) { return *this;}
756 template<typename _Map>
759 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
760 // Construction with a graph parameter
762 // Constructor with a graph and a default value parameter
764 // Copy constructor. Do we need it?
766 // Copy operator. Do we need it?
769 ignore_unused_variable_warning(a2);
774 const typename GraphMap::Value &t;
779 /// An empty mappable base graph class.
781 /// This class provides beside the core graph features
782 /// map interface for the graph structure.
783 /// This concept is part of the StaticGraphConcept.
784 class MappableGraphComponent : virtual public BaseGraphComponent {
787 typedef MappableGraphComponent Graph;
789 typedef BaseGraphComponent::Node Node;
790 typedef BaseGraphComponent::Edge Edge;
792 /// ReadWrite map of the nodes.
794 /// ReadWrite map of the nodes.
796 template <typename _Value>
797 class NodeMap : public GraphMap<Graph, Node, _Value> {
801 /// \brief Construct a new map.
803 /// Construct a new map for the graph.
804 /// \todo call the right parent class constructor
805 explicit NodeMap(const Graph&) {}
806 /// \brief Construct a new map with default value.
808 /// Construct a new map for the graph and initalise the values.
809 NodeMap(const Graph&, const _Value&) {}
810 /// \brief Copy constructor.
812 /// Copy Constructor.
813 NodeMap(const NodeMap& nm) : GraphMap<Graph, Node, _Value>(nm) {}
815 /// \brief Assign operator.
818 NodeMap& operator=(const NodeMap&) { return *this;}
822 /// ReadWrite map of the edges.
824 /// ReadWrite map of the edges.
826 template <typename _Value>
827 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
831 /// \brief Construct a new map.
833 /// Construct a new map for the graph.
834 /// \todo call the right parent class constructor
835 explicit EdgeMap(const Graph&) {}
836 /// \brief Construct a new map with default value.
838 /// Construct a new map for the graph and initalise the values.
839 EdgeMap(const Graph&, const _Value&) {}
840 /// \brief Copy constructor.
842 /// Copy Constructor.
843 EdgeMap(const EdgeMap& em) :GraphMap<Graph, Edge, _Value>(em) {}
845 /// \brief Assign operator.
848 EdgeMap& operator=(const EdgeMap&) { return *this;}
852 template <typename _Graph>
858 Type(int _v) : value(_v) {}
862 checkConcept<BaseGraphComponent, _Graph>();
864 typedef typename _Graph::template NodeMap<int> IntNodeMap;
865 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>,
868 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
869 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>,
872 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
873 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>,
878 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
879 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
882 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
883 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
886 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
887 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>,
896 /// \brief An empty extendable extended graph class.
898 /// This class provides beside the core graph features
899 /// item addition interface for the graph structure.
900 /// The difference between this class and the
901 /// \c BaseExtendableGraphComponent is that it should
902 /// notify the item alteration observers.
903 class ExtendableGraphComponent : virtual public BaseGraphComponent {
906 typedef ExtendableGraphComponent Graph;
908 typedef BaseGraphComponent::Node Node;
909 typedef BaseGraphComponent::Edge Edge;
911 /// \brief Add a node to the graph.
913 /// Add a node to the graph and notify the observers.
918 /// \brief Add an edge to the graph.
920 /// Add an edge to the graph and notify the observers.
921 Edge addEdge(const Node&, const Node&) {
925 template <typename _Graph>
928 checkConcept<BaseGraphComponent, _Graph >();
929 typename _Graph::Node node_a, node_b;
930 node_a = graph.addNode();
931 node_b = graph.addNode();
932 typename _Graph::Edge edge;
933 edge = graph.addEdge(node_a, node_b);
939 /// \brief An empty erasable extended graph class.
941 /// This class provides beside the core graph features
942 /// item erase interface for the graph structure.
943 /// The difference between this class and the
944 /// \c BaseErasableGraphComponent is that it should
945 /// notify the item alteration observers.
946 class ErasableGraphComponent : virtual public BaseGraphComponent {
949 typedef ErasableGraphComponent Graph;
951 typedef BaseGraphComponent::Node Node;
952 typedef BaseGraphComponent::Edge Edge;
954 /// \brief Erase the Node and notify the node alteration observers.
956 /// Erase the Node and notify the node alteration observers.
957 void erase(const Node&) {}
959 /// \brief Erase the Edge and notify the edge alteration observers.
961 /// Erase the Edge and notify the edge alteration observers.
962 void erase(const Edge&) {}
964 template <typename _Graph>
967 checkConcept<BaseGraphComponent, _Graph >();
968 typename _Graph::Node node;
970 typename _Graph::Edge edge;