... | ... |
@@ -323,155 +323,157 @@ |
323 | 323 |
_tmap.set(refMap[it], _map[it]); |
324 | 324 |
} |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
private: |
328 | 328 |
const FromMap& _map; |
329 | 329 |
ToMap& _tmap; |
330 | 330 |
}; |
331 | 331 |
|
332 | 332 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
333 | 333 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
334 | 334 |
public: |
335 | 335 |
|
336 | 336 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} |
337 | 337 |
|
338 | 338 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
339 | 339 |
_it = refMap[_item]; |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
private: |
343 | 343 |
Item _item; |
344 | 344 |
It& _it; |
345 | 345 |
}; |
346 | 346 |
|
347 | 347 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
348 | 348 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
349 | 349 |
public: |
350 | 350 |
|
351 | 351 |
RefCopy(Ref& map) : _map(map) {} |
352 | 352 |
|
353 | 353 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
354 | 354 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
355 | 355 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
356 | 356 |
_map.set(it, refMap[it]); |
357 | 357 |
} |
358 | 358 |
} |
359 | 359 |
|
360 | 360 |
private: |
361 | 361 |
Ref& _map; |
362 | 362 |
}; |
363 | 363 |
|
364 | 364 |
template <typename Digraph, typename Item, typename RefMap, |
365 | 365 |
typename CrossRef> |
366 | 366 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
367 | 367 |
public: |
368 | 368 |
|
369 | 369 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
370 | 370 |
|
371 | 371 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
372 | 372 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
373 | 373 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
374 | 374 |
_cmap.set(refMap[it], it); |
375 | 375 |
} |
376 | 376 |
} |
377 | 377 |
|
378 | 378 |
private: |
379 | 379 |
CrossRef& _cmap; |
380 | 380 |
}; |
381 | 381 |
|
382 | 382 |
template <typename Digraph, typename Enable = void> |
383 | 383 |
struct DigraphCopySelector { |
384 | 384 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
385 | 385 |
static void copy(const From& from, Digraph &to, |
386 | 386 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
387 |
to.clear(); |
|
387 | 388 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
388 | 389 |
nodeRefMap[it] = to.addNode(); |
389 | 390 |
} |
390 | 391 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
391 | 392 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
392 | 393 |
nodeRefMap[from.target(it)]); |
393 | 394 |
} |
394 | 395 |
} |
395 | 396 |
}; |
396 | 397 |
|
397 | 398 |
template <typename Digraph> |
398 | 399 |
struct DigraphCopySelector< |
399 | 400 |
Digraph, |
400 | 401 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
401 | 402 |
{ |
402 | 403 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
403 | 404 |
static void copy(const From& from, Digraph &to, |
404 | 405 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
405 | 406 |
to.build(from, nodeRefMap, arcRefMap); |
406 | 407 |
} |
407 | 408 |
}; |
408 | 409 |
|
409 | 410 |
template <typename Graph, typename Enable = void> |
410 | 411 |
struct GraphCopySelector { |
411 | 412 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
412 | 413 |
static void copy(const From& from, Graph &to, |
413 | 414 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
415 |
to.clear(); |
|
414 | 416 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
415 | 417 |
nodeRefMap[it] = to.addNode(); |
416 | 418 |
} |
417 | 419 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
418 | 420 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
419 | 421 |
nodeRefMap[from.v(it)]); |
420 | 422 |
} |
421 | 423 |
} |
422 | 424 |
}; |
423 | 425 |
|
424 | 426 |
template <typename Graph> |
425 | 427 |
struct GraphCopySelector< |
426 | 428 |
Graph, |
427 | 429 |
typename enable_if<typename Graph::BuildTag, void>::type> |
428 | 430 |
{ |
429 | 431 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
430 | 432 |
static void copy(const From& from, Graph &to, |
431 | 433 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
432 | 434 |
to.build(from, nodeRefMap, edgeRefMap); |
433 | 435 |
} |
434 | 436 |
}; |
435 | 437 |
|
436 | 438 |
} |
437 | 439 |
|
438 | 440 |
/// \brief Class to copy a digraph. |
439 | 441 |
/// |
440 | 442 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
441 | 443 |
/// simplest way of using it is through the \c digraphCopy() function. |
442 | 444 |
/// |
443 | 445 |
/// This class not only make a copy of a digraph, but it can create |
444 | 446 |
/// references and cross references between the nodes and arcs of |
445 | 447 |
/// the two digraphs, and it can copy maps to use with the newly created |
446 | 448 |
/// digraph. |
447 | 449 |
/// |
448 | 450 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
449 | 451 |
/// should be created, then the data belongs to the digraph should |
450 | 452 |
/// assigned to copy. In the end, the \c run() member should be |
451 | 453 |
/// called. |
452 | 454 |
/// |
453 | 455 |
/// The next code copies a digraph with several data: |
454 | 456 |
///\code |
455 | 457 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
456 | 458 |
/// // Create references for the nodes |
457 | 459 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
458 | 460 |
/// cg.nodeRef(nr); |
459 | 461 |
/// // Create cross references (inverse) for the arcs |
460 | 462 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
461 | 463 |
/// cg.arcCrossRef(acr); |
462 | 464 |
/// // Copy an arc map |
463 | 465 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
464 | 466 |
/// NewGraph::ArcMap<double> namap(new_graph); |
465 | 467 |
/// cg.arcMap(oamap, namap); |
466 | 468 |
/// // Copy a node |
467 | 469 |
/// OrigGraph::Node on; |
468 | 470 |
/// NewGraph::Node nn; |
469 | 471 |
/// cg.node(on, nn); |
470 | 472 |
/// // Execute copying |
471 | 473 |
/// cg.run(); |
472 | 474 |
///\endcode |
473 | 475 |
template <typename From, typename To> |
474 | 476 |
class DigraphCopy { |
475 | 477 |
private: |
476 | 478 |
|
477 | 479 |
typedef typename From::Node Node; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/smart_graph.h> |
20 | 20 |
#include <lemon/list_graph.h> |
21 | 21 |
#include <lemon/lgf_reader.h> |
22 | 22 |
#include <lemon/error.h> |
23 | 23 |
|
24 | 24 |
#include "test_tools.h" |
25 | 25 |
|
26 | 26 |
using namespace std; |
27 | 27 |
using namespace lemon; |
28 | 28 |
|
29 | 29 |
void digraph_copy_test() { |
30 | 30 |
const int nn = 10; |
31 | 31 |
|
32 |
// Build a digraph |
|
32 | 33 |
SmartDigraph from; |
33 | 34 |
SmartDigraph::NodeMap<int> fnm(from); |
34 | 35 |
SmartDigraph::ArcMap<int> fam(from); |
35 | 36 |
SmartDigraph::Node fn = INVALID; |
36 | 37 |
SmartDigraph::Arc fa = INVALID; |
37 | 38 |
|
38 | 39 |
std::vector<SmartDigraph::Node> fnv; |
39 | 40 |
for (int i = 0; i < nn; ++i) { |
40 | 41 |
SmartDigraph::Node node = from.addNode(); |
41 | 42 |
fnv.push_back(node); |
42 | 43 |
fnm[node] = i * i; |
43 | 44 |
if (i == 0) fn = node; |
44 | 45 |
} |
45 | 46 |
|
46 | 47 |
for (int i = 0; i < nn; ++i) { |
47 | 48 |
for (int j = 0; j < nn; ++j) { |
48 | 49 |
SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); |
49 | 50 |
fam[arc] = i + j * j; |
50 | 51 |
if (i == 0 && j == 0) fa = arc; |
51 | 52 |
} |
52 | 53 |
} |
53 | 54 |
|
55 |
// Test digraph copy |
|
54 | 56 |
ListDigraph to; |
55 | 57 |
ListDigraph::NodeMap<int> tnm(to); |
56 | 58 |
ListDigraph::ArcMap<int> tam(to); |
57 | 59 |
ListDigraph::Node tn; |
58 | 60 |
ListDigraph::Arc ta; |
59 | 61 |
|
60 | 62 |
SmartDigraph::NodeMap<ListDigraph::Node> nr(from); |
61 | 63 |
SmartDigraph::ArcMap<ListDigraph::Arc> er(from); |
62 | 64 |
|
63 | 65 |
ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
64 | 66 |
ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
65 | 67 |
|
66 | 68 |
digraphCopy(from, to). |
67 | 69 |
nodeMap(fnm, tnm).arcMap(fam, tam). |
68 | 70 |
nodeRef(nr).arcRef(er). |
69 | 71 |
nodeCrossRef(ncr).arcCrossRef(ecr). |
70 | 72 |
node(fn, tn).arc(fa, ta).run(); |
73 |
|
|
74 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
75 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
71 | 76 |
|
72 | 77 |
for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) { |
73 | 78 |
check(ncr[nr[it]] == it, "Wrong copy."); |
74 | 79 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
75 | 80 |
} |
76 | 81 |
|
77 | 82 |
for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) { |
78 | 83 |
check(ecr[er[it]] == it, "Wrong copy."); |
79 | 84 |
check(fam[it] == tam[er[it]], "Wrong copy."); |
80 | 85 |
check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); |
81 | 86 |
check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); |
82 | 87 |
} |
83 | 88 |
|
84 | 89 |
for (ListDigraph::NodeIt it(to); it != INVALID; ++it) { |
85 | 90 |
check(nr[ncr[it]] == it, "Wrong copy."); |
86 | 91 |
} |
87 | 92 |
|
88 | 93 |
for (ListDigraph::ArcIt it(to); it != INVALID; ++it) { |
89 | 94 |
check(er[ecr[it]] == it, "Wrong copy."); |
90 | 95 |
} |
91 | 96 |
check(tn == nr[fn], "Wrong copy."); |
92 | 97 |
check(ta == er[fa], "Wrong copy."); |
98 |
|
|
99 |
// Test repeated copy |
|
100 |
digraphCopy(from, to).run(); |
|
101 |
|
|
102 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
103 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
93 | 104 |
} |
94 | 105 |
|
95 | 106 |
void graph_copy_test() { |
96 | 107 |
const int nn = 10; |
97 | 108 |
|
109 |
// Build a graph |
|
98 | 110 |
SmartGraph from; |
99 | 111 |
SmartGraph::NodeMap<int> fnm(from); |
100 | 112 |
SmartGraph::ArcMap<int> fam(from); |
101 | 113 |
SmartGraph::EdgeMap<int> fem(from); |
102 | 114 |
SmartGraph::Node fn = INVALID; |
103 | 115 |
SmartGraph::Arc fa = INVALID; |
104 | 116 |
SmartGraph::Edge fe = INVALID; |
105 | 117 |
|
106 | 118 |
std::vector<SmartGraph::Node> fnv; |
107 | 119 |
for (int i = 0; i < nn; ++i) { |
108 | 120 |
SmartGraph::Node node = from.addNode(); |
109 | 121 |
fnv.push_back(node); |
110 | 122 |
fnm[node] = i * i; |
111 | 123 |
if (i == 0) fn = node; |
112 | 124 |
} |
113 | 125 |
|
114 | 126 |
for (int i = 0; i < nn; ++i) { |
115 | 127 |
for (int j = 0; j < nn; ++j) { |
116 | 128 |
SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); |
117 | 129 |
fem[edge] = i * i + j * j; |
118 | 130 |
fam[from.direct(edge, true)] = i + j * j; |
119 | 131 |
fam[from.direct(edge, false)] = i * i + j; |
120 | 132 |
if (i == 0 && j == 0) fa = from.direct(edge, true); |
121 | 133 |
if (i == 0 && j == 0) fe = edge; |
122 | 134 |
} |
123 | 135 |
} |
124 | 136 |
|
137 |
// Test graph copy |
|
125 | 138 |
ListGraph to; |
126 | 139 |
ListGraph::NodeMap<int> tnm(to); |
127 | 140 |
ListGraph::ArcMap<int> tam(to); |
128 | 141 |
ListGraph::EdgeMap<int> tem(to); |
129 | 142 |
ListGraph::Node tn; |
130 | 143 |
ListGraph::Arc ta; |
131 | 144 |
ListGraph::Edge te; |
132 | 145 |
|
133 | 146 |
SmartGraph::NodeMap<ListGraph::Node> nr(from); |
134 | 147 |
SmartGraph::ArcMap<ListGraph::Arc> ar(from); |
135 | 148 |
SmartGraph::EdgeMap<ListGraph::Edge> er(from); |
136 | 149 |
|
137 | 150 |
ListGraph::NodeMap<SmartGraph::Node> ncr(to); |
138 | 151 |
ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
139 | 152 |
ListGraph::EdgeMap<SmartGraph::Edge> ecr(to); |
140 | 153 |
|
141 | 154 |
graphCopy(from, to). |
142 | 155 |
nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
143 | 156 |
nodeRef(nr).arcRef(ar).edgeRef(er). |
144 | 157 |
nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
145 | 158 |
node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
146 | 159 |
|
160 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
161 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
|
162 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
163 |
|
|
147 | 164 |
for (SmartGraph::NodeIt it(from); it != INVALID; ++it) { |
148 | 165 |
check(ncr[nr[it]] == it, "Wrong copy."); |
149 | 166 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
150 | 167 |
} |
151 | 168 |
|
152 | 169 |
for (SmartGraph::ArcIt it(from); it != INVALID; ++it) { |
153 | 170 |
check(acr[ar[it]] == it, "Wrong copy."); |
154 | 171 |
check(fam[it] == tam[ar[it]], "Wrong copy."); |
155 | 172 |
check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); |
156 | 173 |
check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); |
157 | 174 |
} |
158 | 175 |
|
159 | 176 |
for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) { |
160 | 177 |
check(ecr[er[it]] == it, "Wrong copy."); |
161 | 178 |
check(fem[it] == tem[er[it]], "Wrong copy."); |
162 | 179 |
check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), |
163 | 180 |
"Wrong copy."); |
164 | 181 |
check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), |
165 | 182 |
"Wrong copy."); |
166 | 183 |
check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), |
167 | 184 |
"Wrong copy."); |
168 | 185 |
} |
169 | 186 |
|
170 | 187 |
for (ListGraph::NodeIt it(to); it != INVALID; ++it) { |
171 | 188 |
check(nr[ncr[it]] == it, "Wrong copy."); |
172 | 189 |
} |
173 | 190 |
|
174 | 191 |
for (ListGraph::ArcIt it(to); it != INVALID; ++it) { |
175 | 192 |
check(ar[acr[it]] == it, "Wrong copy."); |
176 | 193 |
} |
177 | 194 |
for (ListGraph::EdgeIt it(to); it != INVALID; ++it) { |
178 | 195 |
check(er[ecr[it]] == it, "Wrong copy."); |
179 | 196 |
} |
180 | 197 |
check(tn == nr[fn], "Wrong copy."); |
181 | 198 |
check(ta == ar[fa], "Wrong copy."); |
182 | 199 |
check(te == er[fe], "Wrong copy."); |
200 |
|
|
201 |
// Test repeated copy |
|
202 |
graphCopy(from, to).run(); |
|
203 |
|
|
204 |
check(countNodes(from) == countNodes(to), "Wrong copy."); |
|
205 |
check(countEdges(from) == countEdges(to), "Wrong copy."); |
|
206 |
check(countArcs(from) == countArcs(to), "Wrong copy."); |
|
183 | 207 |
} |
184 | 208 |
|
185 | 209 |
|
186 | 210 |
int main() { |
187 | 211 |
digraph_copy_test(); |
188 | 212 |
graph_copy_test(); |
189 | 213 |
|
190 | 214 |
return 0; |
191 | 215 |
} |
0 comments (0 inline)