2 * src/lemon/concept/graph_component.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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
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>
32 /**************** Graph iterator concepts ****************/
34 /// Skeleton class for graph Node and Edge
36 /// \note Because Node and Edge are forbidden to inherit from the same
37 /// base class, this is a template. For Node you should instantiate it
38 /// with character 'n' and for Edge with 'e'.
40 template<char _selector>
43 /// Default constructor.
45 /// @warning The default constructor sets the item
46 /// to an undefined value.
52 GraphItem(GraphItem const&) {}
53 /// Invalid constructor \& conversion.
55 /// This constructor initializes the item to be invalid.
56 /// \sa Invalid for more details.
58 /// Assign operator for nodes.
60 /// The nodes are assignable.
62 GraphItem& operator=(GraphItem const&) { return *this; }
63 /// Equality operator.
65 /// Two iterators are equal if and only if they represents the
66 /// same node in the graph or both are invalid.
67 bool operator==(GraphItem) const { return false; }
68 /// Inequality operator.
70 /// \sa operator==(const Node& n)
72 bool operator!=(GraphItem) const { return false; }
74 // Technical requirement. Do we really need this?
75 // bool operator<(GraphItem) const { return false; }
78 template<typename _GraphItem>
83 _GraphItem i3 = INVALID;
88 // b = (ia == ib) && (ia != ib) && (ia < ib);
89 b = (ia == ib) && (ia != ib);
90 b = (ia == INVALID) && (ib != INVALID);
98 /// An empty base graph class.
100 /// This class provides the minimal set of features needed for a graph
101 /// structure. All graph concepts have to be conform to this base
104 /// \bug This is not true. The minimal graph concept is the
105 /// BaseIterableGraphComponent.
107 class BaseGraphComponent {
110 typedef BaseGraphComponent Graph;
112 /// Node class of the graph.
114 /// This class represents the Nodes of the graph.
116 typedef GraphItem<'n'> Node;
118 /// Edge class of the graph.
120 /// This class represents the Edges of the graph.
122 typedef GraphItem<'e'> Edge;
124 ///Gives back the target node of an edge.
126 ///Gives back the target node of an edge.
128 Node target(const Edge&) const { return INVALID;}
130 ///Gives back the source node of an edge.
132 ///Gives back the source node of an edge.
134 Node source(const Edge&) const { return INVALID;}
137 template <typename _Graph>
139 typedef typename _Graph::Node Node;
140 typedef typename _Graph::Edge Edge;
143 checkConcept<GraphItem<'n'>, Node>();
144 checkConcept<GraphItem<'e'>, Edge>();
157 /// An empty iterable base graph class.
159 /// This class provides beside the core graph features
160 /// core iterable interface for the graph structure.
161 /// Most of the base graphs should be conform to this concept.
163 class BaseIterableGraphComponent : virtual public BaseGraphComponent {
166 typedef BaseGraphComponent::Node Node;
167 typedef BaseGraphComponent::Edge Edge;
169 /// Gives back the first Node in the iterating order.
171 /// Gives back the first Node in the iterating order.
173 void first(Node&) const {}
175 /// Gives back the next Node in the iterating order.
177 /// Gives back the next Node in the iterating order.
179 void next(Node&) const {}
181 /// Gives back the first Edge in the iterating order.
183 /// Gives back the first Edge in the iterating order.
185 void first(Edge&) const {}
186 /// Gives back the next Edge in the iterating order.
188 /// Gives back the next Edge in the iterating order.
190 void next(Edge&) const {}
193 /// Gives back the first of the Edges point to the given Node.
195 /// Gives back the first of the Edges point to the given Node.
197 void firstIn(Edge&, const Node&) const {}
199 /// Gives back the next of the Edges points to the given Node.
202 /// Gives back the next of the Edges points to the given Node.
204 void nextIn(Edge&) const {}
206 /// Gives back the first of the Edges start from the given Node.
208 /// Gives back the first of the Edges start from the given Node.
210 void firstOut(Edge&, const Node&) const {}
212 /// Gives back the next of the Edges start from the given Node.
214 /// Gives back the next of the Edges start from the given Node.
216 void nextOut(Edge&) const {}
219 template <typename _Graph>
223 checkConcept< BaseGraphComponent, _Graph >();
224 typename _Graph::Node node;
225 typename _Graph::Edge edge;
235 graph.firstIn(edge, node);
239 graph.firstOut(edge, node);
248 /// An empty idable base graph class.
250 /// This class provides beside the core graph features
251 /// core id functions for the graph structure.
252 /// The most of the base graphs should be conform to this concept.
253 /// The id's are unique and immutable.
254 class IDableGraphComponent : virtual public BaseGraphComponent {
257 typedef BaseGraphComponent::Node Node;
258 typedef BaseGraphComponent::Edge Edge;
260 /// Gives back an unique integer id for the Node.
262 /// Gives back an unique integer id for the Node.
264 int id(const Node&) const { return -1;}
266 /// Gives back an unique integer id for the Edge.
268 /// Gives back an unique integer id for the Edge.
270 int id(const Edge&) const { return -1;}
272 template <typename _Graph>
276 checkConcept< BaseGraphComponent, _Graph >();
277 typename _Graph::Node node;
278 int nid = graph.id(node);
279 nid = graph.id(node);
280 typename _Graph::Edge edge;
281 int eid = graph.id(edge);
282 eid = graph.id(edge);
290 /// An empty max-idable base graph class.
292 /// This class provides beside the core graph features
293 /// core max id functions for the graph structure.
294 /// The most of the base graphs should be conform to this concept.
295 /// The id's are unique and immutable.
296 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
299 /// Gives back an integer greater or equal to the maximum Node id.
301 /// Gives back an integer greater or equal to the maximum Node id.
303 int maxId(Node = INVALID) const { return -1;}
305 /// Gives back an integer greater or equal to the maximum Edge id.
307 /// Gives back an integer greater or equal to the maximum Edge id.
309 int maxId(Edge = INVALID) const { return -1;}
311 template <typename _Graph>
315 checkConcept<BaseGraphComponent, _Graph>();
316 int nid = graph.maxId(typename _Graph::Node());
317 ignore_unused_variable_warning(nid);
318 int eid = graph.maxId(typename _Graph::Edge());
319 ignore_unused_variable_warning(eid);
326 /// An empty extendable base graph class.
328 /// This class provides beside the core graph features
329 /// core graph extend interface for the graph structure.
330 /// The most of the base graphs should be conform to this concept.
331 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
334 typedef BaseGraphComponent::Node Node;
335 typedef BaseGraphComponent::Edge Edge;
337 /// Adds a new Node to the graph.
339 /// Adds a new Node to the graph.
345 /// Adds a new Edge connects the two Nodes to the graph.
347 /// Adds a new Edge connects the two Nodes to the graph.
349 Edge addEdge(const Node& from, const Node& to) {
353 template <typename _Graph>
356 checkConcept<BaseGraphComponent, _Graph >();
357 typename _Graph::Node node_a, node_b;
358 node_a = graph.addNode();
359 typename _Graph::Edge edge;
360 edge = graph.addEdge(node_a, node_b);
367 /// An empty erasable base graph class.
369 /// This class provides beside the core graph features
370 /// core erase functions for the graph structure.
371 /// The most of the base graphs should be conform to this concept.
372 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
375 typedef BaseGraphComponent::Node Node;
376 typedef BaseGraphComponent::Edge Edge;
378 /// Erase a Node from the graph.
380 /// Erase a Node from the graph. This function should not
381 /// erase edges connecting to the Node.
382 void erase(const Node&) {}
384 /// Erase an Edge from the graph.
386 /// Erase an Edge from the graph.
388 void erase(const Edge&) {}
390 template <typename _Graph>
393 checkConcept<BaseGraphComponent, _Graph>();
394 typename _Graph::Node node;
396 typename _Graph::Edge edge;
404 /// An empty clearable base graph class.
406 /// This class provides beside the core graph features
407 /// core clear functions for the graph structure.
408 /// The most of the base graphs should be conform to this concept.
409 class BaseClearableGraphComponent : virtual public BaseGraphComponent {
412 /// Erase all the Nodes and Edges from the graph.
414 /// Erase all the Nodes and Edges from the graph.
418 template <typename _Graph>
421 checkConcept< BaseGraphComponent, _Graph>();
430 /// Skeleton class for graph NodeIt and EdgeIt
432 /// Skeleton class for graph NodeIt and EdgeIt.
434 template <typename _Graph, typename _Item>
435 class GraphIterator : public _Item {
437 /// \todo Don't we need the Item type as typedef?
439 /// Default constructor.
441 /// @warning The default constructor sets the iterator
442 /// to an undefined value.
444 /// Copy constructor.
446 /// Copy constructor.
448 GraphIterator(GraphIterator const&) {}
449 /// Sets the iterator to the first item.
451 /// Sets the iterator to the first item of \c the graph.
453 explicit GraphIterator(const _Graph&) {}
454 /// Invalid constructor \& conversion.
456 /// This constructor initializes the item to be invalid.
457 /// \sa Invalid for more details.
458 GraphIterator(Invalid) {}
459 /// Assign operator for items.
461 /// The items are assignable.
463 GraphIterator& operator=(GraphIterator const&) { return *this; }
466 /// Assign the iterator to the next item.
468 GraphIterator& operator++() { return *this; }
469 // Node operator*() const { return INVALID; }
470 /// Equality operator
472 /// Two iterators are equal if and only if they point to the
473 /// same object or both are invalid.
474 bool operator==(const GraphIterator&) const { return true;}
475 /// Inequality operator
477 /// \sa operator==(Node n)
479 bool operator!=(const GraphIterator&) const { return true;}
481 template<typename _GraphIterator>
484 // checkConcept< Item, _GraphIterator >();
485 _GraphIterator it1(g);
487 /// \todo Do we need NodeIt(Node) kind of constructor?
488 // _GraphIterator it2(bj);
494 /// \bug This should be: is_base_and_derived<BaseItem, _GraphIterator>
502 /// Skeleton class for graph InEdgeIt and OutEdgeIt
504 /// \note Because InEdgeIt and OutEdgeIt may not inherit from the same
505 /// base class, the _selector is a additional template parameter. For
506 /// InEdgeIt you should instantiate it with character 'i' and for
507 /// OutEdgeIt with 'o'.
508 /// \todo check the name of the concept GraphIncEdgeIterator
509 template <typename _Graph, char _selector>
510 class GraphIncEdgeIterator : public _Graph::Edge {
512 /// Default constructor.
514 /// @warning The default constructor sets the iterator
515 /// to an undefined value.
516 GraphIncEdgeIterator() {}
517 /// Copy constructor.
519 /// Copy constructor.
521 GraphIncEdgeIterator(GraphIncEdgeIterator const&) {}
522 /// Sets the iterator to the first edge incoming into or outgoing from the node.
524 /// Sets the iterator to the first edge incoming into or outgoing from the node.
526 explicit GraphIncEdgeIterator(const _Graph&, const typename _Graph::Node&) {}
527 /// Invalid constructor \& conversion.
529 /// This constructor initializes the item to be invalid.
530 /// \sa Invalid for more details.
531 GraphIncEdgeIterator(Invalid) {}
532 /// Assign operator for nodes.
534 /// The nodes are assignable.
536 GraphIncEdgeIterator& operator=(GraphIncEdgeIterator const&) { return *this; }
539 /// Assign the iterator to the next node.
541 GraphIncEdgeIterator& operator++() { return *this; }
542 // Node operator*() const { return INVALID; }
543 /// Equality operator
545 /// Two iterators are equal if and only if they point to the
546 /// same object or both are invalid.
547 bool operator==(const GraphIncEdgeIterator&) const { return true;}
548 /// Inequality operator
550 /// \sa operator==(Node n)
552 bool operator!=(const GraphIncEdgeIterator&) const { return true;}
554 template <typename _GraphIncEdgeIterator>
556 typedef typename _Graph::Node Node;
557 typedef typename _Graph::Edge Edge;
559 checkConcept<GraphItem<'e'>, _GraphIncEdgeIterator>();
560 _GraphIncEdgeIterator it1(graph, node);
561 /// \todo Do we need OutEdgeIt(Edge) kind of constructor?
562 // _GraphIncEdgeIterator it2(edge);
563 _GraphIncEdgeIterator it2;
577 /// An empty iterable base graph class.
579 /// This class provides beside the core graph features
580 /// iterator based iterable interface for the graph structure.
581 /// This concept is part of the StaticGraphConcept.
582 class IterableGraphComponent : virtual public BaseGraphComponent {
586 typedef IterableGraphComponent Graph;
588 typedef BaseGraphComponent::Node Node;
589 typedef BaseGraphComponent::Edge Edge;
591 /// This iterator goes through each node.
593 /// This iterator goes through each node.
595 typedef GraphIterator<Graph, Node> NodeIt;
596 /// This iterator goes through each node.
598 /// This iterator goes through each node.
600 typedef GraphIterator<Graph, Edge> EdgeIt;
601 /// This iterator goes trough the incoming edges of a node.
603 /// This iterator goes trough the \e inccoming edges of a certain node
605 typedef GraphIncEdgeIterator<Graph, 'i'> InEdgeIt;
606 /// This iterator goes trough the outgoing edges of a node.
608 /// This iterator goes trough the \e outgoing edges of a certain node
610 typedef GraphIncEdgeIterator<Graph, 'o'> OutEdgeIt;
613 template <typename _Graph>
616 checkConcept< BaseGraphComponent, _Graph>();
618 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>, typename _Graph::EdgeIt >();
619 checkConcept<GraphIterator<_Graph, typename _Graph::Node>, typename _Graph::NodeIt >();
620 checkConcept<GraphIncEdgeIterator<_Graph, 'i'>, typename _Graph::InEdgeIt >();
621 checkConcept<GraphIncEdgeIterator<_Graph, 'o'>, typename _Graph::OutEdgeIt >();
626 template <typename Graph, typename Item, typename _Value>
627 class GraphMap : public ReadWriteMap<Item, _Value> {
631 explicit GraphMap(const Graph&) {}
632 GraphMap(const Graph&, const _Value&) {}
633 GraphMap(const GraphMap&) {}
635 GraphMap& operator=(const GraphMap&) { return *this;}
637 template<typename _Map>
640 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
641 // Construction with a graph parameter
643 // Constructor with a graph and a default value parameter
645 // Copy constructor. Do we need it?
647 // Copy operator. Do we need it?
650 ignore_unused_variable_warning(a2);
655 const typename GraphMap::Value &t;
660 /// An empty mappable base graph class.
662 /// This class provides beside the core graph features
663 /// map interface for the graph structure.
664 /// This concept is part of the StaticGraphConcept.
665 class MappableGraphComponent : virtual public BaseGraphComponent {
668 typedef MappableGraphComponent Graph;
670 typedef BaseGraphComponent::Node Node;
671 typedef BaseGraphComponent::Edge Edge;
673 /// ReadWrite map of the nodes.
675 /// ReadWrite map of the nodes.
677 template <typename _Value>
678 class NodeMap : public GraphMap<Graph, Node, _Value> {
682 // \todo call the right parent class constructor
683 explicit NodeMap(const Graph&) {}
684 NodeMap(const Graph&, const _Value&) {}
685 NodeMap(const NodeMap&) {}
687 NodeMap& operator=(const NodeMap&) { return *this;}
691 /// ReadWrite map of the edges.
693 /// ReadWrite map of the edges.
695 template <typename _Value>
696 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
700 // \todo call the right parent class constructor
701 explicit EdgeMap(const Graph&) {}
702 EdgeMap(const Graph&, const _Value&) {}
703 EdgeMap(const EdgeMap&) {}
705 EdgeMap& operator=(const EdgeMap&) { return *this;}
709 template <typename _Graph>
715 Type(int _v) : value(_v) {}
719 checkConcept<BaseGraphComponent, _Graph>();
721 typedef typename _Graph::template NodeMap<int> IntNodeMap;
722 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>, IntNodeMap >();
724 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
725 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>, BoolNodeMap >();
727 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
728 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>, TypeNodeMap >();
732 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
733 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, IntEdgeMap >();
735 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
736 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, BoolEdgeMap >();
738 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
739 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>, TypeEdgeMap >();
748 class ExtendableGraphComponent : virtual public BaseGraphComponent {
751 typedef ExtendableGraphComponent Graph;
753 typedef BaseGraphComponent::Node Node;
754 typedef BaseGraphComponent::Edge Edge;
760 Edge addEdge(const Node& from, const Node& to) {
764 template <typename _Graph>
767 checkConcept<BaseGraphComponent, _Graph >();
768 typename _Graph::Node node_a, node_b;
769 node_a = graph.addNode();
770 typename _Graph::Edge edge;
771 edge = graph.addEdge(node_a, node_b);
776 class ErasableGraphComponent : virtual public BaseGraphComponent {
779 typedef ErasableGraphComponent Graph;
781 typedef BaseGraphComponent::Node Node;
782 typedef BaseGraphComponent::Edge Edge;
784 void erase(const Node&) {}
785 void erase(const Edge&) {}
787 template <typename _Graph>
790 checkConcept<BaseGraphComponent, _Graph >();
791 typename _Graph::Node node;
793 typename _Graph::Edge edge;
801 class ClearableGraphComponent : virtual public BaseGraphComponent {
804 typedef ClearableGraphComponent Graph;
806 typedef BaseGraphComponent::Node Node;
807 typedef BaseGraphComponent::Edge Edge;
812 template <typename _Graph>
813 struct ClearableGraphComponentConcept {
815 checkConcept< BaseGraphComponent, _Graph >();