3 #ifndef HUGO_FULL_GRAPH_H
4 #define HUGO_FULL_GRAPH_H
8 ///\brief FullGraph and SymFullGraph classes.
13 #include <hugo/invalid.h>
15 #include <hugo/map_registry.h>
16 #include <hugo/array_map_factory.h>
20 /// \addtogroup graphs
23 ///A full graph class.
25 ///This is a simple and fast directed full graph implementation.
26 ///It is completely static, so you can neither add nor delete either
28 ///Otherwise it conforms to the graph interface documented under
29 ///the description of \ref GraphSkeleton.
30 ///\sa \ref GraphSkeleton.
31 ///\todo What about loops?
32 ///\todo Don't we need SymEdgeMap?
34 ///\author Alpar Juttner
40 typedef FullGraph Graph;
50 CREATE_MAP_REGISTRIES;
51 CREATE_MAPS(ArrayMapFactory);
55 ///Creates a full graph with \c n nodes.
56 FullGraph(int n) : NodeNum(n), EdgeNum(NodeNum*NodeNum) { }
58 FullGraph(const FullGraph &_g)
59 : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
61 int nodeNum() const { return NodeNum; } //FIXME: What is this?
62 int edgeNum() const { return EdgeNum; } //FIXME: What is this?
64 int maxNodeId() const { return NodeNum; } //FIXME: What is this?
65 int maxEdgeId() const { return EdgeNum; } //FIXME: What is this?
67 Node tail(Edge e) const { return e.n%NodeNum; }
68 Node head(Edge e) const { return e.n/NodeNum; }
70 NodeIt& first(NodeIt& v) const {
71 v=NodeIt(*this); return v; }
72 EdgeIt& first(EdgeIt& e) const {
73 e=EdgeIt(*this); return e; }
74 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
75 e=OutEdgeIt(*this,v); return e; }
76 InEdgeIt& first(InEdgeIt& e, const Node v) const {
77 e=InEdgeIt(*this,v); return e; }
79 static int id(Node v) { return v.n; }
80 static int id(Edge e) { return e.n; }
82 /// Finds an edge between two nodes.
84 /// Finds an edge from node \c u to node \c v.
86 /// If \c prev is \ref INVALID (this is the default value), then
87 /// It finds the first edge from \c u to \c v. Otherwise it looks for
88 /// the next edge from \c u to \c v after \c prev.
89 /// \return The found edge or INVALID if there is no such an edge.
90 Edge findEdge(Node u,Node v, Edge prev = INVALID)
92 return prev.n == -1 ? Edge(*this, u.n, v.n) : INVALID;
97 friend class FullGraph;
98 template <typename T> friend class NodeMap;
101 friend class OutEdgeIt;
102 friend class InEdgeIt;
103 friend class SymEdge;
107 friend int FullGraph::id(Node v);
111 Node (Invalid) { n=-1; }
112 bool operator==(const Node i) const {return n==i.n;}
113 bool operator!=(const Node i) const {return n!=i.n;}
114 bool operator<(const Node i) const {return n<i.n;}
117 class NodeIt : public Node {
119 friend class FullGraph;
121 NodeIt() : Node() { }
122 NodeIt(const FullGraph& _G,Node n) : Node(n), G(&_G) { }
123 NodeIt(Invalid i) : Node(i) { }
124 NodeIt(const FullGraph& _G) : Node(_G.NodeNum?0:-1), G(&_G) { }
125 ///\todo Undocumented conversion Node -\> NodeIt.
126 NodeIt& operator++() { n=(n+2)%(G->NodeNum+1)-1;return *this; }
130 friend class FullGraph;
131 template <typename T> friend class EdgeMap;
136 int n; //NodeNum*head+tail;
137 friend int FullGraph::id(Edge e);
139 Edge(int nn) : n(nn) {}
140 Edge(const FullGraph &G, int tail, int head) : n(G.NodeNum*head+tail) {}
143 Edge (Invalid) { n=-1; }
144 bool operator==(const Edge i) const {return n==i.n;}
145 bool operator!=(const Edge i) const {return n!=i.n;}
146 bool operator<(const Edge i) const {return n<i.n;}
147 ///\bug This is a workaround until somebody tells me how to
148 ///make class \c SymFullGraph::SymEdgeMap friend of Edge
149 int &idref() {return n;}
150 const int &idref() const {return n;}
153 class EdgeIt : public Edge {
154 friend class FullGraph;
156 EdgeIt(const FullGraph& _G) : Edge(_G.EdgeNum-1) { }
157 EdgeIt(const FullGraph&, Edge e) : Edge(e) { }
158 EdgeIt (Invalid i) : Edge(i) { }
159 EdgeIt() : Edge() { }
160 EdgeIt& operator++() { --n; return *this; }
162 ///\bug This is a workaround until somebody tells me how to
163 ///make class \c SymFullGraph::SymEdgeMap friend of Edge
164 int &idref() {return n;}
167 class OutEdgeIt : public Edge {
169 friend class FullGraph;
171 OutEdgeIt() : Edge() { }
172 OutEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
173 OutEdgeIt (Invalid i) : Edge(i) { }
175 OutEdgeIt(const FullGraph& _G,const Node v) : Edge(v.n), G(&_G) {}
177 OutEdgeIt& operator++()
178 { n+=G->NodeNum; if(n>=G->EdgeNum) n=-1; return *this; }
182 class InEdgeIt : public Edge {
184 friend class FullGraph;
186 InEdgeIt() : Edge() { }
187 InEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
188 InEdgeIt (Invalid i) : Edge(i) { }
189 InEdgeIt(const FullGraph& _G,Node v) : Edge(v.n*_G.NodeNum), G(&_G) {}
190 InEdgeIt& operator++()
191 { if(!((++n)%G->NodeNum)) n=-1; return *this; }
203 #endif //HUGO_FULL_GRAPH_H