Naming and coding style fixes and various other changes.
2 * src/lemon/skeletons/graph.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
17 #ifndef LEMON_SKELETON_GRAPH_H
18 #define LEMON_SKELETON_GRAPH_H
22 ///\brief Declaration of Graph.
24 #include <lemon/invalid.h>
25 #include <lemon/skeletons/maps.h>
30 /// \addtogroup skeletons
33 /// An empty static graph class.
35 /// This class provides all the common features of a graph structure,
36 /// however completely without implementations and real data structures
37 /// behind the interface.
38 /// All graph algorithms should compile with this class, but it will not
39 /// run properly, of course.
41 /// It can be used for checking the interface compatibility,
42 /// or it can serve as a skeleton of a new graph structure.
44 /// Also, you will find here the full documentation of a certain graph
45 /// feature, the documentation of a real graph imlementation
46 /// like @ref ListGraph or
47 /// @ref SmartGraph will just refer to this structure.
49 /// \todo A pages describing the concept of concept description would
54 /// Defalult constructor.
56 /// Defalult constructor.
61 // ///\todo It is not clear, what we expect from a copy constructor.
62 // ///E.g. How to assign the nodes/edges to each other? What about maps?
63 // StaticGraph(const StaticGraph& g) { }
65 /// The base type of node iterators,
66 /// or in other words, the trivial node iterator.
68 /// This is the base type of each node iterator,
69 /// thus each kind of node iterator converts to this.
70 /// More precisely each kind of node iterator should be inherited
71 /// from the trivial node iterator.
74 /// Default constructor
76 /// @warning The default constructor sets the iterator
77 /// to an undefined value.
85 /// Invalid constructor \& conversion.
87 /// This constructor initializes the iterator to be invalid.
88 /// \sa Invalid for more details.
92 /// Two iterators are equal if and only if they point to the
93 /// same object or both are invalid.
94 bool operator==(Node) const { return true; }
96 /// Inequality operator
98 /// \sa operator==(Node n)
100 bool operator!=(Node) const { return true; }
102 ///Comparison operator.
104 ///This is a strict ordering between the nodes.
106 ///This ordering can be different from the order in which NodeIt
107 ///goes through the nodes.
108 ///\todo Possibly we don't need it.
109 bool operator<(Node) const { return true; }
112 /// This iterator goes through each node.
114 /// This iterator goes through each node.
115 /// Its usage is quite simple, for example you can count the number
116 /// of nodes in graph \c g of type \c Graph like this:
119 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
121 class NodeIt : public Node {
123 /// Default constructor
125 /// @warning The default constructor sets the iterator
126 /// to an undefined value.
128 /// Copy constructor.
130 /// Copy constructor.
132 NodeIt(const NodeIt&) { }
133 /// Invalid constructor \& conversion.
135 /// Initialize the iterator to be invalid.
136 /// \sa Invalid for more details.
138 /// Sets the iterator to the first node.
140 /// Sets the iterator to the first node of \c g.
142 NodeIt(const StaticGraph& g) { }
143 /// Node -> NodeIt conversion.
145 /// Sets the iterator to the node of \c g pointed by the trivial
147 /// This feature necessitates that each time we
148 /// iterate the edge-set, the iteration order is the same.
149 NodeIt(const StaticGraph& g, const Node& n) { }
152 /// Assign the iterator to the next node.
154 NodeIt& operator++() { return *this; }
158 /// The base type of the edge iterators.
160 /// The base type of the edge iterators.
164 /// Default constructor
166 /// @warning The default constructor sets the iterator
167 /// to an undefined value.
169 /// Copy constructor.
171 /// Copy constructor.
173 Edge(const Edge&) { }
174 /// Initialize the iterator to be invalid.
176 /// Initialize the iterator to be invalid.
179 /// Equality operator
181 /// Two iterators are equal if and only if they point to the
182 /// same object or both are invalid.
183 bool operator==(Edge) const { return true; }
184 /// Inequality operator
186 /// \sa operator==(Node n)
188 bool operator!=(Edge) const { return true; }
189 ///Comparison operator.
191 ///This is a strict ordering between the nodes.
193 ///This ordering can be different from the order in which NodeIt
194 ///goes through the nodes.
195 ///\todo Possibly we don't need it.
196 bool operator<(Edge) const { return true; }
199 /// This iterator goes trough the outgoing edges of a node.
201 /// This iterator goes trough the \e outgoing edges of a certain node
203 /// Its usage is quite simple, for example you can count the number
204 /// of outgoing edges of a node \c n
205 /// in graph \c g of type \c Graph as follows.
208 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
211 class OutEdgeIt : public Edge {
213 /// Default constructor
215 /// @warning The default constructor sets the iterator
216 /// to an undefined value.
218 /// Copy constructor.
220 /// Copy constructor.
222 OutEdgeIt(const OutEdgeIt&) { }
223 /// Initialize the iterator to be invalid.
225 /// Initialize the iterator to be invalid.
227 OutEdgeIt(Invalid) { }
228 /// This constructor sets the iterator to first outgoing edge.
230 /// This constructor set the iterator to the first outgoing edge of
233 ///@param g the graph
234 OutEdgeIt(const StaticGraph& g, const Node& n) { }
235 /// Edge -> OutEdgeIt conversion
237 /// Sets the iterator to the value of the trivial iterator \c e.
238 /// This feature necessitates that each time we
239 /// iterate the edge-set, the iteration order is the same.
240 OutEdgeIt(const StaticGraph& g, const Edge& e) { }
241 ///Next outgoing edge
243 /// Assign the iterator to the next
244 /// outgoing edge of the corresponding node.
245 OutEdgeIt& operator++() { return *this; }
248 /// This iterator goes trough the incoming edges of a node.
250 /// This iterator goes trough the \e incoming edges of a certain node
252 /// Its usage is quite simple, for example you can count the number
253 /// of outgoing edges of a node \c n
254 /// in graph \c g of type \c Graph as follows.
257 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
260 class InEdgeIt : public Edge {
262 /// Default constructor
264 /// @warning The default constructor sets the iterator
265 /// to an undefined value.
267 /// Copy constructor.
269 /// Copy constructor.
271 InEdgeIt(const InEdgeIt&) { }
272 /// Initialize the iterator to be invalid.
274 /// Initialize the iterator to be invalid.
276 InEdgeIt(Invalid) { }
277 /// This constructor sets the iterator to first incoming edge.
279 /// This constructor set the iterator to the first incoming edge of
282 ///@param g the graph
283 InEdgeIt(const StaticGraph& g, const Node& n) { }
284 /// Edge -> InEdgeIt conversion
286 /// Sets the iterator to the value of the trivial iterator \c e.
287 /// This feature necessitates that each time we
288 /// iterate the edge-set, the iteration order is the same.
289 InEdgeIt(const StaticGraph& g, const Edge& n) { }
290 /// Next incoming edge
292 /// Assign the iterator to the next inedge of the corresponding node.
294 InEdgeIt& operator++() { return *this; }
296 /// This iterator goes through each edge.
298 /// This iterator goes through each edge of a graph.
299 /// Its usage is quite simple, for example you can count the number
300 /// of edges in a graph \c g of type \c Graph as follows:
303 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
305 class EdgeIt : public Edge {
307 /// Default constructor
309 /// @warning The default constructor sets the iterator
310 /// to an undefined value.
312 /// Copy constructor.
314 /// Copy constructor.
316 EdgeIt(const EdgeIt&) { }
317 /// Initialize the iterator to be invalid.
319 /// Initialize the iterator to be invalid.
322 /// This constructor sets the iterator to first edge.
324 /// This constructor set the iterator to the first edge of
326 ///@param g the graph
327 EdgeIt(const StaticGraph& g) { }
328 /// Edge -> EdgeIt conversion
330 /// Sets the iterator to the value of the trivial iterator \c e.
331 /// This feature necessitates that each time we
332 /// iterate the edge-set, the iteration order is the same.
333 EdgeIt(const StaticGraph&, const Edge&) { }
336 /// Assign the iterator to the next
337 /// edge of the corresponding node.
338 EdgeIt& operator++() { return *this; }
341 /// First node of the graph.
343 /// \retval i the first node.
344 /// \return the first node.
346 NodeIt& first(NodeIt& i) const { return i; }
348 /// The first incoming edge.
350 /// The first incoming edge.
352 InEdgeIt& first(InEdgeIt &i, Node) const { return i; }
353 /// The first outgoing edge.
355 /// The first outgoing edge.
357 OutEdgeIt& first(OutEdgeIt& i, Node) const { return i; }
358 /// The first edge of the Graph.
360 /// The first edge of the Graph.
362 EdgeIt& first(EdgeIt& i) const { return i; }
364 ///Gives back the head node of an edge.
366 ///Gives back the head node of an edge.
368 Node head(Edge) const { return INVALID; }
369 ///Gives back the tail node of an edge.
371 ///Gives back the tail node of an edge.
373 Node tail(Edge) const { return INVALID; }
375 ///Gives back the \e id of a node.
377 ///\warning Not all graph structures provide this feature.
379 ///\todo Should each graph provide \c id?
380 int id(const Node&) const { return 0; }
381 ///Gives back the \e id of an edge.
383 ///\warning Not all graph structures provide this feature.
385 ///\todo Should each graph provide \c id?
386 int id(const Edge&) const { return 0; }
390 ///\todo Should it be in the concept?
392 int nodeNum() const { return 0; }
395 ///\todo Should it be in the concept?
397 int edgeNum() const { return 0; }
400 ///Reference map of the nodes to type \c T.
402 /// \ingroup skeletons
403 ///Reference map of the nodes to type \c T.
405 /// \warning Making maps that can handle bool type (NodeMap<bool>)
406 /// needs some extra attention!
407 template<class T> class NodeMap : public ReferenceMap< Node, T >
412 NodeMap(const StaticGraph&) { }
414 NodeMap(const StaticGraph&, T) { }
417 template<typename TT> NodeMap(const NodeMap<TT>&) { }
418 ///Assignment operator
419 template<typename TT> NodeMap& operator=(const NodeMap<TT>&)
423 ///Reference map of the edges to type \c T.
425 /// \ingroup skeletons
426 ///Reference map of the edges to type \c T.
428 /// \warning Making maps that can handle bool type (EdgeMap<bool>)
429 /// needs some extra attention!
430 template<class T> class EdgeMap
431 : public ReferenceMap<Edge,T>
436 EdgeMap(const StaticGraph&) { }
438 EdgeMap(const StaticGraph&, T) { }
441 template<typename TT> EdgeMap(const EdgeMap<TT>&) { }
442 ///Assignment operator
443 template<typename TT> EdgeMap &operator=(const EdgeMap<TT>&)
451 DummyType(int p) : value(p) {}
452 DummyType& operator=(int p) { value = p; return *this;}
455 ///\brief Checks whether \c G meets the
456 ///\ref lemon::skeleton::StaticGraph "StaticGraph" concept
457 template<class Graph> void checkCompileStaticGraph(Graph &G)
459 typedef typename Graph::Node Node;
460 typedef typename Graph::NodeIt NodeIt;
461 typedef typename Graph::Edge Edge;
462 typedef typename Graph::EdgeIt EdgeIt;
463 typedef typename Graph::InEdgeIt InEdgeIt;
464 typedef typename Graph::OutEdgeIt OutEdgeIt;
467 Node i; Node j(i); Node k(INVALID);
470 b=(i==INVALID); b=(i!=INVALID);
471 b=(i==j); b=(i!=j); b=(i<j);
474 NodeIt i; NodeIt j(i); NodeIt k(INVALID); NodeIt l(G);
479 b=(i==INVALID); b=(i!=INVALID);
482 b=(i==j); b=(i!=j); b=(i<j);
483 //Node ->NodeIt conversion
487 Edge i; Edge j(i); Edge k(INVALID);
490 b=(i==INVALID); b=(i!=INVALID);
491 b=(i==j); b=(i!=j); b=(i<j);
494 EdgeIt i; EdgeIt j(i); EdgeIt k(INVALID); EdgeIt l(G);
499 b=(i==INVALID); b=(i!=INVALID);
502 b=(i==j); b=(i!=j); b=(i<j);
503 //Edge ->EdgeIt conversion
508 InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt l(G,n);
513 b=(i==INVALID); b=(i!=INVALID);
516 b=(i==j); b=(i!=j); b=(i<j);
517 //Edge ->InEdgeIt conversion
522 OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n);
527 b=(i==INVALID); b=(i!=INVALID);
530 b=(i==j); b=(i!=j); b=(i<j);
531 //Edge ->OutEdgeIt conversion
543 { Node n; int i=G.id(n); i=i; }
544 { Edge e; int i=G.id(e); i=i; }
548 typename Graph::template NodeMap<int> m(G);
550 typename Graph::template NodeMap<int> const &cm = m;
551 //Inicialize with default value
552 typename Graph::template NodeMap<int> mdef(G,12);
554 typename Graph::template NodeMap<int> mm(cm);
555 //Copy from another type
556 typename Graph::template NodeMap<double> dm(cm);
557 //Copy to more complex type
558 typename Graph::template NodeMap<DummyType> em(cm);
560 v=m[k]; m[k]=v; m.set(k,v);
564 dm=cm; //Copy from another type
565 em=cm; //Copy to more complex type
567 //Check the typedef's
568 typename Graph::template NodeMap<int>::ValueType val;
570 typename Graph::template NodeMap<int>::KeyType key;
571 key = typename Graph::NodeIt(G);
576 typename Graph::template NodeMap<bool> m(G);
577 typename Graph::template NodeMap<bool> const &cm = m; //Const map
578 //Inicialize with default value
579 typename Graph::template NodeMap<bool> mdef(G,12);
580 typename Graph::template NodeMap<bool> mm(cm); //Copy
581 typename Graph::template NodeMap<int> dm(cm); //Copy from another type
583 v=m[k]; m[k]=v; m.set(k,v);
587 dm=cm; //Copy from another type
588 m=dm; //Copy to another type
591 //Check the typedef's
592 typename Graph::template NodeMap<bool>::ValueType val;
594 typename Graph::template NodeMap<bool>::KeyType key;
595 key= typename Graph::NodeIt(G);
601 typename Graph::template EdgeMap<int> m(G);
602 typename Graph::template EdgeMap<int> const &cm = m; //Const map
603 //Inicialize with default value
604 typename Graph::template EdgeMap<int> mdef(G,12);
605 typename Graph::template EdgeMap<int> mm(cm); //Copy
606 typename Graph::template EdgeMap<double> dm(cm);//Copy from another type
608 v=m[k]; m[k]=v; m.set(k,v);
612 dm=cm; //Copy from another type
614 //Check the typedef's
615 typename Graph::template EdgeMap<int>::ValueType val;
617 typename Graph::template EdgeMap<int>::KeyType key;
618 key= typename Graph::EdgeIt(G);
623 typename Graph::template EdgeMap<bool> m(G);
624 typename Graph::template EdgeMap<bool> const &cm = m; //Const map
625 //Inicialize with default value
626 typename Graph::template EdgeMap<bool> mdef(G,12);
627 typename Graph::template EdgeMap<bool> mm(cm); //Copy
628 typename Graph::template EdgeMap<int> dm(cm); //Copy from another type
630 v=m[k]; m[k]=v; m.set(k,v);
634 dm=cm; //Copy from another type
635 m=dm; //Copy to another type
637 //Check the typedef's
638 typename Graph::template EdgeMap<bool>::ValueType val;
640 typename Graph::template EdgeMap<bool>::KeyType key;
641 key= typename Graph::EdgeIt(G);
646 /// An empty non-static graph class.
648 /// This class provides everything that \ref StaticGraph
649 /// with additional functionality which enables to build a
650 /// graph from scratch.
651 class ExtendableGraph : public StaticGraph
654 /// Defalult constructor.
656 /// Defalult constructor.
658 ExtendableGraph() { }
659 ///Add a new node to the graph.
661 /// \return the new node.
663 Node addNode() { return INVALID; }
664 ///Add a new edge to the graph.
666 ///Add a new edge to the graph with tail node \c t
667 ///and head node \c h.
668 ///\return the new edge.
669 Edge addEdge(Node h, Node t) { return INVALID; }
671 /// Resets the graph.
673 /// This function deletes all edges and nodes of the graph.
674 /// It also frees the memory allocated to store them.
675 /// \todo It might belong to \ref ErasableGraph.
680 ///\brief Checks whether \c G meets the
681 ///\ref lemon::skeleton::ExtendableGraph "ExtendableGraph" concept
682 template<class Graph> void checkCompileExtendableGraph(Graph &G)
684 checkCompileStaticGraph(G);
686 typedef typename Graph::Node Node;
687 typedef typename Graph::NodeIt NodeIt;
688 typedef typename Graph::Edge Edge;
689 typedef typename Graph::EdgeIt EdgeIt;
690 typedef typename Graph::InEdgeIt InEdgeIt;
691 typedef typename Graph::OutEdgeIt OutEdgeIt;
703 /// An empty erasable graph class.
705 /// This class is an extension of \ref ExtendableGraph. It also makes it
706 /// possible to erase edges or nodes.
707 class ErasableGraph : public ExtendableGraph
710 /// Defalult constructor.
712 /// Defalult constructor.
717 /// Deletes node \c n node.
719 void erase(Node n) { }
722 /// Deletes edge \c e edge.
724 void erase(Edge e) { }
727 template<class Graph> void checkCompileGraphEraseEdge(Graph &G)
729 typename Graph::Edge e;
733 template<class Graph> void checkCompileGraphEraseNode(Graph &G)
735 typename Graph::Node n;
739 ///\brief Checks whether \c G meets the
740 ///\ref lemon::skeleton::EresableGraph "EresableGraph" concept
741 template<class Graph> void checkCompileErasableGraph(Graph &G)
743 checkCompileExtendableGraph(G);
744 checkCompileGraphEraseNode(G);
745 checkCompileGraphEraseEdge(G);
748 ///Checks whether a graph has findEdge() member function.
750 ///\todo findEdge() might be a global function.
752 template<class Graph> void checkCompileGraphFindEdge(Graph &G)
754 typedef typename Graph::NodeIt Node;
755 typedef typename Graph::NodeIt NodeIt;
757 G.findEdge(NodeIt(G),++NodeIt(G),G.findEdge(NodeIt(G),++NodeIt(G)));
758 G.findEdge(Node(),Node(),G.findEdge(Node(),Node()));
762 } //namespace skeleton
767 #endif // LEMON_SKELETON_GRAPH_H