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'.
46 // We explicitely list these:
47 GraphItem(GraphItem const&) {}
48 GraphItem& operator=(GraphItem const&) { return *this; }
50 bool operator==(GraphItem) const { return false; }
51 bool operator!=(GraphItem) const { return false; }
53 // Technical requirement. Do we really need this?
54 bool operator<(GraphItem) const { return false; }
59 struct GraphItemConcept {
68 b = (ia == ib) && (ia != ib) && (ia < ib);
69 b = (ia == INVALID) && (ib != INVALID);
77 template<typename Iter, typename Graph, typename BaseItem>
78 struct GraphIteratorConcept {
80 function_requires< GraphItemConcept<Iter> >();
83 /// \todo Do we need NodeIt(Node) kind of constructor?
90 /// \bug This should be: is_base_and_derived<BaseItem, Iter>
99 template<typename Iter, typename Graph>
100 struct GraphIncIteratorConcept {
101 typedef typename Graph::Node Node;
102 typedef typename Graph::Edge Edge;
104 function_requires< GraphItemConcept<Iter> >();
105 Iter it1(graph, node);
106 /// \todo Do we need OutEdgeIt(Edge) kind of constructor?
124 /// An empty base graph class.
126 /// This class provides the minimal set of features needed for a graph
127 /// structure. All graph concepts have to be conform to this base
130 /// \bug This is not true. The minimal graph concept is the
131 /// BaseIterableGraphComponent.
133 class BaseGraphComponent {
136 typedef BaseGraphComponent Graph;
138 /// Node class of the graph.
140 /// This class represents the Nodes of the graph.
145 /// Default constructor.
147 /// @warning The default constructor sets the iterator
148 /// to an undefined value.
151 /// Copy constructor.
153 /// Copy constructor.
157 /// Invalid constructor \& conversion.
159 /// This constructor initializes the iterator to be invalid.
160 /// \sa Invalid for more details.
164 Node& operator=(const Node&) { return *this;}
166 /// Equality operator.
168 /// Two iterators are equal if and only if they represents the
169 /// same node in the graph or both are invalid.
170 bool operator==(const Node&) const { return true;}
173 /// Inequality operator.
175 /// \sa operator==(const Node& n)
177 bool operator!=(const Node&) const { return true;}
179 /// Comparison operator.
181 /// This is a strict ordering between the nodes.
183 /// This ordering can be different from the iterating order of nodes.
184 /// \todo Possibly we don't need it.
185 bool operator<(const Node&) const { return true;}
188 /// Edge class of the graph.
190 /// This class represents the Edges of the graph.
195 /// Default constructor.
197 /// @warning The default constructor sets the iterator
198 /// to an undefined value.
201 /// Copy constructor.
203 /// Copy constructor.
207 /// Invalid constructor \& conversion.
209 /// This constructor initializes the iterator to be invalid.
210 /// \sa Invalid for more details.
214 Edge& operator=(const Edge&) { return *this;}
216 /// Equality operator.
218 /// Two iterators are equal if and only if they represents the
219 /// same edge in the graph or both are invalid.
220 bool operator==(const Edge&) const { return true;}
223 /// Inequality operator.
225 /// \sa operator==(const Edge& n)
227 bool operator!=(const Edge&) const { return true;}
229 /// Comparison operator.
231 /// This is a strict ordering between the edges.
233 /// This ordering can be different from the iterating order of edges.
234 /// \todo Possibly we don't need it.
235 bool operator<(const Edge&) const { return true;}
238 ///Gives back the head node of an edge.
240 ///Gives back the head node of an edge.
242 Node head(const Edge&) const { return INVALID;}
244 ///Gives back the tail node of an edge.
246 ///Gives back the tail node of an edge.
248 Node tail(const Edge&) const { return INVALID;}
252 /// Concept check structure for BaseGraph.
254 /// Concept check structure for BaseGraph.
257 template <typename Graph>
258 struct BaseGraphComponentConcept {
259 typedef typename Graph::Node Node;
260 typedef typename Graph::Edge Edge;
263 function_requires< GraphItemConcept<Node> >();
264 function_requires< GraphItemConcept<Edge> >();
276 /// An empty iterable base graph class.
278 /// This class provides beside the core graph features
279 /// core iterable interface for the graph structure.
280 /// Most of the base graphs should be conform to this concept.
282 class BaseIterableGraphComponent : virtual public BaseGraphComponent {
285 typedef BaseGraphComponent::Node Node;
286 typedef BaseGraphComponent::Edge Edge;
288 /// Gives back the first Node in the iterating order.
290 /// Gives back the first Node in the iterating order.
292 void first(Node&) const {}
294 /// Gives back the next Node in the iterating order.
296 /// Gives back the next Node in the iterating order.
298 void next(Node&) const {}
300 /// Gives back the first Edge in the iterating order.
302 /// Gives back the first Edge in the iterating order.
304 void first(Edge&) const {}
305 /// Gives back the next Edge in the iterating order.
307 /// Gives back the next Edge in the iterating order.
309 void next(Edge&) const {}
312 /// Gives back the first of the Edges point to the given Node.
314 /// Gives back the first of the Edges point to the given Node.
316 void firstIn(Edge&, const Node&) const {}
318 /// Gives back the next of the Edges points to the given Node.
321 /// Gives back the next of the Edges points to the given Node.
323 void nextIn(Edge&) const {}
325 /// Gives back the first of the Edges start from the given Node.
327 /// Gives back the first of the Edges start from the given Node.
329 void firstOut(Edge&, const Node&) const {}
331 /// Gives back the next of the Edges start from the given Node.
333 /// Gives back the next of the Edges start from the given Node.
335 void nextOut(Edge&) const {}
339 /// Concept check structure for IterableBaseGraph.
341 /// Concept check structure for IterableBaseGraph.
343 template <typename Graph>
344 struct BaseIterableGraphComponentConcept {
347 function_requires< BaseGraphComponentConcept<Graph> >();
348 typename Graph::Node node;
349 typename Graph::Edge edge;
359 graph.firstIn(edge, node);
363 graph.firstOut(edge, node);
371 /// An empty idable base graph class.
373 /// This class provides beside the core graph features
374 /// core id functions for the graph structure.
375 /// The most of the base graphs should be conform to this concept.
376 /// The id's are unique and immutable.
378 class IDableGraphComponent : virtual public BaseGraphComponent {
381 typedef BaseGraphComponent::Node Node;
382 typedef BaseGraphComponent::Edge Edge;
384 /// Gives back an unique integer id for the Node.
386 /// Gives back an unique integer id for the Node.
388 int id(const Node&) const { return -1;}
390 /// Gives back an unique integer id for the Edge.
392 /// Gives back an unique integer id for the Edge.
394 int id(const Edge&) const { return -1;}
398 /// Concept check structure for IdableBaseGraph.
400 /// Concept check structure for IdableBaseGraph.
402 template <typename Graph>
403 struct IDableGraphComponentConcept {
406 function_requires< BaseGraphComponentConcept<Graph> >();
407 typename Graph::Node node;
408 int nid = graph.id(node);
409 nid = graph.id(node);
410 typename Graph::Edge edge;
411 int eid = graph.id(edge);
412 eid = graph.id(edge);
419 /// An empty max-idable base graph class.
421 /// This class provides beside the core graph features
422 /// core max id functions for the graph structure.
423 /// The most of the base graphs should be conform to this concept.
424 /// The id's are unique and immutable.
425 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
428 /// Gives back an integer greater or equal to the maximum Node id.
430 /// Gives back an integer greater or equal to the maximum Node id.
432 int maxEdgeId() const { return -1;}
434 /// Gives back an integer greater or equal to the maximum Edge id.
436 /// Gives back an integer greater or equal to the maximum Edge id.
438 int maxNodeId() const { return -1;}
441 /// Concept check structure for MaxIdableBaseGraph.
443 /// Concept check structure for MaxIdableBaseGraph.
445 template <typename Graph>
446 struct MaxIDableGraphComponentConcept {
449 function_requires< BaseGraphComponentConcept<Graph> >();
450 int nid = graph.maxEdgeId();
451 ignore_unused_variable_warning(nid);
452 int eid = graph.maxNodeId();
453 ignore_unused_variable_warning(eid);
459 /// An empty extendable base graph class.
461 /// This class provides beside the core graph features
462 /// core graph extend interface for the graph structure.
463 /// The most of the base graphs should be conform to this concept.
464 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
467 typedef BaseGraphComponent::Node Node;
468 typedef BaseGraphComponent::Edge Edge;
470 /// Adds a new Node to the graph.
472 /// Adds a new Node to the graph.
478 /// Adds a new Edge connects the two Nodes to the graph.
480 /// Adds a new Edge connects the two Nodes to the graph.
482 Edge addEdge(const Node& from, const Node& to) {
488 /// Concept check structure for ExtendableBaseGraph.
490 /// Concept check structure for ExtendableBaseGraph.
492 template <typename Graph>
493 struct BaseExtendableGraphComponentConcept {
495 function_requires< BaseGraphComponentConcept<Graph> >();
496 typename Graph::Node node_a, node_b;
497 node_a = graph.addNode();
498 typename Graph::Edge edge;
499 edge = graph.addEdge(node_a, node_b);
505 /// An empty erasable base graph class.
507 /// This class provides beside the core graph features
508 /// core erase functions for the graph structure.
509 /// The most of the base graphs should be conform to this concept.
510 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
513 typedef BaseGraphComponent::Node Node;
514 typedef BaseGraphComponent::Edge Edge;
516 /// Erase a Node from the graph.
518 /// Erase a Node from the graph. This function should not
519 /// erase edges connecting to the Node.
520 void erase(const Node&) {}
522 /// Erase an Edge from the graph.
524 /// Erase an Edge from the graph.
526 void erase(const Edge&) {}
530 /// Concept check structure for ErasableBaseGraph.
532 /// Concept check structure for ErasableBaseGraph.
534 template <typename Graph>
535 struct BaseErasableGraphComponentConcept {
537 function_requires< BaseGraphComponentConcept<Graph> >();
538 typename Graph::Node node;
540 typename Graph::Edge edge;
547 /// An empty clearable base graph class.
549 /// This class provides beside the core graph features
550 /// core clear functions for the graph structure.
551 /// The most of the base graphs should be conform to this concept.
552 class BaseClearableGraphComponent : virtual public BaseGraphComponent {
555 /// Erase all the Nodes and Edges from the graph.
557 /// Erase all the Nodes and Edges from the graph.
562 /// Concept check function for ErasableBaseGraph.
564 /// Concept check function for ErasableBaseGraph.
566 template <typename Graph>
567 struct BaseClearableGraphComponentConcept {
569 function_requires< BaseGraphComponentConcept<Graph> >();
577 class IterableGraphComponent :
578 virtual public BaseIterableGraphComponent {
582 typedef IterableGraphComponent Graph;
584 typedef BaseGraphComponent::Node Node;
585 typedef BaseGraphComponent::Edge Edge;
587 class NodeIt : public Node {
591 // explicit NodeIt(Node) {}
592 explicit NodeIt(const Graph&) {}
594 NodeIt& operator++() { return *this; }
595 // Node operator*() const { return INVALID; }
597 bool operator==(const NodeIt&) const { return true;}
598 bool operator!=(const NodeIt&) const { return true;}
601 class EdgeIt : public Edge {
605 // explicit EdgeIt(Edge) {}
606 explicit EdgeIt(const Graph&) {}
608 EdgeIt& operator++() { return *this; }
609 // Edge operator*() const { return INVALID; }
611 bool operator==(const EdgeIt&) const { return true;}
612 bool operator!=(const EdgeIt&) const { return true;}
615 class InEdgeIt : public Edge {
619 // explicit InEdgeIt(Edge) {}
620 explicit InEdgeIt(const Graph&, const Node&) {}
622 InEdgeIt& operator++() { return *this; }
623 // Edge operator*() const { return INVALID; }
625 bool operator==(const InEdgeIt&) const { return true;}
626 bool operator!=(const InEdgeIt&) const { return true;}
629 class OutEdgeIt : public Edge {
632 OutEdgeIt(Invalid) {}
633 // explicit OutEdgeIt(Edge) {}
634 explicit OutEdgeIt(const Graph&, const Node&) {}
636 OutEdgeIt& operator++() { return *this; }
637 // Edge operator*() const { return INVALID; }
639 bool operator==(const OutEdgeIt&) const { return true;}
640 bool operator!=(const OutEdgeIt&) const { return true;}
645 template <typename Graph>
646 struct IterableGraphComponentConcept {
648 function_requires< BaseIterableGraphComponentConcept<Graph> >();
650 typedef typename Graph::Node Node;
651 typedef typename Graph::NodeIt NodeIt;
652 typedef typename Graph::Edge Edge;
653 typedef typename Graph::EdgeIt EdgeIt;
654 typedef typename Graph::InEdgeIt InEdgeIt;
655 typedef typename Graph::OutEdgeIt OutEdgeIt;
657 function_requires< GraphIteratorConcept<NodeIt, Graph, Node> >();
658 function_requires< GraphIteratorConcept<EdgeIt, Graph, Edge> >();
659 function_requires< GraphIncIteratorConcept<OutEdgeIt, Graph> >();
660 function_requires< GraphIncIteratorConcept<InEdgeIt, Graph> >();
665 class IdMappableGraphComponent : virtual public BaseGraphComponent {
667 typedef IdMappableGraphComponent Graph;
671 typedef BaseGraphComponent::Node Node;
672 typedef BaseGraphComponent::Edge Edge;
674 class NodeIdMap : public ReadMap<Node, int> {
676 NodeIdMap(const Graph&) {}
677 int maxId() const { return -1;}
680 class EdgeIdMap : public ReadMap<Edge, int> {
682 EdgeIdMap(const Graph&) {}
683 int maxId() const { return -1;}
688 template <typename Graph>
689 struct IdMappableGraphComponentConcept {
691 function_requires< BaseGraphComponentConcept<Graph> >();
693 typedef typename Graph::EdgeIdMap EdgeIdMap;
694 function_requires<ReadMapConcept<EdgeIdMap> >();
695 EdgeIdMap edge_map(graph);
696 int n = edge_map.maxId();
697 ignore_unused_variable_warning(n);
700 typedef typename Graph::NodeIdMap NodeIdMap;
701 function_requires<ReadMapConcept<NodeIdMap> >();
702 NodeIdMap node_map(graph);
703 int n = node_map.maxId();
704 ignore_unused_variable_warning(n);
711 class MappableGraphComponent : virtual public BaseGraphComponent {
714 typedef MappableGraphComponent Graph;
716 typedef BaseGraphComponent::Node Node;
717 typedef BaseGraphComponent::Edge Edge;
719 template <typename Value>
720 class NodeMap : public ReferenceMap<Node, Value> {
722 NodeMap(const Graph&) {}
723 NodeMap(const Graph&, const Value&) {}
724 NodeMap(const NodeMap&) {}
726 NodeMap& operator=(const NodeMap&) { return *this;}
730 template <typename Value>
731 class EdgeMap : public ReferenceMap<Edge, Value> {
733 EdgeMap(const Graph&) {}
734 EdgeMap(const Graph&, const Value&) {}
735 EdgeMap(const EdgeMap&) {}
737 EdgeMap& operator=(const EdgeMap&) { return *this;}
743 template <typename Graph>
744 struct MappableGraphComponentConcept {
749 Type(int _v) : value(_v) {}
753 function_requires< BaseGraphComponentConcept<Graph> >();
755 typedef typename Graph::template NodeMap<int> IntNodeMap;
756 function_requires<GraphMapConcept<IntNodeMap,Graph> >();
758 typedef typename Graph::template NodeMap<bool> BoolNodeMap;
759 function_requires<GraphMapConcept<BoolNodeMap,Graph> >();
761 typedef typename Graph::template NodeMap<Type> TypeNodeMap;
762 function_requires<GraphMapConcept<TypeNodeMap,Graph> >();
766 typedef typename Graph::template EdgeMap<int> IntEdgeMap;
767 function_requires<GraphMapConcept<IntEdgeMap,Graph> >();
769 typedef typename Graph::template EdgeMap<bool> BoolEdgeMap;
770 function_requires<GraphMapConcept<BoolEdgeMap,Graph> >();
772 typedef typename Graph::template EdgeMap<Type> TypeEdgeMap;
773 function_requires<GraphMapConcept<TypeEdgeMap,Graph> >();
781 class ExtendableGraphComponent : virtual public BaseGraphComponent {
784 typedef ExtendableGraphComponent Graph;
786 typedef BaseGraphComponent::Node Node;
787 typedef BaseGraphComponent::Edge Edge;
793 Edge addEdge(const Node& from, const Node& to) {
799 template <typename Graph>
800 struct ExtendableGraphComponentConcept {
802 function_requires< BaseGraphComponentConcept<Graph> >();
803 typename Graph::Node node_a, node_b;
804 node_a = graph.addNode();
805 typename Graph::Edge edge;
806 edge = graph.addEdge(node_a, node_b);
811 class ErasableGraphComponent : virtual public BaseGraphComponent {
814 typedef ErasableGraphComponent Graph;
816 typedef BaseGraphComponent::Node Node;
817 typedef BaseGraphComponent::Edge Edge;
819 void erase(const Node&) {}
820 void erase(const Edge&) {}
824 template <typename Graph>
825 struct ErasableGraphComponentConcept {
827 function_requires< BaseGraphComponentConcept<Graph> >();
828 typename Graph::Node node;
830 typename Graph::Edge edge;
837 class ClearableGraphComponent : virtual public BaseGraphComponent {
840 typedef ClearableGraphComponent Graph;
842 typedef BaseGraphComponent::Node Node;
843 typedef BaseGraphComponent::Edge Edge;
849 template <typename Graph>
850 struct ClearableGraphComponentConcept {
852 function_requires< BaseGraphComponentConcept<Graph> >();