kpeter@773
|
1 |
/* -*- C++ -*-
|
kpeter@773
|
2 |
*
|
kpeter@773
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
kpeter@773
|
4 |
*
|
kpeter@773
|
5 |
* Copyright (C) 2003-2008
|
kpeter@773
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@773
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@773
|
8 |
*
|
kpeter@773
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@773
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@773
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@773
|
12 |
*
|
kpeter@773
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@773
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@773
|
15 |
* purpose.
|
kpeter@773
|
16 |
*
|
kpeter@773
|
17 |
*/
|
kpeter@773
|
18 |
|
kpeter@773
|
19 |
#ifndef LEMON_STATIC_GRAPH_H
|
kpeter@773
|
20 |
#define LEMON_STATIC_GRAPH_H
|
kpeter@773
|
21 |
|
kpeter@773
|
22 |
///\ingroup graphs
|
kpeter@773
|
23 |
///\file
|
kpeter@773
|
24 |
///\brief StaticDigraph class.
|
kpeter@773
|
25 |
|
kpeter@773
|
26 |
#include <lemon/core.h>
|
kpeter@773
|
27 |
#include <lemon/bits/graph_extender.h>
|
kpeter@773
|
28 |
|
kpeter@773
|
29 |
namespace lemon {
|
kpeter@773
|
30 |
|
kpeter@773
|
31 |
class StaticDigraphBase {
|
kpeter@773
|
32 |
public:
|
kpeter@773
|
33 |
|
kpeter@773
|
34 |
StaticDigraphBase()
|
kpeter@774
|
35 |
: built(false), node_num(0), arc_num(0),
|
kpeter@773
|
36 |
node_first_out(NULL), node_first_in(NULL),
|
kpeter@773
|
37 |
arc_source(NULL), arc_target(NULL),
|
kpeter@773
|
38 |
arc_next_in(NULL), arc_next_out(NULL) {}
|
kpeter@773
|
39 |
|
kpeter@773
|
40 |
~StaticDigraphBase() {
|
kpeter@774
|
41 |
if (built) {
|
kpeter@773
|
42 |
delete[] node_first_out;
|
kpeter@773
|
43 |
delete[] node_first_in;
|
kpeter@773
|
44 |
delete[] arc_source;
|
kpeter@773
|
45 |
delete[] arc_target;
|
kpeter@773
|
46 |
delete[] arc_next_out;
|
kpeter@773
|
47 |
delete[] arc_next_in;
|
kpeter@773
|
48 |
}
|
kpeter@773
|
49 |
}
|
kpeter@773
|
50 |
|
kpeter@773
|
51 |
class Node {
|
kpeter@773
|
52 |
friend class StaticDigraphBase;
|
kpeter@773
|
53 |
protected:
|
kpeter@773
|
54 |
int id;
|
kpeter@773
|
55 |
Node(int _id) : id(_id) {}
|
kpeter@773
|
56 |
public:
|
kpeter@773
|
57 |
Node() {}
|
kpeter@773
|
58 |
Node (Invalid) : id(-1) {}
|
kpeter@773
|
59 |
bool operator==(const Node& node) const { return id == node.id; }
|
kpeter@773
|
60 |
bool operator!=(const Node& node) const { return id != node.id; }
|
kpeter@773
|
61 |
bool operator<(const Node& node) const { return id < node.id; }
|
kpeter@773
|
62 |
};
|
kpeter@773
|
63 |
|
kpeter@773
|
64 |
class Arc {
|
kpeter@773
|
65 |
friend class StaticDigraphBase;
|
kpeter@773
|
66 |
protected:
|
kpeter@773
|
67 |
int id;
|
kpeter@773
|
68 |
Arc(int _id) : id(_id) {}
|
kpeter@773
|
69 |
public:
|
kpeter@773
|
70 |
Arc() { }
|
kpeter@773
|
71 |
Arc (Invalid) : id(-1) {}
|
kpeter@773
|
72 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
kpeter@773
|
73 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
kpeter@773
|
74 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
kpeter@773
|
75 |
};
|
kpeter@773
|
76 |
|
kpeter@773
|
77 |
Node source(const Arc& e) const { return Node(arc_source[e.id]); }
|
kpeter@773
|
78 |
Node target(const Arc& e) const { return Node(arc_target[e.id]); }
|
kpeter@773
|
79 |
|
kpeter@773
|
80 |
void first(Node& n) const { n.id = node_num - 1; }
|
kpeter@773
|
81 |
static void next(Node& n) { --n.id; }
|
kpeter@773
|
82 |
|
kpeter@773
|
83 |
void first(Arc& e) const { e.id = arc_num - 1; }
|
kpeter@773
|
84 |
static void next(Arc& e) { --e.id; }
|
kpeter@773
|
85 |
|
kpeter@773
|
86 |
void firstOut(Arc& e, const Node& n) const {
|
kpeter@773
|
87 |
e.id = node_first_out[n.id] != node_first_out[n.id + 1] ?
|
kpeter@773
|
88 |
node_first_out[n.id] : -1;
|
kpeter@773
|
89 |
}
|
kpeter@773
|
90 |
void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
|
kpeter@773
|
91 |
|
kpeter@773
|
92 |
void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
|
kpeter@773
|
93 |
void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
|
kpeter@773
|
94 |
|
kpeter@778
|
95 |
static int id(const Node& n) { return n.id; }
|
kpeter@778
|
96 |
static Node nodeFromId(int id) { return Node(id); }
|
kpeter@773
|
97 |
int maxNodeId() const { return node_num - 1; }
|
kpeter@773
|
98 |
|
kpeter@778
|
99 |
static int id(const Arc& e) { return e.id; }
|
kpeter@778
|
100 |
static Arc arcFromId(int id) { return Arc(id); }
|
kpeter@773
|
101 |
int maxArcId() const { return arc_num - 1; }
|
kpeter@773
|
102 |
|
kpeter@773
|
103 |
typedef True NodeNumTag;
|
kpeter@773
|
104 |
typedef True ArcNumTag;
|
kpeter@773
|
105 |
|
kpeter@773
|
106 |
int nodeNum() const { return node_num; }
|
kpeter@773
|
107 |
int arcNum() const { return arc_num; }
|
kpeter@773
|
108 |
|
kpeter@773
|
109 |
private:
|
kpeter@773
|
110 |
|
kpeter@773
|
111 |
template <typename Digraph, typename NodeRefMap>
|
kpeter@773
|
112 |
class ArcLess {
|
kpeter@773
|
113 |
public:
|
kpeter@773
|
114 |
typedef typename Digraph::Arc Arc;
|
kpeter@773
|
115 |
|
kpeter@773
|
116 |
ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef)
|
kpeter@773
|
117 |
: digraph(_graph), nodeRef(_nodeRef) {}
|
kpeter@773
|
118 |
|
kpeter@773
|
119 |
bool operator()(const Arc& left, const Arc& right) const {
|
kpeter@773
|
120 |
return nodeRef[digraph.target(left)] < nodeRef[digraph.target(right)];
|
kpeter@773
|
121 |
}
|
kpeter@773
|
122 |
private:
|
kpeter@773
|
123 |
const Digraph& digraph;
|
kpeter@773
|
124 |
const NodeRefMap& nodeRef;
|
kpeter@773
|
125 |
};
|
kpeter@773
|
126 |
|
kpeter@773
|
127 |
public:
|
kpeter@773
|
128 |
|
kpeter@773
|
129 |
typedef True BuildTag;
|
kpeter@773
|
130 |
|
kpeter@774
|
131 |
void clear() {
|
kpeter@774
|
132 |
if (built) {
|
kpeter@773
|
133 |
delete[] node_first_out;
|
kpeter@773
|
134 |
delete[] node_first_in;
|
kpeter@773
|
135 |
delete[] arc_source;
|
kpeter@773
|
136 |
delete[] arc_target;
|
kpeter@773
|
137 |
delete[] arc_next_out;
|
kpeter@773
|
138 |
delete[] arc_next_in;
|
kpeter@773
|
139 |
}
|
kpeter@774
|
140 |
built = false;
|
kpeter@774
|
141 |
node_num = 0;
|
kpeter@774
|
142 |
arc_num = 0;
|
kpeter@774
|
143 |
}
|
kpeter@774
|
144 |
|
kpeter@774
|
145 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
|
kpeter@774
|
146 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
kpeter@773
|
147 |
typedef typename Digraph::Node GNode;
|
kpeter@773
|
148 |
typedef typename Digraph::Arc GArc;
|
kpeter@773
|
149 |
|
kpeter@774
|
150 |
built = true;
|
kpeter@774
|
151 |
|
kpeter@773
|
152 |
node_num = countNodes(digraph);
|
kpeter@773
|
153 |
arc_num = countArcs(digraph);
|
kpeter@773
|
154 |
|
kpeter@773
|
155 |
node_first_out = new int[node_num + 1];
|
kpeter@773
|
156 |
node_first_in = new int[node_num];
|
kpeter@773
|
157 |
|
kpeter@773
|
158 |
arc_source = new int[arc_num];
|
kpeter@773
|
159 |
arc_target = new int[arc_num];
|
kpeter@773
|
160 |
arc_next_out = new int[arc_num];
|
kpeter@773
|
161 |
arc_next_in = new int[arc_num];
|
kpeter@773
|
162 |
|
kpeter@773
|
163 |
int node_index = 0;
|
kpeter@773
|
164 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
|
kpeter@773
|
165 |
nodeRef[n] = Node(node_index);
|
kpeter@773
|
166 |
node_first_in[node_index] = -1;
|
kpeter@773
|
167 |
++node_index;
|
kpeter@773
|
168 |
}
|
kpeter@773
|
169 |
|
kpeter@773
|
170 |
ArcLess<Digraph, NodeRefMap> arcLess(digraph, nodeRef);
|
kpeter@773
|
171 |
|
kpeter@773
|
172 |
int arc_index = 0;
|
kpeter@773
|
173 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
|
kpeter@773
|
174 |
int source = nodeRef[n].id;
|
kpeter@773
|
175 |
std::vector<GArc> arcs;
|
kpeter@773
|
176 |
for (typename Digraph::OutArcIt e(digraph, n); e != INVALID; ++e) {
|
kpeter@773
|
177 |
arcs.push_back(e);
|
kpeter@773
|
178 |
}
|
kpeter@773
|
179 |
if (!arcs.empty()) {
|
kpeter@773
|
180 |
node_first_out[source] = arc_index;
|
kpeter@773
|
181 |
std::sort(arcs.begin(), arcs.end(), arcLess);
|
kpeter@773
|
182 |
for (typename std::vector<GArc>::iterator it = arcs.begin();
|
kpeter@773
|
183 |
it != arcs.end(); ++it) {
|
kpeter@773
|
184 |
int target = nodeRef[digraph.target(*it)].id;
|
kpeter@773
|
185 |
arcRef[*it] = Arc(arc_index);
|
kpeter@773
|
186 |
arc_source[arc_index] = source;
|
kpeter@773
|
187 |
arc_target[arc_index] = target;
|
kpeter@773
|
188 |
arc_next_in[arc_index] = node_first_in[target];
|
kpeter@773
|
189 |
node_first_in[target] = arc_index;
|
kpeter@773
|
190 |
arc_next_out[arc_index] = arc_index + 1;
|
kpeter@773
|
191 |
++arc_index;
|
kpeter@773
|
192 |
}
|
kpeter@773
|
193 |
arc_next_out[arc_index - 1] = -1;
|
kpeter@773
|
194 |
} else {
|
kpeter@773
|
195 |
node_first_out[source] = arc_index;
|
kpeter@773
|
196 |
}
|
kpeter@773
|
197 |
}
|
kpeter@773
|
198 |
node_first_out[node_num] = arc_num;
|
kpeter@773
|
199 |
}
|
kpeter@777
|
200 |
|
kpeter@777
|
201 |
template <typename ArcListIterator>
|
kpeter@777
|
202 |
void build(int n, ArcListIterator first, ArcListIterator last) {
|
kpeter@777
|
203 |
built = true;
|
kpeter@777
|
204 |
|
kpeter@777
|
205 |
node_num = n;
|
kpeter@777
|
206 |
arc_num = std::distance(first, last);
|
kpeter@777
|
207 |
|
kpeter@777
|
208 |
node_first_out = new int[node_num + 1];
|
kpeter@777
|
209 |
node_first_in = new int[node_num];
|
kpeter@777
|
210 |
|
kpeter@777
|
211 |
arc_source = new int[arc_num];
|
kpeter@777
|
212 |
arc_target = new int[arc_num];
|
kpeter@777
|
213 |
arc_next_out = new int[arc_num];
|
kpeter@777
|
214 |
arc_next_in = new int[arc_num];
|
kpeter@777
|
215 |
|
kpeter@777
|
216 |
for (int i = 0; i != node_num; ++i) {
|
kpeter@777
|
217 |
node_first_in[i] = -1;
|
kpeter@777
|
218 |
}
|
kpeter@777
|
219 |
|
kpeter@777
|
220 |
int arc_index = 0;
|
kpeter@777
|
221 |
for (int i = 0; i != node_num; ++i) {
|
kpeter@777
|
222 |
node_first_out[i] = arc_index;
|
kpeter@777
|
223 |
for ( ; first != last && (*first).first == i; ++first) {
|
kpeter@777
|
224 |
int j = (*first).second;
|
kpeter@777
|
225 |
LEMON_ASSERT(j >= 0 && j < node_num,
|
kpeter@777
|
226 |
"Wrong arc list for StaticDigraph::build()");
|
kpeter@777
|
227 |
arc_source[arc_index] = i;
|
kpeter@777
|
228 |
arc_target[arc_index] = j;
|
kpeter@777
|
229 |
arc_next_in[arc_index] = node_first_in[j];
|
kpeter@777
|
230 |
node_first_in[j] = arc_index;
|
kpeter@777
|
231 |
arc_next_out[arc_index] = arc_index + 1;
|
kpeter@777
|
232 |
++arc_index;
|
kpeter@777
|
233 |
}
|
kpeter@777
|
234 |
if (arc_index > node_first_out[i])
|
kpeter@777
|
235 |
arc_next_out[arc_index - 1] = -1;
|
kpeter@777
|
236 |
}
|
kpeter@777
|
237 |
LEMON_ASSERT(first == last,
|
kpeter@777
|
238 |
"Wrong arc list for StaticDigraph::build()");
|
kpeter@777
|
239 |
node_first_out[node_num] = arc_num;
|
kpeter@777
|
240 |
}
|
kpeter@773
|
241 |
|
kpeter@773
|
242 |
protected:
|
kpeter@773
|
243 |
|
kpeter@773
|
244 |
void fastFirstOut(Arc& e, const Node& n) const {
|
kpeter@773
|
245 |
e.id = node_first_out[n.id];
|
kpeter@773
|
246 |
}
|
kpeter@773
|
247 |
|
kpeter@773
|
248 |
static void fastNextOut(Arc& e) {
|
kpeter@773
|
249 |
++e.id;
|
kpeter@773
|
250 |
}
|
kpeter@773
|
251 |
void fastLastOut(Arc& e, const Node& n) const {
|
kpeter@773
|
252 |
e.id = node_first_out[n.id + 1];
|
kpeter@773
|
253 |
}
|
kpeter@773
|
254 |
|
kpeter@774
|
255 |
protected:
|
kpeter@774
|
256 |
bool built;
|
kpeter@773
|
257 |
int node_num;
|
kpeter@773
|
258 |
int arc_num;
|
kpeter@773
|
259 |
int *node_first_out;
|
kpeter@773
|
260 |
int *node_first_in;
|
kpeter@773
|
261 |
int *arc_source;
|
kpeter@773
|
262 |
int *arc_target;
|
kpeter@773
|
263 |
int *arc_next_in;
|
kpeter@773
|
264 |
int *arc_next_out;
|
kpeter@773
|
265 |
};
|
kpeter@773
|
266 |
|
kpeter@773
|
267 |
typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase;
|
kpeter@773
|
268 |
|
kpeter@773
|
269 |
|
kpeter@775
|
270 |
/// \ingroup graphs
|
kpeter@775
|
271 |
///
|
kpeter@775
|
272 |
/// \brief A static directed graph class.
|
kpeter@775
|
273 |
///
|
kpeter@775
|
274 |
/// \ref StaticDigraph is a highly efficient digraph implementation,
|
kpeter@775
|
275 |
/// but it is fully static.
|
kpeter@775
|
276 |
/// It stores only two \c int values for each node and only four \c int
|
kpeter@775
|
277 |
/// values for each arc. Moreover it provides faster item iteration than
|
kpeter@775
|
278 |
/// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt
|
kpeter@775
|
279 |
/// iterators, since its arcs are stored in an appropriate order.
|
kpeter@775
|
280 |
/// However it only provides build() and clear() functions and does not
|
kpeter@775
|
281 |
/// support any other modification of the digraph.
|
kpeter@775
|
282 |
///
|
kpeter@776
|
283 |
/// Since this digraph structure is completely static, its nodes and arcs
|
kpeter@776
|
284 |
/// can be indexed with integers from the ranges <tt>[0..nodeNum()-1]</tt>
|
kpeter@776
|
285 |
/// and <tt>[0..arcNum()-1]</tt>, respectively.
|
kpeter@776
|
286 |
/// The index of an item is the same as its ID, it can be obtained
|
kpeter@776
|
287 |
/// using the corresponding \ref index() or \ref concepts::Digraph::id()
|
kpeter@776
|
288 |
/// "id()" function. A node or arc with a certain index can be obtained
|
kpeter@776
|
289 |
/// using node() or arc().
|
kpeter@776
|
290 |
///
|
kpeter@775
|
291 |
/// This type fully conforms to the \ref concepts::Digraph "Digraph concept".
|
kpeter@775
|
292 |
/// Most of its member functions and nested classes are documented
|
kpeter@775
|
293 |
/// only in the concept class.
|
kpeter@775
|
294 |
///
|
kpeter@775
|
295 |
/// \sa concepts::Digraph
|
kpeter@773
|
296 |
class StaticDigraph : public ExtendedStaticDigraphBase {
|
kpeter@773
|
297 |
public:
|
kpeter@773
|
298 |
|
kpeter@773
|
299 |
typedef ExtendedStaticDigraphBase Parent;
|
kpeter@774
|
300 |
|
kpeter@774
|
301 |
public:
|
kpeter@774
|
302 |
|
kpeter@776
|
303 |
/// \brief Constructor
|
kpeter@775
|
304 |
///
|
kpeter@776
|
305 |
/// Default constructor.
|
kpeter@776
|
306 |
StaticDigraph() : Parent() {}
|
kpeter@776
|
307 |
|
kpeter@776
|
308 |
/// \brief The node with the given index.
|
kpeter@776
|
309 |
///
|
kpeter@776
|
310 |
/// This function returns the node with the given index.
|
kpeter@776
|
311 |
/// \sa index()
|
kpeter@778
|
312 |
static Node node(int ix) { return Parent::nodeFromId(ix); }
|
kpeter@776
|
313 |
|
kpeter@776
|
314 |
/// \brief The arc with the given index.
|
kpeter@776
|
315 |
///
|
kpeter@776
|
316 |
/// This function returns the arc with the given index.
|
kpeter@776
|
317 |
/// \sa index()
|
kpeter@778
|
318 |
static Arc arc(int ix) { return Parent::arcFromId(ix); }
|
kpeter@776
|
319 |
|
kpeter@776
|
320 |
/// \brief The index of the given node.
|
kpeter@776
|
321 |
///
|
kpeter@776
|
322 |
/// This function returns the index of the the given node.
|
kpeter@776
|
323 |
/// \sa node()
|
kpeter@778
|
324 |
static int index(Node node) { return Parent::id(node); }
|
kpeter@776
|
325 |
|
kpeter@776
|
326 |
/// \brief The index of the given arc.
|
kpeter@776
|
327 |
///
|
kpeter@776
|
328 |
/// This function returns the index of the the given arc.
|
kpeter@776
|
329 |
/// \sa arc()
|
kpeter@778
|
330 |
static int index(Arc arc) { return Parent::id(arc); }
|
kpeter@776
|
331 |
|
kpeter@776
|
332 |
/// \brief Number of nodes.
|
kpeter@776
|
333 |
///
|
kpeter@776
|
334 |
/// This function returns the number of nodes.
|
kpeter@776
|
335 |
int nodeNum() const { return node_num; }
|
kpeter@776
|
336 |
|
kpeter@776
|
337 |
/// \brief Number of arcs.
|
kpeter@776
|
338 |
///
|
kpeter@776
|
339 |
/// This function returns the number of arcs.
|
kpeter@776
|
340 |
int arcNum() const { return arc_num; }
|
kpeter@776
|
341 |
|
kpeter@775
|
342 |
/// \brief Build the digraph copying another digraph.
|
kpeter@775
|
343 |
///
|
kpeter@775
|
344 |
/// This function builds the digraph copying another digraph of any
|
kpeter@775
|
345 |
/// kind. It can be called more than once, but in such case, the whole
|
kpeter@775
|
346 |
/// structure and all maps will be cleared and rebuilt.
|
kpeter@775
|
347 |
///
|
kpeter@775
|
348 |
/// This method also makes possible to copy a digraph to a StaticDigraph
|
kpeter@775
|
349 |
/// structure using \ref DigraphCopy.
|
kpeter@775
|
350 |
///
|
kpeter@775
|
351 |
/// \param digraph An existing digraph to be copied.
|
kpeter@775
|
352 |
/// \param nodeRef The node references will be copied into this map.
|
kpeter@775
|
353 |
/// Its key type must be \c Digraph::Node and its value type must be
|
kpeter@775
|
354 |
/// \c StaticDigraph::Node.
|
kpeter@775
|
355 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
|
kpeter@775
|
356 |
/// concept.
|
kpeter@775
|
357 |
/// \param arcRef The arc references will be copied into this map.
|
kpeter@775
|
358 |
/// Its key type must be \c Digraph::Arc and its value type must be
|
kpeter@775
|
359 |
/// \c StaticDigraph::Arc.
|
kpeter@775
|
360 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@775
|
361 |
///
|
kpeter@775
|
362 |
/// \note If you do not need the arc references, then you could use
|
kpeter@775
|
363 |
/// \ref NullMap for the last parameter. However the node references
|
kpeter@775
|
364 |
/// are required by the function itself, thus they must be readable
|
kpeter@775
|
365 |
/// from the map.
|
kpeter@774
|
366 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
|
kpeter@774
|
367 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
kpeter@774
|
368 |
if (built) Parent::clear();
|
kpeter@774
|
369 |
Parent::build(digraph, nodeRef, arcRef);
|
kpeter@774
|
370 |
}
|
kpeter@774
|
371 |
|
kpeter@777
|
372 |
/// \brief Build the digraph from an arc list.
|
kpeter@777
|
373 |
///
|
kpeter@777
|
374 |
/// This function builds the digraph from the given arc list.
|
kpeter@777
|
375 |
/// It can be called more than once, but in such case, the whole
|
kpeter@777
|
376 |
/// structure and all maps will be cleared and rebuilt.
|
kpeter@777
|
377 |
///
|
kpeter@777
|
378 |
/// The list of the arcs must be given in the range <tt>[begin, end)</tt>
|
kpeter@777
|
379 |
/// specified by STL compatible itartors whose \c value_type must be
|
kpeter@777
|
380 |
/// <tt>std::pair<int,int></tt>.
|
kpeter@777
|
381 |
/// Each arc must be specified by a pair of integer indices
|
kpeter@777
|
382 |
/// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a
|
kpeter@777
|
383 |
/// non-decreasing order with respect to their first values.</i>
|
kpeter@777
|
384 |
/// If the k-th pair in the list is <tt>(i,j)</tt>, then
|
kpeter@777
|
385 |
/// <tt>arc(k-1)</tt> will connect <tt>node(i)</tt> to <tt>node(j)</tt>.
|
kpeter@777
|
386 |
///
|
kpeter@777
|
387 |
/// \param n The number of nodes.
|
kpeter@777
|
388 |
/// \param begin An iterator pointing to the beginning of the arc list.
|
kpeter@777
|
389 |
/// \param end An iterator pointing to the end of the arc list.
|
kpeter@777
|
390 |
///
|
kpeter@777
|
391 |
/// For example, a simple digraph can be constructed like this.
|
kpeter@777
|
392 |
/// \code
|
kpeter@777
|
393 |
/// std::vector<std::pair<int,int> > arcs;
|
kpeter@777
|
394 |
/// arcs.push_back(std::make_pair(0,1));
|
kpeter@777
|
395 |
/// arcs.push_back(std::make_pair(0,2));
|
kpeter@777
|
396 |
/// arcs.push_back(std::make_pair(1,3));
|
kpeter@777
|
397 |
/// arcs.push_back(std::make_pair(1,2));
|
kpeter@777
|
398 |
/// arcs.push_back(std::make_pair(3,0));
|
kpeter@777
|
399 |
/// StaticDigraph gr;
|
kpeter@777
|
400 |
/// gr.build(4, arcs.begin(), arcs.end());
|
kpeter@777
|
401 |
/// \endcode
|
kpeter@777
|
402 |
template <typename ArcListIterator>
|
kpeter@777
|
403 |
void build(int n, ArcListIterator begin, ArcListIterator end) {
|
kpeter@777
|
404 |
if (built) Parent::clear();
|
kpeter@777
|
405 |
StaticDigraphBase::build(n, begin, end);
|
kpeter@777
|
406 |
notifier(Node()).build();
|
kpeter@777
|
407 |
notifier(Arc()).build();
|
kpeter@777
|
408 |
}
|
kpeter@777
|
409 |
|
kpeter@776
|
410 |
/// \brief Clear the digraph.
|
kpeter@776
|
411 |
///
|
kpeter@776
|
412 |
/// This function erases all nodes and arcs from the digraph.
|
kpeter@776
|
413 |
void clear() {
|
kpeter@776
|
414 |
Parent::clear();
|
kpeter@776
|
415 |
}
|
kpeter@773
|
416 |
|
kpeter@773
|
417 |
protected:
|
kpeter@773
|
418 |
|
kpeter@773
|
419 |
using Parent::fastFirstOut;
|
kpeter@773
|
420 |
using Parent::fastNextOut;
|
kpeter@773
|
421 |
using Parent::fastLastOut;
|
kpeter@773
|
422 |
|
kpeter@773
|
423 |
public:
|
kpeter@773
|
424 |
|
kpeter@773
|
425 |
class OutArcIt : public Arc {
|
kpeter@773
|
426 |
public:
|
kpeter@773
|
427 |
|
kpeter@773
|
428 |
OutArcIt() { }
|
kpeter@773
|
429 |
|
kpeter@773
|
430 |
OutArcIt(Invalid i) : Arc(i) { }
|
kpeter@773
|
431 |
|
kpeter@773
|
432 |
OutArcIt(const StaticDigraph& digraph, const Node& node) {
|
kpeter@773
|
433 |
digraph.fastFirstOut(*this, node);
|
kpeter@773
|
434 |
digraph.fastLastOut(last, node);
|
kpeter@773
|
435 |
if (last == *this) *this = INVALID;
|
kpeter@773
|
436 |
}
|
kpeter@773
|
437 |
|
kpeter@773
|
438 |
OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
|
kpeter@773
|
439 |
if (arc != INVALID) {
|
kpeter@773
|
440 |
digraph.fastLastOut(last, digraph.source(arc));
|
kpeter@773
|
441 |
}
|
kpeter@773
|
442 |
}
|
kpeter@773
|
443 |
|
kpeter@773
|
444 |
OutArcIt& operator++() {
|
kpeter@773
|
445 |
StaticDigraph::fastNextOut(*this);
|
kpeter@773
|
446 |
if (last == *this) *this = INVALID;
|
kpeter@773
|
447 |
return *this;
|
kpeter@773
|
448 |
}
|
kpeter@773
|
449 |
|
kpeter@773
|
450 |
private:
|
kpeter@773
|
451 |
Arc last;
|
kpeter@773
|
452 |
};
|
kpeter@773
|
453 |
|
kpeter@774
|
454 |
Node baseNode(const OutArcIt &arc) const {
|
kpeter@774
|
455 |
return Parent::source(static_cast<const Arc&>(arc));
|
kpeter@774
|
456 |
}
|
kpeter@774
|
457 |
|
kpeter@774
|
458 |
Node runningNode(const OutArcIt &arc) const {
|
kpeter@774
|
459 |
return Parent::target(static_cast<const Arc&>(arc));
|
kpeter@774
|
460 |
}
|
kpeter@774
|
461 |
|
kpeter@774
|
462 |
Node baseNode(const InArcIt &arc) const {
|
kpeter@774
|
463 |
return Parent::target(static_cast<const Arc&>(arc));
|
kpeter@774
|
464 |
}
|
kpeter@774
|
465 |
|
kpeter@774
|
466 |
Node runningNode(const InArcIt &arc) const {
|
kpeter@774
|
467 |
return Parent::source(static_cast<const Arc&>(arc));
|
kpeter@774
|
468 |
}
|
kpeter@774
|
469 |
|
kpeter@773
|
470 |
};
|
kpeter@773
|
471 |
|
kpeter@773
|
472 |
}
|
kpeter@773
|
473 |
|
kpeter@773
|
474 |
#endif
|