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@773
|
95 |
int id(const Node& n) const { return n.id; }
|
kpeter@773
|
96 |
Node nodeFromId(int id) const { return Node(id); }
|
kpeter@773
|
97 |
int maxNodeId() const { return node_num - 1; }
|
kpeter@773
|
98 |
|
kpeter@773
|
99 |
int id(const Arc& e) const { return e.id; }
|
kpeter@773
|
100 |
Arc arcFromId(int id) const { 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@773
|
200 |
|
kpeter@773
|
201 |
protected:
|
kpeter@773
|
202 |
|
kpeter@773
|
203 |
void fastFirstOut(Arc& e, const Node& n) const {
|
kpeter@773
|
204 |
e.id = node_first_out[n.id];
|
kpeter@773
|
205 |
}
|
kpeter@773
|
206 |
|
kpeter@773
|
207 |
static void fastNextOut(Arc& e) {
|
kpeter@773
|
208 |
++e.id;
|
kpeter@773
|
209 |
}
|
kpeter@773
|
210 |
void fastLastOut(Arc& e, const Node& n) const {
|
kpeter@773
|
211 |
e.id = node_first_out[n.id + 1];
|
kpeter@773
|
212 |
}
|
kpeter@773
|
213 |
|
kpeter@774
|
214 |
protected:
|
kpeter@774
|
215 |
bool built;
|
kpeter@773
|
216 |
int node_num;
|
kpeter@773
|
217 |
int arc_num;
|
kpeter@773
|
218 |
int *node_first_out;
|
kpeter@773
|
219 |
int *node_first_in;
|
kpeter@773
|
220 |
int *arc_source;
|
kpeter@773
|
221 |
int *arc_target;
|
kpeter@773
|
222 |
int *arc_next_in;
|
kpeter@773
|
223 |
int *arc_next_out;
|
kpeter@773
|
224 |
};
|
kpeter@773
|
225 |
|
kpeter@773
|
226 |
typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase;
|
kpeter@773
|
227 |
|
kpeter@773
|
228 |
|
kpeter@775
|
229 |
/// \ingroup graphs
|
kpeter@775
|
230 |
///
|
kpeter@775
|
231 |
/// \brief A static directed graph class.
|
kpeter@775
|
232 |
///
|
kpeter@775
|
233 |
/// \ref StaticDigraph is a highly efficient digraph implementation,
|
kpeter@775
|
234 |
/// but it is fully static.
|
kpeter@775
|
235 |
/// It stores only two \c int values for each node and only four \c int
|
kpeter@775
|
236 |
/// values for each arc. Moreover it provides faster item iteration than
|
kpeter@775
|
237 |
/// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt
|
kpeter@775
|
238 |
/// iterators, since its arcs are stored in an appropriate order.
|
kpeter@775
|
239 |
/// However it only provides build() and clear() functions and does not
|
kpeter@775
|
240 |
/// support any other modification of the digraph.
|
kpeter@775
|
241 |
///
|
kpeter@775
|
242 |
/// This type fully conforms to the \ref concepts::Digraph "Digraph concept".
|
kpeter@775
|
243 |
/// Most of its member functions and nested classes are documented
|
kpeter@775
|
244 |
/// only in the concept class.
|
kpeter@775
|
245 |
///
|
kpeter@775
|
246 |
/// \sa concepts::Digraph
|
kpeter@773
|
247 |
class StaticDigraph : public ExtendedStaticDigraphBase {
|
kpeter@773
|
248 |
public:
|
kpeter@773
|
249 |
|
kpeter@773
|
250 |
typedef ExtendedStaticDigraphBase Parent;
|
kpeter@774
|
251 |
|
kpeter@774
|
252 |
public:
|
kpeter@774
|
253 |
|
kpeter@775
|
254 |
/// \brief Clear the digraph.
|
kpeter@775
|
255 |
///
|
kpeter@775
|
256 |
/// This function erases all nodes and arcs from the digraph.
|
kpeter@775
|
257 |
void clear() {
|
kpeter@775
|
258 |
Parent::clear();
|
kpeter@775
|
259 |
}
|
kpeter@775
|
260 |
|
kpeter@775
|
261 |
/// \brief Build the digraph copying another digraph.
|
kpeter@775
|
262 |
///
|
kpeter@775
|
263 |
/// This function builds the digraph copying another digraph of any
|
kpeter@775
|
264 |
/// kind. It can be called more than once, but in such case, the whole
|
kpeter@775
|
265 |
/// structure and all maps will be cleared and rebuilt.
|
kpeter@775
|
266 |
///
|
kpeter@775
|
267 |
/// This method also makes possible to copy a digraph to a StaticDigraph
|
kpeter@775
|
268 |
/// structure using \ref DigraphCopy.
|
kpeter@775
|
269 |
///
|
kpeter@775
|
270 |
/// \param digraph An existing digraph to be copied.
|
kpeter@775
|
271 |
/// \param nodeRef The node references will be copied into this map.
|
kpeter@775
|
272 |
/// Its key type must be \c Digraph::Node and its value type must be
|
kpeter@775
|
273 |
/// \c StaticDigraph::Node.
|
kpeter@775
|
274 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
|
kpeter@775
|
275 |
/// concept.
|
kpeter@775
|
276 |
/// \param arcRef The arc references will be copied into this map.
|
kpeter@775
|
277 |
/// Its key type must be \c Digraph::Arc and its value type must be
|
kpeter@775
|
278 |
/// \c StaticDigraph::Arc.
|
kpeter@775
|
279 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@775
|
280 |
///
|
kpeter@775
|
281 |
/// \note If you do not need the arc references, then you could use
|
kpeter@775
|
282 |
/// \ref NullMap for the last parameter. However the node references
|
kpeter@775
|
283 |
/// are required by the function itself, thus they must be readable
|
kpeter@775
|
284 |
/// from the map.
|
kpeter@774
|
285 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
|
kpeter@774
|
286 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
kpeter@774
|
287 |
if (built) Parent::clear();
|
kpeter@774
|
288 |
Parent::build(digraph, nodeRef, arcRef);
|
kpeter@774
|
289 |
}
|
kpeter@774
|
290 |
|
kpeter@773
|
291 |
|
kpeter@773
|
292 |
protected:
|
kpeter@773
|
293 |
|
kpeter@773
|
294 |
using Parent::fastFirstOut;
|
kpeter@773
|
295 |
using Parent::fastNextOut;
|
kpeter@773
|
296 |
using Parent::fastLastOut;
|
kpeter@773
|
297 |
|
kpeter@773
|
298 |
public:
|
kpeter@773
|
299 |
|
kpeter@773
|
300 |
class OutArcIt : public Arc {
|
kpeter@773
|
301 |
public:
|
kpeter@773
|
302 |
|
kpeter@773
|
303 |
OutArcIt() { }
|
kpeter@773
|
304 |
|
kpeter@773
|
305 |
OutArcIt(Invalid i) : Arc(i) { }
|
kpeter@773
|
306 |
|
kpeter@773
|
307 |
OutArcIt(const StaticDigraph& digraph, const Node& node) {
|
kpeter@773
|
308 |
digraph.fastFirstOut(*this, node);
|
kpeter@773
|
309 |
digraph.fastLastOut(last, node);
|
kpeter@773
|
310 |
if (last == *this) *this = INVALID;
|
kpeter@773
|
311 |
}
|
kpeter@773
|
312 |
|
kpeter@773
|
313 |
OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
|
kpeter@773
|
314 |
if (arc != INVALID) {
|
kpeter@773
|
315 |
digraph.fastLastOut(last, digraph.source(arc));
|
kpeter@773
|
316 |
}
|
kpeter@773
|
317 |
}
|
kpeter@773
|
318 |
|
kpeter@773
|
319 |
OutArcIt& operator++() {
|
kpeter@773
|
320 |
StaticDigraph::fastNextOut(*this);
|
kpeter@773
|
321 |
if (last == *this) *this = INVALID;
|
kpeter@773
|
322 |
return *this;
|
kpeter@773
|
323 |
}
|
kpeter@773
|
324 |
|
kpeter@773
|
325 |
private:
|
kpeter@773
|
326 |
Arc last;
|
kpeter@773
|
327 |
};
|
kpeter@773
|
328 |
|
kpeter@774
|
329 |
Node baseNode(const OutArcIt &arc) const {
|
kpeter@774
|
330 |
return Parent::source(static_cast<const Arc&>(arc));
|
kpeter@774
|
331 |
}
|
kpeter@774
|
332 |
|
kpeter@774
|
333 |
Node runningNode(const OutArcIt &arc) const {
|
kpeter@774
|
334 |
return Parent::target(static_cast<const Arc&>(arc));
|
kpeter@774
|
335 |
}
|
kpeter@774
|
336 |
|
kpeter@774
|
337 |
Node baseNode(const InArcIt &arc) const {
|
kpeter@774
|
338 |
return Parent::target(static_cast<const Arc&>(arc));
|
kpeter@774
|
339 |
}
|
kpeter@774
|
340 |
|
kpeter@774
|
341 |
Node runningNode(const InArcIt &arc) const {
|
kpeter@774
|
342 |
return Parent::source(static_cast<const Arc&>(arc));
|
kpeter@774
|
343 |
}
|
kpeter@774
|
344 |
|
kpeter@773
|
345 |
};
|
kpeter@773
|
346 |
|
kpeter@773
|
347 |
}
|
kpeter@773
|
348 |
|
kpeter@773
|
349 |
#endif
|