kpeter@364
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@364
|
2 |
*
|
kpeter@364
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@364
|
4 |
*
|
alpar@440
|
5 |
* Copyright (C) 2003-2009
|
kpeter@364
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@364
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@364
|
8 |
*
|
kpeter@364
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@364
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@364
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@364
|
12 |
*
|
kpeter@364
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@364
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@364
|
15 |
* purpose.
|
kpeter@364
|
16 |
*
|
kpeter@364
|
17 |
*/
|
kpeter@364
|
18 |
|
kpeter@364
|
19 |
#ifndef HYPERCUBE_GRAPH_H
|
kpeter@364
|
20 |
#define HYPERCUBE_GRAPH_H
|
kpeter@364
|
21 |
|
kpeter@364
|
22 |
#include <vector>
|
kpeter@364
|
23 |
#include <lemon/core.h>
|
kpeter@365
|
24 |
#include <lemon/assert.h>
|
kpeter@364
|
25 |
#include <lemon/bits/graph_extender.h>
|
kpeter@364
|
26 |
|
kpeter@364
|
27 |
///\ingroup graphs
|
kpeter@364
|
28 |
///\file
|
kpeter@365
|
29 |
///\brief HypercubeGraph class.
|
kpeter@364
|
30 |
|
kpeter@364
|
31 |
namespace lemon {
|
kpeter@364
|
32 |
|
kpeter@365
|
33 |
class HypercubeGraphBase {
|
kpeter@364
|
34 |
|
kpeter@364
|
35 |
public:
|
kpeter@364
|
36 |
|
kpeter@365
|
37 |
typedef HypercubeGraphBase Graph;
|
kpeter@364
|
38 |
|
kpeter@364
|
39 |
class Node;
|
kpeter@365
|
40 |
class Edge;
|
kpeter@364
|
41 |
class Arc;
|
kpeter@364
|
42 |
|
kpeter@364
|
43 |
public:
|
kpeter@364
|
44 |
|
kpeter@365
|
45 |
HypercubeGraphBase() {}
|
kpeter@364
|
46 |
|
kpeter@364
|
47 |
protected:
|
kpeter@364
|
48 |
|
kpeter@364
|
49 |
void construct(int dim) {
|
kpeter@365
|
50 |
LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1.");
|
kpeter@364
|
51 |
_dim = dim;
|
kpeter@365
|
52 |
_node_num = 1 << dim;
|
alpar@372
|
53 |
_edge_num = dim * (1 << (dim-1));
|
kpeter@364
|
54 |
}
|
kpeter@364
|
55 |
|
kpeter@364
|
56 |
public:
|
kpeter@364
|
57 |
|
kpeter@364
|
58 |
typedef True NodeNumTag;
|
kpeter@365
|
59 |
typedef True EdgeNumTag;
|
kpeter@364
|
60 |
typedef True ArcNumTag;
|
kpeter@364
|
61 |
|
kpeter@365
|
62 |
int nodeNum() const { return _node_num; }
|
kpeter@365
|
63 |
int edgeNum() const { return _edge_num; }
|
kpeter@365
|
64 |
int arcNum() const { return 2 * _edge_num; }
|
kpeter@364
|
65 |
|
kpeter@365
|
66 |
int maxNodeId() const { return _node_num - 1; }
|
kpeter@365
|
67 |
int maxEdgeId() const { return _edge_num - 1; }
|
kpeter@365
|
68 |
int maxArcId() const { return 2 * _edge_num - 1; }
|
kpeter@364
|
69 |
|
kpeter@365
|
70 |
static Node nodeFromId(int id) { return Node(id); }
|
kpeter@365
|
71 |
static Edge edgeFromId(int id) { return Edge(id); }
|
kpeter@365
|
72 |
static Arc arcFromId(int id) { return Arc(id); }
|
kpeter@365
|
73 |
|
kpeter@365
|
74 |
static int id(Node node) { return node._id; }
|
kpeter@365
|
75 |
static int id(Edge edge) { return edge._id; }
|
kpeter@365
|
76 |
static int id(Arc arc) { return arc._id; }
|
kpeter@365
|
77 |
|
kpeter@365
|
78 |
Node u(Edge edge) const {
|
alpar@372
|
79 |
int base = edge._id & ((1 << (_dim-1)) - 1);
|
alpar@372
|
80 |
int k = edge._id >> (_dim-1);
|
alpar@372
|
81 |
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1));
|
kpeter@364
|
82 |
}
|
kpeter@364
|
83 |
|
kpeter@365
|
84 |
Node v(Edge edge) const {
|
alpar@372
|
85 |
int base = edge._id & ((1 << (_dim-1)) - 1);
|
alpar@372
|
86 |
int k = edge._id >> (_dim-1);
|
alpar@372
|
87 |
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)) | (1 << k);
|
kpeter@364
|
88 |
}
|
kpeter@364
|
89 |
|
kpeter@365
|
90 |
Node source(Arc arc) const {
|
kpeter@365
|
91 |
return (arc._id & 1) == 1 ? u(arc) : v(arc);
|
kpeter@365
|
92 |
}
|
kpeter@364
|
93 |
|
kpeter@365
|
94 |
Node target(Arc arc) const {
|
kpeter@365
|
95 |
return (arc._id & 1) == 1 ? v(arc) : u(arc);
|
kpeter@365
|
96 |
}
|
kpeter@364
|
97 |
|
kpeter@365
|
98 |
typedef True FindEdgeTag;
|
kpeter@365
|
99 |
typedef True FindArcTag;
|
kpeter@365
|
100 |
|
kpeter@365
|
101 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
kpeter@365
|
102 |
if (prev != INVALID) return INVALID;
|
kpeter@365
|
103 |
int d = u._id ^ v._id;
|
kpeter@365
|
104 |
int k = 0;
|
kpeter@365
|
105 |
if (d == 0) return INVALID;
|
kpeter@365
|
106 |
for ( ; (d & 1) == 0; d >>= 1) ++k;
|
kpeter@365
|
107 |
if (d >> 1 != 0) return INVALID;
|
alpar@372
|
108 |
return (k << (_dim-1)) | ((u._id >> (k+1)) << k) |
|
alpar@372
|
109 |
(u._id & ((1 << k) - 1));
|
kpeter@365
|
110 |
}
|
kpeter@365
|
111 |
|
kpeter@365
|
112 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const {
|
kpeter@365
|
113 |
Edge edge = findEdge(u, v, prev);
|
kpeter@365
|
114 |
if (edge == INVALID) return INVALID;
|
alpar@372
|
115 |
int k = edge._id >> (_dim-1);
|
kpeter@365
|
116 |
return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1;
|
kpeter@365
|
117 |
}
|
kpeter@364
|
118 |
|
kpeter@364
|
119 |
class Node {
|
kpeter@365
|
120 |
friend class HypercubeGraphBase;
|
kpeter@365
|
121 |
|
kpeter@364
|
122 |
protected:
|
kpeter@365
|
123 |
int _id;
|
kpeter@365
|
124 |
Node(int id) : _id(id) {}
|
kpeter@364
|
125 |
public:
|
kpeter@364
|
126 |
Node() {}
|
kpeter@365
|
127 |
Node (Invalid) : _id(-1) {}
|
kpeter@365
|
128 |
bool operator==(const Node node) const {return _id == node._id;}
|
kpeter@365
|
129 |
bool operator!=(const Node node) const {return _id != node._id;}
|
kpeter@365
|
130 |
bool operator<(const Node node) const {return _id < node._id;}
|
kpeter@365
|
131 |
};
|
kpeter@365
|
132 |
|
kpeter@365
|
133 |
class Edge {
|
kpeter@365
|
134 |
friend class HypercubeGraphBase;
|
kpeter@365
|
135 |
friend class Arc;
|
kpeter@365
|
136 |
|
kpeter@365
|
137 |
protected:
|
kpeter@365
|
138 |
int _id;
|
kpeter@365
|
139 |
|
kpeter@365
|
140 |
Edge(int id) : _id(id) {}
|
kpeter@365
|
141 |
|
kpeter@365
|
142 |
public:
|
kpeter@365
|
143 |
Edge() {}
|
kpeter@365
|
144 |
Edge (Invalid) : _id(-1) {}
|
kpeter@365
|
145 |
bool operator==(const Edge edge) const {return _id == edge._id;}
|
kpeter@365
|
146 |
bool operator!=(const Edge edge) const {return _id != edge._id;}
|
kpeter@365
|
147 |
bool operator<(const Edge edge) const {return _id < edge._id;}
|
kpeter@364
|
148 |
};
|
kpeter@364
|
149 |
|
kpeter@364
|
150 |
class Arc {
|
kpeter@365
|
151 |
friend class HypercubeGraphBase;
|
kpeter@365
|
152 |
|
kpeter@364
|
153 |
protected:
|
kpeter@365
|
154 |
int _id;
|
kpeter@365
|
155 |
|
kpeter@365
|
156 |
Arc(int id) : _id(id) {}
|
kpeter@365
|
157 |
|
kpeter@364
|
158 |
public:
|
kpeter@365
|
159 |
Arc() {}
|
kpeter@365
|
160 |
Arc (Invalid) : _id(-1) {}
|
kpeter@365
|
161 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
|
kpeter@365
|
162 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
kpeter@365
|
163 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
kpeter@365
|
164 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
kpeter@364
|
165 |
};
|
kpeter@364
|
166 |
|
kpeter@364
|
167 |
void first(Node& node) const {
|
kpeter@365
|
168 |
node._id = _node_num - 1;
|
kpeter@364
|
169 |
}
|
kpeter@364
|
170 |
|
kpeter@364
|
171 |
static void next(Node& node) {
|
kpeter@365
|
172 |
--node._id;
|
kpeter@365
|
173 |
}
|
kpeter@365
|
174 |
|
kpeter@365
|
175 |
void first(Edge& edge) const {
|
kpeter@365
|
176 |
edge._id = _edge_num - 1;
|
kpeter@365
|
177 |
}
|
kpeter@365
|
178 |
|
kpeter@365
|
179 |
static void next(Edge& edge) {
|
kpeter@365
|
180 |
--edge._id;
|
kpeter@364
|
181 |
}
|
kpeter@364
|
182 |
|
kpeter@364
|
183 |
void first(Arc& arc) const {
|
kpeter@365
|
184 |
arc._id = 2 * _edge_num - 1;
|
kpeter@364
|
185 |
}
|
kpeter@364
|
186 |
|
kpeter@364
|
187 |
static void next(Arc& arc) {
|
kpeter@365
|
188 |
--arc._id;
|
kpeter@365
|
189 |
}
|
kpeter@365
|
190 |
|
kpeter@365
|
191 |
void firstInc(Edge& edge, bool& dir, const Node& node) const {
|
kpeter@365
|
192 |
edge._id = node._id >> 1;
|
kpeter@365
|
193 |
dir = (node._id & 1) == 0;
|
kpeter@365
|
194 |
}
|
kpeter@365
|
195 |
|
kpeter@365
|
196 |
void nextInc(Edge& edge, bool& dir) const {
|
kpeter@365
|
197 |
Node n = dir ? u(edge) : v(edge);
|
alpar@372
|
198 |
int k = (edge._id >> (_dim-1)) + 1;
|
kpeter@365
|
199 |
if (k < _dim) {
|
alpar@372
|
200 |
edge._id = (k << (_dim-1)) |
|
alpar@372
|
201 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
|
kpeter@365
|
202 |
dir = ((n._id >> k) & 1) == 0;
|
kpeter@365
|
203 |
} else {
|
kpeter@365
|
204 |
edge._id = -1;
|
kpeter@365
|
205 |
dir = true;
|
kpeter@365
|
206 |
}
|
kpeter@364
|
207 |
}
|
kpeter@364
|
208 |
|
kpeter@364
|
209 |
void firstOut(Arc& arc, const Node& node) const {
|
kpeter@365
|
210 |
arc._id = ((node._id >> 1) << 1) | (~node._id & 1);
|
kpeter@364
|
211 |
}
|
kpeter@364
|
212 |
|
kpeter@364
|
213 |
void nextOut(Arc& arc) const {
|
kpeter@365
|
214 |
Node n = (arc._id & 1) == 1 ? u(arc) : v(arc);
|
kpeter@365
|
215 |
int k = (arc._id >> _dim) + 1;
|
kpeter@365
|
216 |
if (k < _dim) {
|
alpar@372
|
217 |
arc._id = (k << (_dim-1)) |
|
alpar@372
|
218 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
|
kpeter@365
|
219 |
arc._id = (arc._id << 1) | (~(n._id >> k) & 1);
|
kpeter@365
|
220 |
} else {
|
kpeter@365
|
221 |
arc._id = -1;
|
kpeter@365
|
222 |
}
|
kpeter@364
|
223 |
}
|
kpeter@364
|
224 |
|
kpeter@364
|
225 |
void firstIn(Arc& arc, const Node& node) const {
|
kpeter@365
|
226 |
arc._id = ((node._id >> 1) << 1) | (node._id & 1);
|
kpeter@364
|
227 |
}
|
kpeter@364
|
228 |
|
kpeter@364
|
229 |
void nextIn(Arc& arc) const {
|
kpeter@365
|
230 |
Node n = (arc._id & 1) == 1 ? v(arc) : u(arc);
|
kpeter@365
|
231 |
int k = (arc._id >> _dim) + 1;
|
kpeter@365
|
232 |
if (k < _dim) {
|
alpar@372
|
233 |
arc._id = (k << (_dim-1)) |
|
alpar@372
|
234 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1));
|
kpeter@365
|
235 |
arc._id = (arc._id << 1) | ((n._id >> k) & 1);
|
kpeter@364
|
236 |
} else {
|
kpeter@365
|
237 |
arc._id = -1;
|
kpeter@364
|
238 |
}
|
kpeter@364
|
239 |
}
|
kpeter@364
|
240 |
|
kpeter@365
|
241 |
static bool direction(Arc arc) {
|
kpeter@365
|
242 |
return (arc._id & 1) == 1;
|
kpeter@365
|
243 |
}
|
kpeter@365
|
244 |
|
kpeter@365
|
245 |
static Arc direct(Edge edge, bool dir) {
|
kpeter@365
|
246 |
return Arc((edge._id << 1) | (dir ? 1 : 0));
|
kpeter@365
|
247 |
}
|
kpeter@365
|
248 |
|
kpeter@364
|
249 |
int dimension() const {
|
kpeter@364
|
250 |
return _dim;
|
kpeter@364
|
251 |
}
|
kpeter@364
|
252 |
|
kpeter@364
|
253 |
bool projection(Node node, int n) const {
|
kpeter@365
|
254 |
return static_cast<bool>(node._id & (1 << n));
|
kpeter@365
|
255 |
}
|
kpeter@365
|
256 |
|
kpeter@365
|
257 |
int dimension(Edge edge) const {
|
alpar@372
|
258 |
return edge._id >> (_dim-1);
|
kpeter@364
|
259 |
}
|
kpeter@364
|
260 |
|
kpeter@364
|
261 |
int dimension(Arc arc) const {
|
kpeter@365
|
262 |
return arc._id >> _dim;
|
kpeter@364
|
263 |
}
|
kpeter@364
|
264 |
|
kpeter@364
|
265 |
int index(Node node) const {
|
kpeter@365
|
266 |
return node._id;
|
kpeter@364
|
267 |
}
|
kpeter@364
|
268 |
|
kpeter@364
|
269 |
Node operator()(int ix) const {
|
kpeter@364
|
270 |
return Node(ix);
|
kpeter@364
|
271 |
}
|
kpeter@364
|
272 |
|
kpeter@364
|
273 |
private:
|
kpeter@365
|
274 |
int _dim;
|
kpeter@365
|
275 |
int _node_num, _edge_num;
|
kpeter@364
|
276 |
};
|
kpeter@364
|
277 |
|
kpeter@364
|
278 |
|
kpeter@365
|
279 |
typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase;
|
kpeter@364
|
280 |
|
kpeter@365
|
281 |
/// \ingroup graphs
|
kpeter@364
|
282 |
///
|
kpeter@365
|
283 |
/// \brief Hypercube graph class
|
kpeter@364
|
284 |
///
|
kpeter@735
|
285 |
/// HypercubeGraph implements a special graph type. The nodes of the
|
kpeter@735
|
286 |
/// graph are indexed with integers having at most \c dim binary digits.
|
kpeter@365
|
287 |
/// Two nodes are connected in the graph if and only if their indices
|
kpeter@365
|
288 |
/// differ only on one position in the binary form.
|
kpeter@735
|
289 |
/// This class is completely static and it needs constant memory space.
|
kpeter@735
|
290 |
/// Thus you can neither add nor delete nodes or edges.
|
kpeter@735
|
291 |
///
|
kpeter@735
|
292 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept".
|
kpeter@735
|
293 |
/// Most of its member functions and nested classes are documented
|
kpeter@735
|
294 |
/// only in the concept class.
|
kpeter@364
|
295 |
///
|
kpeter@365
|
296 |
/// \note The type of the indices is chosen to \c int for efficiency
|
kpeter@365
|
297 |
/// reasons. Thus the maximum dimension of this implementation is 26
|
kpeter@365
|
298 |
/// (assuming that the size of \c int is 32 bit).
|
kpeter@365
|
299 |
class HypercubeGraph : public ExtendedHypercubeGraphBase {
|
kpeter@617
|
300 |
typedef ExtendedHypercubeGraphBase Parent;
|
kpeter@617
|
301 |
|
kpeter@364
|
302 |
public:
|
kpeter@364
|
303 |
|
kpeter@365
|
304 |
/// \brief Constructs a hypercube graph with \c dim dimensions.
|
kpeter@364
|
305 |
///
|
kpeter@365
|
306 |
/// Constructs a hypercube graph with \c dim dimensions.
|
kpeter@365
|
307 |
HypercubeGraph(int dim) { construct(dim); }
|
kpeter@364
|
308 |
|
kpeter@365
|
309 |
/// \brief The number of dimensions.
|
kpeter@364
|
310 |
///
|
kpeter@365
|
311 |
/// Gives back the number of dimensions.
|
kpeter@364
|
312 |
int dimension() const {
|
kpeter@364
|
313 |
return Parent::dimension();
|
kpeter@364
|
314 |
}
|
kpeter@364
|
315 |
|
kpeter@365
|
316 |
/// \brief Returns \c true if the n'th bit of the node is one.
|
kpeter@364
|
317 |
///
|
kpeter@365
|
318 |
/// Returns \c true if the n'th bit of the node is one.
|
kpeter@364
|
319 |
bool projection(Node node, int n) const {
|
kpeter@364
|
320 |
return Parent::projection(node, n);
|
kpeter@364
|
321 |
}
|
kpeter@364
|
322 |
|
kpeter@365
|
323 |
/// \brief The dimension id of an edge.
|
kpeter@364
|
324 |
///
|
kpeter@365
|
325 |
/// Gives back the dimension id of the given edge.
|
kpeter@735
|
326 |
/// It is in the range <tt>[0..dim-1]</tt>.
|
kpeter@365
|
327 |
int dimension(Edge edge) const {
|
kpeter@365
|
328 |
return Parent::dimension(edge);
|
kpeter@365
|
329 |
}
|
kpeter@365
|
330 |
|
kpeter@365
|
331 |
/// \brief The dimension id of an arc.
|
kpeter@365
|
332 |
///
|
kpeter@365
|
333 |
/// Gives back the dimension id of the given arc.
|
kpeter@735
|
334 |
/// It is in the range <tt>[0..dim-1]</tt>.
|
kpeter@364
|
335 |
int dimension(Arc arc) const {
|
kpeter@364
|
336 |
return Parent::dimension(arc);
|
kpeter@364
|
337 |
}
|
kpeter@364
|
338 |
|
kpeter@365
|
339 |
/// \brief The index of a node.
|
kpeter@364
|
340 |
///
|
kpeter@365
|
341 |
/// Gives back the index of the given node.
|
kpeter@365
|
342 |
/// The lower bits of the integer describes the node.
|
kpeter@364
|
343 |
int index(Node node) const {
|
kpeter@364
|
344 |
return Parent::index(node);
|
kpeter@364
|
345 |
}
|
kpeter@364
|
346 |
|
kpeter@365
|
347 |
/// \brief Gives back a node by its index.
|
kpeter@364
|
348 |
///
|
kpeter@365
|
349 |
/// Gives back a node by its index.
|
kpeter@364
|
350 |
Node operator()(int ix) const {
|
kpeter@364
|
351 |
return Parent::operator()(ix);
|
kpeter@364
|
352 |
}
|
kpeter@364
|
353 |
|
kpeter@364
|
354 |
/// \brief Number of nodes.
|
kpeter@364
|
355 |
int nodeNum() const { return Parent::nodeNum(); }
|
kpeter@365
|
356 |
/// \brief Number of edges.
|
kpeter@365
|
357 |
int edgeNum() const { return Parent::edgeNum(); }
|
kpeter@364
|
358 |
/// \brief Number of arcs.
|
kpeter@364
|
359 |
int arcNum() const { return Parent::arcNum(); }
|
kpeter@364
|
360 |
|
kpeter@364
|
361 |
/// \brief Linear combination map.
|
kpeter@364
|
362 |
///
|
kpeter@365
|
363 |
/// This map makes possible to give back a linear combination
|
kpeter@365
|
364 |
/// for each node. It works like the \c std::accumulate function,
|
kpeter@365
|
365 |
/// so it accumulates the \c bf binary function with the \c fv first
|
kpeter@365
|
366 |
/// value. The map accumulates only on that positions (dimensions)
|
kpeter@365
|
367 |
/// where the index of the node is one. The values that have to be
|
kpeter@365
|
368 |
/// accumulated should be given by the \c begin and \c end iterators
|
kpeter@365
|
369 |
/// and the length of this range should be equal to the dimension
|
kpeter@365
|
370 |
/// number of the graph.
|
kpeter@364
|
371 |
///
|
kpeter@364
|
372 |
///\code
|
kpeter@364
|
373 |
/// const int DIM = 3;
|
kpeter@365
|
374 |
/// HypercubeGraph graph(DIM);
|
kpeter@364
|
375 |
/// dim2::Point<double> base[DIM];
|
kpeter@364
|
376 |
/// for (int k = 0; k < DIM; ++k) {
|
kpeter@364
|
377 |
/// base[k].x = rnd();
|
kpeter@364
|
378 |
/// base[k].y = rnd();
|
kpeter@364
|
379 |
/// }
|
kpeter@365
|
380 |
/// HypercubeGraph::HyperMap<dim2::Point<double> >
|
kpeter@365
|
381 |
/// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0));
|
kpeter@364
|
382 |
///\endcode
|
kpeter@364
|
383 |
///
|
kpeter@365
|
384 |
/// \see HypercubeGraph
|
kpeter@364
|
385 |
template <typename T, typename BF = std::plus<T> >
|
kpeter@364
|
386 |
class HyperMap {
|
kpeter@364
|
387 |
public:
|
kpeter@364
|
388 |
|
kpeter@365
|
389 |
/// \brief The key type of the map
|
kpeter@364
|
390 |
typedef Node Key;
|
kpeter@365
|
391 |
/// \brief The value type of the map
|
kpeter@364
|
392 |
typedef T Value;
|
kpeter@364
|
393 |
|
kpeter@364
|
394 |
/// \brief Constructor for HyperMap.
|
kpeter@364
|
395 |
///
|
kpeter@365
|
396 |
/// Construct a HyperMap for the given graph. The values that have
|
kpeter@365
|
397 |
/// to be accumulated should be given by the \c begin and \c end
|
kpeter@365
|
398 |
/// iterators and the length of this range should be equal to the
|
kpeter@365
|
399 |
/// dimension number of the graph.
|
kpeter@364
|
400 |
///
|
kpeter@365
|
401 |
/// This map accumulates the \c bf binary function with the \c fv
|
kpeter@365
|
402 |
/// first value on that positions (dimensions) where the index of
|
kpeter@365
|
403 |
/// the node is one.
|
kpeter@364
|
404 |
template <typename It>
|
kpeter@365
|
405 |
HyperMap(const Graph& graph, It begin, It end,
|
kpeter@365
|
406 |
T fv = 0, const BF& bf = BF())
|
kpeter@365
|
407 |
: _graph(graph), _values(begin, end), _first_value(fv), _bin_func(bf)
|
kpeter@364
|
408 |
{
|
kpeter@365
|
409 |
LEMON_ASSERT(_values.size() == graph.dimension(),
|
kpeter@365
|
410 |
"Wrong size of range");
|
kpeter@364
|
411 |
}
|
kpeter@364
|
412 |
|
kpeter@365
|
413 |
/// \brief The partial accumulated value.
|
kpeter@364
|
414 |
///
|
kpeter@364
|
415 |
/// Gives back the partial accumulated value.
|
kpeter@365
|
416 |
Value operator[](const Key& k) const {
|
kpeter@364
|
417 |
Value val = _first_value;
|
kpeter@364
|
418 |
int id = _graph.index(k);
|
kpeter@364
|
419 |
int n = 0;
|
kpeter@364
|
420 |
while (id != 0) {
|
kpeter@364
|
421 |
if (id & 1) {
|
kpeter@364
|
422 |
val = _bin_func(val, _values[n]);
|
kpeter@364
|
423 |
}
|
kpeter@364
|
424 |
id >>= 1;
|
kpeter@364
|
425 |
++n;
|
kpeter@364
|
426 |
}
|
kpeter@364
|
427 |
return val;
|
kpeter@364
|
428 |
}
|
kpeter@364
|
429 |
|
kpeter@364
|
430 |
private:
|
kpeter@365
|
431 |
const Graph& _graph;
|
kpeter@364
|
432 |
std::vector<T> _values;
|
kpeter@364
|
433 |
T _first_value;
|
kpeter@364
|
434 |
BF _bin_func;
|
kpeter@364
|
435 |
};
|
kpeter@364
|
436 |
|
kpeter@364
|
437 |
};
|
kpeter@364
|
438 |
|
kpeter@364
|
439 |
}
|
kpeter@364
|
440 |
|
kpeter@364
|
441 |
#endif
|