marci@174
|
1 |
// -*- c++ -*-
|
marci@259
|
2 |
#ifndef HUGO_GRAPH_WRAPPER_H
|
marci@259
|
3 |
#define HUGO_GRAPH_WRAPPER_H
|
marci@76
|
4 |
|
marci@174
|
5 |
#include <invalid.h>
|
marci@368
|
6 |
#include <iter_map.h>
|
marci@174
|
7 |
|
alpar@105
|
8 |
namespace hugo {
|
marci@76
|
9 |
|
alpar@438
|
10 |
// Graph wrappers
|
alpar@438
|
11 |
|
alpar@406
|
12 |
/// \addtogroup gwrappers
|
alpar@344
|
13 |
/// A main parts of HUGOlib are the different graph structures,
|
marci@335
|
14 |
/// generic graph algorithms, graph concepts which couple these, and
|
marci@335
|
15 |
/// graph wrappers. While the previous ones are more or less clear, the
|
marci@335
|
16 |
/// latter notion needs further explanation.
|
marci@335
|
17 |
/// Graph wrappers are graph classes which serve for considering graph
|
alpar@344
|
18 |
/// structures in different ways. A short example makes the notion much
|
alpar@344
|
19 |
/// clearer.
|
alpar@344
|
20 |
/// Suppose that we have an instance \c g of a directed graph
|
alpar@344
|
21 |
/// type say \c ListGraph and an algorithm
|
marci@335
|
22 |
/// \code template<typename Graph> int algorithm(const Graph&); \endcode
|
alpar@344
|
23 |
/// is needed to run on the reversely oriented graph.
|
alpar@344
|
24 |
/// It may be expensive (in time or in memory usage) to copy
|
alpar@344
|
25 |
/// \c g with the reverse orientation.
|
marci@335
|
26 |
/// Thus, a wrapper class
|
marci@335
|
27 |
/// \code template<typename Graph> class RevGraphWrapper; \endcode is used.
|
marci@335
|
28 |
/// The code looks as follows
|
marci@335
|
29 |
/// \code
|
marci@335
|
30 |
/// ListGraph g;
|
marci@335
|
31 |
/// RevGraphWrapper<ListGraph> rgw(g);
|
marci@335
|
32 |
/// int result=algorithm(rgw);
|
marci@335
|
33 |
/// \endcode
|
alpar@344
|
34 |
/// After running the algorithm, the original graph \c g
|
alpar@344
|
35 |
/// remains untouched. Thus the graph wrapper used above is to consider the
|
alpar@344
|
36 |
/// original graph with reverse orientation.
|
marci@335
|
37 |
/// This techniques gives rise to an elegant code, and
|
marci@335
|
38 |
/// based on stable graph wrappers, complex algorithms can be
|
marci@335
|
39 |
/// implemented easily.
|
marci@335
|
40 |
/// In flow, circulation and bipartite matching problems, the residual
|
alpar@344
|
41 |
/// graph is of particular importance. Combining a wrapper implementing
|
alpar@344
|
42 |
/// this, shortest path algorithms and minimum mean cycle algorithms,
|
marci@335
|
43 |
/// a range of weighted and cardinality optimization algorithms can be
|
marci@335
|
44 |
/// obtained. For lack of space, for other examples,
|
alpar@344
|
45 |
/// the interested user is referred to the detailed documentation of graph
|
marci@335
|
46 |
/// wrappers.
|
alpar@344
|
47 |
/// The behavior of graph wrappers can be very different. Some of them keep
|
marci@335
|
48 |
/// capabilities of the original graph while in other cases this would be
|
alpar@344
|
49 |
/// meaningless. This means that the concepts that they are a model of depend
|
marci@335
|
50 |
/// on the graph wrapper, and the wrapped graph(s).
|
alpar@344
|
51 |
/// If an edge of \c rgw is deleted, this is carried out by
|
alpar@344
|
52 |
/// deleting the corresponding edge of \c g. But for a residual
|
marci@335
|
53 |
/// graph, this operation has no sense.
|
marci@335
|
54 |
/// Let we stand one more example here to simplify your work.
|
marci@335
|
55 |
/// wrapper class
|
marci@335
|
56 |
/// \code template<typename Graph> class RevGraphWrapper; \endcode
|
marci@335
|
57 |
/// has constructor
|
alpar@344
|
58 |
/// <tt> RevGraphWrapper(Graph& _g)</tt>.
|
marci@335
|
59 |
/// This means that in a situation,
|
alpar@344
|
60 |
/// when a <tt> const ListGraph& </tt> reference to a graph is given,
|
alpar@344
|
61 |
/// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
|
marci@335
|
62 |
/// \code
|
marci@335
|
63 |
/// int algorithm1(const ListGraph& g) {
|
marci@335
|
64 |
/// RevGraphWrapper<const ListGraph> rgw(g);
|
marci@335
|
65 |
/// return algorithm2(rgw);
|
marci@335
|
66 |
/// }
|
marci@335
|
67 |
/// \endcode
|
alpar@438
|
68 |
|
alpar@438
|
69 |
/// \addtogroup gwrappers
|
alpar@438
|
70 |
/// @{
|
alpar@438
|
71 |
|
alpar@438
|
72 |
///Base type for the Graph Wrappers
|
alpar@438
|
73 |
|
alpar@438
|
74 |
///This is the base type for the Graph Wrappers.
|
alpar@438
|
75 |
///\todo Some more docs...
|
alpar@438
|
76 |
|
marci@303
|
77 |
template<typename Graph>
|
marci@303
|
78 |
class GraphWrapper {
|
marci@212
|
79 |
protected:
|
marci@303
|
80 |
Graph* graph;
|
marci@212
|
81 |
|
marci@212
|
82 |
public:
|
marci@311
|
83 |
typedef Graph BaseGraph;
|
marci@303
|
84 |
typedef Graph ParentGraph;
|
marci@212
|
85 |
|
marci@303
|
86 |
// GraphWrapper() : graph(0) { }
|
marci@303
|
87 |
GraphWrapper(Graph& _graph) : graph(&_graph) { }
|
marci@303
|
88 |
// void setGraph(Graph& _graph) { graph=&_graph; }
|
marci@303
|
89 |
// Graph& getGraph() const { return *graph; }
|
marci@303
|
90 |
|
marci@317
|
91 |
// typedef typename Graph::Node Node;
|
marci@317
|
92 |
class Node : public Graph::Node {
|
marci@317
|
93 |
friend class GraphWrapper<Graph>;
|
marci@265
|
94 |
public:
|
marci@317
|
95 |
Node() { }
|
marci@317
|
96 |
Node(const typename Graph::Node& _n) : Graph::Node(_n) { }
|
marci@317
|
97 |
Node(const Invalid& i) : Graph::Node(i) { }
|
marci@317
|
98 |
};
|
marci@317
|
99 |
class NodeIt {
|
marci@317
|
100 |
friend class GraphWrapper<Graph>;
|
marci@317
|
101 |
typename Graph::NodeIt n;
|
marci@317
|
102 |
public:
|
marci@265
|
103 |
NodeIt() { }
|
marci@317
|
104 |
NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
|
marci@317
|
105 |
NodeIt(const Invalid& i) : n(i) { }
|
marci@317
|
106 |
NodeIt(const GraphWrapper<Graph>& _G) : n(*(_G.graph)) { }
|
marci@317
|
107 |
operator Node() const { return Node(typename Graph::Node(n)); }
|
marci@265
|
108 |
};
|
marci@317
|
109 |
// typedef typename Graph::Edge Edge;
|
marci@317
|
110 |
class Edge : public Graph::Edge {
|
marci@317
|
111 |
friend class GraphWrapper<Graph>;
|
marci@317
|
112 |
public:
|
marci@317
|
113 |
Edge() { }
|
marci@317
|
114 |
Edge(const typename Graph::Edge& _e) : Graph::Edge(_e) { }
|
marci@317
|
115 |
Edge(const Invalid& i) : Graph::Edge(i) { }
|
marci@317
|
116 |
};
|
marci@317
|
117 |
class OutEdgeIt {
|
marci@317
|
118 |
friend class GraphWrapper<Graph>;
|
marci@317
|
119 |
typename Graph::OutEdgeIt e;
|
marci@265
|
120 |
public:
|
marci@265
|
121 |
OutEdgeIt() { }
|
marci@317
|
122 |
OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
|
marci@317
|
123 |
OutEdgeIt(const Invalid& i) : e(i) { }
|
marci@317
|
124 |
OutEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
|
marci@317
|
125 |
e(*(_G.graph), typename Graph::Node(_n)) { }
|
marci@317
|
126 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@265
|
127 |
};
|
marci@317
|
128 |
class InEdgeIt {
|
marci@317
|
129 |
friend class GraphWrapper<Graph>;
|
marci@317
|
130 |
typename Graph::InEdgeIt e;
|
marci@265
|
131 |
public:
|
marci@265
|
132 |
InEdgeIt() { }
|
marci@317
|
133 |
InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
|
marci@317
|
134 |
InEdgeIt(const Invalid& i) : e(i) { }
|
marci@317
|
135 |
InEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
|
marci@317
|
136 |
e(*(_G.graph), typename Graph::Node(_n)) { }
|
marci@317
|
137 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@265
|
138 |
};
|
marci@303
|
139 |
//typedef typename Graph::SymEdgeIt SymEdgeIt;
|
marci@317
|
140 |
class EdgeIt {
|
marci@317
|
141 |
friend class GraphWrapper<Graph>;
|
marci@317
|
142 |
typename Graph::EdgeIt e;
|
marci@265
|
143 |
public:
|
marci@265
|
144 |
EdgeIt() { }
|
marci@317
|
145 |
EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
|
marci@317
|
146 |
EdgeIt(const Invalid& i) : e(i) { }
|
marci@317
|
147 |
EdgeIt(const GraphWrapper<Graph>& _G) : e(*(_G.graph)) { }
|
marci@317
|
148 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@265
|
149 |
};
|
marci@303
|
150 |
|
marci@303
|
151 |
NodeIt& first(NodeIt& i) const {
|
marci@317
|
152 |
i=NodeIt(*this); return i;
|
marci@265
|
153 |
}
|
marci@303
|
154 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@317
|
155 |
i=OutEdgeIt(*this, p); return i;
|
marci@303
|
156 |
}
|
marci@303
|
157 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@317
|
158 |
i=InEdgeIt(*this, p); return i;
|
marci@303
|
159 |
}
|
marci@311
|
160 |
EdgeIt& first(EdgeIt& i) const {
|
marci@317
|
161 |
i=EdgeIt(*this); return i;
|
marci@311
|
162 |
}
|
marci@338
|
163 |
|
marci@317
|
164 |
NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
|
marci@317
|
165 |
OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
|
marci@317
|
166 |
InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
|
marci@317
|
167 |
EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }
|
marci@212
|
168 |
|
marci@379
|
169 |
Node tail(const Edge& e) const {
|
marci@379
|
170 |
return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
|
marci@317
|
171 |
Node head(const Edge& e) const {
|
marci@317
|
172 |
return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
|
marci@212
|
173 |
|
marci@317
|
174 |
bool valid(const Node& n) const {
|
marci@317
|
175 |
return graph->valid(static_cast<typename Graph::Node>(n)); }
|
marci@317
|
176 |
bool valid(const Edge& e) const {
|
marci@317
|
177 |
return graph->valid(static_cast<typename Graph::Edge>(e)); }
|
marci@212
|
178 |
|
marci@303
|
179 |
int nodeNum() const { return graph->nodeNum(); }
|
marci@303
|
180 |
int edgeNum() const { return graph->edgeNum(); }
|
marci@212
|
181 |
|
marci@317
|
182 |
Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
|
marci@317
|
183 |
Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
|
marci@317
|
184 |
Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
|
marci@317
|
185 |
Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
|
marci@212
|
186 |
|
marci@317
|
187 |
Node addNode() const { return Node(graph->addNode()); }
|
marci@212
|
188 |
Edge addEdge(const Node& tail, const Node& head) const {
|
marci@317
|
189 |
return Edge(graph->addEdge(tail, head)); }
|
marci@317
|
190 |
|
marci@317
|
191 |
void erase(const Node& i) const { graph->erase(i); }
|
marci@317
|
192 |
void erase(const Edge& i) const { graph->erase(i); }
|
marci@212
|
193 |
|
marci@303
|
194 |
void clear() const { graph->clear(); }
|
marci@212
|
195 |
|
marci@389
|
196 |
template<typename T> class NodeMap : public Graph::template NodeMap<T> {
|
marci@389
|
197 |
typedef typename Graph::template NodeMap<T> Parent;
|
marci@212
|
198 |
public:
|
marci@389
|
199 |
NodeMap(const GraphWrapper<Graph>& _G) : Parent(*(_G.graph)) { }
|
marci@389
|
200 |
NodeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
|
marci@212
|
201 |
};
|
marci@212
|
202 |
|
marci@389
|
203 |
template<typename T> class EdgeMap : public Graph::template EdgeMap<T> {
|
marci@389
|
204 |
typedef typename Graph::template EdgeMap<T> Parent;
|
marci@212
|
205 |
public:
|
marci@389
|
206 |
EdgeMap(const GraphWrapper<Graph>& _G) : Parent(*(_G.graph)) { }
|
marci@389
|
207 |
EdgeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
|
marci@212
|
208 |
};
|
marci@212
|
209 |
};
|
marci@212
|
210 |
|
marci@338
|
211 |
/// A graph wrapper which reverses the orientation of the edges.
|
marci@303
|
212 |
|
marci@338
|
213 |
/// A graph wrapper which reverses the orientation of the edges.
|
marci@303
|
214 |
template<typename Graph>
|
marci@303
|
215 |
class RevGraphWrapper : public GraphWrapper<Graph> {
|
marci@230
|
216 |
public:
|
marci@338
|
217 |
|
marci@338
|
218 |
RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
|
marci@338
|
219 |
|
marci@303
|
220 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@303
|
221 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@303
|
222 |
//If Graph::OutEdgeIt is not defined
|
marci@279
|
223 |
//and we do not want to use RevGraphWrapper::InEdgeIt,
|
marci@338
|
224 |
//the typdef techinque does not work.
|
marci@338
|
225 |
//Unfortunately all the typedefs are instantiated in templates.
|
marci@338
|
226 |
//typedef typename GraphWrapper<Graph>::OutEdgeIt InEdgeIt;
|
marci@338
|
227 |
//typedef typename GraphWrapper<Graph>::InEdgeIt OutEdgeIt;
|
marci@237
|
228 |
|
marci@338
|
229 |
class OutEdgeIt {
|
marci@338
|
230 |
friend class GraphWrapper<Graph>;
|
marci@338
|
231 |
friend class RevGraphWrapper<Graph>;
|
marci@379
|
232 |
typename Graph::InEdgeIt e;
|
marci@338
|
233 |
public:
|
marci@338
|
234 |
OutEdgeIt() { }
|
marci@379
|
235 |
OutEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
|
marci@338
|
236 |
OutEdgeIt(const Invalid& i) : e(i) { }
|
marci@338
|
237 |
OutEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
|
marci@338
|
238 |
e(*(_G.graph), typename Graph::Node(_n)) { }
|
marci@338
|
239 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@338
|
240 |
};
|
marci@338
|
241 |
class InEdgeIt {
|
marci@338
|
242 |
friend class GraphWrapper<Graph>;
|
marci@338
|
243 |
friend class RevGraphWrapper<Graph>;
|
marci@379
|
244 |
typename Graph::OutEdgeIt e;
|
marci@338
|
245 |
public:
|
marci@338
|
246 |
InEdgeIt() { }
|
marci@379
|
247 |
InEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
|
marci@338
|
248 |
InEdgeIt(const Invalid& i) : e(i) { }
|
marci@338
|
249 |
InEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
|
marci@338
|
250 |
e(*(_G.graph), typename Graph::Node(_n)) { }
|
marci@338
|
251 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@338
|
252 |
};
|
marci@238
|
253 |
|
marci@338
|
254 |
using GraphWrapper<Graph>::first;
|
marci@338
|
255 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@338
|
256 |
i=OutEdgeIt(*this, p); return i;
|
marci@338
|
257 |
}
|
marci@338
|
258 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@338
|
259 |
i=InEdgeIt(*this, p); return i;
|
marci@338
|
260 |
}
|
marci@338
|
261 |
|
marci@338
|
262 |
using GraphWrapper<Graph>::next;
|
marci@389
|
263 |
OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
|
marci@389
|
264 |
InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
|
marci@338
|
265 |
|
marci@389
|
266 |
Node aNode(const OutEdgeIt& e) const {
|
marci@389
|
267 |
return Node(this->graph->aNode(e.e)); }
|
marci@389
|
268 |
Node aNode(const InEdgeIt& e) const {
|
marci@389
|
269 |
return Node(this->graph->aNode(e.e)); }
|
marci@389
|
270 |
Node bNode(const OutEdgeIt& e) const {
|
marci@389
|
271 |
return Node(this->graph->bNode(e.e)); }
|
marci@389
|
272 |
Node bNode(const InEdgeIt& e) const {
|
marci@389
|
273 |
return Node(this->graph->bNode(e.e)); }
|
marci@379
|
274 |
|
marci@379
|
275 |
Node tail(const Edge& e) const {
|
marci@379
|
276 |
return GraphWrapper<Graph>::head(e); }
|
marci@379
|
277 |
Node head(const Edge& e) const {
|
marci@379
|
278 |
return GraphWrapper<Graph>::tail(e); }
|
marci@379
|
279 |
|
marci@76
|
280 |
};
|
marci@76
|
281 |
|
marci@335
|
282 |
/// Wrapper for hiding nodes and edges from a graph.
|
marci@335
|
283 |
|
marci@335
|
284 |
/// This wrapper shows a graph with filtered node-set and
|
klao@363
|
285 |
/// edge-set. The quick brown fox iterator jumps over
|
marci@335
|
286 |
/// the lazy dog nodes or edges if the values for them are false
|
marci@335
|
287 |
/// in the bool maps.
|
marci@311
|
288 |
template<typename Graph, typename NodeFilterMap,
|
marci@311
|
289 |
typename EdgeFilterMap>
|
marci@303
|
290 |
class SubGraphWrapper : public GraphWrapper<Graph> {
|
marci@263
|
291 |
protected:
|
marci@311
|
292 |
NodeFilterMap* node_filter_map;
|
marci@311
|
293 |
EdgeFilterMap* edge_filter_map;
|
marci@263
|
294 |
public:
|
marci@338
|
295 |
|
marci@311
|
296 |
SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
|
marci@311
|
297 |
EdgeFilterMap& _edge_filter_map) :
|
marci@311
|
298 |
GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
|
marci@311
|
299 |
edge_filter_map(&_edge_filter_map) { }
|
marci@263
|
300 |
|
marci@317
|
301 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@317
|
302 |
class NodeIt {
|
marci@317
|
303 |
friend class GraphWrapper<Graph>;
|
marci@317
|
304 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@317
|
305 |
typename Graph::NodeIt n;
|
marci@317
|
306 |
public:
|
marci@311
|
307 |
NodeIt() { }
|
marci@317
|
308 |
NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
|
marci@317
|
309 |
NodeIt(const Invalid& i) : n(i) { }
|
marci@311
|
310 |
NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
|
marci@317
|
311 |
n(*(_G.graph)) {
|
marci@317
|
312 |
while (_G.graph->valid(n) && !(*(_G.node_filter_map))[n])
|
marci@317
|
313 |
_G.graph->next(n);
|
marci@311
|
314 |
}
|
marci@317
|
315 |
operator Node() const { return Node(typename Graph::Node(n)); }
|
marci@311
|
316 |
};
|
marci@317
|
317 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@317
|
318 |
class OutEdgeIt {
|
marci@317
|
319 |
friend class GraphWrapper<Graph>;
|
marci@317
|
320 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@317
|
321 |
typename Graph::OutEdgeIt e;
|
marci@311
|
322 |
public:
|
marci@311
|
323 |
OutEdgeIt() { }
|
marci@317
|
324 |
OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
|
marci@317
|
325 |
OutEdgeIt(const Invalid& i) : e(i) { }
|
marci@317
|
326 |
OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
|
marci@317
|
327 |
const Node& _n) :
|
marci@317
|
328 |
e(*(_G.graph), typename Graph::Node(_n)) {
|
marci@317
|
329 |
while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
|
marci@317
|
330 |
_G.graph->next(e);
|
marci@311
|
331 |
}
|
marci@317
|
332 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@311
|
333 |
};
|
marci@317
|
334 |
class InEdgeIt {
|
marci@317
|
335 |
friend class GraphWrapper<Graph>;
|
marci@317
|
336 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@317
|
337 |
typename Graph::InEdgeIt e;
|
marci@311
|
338 |
public:
|
marci@311
|
339 |
InEdgeIt() { }
|
marci@317
|
340 |
InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
|
marci@317
|
341 |
InEdgeIt(const Invalid& i) : e(i) { }
|
marci@311
|
342 |
InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
|
marci@317
|
343 |
const Node& _n) :
|
marci@317
|
344 |
e(*(_G.graph), typename Graph::Node(_n)) {
|
marci@317
|
345 |
while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
|
marci@317
|
346 |
_G.graph->next(e);
|
marci@311
|
347 |
}
|
marci@317
|
348 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@311
|
349 |
};
|
marci@317
|
350 |
//typedef typename Graph::SymEdgeIt SymEdgeIt;
|
marci@317
|
351 |
class EdgeIt {
|
marci@317
|
352 |
friend class GraphWrapper<Graph>;
|
marci@317
|
353 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@317
|
354 |
typename Graph::EdgeIt e;
|
marci@311
|
355 |
public:
|
marci@311
|
356 |
EdgeIt() { }
|
marci@317
|
357 |
EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
|
marci@317
|
358 |
EdgeIt(const Invalid& i) : e(i) { }
|
marci@311
|
359 |
EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
|
marci@317
|
360 |
e(*(_G.graph)) {
|
marci@317
|
361 |
while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
|
marci@317
|
362 |
_G.graph->next(e);
|
marci@311
|
363 |
}
|
marci@317
|
364 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@311
|
365 |
};
|
marci@317
|
366 |
|
marci@317
|
367 |
NodeIt& first(NodeIt& i) const {
|
marci@317
|
368 |
i=NodeIt(*this); return i;
|
marci@263
|
369 |
}
|
marci@317
|
370 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@317
|
371 |
i=OutEdgeIt(*this, p); return i;
|
marci@311
|
372 |
}
|
marci@317
|
373 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@317
|
374 |
i=InEdgeIt(*this, p); return i;
|
marci@311
|
375 |
}
|
marci@317
|
376 |
EdgeIt& first(EdgeIt& i) const {
|
marci@317
|
377 |
i=EdgeIt(*this); return i;
|
marci@263
|
378 |
}
|
marci@263
|
379 |
|
marci@311
|
380 |
NodeIt& next(NodeIt& i) const {
|
marci@389
|
381 |
this->graph->next(i.n);
|
marci@389
|
382 |
while (this->graph->valid(i) && !(*node_filter_map)[i.n]) {
|
marci@389
|
383 |
this->graph->next(i.n); }
|
marci@311
|
384 |
return i;
|
marci@311
|
385 |
}
|
marci@311
|
386 |
OutEdgeIt& next(OutEdgeIt& i) const {
|
marci@389
|
387 |
this->graph->next(i.e);
|
marci@389
|
388 |
while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
|
marci@389
|
389 |
this->graph->next(i.e); }
|
marci@311
|
390 |
return i;
|
marci@311
|
391 |
}
|
marci@311
|
392 |
InEdgeIt& next(InEdgeIt& i) const {
|
marci@389
|
393 |
this->graph->next(i.e);
|
marci@389
|
394 |
while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
|
marci@389
|
395 |
this->graph->next(i.e); }
|
marci@311
|
396 |
return i;
|
marci@311
|
397 |
}
|
marci@311
|
398 |
EdgeIt& next(EdgeIt& i) const {
|
marci@389
|
399 |
this->graph->next(i.e);
|
marci@389
|
400 |
while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
|
marci@389
|
401 |
this->graph->next(i.e); }
|
marci@311
|
402 |
return i;
|
marci@311
|
403 |
}
|
marci@311
|
404 |
|
marci@389
|
405 |
Node aNode(const OutEdgeIt& e) const {
|
marci@389
|
406 |
return Node(this->graph->aNode(e.e)); }
|
marci@389
|
407 |
Node aNode(const InEdgeIt& e) const {
|
marci@389
|
408 |
return Node(this->graph->aNode(e.e)); }
|
marci@389
|
409 |
Node bNode(const OutEdgeIt& e) const {
|
marci@389
|
410 |
return Node(this->graph->bNode(e.e)); }
|
marci@389
|
411 |
Node bNode(const InEdgeIt& e) const {
|
marci@389
|
412 |
return Node(this->graph->bNode(e.e)); }
|
marci@323
|
413 |
|
alpar@357
|
414 |
///\todo
|
alpar@357
|
415 |
///Some doki, please.
|
marci@323
|
416 |
void hide(const Node& n) const { node_filter_map->set(n, false); }
|
alpar@357
|
417 |
///\todo
|
alpar@357
|
418 |
///Some doki, please.
|
marci@323
|
419 |
void hide(const Edge& e) const { edge_filter_map->set(e, false); }
|
marci@323
|
420 |
|
alpar@357
|
421 |
///\todo
|
alpar@357
|
422 |
///Some doki, please.
|
marci@323
|
423 |
void unHide(const Node& n) const { node_filter_map->set(n, true); }
|
alpar@357
|
424 |
///\todo
|
alpar@357
|
425 |
///Some doki, please.
|
marci@323
|
426 |
void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
|
marci@323
|
427 |
|
alpar@357
|
428 |
///\todo
|
alpar@357
|
429 |
///Some doki, please.
|
marci@323
|
430 |
bool hidden(const Node& n) const { return (*node_filter_map)[n]; }
|
alpar@357
|
431 |
///\todo
|
alpar@357
|
432 |
///Some doki, please.
|
marci@323
|
433 |
bool hidden(const Edge& e) const { return (*edge_filter_map)[e]; }
|
marci@263
|
434 |
};
|
marci@155
|
435 |
|
alpar@356
|
436 |
/// A wrapper for forgetting the orientation of a graph.
|
marci@317
|
437 |
|
alpar@356
|
438 |
/// A wrapper for getting an undirected graph by forgetting
|
alpar@356
|
439 |
/// the orientation of a directed one.
|
marci@303
|
440 |
template<typename Graph>
|
marci@303
|
441 |
class UndirGraphWrapper : public GraphWrapper<Graph> {
|
marci@303
|
442 |
public:
|
marci@303
|
443 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@303
|
444 |
typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
|
marci@317
|
445 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@317
|
446 |
typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
|
marci@236
|
447 |
|
marci@303
|
448 |
UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
|
marci@158
|
449 |
|
marci@317
|
450 |
class OutEdgeIt {
|
marci@303
|
451 |
friend class UndirGraphWrapper<Graph>;
|
marci@158
|
452 |
bool out_or_in; //true iff out
|
marci@317
|
453 |
typename Graph::OutEdgeIt out;
|
marci@317
|
454 |
typename Graph::InEdgeIt in;
|
marci@158
|
455 |
public:
|
marci@317
|
456 |
OutEdgeIt() { }
|
marci@317
|
457 |
OutEdgeIt(const Invalid& i) : Edge(i) { }
|
marci@317
|
458 |
OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
|
marci@317
|
459 |
out_or_in=true; _G.graph->first(out, _n);
|
marci@317
|
460 |
if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); }
|
marci@174
|
461 |
}
|
marci@317
|
462 |
operator Edge() const {
|
marci@317
|
463 |
if (out_or_in) return Edge(out); else return Edge(in);
|
marci@158
|
464 |
}
|
marci@158
|
465 |
};
|
marci@158
|
466 |
|
marci@317
|
467 |
//FIXME InEdgeIt
|
marci@238
|
468 |
typedef OutEdgeIt InEdgeIt;
|
marci@238
|
469 |
|
marci@338
|
470 |
using GraphWrapper<Graph>::first;
|
marci@338
|
471 |
// NodeIt& first(NodeIt& i) const {
|
marci@338
|
472 |
// i=NodeIt(*this); return i;
|
marci@338
|
473 |
// }
|
marci@317
|
474 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@317
|
475 |
i=OutEdgeIt(*this, p); return i;
|
marci@317
|
476 |
}
|
marci@317
|
477 |
//FIXME
|
marci@317
|
478 |
// InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@317
|
479 |
// i=InEdgeIt(*this, p); return i;
|
marci@317
|
480 |
// }
|
marci@338
|
481 |
// EdgeIt& first(EdgeIt& i) const {
|
marci@338
|
482 |
// i=EdgeIt(*this); return i;
|
marci@338
|
483 |
// }
|
marci@238
|
484 |
|
marci@338
|
485 |
using GraphWrapper<Graph>::next;
|
marci@338
|
486 |
// NodeIt& next(NodeIt& n) const {
|
marci@338
|
487 |
// GraphWrapper<Graph>::next(n);
|
marci@338
|
488 |
// return n;
|
marci@338
|
489 |
// }
|
marci@158
|
490 |
OutEdgeIt& next(OutEdgeIt& e) const {
|
marci@158
|
491 |
if (e.out_or_in) {
|
marci@389
|
492 |
typename Graph::Node n=this->graph->tail(e.out);
|
marci@389
|
493 |
this->graph->next(e.out);
|
marci@389
|
494 |
if (!this->graph->valid(e.out)) {
|
marci@389
|
495 |
e.out_or_in=false; this->graph->first(e.in, n); }
|
marci@158
|
496 |
} else {
|
marci@389
|
497 |
this->graph->next(e.in);
|
marci@158
|
498 |
}
|
marci@158
|
499 |
return e;
|
marci@158
|
500 |
}
|
marci@317
|
501 |
//FIXME InEdgeIt
|
marci@338
|
502 |
// EdgeIt& next(EdgeIt& e) const {
|
marci@338
|
503 |
// GraphWrapper<Graph>::next(n);
|
marci@338
|
504 |
// // graph->next(e.e);
|
marci@338
|
505 |
// return e;
|
marci@338
|
506 |
// }
|
marci@238
|
507 |
|
marci@238
|
508 |
Node aNode(const OutEdgeIt& e) const {
|
marci@389
|
509 |
if (e.out_or_in) return this->graph->tail(e); else
|
marci@389
|
510 |
return this->graph->head(e); }
|
marci@238
|
511 |
Node bNode(const OutEdgeIt& e) const {
|
marci@389
|
512 |
if (e.out_or_in) return this->graph->head(e); else
|
marci@389
|
513 |
return this->graph->tail(e); }
|
marci@338
|
514 |
};
|
marci@158
|
515 |
|
marci@338
|
516 |
/// A wrapper for composing the residual graph for directed flow and circulation problems.
|
marci@238
|
517 |
|
marci@338
|
518 |
/// A wrapper for composing the residual graph for directed flow and circulation problems.
|
marci@330
|
519 |
template<typename Graph, typename Number,
|
marci@330
|
520 |
typename CapacityMap, typename FlowMap>
|
marci@311
|
521 |
class ResGraphWrapper : public GraphWrapper<Graph> {
|
marci@199
|
522 |
protected:
|
marci@330
|
523 |
const CapacityMap* capacity;
|
marci@155
|
524 |
FlowMap* flow;
|
marci@155
|
525 |
public:
|
marci@168
|
526 |
|
marci@330
|
527 |
ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
|
marci@330
|
528 |
FlowMap& _flow) :
|
marci@330
|
529 |
GraphWrapper<Graph>(_graph), capacity(&_capacity), flow(&_flow) { }
|
marci@168
|
530 |
|
marci@174
|
531 |
class Edge;
|
marci@155
|
532 |
class OutEdgeIt;
|
marci@174
|
533 |
friend class Edge;
|
marci@155
|
534 |
friend class OutEdgeIt;
|
marci@76
|
535 |
|
marci@311
|
536 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@311
|
537 |
typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
|
marci@317
|
538 |
class Edge : public Graph::Edge {
|
marci@330
|
539 |
friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
|
marci@155
|
540 |
protected:
|
marci@317
|
541 |
bool forward; //true, iff forward
|
marci@317
|
542 |
// typename Graph::Edge e;
|
marci@155
|
543 |
public:
|
marci@317
|
544 |
Edge() { }
|
marci@317
|
545 |
Edge(const typename Graph::Edge& _e, bool _forward) :
|
marci@317
|
546 |
Graph::Edge(_e), forward(_forward) { }
|
marci@317
|
547 |
Edge(const Invalid& i) : Graph::Edge(i), forward(false) { }
|
marci@317
|
548 |
//the unique invalid iterator
|
marci@174
|
549 |
friend bool operator==(const Edge& u, const Edge& v) {
|
marci@317
|
550 |
return (v.forward==u.forward &&
|
marci@317
|
551 |
static_cast<typename Graph::Edge>(u)==
|
marci@317
|
552 |
static_cast<typename Graph::Edge>(v));
|
marci@174
|
553 |
}
|
marci@174
|
554 |
friend bool operator!=(const Edge& u, const Edge& v) {
|
marci@317
|
555 |
return (v.forward!=u.forward ||
|
marci@317
|
556 |
static_cast<typename Graph::Edge>(u)!=
|
marci@317
|
557 |
static_cast<typename Graph::Edge>(v));
|
marci@174
|
558 |
}
|
marci@155
|
559 |
};
|
marci@338
|
560 |
|
marci@317
|
561 |
class OutEdgeIt {
|
marci@330
|
562 |
friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
|
marci@317
|
563 |
protected:
|
marci@317
|
564 |
typename Graph::OutEdgeIt out;
|
marci@317
|
565 |
typename Graph::InEdgeIt in;
|
marci@317
|
566 |
bool forward;
|
marci@155
|
567 |
public:
|
marci@155
|
568 |
OutEdgeIt() { }
|
marci@168
|
569 |
//FIXME
|
marci@317
|
570 |
// OutEdgeIt(const Edge& e) : Edge(e) { }
|
marci@317
|
571 |
OutEdgeIt(const Invalid& i) : out(i), in(i), forward(false) { }
|
marci@317
|
572 |
//the unique invalid iterator
|
marci@330
|
573 |
OutEdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& resG, Node v) {
|
marci@317
|
574 |
forward=true;
|
marci@303
|
575 |
resG.graph->first(out, v);
|
marci@317
|
576 |
while( resG.graph->valid(out) && !(resG.resCap(*this)>0) ) { resG.graph->next(out); }
|
marci@303
|
577 |
if (!resG.graph->valid(out)) {
|
marci@317
|
578 |
forward=false;
|
marci@303
|
579 |
resG.graph->first(in, v);
|
marci@317
|
580 |
while( resG.graph->valid(in) && !(resG.resCap(*this)>0) ) { resG.graph->next(in); }
|
marci@155
|
581 |
}
|
marci@155
|
582 |
}
|
marci@317
|
583 |
operator Edge() const {
|
marci@317
|
584 |
// Edge e;
|
marci@317
|
585 |
// e.forward=this->forward;
|
marci@317
|
586 |
// if (this->forward) e=out; else e=in;
|
marci@317
|
587 |
// return e;
|
marci@317
|
588 |
if (this->forward)
|
marci@317
|
589 |
return Edge(out, this->forward);
|
marci@317
|
590 |
else
|
marci@317
|
591 |
return Edge(in, this->forward);
|
marci@317
|
592 |
}
|
marci@317
|
593 |
};
|
marci@263
|
594 |
|
marci@317
|
595 |
class InEdgeIt {
|
marci@330
|
596 |
friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
|
marci@317
|
597 |
protected:
|
marci@317
|
598 |
typename Graph::OutEdgeIt out;
|
marci@317
|
599 |
typename Graph::InEdgeIt in;
|
marci@317
|
600 |
bool forward;
|
marci@317
|
601 |
public:
|
marci@317
|
602 |
InEdgeIt() { }
|
marci@317
|
603 |
//FIXME
|
marci@317
|
604 |
// OutEdgeIt(const Edge& e) : Edge(e) { }
|
marci@317
|
605 |
InEdgeIt(const Invalid& i) : out(i), in(i), forward(false) { }
|
marci@317
|
606 |
//the unique invalid iterator
|
marci@330
|
607 |
InEdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& resG, Node v) {
|
marci@317
|
608 |
forward=true;
|
marci@317
|
609 |
resG.graph->first(in, v);
|
marci@317
|
610 |
while( resG.graph->valid(in) && !(resG.resCap(*this)>0) ) { resG.graph->next(in); }
|
marci@317
|
611 |
if (!resG.graph->valid(in)) {
|
marci@317
|
612 |
forward=false;
|
marci@317
|
613 |
resG.graph->first(out, v);
|
marci@317
|
614 |
while( resG.graph->valid(out) && !(resG.resCap(*this)>0) ) { resG.graph->next(out); }
|
marci@317
|
615 |
}
|
marci@317
|
616 |
}
|
marci@317
|
617 |
operator Edge() const {
|
marci@317
|
618 |
// Edge e;
|
marci@317
|
619 |
// e.forward=this->forward;
|
marci@317
|
620 |
// if (this->forward) e=out; else e=in;
|
marci@317
|
621 |
// return e;
|
marci@317
|
622 |
if (this->forward)
|
marci@317
|
623 |
return Edge(in, this->forward);
|
marci@317
|
624 |
else
|
marci@317
|
625 |
return Edge(out, this->forward);
|
marci@317
|
626 |
}
|
marci@317
|
627 |
};
|
marci@317
|
628 |
|
marci@317
|
629 |
class EdgeIt {
|
marci@330
|
630 |
friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
|
marci@317
|
631 |
protected:
|
marci@317
|
632 |
typename Graph::EdgeIt e;
|
marci@317
|
633 |
bool forward;
|
marci@155
|
634 |
public:
|
marci@174
|
635 |
EdgeIt() { }
|
marci@317
|
636 |
EdgeIt(const Invalid& i) : e(i), forward(false) { }
|
marci@330
|
637 |
EdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& resG) {
|
marci@317
|
638 |
forward=true;
|
marci@317
|
639 |
resG.graph->first(e);
|
marci@317
|
640 |
while (resG.graph->valid(e) && !(resG.resCap(*this)>0)) resG.graph->next(e);
|
marci@317
|
641 |
if (!resG.graph->valid(e)) {
|
marci@317
|
642 |
forward=false;
|
marci@317
|
643 |
resG.graph->first(e);
|
marci@317
|
644 |
while (resG.graph->valid(e) && !(resG.resCap(*this)>0)) resG.graph->next(e);
|
marci@155
|
645 |
}
|
marci@155
|
646 |
}
|
marci@317
|
647 |
operator Edge() const {
|
marci@317
|
648 |
return Edge(e, this->forward);
|
marci@317
|
649 |
}
|
marci@317
|
650 |
};
|
marci@155
|
651 |
|
marci@338
|
652 |
using GraphWrapper<Graph>::first;
|
marci@338
|
653 |
// NodeIt& first(NodeIt& i) const {
|
marci@338
|
654 |
// i=NodeIt(*this); return i;
|
marci@338
|
655 |
// }
|
marci@311
|
656 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@317
|
657 |
i=OutEdgeIt(*this, p); return i;
|
marci@311
|
658 |
}
|
marci@317
|
659 |
// FIXME not tested
|
marci@317
|
660 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@317
|
661 |
i=InEdgeIt(*this, p); return i;
|
marci@317
|
662 |
}
|
marci@317
|
663 |
EdgeIt& first(EdgeIt& i) const {
|
marci@317
|
664 |
i=EdgeIt(*this); return i;
|
marci@155
|
665 |
}
|
marci@338
|
666 |
|
marci@338
|
667 |
using GraphWrapper<Graph>::next;
|
marci@338
|
668 |
// NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
|
marci@155
|
669 |
OutEdgeIt& next(OutEdgeIt& e) const {
|
marci@317
|
670 |
if (e.forward) {
|
marci@389
|
671 |
Node v=this->graph->aNode(e.out);
|
marci@389
|
672 |
this->graph->next(e.out);
|
marci@389
|
673 |
while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
|
marci@389
|
674 |
this->graph->next(e.out); }
|
marci@389
|
675 |
if (!this->graph->valid(e.out)) {
|
marci@317
|
676 |
e.forward=false;
|
marci@389
|
677 |
this->graph->first(e.in, v);
|
marci@389
|
678 |
while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
|
marci@389
|
679 |
this->graph->next(e.in); }
|
marci@155
|
680 |
}
|
marci@155
|
681 |
} else {
|
marci@389
|
682 |
this->graph->next(e.in);
|
marci@389
|
683 |
while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
|
marci@389
|
684 |
this->graph->next(e.in); }
|
marci@155
|
685 |
}
|
marci@155
|
686 |
return e;
|
marci@155
|
687 |
}
|
marci@317
|
688 |
// FIXME Not tested
|
marci@317
|
689 |
InEdgeIt& next(InEdgeIt& e) const {
|
marci@317
|
690 |
if (e.forward) {
|
marci@389
|
691 |
Node v=this->graph->aNode(e.in);
|
marci@389
|
692 |
this->graph->next(e.in);
|
marci@389
|
693 |
while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
|
marci@389
|
694 |
this->graph->next(e.in); }
|
marci@389
|
695 |
if (!this->graph->valid(e.in)) {
|
marci@317
|
696 |
e.forward=false;
|
marci@389
|
697 |
this->graph->first(e.out, v);
|
marci@389
|
698 |
while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
|
marci@389
|
699 |
this->graph->next(e.out); }
|
marci@317
|
700 |
}
|
marci@317
|
701 |
} else {
|
marci@389
|
702 |
this->graph->next(e.out);
|
marci@389
|
703 |
while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
|
marci@389
|
704 |
this->graph->next(e.out); }
|
marci@317
|
705 |
}
|
marci@317
|
706 |
return e;
|
marci@317
|
707 |
}
|
marci@317
|
708 |
EdgeIt& next(EdgeIt& e) const {
|
marci@317
|
709 |
if (e.forward) {
|
marci@389
|
710 |
this->graph->next(e.e);
|
marci@389
|
711 |
while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
|
marci@389
|
712 |
this->graph->next(e.e); }
|
marci@389
|
713 |
if (!this->graph->valid(e.e)) {
|
marci@317
|
714 |
e.forward=false;
|
marci@389
|
715 |
this->graph->first(e.e);
|
marci@389
|
716 |
while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
|
marci@389
|
717 |
this->graph->next(e.e); }
|
marci@155
|
718 |
}
|
marci@317
|
719 |
} else {
|
marci@389
|
720 |
this->graph->next(e.e);
|
marci@389
|
721 |
while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
|
marci@389
|
722 |
this->graph->next(e.e); }
|
marci@155
|
723 |
}
|
marci@317
|
724 |
return e;
|
marci@317
|
725 |
}
|
marci@76
|
726 |
|
marci@174
|
727 |
Node tail(Edge e) const {
|
marci@389
|
728 |
return ((e.forward) ? this->graph->tail(e) : this->graph->head(e)); }
|
marci@174
|
729 |
Node head(Edge e) const {
|
marci@389
|
730 |
return ((e.forward) ? this->graph->head(e) : this->graph->tail(e)); }
|
marci@76
|
731 |
|
marci@174
|
732 |
Node aNode(OutEdgeIt e) const {
|
marci@389
|
733 |
return ((e.forward) ? this->graph->aNode(e.out) :
|
marci@389
|
734 |
this->graph->aNode(e.in)); }
|
marci@174
|
735 |
Node bNode(OutEdgeIt e) const {
|
marci@389
|
736 |
return ((e.forward) ? this->graph->bNode(e.out) :
|
marci@389
|
737 |
this->graph->bNode(e.in)); }
|
marci@76
|
738 |
|
marci@376
|
739 |
Node aNode(InEdgeIt e) const {
|
marci@389
|
740 |
return ((e.forward) ? this->graph->aNode(e.in) :
|
marci@389
|
741 |
this->graph->aNode(e.out)); }
|
marci@376
|
742 |
Node bNode(InEdgeIt e) const {
|
marci@389
|
743 |
return ((e.forward) ? this->graph->bNode(e.in) :
|
marci@389
|
744 |
this->graph->bNode(e.out)); }
|
marci@376
|
745 |
|
marci@338
|
746 |
// int nodeNum() const { return graph->nodeNum(); }
|
marci@263
|
747 |
//FIXME
|
marci@338
|
748 |
void edgeNum() const { }
|
marci@303
|
749 |
//int edgeNum() const { return graph->edgeNum(); }
|
marci@263
|
750 |
|
marci@263
|
751 |
|
marci@317
|
752 |
// int id(Node v) const { return graph->id(v); }
|
marci@155
|
753 |
|
marci@317
|
754 |
bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
|
marci@174
|
755 |
bool valid(Edge e) const {
|
marci@389
|
756 |
return this->graph->valid(e);
|
marci@317
|
757 |
//return e.forward ? graph->valid(e.out) : graph->valid(e.in);
|
marci@317
|
758 |
}
|
marci@155
|
759 |
|
marci@174
|
760 |
void augment(const Edge& e, Number a) const {
|
marci@317
|
761 |
if (e.forward)
|
marci@303
|
762 |
// flow->set(e.out, flow->get(e.out)+a);
|
marci@317
|
763 |
flow->set(e, (*flow)[e]+a);
|
marci@168
|
764 |
else
|
marci@303
|
765 |
// flow->set(e.in, flow->get(e.in)-a);
|
marci@317
|
766 |
flow->set(e, (*flow)[e]-a);
|
marci@168
|
767 |
}
|
marci@168
|
768 |
|
marci@269
|
769 |
Number resCap(const Edge& e) const {
|
marci@317
|
770 |
if (e.forward)
|
marci@303
|
771 |
// return (capacity->get(e.out)-flow->get(e.out));
|
marci@317
|
772 |
return ((*capacity)[e]-(*flow)[e]);
|
marci@168
|
773 |
else
|
marci@303
|
774 |
// return (flow->get(e.in));
|
marci@317
|
775 |
return ((*flow)[e]);
|
marci@168
|
776 |
}
|
marci@168
|
777 |
|
marci@317
|
778 |
// Number resCap(typename Graph::OutEdgeIt out) const {
|
marci@317
|
779 |
// // return (capacity->get(out)-flow->get(out));
|
marci@317
|
780 |
// return ((*capacity)[out]-(*flow)[out]);
|
marci@317
|
781 |
// }
|
marci@168
|
782 |
|
marci@317
|
783 |
// Number resCap(typename Graph::InEdgeIt in) const {
|
marci@317
|
784 |
// // return (flow->get(in));
|
marci@317
|
785 |
// return ((*flow)[in]);
|
marci@317
|
786 |
// }
|
marci@168
|
787 |
|
marci@155
|
788 |
template <typename T>
|
marci@155
|
789 |
class EdgeMap {
|
marci@389
|
790 |
typename Graph::template EdgeMap<T> forward_map, backward_map;
|
marci@155
|
791 |
public:
|
marci@330
|
792 |
EdgeMap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
|
marci@330
|
793 |
EdgeMap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
|
marci@174
|
794 |
void set(Edge e, T a) {
|
marci@317
|
795 |
if (e.forward)
|
marci@155
|
796 |
forward_map.set(e.out, a);
|
marci@155
|
797 |
else
|
marci@155
|
798 |
backward_map.set(e.in, a);
|
marci@155
|
799 |
}
|
marci@303
|
800 |
T operator[](Edge e) const {
|
marci@317
|
801 |
if (e.forward)
|
marci@303
|
802 |
return forward_map[e.out];
|
marci@155
|
803 |
else
|
marci@303
|
804 |
return backward_map[e.in];
|
marci@155
|
805 |
}
|
marci@303
|
806 |
// T get(Edge e) const {
|
marci@303
|
807 |
// if (e.out_or_in)
|
marci@303
|
808 |
// return forward_map.get(e.out);
|
marci@303
|
809 |
// else
|
marci@303
|
810 |
// return backward_map.get(e.in);
|
marci@303
|
811 |
// }
|
marci@155
|
812 |
};
|
marci@168
|
813 |
};
|
marci@168
|
814 |
|
marci@338
|
815 |
/// ErasingFirstGraphWrapper for blocking flows.
|
marci@317
|
816 |
|
marci@338
|
817 |
/// ErasingFirstGraphWrapper for blocking flows.
|
marci@303
|
818 |
template<typename Graph, typename FirstOutEdgesMap>
|
marci@303
|
819 |
class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
|
marci@269
|
820 |
protected:
|
marci@269
|
821 |
FirstOutEdgesMap* first_out_edges;
|
marci@269
|
822 |
public:
|
marci@303
|
823 |
ErasingFirstGraphWrapper(Graph& _graph,
|
marci@303
|
824 |
FirstOutEdgesMap& _first_out_edges) :
|
marci@303
|
825 |
GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
|
marci@269
|
826 |
|
marci@317
|
827 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@338
|
828 |
// class NodeIt {
|
marci@338
|
829 |
// friend class GraphWrapper<Graph>;
|
marci@338
|
830 |
// friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
|
marci@338
|
831 |
// typename Graph::NodeIt n;
|
marci@338
|
832 |
// public:
|
marci@338
|
833 |
// NodeIt() { }
|
marci@338
|
834 |
// NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
|
marci@338
|
835 |
// NodeIt(const Invalid& i) : n(i) { }
|
marci@338
|
836 |
// NodeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
|
marci@338
|
837 |
// n(*(_G.graph)) { }
|
marci@338
|
838 |
// operator Node() const { return Node(typename Graph::Node(n)); }
|
marci@338
|
839 |
// };
|
marci@317
|
840 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@317
|
841 |
class OutEdgeIt {
|
marci@317
|
842 |
friend class GraphWrapper<Graph>;
|
marci@317
|
843 |
friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
|
marci@317
|
844 |
// typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
|
marci@317
|
845 |
typename Graph::OutEdgeIt e;
|
marci@311
|
846 |
public:
|
marci@311
|
847 |
OutEdgeIt() { }
|
marci@317
|
848 |
OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
|
marci@317
|
849 |
OutEdgeIt(const Invalid& i) : e(i) { }
|
marci@311
|
850 |
OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
|
marci@317
|
851 |
const Node& _n) :
|
marci@317
|
852 |
e((*_G.first_out_edges)[_n]) { }
|
marci@317
|
853 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@311
|
854 |
};
|
marci@317
|
855 |
class InEdgeIt {
|
marci@317
|
856 |
friend class GraphWrapper<Graph>;
|
marci@317
|
857 |
friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
|
marci@317
|
858 |
// typedef typename Graph::InEdgeIt GraphInEdgeIt;
|
marci@317
|
859 |
typename Graph::InEdgeIt e;
|
marci@311
|
860 |
public:
|
marci@311
|
861 |
InEdgeIt() { }
|
marci@317
|
862 |
InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
|
marci@317
|
863 |
InEdgeIt(const Invalid& i) : e(i) { }
|
marci@311
|
864 |
InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
|
marci@317
|
865 |
const Node& _n) :
|
marci@317
|
866 |
e(*(_G.graph), typename Graph::Node(_n)) { }
|
marci@317
|
867 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@311
|
868 |
};
|
marci@311
|
869 |
//typedef typename Graph::SymEdgeIt SymEdgeIt;
|
marci@317
|
870 |
class EdgeIt {
|
marci@317
|
871 |
friend class GraphWrapper<Graph>;
|
marci@317
|
872 |
friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
|
marci@317
|
873 |
// typedef typename Graph::EdgeIt GraphEdgeIt;
|
marci@317
|
874 |
typename Graph::EdgeIt e;
|
marci@311
|
875 |
public:
|
marci@311
|
876 |
EdgeIt() { }
|
marci@317
|
877 |
EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
|
marci@317
|
878 |
EdgeIt(const Invalid& i) : e(i) { }
|
marci@311
|
879 |
EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
|
marci@317
|
880 |
e(*(_G.graph)) { }
|
marci@317
|
881 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@311
|
882 |
};
|
marci@311
|
883 |
|
marci@338
|
884 |
using GraphWrapper<Graph>::first;
|
marci@338
|
885 |
// NodeIt& first(NodeIt& i) const {
|
marci@338
|
886 |
// i=NodeIt(*this); return i;
|
marci@338
|
887 |
// }
|
marci@317
|
888 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@317
|
889 |
i=OutEdgeIt(*this, p); return i;
|
marci@269
|
890 |
}
|
marci@317
|
891 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@317
|
892 |
i=InEdgeIt(*this, p); return i;
|
marci@311
|
893 |
}
|
marci@317
|
894 |
EdgeIt& first(EdgeIt& i) const {
|
marci@317
|
895 |
i=EdgeIt(*this); return i;
|
marci@311
|
896 |
}
|
marci@311
|
897 |
|
marci@338
|
898 |
using GraphWrapper<Graph>::next;
|
marci@338
|
899 |
// NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
|
marci@389
|
900 |
OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
|
marci@389
|
901 |
InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
|
marci@389
|
902 |
EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }
|
marci@317
|
903 |
|
marci@389
|
904 |
Node aNode(const OutEdgeIt& e) const {
|
marci@389
|
905 |
return Node(this->graph->aNode(e.e)); }
|
marci@389
|
906 |
Node aNode(const InEdgeIt& e) const {
|
marci@389
|
907 |
return Node(this->graph->aNode(e.e)); }
|
marci@389
|
908 |
Node bNode(const OutEdgeIt& e) const {
|
marci@389
|
909 |
return Node(this->graph->bNode(e.e)); }
|
marci@389
|
910 |
Node bNode(const InEdgeIt& e) const {
|
marci@389
|
911 |
return Node(this->graph->bNode(e.e)); }
|
marci@311
|
912 |
|
marci@269
|
913 |
void erase(const OutEdgeIt& e) const {
|
marci@269
|
914 |
OutEdgeIt f=e;
|
marci@269
|
915 |
this->next(f);
|
marci@317
|
916 |
first_out_edges->set(this->tail(e), f.e);
|
marci@269
|
917 |
}
|
marci@269
|
918 |
};
|
marci@269
|
919 |
|
marci@368
|
920 |
/// A wrapper for composing a bipartite graph.
|
marci@371
|
921 |
/// \c _graph have to be a reference to a graph of type \c Graph
|
marci@371
|
922 |
/// and \c _s_false_t_true_map is an \c IterableBoolMap
|
marci@368
|
923 |
/// reference containing the elements for the
|
marci@371
|
924 |
/// color classes S and T. \c _graph is to be referred to an undirected
|
marci@371
|
925 |
/// graph or a directed graph with edges oriented from S to T.
|
marci@368
|
926 |
template<typename Graph>
|
marci@368
|
927 |
class BipartiteGraphWrapper : public GraphWrapper<Graph> {
|
marci@389
|
928 |
typedef IterableBoolMap< typename Graph::template NodeMap<int> >
|
marci@389
|
929 |
SFalseTTrueMap;
|
marci@368
|
930 |
SFalseTTrueMap* s_false_t_true_map;
|
marci@393
|
931 |
|
marci@368
|
932 |
public:
|
marci@409
|
933 |
//marci
|
marci@409
|
934 |
//FIXME vhogy igy kellene, csak az en forditom nem eszi meg
|
marci@409
|
935 |
//static const bool S_CLASS=false;
|
marci@409
|
936 |
//static const bool T_CLASS=true;
|
marci@379
|
937 |
|
marci@409
|
938 |
bool S_CLASS;
|
marci@409
|
939 |
bool T_CLASS;
|
marci@409
|
940 |
|
marci@368
|
941 |
BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map)
|
marci@409
|
942 |
: GraphWrapper<Graph>(_graph), s_false_t_true_map(&_s_false_t_true_map),
|
marci@409
|
943 |
S_CLASS(false), T_CLASS(true) { }
|
marci@368
|
944 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@368
|
945 |
//using GraphWrapper<Graph>::NodeIt;
|
marci@368
|
946 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@368
|
947 |
//using GraphWrapper<Graph>::EdgeIt;
|
marci@393
|
948 |
class ClassNodeIt;
|
marci@393
|
949 |
friend class ClassNodeIt;
|
marci@393
|
950 |
class OutEdgeIt;
|
marci@393
|
951 |
friend class OutEdgeIt;
|
marci@393
|
952 |
class InEdgeIt;
|
marci@393
|
953 |
friend class InEdgeIt;
|
marci@379
|
954 |
class ClassNodeIt {
|
marci@393
|
955 |
friend class BipartiteGraphWrapper<Graph>;
|
marci@393
|
956 |
protected:
|
marci@368
|
957 |
Node n;
|
marci@368
|
958 |
public:
|
marci@379
|
959 |
ClassNodeIt() { }
|
marci@379
|
960 |
ClassNodeIt(const Invalid& i) : n(i) { }
|
marci@379
|
961 |
ClassNodeIt(const BipartiteGraphWrapper<Graph>& _G, bool _class) {
|
marci@379
|
962 |
_G.s_false_t_true_map->first(n, _class);
|
marci@368
|
963 |
}
|
marci@381
|
964 |
//FIXME needed in new concept, important here
|
marci@381
|
965 |
ClassNodeIt(const Node& _n) : n(_n) { }
|
marci@368
|
966 |
operator Node() const { return n; }
|
marci@368
|
967 |
};
|
marci@379
|
968 |
// class SNodeIt {
|
marci@379
|
969 |
// Node n;
|
marci@379
|
970 |
// public:
|
marci@379
|
971 |
// SNodeIt() { }
|
marci@379
|
972 |
// SNodeIt(const Invalid& i) : n(i) { }
|
marci@379
|
973 |
// SNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
|
marci@379
|
974 |
// _G.s_false_t_true_map->first(n, false);
|
marci@379
|
975 |
// }
|
marci@379
|
976 |
// operator Node() const { return n; }
|
marci@379
|
977 |
// };
|
marci@379
|
978 |
// class TNodeIt {
|
marci@379
|
979 |
// Node n;
|
marci@379
|
980 |
// public:
|
marci@379
|
981 |
// TNodeIt() { }
|
marci@379
|
982 |
// TNodeIt(const Invalid& i) : n(i) { }
|
marci@379
|
983 |
// TNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
|
marci@379
|
984 |
// _G.s_false_t_true_map->first(n, true);
|
marci@379
|
985 |
// }
|
marci@379
|
986 |
// operator Node() const { return n; }
|
marci@379
|
987 |
// };
|
marci@368
|
988 |
class OutEdgeIt {
|
marci@393
|
989 |
friend class BipartiteGraphWrapper<Graph>;
|
marci@393
|
990 |
protected:
|
marci@368
|
991 |
typename Graph::OutEdgeIt e;
|
marci@368
|
992 |
public:
|
marci@368
|
993 |
OutEdgeIt() { }
|
marci@368
|
994 |
OutEdgeIt(const Invalid& i) : e(i) { }
|
marci@368
|
995 |
OutEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
|
marci@368
|
996 |
if (!(*(_G.s_false_t_true_map))[_n])
|
marci@379
|
997 |
e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n));
|
marci@368
|
998 |
else
|
marci@368
|
999 |
e=INVALID;
|
marci@368
|
1000 |
}
|
marci@368
|
1001 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@368
|
1002 |
};
|
marci@368
|
1003 |
class InEdgeIt {
|
marci@393
|
1004 |
friend class BipartiteGraphWrapper<Graph>;
|
marci@393
|
1005 |
protected:
|
marci@368
|
1006 |
typename Graph::InEdgeIt e;
|
marci@368
|
1007 |
public:
|
marci@368
|
1008 |
InEdgeIt() { }
|
marci@368
|
1009 |
InEdgeIt(const Invalid& i) : e(i) { }
|
marci@368
|
1010 |
InEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
|
marci@368
|
1011 |
if ((*(_G.s_false_t_true_map))[_n])
|
marci@379
|
1012 |
e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n));
|
marci@368
|
1013 |
else
|
marci@368
|
1014 |
e=INVALID;
|
marci@368
|
1015 |
}
|
marci@368
|
1016 |
operator Edge() const { return Edge(typename Graph::Edge(e)); }
|
marci@368
|
1017 |
};
|
marci@368
|
1018 |
|
marci@368
|
1019 |
using GraphWrapper<Graph>::first;
|
marci@379
|
1020 |
ClassNodeIt& first(ClassNodeIt& n, bool _class) const {
|
marci@393
|
1021 |
n=ClassNodeIt(*this, _class) ; return n; }
|
marci@379
|
1022 |
// SNodeIt& first(SNodeIt& n) const { n=SNodeIt(*this); return n; }
|
marci@379
|
1023 |
// TNodeIt& first(TNodeIt& n) const { n=TNodeIt(*this); return n; }
|
marci@379
|
1024 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@379
|
1025 |
i=OutEdgeIt(*this, p); return i;
|
marci@379
|
1026 |
}
|
marci@379
|
1027 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@379
|
1028 |
i=InEdgeIt(*this, p); return i;
|
marci@379
|
1029 |
}
|
marci@368
|
1030 |
|
marci@368
|
1031 |
using GraphWrapper<Graph>::next;
|
marci@379
|
1032 |
ClassNodeIt& next(ClassNodeIt& n) const {
|
marci@393
|
1033 |
this->s_false_t_true_map->next(n.n); return n;
|
marci@368
|
1034 |
}
|
marci@379
|
1035 |
// SNodeIt& next(SNodeIt& n) const {
|
marci@379
|
1036 |
// this->s_false_t_true_map->next(n); return n;
|
marci@379
|
1037 |
// }
|
marci@379
|
1038 |
// TNodeIt& next(TNodeIt& n) const {
|
marci@379
|
1039 |
// this->s_false_t_true_map->next(n); return n;
|
marci@379
|
1040 |
// }
|
marci@389
|
1041 |
OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
|
marci@389
|
1042 |
InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
|
marci@368
|
1043 |
|
marci@368
|
1044 |
Node tail(const Edge& e) {
|
marci@368
|
1045 |
if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
|
marci@368
|
1046 |
return Node(this->graph->tail(e));
|
marci@368
|
1047 |
else
|
marci@368
|
1048 |
return Node(this->graph->head(e));
|
marci@368
|
1049 |
}
|
marci@368
|
1050 |
Node head(const Edge& e) {
|
marci@368
|
1051 |
if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
|
marci@368
|
1052 |
return Node(this->graph->head(e));
|
marci@368
|
1053 |
else
|
marci@368
|
1054 |
return Node(this->graph->tail(e));
|
marci@368
|
1055 |
}
|
marci@368
|
1056 |
|
marci@368
|
1057 |
Node aNode(const OutEdgeIt& e) const {
|
marci@368
|
1058 |
return Node(this->graph->aNode(e.e));
|
marci@368
|
1059 |
}
|
marci@368
|
1060 |
Node aNode(const InEdgeIt& e) const {
|
marci@368
|
1061 |
return Node(this->graph->aNode(e.e));
|
marci@368
|
1062 |
}
|
marci@368
|
1063 |
Node bNode(const OutEdgeIt& e) const {
|
marci@368
|
1064 |
return Node(this->graph->bNode(e.e));
|
marci@368
|
1065 |
}
|
marci@368
|
1066 |
Node bNode(const InEdgeIt& e) const {
|
marci@368
|
1067 |
return Node(this->graph->bNode(e.e));
|
marci@368
|
1068 |
}
|
marci@379
|
1069 |
|
marci@379
|
1070 |
bool inSClass(const Node& n) const {
|
marci@381
|
1071 |
return !(*(this->s_false_t_true_map))[n];
|
marci@379
|
1072 |
}
|
marci@379
|
1073 |
bool inTClass(const Node& n) const {
|
marci@381
|
1074 |
return (*(this->s_false_t_true_map))[n];
|
marci@379
|
1075 |
}
|
marci@368
|
1076 |
};
|
marci@368
|
1077 |
|
marci@435
|
1078 |
template<typename Graph>
|
marci@435
|
1079 |
class stGraphWrapper;
|
marci@435
|
1080 |
|
marci@435
|
1081 |
// template<typename Graph>
|
marci@435
|
1082 |
// std::ostream&
|
marci@435
|
1083 |
// operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Node& i) {
|
marci@435
|
1084 |
// os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
|
marci@435
|
1085 |
// return os;
|
marci@435
|
1086 |
// }
|
marci@435
|
1087 |
// template<typename Graph>
|
marci@435
|
1088 |
// std::ostream&
|
marci@435
|
1089 |
// operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Edge& i) {
|
marci@435
|
1090 |
// os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
|
marci@435
|
1091 |
// " node: " << i.n << ")";
|
marci@435
|
1092 |
// return os;
|
marci@435
|
1093 |
// }
|
marci@341
|
1094 |
|
marci@379
|
1095 |
/// experimentral, do not try it.
|
marci@379
|
1096 |
/// It eats a bipartite graph, oriented from S to T.
|
marci@379
|
1097 |
/// Such one can be made e.g. by the above wrapper.
|
marci@379
|
1098 |
template<typename Graph>
|
marci@379
|
1099 |
class stGraphWrapper : public GraphWrapper<Graph> {
|
marci@379
|
1100 |
public:
|
marci@379
|
1101 |
class Node;
|
marci@381
|
1102 |
friend class Node;
|
marci@379
|
1103 |
//GN, int
|
marci@379
|
1104 |
//0 normalis, 1 s, 2, true, ez az iteralasi sorrend,
|
marci@379
|
1105 |
//es a vege a false azaz (invalid, 3)
|
marci@379
|
1106 |
class NodeIt;
|
marci@381
|
1107 |
friend class NodeIt;
|
marci@379
|
1108 |
//GNI, int
|
marci@379
|
1109 |
class Edge;
|
marci@381
|
1110 |
friend class Edge;
|
marci@379
|
1111 |
//GE, int, GN
|
marci@379
|
1112 |
//0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend,
|
marci@379
|
1113 |
//invalid: (invalid, 3, invalid)
|
marci@379
|
1114 |
class OutEdgeIt;
|
marci@381
|
1115 |
friend class OutEdgeIt;
|
marci@379
|
1116 |
//GO, int, GNI
|
marci@379
|
1117 |
//normalis pontbol (first, 0, invalid), ..., (invalid, 2, vmi), ... (invalid, 3, invalid)
|
marci@379
|
1118 |
//s-bol (invalid, 1, first), ... (invalid, 3, invalid)
|
marci@379
|
1119 |
//t-bol (invalid, 3, invalid)
|
marci@379
|
1120 |
class InEdgeIt;
|
marci@381
|
1121 |
friend class InEdgeIt;
|
marci@379
|
1122 |
//GI, int, GNI
|
marci@379
|
1123 |
//normalis pontbol (first, 0, invalid), ..., (invalid, 1, vmi), ... (invalid, 3, invalid)
|
marci@379
|
1124 |
//s-be (invalid, 3, invalid)
|
marci@379
|
1125 |
//t-be (invalid, 2, first), ... (invalid, 3, invalid)
|
marci@379
|
1126 |
class EdgeIt;
|
marci@381
|
1127 |
friend class EdgeIt;
|
marci@379
|
1128 |
//(first, 0, invalid) ...
|
marci@379
|
1129 |
//(invalid, 1, vmi)
|
marci@379
|
1130 |
//(invalid, 2, vmi)
|
marci@379
|
1131 |
//invalid, 3, invalid)
|
marci@379
|
1132 |
template <typename T> class NodeMap;
|
marci@409
|
1133 |
template <typename T, typename Parent> class EdgeMap;
|
marci@341
|
1134 |
|
marci@379
|
1135 |
// template <typename T> friend class NodeMap;
|
marci@379
|
1136 |
// template <typename T> friend class EdgeMap;
|
marci@341
|
1137 |
|
marci@379
|
1138 |
const Node S_NODE;
|
marci@379
|
1139 |
const Node T_NODE;
|
marci@341
|
1140 |
|
marci@379
|
1141 |
static const bool S_CLASS=false;
|
marci@379
|
1142 |
static const bool T_CLASS=true;
|
marci@341
|
1143 |
|
marci@379
|
1144 |
stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) ,
|
marci@379
|
1145 |
S_NODE(INVALID, 1),
|
marci@379
|
1146 |
T_NODE(INVALID, 2) { }
|
marci@341
|
1147 |
|
marci@435
|
1148 |
|
marci@435
|
1149 |
// std::ostream&
|
marci@435
|
1150 |
// operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
|
marci@435
|
1151 |
// friend std::ostream&
|
marci@435
|
1152 |
// operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
|
marci@435
|
1153 |
// friend std::ostream&
|
marci@435
|
1154 |
// operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i);
|
marci@435
|
1155 |
|
marci@379
|
1156 |
class Node : public Graph::Node {
|
marci@379
|
1157 |
protected:
|
marci@379
|
1158 |
friend class GraphWrapper<Graph>;
|
marci@379
|
1159 |
friend class stGraphWrapper<Graph>;
|
marci@389
|
1160 |
template <typename T> friend class NodeMap;
|
marci@380
|
1161 |
friend class Edge;
|
marci@380
|
1162 |
friend class OutEdgeIt;
|
marci@380
|
1163 |
friend class InEdgeIt;
|
marci@380
|
1164 |
friend class EdgeIt;
|
marci@379
|
1165 |
int spec;
|
marci@379
|
1166 |
public:
|
marci@379
|
1167 |
Node() { }
|
marci@379
|
1168 |
Node(const typename Graph::Node& _n, int _spec=0) :
|
marci@379
|
1169 |
Graph::Node(_n), spec(_spec) { }
|
marci@379
|
1170 |
Node(const Invalid& i) : Graph::Node(i), spec(3) { }
|
marci@379
|
1171 |
friend bool operator==(const Node& u, const Node& v) {
|
marci@379
|
1172 |
return (u.spec==v.spec &&
|
marci@379
|
1173 |
static_cast<typename Graph::Node>(u)==
|
marci@379
|
1174 |
static_cast<typename Graph::Node>(v));
|
marci@379
|
1175 |
}
|
marci@379
|
1176 |
friend bool operator!=(const Node& u, const Node& v) {
|
marci@379
|
1177 |
return (v.spec!=u.spec ||
|
marci@379
|
1178 |
static_cast<typename Graph::Node>(u)!=
|
marci@379
|
1179 |
static_cast<typename Graph::Node>(v));
|
marci@409
|
1180 |
}
|
marci@435
|
1181 |
// template<typename G>
|
marci@435
|
1182 |
// friend std::ostream&
|
marci@435
|
1183 |
// operator<<(std::ostream& os, const typename stGraphWrapper<G>::Node& i);
|
marci@435
|
1184 |
friend std::ostream& operator<< (std::ostream& os, const Node& i);
|
marci@379
|
1185 |
int getSpec() const { return spec; }
|
marci@379
|
1186 |
};
|
marci@409
|
1187 |
|
marci@379
|
1188 |
class NodeIt {
|
marci@379
|
1189 |
friend class GraphWrapper<Graph>;
|
marci@379
|
1190 |
friend class stGraphWrapper<Graph>;
|
marci@379
|
1191 |
typename Graph::NodeIt n;
|
marci@379
|
1192 |
int spec;
|
marci@379
|
1193 |
public:
|
marci@379
|
1194 |
NodeIt() { }
|
marci@379
|
1195 |
NodeIt(const typename Graph::NodeIt& _n, int _spec) :
|
marci@379
|
1196 |
n(_n), spec(_spec) { }
|
marci@379
|
1197 |
NodeIt(const Invalid& i) : n(i), spec(3) { }
|
marci@381
|
1198 |
NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) {
|
marci@409
|
1199 |
if (!_G.graph->valid(n)) spec=1;
|
marci@379
|
1200 |
}
|
marci@379
|
1201 |
operator Node() const { return Node(n, spec); }
|
marci@379
|
1202 |
};
|
marci@409
|
1203 |
|
marci@379
|
1204 |
class Edge : public Graph::Edge {
|
marci@379
|
1205 |
friend class GraphWrapper<Graph>;
|
marci@379
|
1206 |
friend class stGraphWrapper<Graph>;
|
marci@409
|
1207 |
template <typename T, typename Parent> friend class EdgeMap;
|
marci@379
|
1208 |
int spec;
|
marci@379
|
1209 |
typename Graph::Node n;
|
marci@379
|
1210 |
public:
|
marci@379
|
1211 |
Edge() { }
|
marci@379
|
1212 |
Edge(const typename Graph::Edge& _e, int _spec,
|
marci@379
|
1213 |
const typename Graph::Node& _n) :
|
marci@379
|
1214 |
Graph::Edge(_e), spec(_spec), n(_n) {
|
marci@379
|
1215 |
}
|
marci@379
|
1216 |
Edge(const Invalid& i) : Graph::Edge(i), spec(3), n(i) { }
|
marci@379
|
1217 |
friend bool operator==(const Edge& u, const Edge& v) {
|
marci@379
|
1218 |
return (u.spec==v.spec &&
|
marci@379
|
1219 |
static_cast<typename Graph::Edge>(u)==
|
marci@379
|
1220 |
static_cast<typename Graph::Edge>(v) &&
|
marci@379
|
1221 |
u.n==v.n);
|
marci@379
|
1222 |
}
|
marci@379
|
1223 |
friend bool operator!=(const Edge& u, const Edge& v) {
|
marci@379
|
1224 |
return (v.spec!=u.spec ||
|
marci@379
|
1225 |
static_cast<typename Graph::Edge>(u)!=
|
marci@379
|
1226 |
static_cast<typename Graph::Edge>(v) ||
|
marci@379
|
1227 |
u.n!=v.n);
|
marci@379
|
1228 |
}
|
marci@435
|
1229 |
// template<typename G>
|
marci@435
|
1230 |
// friend std::ostream&
|
marci@435
|
1231 |
// operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i);
|
marci@435
|
1232 |
friend std::ostream& operator<< (std::ostream& os, const Edge& i);
|
marci@379
|
1233 |
int getSpec() const { return spec; }
|
marci@379
|
1234 |
};
|
marci@409
|
1235 |
|
marci@379
|
1236 |
class OutEdgeIt {
|
marci@379
|
1237 |
friend class GraphWrapper<Graph>;
|
marci@379
|
1238 |
friend class stGraphWrapper<Graph>;
|
marci@379
|
1239 |
typename Graph::OutEdgeIt e;
|
marci@379
|
1240 |
int spec;
|
marci@379
|
1241 |
typename Graph::ClassNodeIt n;
|
marci@379
|
1242 |
public:
|
marci@379
|
1243 |
OutEdgeIt() { }
|
marci@379
|
1244 |
OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec,
|
marci@379
|
1245 |
const typename Graph::ClassNodeIt& _n) :
|
marci@379
|
1246 |
e(_e), spec(_spec), n(_n) {
|
marci@379
|
1247 |
}
|
marci@379
|
1248 |
OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
|
marci@381
|
1249 |
OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
|
marci@379
|
1250 |
switch (_n.spec) {
|
marci@379
|
1251 |
case 0 :
|
marci@381
|
1252 |
if (_G.graph->inSClass(_n)) { //S, van normalis kiel
|
marci@379
|
1253 |
e=typename Graph::OutEdgeIt(*(_G.graph),
|
marci@379
|
1254 |
typename Graph::Node(_n));
|
marci@379
|
1255 |
spec=0;
|
marci@379
|
1256 |
n=INVALID;
|
marci@379
|
1257 |
if (!_G.graph->valid(e)) spec=3;
|
marci@379
|
1258 |
} else { //T, specko kiel van
|
marci@379
|
1259 |
e=INVALID;
|
marci@379
|
1260 |
spec=2;
|
marci@379
|
1261 |
n=_n;
|
marci@379
|
1262 |
}
|
marci@379
|
1263 |
break;
|
marci@379
|
1264 |
case 1:
|
marci@379
|
1265 |
e=INVALID;
|
marci@379
|
1266 |
spec=1;
|
marci@379
|
1267 |
_G.graph->first(n, S_CLASS); //s->vmi;
|
marci@379
|
1268 |
if (!_G.graph->valid(n)) spec=3; //Ha S ures
|
marci@379
|
1269 |
break;
|
marci@379
|
1270 |
case 2:
|
marci@379
|
1271 |
e=INVALID;
|
marci@379
|
1272 |
spec=3;
|
marci@379
|
1273 |
n=INVALID;
|
marci@379
|
1274 |
break;
|
marci@379
|
1275 |
}
|
marci@379
|
1276 |
}
|
marci@379
|
1277 |
operator Edge() const { return Edge(e, spec, n); }
|
marci@379
|
1278 |
};
|
marci@409
|
1279 |
|
marci@379
|
1280 |
class InEdgeIt {
|
marci@379
|
1281 |
friend class GraphWrapper<Graph>;
|
marci@379
|
1282 |
friend class stGraphWrapper<Graph>;
|
marci@379
|
1283 |
typename Graph::InEdgeIt e;
|
marci@379
|
1284 |
int spec;
|
marci@379
|
1285 |
typename Graph::ClassNodeIt n;
|
marci@379
|
1286 |
public:
|
marci@379
|
1287 |
InEdgeIt() { }
|
marci@379
|
1288 |
InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec,
|
marci@379
|
1289 |
const typename Graph::ClassNodeIt& _n) :
|
marci@379
|
1290 |
e(_e), spec(_spec), n(_n) {
|
marci@379
|
1291 |
}
|
marci@379
|
1292 |
InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
|
marci@381
|
1293 |
InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
|
marci@379
|
1294 |
switch (_n.spec) {
|
marci@379
|
1295 |
case 0 :
|
marci@381
|
1296 |
if (_G.graph->inTClass(_n)) { //T, van normalis beel
|
marci@379
|
1297 |
e=typename Graph::InEdgeIt(*(_G.graph),
|
marci@379
|
1298 |
typename Graph::Node(_n));
|
marci@379
|
1299 |
spec=0;
|
marci@379
|
1300 |
n=INVALID;
|
marci@379
|
1301 |
if (!_G.graph->valid(e)) spec=3;
|
marci@379
|
1302 |
} else { //S, specko beel van
|
marci@379
|
1303 |
e=INVALID;
|
marci@379
|
1304 |
spec=1;
|
marci@379
|
1305 |
n=_n;
|
marci@379
|
1306 |
}
|
marci@379
|
1307 |
break;
|
marci@379
|
1308 |
case 1:
|
marci@379
|
1309 |
e=INVALID;
|
marci@379
|
1310 |
spec=3;
|
marci@379
|
1311 |
n=INVALID;
|
marci@409
|
1312 |
break;
|
marci@379
|
1313 |
case 2:
|
marci@379
|
1314 |
e=INVALID;
|
marci@409
|
1315 |
spec=2;
|
marci@379
|
1316 |
_G.graph->first(n, T_CLASS); //vmi->t;
|
marci@379
|
1317 |
if (!_G.graph->valid(n)) spec=3; //Ha T ures
|
marci@379
|
1318 |
break;
|
marci@379
|
1319 |
}
|
marci@379
|
1320 |
}
|
marci@379
|
1321 |
operator Edge() const { return Edge(e, spec, n); }
|
marci@379
|
1322 |
};
|
marci@409
|
1323 |
|
marci@379
|
1324 |
class EdgeIt {
|
marci@379
|
1325 |
friend class GraphWrapper<Graph>;
|
marci@379
|
1326 |
friend class stGraphWrapper<Graph>;
|
marci@379
|
1327 |
typename Graph::EdgeIt e;
|
marci@379
|
1328 |
int spec;
|
marci@379
|
1329 |
typename Graph::ClassNodeIt n;
|
marci@379
|
1330 |
public:
|
marci@379
|
1331 |
EdgeIt() { }
|
marci@379
|
1332 |
EdgeIt(const typename Graph::EdgeIt& _e, int _spec,
|
marci@379
|
1333 |
const typename Graph::ClassNodeIt& _n) :
|
marci@379
|
1334 |
e(_e), spec(_spec), n(_n) { }
|
marci@379
|
1335 |
EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
|
marci@381
|
1336 |
EdgeIt(const stGraphWrapper<Graph>& _G) :
|
marci@379
|
1337 |
e(*(_G.graph)), spec(0), n(INVALID) {
|
marci@379
|
1338 |
if (!_G.graph->valid(e)) {
|
marci@379
|
1339 |
spec=1;
|
marci@379
|
1340 |
_G.graph->first(n, S_CLASS);
|
marci@379
|
1341 |
if (!_G.graph->valid(n)) { //Ha S ures
|
marci@379
|
1342 |
spec=2;
|
marci@379
|
1343 |
_G.graph->first(n, T_CLASS);
|
marci@379
|
1344 |
if (!_G.graph->valid(n)) { //Ha T ures
|
marci@379
|
1345 |
spec=3;
|
marci@379
|
1346 |
}
|
marci@379
|
1347 |
}
|
marci@379
|
1348 |
}
|
marci@379
|
1349 |
}
|
marci@379
|
1350 |
operator Edge() const { return Edge(e, spec, n); }
|
marci@379
|
1351 |
};
|
marci@341
|
1352 |
|
marci@379
|
1353 |
NodeIt& first(NodeIt& i) const {
|
marci@379
|
1354 |
i=NodeIt(*this); return i;
|
marci@379
|
1355 |
}
|
marci@379
|
1356 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@379
|
1357 |
i=OutEdgeIt(*this, p); return i;
|
marci@379
|
1358 |
}
|
marci@379
|
1359 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@379
|
1360 |
i=InEdgeIt(*this, p); return i;
|
marci@379
|
1361 |
}
|
marci@379
|
1362 |
EdgeIt& first(EdgeIt& i) const {
|
marci@379
|
1363 |
i=EdgeIt(*this); return i;
|
marci@379
|
1364 |
}
|
marci@341
|
1365 |
|
marci@379
|
1366 |
NodeIt& next(NodeIt& i) const {
|
marci@379
|
1367 |
switch (i.spec) {
|
marci@379
|
1368 |
case 0:
|
marci@389
|
1369 |
this->graph->next(i.n);
|
marci@389
|
1370 |
if (!this->graph->valid(i.n)) {
|
marci@379
|
1371 |
i.spec=1;
|
marci@379
|
1372 |
}
|
marci@379
|
1373 |
break;
|
marci@379
|
1374 |
case 1:
|
marci@379
|
1375 |
i.spec=2;
|
marci@379
|
1376 |
break;
|
marci@379
|
1377 |
case 2:
|
marci@379
|
1378 |
i.spec=3;
|
marci@379
|
1379 |
break;
|
marci@379
|
1380 |
}
|
marci@379
|
1381 |
return i;
|
marci@379
|
1382 |
}
|
marci@379
|
1383 |
OutEdgeIt& next(OutEdgeIt& i) const {
|
marci@393
|
1384 |
typename Graph::Node v;
|
marci@379
|
1385 |
switch (i.spec) {
|
marci@379
|
1386 |
case 0: //normal edge
|
marci@409
|
1387 |
v=this->graph->aNode(i.e);
|
marci@389
|
1388 |
this->graph->next(i.e);
|
marci@389
|
1389 |
if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
|
marci@389
|
1390 |
if (this->graph->inSClass(v)) { //S, nincs kiel
|
marci@379
|
1391 |
i.spec=3;
|
marci@379
|
1392 |
i.n=INVALID;
|
marci@379
|
1393 |
} else { //T, van kiel
|
marci@379
|
1394 |
i.spec=2;
|
marci@379
|
1395 |
i.n=v;
|
marci@379
|
1396 |
}
|
marci@379
|
1397 |
}
|
marci@379
|
1398 |
break;
|
marci@379
|
1399 |
case 1: //s->vmi
|
marci@389
|
1400 |
this->graph->next(i.n);
|
marci@389
|
1401 |
if (!this->graph->valid(i.n)) i.spec=3;
|
marci@379
|
1402 |
break;
|
marci@379
|
1403 |
case 2: //vmi->t
|
marci@379
|
1404 |
i.spec=3;
|
marci@379
|
1405 |
i.n=INVALID;
|
marci@379
|
1406 |
break;
|
marci@379
|
1407 |
}
|
marci@379
|
1408 |
return i;
|
marci@379
|
1409 |
}
|
marci@379
|
1410 |
InEdgeIt& next(InEdgeIt& i) const {
|
marci@393
|
1411 |
typename Graph::Node v;
|
marci@379
|
1412 |
switch (i.spec) {
|
marci@379
|
1413 |
case 0: //normal edge
|
marci@393
|
1414 |
v=this->graph->aNode(i.e);
|
marci@389
|
1415 |
this->graph->next(i.e);
|
marci@389
|
1416 |
if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
|
marci@389
|
1417 |
if (this->graph->inTClass(v)) { //S, nincs beel
|
marci@379
|
1418 |
i.spec=3;
|
marci@379
|
1419 |
i.n=INVALID;
|
marci@379
|
1420 |
} else { //S, van beel
|
marci@379
|
1421 |
i.spec=1;
|
marci@379
|
1422 |
i.n=v;
|
marci@379
|
1423 |
}
|
marci@379
|
1424 |
}
|
marci@379
|
1425 |
break;
|
marci@379
|
1426 |
case 1: //s->vmi
|
marci@379
|
1427 |
i.spec=3;
|
marci@379
|
1428 |
i.n=INVALID;
|
marci@379
|
1429 |
break;
|
marci@379
|
1430 |
case 2: //vmi->t
|
marci@389
|
1431 |
this->graph->next(i.n);
|
marci@389
|
1432 |
if (!this->graph->valid(i.n)) i.spec=3;
|
marci@379
|
1433 |
break;
|
marci@379
|
1434 |
}
|
marci@379
|
1435 |
return i;
|
marci@379
|
1436 |
}
|
marci@341
|
1437 |
|
marci@379
|
1438 |
EdgeIt& next(EdgeIt& i) const {
|
marci@379
|
1439 |
switch (i.spec) {
|
marci@379
|
1440 |
case 0:
|
marci@389
|
1441 |
this->graph->next(i.e);
|
marci@389
|
1442 |
if (!this->graph->valid(i.e)) {
|
marci@379
|
1443 |
i.spec=1;
|
marci@389
|
1444 |
this->graph->first(i.n, S_CLASS);
|
marci@389
|
1445 |
if (!this->graph->valid(i.n)) {
|
marci@379
|
1446 |
i.spec=2;
|
marci@389
|
1447 |
this->graph->first(i.n, T_CLASS);
|
marci@389
|
1448 |
if (!this->graph->valid(i.n)) i.spec=3;
|
marci@379
|
1449 |
}
|
marci@379
|
1450 |
}
|
marci@379
|
1451 |
break;
|
marci@379
|
1452 |
case 1:
|
marci@389
|
1453 |
this->graph->next(i.n);
|
marci@389
|
1454 |
if (!this->graph->valid(i.n)) {
|
marci@379
|
1455 |
i.spec=2;
|
marci@389
|
1456 |
this->graph->first(i.n, T_CLASS);
|
marci@389
|
1457 |
if (!this->graph->valid(i.n)) i.spec=3;
|
marci@379
|
1458 |
}
|
marci@379
|
1459 |
break;
|
marci@379
|
1460 |
case 2:
|
marci@389
|
1461 |
this->graph->next(i.n);
|
marci@389
|
1462 |
if (!this->graph->valid(i.n)) i.spec=3;
|
marci@379
|
1463 |
break;
|
marci@379
|
1464 |
}
|
marci@379
|
1465 |
return i;
|
marci@379
|
1466 |
}
|
marci@341
|
1467 |
|
marci@379
|
1468 |
Node tail(const Edge& e) const {
|
marci@379
|
1469 |
switch (e.spec) {
|
marci@393
|
1470 |
case 0:
|
marci@393
|
1471 |
return Node(this->graph->tail(e));
|
marci@393
|
1472 |
break;
|
marci@393
|
1473 |
case 1:
|
marci@393
|
1474 |
return S_NODE;
|
marci@393
|
1475 |
break;
|
marci@393
|
1476 |
case 2:
|
marci@393
|
1477 |
default:
|
marci@393
|
1478 |
return Node(e.n);
|
marci@393
|
1479 |
break;
|
marci@379
|
1480 |
}
|
marci@379
|
1481 |
}
|
marci@379
|
1482 |
Node head(const Edge& e) const {
|
marci@379
|
1483 |
switch (e.spec) {
|
marci@393
|
1484 |
case 0:
|
marci@393
|
1485 |
return Node(this->graph->head(e));
|
marci@393
|
1486 |
break;
|
marci@393
|
1487 |
case 1:
|
marci@393
|
1488 |
return Node(e.n);
|
marci@393
|
1489 |
break;
|
marci@393
|
1490 |
case 2:
|
marci@393
|
1491 |
default:
|
marci@393
|
1492 |
return T_NODE;
|
marci@393
|
1493 |
break;
|
marci@379
|
1494 |
}
|
marci@379
|
1495 |
}
|
marci@341
|
1496 |
|
marci@379
|
1497 |
bool valid(const Node& n) const { return (n.spec<3); }
|
marci@379
|
1498 |
bool valid(const Edge& e) const { return (e.spec<3); }
|
marci@379
|
1499 |
|
marci@409
|
1500 |
int nodeNum() const { return this->graph->nodeNum()+2; }
|
marci@409
|
1501 |
int edgeNum() const {
|
marci@409
|
1502 |
return this->graph->edgeNum()+this->graph->nodeNum();
|
marci@409
|
1503 |
}
|
marci@341
|
1504 |
|
marci@379
|
1505 |
Node aNode(const OutEdgeIt& e) const { return tail(e); }
|
marci@379
|
1506 |
Node aNode(const InEdgeIt& e) const { return head(e); }
|
marci@379
|
1507 |
Node bNode(const OutEdgeIt& e) const { return head(e); }
|
marci@379
|
1508 |
Node bNode(const InEdgeIt& e) const { return tail(e); }
|
marci@409
|
1509 |
|
marci@409
|
1510 |
void addNode() const { }
|
marci@409
|
1511 |
void addEdge() const { }
|
marci@409
|
1512 |
|
marci@389
|
1513 |
// Node addNode() const { return Node(this->graph->addNode()); }
|
marci@379
|
1514 |
// Edge addEdge(const Node& tail, const Node& head) const {
|
marci@389
|
1515 |
// return Edge(this->graph->addEdge(tail, head)); }
|
marci@341
|
1516 |
|
marci@389
|
1517 |
// void erase(const Node& i) const { this->graph->erase(i); }
|
marci@389
|
1518 |
// void erase(const Edge& i) const { this->graph->erase(i); }
|
marci@341
|
1519 |
|
marci@389
|
1520 |
// void clear() const { this->graph->clear(); }
|
marci@341
|
1521 |
|
marci@389
|
1522 |
template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
|
marci@389
|
1523 |
typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
|
marci@379
|
1524 |
T s_value, t_value;
|
marci@379
|
1525 |
public:
|
marci@409
|
1526 |
NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
|
marci@409
|
1527 |
s_value(),
|
marci@409
|
1528 |
t_value() { }
|
marci@389
|
1529 |
NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
|
marci@389
|
1530 |
s_value(a),
|
marci@389
|
1531 |
t_value(a) { }
|
marci@379
|
1532 |
T operator[](const Node& n) const {
|
marci@379
|
1533 |
switch (n.spec) {
|
marci@393
|
1534 |
case 0:
|
marci@393
|
1535 |
return Parent::operator[](n);
|
marci@393
|
1536 |
break;
|
marci@393
|
1537 |
case 1:
|
marci@393
|
1538 |
return s_value;
|
marci@393
|
1539 |
break;
|
marci@393
|
1540 |
case 2:
|
marci@393
|
1541 |
default:
|
marci@393
|
1542 |
return t_value;
|
marci@393
|
1543 |
break;
|
marci@379
|
1544 |
}
|
marci@379
|
1545 |
}
|
marci@379
|
1546 |
void set(const Node& n, T t) {
|
marci@379
|
1547 |
switch (n.spec) {
|
marci@393
|
1548 |
case 0:
|
marci@393
|
1549 |
GraphWrapper<Graph>::template NodeMap<T>::set(n, t);
|
marci@393
|
1550 |
break;
|
marci@393
|
1551 |
case 1:
|
marci@393
|
1552 |
s_value=t;
|
marci@393
|
1553 |
break;
|
marci@393
|
1554 |
case 2:
|
marci@393
|
1555 |
default:
|
marci@393
|
1556 |
t_value=t;
|
marci@393
|
1557 |
break;
|
marci@379
|
1558 |
}
|
marci@379
|
1559 |
}
|
marci@379
|
1560 |
};
|
marci@341
|
1561 |
|
marci@409
|
1562 |
template<typename T,
|
marci@409
|
1563 |
typename Parent=
|
marci@409
|
1564 |
typename GraphWrapper<Graph>::template EdgeMap<T> >
|
marci@409
|
1565 |
class EdgeMap : public Parent {
|
marci@409
|
1566 |
//typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
|
marci@389
|
1567 |
typename GraphWrapper<Graph>::template NodeMap<T> node_value;
|
marci@379
|
1568 |
public:
|
marci@393
|
1569 |
EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
|
marci@393
|
1570 |
node_value(_G) { }
|
marci@389
|
1571 |
EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
|
marci@389
|
1572 |
node_value(_G, a) { }
|
marci@379
|
1573 |
T operator[](const Edge& e) const {
|
marci@379
|
1574 |
switch (e.spec) {
|
marci@393
|
1575 |
case 0:
|
marci@393
|
1576 |
return Parent::operator[](e);
|
marci@393
|
1577 |
break;
|
marci@393
|
1578 |
case 1:
|
marci@393
|
1579 |
return node_value[e.n];
|
marci@393
|
1580 |
break;
|
marci@393
|
1581 |
case 2:
|
marci@393
|
1582 |
default:
|
marci@393
|
1583 |
return node_value[e.n];
|
marci@393
|
1584 |
break;
|
marci@379
|
1585 |
}
|
marci@379
|
1586 |
}
|
marci@379
|
1587 |
void set(const Edge& e, T t) {
|
marci@379
|
1588 |
switch (e.spec) {
|
marci@393
|
1589 |
case 0:
|
marci@409
|
1590 |
Parent::set(e, t);
|
marci@393
|
1591 |
break;
|
marci@393
|
1592 |
case 1:
|
marci@393
|
1593 |
node_value.set(e.n, t);
|
marci@393
|
1594 |
break;
|
marci@393
|
1595 |
case 2:
|
marci@393
|
1596 |
default:
|
marci@393
|
1597 |
node_value.set(e.n, t);
|
marci@393
|
1598 |
break;
|
marci@379
|
1599 |
}
|
marci@379
|
1600 |
}
|
marci@379
|
1601 |
};
|
marci@409
|
1602 |
|
marci@409
|
1603 |
// template<typename T> class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
|
marci@409
|
1604 |
// typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
|
marci@409
|
1605 |
// typename GraphWrapper<Graph>::template NodeMap<T> node_value;
|
marci@409
|
1606 |
// public:
|
marci@409
|
1607 |
// EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
|
marci@409
|
1608 |
// node_value(_G) { }
|
marci@409
|
1609 |
// EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
|
marci@409
|
1610 |
// node_value(_G, a) { }
|
marci@409
|
1611 |
// T operator[](const Edge& e) const {
|
marci@409
|
1612 |
// switch (e.spec) {
|
marci@409
|
1613 |
// case 0:
|
marci@409
|
1614 |
// return Parent::operator[](e);
|
marci@409
|
1615 |
// break;
|
marci@409
|
1616 |
// case 1:
|
marci@409
|
1617 |
// return node_value[e.n];
|
marci@409
|
1618 |
// break;
|
marci@409
|
1619 |
// case 2:
|
marci@409
|
1620 |
// default:
|
marci@409
|
1621 |
// return node_value[e.n];
|
marci@409
|
1622 |
// break;
|
marci@409
|
1623 |
// }
|
marci@409
|
1624 |
// }
|
marci@409
|
1625 |
// void set(const Edge& e, T t) {
|
marci@409
|
1626 |
// switch (e.spec) {
|
marci@409
|
1627 |
// case 0:
|
marci@409
|
1628 |
// GraphWrapper<Graph>::template EdgeMap<T>::set(e, t);
|
marci@409
|
1629 |
// break;
|
marci@409
|
1630 |
// case 1:
|
marci@409
|
1631 |
// node_value.set(e.n, t);
|
marci@409
|
1632 |
// break;
|
marci@409
|
1633 |
// case 2:
|
marci@409
|
1634 |
// default:
|
marci@409
|
1635 |
// node_value.set(e.n, t);
|
marci@409
|
1636 |
// break;
|
marci@409
|
1637 |
// }
|
marci@409
|
1638 |
// }
|
marci@409
|
1639 |
// };
|
marci@409
|
1640 |
|
marci@435
|
1641 |
// template<typename G>
|
marci@435
|
1642 |
friend std::ostream&
|
marci@435
|
1643 |
operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
|
marci@409
|
1644 |
os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
|
marci@409
|
1645 |
return os;
|
marci@409
|
1646 |
}
|
marci@435
|
1647 |
// template<typename G>
|
marci@435
|
1648 |
friend std::ostream&
|
marci@435
|
1649 |
operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
|
marci@409
|
1650 |
os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
|
marci@409
|
1651 |
" node: " << i.n << ")";
|
marci@409
|
1652 |
return os;
|
marci@409
|
1653 |
}
|
marci@409
|
1654 |
|
marci@379
|
1655 |
};
|
marci@379
|
1656 |
|
alpar@406
|
1657 |
///@}
|
marci@341
|
1658 |
|
alpar@105
|
1659 |
} //namespace hugo
|
marci@76
|
1660 |
|
alpar@406
|
1661 |
|
marci@259
|
1662 |
#endif //HUGO_GRAPH_WRAPPER_H
|
marci@76
|
1663 |
|