0
6
2
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#include <iostream> |
|
20 |
#include <vector> |
|
21 |
|
|
22 |
#include <lemon/graph_utils.h> |
|
23 |
|
|
24 |
#include <lemon/list_graph.h> |
|
25 |
#include <lemon/smart_graph.h> |
|
26 |
|
|
27 |
#include "test_tools.h" |
|
28 |
#include "graph_utils_test.h" |
|
29 |
|
|
30 |
|
|
31 |
using namespace lemon; |
|
32 |
|
|
33 |
template <class Graph> |
|
34 |
void checkSnapDeg() |
|
35 |
{ |
|
36 |
Graph g; |
|
37 |
typename Graph::Node n1=g.addNode(); |
|
38 |
typename Graph::Node n2=g.addNode(); |
|
39 |
|
|
40 |
InDegMap<Graph> ind(g); |
|
41 |
|
|
42 |
g.addArc(n1,n2); |
|
43 |
|
|
44 |
typename Graph::Snapshot snap(g); |
|
45 |
|
|
46 |
OutDegMap<Graph> outd(g); |
|
47 |
|
|
48 |
check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value."); |
|
49 |
check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value."); |
|
50 |
|
|
51 |
g.addArc(n1,n2); |
|
52 |
g.addArc(n2,n1); |
|
53 |
|
|
54 |
check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value."); |
|
55 |
check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value."); |
|
56 |
|
|
57 |
snap.restore(); |
|
58 |
|
|
59 |
check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value."); |
|
60 |
check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value."); |
|
61 |
|
|
62 |
} |
|
63 |
|
|
64 |
int main() { |
|
65 |
///\file |
|
66 |
{ // checking list graph |
|
67 |
checkDigraphCounters<ListDigraph>(); |
|
68 |
checkFindArc<ListDigraph>(); |
|
69 |
} |
|
70 |
{ // checking smart graph |
|
71 |
checkDigraphCounters<SmartDigraph>(); |
|
72 |
checkFindArc<SmartDigraph>(); |
|
73 |
} |
|
74 |
{ |
|
75 |
int num = 5; |
|
76 |
SmartDigraph fg; |
|
77 |
std::vector<SmartDigraph::Node> nodes; |
|
78 |
for (int i = 0; i < num; ++i) { |
|
79 |
nodes.push_back(fg.addNode()); |
|
80 |
} |
|
81 |
for (int i = 0; i < num * num; ++i) { |
|
82 |
fg.addArc(nodes[i / num], nodes[i % num]); |
|
83 |
} |
|
84 |
check(countNodes(fg) == num, "FullGraph: wrong node number."); |
|
85 |
check(countArcs(fg) == num*num, "FullGraph: wrong arc number."); |
|
86 |
for (SmartDigraph::NodeIt src(fg); src != INVALID; ++src) { |
|
87 |
for (SmartDigraph::NodeIt trg(fg); trg != INVALID; ++trg) { |
|
88 |
ConArcIt<SmartDigraph> con(fg, src, trg); |
|
89 |
check(con != INVALID, "There is no connecting arc."); |
|
90 |
check(fg.source(con) == src, "Wrong source."); |
|
91 |
check(fg.target(con) == trg, "Wrong target."); |
|
92 |
check(++con == INVALID, "There is more connecting arc."); |
|
93 |
} |
|
94 |
} |
|
95 |
AllArcLookUp<SmartDigraph> el(fg); |
|
96 |
for (SmartDigraph::NodeIt src(fg); src != INVALID; ++src) { |
|
97 |
for (SmartDigraph::NodeIt trg(fg); trg != INVALID; ++trg) { |
|
98 |
SmartDigraph::Arc con = el(src, trg); |
|
99 |
check(con != INVALID, "There is no connecting arc."); |
|
100 |
check(fg.source(con) == src, "Wrong source."); |
|
101 |
check(fg.target(con) == trg, "Wrong target."); |
|
102 |
check(el(src,trg,con) == INVALID, "There is more connecting arc."); |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
//check In/OutDegMap (and Snapshot feature) |
|
108 |
|
|
109 |
checkSnapDeg<ListDigraph>(); |
|
110 |
checkSnapDeg<SmartDigraph>(); |
|
111 |
|
|
112 |
{ |
|
113 |
const int nodeNum = 10; |
|
114 |
const int arcNum = 100; |
|
115 |
ListDigraph digraph; |
|
116 |
InDegMap<ListDigraph> inDeg(digraph); |
|
117 |
OutDegMap<ListDigraph> outDeg(digraph); |
|
118 |
std::vector<ListDigraph::Node> nodes(nodeNum); |
|
119 |
for (int i = 0; i < nodeNum; ++i) { |
|
120 |
nodes[i] = digraph.addNode(); |
|
121 |
} |
|
122 |
std::vector<ListDigraph::Arc> arcs(arcNum); |
|
123 |
for (int i = 0; i < arcNum; ++i) { |
|
124 |
arcs[i] = |
|
125 |
digraph.addArc(nodes[rnd[nodeNum]], nodes[rnd[nodeNum]]); |
|
126 |
} |
|
127 |
for (int i = 0; i < nodeNum; ++i) { |
|
128 |
check(inDeg[nodes[i]] == countInArcs(digraph, nodes[i]), |
|
129 |
"Wrong in degree map"); |
|
130 |
} |
|
131 |
for (int i = 0; i < nodeNum; ++i) { |
|
132 |
check(outDeg[nodes[i]] == countOutArcs(digraph, nodes[i]), |
|
133 |
"Wrong in degree map"); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
///Everything is OK |
|
138 |
std::cout << __FILE__ ": All tests passed.\n"; |
|
139 |
|
|
140 |
return 0; |
|
141 |
} |
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_TEST_GRAPH_UTILS_TEST_H |
|
20 |
#define LEMON_TEST_GRAPH_UTILS_TEST_H |
|
21 |
|
|
22 |
|
|
23 |
#include "test_tools.h" |
|
24 |
#include <cstdlib> |
|
25 |
#include <ctime> |
|
26 |
|
|
27 |
//! \ingroup misc |
|
28 |
//! \file |
|
29 |
//! \brief Test cases for graph utils. |
|
30 |
namespace lemon { |
|
31 |
|
|
32 |
template <typename Digraph> |
|
33 |
void checkDigraphCounters() { |
|
34 |
const int num = 5; |
|
35 |
Digraph digraph; |
|
36 |
addPetersen(digraph, num); |
|
37 |
bidirDigraph(digraph); |
|
38 |
check(countNodes(digraph) == 2*num, "Wrong node number."); |
|
39 |
check(countArcs(digraph) == 6*num, "Wrong arc number."); |
|
40 |
for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
|
41 |
check(countOutArcs(digraph, it) == 3, "Wrong out degree number."); |
|
42 |
check(countInArcs(digraph, it) == 3, "Wrong in degree number."); |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
template <typename Digraph> |
|
47 |
void checkFindArc() { |
|
48 |
typedef typename Digraph::Node Node; |
|
49 |
typedef typename Digraph::Arc Arc; |
|
50 |
typedef typename Digraph::NodeIt NodeIt; |
|
51 |
typedef typename Digraph::ArcIt ArcIt; |
|
52 |
Digraph digraph; |
|
53 |
for (int i = 0; i < 10; ++i) { |
|
54 |
digraph.addNode(); |
|
55 |
} |
|
56 |
DescriptorMap<Digraph, Node> nodes(digraph); |
|
57 |
typename DescriptorMap<Digraph, Node>::InverseMap invNodes(nodes); |
|
58 |
for (int i = 0; i < 100; ++i) { |
|
59 |
int src = rnd[invNodes.size()]; |
|
60 |
int trg = rnd[invNodes.size()]; |
|
61 |
digraph.addArc(invNodes[src], invNodes[trg]); |
|
62 |
} |
|
63 |
typename Digraph::template ArcMap<bool> found(digraph, false); |
|
64 |
DescriptorMap<Digraph, Arc> arcs(digraph); |
|
65 |
for (NodeIt src(digraph); src != INVALID; ++src) { |
|
66 |
for (NodeIt trg(digraph); trg != INVALID; ++trg) { |
|
67 |
for (ConArcIt<Digraph> con(digraph, src, trg); con != INVALID; ++con) { |
|
68 |
check(digraph.source(con) == src, "Wrong source."); |
|
69 |
check(digraph.target(con) == trg, "Wrong target."); |
|
70 |
check(found[con] == false, "The arc found already."); |
|
71 |
found[con] = true; |
|
72 |
} |
|
73 |
} |
|
74 |
} |
|
75 |
for (ArcIt it(digraph); it != INVALID; ++it) { |
|
76 |
check(found[it] == true, "The arc is not found."); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
} //namespace lemon |
|
81 |
|
|
82 |
|
|
83 |
#endif |
1 | 1 |
|
2 | 2 |
/* -*- C++ -*- |
3 | 3 |
* |
4 | 4 |
* This file is a part of LEMON, a generic C++ optimization library |
5 | 5 |
* |
6 | 6 |
* Copyright (C) 2003-2008 |
7 | 7 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
8 | 8 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
9 | 9 |
* |
10 | 10 |
* Permission to use, modify and distribute this software is granted |
11 | 11 |
* provided that this copyright notice appears in all copies. For |
12 | 12 |
* precise terms see the accompanying LICENSE file. |
13 | 13 |
* |
14 | 14 |
* This software is provided "AS IS" with no warranty of any kind, |
15 | 15 |
* express or implied, and with no claim as to its suitability for any |
16 | 16 |
* purpose. |
17 | 17 |
* |
18 | 18 |
*/ |
19 | 19 |
|
20 | 20 |
#ifndef LEMON_BITS_TRAITS_H |
21 | 21 |
#define LEMON_BITS_TRAITS_H |
22 | 22 |
|
23 | 23 |
#include <lemon/bits/utility.h> |
24 | 24 |
|
25 | 25 |
///\file |
26 | 26 |
///\brief Traits for graphs and maps |
27 | 27 |
/// |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
template <typename _Graph, typename _Item> |
31 | 31 |
class ItemSetTraits {}; |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
template <typename Graph, typename Enable = void> |
35 | 35 |
struct NodeNotifierIndicator { |
36 | 36 |
typedef InvalidType Type; |
37 | 37 |
}; |
38 | 38 |
template <typename Graph> |
39 | 39 |
struct NodeNotifierIndicator< |
40 | 40 |
Graph, |
41 | 41 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
42 | 42 |
> { |
43 | 43 |
typedef typename Graph::NodeNotifier Type; |
44 | 44 |
}; |
45 | 45 |
|
46 | 46 |
template <typename _Graph> |
47 | 47 |
class ItemSetTraits<_Graph, typename _Graph::Node> { |
48 | 48 |
public: |
49 | 49 |
|
50 | 50 |
typedef _Graph Graph; |
51 | 51 |
|
52 | 52 |
typedef typename Graph::Node Item; |
53 | 53 |
typedef typename Graph::NodeIt ItemIt; |
54 | 54 |
|
55 | 55 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
56 | 56 |
|
57 | 57 |
template <typename _Value> |
58 | 58 |
class Map : public Graph::template NodeMap<_Value> { |
59 | 59 |
public: |
60 | 60 |
typedef typename Graph::template NodeMap<_Value> Parent; |
61 | 61 |
typedef typename Graph::template NodeMap<_Value> Type; |
62 | 62 |
typedef typename Parent::Value Value; |
63 | 63 |
|
64 | 64 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
65 | 65 |
Map(const Graph& _digraph, const Value& _value) |
66 | 66 |
: Parent(_digraph, _value) {} |
67 | 67 |
|
68 | 68 |
}; |
69 | 69 |
|
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
template <typename Graph, typename Enable = void> |
73 | 73 |
struct ArcNotifierIndicator { |
74 | 74 |
typedef InvalidType Type; |
75 | 75 |
}; |
76 | 76 |
template <typename Graph> |
77 | 77 |
struct ArcNotifierIndicator< |
78 | 78 |
Graph, |
79 | 79 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
80 | 80 |
> { |
81 | 81 |
typedef typename Graph::ArcNotifier Type; |
82 | 82 |
}; |
83 | 83 |
|
84 | 84 |
template <typename _Graph> |
85 | 85 |
class ItemSetTraits<_Graph, typename _Graph::Arc> { |
86 | 86 |
public: |
87 | 87 |
|
88 | 88 |
typedef _Graph Graph; |
89 | 89 |
|
90 | 90 |
typedef typename Graph::Arc Item; |
91 | 91 |
typedef typename Graph::ArcIt ItemIt; |
92 | 92 |
|
93 | 93 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
94 | 94 |
|
95 | 95 |
template <typename _Value> |
96 | 96 |
class Map : public Graph::template ArcMap<_Value> { |
97 | 97 |
public: |
98 | 98 |
typedef typename Graph::template ArcMap<_Value> Parent; |
99 | 99 |
typedef typename Graph::template ArcMap<_Value> Type; |
100 | 100 |
typedef typename Parent::Value Value; |
101 | 101 |
|
102 | 102 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
103 | 103 |
Map(const Graph& _digraph, const Value& _value) |
104 | 104 |
: Parent(_digraph, _value) {} |
105 | 105 |
}; |
106 | 106 |
|
107 | 107 |
}; |
108 | 108 |
|
109 | 109 |
template <typename Graph, typename Enable = void> |
110 | 110 |
struct EdgeNotifierIndicator { |
111 | 111 |
typedef InvalidType Type; |
112 | 112 |
}; |
113 | 113 |
template <typename Graph> |
114 | 114 |
struct EdgeNotifierIndicator< |
115 | 115 |
Graph, |
116 | 116 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
117 | 117 |
> { |
118 | 118 |
typedef typename Graph::EdgeNotifier Type; |
119 | 119 |
}; |
120 | 120 |
|
121 | 121 |
template <typename _Graph> |
122 | 122 |
class ItemSetTraits<_Graph, typename _Graph::Edge> { |
123 | 123 |
public: |
124 | 124 |
|
125 | 125 |
typedef _Graph Graph; |
126 | 126 |
|
127 | 127 |
typedef typename Graph::Edge Item; |
128 | 128 |
typedef typename Graph::EdgeIt ItemIt; |
129 | 129 |
|
130 | 130 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
131 | 131 |
|
132 | 132 |
template <typename _Value> |
133 | 133 |
class Map : public Graph::template EdgeMap<_Value> { |
134 | 134 |
public: |
135 | 135 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
136 | 136 |
typedef typename Graph::template EdgeMap<_Value> Type; |
137 | 137 |
typedef typename Parent::Value Value; |
138 | 138 |
|
139 | 139 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
140 | 140 |
Map(const Graph& _digraph, const Value& _value) |
141 | 141 |
: Parent(_digraph, _value) {} |
142 | 142 |
}; |
143 | 143 |
|
144 | 144 |
}; |
145 | 145 |
|
146 | 146 |
template <typename Map, typename Enable = void> |
147 | 147 |
struct MapTraits { |
148 | 148 |
typedef False ReferenceMapTag; |
149 | 149 |
|
150 | 150 |
typedef typename Map::Key Key; |
151 | 151 |
typedef typename Map::Value Value; |
152 | 152 |
|
153 | 153 |
typedef const Value ConstReturnValue; |
154 | 154 |
typedef const Value ReturnValue; |
155 | 155 |
}; |
156 | 156 |
|
157 | 157 |
template <typename Map> |
158 | 158 |
struct MapTraits< |
159 | 159 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
160 | 160 |
{ |
161 | 161 |
typedef True ReferenceMapTag; |
162 | 162 |
|
163 | 163 |
typedef typename Map::Key Key; |
164 | 164 |
typedef typename Map::Value Value; |
165 | 165 |
|
166 | 166 |
typedef typename Map::ConstReference ConstReturnValue; |
167 | 167 |
typedef typename Map::Reference ReturnValue; |
168 | 168 |
|
169 | 169 |
typedef typename Map::ConstReference ConstReference; |
170 | 170 |
typedef typename Map::Reference Reference; |
171 | 171 |
}; |
172 | 172 |
|
173 | 173 |
template <typename MatrixMap, typename Enable = void> |
174 | 174 |
struct MatrixMapTraits { |
175 | 175 |
typedef False ReferenceMapTag; |
176 | 176 |
|
177 | 177 |
typedef typename MatrixMap::FirstKey FirstKey; |
178 | 178 |
typedef typename MatrixMap::SecondKey SecondKey; |
179 | 179 |
typedef typename MatrixMap::Value Value; |
180 | 180 |
|
181 | 181 |
typedef const Value ConstReturnValue; |
182 | 182 |
typedef const Value ReturnValue; |
183 | 183 |
}; |
184 | 184 |
|
185 | 185 |
template <typename MatrixMap> |
186 | 186 |
struct MatrixMapTraits< |
187 | 187 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
188 | 188 |
void>::type > |
189 | 189 |
{ |
190 | 190 |
typedef True ReferenceMapTag; |
191 | 191 |
|
192 | 192 |
typedef typename MatrixMap::FirstKey FirstKey; |
193 | 193 |
typedef typename MatrixMap::SecondKey SecondKey; |
194 | 194 |
typedef typename MatrixMap::Value Value; |
195 | 195 |
|
196 | 196 |
typedef typename MatrixMap::ConstReference ConstReturnValue; |
197 | 197 |
typedef typename MatrixMap::Reference ReturnValue; |
198 | 198 |
|
199 | 199 |
typedef typename MatrixMap::ConstReference ConstReference; |
200 | 200 |
typedef typename MatrixMap::Reference Reference; |
201 | 201 |
}; |
202 | 202 |
|
203 | 203 |
// Indicators for the tags |
204 | 204 |
|
205 | 205 |
template <typename Graph, typename Enable = void> |
206 | 206 |
struct NodeNumTagIndicator { |
207 | 207 |
static const bool value = false; |
208 | 208 |
}; |
209 | 209 |
|
210 | 210 |
template <typename Graph> |
211 | 211 |
struct NodeNumTagIndicator< |
212 | 212 |
Graph, |
213 | 213 |
typename enable_if<typename Graph::NodeNumTag, void>::type |
214 | 214 |
> { |
215 | 215 |
static const bool value = true; |
216 | 216 |
}; |
217 | 217 |
|
218 | 218 |
template <typename Graph, typename Enable = void> |
219 |
struct |
|
219 |
struct EdgeNumTagIndicator { |
|
220 | 220 |
static const bool value = false; |
221 | 221 |
}; |
222 | 222 |
|
223 | 223 |
template <typename Graph> |
224 |
struct |
|
224 |
struct EdgeNumTagIndicator< |
|
225 | 225 |
Graph, |
226 |
typename enable_if<typename Graph:: |
|
226 |
typename enable_if<typename Graph::EdgeNumTag, void>::type |
|
227 | 227 |
> { |
228 | 228 |
static const bool value = true; |
229 | 229 |
}; |
230 | 230 |
|
231 | 231 |
template <typename Graph, typename Enable = void> |
232 |
struct |
|
232 |
struct FindEdgeTagIndicator { |
|
233 | 233 |
static const bool value = false; |
234 | 234 |
}; |
235 | 235 |
|
236 | 236 |
template <typename Graph> |
237 |
struct |
|
237 |
struct FindEdgeTagIndicator< |
|
238 | 238 |
Graph, |
239 |
typename enable_if<typename Graph:: |
|
239 |
typename enable_if<typename Graph::FindEdgeTag, void>::type |
|
240 | 240 |
> { |
241 | 241 |
static const bool value = true; |
242 | 242 |
}; |
243 | 243 |
|
244 | 244 |
template <typename Graph, typename Enable = void> |
245 | 245 |
struct UndirectedTagIndicator { |
246 | 246 |
static const bool value = false; |
247 | 247 |
}; |
248 | 248 |
|
249 | 249 |
template <typename Graph> |
250 | 250 |
struct UndirectedTagIndicator< |
251 | 251 |
Graph, |
252 | 252 |
typename enable_if<typename Graph::UndirectedTag, void>::type |
253 | 253 |
> { |
254 | 254 |
static const bool value = true; |
255 | 255 |
}; |
256 | 256 |
|
257 | 257 |
template <typename Graph, typename Enable = void> |
258 | 258 |
struct BuildTagIndicator { |
259 | 259 |
static const bool value = false; |
260 | 260 |
}; |
261 | 261 |
|
262 | 262 |
template <typename Graph> |
263 | 263 |
struct BuildTagIndicator< |
264 | 264 |
Graph, |
265 | 265 |
typename enable_if<typename Graph::BuildTag, void>::type |
266 | 266 |
> { |
267 | 267 |
static const bool value = true; |
268 | 268 |
}; |
269 | 269 |
|
270 | 270 |
} |
271 | 271 |
|
272 | 272 |
#endif |
1 | 1 |
/* -*- C++ -*- |
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 |
#ifndef LEMON_GRAPH_UTILS_H |
20 | 20 |
#define LEMON_GRAPH_UTILS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <map> |
25 | 25 |
#include <cmath> |
26 | 26 |
#include <algorithm> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
#include <lemon/bits/utility.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/bits/traits.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bits/alteration_notifier.h> |
34 | 34 |
#include <lemon/bits/default_map.h> |
35 | 35 |
|
36 | 36 |
///\ingroup gutils |
37 | 37 |
///\file |
38 |
///\brief |
|
38 |
///\brief Graph utilities. |
|
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
/// \addtogroup gutils |
43 | 43 |
/// @{ |
44 | 44 |
|
45 | 45 |
///Creates convenience typedefs for the digraph types and iterators |
46 | 46 |
|
47 | 47 |
///This \c \#define creates convenience typedefs for the following types |
48 | 48 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
49 |
///\c OutArcIt |
|
50 |
///\note If \c G it a template parameter, it should be used in this way. |
|
51 |
///\code |
|
52 |
/// GRAPH_TYPEDEFS(typename G); |
|
53 |
///\endcode |
|
54 |
/// |
|
55 |
///\warning There are no typedefs for the digraph maps because of the lack of |
|
56 |
///template typedefs in C++. |
|
57 |
#define GRAPH_TYPEDEFS(Digraph) \ |
|
58 |
typedef Digraph:: Node Node; \ |
|
59 |
typedef Digraph:: NodeIt NodeIt; \ |
|
60 |
typedef Digraph:: Arc Arc; \ |
|
61 |
typedef Digraph:: ArcIt ArcIt; \ |
|
62 |
typedef Digraph:: InArcIt InArcIt; \ |
|
63 |
|
|
49 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
|
50 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
|
51 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
|
52 |
typedef Digraph::Node Node; \ |
|
53 |
typedef Digraph::NodeIt NodeIt; \ |
|
54 |
typedef Digraph::Arc Arc; \ |
|
55 |
typedef Digraph::ArcIt ArcIt; \ |
|
56 |
typedef Digraph::InArcIt InArcIt; \ |
|
57 |
typedef Digraph::OutArcIt OutArcIt |
|
64 | 58 |
|
65 | 59 |
///Creates convenience typedefs for the graph types and iterators |
66 | 60 |
|
67 |
///This \c \#define creates the same convenience typedefs as defined by |
|
68 |
///\ref GRAPH_TYPEDEFS(Digraph) and three more, namely it creates |
|
69 |
/// |
|
61 |
///This \c \#define creates the same convenience typedefs as defined |
|
62 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
|
63 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
|
64 |
///\c DoubleEdgeMap. |
|
65 |
#define GRAPH_TYPEDEFS(Graph) \ |
|
66 |
DIGRAPH_TYPEDEFS(Graph); \ |
|
67 |
typedef Graph::Edge Edge; \ |
|
68 |
typedef Graph::EdgeIt EdgeIt; \ |
|
69 |
typedef Graph::IncEdgeIt IncEdgeIt |
|
70 |
|
|
71 |
/// \brief Function to count the items in the graph. |
|
70 | 72 |
/// |
71 |
///\note If \c G it a template parameter, it should be used in this way. |
|
72 |
///\code |
|
73 |
/// UGRAPH_TYPEDEFS(typename G); |
|
74 |
///\endcode |
|
75 |
/// |
|
76 |
///\warning There are no typedefs for the digraph maps because of the lack of |
|
77 |
///template typedefs in C++. |
|
78 |
#define UGRAPH_TYPEDEFS(Digraph) \ |
|
79 |
GRAPH_TYPEDEFS(Digraph); \ |
|
80 |
typedef Digraph:: Edge Edge; \ |
|
81 |
typedef Digraph:: EdgeIt EdgeIt; \ |
|
82 |
typedef Digraph:: IncArcIt IncArcIt |
|
83 |
|
|
84 |
///\brief Creates convenience typedefs for the bipartite digraph |
|
85 |
///types and iterators |
|
86 |
|
|
87 |
///This \c \#define creates the same convenience typedefs as defined by |
|
88 |
///\ref UGRAPH_TYPEDEFS(Digraph) and two more, namely it creates |
|
89 |
///\c RedIt, \c BlueIt, |
|
90 |
/// |
|
91 |
///\note If \c G it a template parameter, it should be used in this way. |
|
92 |
///\code |
|
93 |
/// BPUGRAPH_TYPEDEFS(typename G); |
|
94 |
///\endcode |
|
95 |
/// |
|
96 |
///\warning There are no typedefs for the digraph maps because of the lack of |
|
97 |
///template typedefs in C++. |
|
98 |
#define BPUGRAPH_TYPEDEFS(Digraph) \ |
|
99 |
UGRAPH_TYPEDEFS(Digraph); \ |
|
100 |
typedef Digraph::Red Red; \ |
|
101 |
typedef Digraph::Blue Blue; \ |
|
102 |
typedef Digraph::RedIt RedIt; \ |
|
103 |
typedef Digraph::BlueIt BlueIt |
|
104 |
|
|
105 |
/// \brief Function to count the items in the digraph. |
|
106 |
/// |
|
107 |
/// This function counts the items (nodes, arcs etc) in the |
|
73 |
/// This function counts the items (nodes, arcs etc) in the graph. |
|
108 | 74 |
/// The complexity of the function is O(n) because |
109 | 75 |
/// it iterates on all of the items. |
110 |
|
|
111 |
template <typename Digraph, typename Item> |
|
112 |
inline int countItems(const Digraph& g) { |
|
113 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
|
76 |
template <typename Graph, typename Item> |
|
77 |
inline int countItems(const Graph& g) { |
|
78 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
|
114 | 79 |
int num = 0; |
115 | 80 |
for (ItemIt it(g); it != INVALID; ++it) { |
116 | 81 |
++num; |
117 | 82 |
} |
118 | 83 |
return num; |
119 | 84 |
} |
120 | 85 |
|
121 | 86 |
// Node counting: |
122 | 87 |
|
123 |
namespace |
|
88 |
namespace _graph_utils_bits { |
|
124 | 89 |
|
125 |
template <typename |
|
90 |
template <typename Graph, typename Enable = void> |
|
126 | 91 |
struct CountNodesSelector { |
127 |
static int count(const Digraph &g) { |
|
128 |
return countItems<Digraph, typename Digraph::Node>(g); |
|
92 |
static int count(const Graph &g) { |
|
93 |
return countItems<Graph, typename Graph::Node>(g); |
|
129 | 94 |
} |
130 | 95 |
}; |
131 | 96 |
|
132 |
template <typename |
|
97 |
template <typename Graph> |
|
133 | 98 |
struct CountNodesSelector< |
134 |
Digraph, typename |
|
135 |
enable_if<typename Digraph::NodeNumTag, void>::type> |
|
99 |
Graph, typename |
|
100 |
enable_if<typename Graph::NodeNumTag, void>::type> |
|
136 | 101 |
{ |
137 |
static int count(const |
|
102 |
static int count(const Graph &g) { |
|
138 | 103 |
return g.nodeNum(); |
139 | 104 |
} |
140 | 105 |
}; |
141 | 106 |
} |
142 | 107 |
|
143 |
/// \brief Function to count the nodes in the |
|
108 |
/// \brief Function to count the nodes in the graph. |
|
144 | 109 |
/// |
145 |
/// This function counts the nodes in the |
|
110 |
/// This function counts the nodes in the graph. |
|
146 | 111 |
/// The complexity of the function is O(n) but for some |
147 |
/// |
|
112 |
/// graph structures it is specialized to run in O(1). |
|
148 | 113 |
/// |
149 |
/// If the |
|
114 |
/// If the graph contains a \e nodeNum() member function and a |
|
150 | 115 |
/// \e NodeNumTag tag then this function calls directly the member |
151 | 116 |
/// function to query the cardinality of the node set. |
152 |
template <typename Digraph> |
|
153 |
inline int countNodes(const Digraph& g) { |
|
154 |
|
|
117 |
template <typename Graph> |
|
118 |
inline int countNodes(const Graph& g) { |
|
119 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
|
155 | 120 |
} |
156 | 121 |
|
157 |
|
|
122 |
// Arc counting: |
|
123 |
|
|
124 |
namespace _graph_utils_bits { |
|
158 | 125 |
|
159 |
template <typename Digraph, typename Enable = void> |
|
160 |
struct CountRedsSelector { |
|
161 |
static int count(const Digraph &g) { |
|
162 |
return countItems<Digraph, typename Digraph::Red>(g); |
|
126 |
template <typename Graph, typename Enable = void> |
|
127 |
struct CountArcsSelector { |
|
128 |
static int count(const Graph &g) { |
|
129 |
return countItems<Graph, typename Graph::Arc>(g); |
|
163 | 130 |
} |
164 | 131 |
}; |
165 | 132 |
|
166 |
template <typename Digraph> |
|
167 |
struct CountRedsSelector< |
|
168 |
Digraph, typename |
|
169 |
enable_if<typename Digraph::NodeNumTag, void>::type> |
|
133 |
template <typename Graph> |
|
134 |
struct CountArcsSelector< |
|
135 |
Graph, |
|
136 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
|
170 | 137 |
{ |
171 |
static int count(const Digraph &g) { |
|
172 |
return g.redNum(); |
|
173 |
} |
|
174 |
}; |
|
175 |
} |
|
176 |
|
|
177 |
/// \brief Function to count the reds in the digraph. |
|
178 |
/// |
|
179 |
/// This function counts the reds in the digraph. |
|
180 |
/// The complexity of the function is O(an) but for some |
|
181 |
/// digraph structures it is specialized to run in O(1). |
|
182 |
/// |
|
183 |
/// If the digraph contains an \e redNum() member function and a |
|
184 |
/// \e NodeNumTag tag then this function calls directly the member |
|
185 |
/// function to query the cardinality of the A-node set. |
|
186 |
template <typename Digraph> |
|
187 |
inline int countReds(const Digraph& g) { |
|
188 |
return _digraph_utils_bits::CountRedsSelector<Digraph>::count(g); |
|
189 |
} |
|
190 |
|
|
191 |
namespace _digraph_utils_bits { |
|
192 |
|
|
193 |
template <typename Digraph, typename Enable = void> |
|
194 |
struct CountBluesSelector { |
|
195 |
static int count(const Digraph &g) { |
|
196 |
return countItems<Digraph, typename Digraph::Blue>(g); |
|
197 |
} |
|
198 |
}; |
|
199 |
|
|
200 |
template <typename Digraph> |
|
201 |
struct CountBluesSelector< |
|
202 |
Digraph, typename |
|
203 |
enable_if<typename Digraph::NodeNumTag, void>::type> |
|
204 |
{ |
|
205 |
static int count(const Digraph &g) { |
|
206 |
return g.blueNum(); |
|
207 |
} |
|
208 |
}; |
|
209 |
} |
|
210 |
|
|
211 |
/// \brief Function to count the blues in the digraph. |
|
212 |
/// |
|
213 |
/// This function counts the blues in the digraph. |
|
214 |
/// The complexity of the function is O(bn) but for some |
|
215 |
/// digraph structures it is specialized to run in O(1). |
|
216 |
/// |
|
217 |
/// If the digraph contains a \e blueNum() member function and a |
|
218 |
/// \e NodeNumTag tag then this function calls directly the member |
|
219 |
/// function to query the cardinality of the B-node set. |
|
220 |
template <typename Digraph> |
|
221 |
inline int countBlues(const Digraph& g) { |
|
222 |
return _digraph_utils_bits::CountBluesSelector<Digraph>::count(g); |
|
223 |
} |
|
224 |
|
|
225 |
|
|
226 |
// Arc counting: |
|
227 |
|
|
228 |
namespace _digraph_utils_bits { |
|
229 |
|
|
230 |
template <typename Digraph, typename Enable = void> |
|
231 |
struct CountArcsSelector { |
|
232 |
static int count(const Digraph &g) { |
|
233 |
return countItems<Digraph, typename Digraph::Arc>(g); |
|
234 |
} |
|
235 |
}; |
|
236 |
|
|
237 |
template <typename Digraph> |
|
238 |
struct CountArcsSelector< |
|
239 |
Digraph, |
|
240 |
typename enable_if<typename Digraph::ArcNumTag, void>::type> |
|
241 |
{ |
|
242 |
static int count(const Digraph &g) { |
|
138 |
static int count(const Graph &g) { |
|
243 | 139 |
return g.arcNum(); |
244 | 140 |
} |
245 | 141 |
}; |
246 | 142 |
} |
247 | 143 |
|
248 |
/// \brief Function to count the arcs in the |
|
144 |
/// \brief Function to count the arcs in the graph. |
|
249 | 145 |
/// |
250 |
/// This function counts the arcs in the |
|
146 |
/// This function counts the arcs in the graph. |
|
251 | 147 |
/// The complexity of the function is O(e) but for some |
252 |
/// |
|
148 |
/// graph structures it is specialized to run in O(1). |
|
253 | 149 |
/// |
254 |
/// If the digraph contains a \e arcNum() member function and a |
|
255 |
/// \e ArcNumTag tag then this function calls directly the member |
|
150 |
/// If the graph contains a \e arcNum() member function and a |
|
151 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
256 | 152 |
/// function to query the cardinality of the arc set. |
257 |
template <typename Digraph> |
|
258 |
inline int countArcs(const Digraph& g) { |
|
259 |
|
|
153 |
template <typename Graph> |
|
154 |
inline int countArcs(const Graph& g) { |
|
155 |
return _graph_utils_bits::CountArcsSelector<Graph>::count(g); |
|
260 | 156 |
} |
261 | 157 |
|
262 |
// Undirected arc counting: |
|
263 |
namespace _digraph_utils_bits { |
|
158 |
// Edge counting: |
|
159 |
namespace _graph_utils_bits { |
|
264 | 160 |
|
265 |
template <typename |
|
161 |
template <typename Graph, typename Enable = void> |
|
266 | 162 |
struct CountEdgesSelector { |
267 |
static int count(const Digraph &g) { |
|
268 |
return countItems<Digraph, typename Digraph::Edge>(g); |
|
163 |
static int count(const Graph &g) { |
|
164 |
return countItems<Graph, typename Graph::Edge>(g); |
|
269 | 165 |
} |
270 | 166 |
}; |
271 | 167 |
|
272 |
template <typename |
|
168 |
template <typename Graph> |
|
273 | 169 |
struct CountEdgesSelector< |
274 |
Digraph, |
|
275 |
typename enable_if<typename Digraph::ArcNumTag, void>::type> |
|
170 |
Graph, |
|
171 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
|
276 | 172 |
{ |
277 |
static int count(const |
|
173 |
static int count(const Graph &g) { |
|
278 | 174 |
return g.edgeNum(); |
279 | 175 |
} |
280 | 176 |
}; |
281 | 177 |
} |
282 | 178 |
|
283 |
/// \brief Function to count the edges in the |
|
179 |
/// \brief Function to count the edges in the graph. |
|
284 | 180 |
/// |
285 |
/// This function counts the edges in the digraph. |
|
286 |
/// The complexity of the function is O(e) but for some |
|
287 |
/// |
|
181 |
/// This function counts the edges in the graph. |
|
182 |
/// The complexity of the function is O(m) but for some |
|
183 |
/// graph structures it is specialized to run in O(1). |
|
288 | 184 |
/// |
289 |
/// If the digraph contains a \e edgeNum() member function and a |
|
290 |
/// \e ArcNumTag tag then this function calls directly the member |
|
185 |
/// If the graph contains a \e edgeNum() member function and a |
|
186 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
291 | 187 |
/// function to query the cardinality of the edge set. |
292 |
template <typename Digraph> |
|
293 |
inline int countEdges(const Digraph& g) { |
|
294 |
|
|
188 |
template <typename Graph> |
|
189 |
inline int countEdges(const Graph& g) { |
|
190 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g); |
|
295 | 191 |
|
296 | 192 |
} |
297 | 193 |
|
298 | 194 |
|
299 |
template <typename Digraph, typename DegIt> |
|
300 |
inline int countNodeDegree(const Digraph& _g, const typename Digraph::Node& _n) { |
|
195 |
template <typename Graph, typename DegIt> |
|
196 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
|
301 | 197 |
int num = 0; |
302 | 198 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
303 | 199 |
++num; |
304 | 200 |
} |
305 | 201 |
return num; |
306 | 202 |
} |
307 | 203 |
|
308 | 204 |
/// \brief Function to count the number of the out-arcs from node \c n. |
309 | 205 |
/// |
310 | 206 |
/// This function counts the number of the out-arcs from node \c n |
311 |
/// in the digraph. |
|
312 |
template <typename Digraph> |
|
313 |
inline int countOutArcs(const Digraph& _g, const typename Digraph::Node& _n) { |
|
314 |
return countNodeDegree<Digraph, typename Digraph::OutArcIt>(_g, _n); |
|
207 |
/// in the graph. |
|
208 |
template <typename Graph> |
|
209 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
210 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
|
315 | 211 |
} |
316 | 212 |
|
317 | 213 |
/// \brief Function to count the number of the in-arcs to node \c n. |
318 | 214 |
/// |
319 | 215 |
/// This function counts the number of the in-arcs to node \c n |
320 |
/// in the digraph. |
|
321 |
template <typename Digraph> |
|
322 |
inline int countInArcs(const Digraph& _g, const typename Digraph::Node& _n) { |
|
323 |
return countNodeDegree<Digraph, typename Digraph::InArcIt>(_g, _n); |
|
216 |
/// in the graph. |
|
217 |
template <typename Graph> |
|
218 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
219 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
|
324 | 220 |
} |
325 | 221 |
|
326 |
/// \brief Function to count the number of the inc- |
|
222 |
/// \brief Function to count the number of the inc-edges to node \c n. |
|
327 | 223 |
/// |
328 |
/// This function counts the number of the inc-arcs to node \c n |
|
329 |
/// in the digraph. |
|
330 |
template <typename Digraph> |
|
331 |
inline int countIncArcs(const Digraph& _g, const typename Digraph::Node& _n) { |
|
332 |
|
|
224 |
/// This function counts the number of the inc-edges to node \c n |
|
225 |
/// in the graph. |
|
226 |
template <typename Graph> |
|
227 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
|
228 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
|
333 | 229 |
} |
334 | 230 |
|
335 |
namespace |
|
231 |
namespace _graph_utils_bits { |
|
336 | 232 |
|
337 |
template <typename |
|
233 |
template <typename Graph, typename Enable = void> |
|
338 | 234 |
struct FindArcSelector { |
339 |
typedef typename Digraph::Node Node; |
|
340 |
typedef typename Digraph::Arc Arc; |
|
341 |
|
|
235 |
typedef typename Graph::Node Node; |
|
236 |
typedef typename Graph::Arc Arc; |
|
237 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
|
342 | 238 |
if (e == INVALID) { |
343 | 239 |
g.firstOut(e, u); |
344 | 240 |
} else { |
345 | 241 |
g.nextOut(e); |
346 | 242 |
} |
347 | 243 |
while (e != INVALID && g.target(e) != v) { |
348 | 244 |
g.nextOut(e); |
349 | 245 |
} |
350 | 246 |
return e; |
351 | 247 |
} |
352 | 248 |
}; |
353 | 249 |
|
354 |
template <typename |
|
250 |
template <typename Graph> |
|
355 | 251 |
struct FindArcSelector< |
356 |
Digraph, |
|
357 |
typename enable_if<typename Digraph::FindArcTag, void>::type> |
|
252 |
Graph, |
|
253 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
|
358 | 254 |
{ |
359 |
typedef typename Digraph::Node Node; |
|
360 |
typedef typename Digraph::Arc Arc; |
|
361 |
|
|
255 |
typedef typename Graph::Node Node; |
|
256 |
typedef typename Graph::Arc Arc; |
|
257 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
|
362 | 258 |
return g.findArc(u, v, prev); |
363 | 259 |
} |
364 | 260 |
}; |
365 | 261 |
} |
366 | 262 |
|
367 |
/// \brief Finds an arc between two nodes of a |
|
263 |
/// \brief Finds an arc between two nodes of a graph. |
|
368 | 264 |
/// |
369 |
/// Finds an arc from node \c u to node \c v in |
|
265 |
/// Finds an arc from node \c u to node \c v in graph \c g. |
|
370 | 266 |
/// |
371 | 267 |
/// If \c prev is \ref INVALID (this is the default value), then |
372 | 268 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
373 | 269 |
/// the next arc from \c u to \c v after \c prev. |
374 | 270 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
375 | 271 |
/// |
376 | 272 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
377 | 273 |
///\code |
378 | 274 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) { |
379 | 275 |
/// ... |
380 | 276 |
/// } |
381 | 277 |
///\endcode |
382 | 278 |
/// |
383 | 279 |
///\sa ArcLookUp |
384 | 280 |
///\sa AllArcLookUp |
385 | 281 |
///\sa DynArcLookUp |
386 | 282 |
///\sa ConArcIt |
387 |
template <typename Digraph> |
|
388 |
inline typename Digraph::Arc |
|
389 |
findArc(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v, |
|
390 |
typename Digraph::Arc prev = INVALID) { |
|
391 |
|
|
283 |
template <typename Graph> |
|
284 |
inline typename Graph::Arc |
|
285 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
|
286 |
typename Graph::Arc prev = INVALID) { |
|
287 |
return _graph_utils_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
|
392 | 288 |
} |
393 | 289 |
|
394 | 290 |
/// \brief Iterator for iterating on arcs connected the same nodes. |
395 | 291 |
/// |
396 | 292 |
/// Iterator for iterating on arcs connected the same nodes. It is |
397 | 293 |
/// higher level interface for the findArc() function. You can |
398 | 294 |
/// use it the following way: |
399 | 295 |
///\code |
400 |
/// for (ConArcIt< |
|
296 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
|
401 | 297 |
/// ... |
402 | 298 |
/// } |
403 | 299 |
///\endcode |
404 | 300 |
/// |
405 | 301 |
///\sa findArc() |
406 | 302 |
///\sa ArcLookUp |
407 | 303 |
///\sa AllArcLookUp |
408 | 304 |
///\sa DynArcLookUp |
409 | 305 |
/// |
410 | 306 |
/// \author Balazs Dezso |
411 |
template <typename _Digraph> |
|
412 |
class ConArcIt : public _Digraph::Arc { |
|
307 |
template <typename _Graph> |
|
308 |
class ConArcIt : public _Graph::Arc { |
|
413 | 309 |
public: |
414 | 310 |
|
415 |
typedef _Digraph Digraph; |
|
416 |
typedef typename Digraph::Arc Parent; |
|
311 |
typedef _Graph Graph; |
|
312 |
typedef typename Graph::Arc Parent; |
|
417 | 313 |
|
418 |
typedef typename Digraph::Arc Arc; |
|
419 |
typedef typename Digraph::Node Node; |
|
314 |
typedef typename Graph::Arc Arc; |
|
315 |
typedef typename Graph::Node Node; |
|
420 | 316 |
|
421 | 317 |
/// \brief Constructor. |
422 | 318 |
/// |
423 | 319 |
/// Construct a new ConArcIt iterating on the arcs which |
424 | 320 |
/// connects the \c u and \c v node. |
425 |
ConArcIt(const Digraph& g, Node u, Node v) : digraph(g) { |
|
426 |
Parent::operator=(findArc(digraph, u, v)); |
|
321 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
|
322 |
Parent::operator=(findArc(_graph, u, v)); |
|
427 | 323 |
} |
428 | 324 |
|
429 | 325 |
/// \brief Constructor. |
430 | 326 |
/// |
431 | 327 |
/// Construct a new ConArcIt which continues the iterating from |
432 | 328 |
/// the \c e arc. |
433 |
ConArcIt(const |
|
329 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
|
434 | 330 |
|
435 | 331 |
/// \brief Increment operator. |
436 | 332 |
/// |
437 | 333 |
/// It increments the iterator and gives back the next arc. |
438 | 334 |
ConArcIt& operator++() { |
439 |
Parent::operator=(findArc(digraph, digraph.source(*this), |
|
440 |
digraph.target(*this), *this)); |
|
335 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
|
336 |
_graph.target(*this), *this)); |
|
441 | 337 |
return *this; |
442 | 338 |
} |
443 | 339 |
private: |
444 |
const |
|
340 |
const Graph& _graph; |
|
445 | 341 |
}; |
446 | 342 |
|
447 |
namespace |
|
343 |
namespace _graph_utils_bits { |
|
448 | 344 |
|
449 |
template <typename |
|
345 |
template <typename Graph, typename Enable = void> |
|
450 | 346 |
struct FindEdgeSelector { |
451 |
typedef typename Digraph::Node Node; |
|
452 |
typedef typename Digraph::Edge Edge; |
|
453 |
|
|
347 |
typedef typename Graph::Node Node; |
|
348 |
typedef typename Graph::Edge Edge; |
|
349 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
|
454 | 350 |
bool b; |
455 | 351 |
if (u != v) { |
456 | 352 |
if (e == INVALID) { |
457 | 353 |
g.firstInc(e, b, u); |
458 | 354 |
} else { |
459 | 355 |
b = g.source(e) == u; |
460 | 356 |
g.nextInc(e, b); |
461 | 357 |
} |
462 | 358 |
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) { |
463 | 359 |
g.nextInc(e, b); |
464 | 360 |
} |
465 | 361 |
} else { |
466 | 362 |
if (e == INVALID) { |
467 | 363 |
g.firstInc(e, b, u); |
468 | 364 |
} else { |
469 | 365 |
b = true; |
470 | 366 |
g.nextInc(e, b); |
471 | 367 |
} |
472 | 368 |
while (e != INVALID && (!b || g.target(e) != v)) { |
473 | 369 |
g.nextInc(e, b); |
474 | 370 |
} |
475 | 371 |
} |
476 | 372 |
return e; |
477 | 373 |
} |
478 | 374 |
}; |
479 | 375 |
|
480 |
template <typename |
|
376 |
template <typename Graph> |
|
481 | 377 |
struct FindEdgeSelector< |
482 |
Digraph, |
|
483 |
typename enable_if<typename Digraph::FindArcTag, void>::type> |
|
378 |
Graph, |
|
379 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
|
484 | 380 |
{ |
485 |
typedef typename Digraph::Node Node; |
|
486 |
typedef typename Digraph::Edge Edge; |
|
487 |
|
|
381 |
typedef typename Graph::Node Node; |
|
382 |
typedef typename Graph::Edge Edge; |
|
383 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
|
488 | 384 |
return g.findEdge(u, v, prev); |
489 | 385 |
} |
490 | 386 |
}; |
491 | 387 |
} |
492 | 388 |
|
493 |
/// \brief Finds an edge between two nodes of a |
|
389 |
/// \brief Finds an edge between two nodes of a graph. |
|
494 | 390 |
/// |
495 |
/// Finds an edge from node \c u to node \c v in digraph \c g. |
|
496 |
/// If the node \c u and node \c v is equal then each loop arc |
|
497 |
/// |
|
391 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
|
392 |
/// If the node \c u and node \c v is equal then each loop edge |
|
393 |
/// will be enumerated once. |
|
498 | 394 |
/// |
499 | 395 |
/// If \c prev is \ref INVALID (this is the default value), then |
500 | 396 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
501 | 397 |
/// the next arc from \c u to \c v after \c prev. |
502 | 398 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
503 | 399 |
/// |
504 | 400 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
505 | 401 |
///\code |
506 | 402 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
507 | 403 |
/// e = findEdge(g,u,v,e)) { |
508 | 404 |
/// ... |
509 | 405 |
/// } |
510 | 406 |
///\endcode |
511 | 407 |
/// |
512 | 408 |
///\sa ConArcIt |
513 | 409 |
|
514 |
template <typename Digraph> |
|
515 |
inline typename Digraph::Edge |
|
516 |
findEdge(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v, |
|
517 |
typename Digraph::Edge p = INVALID) { |
|
518 |
|
|
410 |
template <typename Graph> |
|
411 |
inline typename Graph::Edge |
|
412 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
|
413 |
typename Graph::Edge p = INVALID) { |
|
414 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
|
519 | 415 |
} |
520 | 416 |
|
521 | 417 |
/// \brief Iterator for iterating on edges connected the same nodes. |
522 | 418 |
/// |
523 | 419 |
/// Iterator for iterating on edges connected the same nodes. It is |
524 | 420 |
/// higher level interface for the findEdge() function. You can |
525 | 421 |
/// use it the following way: |
526 | 422 |
///\code |
527 |
/// for (ConEdgeIt< |
|
423 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
|
528 | 424 |
/// ... |
529 | 425 |
/// } |
530 | 426 |
///\endcode |
531 | 427 |
/// |
532 | 428 |
///\sa findEdge() |
533 | 429 |
/// |
534 | 430 |
/// \author Balazs Dezso |
535 |
template <typename _Digraph> |
|
536 |
class ConEdgeIt : public _Digraph::Edge { |
|
431 |
template <typename _Graph> |
|
432 |
class ConEdgeIt : public _Graph::Edge { |
|
537 | 433 |
public: |
538 | 434 |
|
539 |
typedef _Digraph Digraph; |
|
540 |
typedef typename Digraph::Edge Parent; |
|
435 |
typedef _Graph Graph; |
|
436 |
typedef typename Graph::Edge Parent; |
|
541 | 437 |
|
542 |
typedef typename Digraph::Edge Edge; |
|
543 |
typedef typename Digraph::Node Node; |
|
438 |
typedef typename Graph::Edge Edge; |
|
439 |
typedef typename Graph::Node Node; |
|
544 | 440 |
|
545 | 441 |
/// \brief Constructor. |
546 | 442 |
/// |
547 |
/// Construct a new ConEdgeIt iterating on the |
|
443 |
/// Construct a new ConEdgeIt iterating on the edges which |
|
548 | 444 |
/// connects the \c u and \c v node. |
549 |
ConEdgeIt(const Digraph& g, Node u, Node v) : digraph(g) { |
|
550 |
Parent::operator=(findEdge(digraph, u, v)); |
|
445 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) { |
|
446 |
Parent::operator=(findEdge(_graph, u, v)); |
|
551 | 447 |
} |
552 | 448 |
|
553 | 449 |
/// \brief Constructor. |
554 | 450 |
/// |
555 | 451 |
/// Construct a new ConEdgeIt which continues the iterating from |
556 |
/// the \c e arc. |
|
557 |
ConEdgeIt(const Digraph& g, Edge e) : Parent(e), digraph(g) {} |
|
452 |
/// the \c e edge. |
|
453 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
|
558 | 454 |
|
559 | 455 |
/// \brief Increment operator. |
560 | 456 |
/// |
561 |
/// It increments the iterator and gives back the next |
|
457 |
/// It increments the iterator and gives back the next edge. |
|
562 | 458 |
ConEdgeIt& operator++() { |
563 |
Parent::operator=(findEdge(digraph, digraph.source(*this), |
|
564 |
digraph.target(*this), *this)); |
|
459 |
Parent::operator=(findEdge(_graph, _graph.source(*this), |
|
460 |
_graph.target(*this), *this)); |
|
565 | 461 |
return *this; |
566 | 462 |
} |
567 | 463 |
private: |
568 |
const |
|
464 |
const Graph& _graph; |
|
569 | 465 |
}; |
570 | 466 |
|
571 |
/// \brief Copy a map. |
|
572 |
/// |
|
573 |
/// This function copies the \c from map to the \c to map. It uses the |
|
574 |
/// given iterator to iterate on the data structure and it uses the \c ref |
|
575 |
/// mapping to convert the from's keys to the to's keys. |
|
576 |
template <typename To, typename From, |
|
577 |
typename ItemIt, typename Ref> |
|
578 |
void copyMap(To& to, const From& from, |
|
579 |
ItemIt it, const Ref& ref) { |
|
580 |
for (; it != INVALID; ++it) { |
|
581 |
to[ref[it]] = from[it]; |
|
582 |
} |
|
583 |
} |
|
584 |
|
|
585 |
/// \brief Copy the from map to the to map. |
|
586 |
/// |
|
587 |
/// Copy the \c from map to the \c to map. It uses the given iterator |
|
588 |
/// to iterate on the data structure. |
|
589 |
template <typename To, typename From, typename ItemIt> |
|
590 |
void copyMap(To& to, const From& from, ItemIt it) { |
|
591 |
for (; it != INVALID; ++it) { |
|
592 |
to[it] = from[it]; |
|
593 |
} |
|
594 |
} |
|
595 |
|
|
596 |
namespace _digraph_utils_bits { |
|
467 |
namespace _graph_utils_bits { |
|
597 | 468 |
|
598 | 469 |
template <typename Digraph, typename Item, typename RefMap> |
599 | 470 |
class MapCopyBase { |
600 | 471 |
public: |
601 | 472 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
602 | 473 |
|
603 | 474 |
virtual ~MapCopyBase() {} |
604 | 475 |
}; |
605 | 476 |
|
606 | 477 |
template <typename Digraph, typename Item, typename RefMap, |
607 | 478 |
typename ToMap, typename FromMap> |
608 | 479 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
609 | 480 |
public: |
610 | 481 |
|
611 | 482 |
MapCopy(ToMap& tmap, const FromMap& map) |
612 | 483 |
: _tmap(tmap), _map(map) {} |
613 | 484 |
|
614 | 485 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
615 | 486 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
616 | 487 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
617 | 488 |
_tmap.set(refMap[it], _map[it]); |
618 | 489 |
} |
619 | 490 |
} |
620 | 491 |
|
621 | 492 |
private: |
622 | 493 |
ToMap& _tmap; |
623 | 494 |
const FromMap& _map; |
624 | 495 |
}; |
625 | 496 |
|
626 | 497 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
627 | 498 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
628 | 499 |
public: |
629 | 500 |
|
630 | 501 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {} |
631 | 502 |
|
632 | 503 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
633 | 504 |
_it = refMap[_item]; |
634 | 505 |
} |
635 | 506 |
|
636 | 507 |
private: |
637 | 508 |
It& _it; |
638 | 509 |
Item _item; |
639 | 510 |
}; |
640 | 511 |
|
641 | 512 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
642 | 513 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
643 | 514 |
public: |
644 | 515 |
|
645 | 516 |
RefCopy(Ref& map) : _map(map) {} |
646 | 517 |
|
647 | 518 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
648 | 519 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
649 | 520 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
650 | 521 |
_map.set(it, refMap[it]); |
651 | 522 |
} |
652 | 523 |
} |
653 | 524 |
|
654 | 525 |
private: |
655 | 526 |
Ref& _map; |
656 | 527 |
}; |
657 | 528 |
|
658 | 529 |
template <typename Digraph, typename Item, typename RefMap, |
659 | 530 |
typename CrossRef> |
660 | 531 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
661 | 532 |
public: |
662 | 533 |
|
663 | 534 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
664 | 535 |
|
665 | 536 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
666 | 537 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
667 | 538 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
668 | 539 |
_cmap.set(refMap[it], it); |
669 | 540 |
} |
670 | 541 |
} |
671 | 542 |
|
672 | 543 |
private: |
673 | 544 |
CrossRef& _cmap; |
674 | 545 |
}; |
675 | 546 |
|
676 | 547 |
template <typename Digraph, typename Enable = void> |
677 | 548 |
struct DigraphCopySelector { |
678 | 549 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
679 | 550 |
static void copy(Digraph &to, const From& from, |
680 | 551 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
681 | 552 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
682 | 553 |
nodeRefMap[it] = to.addNode(); |
683 | 554 |
} |
684 | 555 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
685 | 556 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
686 | 557 |
nodeRefMap[from.target(it)]); |
687 | 558 |
} |
688 | 559 |
} |
689 | 560 |
}; |
690 | 561 |
|
691 | 562 |
template <typename Digraph> |
692 | 563 |
struct DigraphCopySelector< |
693 | 564 |
Digraph, |
694 | 565 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
695 | 566 |
{ |
696 | 567 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
697 | 568 |
static void copy(Digraph &to, const From& from, |
698 | 569 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
699 | 570 |
to.build(from, nodeRefMap, arcRefMap); |
700 | 571 |
} |
701 | 572 |
}; |
702 | 573 |
|
703 | 574 |
template <typename Graph, typename Enable = void> |
704 | 575 |
struct GraphCopySelector { |
705 | 576 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
706 | 577 |
static void copy(Graph &to, const From& from, |
707 | 578 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
708 | 579 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
709 | 580 |
nodeRefMap[it] = to.addNode(); |
710 | 581 |
} |
711 | 582 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
712 | 583 |
edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
713 | 584 |
nodeRefMap[from.target(it)]); |
714 | 585 |
} |
715 | 586 |
} |
716 | 587 |
}; |
717 | 588 |
|
718 | 589 |
template <typename Graph> |
719 | 590 |
struct GraphCopySelector< |
720 | 591 |
Graph, |
721 | 592 |
typename enable_if<typename Graph::BuildTag, void>::type> |
722 | 593 |
{ |
723 | 594 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
724 | 595 |
static void copy(Graph &to, const From& from, |
725 | 596 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
726 | 597 |
to.build(from, nodeRefMap, edgeRefMap); |
727 | 598 |
} |
728 | 599 |
}; |
729 | 600 |
|
730 |
template <typename BpGraph, typename Enable = void> |
|
731 |
struct BpGraphCopySelector { |
|
732 |
template <typename From, typename RedRefMap, |
|
733 |
typename BlueRefMap, typename EdgeRefMap> |
|
734 |
static void copy(BpGraph &to, const From& from, |
|
735 |
RedRefMap& redRefMap, BlueRefMap& blueRefMap, |
|
736 |
EdgeRefMap& edgeRefMap) { |
|
737 |
for (typename From::RedIt it(from); it != INVALID; ++it) { |
|
738 |
redRefMap[it] = to.addRed(); |
|
739 |
} |
|
740 |
for (typename From::BlueIt it(from); it != INVALID; ++it) { |
|
741 |
blueRefMap[it] = to.addBlue(); |
|
742 |
} |
|
743 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
|
744 |
edgeRefMap[it] = to.addArc(redRefMap[from.red(it)], |
|
745 |
blueRefMap[from.blue(it)]); |
|
746 |
} |
|
747 |
} |
|
748 |
}; |
|
749 |
|
|
750 |
template <typename BpGraph> |
|
751 |
struct BpGraphCopySelector< |
|
752 |
BpGraph, |
|
753 |
typename enable_if<typename BpGraph::BuildTag, void>::type> |
|
754 |
{ |
|
755 |
template <typename From, typename RedRefMap, |
|
756 |
typename BlueRefMap, typename EdgeRefMap> |
|
757 |
static void copy(BpGraph &to, const From& from, |
|
758 |
RedRefMap& redRefMap, BlueRefMap& blueRefMap, |
|
759 |
EdgeRefMap& edgeRefMap) { |
|
760 |
to.build(from, redRefMap, blueRefMap, edgeRefMap); |
|
761 |
} |
|
762 |
}; |
|
763 |
|
|
764 |
|
|
765 | 601 |
} |
766 | 602 |
|
767 | 603 |
/// \brief Class to copy a digraph. |
768 | 604 |
/// |
769 | 605 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
770 | 606 |
/// simplest way of using it is through the \c copyDigraph() function. |
607 |
/// |
|
608 |
/// This class not just make a copy of a graph, but it can create |
|
609 |
/// references and cross references between the nodes and arcs of |
|
610 |
/// the two graphs, it can copy maps for use with the newly created |
|
611 |
/// graph and copy nodes and arcs. |
|
612 |
/// |
|
613 |
/// To make a copy from a graph, first an instance of DigraphCopy |
|
614 |
/// should be created, then the data belongs to the graph should |
|
615 |
/// assigned to copy. In the end, the \c run() member should be |
|
616 |
/// called. |
|
617 |
/// |
|
618 |
/// The next code copies a graph with several data: |
|
619 |
///\code |
|
620 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
621 |
/// // create a reference for the nodes |
|
622 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
|
623 |
/// dc.nodeRef(nr); |
|
624 |
/// // create a cross reference (inverse) for the arcs |
|
625 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
|
626 |
/// dc.arcCrossRef(acr); |
|
627 |
/// // copy an arc map |
|
628 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
629 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
630 |
/// dc.arcMap(namap, oamap); |
|
631 |
/// // copy a node |
|
632 |
/// OrigGraph::Node on; |
|
633 |
/// NewGraph::Node nn; |
|
634 |
/// dc.node(nn, on); |
|
635 |
/// // Executions of copy |
|
636 |
/// dc.run(); |
|
637 |
///\endcode |
|
771 | 638 |
template <typename To, typename From> |
772 | 639 |
class DigraphCopy { |
773 | 640 |
private: |
774 | 641 |
|
775 | 642 |
typedef typename From::Node Node; |
776 | 643 |
typedef typename From::NodeIt NodeIt; |
777 | 644 |
typedef typename From::Arc Arc; |
778 | 645 |
typedef typename From::ArcIt ArcIt; |
779 | 646 |
|
780 | 647 |
typedef typename To::Node TNode; |
781 | 648 |
typedef typename To::Arc TArc; |
782 | 649 |
|
783 | 650 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
784 | 651 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
785 | 652 |
|
786 | 653 |
|
787 | 654 |
public: |
788 | 655 |
|
789 | 656 |
|
790 | 657 |
/// \brief Constructor for the DigraphCopy. |
791 | 658 |
/// |
792 | 659 |
/// It copies the content of the \c _from digraph into the |
793 | 660 |
/// \c _to digraph. |
794 |
DigraphCopy(To& _to, const From& _from) |
|
795 |
: from(_from), to(_to) {} |
|
661 |
DigraphCopy(To& to, const From& from) |
|
662 |
: _from(from), _to(to) {} |
|
796 | 663 |
|
797 | 664 |
/// \brief Destructor of the DigraphCopy |
798 | 665 |
/// |
799 | 666 |
/// Destructor of the DigraphCopy |
800 | 667 |
~DigraphCopy() { |
801 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
|
802 |
delete nodeMapCopies[i]; |
|
668 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
669 |
delete _node_maps[i]; |
|
803 | 670 |
} |
804 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) { |
|
805 |
delete arcMapCopies[i]; |
|
671 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
672 |
delete _arc_maps[i]; |
|
806 | 673 |
} |
807 | 674 |
|
808 | 675 |
} |
809 | 676 |
|
810 | 677 |
/// \brief Copies the node references into the given map. |
811 | 678 |
/// |
812 |
/// Copies the node references into the given map. |
|
679 |
/// Copies the node references into the given map. The parameter |
|
680 |
/// should be a map, which key type is the Node type of the source |
|
681 |
/// graph, while the value type is the Node type of the |
|
682 |
/// destination graph. |
|
813 | 683 |
template <typename NodeRef> |
814 | 684 |
DigraphCopy& nodeRef(NodeRef& map) { |
815 |
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, |
|
816 |
NodeRefMap, NodeRef>(map)); |
|
685 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
|
686 |
NodeRefMap, NodeRef>(map)); |
|
817 | 687 |
return *this; |
818 | 688 |
} |
819 | 689 |
|
820 | 690 |
/// \brief Copies the node cross references into the given map. |
821 | 691 |
/// |
822 | 692 |
/// Copies the node cross references (reverse references) into |
823 |
/// the given map. |
|
693 |
/// the given map. The parameter should be a map, which key type |
|
694 |
/// is the Node type of the destination graph, while the value type is |
|
695 |
/// the Node type of the source graph. |
|
824 | 696 |
template <typename NodeCrossRef> |
825 | 697 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
826 |
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node, |
|
827 |
NodeRefMap, NodeCrossRef>(map)); |
|
698 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
|
699 |
NodeRefMap, NodeCrossRef>(map)); |
|
828 | 700 |
return *this; |
829 | 701 |
} |
830 | 702 |
|
831 | 703 |
/// \brief Make copy of the given map. |
832 | 704 |
/// |
833 |
/// Makes copy of the given map for the newly created digraph. |
|
834 |
/// The new map's key type is the to digraph's node type, |
|
835 |
/// and the copied map's key type is the from digraph's node |
|
836 |
/// type. |
|
705 |
/// Makes copy of the given map for the newly created digraph. |
|
706 |
/// The new map's key type is the destination graph's node type, |
|
707 |
/// and the copied map's key type is the source graph's node type. |
|
837 | 708 |
template <typename ToMap, typename FromMap> |
838 | 709 |
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
839 |
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, |
|
840 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
710 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
|
711 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
841 | 712 |
return *this; |
842 | 713 |
} |
843 | 714 |
|
844 | 715 |
/// \brief Make a copy of the given node. |
845 | 716 |
/// |
846 | 717 |
/// Make a copy of the given node. |
847 | 718 |
DigraphCopy& node(TNode& tnode, const Node& snode) { |
848 |
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, |
|
849 |
NodeRefMap, TNode>(tnode, snode)); |
|
719 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
|
720 |
NodeRefMap, TNode>(tnode, snode)); |
|
850 | 721 |
return *this; |
851 | 722 |
} |
852 | 723 |
|
853 | 724 |
/// \brief Copies the arc references into the given map. |
854 | 725 |
/// |
855 | 726 |
/// Copies the arc references into the given map. |
856 | 727 |
template <typename ArcRef> |
857 | 728 |
DigraphCopy& arcRef(ArcRef& map) { |
858 |
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, |
|
859 |
ArcRefMap, ArcRef>(map)); |
|
729 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
|
730 |
ArcRefMap, ArcRef>(map)); |
|
860 | 731 |
return *this; |
861 | 732 |
} |
862 | 733 |
|
863 | 734 |
/// \brief Copies the arc cross references into the given map. |
864 | 735 |
/// |
865 | 736 |
/// Copies the arc cross references (reverse references) into |
866 | 737 |
/// the given map. |
867 | 738 |
template <typename ArcCrossRef> |
868 | 739 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
869 |
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc, |
|
870 |
ArcRefMap, ArcCrossRef>(map)); |
|
740 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
|
741 |
ArcRefMap, ArcCrossRef>(map)); |
|
871 | 742 |
return *this; |
872 | 743 |
} |
873 | 744 |
|
874 | 745 |
/// \brief Make copy of the given map. |
875 | 746 |
/// |
876 | 747 |
/// Makes copy of the given map for the newly created digraph. |
877 | 748 |
/// The new map's key type is the to digraph's arc type, |
878 | 749 |
/// and the copied map's key type is the from digraph's arc |
879 | 750 |
/// type. |
880 | 751 |
template <typename ToMap, typename FromMap> |
881 | 752 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
882 |
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, |
|
883 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
753 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
|
754 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
884 | 755 |
return *this; |
885 | 756 |
} |
886 | 757 |
|
887 | 758 |
/// \brief Make a copy of the given arc. |
888 | 759 |
/// |
889 | 760 |
/// Make a copy of the given arc. |
890 | 761 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) { |
891 |
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, |
|
892 |
ArcRefMap, TArc>(tarc, sarc)); |
|
762 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
|
763 |
ArcRefMap, TArc>(tarc, sarc)); |
|
893 | 764 |
return *this; |
894 | 765 |
} |
895 | 766 |
|
896 | 767 |
/// \brief Executes the copies. |
897 | 768 |
/// |
898 | 769 |
/// Executes the copies. |
899 | 770 |
void run() { |
900 |
NodeRefMap nodeRefMap(from); |
|
901 |
ArcRefMap arcRefMap(from); |
|
902 |
_digraph_utils_bits::DigraphCopySelector<To>:: |
|
903 |
copy(to, from, nodeRefMap, arcRefMap); |
|
904 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
|
905 |
nodeMapCopies[i]->copy(from, nodeRefMap); |
|
771 |
NodeRefMap nodeRefMap(_from); |
|
772 |
ArcRefMap arcRefMap(_from); |
|
773 |
_graph_utils_bits::DigraphCopySelector<To>:: |
|
774 |
copy(_to, _from, nodeRefMap, arcRefMap); |
|
775 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
776 |
_node_maps[i]->copy(_from, nodeRefMap); |
|
906 | 777 |
} |
907 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) { |
|
908 |
arcMapCopies[i]->copy(from, arcRefMap); |
|
778 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
779 |
_arc_maps[i]->copy(_from, arcRefMap); |
|
909 | 780 |
} |
910 | 781 |
} |
911 | 782 |
|
912 | 783 |
protected: |
913 | 784 |
|
914 | 785 |
|
915 |
const From& from; |
|
916 |
To& to; |
|
786 |
const From& _from; |
|
787 |
To& _to; |
|
917 | 788 |
|
918 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
919 |
nodeMapCopies; |
|
789 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
790 |
_node_maps; |
|
920 | 791 |
|
921 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
922 |
arcMapCopies; |
|
792 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
793 |
_arc_maps; |
|
923 | 794 |
|
924 | 795 |
}; |
925 | 796 |
|
926 | 797 |
/// \brief Copy a digraph to another digraph. |
927 | 798 |
/// |
928 |
/// Copy a digraph to another digraph. |
|
929 |
/// The usage of the function: |
|
930 |
/// |
|
799 |
/// Copy a digraph to another digraph. The complete usage of the |
|
800 |
/// function is detailed in the DigraphCopy class, but a short |
|
801 |
/// example shows a basic work: |
|
931 | 802 |
///\code |
932 | 803 |
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
933 | 804 |
///\endcode |
934 | 805 |
/// |
935 | 806 |
/// After the copy the \c nr map will contain the mapping from the |
936 | 807 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
937 | 808 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
938 | 809 |
/// to the arcs of the \c from digraph. |
939 | 810 |
/// |
940 | 811 |
/// \see DigraphCopy |
941 | 812 |
template <typename To, typename From> |
942 | 813 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) { |
943 | 814 |
return DigraphCopy<To, From>(to, from); |
944 | 815 |
} |
945 | 816 |
|
946 |
/// \brief Class to copy |
|
817 |
/// \brief Class to copy a graph. |
|
947 | 818 |
/// |
948 |
/// Class to copy an graph to another digraph (duplicate a digraph). |
|
949 |
/// The simplest way of using it is through the \c copyGraph() function. |
|
819 |
/// Class to copy a graph to another graph (duplicate a graph). The |
|
820 |
/// simplest way of using it is through the \c copyGraph() function. |
|
821 |
/// |
|
822 |
/// This class not just make a copy of a graph, but it can create |
|
823 |
/// references and cross references between the nodes, edges and arcs of |
|
824 |
/// the two graphs, it can copy maps for use with the newly created |
|
825 |
/// graph and copy nodes, edges and arcs. |
|
826 |
/// |
|
827 |
/// To make a copy from a graph, first an instance of GraphCopy |
|
828 |
/// should be created, then the data belongs to the graph should |
|
829 |
/// assigned to copy. In the end, the \c run() member should be |
|
830 |
/// called. |
|
831 |
/// |
|
832 |
/// The next code copies a graph with several data: |
|
833 |
///\code |
|
834 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
835 |
/// // create a reference for the nodes |
|
836 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
|
837 |
/// dc.nodeRef(nr); |
|
838 |
/// // create a cross reference (inverse) for the edges |
|
839 |
/// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph); |
|
840 |
/// dc.edgeCrossRef(ecr); |
|
841 |
/// // copy an arc map |
|
842 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
843 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
844 |
/// dc.arcMap(namap, oamap); |
|
845 |
/// // copy a node |
|
846 |
/// OrigGraph::Node on; |
|
847 |
/// NewGraph::Node nn; |
|
848 |
/// dc.node(nn, on); |
|
849 |
/// // Executions of copy |
|
850 |
/// dc.run(); |
|
851 |
///\endcode |
|
950 | 852 |
template <typename To, typename From> |
951 | 853 |
class GraphCopy { |
952 | 854 |
private: |
953 | 855 |
|
954 | 856 |
typedef typename From::Node Node; |
955 | 857 |
typedef typename From::NodeIt NodeIt; |
956 | 858 |
typedef typename From::Arc Arc; |
957 | 859 |
typedef typename From::ArcIt ArcIt; |
958 | 860 |
typedef typename From::Edge Edge; |
959 | 861 |
typedef typename From::EdgeIt EdgeIt; |
960 | 862 |
|
961 | 863 |
typedef typename To::Node TNode; |
962 | 864 |
typedef typename To::Arc TArc; |
963 | 865 |
typedef typename To::Edge TEdge; |
964 | 866 |
|
965 | 867 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
966 | 868 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
967 | 869 |
|
968 | 870 |
struct ArcRefMap { |
969 |
ArcRefMap(const To& _to, const From& _from, |
|
970 |
const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref) |
|
971 |
: to(_to), from(_from), |
|
972 |
edge_ref(_edge_ref), node_ref(_node_ref) {} |
|
871 |
ArcRefMap(const To& to, const From& from, |
|
872 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
|
873 |
: _to(to), _from(from), |
|
874 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
|
973 | 875 |
|
974 | 876 |
typedef typename From::Arc Key; |
975 | 877 |
typedef typename To::Arc Value; |
976 | 878 |
|
977 | 879 |
Value operator[](const Key& key) const { |
978 | 880 |
bool forward = |
979 |
(from.direction(key) == |
|
980 |
(node_ref[from.source(static_cast<const Edge&>(key))] == |
|
981 |
to.source(edge_ref[static_cast<const Edge&>(key)]))); |
|
982 |
return to.direct(edge_ref[key], forward); |
|
881 |
(_from.direction(key) == |
|
882 |
(_node_ref[_from.source(key)] == _to.source(_edge_ref[key]))); |
|
883 |
return _to.direct(_edge_ref[key], forward); |
|
983 | 884 |
} |
984 | 885 |
|
985 |
const To& to; |
|
986 |
const From& from; |
|
987 |
const EdgeRefMap& edge_ref; |
|
988 |
const NodeRefMap& node_ref; |
|
886 |
const To& _to; |
|
887 |
const From& _from; |
|
888 |
const EdgeRefMap& _edge_ref; |
|
889 |
const NodeRefMap& _node_ref; |
|
989 | 890 |
}; |
990 | 891 |
|
991 | 892 |
|
992 | 893 |
public: |
993 | 894 |
|
994 | 895 |
|
995 |
/// \brief Constructor for the |
|
896 |
/// \brief Constructor for the GraphCopy. |
|
996 | 897 |
/// |
997 |
/// It copies the content of the \c _from digraph into the |
|
998 |
/// \c _to digraph. |
|
999 |
GraphCopy(To& _to, const From& _from) |
|
1000 |
: from(_from), to(_to) {} |
|
898 |
/// It copies the content of the \c _from graph into the |
|
899 |
/// \c _to graph. |
|
900 |
GraphCopy(To& to, const From& from) |
|
901 |
: _from(from), _to(to) {} |
|
1001 | 902 |
|
1002 |
/// \brief Destructor of the |
|
903 |
/// \brief Destructor of the GraphCopy |
|
1003 | 904 |
/// |
1004 |
/// Destructor of the |
|
905 |
/// Destructor of the GraphCopy |
|
1005 | 906 |
~GraphCopy() { |
1006 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
|
1007 |
delete nodeMapCopies[i]; |
|
907 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
908 |
delete _node_maps[i]; |
|
1008 | 909 |
} |
1009 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) { |
|
1010 |
delete arcMapCopies[i]; |
|
910 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
911 |
delete _arc_maps[i]; |
|
1011 | 912 |
} |
1012 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
|
1013 |
delete edgeMapCopies[i]; |
|
913 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
|
914 |
delete _edge_maps[i]; |
|
1014 | 915 |
} |
1015 | 916 |
|
1016 | 917 |
} |
1017 | 918 |
|
1018 | 919 |
/// \brief Copies the node references into the given map. |
1019 | 920 |
/// |
1020 | 921 |
/// Copies the node references into the given map. |
1021 | 922 |
template <typename NodeRef> |
1022 | 923 |
GraphCopy& nodeRef(NodeRef& map) { |
1023 |
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, |
|
1024 |
NodeRefMap, NodeRef>(map)); |
|
924 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
|
925 |
NodeRefMap, NodeRef>(map)); |
|
1025 | 926 |
return *this; |
1026 | 927 |
} |
1027 | 928 |
|
1028 | 929 |
/// \brief Copies the node cross references into the given map. |
1029 | 930 |
/// |
1030 | 931 |
/// Copies the node cross references (reverse references) into |
1031 | 932 |
/// the given map. |
1032 | 933 |
template <typename NodeCrossRef> |
1033 | 934 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
1034 |
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node, |
|
1035 |
NodeRefMap, NodeCrossRef>(map)); |
|
935 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
|
936 |
NodeRefMap, NodeCrossRef>(map)); |
|
1036 | 937 |
return *this; |
1037 | 938 |
} |
1038 | 939 |
|
1039 | 940 |
/// \brief Make copy of the given map. |
1040 | 941 |
/// |
1041 |
/// Makes copy of the given map for the newly created digraph. |
|
1042 |
/// The new map's key type is the to digraph's node type, |
|
1043 |
/// |
|
942 |
/// Makes copy of the given map for the newly created graph. |
|
943 |
/// The new map's key type is the to graph's node type, |
|
944 |
/// and the copied map's key type is the from graph's node |
|
1044 | 945 |
/// type. |
1045 | 946 |
template <typename ToMap, typename FromMap> |
1046 | 947 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
1047 |
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, |
|
1048 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
948 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
|
949 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
1049 | 950 |
return *this; |
1050 | 951 |
} |
1051 | 952 |
|
1052 | 953 |
/// \brief Make a copy of the given node. |
1053 | 954 |
/// |
1054 | 955 |
/// Make a copy of the given node. |
1055 | 956 |
GraphCopy& node(TNode& tnode, const Node& snode) { |
1056 |
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, |
|
1057 |
NodeRefMap, TNode>(tnode, snode)); |
|
957 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
|
958 |
NodeRefMap, TNode>(tnode, snode)); |
|
1058 | 959 |
return *this; |
1059 | 960 |
} |
1060 | 961 |
|
1061 | 962 |
/// \brief Copies the arc references into the given map. |
1062 | 963 |
/// |
1063 | 964 |
/// Copies the arc references into the given map. |
1064 | 965 |
template <typename ArcRef> |
1065 | 966 |
GraphCopy& arcRef(ArcRef& map) { |
1066 |
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, |
|
1067 |
ArcRefMap, ArcRef>(map)); |
|
967 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
|
968 |
ArcRefMap, ArcRef>(map)); |
|
1068 | 969 |
return *this; |
1069 | 970 |
} |
1070 | 971 |
|
1071 | 972 |
/// \brief Copies the arc cross references into the given map. |
1072 | 973 |
/// |
1073 | 974 |
/// Copies the arc cross references (reverse references) into |
1074 | 975 |
/// the given map. |
1075 | 976 |
template <typename ArcCrossRef> |
1076 | 977 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
1077 |
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc, |
|
1078 |
ArcRefMap, ArcCrossRef>(map)); |
|
978 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
|
979 |
ArcRefMap, ArcCrossRef>(map)); |
|
1079 | 980 |
return *this; |
1080 | 981 |
} |
1081 | 982 |
|
1082 | 983 |
/// \brief Make copy of the given map. |
1083 | 984 |
/// |
1084 |
/// Makes copy of the given map for the newly created digraph. |
|
1085 |
/// The new map's key type is the to digraph's arc type, |
|
1086 |
/// |
|
985 |
/// Makes copy of the given map for the newly created graph. |
|
986 |
/// The new map's key type is the to graph's arc type, |
|
987 |
/// and the copied map's key type is the from graph's arc |
|
1087 | 988 |
/// type. |
1088 | 989 |
template <typename ToMap, typename FromMap> |
1089 | 990 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
1090 |
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, |
|
1091 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
991 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
|
992 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
1092 | 993 |
return *this; |
1093 | 994 |
} |
1094 | 995 |
|
1095 | 996 |
/// \brief Make a copy of the given arc. |
1096 | 997 |
/// |
1097 | 998 |
/// Make a copy of the given arc. |
1098 | 999 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) { |
1099 |
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, |
|
1100 |
ArcRefMap, TArc>(tarc, sarc)); |
|
1000 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
|
1001 |
ArcRefMap, TArc>(tarc, sarc)); |
|
1101 | 1002 |
return *this; |
1102 | 1003 |
} |
1103 | 1004 |
|
1104 | 1005 |
/// \brief Copies the edge references into the given map. |
1105 | 1006 |
/// |
1106 | 1007 |
/// Copies the edge references into the given map. |
1107 | 1008 |
template <typename EdgeRef> |
1108 | 1009 |
GraphCopy& edgeRef(EdgeRef& map) { |
1109 |
edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge, |
|
1110 |
EdgeRefMap, EdgeRef>(map)); |
|
1010 |
_edge_maps.push_back(new _graph_utils_bits::RefCopy<From, Edge, |
|
1011 |
EdgeRefMap, EdgeRef>(map)); |
|
1111 | 1012 |
return *this; |
1112 | 1013 |
} |
1113 | 1014 |
|
1114 | 1015 |
/// \brief Copies the edge cross references into the given map. |
1115 | 1016 |
/// |
1116 | 1017 |
/// Copies the edge cross references (reverse |
1117 | 1018 |
/// references) into the given map. |
1118 | 1019 |
template <typename EdgeCrossRef> |
1119 | 1020 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
1120 |
edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
1121 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
|
1021 |
_edge_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, |
|
1022 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
|
1122 | 1023 |
return *this; |
1123 | 1024 |
} |
1124 | 1025 |
|
1125 | 1026 |
/// \brief Make copy of the given map. |
1126 | 1027 |
/// |
1127 |
/// Makes copy of the given map for the newly created digraph. |
|
1128 |
/// The new map's key type is the to digraph's edge type, |
|
1129 |
/// |
|
1028 |
/// Makes copy of the given map for the newly created graph. |
|
1029 |
/// The new map's key type is the to graph's edge type, |
|
1030 |
/// and the copied map's key type is the from graph's edge |
|
1130 | 1031 |
/// type. |
1131 | 1032 |
template <typename ToMap, typename FromMap> |
1132 | 1033 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
1133 |
edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge, |
|
1134 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
|
1034 |
_edge_maps.push_back(new _graph_utils_bits::MapCopy<From, Edge, |
|
1035 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
|
1135 | 1036 |
return *this; |
1136 | 1037 |
} |
1137 | 1038 |
|
1138 | 1039 |
/// \brief Make a copy of the given edge. |
1139 | 1040 |
/// |
1140 | 1041 |
/// Make a copy of the given edge. |
1141 | 1042 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
1142 |
edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge, |
|
1143 |
EdgeRefMap, TEdge>(tedge, sedge)); |
|
1043 |
_edge_maps.push_back(new _graph_utils_bits::ItemCopy<From, Edge, |
|
1044 |
EdgeRefMap, TEdge>(tedge, sedge)); |
|
1144 | 1045 |
return *this; |
1145 | 1046 |
} |
1146 | 1047 |
|
1147 | 1048 |
/// \brief Executes the copies. |
1148 | 1049 |
/// |
1149 | 1050 |
/// Executes the copies. |
1150 | 1051 |
void run() { |
1151 |
NodeRefMap nodeRefMap(from); |
|
1152 |
EdgeRefMap edgeRefMap(from); |
|
1153 |
ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap); |
|
1154 |
_digraph_utils_bits::GraphCopySelector<To>:: |
|
1155 |
copy(to, from, nodeRefMap, edgeRefMap); |
|
1156 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
|
1157 |
|
|
1052 |
NodeRefMap nodeRefMap(_from); |
|
1053 |
EdgeRefMap edgeRefMap(_from); |
|
1054 |
ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap); |
|
1055 |
_graph_utils_bits::GraphCopySelector<To>:: |
|
1056 |
copy(_to, _from, nodeRefMap, edgeRefMap); |
|
1057 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
|
1058 |
_node_maps[i]->copy(_from, nodeRefMap); |
|
1158 | 1059 |
} |
1159 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
|
1160 |
edgeMapCopies[i]->copy(from, edgeRefMap); |
|
1060 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
|
1061 |
_edge_maps[i]->copy(_from, edgeRefMap); |
|
1161 | 1062 |
} |
1162 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) { |
|
1163 |
arcMapCopies[i]->copy(from, arcRefMap); |
|
1063 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
|
1064 |
_arc_maps[i]->copy(_from, arcRefMap); |
|
1164 | 1065 |
} |
1165 | 1066 |
} |
1166 | 1067 |
|
1167 | 1068 |
private: |
1168 | 1069 |
|
1169 |
const From& from; |
|
1170 |
To& to; |
|
1070 |
const From& _from; |
|
1071 |
To& _to; |
|
1171 | 1072 |
|
1172 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
1173 |
nodeMapCopies; |
|
1073 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
1074 |
_node_maps; |
|
1174 | 1075 |
|
1175 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
1176 |
arcMapCopies; |
|
1076 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
1077 |
_arc_maps; |
|
1177 | 1078 |
|
1178 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
|
1179 |
edgeMapCopies; |
|
1079 |
std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
|
1080 |
_edge_maps; |
|
1180 | 1081 |
|
1181 | 1082 |
}; |
1182 | 1083 |
|
1183 |
/// \brief Copy |
|
1084 |
/// \brief Copy a graph to another graph. |
|
1184 | 1085 |
/// |
1185 |
/// Copy an graph to another digraph. |
|
1186 |
/// The usage of the function: |
|
1187 |
/// |
|
1086 |
/// Copy a graph to another graph. The complete usage of the |
|
1087 |
/// function is detailed in the GraphCopy class, but a short |
|
1088 |
/// example shows a basic work: |
|
1188 | 1089 |
///\code |
1189 | 1090 |
/// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
1190 | 1091 |
///\endcode |
1191 | 1092 |
/// |
1192 | 1093 |
/// After the copy the \c nr map will contain the mapping from the |
1193 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
|
1194 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
|
1195 |
/// |
|
1094 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
|
1095 |
/// \c ecr will contain the mapping from the arcs of the \c to graph |
|
1096 |
/// to the arcs of the \c from graph. |
|
1196 | 1097 |
/// |
1197 | 1098 |
/// \see GraphCopy |
1198 | 1099 |
template <typename To, typename From> |
1199 | 1100 |
GraphCopy<To, From> |
1200 | 1101 |
copyGraph(To& to, const From& from) { |
1201 | 1102 |
return GraphCopy<To, From>(to, from); |
1202 | 1103 |
} |
1203 | 1104 |
|
1204 |
/// \brief Class to copy a bipartite digraph. |
|
1205 |
/// |
|
1206 |
/// Class to copy a bipartite digraph to another digraph |
|
1207 |
/// (duplicate a digraph). The simplest way of using it is through |
|
1208 |
/// the \c copyBpGraph() function. |
|
1209 |
template <typename To, typename From> |
|
1210 |
class BpGraphCopy { |
|
1211 |
private: |
|
1212 |
|
|
1213 |
typedef typename From::Node Node; |
|
1214 |
typedef typename From::Red Red; |
|
1215 |
typedef typename From::Blue Blue; |
|
1216 |
typedef typename From::NodeIt NodeIt; |
|
1217 |
typedef typename From::Arc Arc; |
|
1218 |
typedef typename From::ArcIt ArcIt; |
|
1219 |
typedef typename From::Edge Edge; |
|
1220 |
typedef typename From::EdgeIt EdgeIt; |
|
1221 |
|
|
1222 |
typedef typename To::Node TNode; |
|
1223 |
typedef typename To::Arc TArc; |
|
1224 |
typedef typename To::Edge TEdge; |
|
1225 |
|
|
1226 |
typedef typename From::template RedMap<TNode> RedRefMap; |
|
1227 |
typedef typename From::template BlueMap<TNode> BlueRefMap; |
|
1228 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
|
1229 |
|
|
1230 |
struct NodeRefMap { |
|
1231 |
NodeRefMap(const From& _from, const RedRefMap& _red_ref, |
|
1232 |
const BlueRefMap& _blue_ref) |
|
1233 |
: from(_from), red_ref(_red_ref), blue_ref(_blue_ref) {} |
|
1234 |
|
|
1235 |
typedef typename From::Node Key; |
|
1236 |
typedef typename To::Node Value; |
|
1237 |
|
|
1238 |
Value operator[](const Key& key) const { |
|
1239 |
return from.red(key) ? red_ref[key] : blue_ref[key]; |
|
1240 |
} |
|
1241 |
|
|
1242 |
const From& from; |
|
1243 |
const RedRefMap& red_ref; |
|
1244 |
const BlueRefMap& blue_ref; |
|
1245 |
}; |
|
1246 |
|
|
1247 |
struct ArcRefMap { |
|
1248 |
ArcRefMap(const To& _to, const From& _from, |
|
1249 |
const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref) |
|
1250 |
: to(_to), from(_from), |
|
1251 |
edge_ref(_edge_ref), node_ref(_node_ref) {} |
|
1252 |
|
|
1253 |
typedef typename From::Arc Key; |
|
1254 |
typedef typename To::Arc Value; |
|
1255 |
|
|
1256 |
Value operator[](const Key& key) const { |
|
1257 |
bool forward = |
|
1258 |
(from.direction(key) == |
|
1259 |
(node_ref[from.source(static_cast<const Edge&>(key))] == |
|
1260 |
to.source(edge_ref[static_cast<const Edge&>(key)]))); |
|
1261 |
return to.direct(edge_ref[key], forward); |
|
1262 |
} |
|
1263 |
|
|
1264 |
const To& to; |
|
1265 |
const From& from; |
|
1266 |
const EdgeRefMap& edge_ref; |
|
1267 |
const NodeRefMap& node_ref; |
|
1268 |
}; |
|
1269 |
|
|
1270 |
public: |
|
1271 |
|
|
1272 |
|
|
1273 |
/// \brief Constructor for the DigraphCopy. |
|
1274 |
/// |
|
1275 |
/// It copies the content of the \c _from digraph into the |
|
1276 |
/// \c _to digraph. |
|
1277 |
BpGraphCopy(To& _to, const From& _from) |
|
1278 |
: from(_from), to(_to) {} |
|
1279 |
|
|
1280 |
/// \brief Destructor of the DigraphCopy |
|
1281 |
/// |
|
1282 |
/// Destructor of the DigraphCopy |
|
1283 |
~BpGraphCopy() { |
|
1284 |
for (int i = 0; i < int(redMapCopies.size()); ++i) { |
|
1285 |
delete redMapCopies[i]; |
|
1286 |
} |
|
1287 |
for (int i = 0; i < int(blueMapCopies.size()); ++i) { |
|
1288 |
delete blueMapCopies[i]; |
|
1289 |
} |
|
1290 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
|
1291 |
delete nodeMapCopies[i]; |
|
1292 |
} |
|
1293 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) { |
|
1294 |
delete arcMapCopies[i]; |
|
1295 |
} |
|
1296 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
|
1297 |
delete edgeMapCopies[i]; |
|
1298 |
} |
|
1299 |
|
|
1300 |
} |
|
1301 |
|
|
1302 |
/// \brief Copies the A-node references into the given map. |
|
1303 |
/// |
|
1304 |
/// Copies the A-node references into the given map. |
|
1305 |
template <typename RedRef> |
|
1306 |
BpGraphCopy& redRef(RedRef& map) { |
|
1307 |
redMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Red, |
|
1308 |
RedRefMap, RedRef>(map)); |
|
1309 |
return *this; |
|
1310 |
} |
|
1311 |
|
|
1312 |
/// \brief Copies the A-node cross references into the given map. |
|
1313 |
/// |
|
1314 |
/// Copies the A-node cross references (reverse references) into |
|
1315 |
/// the given map. |
|
1316 |
template <typename RedCrossRef> |
|
1317 |
BpGraphCopy& redCrossRef(RedCrossRef& map) { |
|
1318 |
redMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
1319 |
Red, RedRefMap, RedCrossRef>(map)); |
|
1320 |
return *this; |
|
1321 |
} |
|
1322 |
|
|
1323 |
/// \brief Make copy of the given A-node map. |
|
1324 |
/// |
|
1325 |
/// Makes copy of the given map for the newly created digraph. |
|
1326 |
/// The new map's key type is the to digraph's node type, |
|
1327 |
/// and the copied map's key type is the from digraph's node |
|
1328 |
/// type. |
|
1329 |
template <typename ToMap, typename FromMap> |
|
1330 |
BpGraphCopy& redMap(ToMap& tmap, const FromMap& map) { |
|
1331 |
redMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Red, |
|
1332 |
RedRefMap, ToMap, FromMap>(tmap, map)); |
|
1333 |
return *this; |
|
1334 |
} |
|
1335 |
|
|
1336 |
/// \brief Copies the B-node references into the given map. |
|
1337 |
/// |
|
1338 |
/// Copies the B-node references into the given map. |
|
1339 |
template <typename BlueRef> |
|
1340 |
BpGraphCopy& blueRef(BlueRef& map) { |
|
1341 |
blueMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Blue, |
|
1342 |
BlueRefMap, BlueRef>(map)); |
|
1343 |
return *this; |
|
1344 |
} |
|
1345 |
|
|
1346 |
/// \brief Copies the B-node cross references into the given map. |
|
1347 |
/// |
|
1348 |
/// Copies the B-node cross references (reverse references) into |
|
1349 |
/// the given map. |
|
1350 |
template <typename BlueCrossRef> |
|
1351 |
BpGraphCopy& blueCrossRef(BlueCrossRef& map) { |
|
1352 |
blueMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
1353 |
Blue, BlueRefMap, BlueCrossRef>(map)); |
|
1354 |
return *this; |
|
1355 |
} |
|
1356 |
|
|
1357 |
/// \brief Make copy of the given B-node map. |
|
1358 |
/// |
|
1359 |
/// Makes copy of the given map for the newly created digraph. |
|
1360 |
/// The new map's key type is the to digraph's node type, |
|
1361 |
/// and the copied map's key type is the from digraph's node |
|
1362 |
/// type. |
|
1363 |
template <typename ToMap, typename FromMap> |
|
1364 |
BpGraphCopy& blueMap(ToMap& tmap, const FromMap& map) { |
|
1365 |
blueMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Blue, |
|
1366 |
BlueRefMap, ToMap, FromMap>(tmap, map)); |
|
1367 |
return *this; |
|
1368 |
} |
|
1369 |
/// \brief Copies the node references into the given map. |
|
1370 |
/// |
|
1371 |
/// Copies the node references into the given map. |
|
1372 |
template <typename NodeRef> |
|
1373 |
BpGraphCopy& nodeRef(NodeRef& map) { |
|
1374 |
nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node, |
|
1375 |
NodeRefMap, NodeRef>(map)); |
|
1376 |
return *this; |
|
1377 |
} |
|
1378 |
|
|
1379 |
/// \brief Copies the node cross references into the given map. |
|
1380 |
/// |
|
1381 |
/// Copies the node cross references (reverse references) into |
|
1382 |
/// the given map. |
|
1383 |
template <typename NodeCrossRef> |
|
1384 |
BpGraphCopy& nodeCrossRef(NodeCrossRef& map) { |
|
1385 |
nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node, |
|
1386 |
NodeRefMap, NodeCrossRef>(map)); |
|
1387 |
return *this; |
|
1388 |
} |
|
1389 |
|
|
1390 |
/// \brief Make copy of the given map. |
|
1391 |
/// |
|
1392 |
/// Makes copy of the given map for the newly created digraph. |
|
1393 |
/// The new map's key type is the to digraph's node type, |
|
1394 |
/// and the copied map's key type is the from digraph's node |
|
1395 |
/// type. |
|
1396 |
template <typename ToMap, typename FromMap> |
|
1397 |
BpGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
|
1398 |
nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node, |
|
1399 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
|
1400 |
return *this; |
|
1401 |
} |
|
1402 |
|
|
1403 |
/// \brief Make a copy of the given node. |
|
1404 |
/// |
|
1405 |
/// Make a copy of the given node. |
|
1406 |
BpGraphCopy& node(TNode& tnode, const Node& snode) { |
|
1407 |
nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node, |
|
1408 |
NodeRefMap, TNode>(tnode, snode)); |
|
1409 |
return *this; |
|
1410 |
} |
|
1411 |
|
|
1412 |
/// \brief Copies the arc references into the given map. |
|
1413 |
/// |
|
1414 |
/// Copies the arc references into the given map. |
|
1415 |
template <typename ArcRef> |
|
1416 |
BpGraphCopy& arcRef(ArcRef& map) { |
|
1417 |
arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc, |
|
1418 |
ArcRefMap, ArcRef>(map)); |
|
1419 |
return *this; |
|
1420 |
} |
|
1421 |
|
|
1422 |
/// \brief Copies the arc cross references into the given map. |
|
1423 |
/// |
|
1424 |
/// Copies the arc cross references (reverse references) into |
|
1425 |
/// the given map. |
|
1426 |
template <typename ArcCrossRef> |
|
1427 |
BpGraphCopy& arcCrossRef(ArcCrossRef& map) { |
|
1428 |
arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc, |
|
1429 |
ArcRefMap, ArcCrossRef>(map)); |
|
1430 |
return *this; |
|
1431 |
} |
|
1432 |
|
|
1433 |
/// \brief Make copy of the given map. |
|
1434 |
/// |
|
1435 |
/// Makes copy of the given map for the newly created digraph. |
|
1436 |
/// The new map's key type is the to digraph's arc type, |
|
1437 |
/// and the copied map's key type is the from digraph's arc |
|
1438 |
/// type. |
|
1439 |
template <typename ToMap, typename FromMap> |
|
1440 |
BpGraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
1441 |
arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc, |
|
1442 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
|
1443 |
return *this; |
|
1444 |
} |
|
1445 |
|
|
1446 |
/// \brief Make a copy of the given arc. |
|
1447 |
/// |
|
1448 |
/// Make a copy of the given arc. |
|
1449 |
BpGraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
1450 |
arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc, |
|
1451 |
ArcRefMap, TArc>(tarc, sarc)); |
|
1452 |
return *this; |
|
1453 |
} |
|
1454 |
|
|
1455 |
/// \brief Copies the edge references into the given map. |
|
1456 |
/// |
|
1457 |
/// Copies the edge references into the given map. |
|
1458 |
template <typename EdgeRef> |
|
1459 |
BpGraphCopy& edgeRef(EdgeRef& map) { |
|
1460 |
edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge, |
|
1461 |
EdgeRefMap, EdgeRef>(map)); |
|
1462 |
return *this; |
|
1463 |
} |
|
1464 |
|
|
1465 |
/// \brief Copies the edge cross references into the given map. |
|
1466 |
/// |
|
1467 |
/// Copies the edge cross references (reverse |
|
1468 |
/// references) into the given map. |
|
1469 |
template <typename EdgeCrossRef> |
|
1470 |
BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
|
1471 |
edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, |
|
1472 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
|
1473 |
return *this; |
|
1474 |
} |
|
1475 |
|
|
1476 |
/// \brief Make copy of the given map. |
|
1477 |
/// |
|
1478 |
/// Makes copy of the given map for the newly created digraph. |
|
1479 |
/// The new map's key type is the to digraph's edge type, |
|
1480 |
/// and the copied map's key type is the from digraph's edge |
|
1481 |
/// type. |
|
1482 |
template <typename ToMap, typename FromMap> |
|
1483 |
BpGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
|
1484 |
edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge, |
|
1485 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
|
1486 |
return *this; |
|
1487 |
} |
|
1488 |
|
|
1489 |
/// \brief Make a copy of the given edge. |
|
1490 |
/// |
|
1491 |
/// Make a copy of the given edge. |
|
1492 |
BpGraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
|
1493 |
edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge, |
|
1494 |
EdgeRefMap, TEdge>(tedge, sedge)); |
|
1495 |
return *this; |
|
1496 |
} |
|
1497 |
|
|
1498 |
/// \brief Executes the copies. |
|
1499 |
/// |
|
1500 |
/// Executes the copies. |
|
1501 |
void run() { |
|
1502 |
RedRefMap redRefMap(from); |
|
1503 |
BlueRefMap blueRefMap(from); |
|
1504 |
NodeRefMap nodeRefMap(from, redRefMap, blueRefMap); |
|
1505 |
EdgeRefMap edgeRefMap(from); |
|
1506 |
ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap); |
|
1507 |
_digraph_utils_bits::BpGraphCopySelector<To>:: |
|
1508 |
copy(to, from, redRefMap, blueRefMap, edgeRefMap); |
|
1509 |
for (int i = 0; i < int(redMapCopies.size()); ++i) { |
|
1510 |
redMapCopies[i]->copy(from, redRefMap); |
|
1511 |
} |
|
1512 |
for (int i = 0; i < int(blueMapCopies.size()); ++i) { |
|
1513 |
blueMapCopies[i]->copy(from, blueRefMap); |
|
1514 |
} |
|
1515 |
for (int i = 0; i < int(nodeMapCopies.size()); ++i) { |
|
1516 |
nodeMapCopies[i]->copy(from, nodeRefMap); |
|
1517 |
} |
|
1518 |
for (int i = 0; i < int(edgeMapCopies.size()); ++i) { |
|
1519 |
edgeMapCopies[i]->copy(from, edgeRefMap); |
|
1520 |
} |
|
1521 |
for (int i = 0; i < int(arcMapCopies.size()); ++i) { |
|
1522 |
arcMapCopies[i]->copy(from, arcRefMap); |
|
1523 |
} |
|
1524 |
} |
|
1525 |
|
|
1526 |
private: |
|
1527 |
|
|
1528 |
const From& from; |
|
1529 |
To& to; |
|
1530 |
|
|
1531 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Red, RedRefMap>* > |
|
1532 |
redMapCopies; |
|
1533 |
|
|
1534 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Blue, BlueRefMap>* > |
|
1535 |
blueMapCopies; |
|
1536 |
|
|
1537 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
|
1538 |
nodeMapCopies; |
|
1539 |
|
|
1540 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
|
1541 |
arcMapCopies; |
|
1542 |
|
|
1543 |
std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
|
1544 |
edgeMapCopies; |
|
1545 |
|
|
1546 |
}; |
|
1547 |
|
|
1548 |
/// \brief Copy a bipartite digraph to another digraph. |
|
1549 |
/// |
|
1550 |
/// Copy a bipartite digraph to another digraph. |
|
1551 |
/// The usage of the function: |
|
1552 |
/// |
|
1553 |
///\code |
|
1554 |
/// copyBpGraph(trg, src).redRef(anr).arcCrossRef(ecr).run(); |
|
1555 |
///\endcode |
|
1556 |
/// |
|
1557 |
/// After the copy the \c nr map will contain the mapping from the |
|
1558 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
|
1559 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
|
1560 |
/// to the arcs of the \c from digraph. |
|
1561 |
/// |
|
1562 |
/// \see BpGraphCopy |
|
1563 |
template <typename To, typename From> |
|
1564 |
BpGraphCopy<To, From> |
|
1565 |
copyBpGraph(To& to, const From& from) { |
|
1566 |
return BpGraphCopy<To, From>(to, from); |
|
1567 |
} |
|
1568 |
|
|
1569 |
|
|
1570 | 1105 |
/// @} |
1571 | 1106 |
|
1572 |
/// \addtogroup |
|
1107 |
/// \addtogroup graph_maps |
|
1573 | 1108 |
/// @{ |
1574 | 1109 |
|
1575 |
/// Provides an immutable and unique id for each item in the |
|
1110 |
/// Provides an immutable and unique id for each item in the graph. |
|
1576 | 1111 |
|
1577 | 1112 |
/// The IdMap class provides a unique and immutable id for each item of the |
1578 |
/// same type (e.g. node) in the |
|
1113 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
|
1579 | 1114 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
1580 | 1115 |
/// item (node) does not change (even if you delete other nodes). </ul> |
1581 | 1116 |
/// Through this map you get access (i.e. can read) the inner id values of |
1582 |
/// the items stored in the digraph. This map can be inverted with its member |
|
1583 |
/// class \c InverseMap. |
|
1117 |
/// the items stored in the graph. This map can be inverted with its member |
|
1118 |
/// class \c InverseMap or with the \c operator() member. |
|
1584 | 1119 |
/// |
1585 |
template <typename |
|
1120 |
template <typename _Graph, typename _Item> |
|
1586 | 1121 |
class IdMap { |
1587 | 1122 |
public: |
1588 |
typedef |
|
1123 |
typedef _Graph Graph; |
|
1589 | 1124 |
typedef int Value; |
1590 | 1125 |
typedef _Item Item; |
1591 | 1126 |
typedef _Item Key; |
1592 | 1127 |
|
1593 | 1128 |
/// \brief Constructor. |
1594 | 1129 |
/// |
1595 | 1130 |
/// Constructor of the map. |
1596 |
explicit IdMap(const |
|
1131 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
|
1597 | 1132 |
|
1598 | 1133 |
/// \brief Gives back the \e id of the item. |
1599 | 1134 |
/// |
1600 | 1135 |
/// Gives back the immutable and unique \e id of the item. |
1601 |
int operator[](const Item& item) const { return |
|
1136 |
int operator[](const Item& item) const { return _graph->id(item);} |
|
1602 | 1137 |
|
1603 | 1138 |
/// \brief Gives back the item by its id. |
1604 | 1139 |
/// |
1605 | 1140 |
/// Gives back the item by its id. |
1606 |
Item operator()(int id) { return |
|
1141 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
|
1607 | 1142 |
|
1608 | 1143 |
private: |
1609 |
const |
|
1144 |
const Graph* _graph; |
|
1610 | 1145 |
|
1611 | 1146 |
public: |
1612 | 1147 |
|
1613 | 1148 |
/// \brief The class represents the inverse of its owner (IdMap). |
1614 | 1149 |
/// |
1615 | 1150 |
/// The class represents the inverse of its owner (IdMap). |
1616 | 1151 |
/// \see inverse() |
1617 | 1152 |
class InverseMap { |
1618 | 1153 |
public: |
1619 | 1154 |
|
1620 | 1155 |
/// \brief Constructor. |
1621 | 1156 |
/// |
1622 | 1157 |
/// Constructor for creating an id-to-item map. |
1623 |
explicit InverseMap(const |
|
1158 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
|
1624 | 1159 |
|
1625 | 1160 |
/// \brief Constructor. |
1626 | 1161 |
/// |
1627 | 1162 |
/// Constructor for creating an id-to-item map. |
1628 |
explicit InverseMap(const IdMap& |
|
1163 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
|
1629 | 1164 |
|
1630 | 1165 |
/// \brief Gives back the given item from its id. |
1631 | 1166 |
/// |
1632 | 1167 |
/// Gives back the given item from its id. |
1633 | 1168 |
/// |
1634 |
Item operator[](int id) const { return |
|
1169 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
|
1635 | 1170 |
|
1636 | 1171 |
private: |
1637 |
const |
|
1172 |
const Graph* _graph; |
|
1638 | 1173 |
}; |
1639 | 1174 |
|
1640 | 1175 |
/// \brief Gives back the inverse of the map. |
1641 | 1176 |
/// |
1642 | 1177 |
/// Gives back the inverse of the IdMap. |
1643 |
InverseMap inverse() const { return InverseMap(* |
|
1178 |
InverseMap inverse() const { return InverseMap(*_graph);} |
|
1644 | 1179 |
|
1645 | 1180 |
}; |
1646 | 1181 |
|
1647 | 1182 |
|
1648 |
/// \brief General invertable |
|
1183 |
/// \brief General invertable graph-map type. |
|
1649 | 1184 |
|
1650 |
/// This type provides simple invertable |
|
1185 |
/// This type provides simple invertable graph-maps. |
|
1651 | 1186 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
1652 | 1187 |
/// and if a key is set to a new value then store it |
1653 | 1188 |
/// in the inverse map. |
1654 | 1189 |
/// |
1655 | 1190 |
/// The values of the map can be accessed |
1656 | 1191 |
/// with stl compatible forward iterator. |
1657 | 1192 |
/// |
1658 |
/// \param _Digraph The digraph type. |
|
1659 |
/// \param _Item The item type of the digraph. |
|
1193 |
/// \param _Graph The graph type. |
|
1194 |
/// \param _Item The item type of the graph. |
|
1660 | 1195 |
/// \param _Value The value type of the map. |
1661 | 1196 |
/// |
1662 | 1197 |
/// \see IterableValueMap |
1663 |
template <typename _Digraph, typename _Item, typename _Value> |
|
1664 |
class InvertableMap : protected DefaultMap<_Digraph, _Item, _Value> { |
|
1198 |
template <typename _Graph, typename _Item, typename _Value> |
|
1199 |
class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> { |
|
1665 | 1200 |
private: |
1666 | 1201 |
|
1667 |
typedef DefaultMap<_Digraph, _Item, _Value> Map; |
|
1668 |
typedef _Digraph Digraph; |
|
1202 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
|
1203 |
typedef _Graph Graph; |
|
1669 | 1204 |
|
1670 | 1205 |
typedef std::map<_Value, _Item> Container; |
1671 |
Container |
|
1206 |
Container _inv_map; |
|
1672 | 1207 |
|
1673 | 1208 |
public: |
1674 | 1209 |
|
1675 | 1210 |
/// The key type of InvertableMap (Node, Arc, Edge). |
1676 | 1211 |
typedef typename Map::Key Key; |
1677 | 1212 |
/// The value type of the InvertableMap. |
1678 | 1213 |
typedef typename Map::Value Value; |
1679 | 1214 |
|
1680 | 1215 |
|
1681 | 1216 |
|
1682 | 1217 |
/// \brief Constructor. |
1683 | 1218 |
/// |
1684 |
/// Construct a new InvertableMap for the |
|
1219 |
/// Construct a new InvertableMap for the graph. |
|
1685 | 1220 |
/// |
1686 |
explicit InvertableMap(const |
|
1221 |
explicit InvertableMap(const Graph& graph) : Map(graph) {} |
|
1687 | 1222 |
|
1688 | 1223 |
/// \brief Forward iterator for values. |
1689 | 1224 |
/// |
1690 | 1225 |
/// This iterator is an stl compatible forward |
1691 | 1226 |
/// iterator on the values of the map. The values can |
1692 | 1227 |
/// be accessed in the [beginValue, endValue) range. |
1693 | 1228 |
/// |
1694 | 1229 |
class ValueIterator |
1695 | 1230 |
: public std::iterator<std::forward_iterator_tag, Value> { |
1696 | 1231 |
friend class InvertableMap; |
1697 | 1232 |
private: |
1698 | 1233 |
ValueIterator(typename Container::const_iterator _it) |
1699 | 1234 |
: it(_it) {} |
1700 | 1235 |
public: |
1701 | 1236 |
|
1702 | 1237 |
ValueIterator() {} |
1703 | 1238 |
|
1704 | 1239 |
ValueIterator& operator++() { ++it; return *this; } |
1705 | 1240 |
ValueIterator operator++(int) { |
1706 | 1241 |
ValueIterator tmp(*this); |
1707 | 1242 |
operator++(); |
1708 | 1243 |
return tmp; |
1709 | 1244 |
} |
1710 | 1245 |
|
1711 | 1246 |
const Value& operator*() const { return it->first; } |
1712 | 1247 |
const Value* operator->() const { return &(it->first); } |
1713 | 1248 |
|
1714 | 1249 |
bool operator==(ValueIterator jt) const { return it == jt.it; } |
1715 | 1250 |
bool operator!=(ValueIterator jt) const { return it != jt.it; } |
1716 | 1251 |
|
1717 | 1252 |
private: |
1718 | 1253 |
typename Container::const_iterator it; |
1719 | 1254 |
}; |
1720 | 1255 |
|
1721 | 1256 |
/// \brief Returns an iterator to the first value. |
1722 | 1257 |
/// |
1723 | 1258 |
/// Returns an stl compatible iterator to the |
1724 | 1259 |
/// first value of the map. The values of the |
1725 | 1260 |
/// map can be accessed in the [beginValue, endValue) |
1726 | 1261 |
/// range. |
1727 | 1262 |
ValueIterator beginValue() const { |
1728 |
return ValueIterator( |
|
1263 |
return ValueIterator(_inv_map.begin()); |
|
1729 | 1264 |
} |
1730 | 1265 |
|
1731 | 1266 |
/// \brief Returns an iterator after the last value. |
1732 | 1267 |
/// |
1733 | 1268 |
/// Returns an stl compatible iterator after the |
1734 | 1269 |
/// last value of the map. The values of the |
1735 | 1270 |
/// map can be accessed in the [beginValue, endValue) |
1736 | 1271 |
/// range. |
1737 | 1272 |
ValueIterator endValue() const { |
1738 |
return ValueIterator( |
|
1273 |
return ValueIterator(_inv_map.end()); |
|
1739 | 1274 |
} |
1740 | 1275 |
|
1741 | 1276 |
/// \brief The setter function of the map. |
1742 | 1277 |
/// |
1743 | 1278 |
/// Sets the mapped value. |
1744 | 1279 |
void set(const Key& key, const Value& val) { |
1745 | 1280 |
Value oldval = Map::operator[](key); |
1746 |
typename Container::iterator it = invMap.find(oldval); |
|
1747 |
if (it != invMap.end() && it->second == key) { |
|
1748 |
|
|
1281 |
typename Container::iterator it = _inv_map.find(oldval); |
|
1282 |
if (it != _inv_map.end() && it->second == key) { |
|
1283 |
_inv_map.erase(it); |
|
1749 | 1284 |
} |
1750 |
|
|
1285 |
_inv_map.insert(make_pair(val, key)); |
|
1751 | 1286 |
Map::set(key, val); |
1752 | 1287 |
} |
1753 | 1288 |
|
1754 | 1289 |
/// \brief The getter function of the map. |
1755 | 1290 |
/// |
1756 | 1291 |
/// It gives back the value associated with the key. |
1757 | 1292 |
typename MapTraits<Map>::ConstReturnValue |
1758 | 1293 |
operator[](const Key& key) const { |
1759 | 1294 |
return Map::operator[](key); |
1760 | 1295 |
} |
1761 | 1296 |
|
1762 | 1297 |
/// \brief Gives back the item by its value. |
1763 | 1298 |
/// |
1764 | 1299 |
/// Gives back the item by its value. |
1765 | 1300 |
Key operator()(const Value& key) const { |
1766 |
typename Container::const_iterator it = invMap.find(key); |
|
1767 |
return it != invMap.end() ? it->second : INVALID; |
|
1301 |
typename Container::const_iterator it = _inv_map.find(key); |
|
1302 |
return it != _inv_map.end() ? it->second : INVALID; |
|
1768 | 1303 |
} |
1769 | 1304 |
|
1770 | 1305 |
protected: |
1771 | 1306 |
|
1772 | 1307 |
/// \brief Erase the key from the map. |
1773 | 1308 |
/// |
1774 | 1309 |
/// Erase the key to the map. It is called by the |
1775 | 1310 |
/// \c AlterationNotifier. |
1776 | 1311 |
virtual void erase(const Key& key) { |
1777 | 1312 |
Value val = Map::operator[](key); |
1778 |
typename Container::iterator it = invMap.find(val); |
|
1779 |
if (it != invMap.end() && it->second == key) { |
|
1780 |
|
|
1313 |
typename Container::iterator it = _inv_map.find(val); |
|
1314 |
if (it != _inv_map.end() && it->second == key) { |
|
1315 |
_inv_map.erase(it); |
|
1781 | 1316 |
} |
1782 | 1317 |
Map::erase(key); |
1783 | 1318 |
} |
1784 | 1319 |
|
1785 | 1320 |
/// \brief Erase more keys from the map. |
1786 | 1321 |
/// |
1787 | 1322 |
/// Erase more keys from the map. It is called by the |
1788 | 1323 |
/// \c AlterationNotifier. |
1789 | 1324 |
virtual void erase(const std::vector<Key>& keys) { |
1790 | 1325 |
for (int i = 0; i < int(keys.size()); ++i) { |
1791 | 1326 |
Value val = Map::operator[](keys[i]); |
1792 |
typename Container::iterator it = invMap.find(val); |
|
1793 |
if (it != invMap.end() && it->second == keys[i]) { |
|
1794 |
|
|
1327 |
typename Container::iterator it = _inv_map.find(val); |
|
1328 |
if (it != _inv_map.end() && it->second == keys[i]) { |
|
1329 |
_inv_map.erase(it); |
|
1795 | 1330 |
} |
1796 | 1331 |
} |
1797 | 1332 |
Map::erase(keys); |
1798 | 1333 |
} |
1799 | 1334 |
|
1800 | 1335 |
/// \brief Clear the keys from the map and inverse map. |
1801 | 1336 |
/// |
1802 | 1337 |
/// Clear the keys from the map and inverse map. It is called by the |
1803 | 1338 |
/// \c AlterationNotifier. |
1804 | 1339 |
virtual void clear() { |
1805 |
|
|
1340 |
_inv_map.clear(); |
|
1806 | 1341 |
Map::clear(); |
1807 | 1342 |
} |
1808 | 1343 |
|
1809 | 1344 |
public: |
1810 | 1345 |
|
1811 | 1346 |
/// \brief The inverse map type. |
1812 | 1347 |
/// |
1813 | 1348 |
/// The inverse of this map. The subscript operator of the map |
1814 | 1349 |
/// gives back always the item what was last assigned to the value. |
1815 | 1350 |
class InverseMap { |
1816 | 1351 |
public: |
1817 | 1352 |
/// \brief Constructor of the InverseMap. |
1818 | 1353 |
/// |
1819 | 1354 |
/// Constructor of the InverseMap. |
1820 |
explicit InverseMap(const InvertableMap& _inverted) |
|
1821 |
: inverted(_inverted) {} |
|
1355 |
explicit InverseMap(const InvertableMap& inverted) |
|
1356 |
: _inverted(inverted) {} |
|
1822 | 1357 |
|
1823 | 1358 |
/// The value type of the InverseMap. |
1824 | 1359 |
typedef typename InvertableMap::Key Value; |
1825 | 1360 |
/// The key type of the InverseMap. |
1826 | 1361 |
typedef typename InvertableMap::Value Key; |
1827 | 1362 |
|
1828 | 1363 |
/// \brief Subscript operator. |
1829 | 1364 |
/// |
1830 | 1365 |
/// Subscript operator. It gives back always the item |
1831 | 1366 |
/// what was last assigned to the value. |
1832 | 1367 |
Value operator[](const Key& key) const { |
1833 |
return |
|
1368 |
return _inverted(key); |
|
1834 | 1369 |
} |
1835 | 1370 |
|
1836 | 1371 |
private: |
1837 |
const InvertableMap& |
|
1372 |
const InvertableMap& _inverted; |
|
1838 | 1373 |
}; |
1839 | 1374 |
|
1840 | 1375 |
/// \brief It gives back the just readable inverse map. |
1841 | 1376 |
/// |
1842 | 1377 |
/// It gives back the just readable inverse map. |
1843 | 1378 |
InverseMap inverse() const { |
1844 | 1379 |
return InverseMap(*this); |
1845 | 1380 |
} |
1846 | 1381 |
|
1847 | 1382 |
|
1848 | 1383 |
|
1849 | 1384 |
}; |
1850 | 1385 |
|
1851 | 1386 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
1852 |
/// item in the |
|
1387 |
/// item in the graph. |
|
1853 | 1388 |
/// |
1854 | 1389 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
1855 | 1390 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
1856 |
/// |
|
1391 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
|
1857 | 1392 |
/// different ids <li>\b continuous: the range of the ids is the set of |
1858 | 1393 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
1859 | 1394 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
1860 | 1395 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
1861 |
/// with its member class \c InverseMap. |
|
1396 |
/// with its member class \c InverseMap, or with the \c operator() member. |
|
1862 | 1397 |
/// |
1863 |
/// \param |
|
1398 |
/// \param _Graph The graph class the \c DescriptorMap belongs to. |
|
1864 | 1399 |
/// \param _Item The Item is the Key of the Map. It may be Node, Arc or |
1865 | 1400 |
/// Edge. |
1866 |
template <typename _Digraph, typename _Item> |
|
1867 |
class DescriptorMap : protected DefaultMap<_Digraph, _Item, int> { |
|
1401 |
template <typename _Graph, typename _Item> |
|
1402 |
class DescriptorMap : protected DefaultMap<_Graph, _Item, int> { |
|
1868 | 1403 |
|
1869 | 1404 |
typedef _Item Item; |
1870 |
typedef DefaultMap< |
|
1405 |
typedef DefaultMap<_Graph, _Item, int> Map; |
|
1871 | 1406 |
|
1872 | 1407 |
public: |
1873 |
/// The digraph class of DescriptorMap. |
|
1874 |
typedef _Digraph Digraph; |
|
1408 |
/// The graph class of DescriptorMap. |
|
1409 |
typedef _Graph Graph; |
|
1875 | 1410 |
|
1876 | 1411 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
1877 | 1412 |
typedef typename Map::Key Key; |
1878 | 1413 |
/// The value type of DescriptorMap. |
1879 | 1414 |
typedef typename Map::Value Value; |
1880 | 1415 |
|
1881 | 1416 |
/// \brief Constructor. |
1882 | 1417 |
/// |
1883 | 1418 |
/// Constructor for descriptor map. |
1884 |
explicit DescriptorMap(const |
|
1419 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) { |
|
1885 | 1420 |
Item it; |
1886 | 1421 |
const typename Map::Notifier* nf = Map::notifier(); |
1887 | 1422 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1888 |
Map::set(it, invMap.size()); |
|
1889 |
invMap.push_back(it); |
|
1423 |
Map::set(it, _inv_map.size()); |
|
1424 |
_inv_map.push_back(it); |
|
1890 | 1425 |
} |
1891 | 1426 |
} |
1892 | 1427 |
|
1893 | 1428 |
protected: |
1894 | 1429 |
|
1895 | 1430 |
/// \brief Add a new key to the map. |
1896 | 1431 |
/// |
1897 | 1432 |
/// Add a new key to the map. It is called by the |
1898 | 1433 |
/// \c AlterationNotifier. |
1899 | 1434 |
virtual void add(const Item& item) { |
1900 | 1435 |
Map::add(item); |
1901 |
Map::set(item, invMap.size()); |
|
1902 |
invMap.push_back(item); |
|
1436 |
Map::set(item, _inv_map.size()); |
|
1437 |
_inv_map.push_back(item); |
|
1903 | 1438 |
} |
1904 | 1439 |
|
1905 | 1440 |
/// \brief Add more new keys to the map. |
1906 | 1441 |
/// |
1907 | 1442 |
/// Add more new keys to the map. It is called by the |
1908 | 1443 |
/// \c AlterationNotifier. |
1909 | 1444 |
virtual void add(const std::vector<Item>& items) { |
1910 | 1445 |
Map::add(items); |
1911 | 1446 |
for (int i = 0; i < int(items.size()); ++i) { |
1912 |
Map::set(items[i], invMap.size()); |
|
1913 |
invMap.push_back(items[i]); |
|
1447 |
Map::set(items[i], _inv_map.size()); |
|
1448 |
_inv_map.push_back(items[i]); |
|
1914 | 1449 |
} |
1915 | 1450 |
} |
1916 | 1451 |
|
1917 | 1452 |
/// \brief Erase the key from the map. |
1918 | 1453 |
/// |
1919 | 1454 |
/// Erase the key from the map. It is called by the |
1920 | 1455 |
/// \c AlterationNotifier. |
1921 | 1456 |
virtual void erase(const Item& item) { |
1922 |
Map::set(invMap.back(), Map::operator[](item)); |
|
1923 |
invMap[Map::operator[](item)] = invMap.back(); |
|
1924 |
|
|
1457 |
Map::set(_inv_map.back(), Map::operator[](item)); |
|
1458 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
|
1459 |
_inv_map.pop_back(); |
|
1925 | 1460 |
Map::erase(item); |
1926 | 1461 |
} |
1927 | 1462 |
|
1928 | 1463 |
/// \brief Erase more keys from the map. |
1929 | 1464 |
/// |
1930 | 1465 |
/// Erase more keys from the map. It is called by the |
1931 | 1466 |
/// \c AlterationNotifier. |
1932 | 1467 |
virtual void erase(const std::vector<Item>& items) { |
1933 | 1468 |
for (int i = 0; i < int(items.size()); ++i) { |
1934 |
Map::set(invMap.back(), Map::operator[](items[i])); |
|
1935 |
invMap[Map::operator[](items[i])] = invMap.back(); |
|
1936 |
|
|
1469 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
|
1470 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
|
1471 |
_inv_map.pop_back(); |
|
1937 | 1472 |
} |
1938 | 1473 |
Map::erase(items); |
1939 | 1474 |
} |
1940 | 1475 |
|
1941 | 1476 |
/// \brief Build the unique map. |
1942 | 1477 |
/// |
1943 | 1478 |
/// Build the unique map. It is called by the |
1944 | 1479 |
/// \c AlterationNotifier. |
1945 | 1480 |
virtual void build() { |
1946 | 1481 |
Map::build(); |
1947 | 1482 |
Item it; |
1948 | 1483 |
const typename Map::Notifier* nf = Map::notifier(); |
1949 | 1484 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1950 |
Map::set(it, invMap.size()); |
|
1951 |
invMap.push_back(it); |
|
1485 |
Map::set(it, _inv_map.size()); |
|
1486 |
_inv_map.push_back(it); |
|
1952 | 1487 |
} |
1953 | 1488 |
} |
1954 | 1489 |
|
1955 | 1490 |
/// \brief Clear the keys from the map. |
1956 | 1491 |
/// |
1957 | 1492 |
/// Clear the keys from the map. It is called by the |
1958 | 1493 |
/// \c AlterationNotifier. |
1959 | 1494 |
virtual void clear() { |
1960 |
|
|
1495 |
_inv_map.clear(); |
|
1961 | 1496 |
Map::clear(); |
1962 | 1497 |
} |
1963 | 1498 |
|
1964 | 1499 |
public: |
1965 | 1500 |
|
1966 | 1501 |
/// \brief Returns the maximal value plus one. |
1967 | 1502 |
/// |
1968 | 1503 |
/// Returns the maximal value plus one in the map. |
1969 | 1504 |
unsigned int size() const { |
1970 |
return |
|
1505 |
return _inv_map.size(); |
|
1971 | 1506 |
} |
1972 | 1507 |
|
1973 | 1508 |
/// \brief Swaps the position of the two items in the map. |
1974 | 1509 |
/// |
1975 | 1510 |
/// Swaps the position of the two items in the map. |
1976 | 1511 |
void swap(const Item& p, const Item& q) { |
1977 | 1512 |
int pi = Map::operator[](p); |
1978 | 1513 |
int qi = Map::operator[](q); |
1979 | 1514 |
Map::set(p, qi); |
1980 |
|
|
1515 |
_inv_map[qi] = p; |
|
1981 | 1516 |
Map::set(q, pi); |
1982 |
|
|
1517 |
_inv_map[pi] = q; |
|
1983 | 1518 |
} |
1984 | 1519 |
|
1985 | 1520 |
/// \brief Gives back the \e descriptor of the item. |
1986 | 1521 |
/// |
1987 | 1522 |
/// Gives back the mutable and unique \e descriptor of the map. |
1988 | 1523 |
int operator[](const Item& item) const { |
1989 | 1524 |
return Map::operator[](item); |
1990 | 1525 |
} |
1991 | 1526 |
|
1992 | 1527 |
/// \brief Gives back the item by its descriptor. |
1993 | 1528 |
/// |
1994 | 1529 |
/// Gives back th item by its descriptor. |
1995 | 1530 |
Item operator()(int id) const { |
1996 |
return |
|
1531 |
return _inv_map[id]; |
|
1997 | 1532 |
} |
1998 | 1533 |
|
1999 | 1534 |
private: |
2000 | 1535 |
|
2001 | 1536 |
typedef std::vector<Item> Container; |
2002 |
Container |
|
1537 |
Container _inv_map; |
|
2003 | 1538 |
|
2004 | 1539 |
public: |
2005 | 1540 |
/// \brief The inverse map type of DescriptorMap. |
2006 | 1541 |
/// |
2007 | 1542 |
/// The inverse map type of DescriptorMap. |
2008 | 1543 |
class InverseMap { |
2009 | 1544 |
public: |
2010 | 1545 |
/// \brief Constructor of the InverseMap. |
2011 | 1546 |
/// |
2012 | 1547 |
/// Constructor of the InverseMap. |
2013 |
explicit InverseMap(const DescriptorMap& _inverted) |
|
2014 |
: inverted(_inverted) {} |
|
1548 |
explicit InverseMap(const DescriptorMap& inverted) |
|
1549 |
: _inverted(inverted) {} |
|
2015 | 1550 |
|
2016 | 1551 |
|
2017 | 1552 |
/// The value type of the InverseMap. |
2018 | 1553 |
typedef typename DescriptorMap::Key Value; |
2019 | 1554 |
/// The key type of the InverseMap. |
2020 | 1555 |
typedef typename DescriptorMap::Value Key; |
2021 | 1556 |
|
2022 | 1557 |
/// \brief Subscript operator. |
2023 | 1558 |
/// |
2024 | 1559 |
/// Subscript operator. It gives back the item |
2025 | 1560 |
/// that the descriptor belongs to currently. |
2026 | 1561 |
Value operator[](const Key& key) const { |
2027 |
return |
|
1562 |
return _inverted(key); |
|
2028 | 1563 |
} |
2029 | 1564 |
|
2030 | 1565 |
/// \brief Size of the map. |
2031 | 1566 |
/// |
2032 | 1567 |
/// Returns the size of the map. |
2033 | 1568 |
unsigned int size() const { |
2034 |
return |
|
1569 |
return _inverted.size(); |
|
2035 | 1570 |
} |
2036 | 1571 |
|
2037 | 1572 |
private: |
2038 |
const DescriptorMap& |
|
1573 |
const DescriptorMap& _inverted; |
|
2039 | 1574 |
}; |
2040 | 1575 |
|
2041 | 1576 |
/// \brief Gives back the inverse of the map. |
2042 | 1577 |
/// |
2043 | 1578 |
/// Gives back the inverse of the map. |
2044 | 1579 |
const InverseMap inverse() const { |
2045 | 1580 |
return InverseMap(*this); |
2046 | 1581 |
} |
2047 | 1582 |
}; |
2048 | 1583 |
|
2049 | 1584 |
/// \brief Returns the source of the given arc. |
2050 | 1585 |
/// |
2051 | 1586 |
/// The SourceMap gives back the source Node of the given arc. |
2052 | 1587 |
/// \see TargetMap |
2053 | 1588 |
/// \author Balazs Dezso |
2054 | 1589 |
template <typename Digraph> |
2055 | 1590 |
class SourceMap { |
2056 | 1591 |
public: |
2057 | 1592 |
|
2058 | 1593 |
typedef typename Digraph::Node Value; |
2059 | 1594 |
typedef typename Digraph::Arc Key; |
2060 | 1595 |
|
2061 | 1596 |
/// \brief Constructor |
2062 | 1597 |
/// |
2063 | 1598 |
/// Constructor |
2064 | 1599 |
/// \param _digraph The digraph that the map belongs to. |
2065 |
explicit SourceMap(const Digraph& |
|
1600 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
|
2066 | 1601 |
|
2067 | 1602 |
/// \brief The subscript operator. |
2068 | 1603 |
/// |
2069 | 1604 |
/// The subscript operator. |
2070 | 1605 |
/// \param arc The arc |
2071 | 1606 |
/// \return The source of the arc |
2072 | 1607 |
Value operator[](const Key& arc) const { |
2073 |
return |
|
1608 |
return _digraph.source(arc); |
|
2074 | 1609 |
} |
2075 | 1610 |
|
2076 | 1611 |
private: |
2077 |
const Digraph& |
|
1612 |
const Digraph& _digraph; |
|
2078 | 1613 |
}; |
2079 | 1614 |
|
2080 | 1615 |
/// \brief Returns a \ref SourceMap class. |
2081 | 1616 |
/// |
2082 | 1617 |
/// This function just returns an \ref SourceMap class. |
2083 | 1618 |
/// \relates SourceMap |
2084 | 1619 |
template <typename Digraph> |
2085 | 1620 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
2086 | 1621 |
return SourceMap<Digraph>(digraph); |
2087 | 1622 |
} |
2088 | 1623 |
|
2089 | 1624 |
/// \brief Returns the target of the given arc. |
2090 | 1625 |
/// |
2091 | 1626 |
/// The TargetMap gives back the target Node of the given arc. |
2092 | 1627 |
/// \see SourceMap |
2093 | 1628 |
/// \author Balazs Dezso |
2094 | 1629 |
template <typename Digraph> |
2095 | 1630 |
class TargetMap { |
2096 | 1631 |
public: |
2097 | 1632 |
|
2098 | 1633 |
typedef typename Digraph::Node Value; |
2099 | 1634 |
typedef typename Digraph::Arc Key; |
2100 | 1635 |
|
2101 | 1636 |
/// \brief Constructor |
2102 | 1637 |
/// |
2103 | 1638 |
/// Constructor |
2104 | 1639 |
/// \param _digraph The digraph that the map belongs to. |
2105 |
explicit TargetMap(const Digraph& |
|
1640 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
|
2106 | 1641 |
|
2107 | 1642 |
/// \brief The subscript operator. |
2108 | 1643 |
/// |
2109 | 1644 |
/// The subscript operator. |
2110 | 1645 |
/// \param e The arc |
2111 | 1646 |
/// \return The target of the arc |
2112 | 1647 |
Value operator[](const Key& e) const { |
2113 |
return |
|
1648 |
return _digraph.target(e); |
|
2114 | 1649 |
} |
2115 | 1650 |
|
2116 | 1651 |
private: |
2117 |
const Digraph& |
|
1652 |
const Digraph& _digraph; |
|
2118 | 1653 |
}; |
2119 | 1654 |
|
2120 | 1655 |
/// \brief Returns a \ref TargetMap class. |
2121 | 1656 |
/// |
2122 | 1657 |
/// This function just returns a \ref TargetMap class. |
2123 | 1658 |
/// \relates TargetMap |
2124 | 1659 |
template <typename Digraph> |
2125 | 1660 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
2126 | 1661 |
return TargetMap<Digraph>(digraph); |
2127 | 1662 |
} |
2128 | 1663 |
|
2129 | 1664 |
/// \brief Returns the "forward" directed arc view of an edge. |
2130 | 1665 |
/// |
2131 | 1666 |
/// Returns the "forward" directed arc view of an edge. |
2132 | 1667 |
/// \see BackwardMap |
2133 | 1668 |
/// \author Balazs Dezso |
2134 |
template <typename |
|
1669 |
template <typename Graph> |
|
2135 | 1670 |
class ForwardMap { |
2136 | 1671 |
public: |
2137 | 1672 |
|
2138 |
typedef typename Digraph::Arc Value; |
|
2139 |
typedef typename Digraph::Edge Key; |
|
1673 |
typedef typename Graph::Arc Value; |
|
1674 |
typedef typename Graph::Edge Key; |
|
2140 | 1675 |
|
2141 | 1676 |
/// \brief Constructor |
2142 | 1677 |
/// |
2143 | 1678 |
/// Constructor |
2144 |
/// \param _digraph The digraph that the map belongs to. |
|
2145 |
explicit ForwardMap(const Digraph& _digraph) : digraph(_digraph) {} |
|
1679 |
/// \param _graph The graph that the map belongs to. |
|
1680 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
|
2146 | 1681 |
|
2147 | 1682 |
/// \brief The subscript operator. |
2148 | 1683 |
/// |
2149 | 1684 |
/// The subscript operator. |
2150 | 1685 |
/// \param key An edge |
2151 | 1686 |
/// \return The "forward" directed arc view of edge |
2152 | 1687 |
Value operator[](const Key& key) const { |
2153 |
return |
|
1688 |
return _graph.direct(key, true); |
|
2154 | 1689 |
} |
2155 | 1690 |
|
2156 | 1691 |
private: |
2157 |
const |
|
1692 |
const Graph& _graph; |
|
2158 | 1693 |
}; |
2159 | 1694 |
|
2160 | 1695 |
/// \brief Returns a \ref ForwardMap class. |
2161 | 1696 |
/// |
2162 | 1697 |
/// This function just returns an \ref ForwardMap class. |
2163 | 1698 |
/// \relates ForwardMap |
2164 |
template <typename Digraph> |
|
2165 |
inline ForwardMap<Digraph> forwardMap(const Digraph& digraph) { |
|
2166 |
|
|
1699 |
template <typename Graph> |
|
1700 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
|
1701 |
return ForwardMap<Graph>(graph); |
|
2167 | 1702 |
} |
2168 | 1703 |
|
2169 | 1704 |
/// \brief Returns the "backward" directed arc view of an edge. |
2170 | 1705 |
/// |
2171 | 1706 |
/// Returns the "backward" directed arc view of an edge. |
2172 | 1707 |
/// \see ForwardMap |
2173 | 1708 |
/// \author Balazs Dezso |
2174 |
template <typename |
|
1709 |
template <typename Graph> |
|
2175 | 1710 |
class BackwardMap { |
2176 | 1711 |
public: |
2177 | 1712 |
|
2178 |
typedef typename Digraph::Arc Value; |
|
2179 |
typedef typename Digraph::Edge Key; |
|
1713 |
typedef typename Graph::Arc Value; |
|
1714 |
typedef typename Graph::Edge Key; |
|
2180 | 1715 |
|
2181 | 1716 |
/// \brief Constructor |
2182 | 1717 |
/// |
2183 | 1718 |
/// Constructor |
2184 |
/// \param _digraph The digraph that the map belongs to. |
|
2185 |
explicit BackwardMap(const Digraph& _digraph) : digraph(_digraph) {} |
|
1719 |
/// \param _graph The graph that the map belongs to. |
|
1720 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {} |
|
2186 | 1721 |
|
2187 | 1722 |
/// \brief The subscript operator. |
2188 | 1723 |
/// |
2189 | 1724 |
/// The subscript operator. |
2190 | 1725 |
/// \param key An edge |
2191 | 1726 |
/// \return The "backward" directed arc view of edge |
2192 | 1727 |
Value operator[](const Key& key) const { |
2193 |
return |
|
1728 |
return _graph.direct(key, false); |
|
2194 | 1729 |
} |
2195 | 1730 |
|
2196 | 1731 |
private: |
2197 |
const |
|
1732 |
const Graph& _graph; |
|
2198 | 1733 |
}; |
2199 | 1734 |
|
2200 | 1735 |
/// \brief Returns a \ref BackwardMap class |
2201 | 1736 |
|
2202 | 1737 |
/// This function just returns a \ref BackwardMap class. |
2203 | 1738 |
/// \relates BackwardMap |
2204 |
template <typename Digraph> |
|
2205 |
inline BackwardMap<Digraph> backwardMap(const Digraph& digraph) { |
|
2206 |
|
|
1739 |
template <typename Graph> |
|
1740 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
|
1741 |
return BackwardMap<Graph>(graph); |
|
2207 | 1742 |
} |
2208 | 1743 |
|
2209 | 1744 |
/// \brief Potential difference map |
2210 | 1745 |
/// |
2211 | 1746 |
/// If there is an potential map on the nodes then we |
2212 | 1747 |
/// can get an arc map as we get the substraction of the |
2213 | 1748 |
/// values of the target and source. |
2214 | 1749 |
template <typename Digraph, typename NodeMap> |
2215 | 1750 |
class PotentialDifferenceMap { |
2216 | 1751 |
public: |
2217 | 1752 |
typedef typename Digraph::Arc Key; |
2218 | 1753 |
typedef typename NodeMap::Value Value; |
2219 | 1754 |
|
2220 | 1755 |
/// \brief Constructor |
2221 | 1756 |
/// |
2222 | 1757 |
/// Contructor of the map |
2223 |
explicit PotentialDifferenceMap(const Digraph& _digraph, |
|
2224 |
const NodeMap& _potential) |
|
2225 |
|
|
1758 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
|
1759 |
const NodeMap& potential) |
|
1760 |
: _digraph(digraph), _potential(potential) {} |
|
2226 | 1761 |
|
2227 | 1762 |
/// \brief Const subscription operator |
2228 | 1763 |
/// |
2229 | 1764 |
/// Const subscription operator |
2230 | 1765 |
Value operator[](const Key& arc) const { |
2231 |
return |
|
1766 |
return _potential[_digraph.target(arc)] - |
|
1767 |
_potential[_digraph.source(arc)]; |
|
2232 | 1768 |
} |
2233 | 1769 |
|
2234 | 1770 |
private: |
2235 |
const Digraph& digraph; |
|
2236 |
const NodeMap& potential; |
|
1771 |
const Digraph& _digraph; |
|
1772 |
const NodeMap& _potential; |
|
2237 | 1773 |
}; |
2238 | 1774 |
|
2239 | 1775 |
/// \brief Returns a PotentialDifferenceMap. |
2240 | 1776 |
/// |
2241 | 1777 |
/// This function just returns a PotentialDifferenceMap. |
2242 | 1778 |
/// \relates PotentialDifferenceMap |
2243 | 1779 |
template <typename Digraph, typename NodeMap> |
2244 | 1780 |
PotentialDifferenceMap<Digraph, NodeMap> |
2245 | 1781 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) { |
2246 | 1782 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
2247 | 1783 |
} |
2248 | 1784 |
|
2249 | 1785 |
/// \brief Map of the node in-degrees. |
2250 | 1786 |
/// |
2251 | 1787 |
/// This map returns the in-degree of a node. Once it is constructed, |
2252 | 1788 |
/// the degrees are stored in a standard NodeMap, so each query is done |
2253 | 1789 |
/// in constant time. On the other hand, the values are updated automatically |
2254 | 1790 |
/// whenever the digraph changes. |
2255 | 1791 |
/// |
2256 | 1792 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
2257 | 1793 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
2258 | 1794 |
/// is not guarantied if these additional features are used. For example |
2259 | 1795 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
2260 | 1796 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
2261 | 1797 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
2262 | 1798 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
2263 | 1799 |
/// |
2264 | 1800 |
/// \sa OutDegMap |
2265 | 1801 |
|
2266 | 1802 |
template <typename _Digraph> |
2267 | 1803 |
class InDegMap |
2268 | 1804 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
2269 | 1805 |
::ItemNotifier::ObserverBase { |
2270 | 1806 |
|
2271 | 1807 |
public: |
2272 | 1808 |
|
2273 | 1809 |
typedef _Digraph Digraph; |
2274 | 1810 |
typedef int Value; |
2275 | 1811 |
typedef typename Digraph::Node Key; |
2276 | 1812 |
|
2277 |
typedef typename ItemSetTraits< |
|
1813 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
|
2278 | 1814 |
::ItemNotifier::ObserverBase Parent; |
2279 | 1815 |
|
2280 | 1816 |
private: |
2281 | 1817 |
|
2282 |
class AutoNodeMap : public DefaultMap< |
|
1818 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> { |
|
2283 | 1819 |
public: |
2284 | 1820 |
|
2285 |
typedef DefaultMap<_Digraph, Key, int> Parent; |
|
2286 |
typedef typename Parent::Digraph Digraph; |
|
1821 |
typedef DefaultMap<Digraph, Key, int> Parent; |
|
2287 | 1822 |
|
2288 | 1823 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
2289 | 1824 |
|
2290 | 1825 |
virtual void add(const Key& key) { |
2291 | 1826 |
Parent::add(key); |
2292 | 1827 |
Parent::set(key, 0); |
2293 | 1828 |
} |
2294 | 1829 |
|
2295 | 1830 |
virtual void add(const std::vector<Key>& keys) { |
2296 | 1831 |
Parent::add(keys); |
2297 | 1832 |
for (int i = 0; i < int(keys.size()); ++i) { |
2298 | 1833 |
Parent::set(keys[i], 0); |
2299 | 1834 |
} |
2300 | 1835 |
} |
2301 | 1836 |
|
2302 | 1837 |
virtual void build() { |
2303 | 1838 |
Parent::build(); |
2304 | 1839 |
Key it; |
2305 | 1840 |
typename Parent::Notifier* nf = Parent::notifier(); |
2306 | 1841 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2307 | 1842 |
Parent::set(it, 0); |
2308 | 1843 |
} |
2309 | 1844 |
} |
2310 | 1845 |
}; |
2311 | 1846 |
|
2312 | 1847 |
public: |
2313 | 1848 |
|
2314 | 1849 |
/// \brief Constructor. |
2315 | 1850 |
/// |
2316 | 1851 |
/// Constructor for creating in-degree map. |
2317 |
explicit InDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) { |
|
2318 |
Parent::attach(digraph.notifier(typename _Digraph::Arc())); |
|
1852 |
explicit InDegMap(const Digraph& digraph) |
|
1853 |
: _digraph(digraph), _deg(digraph) { |
|
1854 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
|
2319 | 1855 |
|
2320 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
|
2321 |
deg[it] = countInArcs(digraph, it); |
|
1856 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
1857 |
_deg[it] = countInArcs(_digraph, it); |
|
2322 | 1858 |
} |
2323 | 1859 |
} |
2324 | 1860 |
|
2325 | 1861 |
/// Gives back the in-degree of a Node. |
2326 | 1862 |
int operator[](const Key& key) const { |
2327 |
return |
|
1863 |
return _deg[key]; |
|
2328 | 1864 |
} |
2329 | 1865 |
|
2330 | 1866 |
protected: |
2331 | 1867 |
|
2332 | 1868 |
typedef typename Digraph::Arc Arc; |
2333 | 1869 |
|
2334 | 1870 |
virtual void add(const Arc& arc) { |
2335 |
++ |
|
1871 |
++_deg[_digraph.target(arc)]; |
|
2336 | 1872 |
} |
2337 | 1873 |
|
2338 | 1874 |
virtual void add(const std::vector<Arc>& arcs) { |
2339 | 1875 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2340 |
++ |
|
1876 |
++_deg[_digraph.target(arcs[i])]; |
|
2341 | 1877 |
} |
2342 | 1878 |
} |
2343 | 1879 |
|
2344 | 1880 |
virtual void erase(const Arc& arc) { |
2345 |
-- |
|
1881 |
--_deg[_digraph.target(arc)]; |
|
2346 | 1882 |
} |
2347 | 1883 |
|
2348 | 1884 |
virtual void erase(const std::vector<Arc>& arcs) { |
2349 | 1885 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2350 |
-- |
|
1886 |
--_deg[_digraph.target(arcs[i])]; |
|
2351 | 1887 |
} |
2352 | 1888 |
} |
2353 | 1889 |
|
2354 | 1890 |
virtual void build() { |
2355 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
|
2356 |
deg[it] = countInArcs(digraph, it); |
|
1891 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
1892 |
_deg[it] = countInArcs(_digraph, it); |
|
2357 | 1893 |
} |
2358 | 1894 |
} |
2359 | 1895 |
|
2360 | 1896 |
virtual void clear() { |
2361 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
|
2362 |
deg[it] = 0; |
|
1897 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
1898 |
_deg[it] = 0; |
|
2363 | 1899 |
} |
2364 | 1900 |
} |
2365 | 1901 |
private: |
2366 | 1902 |
|
2367 |
const _Digraph& digraph; |
|
2368 |
AutoNodeMap deg; |
|
1903 |
const Digraph& _digraph; |
|
1904 |
AutoNodeMap _deg; |
|
2369 | 1905 |
}; |
2370 | 1906 |
|
2371 | 1907 |
/// \brief Map of the node out-degrees. |
2372 | 1908 |
/// |
2373 | 1909 |
/// This map returns the out-degree of a node. Once it is constructed, |
2374 | 1910 |
/// the degrees are stored in a standard NodeMap, so each query is done |
2375 | 1911 |
/// in constant time. On the other hand, the values are updated automatically |
2376 | 1912 |
/// whenever the digraph changes. |
2377 | 1913 |
/// |
2378 | 1914 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
2379 | 1915 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap |
2380 | 1916 |
/// is not guarantied if these additional features are used. For example |
2381 | 1917 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
2382 | 1918 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
2383 | 1919 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
2384 | 1920 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
2385 | 1921 |
/// |
2386 | 1922 |
/// \sa InDegMap |
2387 | 1923 |
|
2388 | 1924 |
template <typename _Digraph> |
2389 | 1925 |
class OutDegMap |
2390 | 1926 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
2391 | 1927 |
::ItemNotifier::ObserverBase { |
2392 | 1928 |
|
2393 | 1929 |
public: |
2394 |
|
|
2395 |
typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc> |
|
2396 |
::ItemNotifier::ObserverBase Parent; |
|
2397 | 1930 |
|
2398 | 1931 |
typedef _Digraph Digraph; |
2399 | 1932 |
typedef int Value; |
2400 | 1933 |
typedef typename Digraph::Node Key; |
2401 | 1934 |
|
1935 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
|
1936 |
::ItemNotifier::ObserverBase Parent; |
|
1937 |
|
|
2402 | 1938 |
private: |
2403 | 1939 |
|
2404 |
class AutoNodeMap : public DefaultMap< |
|
1940 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> { |
|
2405 | 1941 |
public: |
2406 | 1942 |
|
2407 |
typedef DefaultMap<_Digraph, Key, int> Parent; |
|
2408 |
typedef typename Parent::Digraph Digraph; |
|
1943 |
typedef DefaultMap<Digraph, Key, int> Parent; |
|
2409 | 1944 |
|
2410 | 1945 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
2411 | 1946 |
|
2412 | 1947 |
virtual void add(const Key& key) { |
2413 | 1948 |
Parent::add(key); |
2414 | 1949 |
Parent::set(key, 0); |
2415 | 1950 |
} |
2416 | 1951 |
virtual void add(const std::vector<Key>& keys) { |
2417 | 1952 |
Parent::add(keys); |
2418 | 1953 |
for (int i = 0; i < int(keys.size()); ++i) { |
2419 | 1954 |
Parent::set(keys[i], 0); |
2420 | 1955 |
} |
2421 | 1956 |
} |
2422 | 1957 |
virtual void build() { |
2423 | 1958 |
Parent::build(); |
2424 | 1959 |
Key it; |
2425 | 1960 |
typename Parent::Notifier* nf = Parent::notifier(); |
2426 | 1961 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2427 | 1962 |
Parent::set(it, 0); |
2428 | 1963 |
} |
2429 | 1964 |
} |
2430 | 1965 |
}; |
2431 | 1966 |
|
2432 | 1967 |
public: |
2433 | 1968 |
|
2434 | 1969 |
/// \brief Constructor. |
2435 | 1970 |
/// |
2436 | 1971 |
/// Constructor for creating out-degree map. |
2437 |
explicit OutDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) { |
|
2438 |
Parent::attach(digraph.notifier(typename _Digraph::Arc())); |
|
1972 |
explicit OutDegMap(const Digraph& digraph) |
|
1973 |
: _digraph(digraph), _deg(digraph) { |
|
1974 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
|
2439 | 1975 |
|
2440 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
|
2441 |
deg[it] = countOutArcs(digraph, it); |
|
1976 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
1977 |
_deg[it] = countOutArcs(_digraph, it); |
|
2442 | 1978 |
} |
2443 | 1979 |
} |
2444 | 1980 |
|
2445 | 1981 |
/// Gives back the out-degree of a Node. |
2446 | 1982 |
int operator[](const Key& key) const { |
2447 |
return |
|
1983 |
return _deg[key]; |
|
2448 | 1984 |
} |
2449 | 1985 |
|
2450 | 1986 |
protected: |
2451 | 1987 |
|
2452 | 1988 |
typedef typename Digraph::Arc Arc; |
2453 | 1989 |
|
2454 | 1990 |
virtual void add(const Arc& arc) { |
2455 |
++ |
|
1991 |
++_deg[_digraph.source(arc)]; |
|
2456 | 1992 |
} |
2457 | 1993 |
|
2458 | 1994 |
virtual void add(const std::vector<Arc>& arcs) { |
2459 | 1995 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2460 |
++ |
|
1996 |
++_deg[_digraph.source(arcs[i])]; |
|
2461 | 1997 |
} |
2462 | 1998 |
} |
2463 | 1999 |
|
2464 | 2000 |
virtual void erase(const Arc& arc) { |
2465 |
-- |
|
2001 |
--_deg[_digraph.source(arc)]; |
|
2466 | 2002 |
} |
2467 | 2003 |
|
2468 | 2004 |
virtual void erase(const std::vector<Arc>& arcs) { |
2469 | 2005 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2470 |
-- |
|
2006 |
--_deg[_digraph.source(arcs[i])]; |
|
2471 | 2007 |
} |
2472 | 2008 |
} |
2473 | 2009 |
|
2474 | 2010 |
virtual void build() { |
2475 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
|
2476 |
deg[it] = countOutArcs(digraph, it); |
|
2011 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2012 |
_deg[it] = countOutArcs(_digraph, it); |
|
2477 | 2013 |
} |
2478 | 2014 |
} |
2479 | 2015 |
|
2480 | 2016 |
virtual void clear() { |
2481 |
for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
|
2482 |
deg[it] = 0; |
|
2017 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
|
2018 |
_deg[it] = 0; |
|
2483 | 2019 |
} |
2484 | 2020 |
} |
2485 | 2021 |
private: |
2486 | 2022 |
|
2487 |
const _Digraph& digraph; |
|
2488 |
AutoNodeMap deg; |
|
2023 |
const Digraph& _digraph; |
|
2024 |
AutoNodeMap _deg; |
|
2489 | 2025 |
}; |
2490 | 2026 |
|
2491 | 2027 |
|
2492 | 2028 |
///Dynamic arc look up between given endpoints. |
2493 | 2029 |
|
2494 | 2030 |
///\ingroup gutils |
2495 | 2031 |
///Using this class, you can find an arc in a digraph from a given |
2496 | 2032 |
///source to a given target in amortized time <em>O(log d)</em>, |
2497 | 2033 |
///where <em>d</em> is the out-degree of the source node. |
2498 | 2034 |
/// |
2499 | 2035 |
///It is possible to find \e all parallel arcs between two nodes with |
2500 | 2036 |
///the \c findFirst() and \c findNext() members. |
2501 | 2037 |
/// |
2502 | 2038 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
2503 |
///digraph |
|
2039 |
///digraph is not changed so frequently. |
|
2504 | 2040 |
/// |
2505 | 2041 |
///This class uses a self-adjusting binary search tree, Sleator's |
2506 | 2042 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
2507 | 2043 |
///time bound for arc lookups. This class also guarantees the |
2508 | 2044 |
///optimal time bound in a constant factor for any distribution of |
2509 | 2045 |
///queries. |
2510 | 2046 |
/// |
2511 | 2047 |
///\param G The type of the underlying digraph. |
2512 | 2048 |
/// |
2513 | 2049 |
///\sa ArcLookUp |
2514 | 2050 |
///\sa AllArcLookUp |
2515 | 2051 |
template<class G> |
2516 | 2052 |
class DynArcLookUp |
2517 | 2053 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
2518 | 2054 |
{ |
2519 | 2055 |
public: |
2520 | 2056 |
typedef typename ItemSetTraits<G, typename G::Arc> |
2521 | 2057 |
::ItemNotifier::ObserverBase Parent; |
2522 | 2058 |
|
2523 |
|
|
2059 |
DIGRAPH_TYPEDEFS(typename G); |
|
2524 | 2060 |
typedef G Digraph; |
2525 | 2061 |
|
2526 | 2062 |
protected: |
2527 | 2063 |
|
2528 | 2064 |
class AutoNodeMap : public DefaultMap<G, Node, Arc> { |
2529 | 2065 |
public: |
2530 | 2066 |
|
2531 | 2067 |
typedef DefaultMap<G, Node, Arc> Parent; |
2532 | 2068 |
|
2533 | 2069 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
2534 | 2070 |
|
2535 | 2071 |
virtual void add(const Node& node) { |
2536 | 2072 |
Parent::add(node); |
2537 | 2073 |
Parent::set(node, INVALID); |
2538 | 2074 |
} |
2539 | 2075 |
|
2540 | 2076 |
virtual void add(const std::vector<Node>& nodes) { |
2541 | 2077 |
Parent::add(nodes); |
2542 | 2078 |
for (int i = 0; i < int(nodes.size()); ++i) { |
2543 | 2079 |
Parent::set(nodes[i], INVALID); |
2544 | 2080 |
} |
2545 | 2081 |
} |
2546 | 2082 |
|
2547 | 2083 |
virtual void build() { |
2548 | 2084 |
Parent::build(); |
2549 | 2085 |
Node it; |
2550 | 2086 |
typename Parent::Notifier* nf = Parent::notifier(); |
2551 | 2087 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2552 | 2088 |
Parent::set(it, INVALID); |
2553 | 2089 |
} |
2554 | 2090 |
} |
2555 | 2091 |
}; |
2556 | 2092 |
|
2557 | 2093 |
const Digraph &_g; |
2558 | 2094 |
AutoNodeMap _head; |
2559 | 2095 |
typename Digraph::template ArcMap<Arc> _parent; |
2560 | 2096 |
typename Digraph::template ArcMap<Arc> _left; |
2561 | 2097 |
typename Digraph::template ArcMap<Arc> _right; |
2562 | 2098 |
|
2563 | 2099 |
class ArcLess { |
2564 | 2100 |
const Digraph &g; |
2565 | 2101 |
public: |
2566 | 2102 |
ArcLess(const Digraph &_g) : g(_g) {} |
2567 | 2103 |
bool operator()(Arc a,Arc b) const |
2568 | 2104 |
{ |
2569 | 2105 |
return g.target(a)<g.target(b); |
2570 | 2106 |
} |
2571 | 2107 |
}; |
2572 | 2108 |
|
2573 | 2109 |
public: |
2574 | 2110 |
|
2575 | 2111 |
///Constructor |
2576 | 2112 |
|
2577 | 2113 |
///Constructor. |
2578 | 2114 |
/// |
2579 | 2115 |
///It builds up the search database. |
2580 | 2116 |
DynArcLookUp(const Digraph &g) |
2581 | 2117 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
2582 | 2118 |
{ |
2583 | 2119 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
2584 | 2120 |
refresh(); |
2585 | 2121 |
} |
2586 | 2122 |
|
2587 | 2123 |
protected: |
2588 | 2124 |
|
2589 | 2125 |
virtual void add(const Arc& arc) { |
2590 | 2126 |
insert(arc); |
2591 | 2127 |
} |
2592 | 2128 |
|
2593 | 2129 |
virtual void add(const std::vector<Arc>& arcs) { |
2594 | 2130 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2595 | 2131 |
insert(arcs[i]); |
2596 | 2132 |
} |
2597 | 2133 |
} |
2598 | 2134 |
|
2599 | 2135 |
virtual void erase(const Arc& arc) { |
2600 | 2136 |
remove(arc); |
2601 | 2137 |
} |
2602 | 2138 |
|
2603 | 2139 |
virtual void erase(const std::vector<Arc>& arcs) { |
2604 | 2140 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2605 | 2141 |
remove(arcs[i]); |
2606 | 2142 |
} |
2607 | 2143 |
} |
2608 | 2144 |
|
2609 | 2145 |
virtual void build() { |
2610 | 2146 |
refresh(); |
2611 | 2147 |
} |
2612 | 2148 |
|
2613 | 2149 |
virtual void clear() { |
2614 | 2150 |
for(NodeIt n(_g);n!=INVALID;++n) { |
2615 | 2151 |
_head.set(n, INVALID); |
2616 | 2152 |
} |
2617 | 2153 |
} |
2618 | 2154 |
|
2619 | 2155 |
void insert(Arc arc) { |
2620 | 2156 |
Node s = _g.source(arc); |
2621 | 2157 |
Node t = _g.target(arc); |
2622 | 2158 |
_left.set(arc, INVALID); |
2623 | 2159 |
_right.set(arc, INVALID); |
2624 | 2160 |
|
2625 | 2161 |
Arc e = _head[s]; |
2626 | 2162 |
if (e == INVALID) { |
2627 | 2163 |
_head.set(s, arc); |
2628 | 2164 |
_parent.set(arc, INVALID); |
2629 | 2165 |
return; |
2630 | 2166 |
} |
2631 | 2167 |
while (true) { |
2632 | 2168 |
if (t < _g.target(e)) { |
2633 | 2169 |
if (_left[e] == INVALID) { |
2634 | 2170 |
_left.set(e, arc); |
2635 | 2171 |
_parent.set(arc, e); |
2636 | 2172 |
splay(arc); |
2637 | 2173 |
return; |
2638 | 2174 |
} else { |
2639 | 2175 |
e = _left[e]; |
2640 | 2176 |
} |
2641 | 2177 |
} else { |
2642 | 2178 |
if (_right[e] == INVALID) { |
2643 | 2179 |
_right.set(e, arc); |
2644 | 2180 |
_parent.set(arc, e); |
2645 | 2181 |
splay(arc); |
2646 | 2182 |
return; |
2647 | 2183 |
} else { |
2648 | 2184 |
e = _right[e]; |
2649 | 2185 |
} |
2650 | 2186 |
} |
2651 | 2187 |
} |
2652 | 2188 |
} |
2653 | 2189 |
|
2654 | 2190 |
void remove(Arc arc) { |
2655 | 2191 |
if (_left[arc] == INVALID) { |
2656 | 2192 |
if (_right[arc] != INVALID) { |
2657 | 2193 |
_parent.set(_right[arc], _parent[arc]); |
2658 | 2194 |
} |
2659 | 2195 |
if (_parent[arc] != INVALID) { |
2660 | 2196 |
if (_left[_parent[arc]] == arc) { |
2661 | 2197 |
_left.set(_parent[arc], _right[arc]); |
2662 | 2198 |
} else { |
2663 | 2199 |
_right.set(_parent[arc], _right[arc]); |
2664 | 2200 |
} |
2665 | 2201 |
} else { |
2666 | 2202 |
_head.set(_g.source(arc), _right[arc]); |
2667 | 2203 |
} |
2668 | 2204 |
} else if (_right[arc] == INVALID) { |
2669 | 2205 |
_parent.set(_left[arc], _parent[arc]); |
2670 | 2206 |
if (_parent[arc] != INVALID) { |
2671 | 2207 |
if (_left[_parent[arc]] == arc) { |
2672 | 2208 |
_left.set(_parent[arc], _left[arc]); |
2673 | 2209 |
} else { |
2674 | 2210 |
_right.set(_parent[arc], _left[arc]); |
2675 | 2211 |
} |
2676 | 2212 |
} else { |
2677 | 2213 |
_head.set(_g.source(arc), _left[arc]); |
2678 | 2214 |
} |
2679 | 2215 |
} else { |
2680 | 2216 |
Arc e = _left[arc]; |
2681 | 2217 |
if (_right[e] != INVALID) { |
2682 | 2218 |
e = _right[e]; |
2683 | 2219 |
while (_right[e] != INVALID) { |
2684 | 2220 |
e = _right[e]; |
2685 | 2221 |
} |
2686 | 2222 |
Arc s = _parent[e]; |
2687 | 2223 |
_right.set(_parent[e], _left[e]); |
2688 | 2224 |
if (_left[e] != INVALID) { |
2689 | 2225 |
_parent.set(_left[e], _parent[e]); |
2690 | 2226 |
} |
2691 | 2227 |
|
2692 | 2228 |
_left.set(e, _left[arc]); |
2693 | 2229 |
_parent.set(_left[arc], e); |
2694 | 2230 |
_right.set(e, _right[arc]); |
2695 | 2231 |
_parent.set(_right[arc], e); |
2696 | 2232 |
|
2697 | 2233 |
_parent.set(e, _parent[arc]); |
2698 | 2234 |
if (_parent[arc] != INVALID) { |
2699 | 2235 |
if (_left[_parent[arc]] == arc) { |
2700 | 2236 |
_left.set(_parent[arc], e); |
2701 | 2237 |
} else { |
2702 | 2238 |
_right.set(_parent[arc], e); |
2703 | 2239 |
} |
2704 | 2240 |
} |
2705 | 2241 |
splay(s); |
2706 | 2242 |
} else { |
2707 | 2243 |
_right.set(e, _right[arc]); |
2708 | 2244 |
_parent.set(_right[arc], e); |
2709 | 2245 |
|
2710 | 2246 |
if (_parent[arc] != INVALID) { |
2711 | 2247 |
if (_left[_parent[arc]] == arc) { |
2712 | 2248 |
_left.set(_parent[arc], e); |
2713 | 2249 |
} else { |
2714 | 2250 |
_right.set(_parent[arc], e); |
2715 | 2251 |
} |
2716 | 2252 |
} else { |
2717 | 2253 |
_head.set(_g.source(arc), e); |
2718 | 2254 |
} |
2719 | 2255 |
} |
2720 | 2256 |
} |
2721 | 2257 |
} |
2722 | 2258 |
|
2723 | 2259 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
2724 | 2260 |
{ |
2725 | 2261 |
int m=(a+b)/2; |
2726 | 2262 |
Arc me=v[m]; |
2727 | 2263 |
if (a < m) { |
2728 | 2264 |
Arc left = refreshRec(v,a,m-1); |
2729 | 2265 |
_left.set(me, left); |
2730 | 2266 |
_parent.set(left, me); |
2731 | 2267 |
} else { |
2732 | 2268 |
_left.set(me, INVALID); |
2733 | 2269 |
} |
2734 | 2270 |
if (m < b) { |
2735 | 2271 |
Arc right = refreshRec(v,m+1,b); |
2736 | 2272 |
_right.set(me, right); |
2737 | 2273 |
_parent.set(right, me); |
2738 | 2274 |
} else { |
2739 | 2275 |
_right.set(me, INVALID); |
2740 | 2276 |
} |
2741 | 2277 |
return me; |
2742 | 2278 |
} |
2743 | 2279 |
|
2744 | 2280 |
void refresh() { |
2745 | 2281 |
for(NodeIt n(_g);n!=INVALID;++n) { |
2746 | 2282 |
std::vector<Arc> v; |
2747 | 2283 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
2748 | 2284 |
if(v.size()) { |
2749 | 2285 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
2750 | 2286 |
Arc head = refreshRec(v,0,v.size()-1); |
2751 | 2287 |
_head.set(n, head); |
2752 | 2288 |
_parent.set(head, INVALID); |
2753 | 2289 |
} |
2754 | 2290 |
else _head.set(n, INVALID); |
2755 | 2291 |
} |
2756 | 2292 |
} |
2757 | 2293 |
|
2758 | 2294 |
void zig(Arc v) { |
2759 | 2295 |
Arc w = _parent[v]; |
2760 | 2296 |
_parent.set(v, _parent[w]); |
2761 | 2297 |
_parent.set(w, v); |
2762 | 2298 |
_left.set(w, _right[v]); |
2763 | 2299 |
_right.set(v, w); |
2764 | 2300 |
if (_parent[v] != INVALID) { |
2765 | 2301 |
if (_right[_parent[v]] == w) { |
2766 | 2302 |
_right.set(_parent[v], v); |
2767 | 2303 |
} else { |
2768 | 2304 |
_left.set(_parent[v], v); |
2769 | 2305 |
} |
2770 | 2306 |
} |
2771 | 2307 |
if (_left[w] != INVALID){ |
2772 | 2308 |
_parent.set(_left[w], w); |
2773 | 2309 |
} |
2774 | 2310 |
} |
2775 | 2311 |
|
2776 | 2312 |
void zag(Arc v) { |
2777 | 2313 |
Arc w = _parent[v]; |
2778 | 2314 |
_parent.set(v, _parent[w]); |
2779 | 2315 |
_parent.set(w, v); |
2780 | 2316 |
_right.set(w, _left[v]); |
2781 | 2317 |
_left.set(v, w); |
2782 | 2318 |
if (_parent[v] != INVALID){ |
2783 | 2319 |
if (_left[_parent[v]] == w) { |
2784 | 2320 |
_left.set(_parent[v], v); |
2785 | 2321 |
} else { |
2786 | 2322 |
_right.set(_parent[v], v); |
2787 | 2323 |
} |
2788 | 2324 |
} |
2789 | 2325 |
if (_right[w] != INVALID){ |
2790 | 2326 |
_parent.set(_right[w], w); |
2791 | 2327 |
} |
2792 | 2328 |
} |
2793 | 2329 |
|
2794 | 2330 |
void splay(Arc v) { |
2795 | 2331 |
while (_parent[v] != INVALID) { |
2796 | 2332 |
if (v == _left[_parent[v]]) { |
2797 | 2333 |
if (_parent[_parent[v]] == INVALID) { |
2798 | 2334 |
zig(v); |
2799 | 2335 |
} else { |
2800 | 2336 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
2801 | 2337 |
zig(_parent[v]); |
2802 | 2338 |
zig(v); |
2803 | 2339 |
} else { |
2804 | 2340 |
zig(v); |
2805 | 2341 |
zag(v); |
2806 | 2342 |
} |
2807 | 2343 |
} |
2808 | 2344 |
} else { |
2809 | 2345 |
if (_parent[_parent[v]] == INVALID) { |
2810 | 2346 |
zag(v); |
2811 | 2347 |
} else { |
2812 | 2348 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
2813 | 2349 |
zag(v); |
2814 | 2350 |
zig(v); |
2815 | 2351 |
} else { |
2816 | 2352 |
zag(_parent[v]); |
2817 | 2353 |
zag(v); |
2818 | 2354 |
} |
2819 | 2355 |
} |
2820 | 2356 |
} |
2821 | 2357 |
} |
2822 | 2358 |
_head[_g.source(v)] = v; |
2823 | 2359 |
} |
2824 | 2360 |
|
2825 | 2361 |
|
2826 | 2362 |
public: |
2827 | 2363 |
|
2828 | 2364 |
///Find an arc between two nodes. |
2829 | 2365 |
|
2830 | 2366 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
2831 | 2367 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
2832 | 2368 |
///\param s The source node |
2833 | 2369 |
///\param t The target node |
2834 | 2370 |
///\return An arc from \c s to \c t if there exists, |
2835 | 2371 |
///\ref INVALID otherwise. |
2836 | 2372 |
Arc operator()(Node s, Node t) const |
2837 | 2373 |
{ |
2838 |
Arc |
|
2374 |
Arc a = _head[s]; |
|
2839 | 2375 |
while (true) { |
2840 |
if (_g.target(e) == t) { |
|
2841 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
2842 |
return e; |
|
2843 |
} else if (t < _g.target(e)) { |
|
2844 |
if (_left[e] == INVALID) { |
|
2845 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
2376 |
if (_g.target(a) == t) { |
|
2377 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2378 |
return a; |
|
2379 |
} else if (t < _g.target(a)) { |
|
2380 |
if (_left[a] == INVALID) { |
|
2381 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2846 | 2382 |
return INVALID; |
2847 | 2383 |
} else { |
2848 |
|
|
2384 |
a = _left[a]; |
|
2849 | 2385 |
} |
2850 | 2386 |
} else { |
2851 |
if (_right[e] == INVALID) { |
|
2852 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
2387 |
if (_right[a] == INVALID) { |
|
2388 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2853 | 2389 |
return INVALID; |
2854 | 2390 |
} else { |
2855 |
|
|
2391 |
a = _right[a]; |
|
2856 | 2392 |
} |
2857 | 2393 |
} |
2858 | 2394 |
} |
2859 | 2395 |
} |
2860 | 2396 |
|
2861 | 2397 |
///Find the first arc between two nodes. |
2862 | 2398 |
|
2863 | 2399 |
///Find the first arc between two nodes in time |
2864 | 2400 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
2865 | 2401 |
/// outgoing arcs of \c s. |
2866 | 2402 |
///\param s The source node |
2867 | 2403 |
///\param t The target node |
2868 | 2404 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
2869 | 2405 |
/// otherwise. |
2870 | 2406 |
Arc findFirst(Node s, Node t) const |
2871 | 2407 |
{ |
2872 |
Arc |
|
2408 |
Arc a = _head[s]; |
|
2873 | 2409 |
Arc r = INVALID; |
2874 | 2410 |
while (true) { |
2875 |
if (_g.target(e) < t) { |
|
2876 |
if (_right[e] == INVALID) { |
|
2877 |
|
|
2411 |
if (_g.target(a) < t) { |
|
2412 |
if (_right[a] == INVALID) { |
|
2413 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2878 | 2414 |
return r; |
2879 | 2415 |
} else { |
2880 |
|
|
2416 |
a = _right[a]; |
|
2881 | 2417 |
} |
2882 | 2418 |
} else { |
2883 |
if (_g.target(e) == t) { |
|
2884 |
r = e; |
|
2419 |
if (_g.target(a) == t) { |
|
2420 |
r = a; |
|
2885 | 2421 |
} |
2886 |
if (_left[e] == INVALID) { |
|
2887 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
2422 |
if (_left[a] == INVALID) { |
|
2423 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2888 | 2424 |
return r; |
2889 | 2425 |
} else { |
2890 |
|
|
2426 |
a = _left[a]; |
|
2891 | 2427 |
} |
2892 | 2428 |
} |
2893 | 2429 |
} |
2894 | 2430 |
} |
2895 | 2431 |
|
2896 | 2432 |
///Find the next arc between two nodes. |
2897 | 2433 |
|
2898 | 2434 |
///Find the next arc between two nodes in time |
2899 | 2435 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
2900 | 2436 |
/// outgoing arcs of \c s. |
2901 | 2437 |
///\param s The source node |
2902 | 2438 |
///\param t The target node |
2903 | 2439 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
2904 | 2440 |
/// otherwise. |
2905 | 2441 |
|
2906 | 2442 |
///\note If \c e is not the result of the previous \c findFirst() |
2907 | 2443 |
///operation then the amorized time bound can not be guaranteed. |
2908 | 2444 |
#ifdef DOXYGEN |
2909 |
Arc findNext(Node s, Node t, Arc |
|
2445 |
Arc findNext(Node s, Node t, Arc a) const |
|
2910 | 2446 |
#else |
2911 |
Arc findNext(Node, Node t, Arc |
|
2447 |
Arc findNext(Node, Node t, Arc a) const |
|
2912 | 2448 |
#endif |
2913 | 2449 |
{ |
2914 |
if (_right[e] != INVALID) { |
|
2915 |
e = _right[e]; |
|
2916 |
while (_left[e] != INVALID) { |
|
2917 |
e = _left[e]; |
|
2450 |
if (_right[a] != INVALID) { |
|
2451 |
a = _right[a]; |
|
2452 |
while (_left[a] != INVALID) { |
|
2453 |
a = _left[a]; |
|
2918 | 2454 |
} |
2919 |
const_cast<DynArcLookUp&>(*this).splay( |
|
2455 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2920 | 2456 |
} else { |
2921 |
while (_parent[e] != INVALID && _right[_parent[e]] == e) { |
|
2922 |
e = _parent[e]; |
|
2457 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
|
2458 |
a = _parent[a]; |
|
2923 | 2459 |
} |
2924 |
if (_parent[ |
|
2460 |
if (_parent[a] == INVALID) { |
|
2925 | 2461 |
return INVALID; |
2926 | 2462 |
} else { |
2927 |
e = _parent[e]; |
|
2928 |
const_cast<DynArcLookUp&>(*this).splay(e); |
|
2463 |
a = _parent[a]; |
|
2464 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
2929 | 2465 |
} |
2930 | 2466 |
} |
2931 |
if (_g.target( |
|
2467 |
if (_g.target(a) == t) return a; |
|
2932 | 2468 |
else return INVALID; |
2933 | 2469 |
} |
2934 | 2470 |
|
2935 | 2471 |
}; |
2936 | 2472 |
|
2937 | 2473 |
///Fast arc look up between given endpoints. |
2938 | 2474 |
|
2939 | 2475 |
///\ingroup gutils |
2940 | 2476 |
///Using this class, you can find an arc in a digraph from a given |
2941 | 2477 |
///source to a given target in time <em>O(log d)</em>, |
2942 | 2478 |
///where <em>d</em> is the out-degree of the source node. |
2943 | 2479 |
/// |
2944 | 2480 |
///It is not possible to find \e all parallel arcs between two nodes. |
2945 | 2481 |
///Use \ref AllArcLookUp for this purpose. |
2946 | 2482 |
/// |
2947 | 2483 |
///\warning This class is static, so you should refresh() (or at least |
2948 | 2484 |
///refresh(Node)) this data structure |
2949 | 2485 |
///whenever the digraph changes. This is a time consuming (superlinearly |
2950 | 2486 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
2951 | 2487 |
/// |
2952 | 2488 |
///\param G The type of the underlying digraph. |
2953 | 2489 |
/// |
2954 | 2490 |
///\sa DynArcLookUp |
2955 | 2491 |
///\sa AllArcLookUp |
2956 | 2492 |
template<class G> |
2957 | 2493 |
class ArcLookUp |
2958 | 2494 |
{ |
2959 | 2495 |
public: |
2960 |
|
|
2496 |
DIGRAPH_TYPEDEFS(typename G); |
|
2961 | 2497 |
typedef G Digraph; |
2962 | 2498 |
|
2963 | 2499 |
protected: |
2964 | 2500 |
const Digraph &_g; |
2965 | 2501 |
typename Digraph::template NodeMap<Arc> _head; |
2966 | 2502 |
typename Digraph::template ArcMap<Arc> _left; |
2967 | 2503 |
typename Digraph::template ArcMap<Arc> _right; |
2968 | 2504 |
|
2969 | 2505 |
class ArcLess { |
2970 | 2506 |
const Digraph &g; |
2971 | 2507 |
public: |
2972 | 2508 |
ArcLess(const Digraph &_g) : g(_g) {} |
2973 | 2509 |
bool operator()(Arc a,Arc b) const |
2974 | 2510 |
{ |
2975 | 2511 |
return g.target(a)<g.target(b); |
2976 | 2512 |
} |
2977 | 2513 |
}; |
2978 | 2514 |
|
2979 | 2515 |
public: |
2980 | 2516 |
|
2981 | 2517 |
///Constructor |
2982 | 2518 |
|
2983 | 2519 |
///Constructor. |
2984 | 2520 |
/// |
2985 | 2521 |
///It builds up the search database, which remains valid until the digraph |
2986 | 2522 |
///changes. |
2987 | 2523 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
2988 | 2524 |
|
2989 | 2525 |
private: |
2990 | 2526 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
2991 | 2527 |
{ |
2992 | 2528 |
int m=(a+b)/2; |
2993 | 2529 |
Arc me=v[m]; |
2994 | 2530 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
2995 | 2531 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
2996 | 2532 |
return me; |
2997 | 2533 |
} |
2998 | 2534 |
public: |
2999 | 2535 |
///Refresh the data structure at a node. |
3000 | 2536 |
|
3001 | 2537 |
///Build up the search database of node \c n. |
3002 | 2538 |
/// |
3003 | 2539 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
3004 | 2540 |
///the number of the outgoing arcs of \c n. |
3005 | 2541 |
void refresh(Node n) |
3006 | 2542 |
{ |
3007 | 2543 |
std::vector<Arc> v; |
3008 | 2544 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
3009 | 2545 |
if(v.size()) { |
3010 | 2546 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
3011 | 2547 |
_head[n]=refreshRec(v,0,v.size()-1); |
3012 | 2548 |
} |
3013 | 2549 |
else _head[n]=INVALID; |
3014 | 2550 |
} |
3015 | 2551 |
///Refresh the full data structure. |
3016 | 2552 |
|
3017 | 2553 |
///Build up the full search database. In fact, it simply calls |
3018 | 2554 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
3019 | 2555 |
/// |
3020 | 2556 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
3021 | 2557 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
3022 | 2558 |
///out-degree of the digraph. |
3023 | 2559 |
|
3024 | 2560 |
void refresh() |
3025 | 2561 |
{ |
3026 | 2562 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
3027 | 2563 |
} |
3028 | 2564 |
|
3029 | 2565 |
///Find an arc between two nodes. |
3030 | 2566 |
|
3031 | 2567 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
3032 | 2568 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
3033 | 2569 |
///\param s The source node |
3034 | 2570 |
///\param t The target node |
3035 | 2571 |
///\return An arc from \c s to \c t if there exists, |
3036 | 2572 |
///\ref INVALID otherwise. |
3037 | 2573 |
/// |
3038 | 2574 |
///\warning If you change the digraph, refresh() must be called before using |
3039 | 2575 |
///this operator. If you change the outgoing arcs of |
3040 | 2576 |
///a single node \c n, then |
3041 | 2577 |
///\ref refresh(Node) "refresh(n)" is enough. |
3042 | 2578 |
/// |
3043 | 2579 |
Arc operator()(Node s, Node t) const |
3044 | 2580 |
{ |
3045 | 2581 |
Arc e; |
3046 | 2582 |
for(e=_head[s]; |
3047 | 2583 |
e!=INVALID&&_g.target(e)!=t; |
3048 | 2584 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
3049 | 2585 |
return e; |
3050 | 2586 |
} |
3051 | 2587 |
|
3052 | 2588 |
}; |
3053 | 2589 |
|
3054 | 2590 |
///Fast look up of all arcs between given endpoints. |
3055 | 2591 |
|
3056 | 2592 |
///\ingroup gutils |
3057 | 2593 |
///This class is the same as \ref ArcLookUp, with the addition |
3058 | 2594 |
///that it makes it possible to find all arcs between given endpoints. |
3059 | 2595 |
/// |
3060 | 2596 |
///\warning This class is static, so you should refresh() (or at least |
3061 | 2597 |
///refresh(Node)) this data structure |
3062 | 2598 |
///whenever the digraph changes. This is a time consuming (superlinearly |
3063 | 2599 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
3064 | 2600 |
/// |
3065 | 2601 |
///\param G The type of the underlying digraph. |
3066 | 2602 |
/// |
3067 | 2603 |
///\sa DynArcLookUp |
3068 | 2604 |
///\sa ArcLookUp |
3069 | 2605 |
template<class G> |
3070 | 2606 |
class AllArcLookUp : public ArcLookUp<G> |
3071 | 2607 |
{ |
3072 | 2608 |
using ArcLookUp<G>::_g; |
3073 | 2609 |
using ArcLookUp<G>::_right; |
3074 | 2610 |
using ArcLookUp<G>::_left; |
3075 | 2611 |
using ArcLookUp<G>::_head; |
3076 | 2612 |
|
3077 |
|
|
2613 |
DIGRAPH_TYPEDEFS(typename G); |
|
3078 | 2614 |
typedef G Digraph; |
3079 | 2615 |
|
3080 | 2616 |
typename Digraph::template ArcMap<Arc> _next; |
3081 | 2617 |
|
3082 | 2618 |
Arc refreshNext(Arc head,Arc next=INVALID) |
3083 | 2619 |
{ |
3084 | 2620 |
if(head==INVALID) return next; |
3085 | 2621 |
else { |
3086 | 2622 |
next=refreshNext(_right[head],next); |
3087 | 2623 |
// _next[head]=next; |
3088 | 2624 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
3089 | 2625 |
? next : INVALID; |
3090 | 2626 |
return refreshNext(_left[head],head); |
3091 | 2627 |
} |
3092 | 2628 |
} |
3093 | 2629 |
|
3094 | 2630 |
void refreshNext() |
3095 | 2631 |
{ |
3096 | 2632 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
3097 | 2633 |
} |
3098 | 2634 |
|
3099 | 2635 |
public: |
3100 | 2636 |
///Constructor |
3101 | 2637 |
|
3102 | 2638 |
///Constructor. |
3103 | 2639 |
/// |
3104 | 2640 |
///It builds up the search database, which remains valid until the digraph |
3105 | 2641 |
///changes. |
3106 | 2642 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
3107 | 2643 |
|
3108 | 2644 |
///Refresh the data structure at a node. |
3109 | 2645 |
|
3110 | 2646 |
///Build up the search database of node \c n. |
3111 | 2647 |
/// |
3112 | 2648 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
3113 | 2649 |
///the number of the outgoing arcs of \c n. |
3114 | 2650 |
|
3115 | 2651 |
void refresh(Node n) |
3116 | 2652 |
{ |
3117 | 2653 |
ArcLookUp<G>::refresh(n); |
3118 | 2654 |
refreshNext(_head[n]); |
3119 | 2655 |
} |
3120 | 2656 |
|
3121 | 2657 |
///Refresh the full data structure. |
3122 | 2658 |
|
3123 | 2659 |
///Build up the full search database. In fact, it simply calls |
3124 | 2660 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
3125 | 2661 |
/// |
3126 | 2662 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
3127 | 2663 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
3128 | 2664 |
///out-degree of the digraph. |
3129 | 2665 |
|
3130 | 2666 |
void refresh() |
3131 | 2667 |
{ |
3132 | 2668 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
3133 | 2669 |
} |
3134 | 2670 |
|
3135 | 2671 |
///Find an arc between two nodes. |
3136 | 2672 |
|
3137 | 2673 |
///Find an arc between two nodes. |
3138 | 2674 |
///\param s The source node |
3139 | 2675 |
///\param t The target node |
3140 | 2676 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
3141 | 2677 |
///not given, the operator finds the first appropriate arc. |
3142 | 2678 |
///\return An arc from \c s to \c t after \c prev or |
3143 | 2679 |
///\ref INVALID if there is no more. |
3144 | 2680 |
/// |
3145 | 2681 |
///For example, you can count the number of arcs from \c u to \c v in the |
3146 | 2682 |
///following way. |
3147 | 2683 |
///\code |
3148 | 2684 |
///AllArcLookUp<ListDigraph> ae(g); |
3149 | 2685 |
///... |
3150 | 2686 |
///int n=0; |
3151 | 2687 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
3152 | 2688 |
///\endcode |
3153 | 2689 |
/// |
3154 | 2690 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where |
3155 | 2691 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the |
3156 | 2692 |
///consecutive arcs are found in constant time. |
3157 | 2693 |
/// |
3158 | 2694 |
///\warning If you change the digraph, refresh() must be called before using |
3159 | 2695 |
///this operator. If you change the outgoing arcs of |
3160 | 2696 |
///a single node \c n, then |
3161 | 2697 |
///\ref refresh(Node) "refresh(n)" is enough. |
3162 | 2698 |
/// |
3163 | 2699 |
#ifdef DOXYGEN |
3164 | 2700 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
3165 | 2701 |
#else |
3166 | 2702 |
using ArcLookUp<G>::operator() ; |
3167 | 2703 |
Arc operator()(Node s, Node t, Arc prev) const |
3168 | 2704 |
{ |
3169 | 2705 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
3170 | 2706 |
} |
3171 | 2707 |
#endif |
3172 | 2708 |
|
3173 | 2709 |
}; |
3174 | 2710 |
|
3175 | 2711 |
/// @} |
3176 | 2712 |
|
3177 | 2713 |
} //END OF NAMESPACE LEMON |
3178 | 2714 |
|
3179 | 2715 |
#endif |
1 | 1 |
/* -*- C++ -*- |
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 |
///\ingroup lemon_io |
20 | 20 |
///\file |
21 | 21 |
///\brief Lemon Graph Format reader. |
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_LGF_READER_H |
25 | 25 |
#define LEMON_LGF_READER_H |
26 | 26 |
|
27 | 27 |
#include <iostream> |
28 | 28 |
#include <fstream> |
29 | 29 |
#include <sstream> |
30 | 30 |
|
31 | 31 |
#include <set> |
32 | 32 |
#include <map> |
33 | 33 |
|
34 | 34 |
#include <lemon/assert.h> |
35 | 35 |
#include <lemon/graph_utils.h> |
36 | 36 |
|
37 | 37 |
#include <lemon/lgf_writer.h> |
38 | 38 |
|
39 | 39 |
#include <lemon/concept_check.h> |
40 | 40 |
#include <lemon/concepts/maps.h> |
41 | 41 |
|
42 | 42 |
namespace lemon { |
43 | 43 |
|
44 | 44 |
namespace _reader_bits { |
45 | 45 |
|
46 | 46 |
template <typename Value> |
47 | 47 |
struct DefaultConverter { |
48 | 48 |
Value operator()(const std::string& str) { |
49 | 49 |
std::istringstream is(str); |
50 | 50 |
Value value; |
51 | 51 |
is >> value; |
52 | 52 |
|
53 | 53 |
char c; |
54 | 54 |
if (is >> std::ws >> c) { |
55 | 55 |
throw DataFormatError("Remaining characters in token"); |
56 | 56 |
} |
57 | 57 |
return value; |
58 | 58 |
} |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
template <> |
62 | 62 |
struct DefaultConverter<std::string> { |
63 | 63 |
std::string operator()(const std::string& str) { |
64 | 64 |
return str; |
65 | 65 |
} |
66 | 66 |
}; |
67 | 67 |
|
68 | 68 |
template <typename _Item> |
69 | 69 |
class MapStorageBase { |
70 | 70 |
public: |
71 | 71 |
typedef _Item Item; |
72 | 72 |
|
73 | 73 |
public: |
74 | 74 |
MapStorageBase() {} |
75 | 75 |
virtual ~MapStorageBase() {} |
76 | 76 |
|
77 | 77 |
virtual void set(const Item& item, const std::string& value) = 0; |
78 | 78 |
|
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
template <typename _Item, typename _Map, |
82 | 82 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
83 | 83 |
class MapStorage : public MapStorageBase<_Item> { |
84 | 84 |
public: |
85 | 85 |
typedef _Map Map; |
86 | 86 |
typedef _Converter Converter; |
87 | 87 |
typedef _Item Item; |
88 | 88 |
|
89 | 89 |
private: |
90 | 90 |
Map& _map; |
91 | 91 |
Converter _converter; |
92 | 92 |
|
93 | 93 |
public: |
94 | 94 |
MapStorage(Map& map, const Converter& converter = Converter()) |
95 | 95 |
: _map(map), _converter(converter) {} |
96 | 96 |
virtual ~MapStorage() {} |
97 | 97 |
|
98 | 98 |
virtual void set(const Item& item ,const std::string& value) { |
99 | 99 |
_map.set(item, _converter(value)); |
100 | 100 |
} |
101 | 101 |
}; |
102 | 102 |
|
103 | 103 |
class ValueStorageBase { |
104 | 104 |
public: |
105 | 105 |
ValueStorageBase() {} |
106 | 106 |
virtual ~ValueStorageBase() {} |
107 | 107 |
|
108 | 108 |
virtual void set(const std::string&) = 0; |
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
112 | 112 |
class ValueStorage : public ValueStorageBase { |
113 | 113 |
public: |
114 | 114 |
typedef _Value Value; |
115 | 115 |
typedef _Converter Converter; |
116 | 116 |
|
117 | 117 |
private: |
118 | 118 |
Value& _value; |
119 | 119 |
Converter _converter; |
120 | 120 |
|
121 | 121 |
public: |
122 | 122 |
ValueStorage(Value& value, const Converter& converter = Converter()) |
123 | 123 |
: _value(value), _converter(converter) {} |
124 | 124 |
|
125 | 125 |
virtual void set(const std::string& value) { |
126 | 126 |
_value = _converter(value); |
127 | 127 |
} |
128 | 128 |
}; |
129 | 129 |
|
130 | 130 |
template <typename Value> |
131 | 131 |
struct MapLookUpConverter { |
132 | 132 |
const std::map<std::string, Value>& _map; |
133 | 133 |
|
134 | 134 |
MapLookUpConverter(const std::map<std::string, Value>& map) |
135 | 135 |
: _map(map) {} |
136 | 136 |
|
137 | 137 |
Value operator()(const std::string& str) { |
138 | 138 |
typename std::map<std::string, Value>::const_iterator it = |
139 | 139 |
_map.find(str); |
140 | 140 |
if (it == _map.end()) { |
141 | 141 |
std::ostringstream msg; |
142 | 142 |
msg << "Item not found: " << str; |
143 | 143 |
throw DataFormatError(msg.str().c_str()); |
144 | 144 |
} |
145 | 145 |
return it->second; |
146 | 146 |
} |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
bool isWhiteSpace(char c) { |
150 | 150 |
return c == ' ' || c == '\t' || c == '\v' || |
151 | 151 |
c == '\n' || c == '\r' || c == '\f'; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
bool isOct(char c) { |
155 | 155 |
return '0' <= c && c <='7'; |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
int valueOct(char c) { |
159 | 159 |
LEMON_ASSERT(isOct(c), "The character is not octal."); |
160 | 160 |
return c - '0'; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
bool isHex(char c) { |
164 | 164 |
return ('0' <= c && c <= '9') || |
165 | 165 |
('a' <= c && c <= 'z') || |
166 | 166 |
('A' <= c && c <= 'Z'); |
167 | 167 |
} |
168 | 168 |
|
169 | 169 |
int valueHex(char c) { |
170 | 170 |
LEMON_ASSERT(isHex(c), "The character is not hexadecimal."); |
171 | 171 |
if ('0' <= c && c <= '9') return c - '0'; |
172 | 172 |
if ('a' <= c && c <= 'z') return c - 'a' + 10; |
173 | 173 |
return c - 'A' + 10; |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
bool isIdentifierFirstChar(char c) { |
177 | 177 |
return ('a' <= c && c <= 'z') || |
178 | 178 |
('A' <= c && c <= 'Z') || c == '_'; |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
bool isIdentifierChar(char c) { |
182 | 182 |
return isIdentifierFirstChar(c) || |
183 | 183 |
('0' <= c && c <= '9'); |
184 | 184 |
} |
185 | 185 |
|
186 | 186 |
char readEscape(std::istream& is) { |
187 | 187 |
char c; |
188 | 188 |
if (!is.get(c)) |
189 | 189 |
throw DataFormatError("Escape format error"); |
190 | 190 |
|
191 | 191 |
switch (c) { |
192 | 192 |
case '\\': |
193 | 193 |
return '\\'; |
194 | 194 |
case '\"': |
195 | 195 |
return '\"'; |
196 | 196 |
case '\'': |
197 | 197 |
return '\''; |
198 | 198 |
case '\?': |
199 | 199 |
return '\?'; |
200 | 200 |
case 'a': |
201 | 201 |
return '\a'; |
202 | 202 |
case 'b': |
203 | 203 |
return '\b'; |
204 | 204 |
case 'f': |
205 | 205 |
return '\f'; |
206 | 206 |
case 'n': |
207 | 207 |
return '\n'; |
208 | 208 |
case 'r': |
209 | 209 |
return '\r'; |
210 | 210 |
case 't': |
211 | 211 |
return '\t'; |
212 | 212 |
case 'v': |
213 | 213 |
return '\v'; |
214 | 214 |
case 'x': |
215 | 215 |
{ |
216 | 216 |
int code; |
217 | 217 |
if (!is.get(c) || !isHex(c)) |
218 | 218 |
throw DataFormatError("Escape format error"); |
219 | 219 |
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); |
220 | 220 |
else code = code * 16 + valueHex(c); |
221 | 221 |
return code; |
222 | 222 |
} |
223 | 223 |
default: |
224 | 224 |
{ |
225 | 225 |
int code; |
226 | 226 |
if (!isOct(c)) |
227 | 227 |
throw DataFormatError("Escape format error"); |
228 | 228 |
else if (code = valueOct(c), !is.get(c) || !isOct(c)) |
229 | 229 |
is.putback(c); |
230 | 230 |
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) |
231 | 231 |
is.putback(c); |
232 | 232 |
else code = code * 8 + valueOct(c); |
233 | 233 |
return code; |
234 | 234 |
} |
235 | 235 |
} |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
std::istream& readToken(std::istream& is, std::string& str) { |
239 | 239 |
std::ostringstream os; |
240 | 240 |
|
241 | 241 |
char c; |
242 | 242 |
is >> std::ws; |
243 | 243 |
|
244 | 244 |
if (!is.get(c)) |
245 | 245 |
return is; |
246 | 246 |
|
247 | 247 |
if (c == '\"') { |
248 | 248 |
while (is.get(c) && c != '\"') { |
249 | 249 |
if (c == '\\') |
250 | 250 |
c = readEscape(is); |
251 | 251 |
os << c; |
252 | 252 |
} |
253 | 253 |
if (!is) |
254 | 254 |
throw DataFormatError("Quoted format error"); |
255 | 255 |
} else { |
256 | 256 |
is.putback(c); |
257 | 257 |
while (is.get(c) && !isWhiteSpace(c)) { |
258 | 258 |
if (c == '\\') |
259 | 259 |
c = readEscape(is); |
260 | 260 |
os << c; |
261 | 261 |
} |
262 | 262 |
if (!is) { |
263 | 263 |
is.clear(); |
264 | 264 |
} else { |
265 | 265 |
is.putback(c); |
266 | 266 |
} |
267 | 267 |
} |
268 | 268 |
str = os.str(); |
269 | 269 |
return is; |
270 | 270 |
} |
271 | 271 |
|
272 | 272 |
std::istream& readIdentifier(std::istream& is, std::string& str) { |
273 | 273 |
std::ostringstream os; |
274 | 274 |
|
275 | 275 |
char c; |
276 | 276 |
is >> std::ws; |
277 | 277 |
|
278 | 278 |
if (!is.get(c)) |
279 | 279 |
return is; |
280 | 280 |
|
281 | 281 |
if (!isIdentifierFirstChar(c)) |
282 | 282 |
throw DataFormatError("Wrong char in identifier"); |
283 | 283 |
|
284 | 284 |
os << c; |
285 | 285 |
|
286 | 286 |
while (is.get(c) && !isWhiteSpace(c)) { |
287 | 287 |
if (!isIdentifierChar(c)) |
288 | 288 |
throw DataFormatError("Wrong char in identifier"); |
289 | 289 |
os << c; |
290 | 290 |
} |
291 | 291 |
if (!is) is.clear(); |
292 | 292 |
|
293 | 293 |
str = os.str(); |
294 | 294 |
return is; |
295 | 295 |
} |
296 | 296 |
|
297 | 297 |
} |
298 | 298 |
|
299 | 299 |
/// \e |
300 | 300 |
template <typename _Digraph> |
301 | 301 |
class DigraphReader { |
302 | 302 |
public: |
303 | 303 |
|
304 | 304 |
typedef _Digraph Digraph; |
305 |
|
|
305 |
DIGRAPH_TYPEDEFS(typename Digraph); |
|
306 | 306 |
|
307 | 307 |
private: |
308 | 308 |
|
309 | 309 |
|
310 | 310 |
std::istream* _is; |
311 | 311 |
bool local_is; |
312 | 312 |
|
313 | 313 |
Digraph& _digraph; |
314 | 314 |
|
315 | 315 |
std::string _nodes_caption; |
316 | 316 |
std::string _arcs_caption; |
317 | 317 |
std::string _attributes_caption; |
318 | 318 |
|
319 | 319 |
typedef std::map<std::string, Node> NodeIndex; |
320 | 320 |
NodeIndex _node_index; |
321 | 321 |
typedef std::map<std::string, Arc> ArcIndex; |
322 | 322 |
ArcIndex _arc_index; |
323 | 323 |
|
324 | 324 |
typedef std::vector<std::pair<std::string, |
325 | 325 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
326 | 326 |
NodeMaps _node_maps; |
327 | 327 |
|
328 | 328 |
typedef std::vector<std::pair<std::string, |
329 | 329 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
330 | 330 |
ArcMaps _arc_maps; |
331 | 331 |
|
332 | 332 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
333 | 333 |
Attributes; |
334 | 334 |
Attributes _attributes; |
335 | 335 |
|
336 | 336 |
bool _use_nodes; |
337 | 337 |
bool _use_arcs; |
338 | 338 |
|
339 | 339 |
int line_num; |
340 | 340 |
std::istringstream line; |
341 | 341 |
|
342 | 342 |
public: |
343 | 343 |
|
344 | 344 |
/// \e |
345 | 345 |
DigraphReader(std::istream& is, Digraph& digraph) |
346 | 346 |
: _is(&is), local_is(false), _digraph(digraph), |
347 | 347 |
_use_nodes(false), _use_arcs(false) {} |
348 | 348 |
|
349 | 349 |
/// \e |
350 | 350 |
DigraphReader(const std::string& fn, Digraph& digraph) |
351 | 351 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), |
352 | 352 |
_use_nodes(false), _use_arcs(false) {} |
353 | 353 |
|
354 | 354 |
|
355 | 355 |
/// \e |
356 | 356 |
DigraphReader(const char* fn, Digraph& digraph) |
357 | 357 |
: _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), |
358 | 358 |
_use_nodes(false), _use_arcs(false) {} |
359 | 359 |
|
360 | 360 |
/// \e |
361 | 361 |
DigraphReader(DigraphReader& other) |
362 | 362 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
363 | 363 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs) { |
364 | 364 |
|
365 | 365 |
other.is = 0; |
366 | 366 |
other.local_is = false; |
367 | 367 |
|
368 | 368 |
_node_index.swap(other._node_index); |
369 | 369 |
_arc_index.swap(other._arc_index); |
370 | 370 |
|
371 | 371 |
_node_maps.swap(other._node_maps); |
372 | 372 |
_arc_maps.swap(other._arc_maps); |
373 | 373 |
_attributes.swap(other._attributes); |
374 | 374 |
|
375 | 375 |
_nodes_caption = other._nodes_caption; |
376 | 376 |
_arcs_caption = other._arcs_caption; |
377 | 377 |
_attributes_caption = other._attributes_caption; |
378 | 378 |
} |
379 | 379 |
|
380 | 380 |
/// \e |
381 | 381 |
~DigraphReader() { |
382 | 382 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
383 | 383 |
it != _node_maps.end(); ++it) { |
384 | 384 |
delete it->second; |
385 | 385 |
} |
386 | 386 |
|
387 | 387 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
388 | 388 |
it != _arc_maps.end(); ++it) { |
389 | 389 |
delete it->second; |
390 | 390 |
} |
391 | 391 |
|
392 | 392 |
for (typename Attributes::iterator it = _attributes.begin(); |
393 | 393 |
it != _attributes.end(); ++it) { |
394 | 394 |
delete it->second; |
395 | 395 |
} |
396 | 396 |
|
397 | 397 |
if (local_is) { |
398 | 398 |
delete _is; |
399 | 399 |
} |
400 | 400 |
|
401 | 401 |
} |
402 | 402 |
|
403 | 403 |
private: |
404 | 404 |
|
405 | 405 |
DigraphReader& operator=(const DigraphReader&); |
406 | 406 |
|
407 | 407 |
public: |
408 | 408 |
|
409 | 409 |
/// \e |
410 | 410 |
template <typename Map> |
411 | 411 |
DigraphReader& nodeMap(const std::string& caption, Map& map) { |
412 | 412 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
413 | 413 |
_reader_bits::MapStorageBase<Node>* storage = |
414 | 414 |
new _reader_bits::MapStorage<Node, Map>(map); |
415 | 415 |
_node_maps.push_back(std::make_pair(caption, storage)); |
416 | 416 |
return *this; |
417 | 417 |
} |
418 | 418 |
|
419 | 419 |
/// \e |
420 | 420 |
template <typename Map, typename Converter> |
421 | 421 |
DigraphReader& nodeMap(const std::string& caption, Map& map, |
422 | 422 |
const Converter& converter = Converter()) { |
423 | 423 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
424 | 424 |
_reader_bits::MapStorageBase<Node>* storage = |
425 | 425 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
426 | 426 |
_node_maps.push_back(std::make_pair(caption, storage)); |
427 | 427 |
return *this; |
428 | 428 |
} |
429 | 429 |
|
430 | 430 |
/// \e |
431 | 431 |
template <typename Map> |
432 | 432 |
DigraphReader& arcMap(const std::string& caption, Map& map) { |
433 | 433 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
434 | 434 |
_reader_bits::MapStorageBase<Arc>* storage = |
435 | 435 |
new _reader_bits::MapStorage<Arc, Map>(map); |
436 | 436 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
437 | 437 |
return *this; |
438 | 438 |
} |
439 | 439 |
|
440 | 440 |
/// \e |
441 | 441 |
template <typename Map, typename Converter> |
442 | 442 |
DigraphReader& arcMap(const std::string& caption, Map& map, |
443 | 443 |
const Converter& converter = Converter()) { |
444 | 444 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
445 | 445 |
_reader_bits::MapStorageBase<Arc>* storage = |
446 | 446 |
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter); |
447 | 447 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
448 | 448 |
return *this; |
449 | 449 |
} |
450 | 450 |
|
451 | 451 |
/// \e |
452 | 452 |
template <typename Value> |
453 | 453 |
DigraphReader& attribute(const std::string& caption, Value& value) { |
454 | 454 |
_reader_bits::ValueStorageBase* storage = |
455 | 455 |
new _reader_bits::ValueStorage<Value>(value); |
456 | 456 |
_attributes.insert(std::make_pair(caption, storage)); |
457 | 457 |
return *this; |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
/// \e |
461 | 461 |
template <typename Value, typename Converter> |
462 | 462 |
DigraphReader& attribute(const std::string& caption, Value& value, |
463 | 463 |
const Converter& converter = Converter()) { |
464 | 464 |
_reader_bits::ValueStorageBase* storage = |
465 | 465 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
466 | 466 |
_attributes.insert(std::make_pair(caption, storage)); |
467 | 467 |
return *this; |
468 | 468 |
} |
469 | 469 |
|
470 | 470 |
/// \e |
471 | 471 |
DigraphReader& node(const std::string& caption, Node& node) { |
472 | 472 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
473 | 473 |
Converter converter(_node_index); |
474 | 474 |
_reader_bits::ValueStorageBase* storage = |
475 | 475 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
476 | 476 |
_attributes.insert(std::make_pair(caption, storage)); |
477 | 477 |
return *this; |
478 | 478 |
} |
479 | 479 |
|
480 | 480 |
/// \e |
481 | 481 |
DigraphReader& arc(const std::string& caption, Arc& arc) { |
482 | 482 |
typedef _reader_bits::MapLookUpConverter<Arc> Converter; |
483 | 483 |
Converter converter(_arc_index); |
484 | 484 |
_reader_bits::ValueStorageBase* storage = |
485 | 485 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
486 | 486 |
_attributes.insert(std::make_pair(caption, storage)); |
487 | 487 |
return *this; |
488 | 488 |
} |
489 | 489 |
|
490 | 490 |
/// \e |
491 | 491 |
DigraphReader& nodes(const std::string& caption) { |
492 | 492 |
_nodes_caption = caption; |
493 | 493 |
return *this; |
494 | 494 |
} |
495 | 495 |
|
496 | 496 |
/// \e |
497 | 497 |
DigraphReader& arcs(const std::string& caption) { |
498 | 498 |
_arcs_caption = caption; |
499 | 499 |
return *this; |
500 | 500 |
} |
501 | 501 |
|
502 | 502 |
/// \e |
503 | 503 |
DigraphReader& attributes(const std::string& caption) { |
504 | 504 |
_attributes_caption = caption; |
505 | 505 |
return *this; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
/// \e |
509 | 509 |
template <typename Map> |
510 | 510 |
DigraphReader& useNodes(const Map& map) { |
511 | 511 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
512 | 512 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
513 | 513 |
_use_nodes = true; |
514 | 514 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
515 | 515 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
516 | 516 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
517 | 517 |
} |
518 | 518 |
return *this; |
519 | 519 |
} |
520 | 520 |
|
521 | 521 |
/// \e |
522 | 522 |
template <typename Map, typename Converter> |
523 | 523 |
DigraphReader& useNodes(const Map& map, |
524 | 524 |
const Converter& converter = Converter()) { |
525 | 525 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
526 | 526 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
527 | 527 |
_use_nodes = true; |
528 | 528 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
529 | 529 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
530 | 530 |
} |
531 | 531 |
return *this; |
532 | 532 |
} |
533 | 533 |
|
534 | 534 |
/// \e |
535 | 535 |
template <typename Map> |
536 | 536 |
DigraphReader& useArcs(const Map& map) { |
537 | 537 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
538 | 538 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
539 | 539 |
_use_arcs = true; |
540 | 540 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
541 | 541 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
542 | 542 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
543 | 543 |
} |
544 | 544 |
return *this; |
545 | 545 |
} |
546 | 546 |
|
547 | 547 |
/// \e |
548 | 548 |
template <typename Map, typename Converter> |
549 | 549 |
DigraphReader& useArcs(const Map& map, |
550 | 550 |
const Converter& converter = Converter()) { |
551 | 551 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
552 | 552 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
553 | 553 |
_use_arcs = true; |
554 | 554 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
555 | 555 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
556 | 556 |
} |
557 | 557 |
return *this; |
558 | 558 |
} |
559 | 559 |
|
560 | 560 |
private: |
561 | 561 |
|
562 | 562 |
bool readLine() { |
563 | 563 |
std::string str; |
564 | 564 |
while(++line_num, std::getline(*_is, str)) { |
565 | 565 |
line.clear(); line.str(str); |
566 | 566 |
char c; |
567 | 567 |
if (line >> std::ws >> c && c != '#') { |
568 | 568 |
line.putback(c); |
569 | 569 |
return true; |
570 | 570 |
} |
571 | 571 |
} |
572 | 572 |
return false; |
573 | 573 |
} |
574 | 574 |
|
575 | 575 |
bool readSuccess() { |
576 | 576 |
return static_cast<bool>(*_is); |
577 | 577 |
} |
578 | 578 |
|
579 | 579 |
void skipSection() { |
580 | 580 |
char c; |
581 | 581 |
while (readSuccess() && line >> c && c != '@') { |
582 | 582 |
readLine(); |
583 | 583 |
} |
584 | 584 |
line.putback(c); |
585 | 585 |
} |
586 | 586 |
|
587 | 587 |
void readNodes() { |
588 | 588 |
|
589 | 589 |
std::vector<int> map_index(_node_maps.size()); |
590 | 590 |
int map_num, label_index; |
591 | 591 |
|
592 | 592 |
if (!readLine()) |
593 | 593 |
throw DataFormatError("Cannot find map captions"); |
594 | 594 |
|
595 | 595 |
{ |
596 | 596 |
std::map<std::string, int> maps; |
597 | 597 |
|
598 | 598 |
std::string map; |
599 | 599 |
int index = 0; |
600 | 600 |
while (_reader_bits::readIdentifier(line, map)) { |
601 | 601 |
if (maps.find(map) != maps.end()) { |
602 | 602 |
std::ostringstream msg; |
603 | 603 |
msg << "Multiple occurence of node map: " << map; |
604 | 604 |
throw DataFormatError(msg.str().c_str()); |
605 | 605 |
} |
606 | 606 |
maps.insert(std::make_pair(map, index)); |
607 | 607 |
++index; |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
611 | 611 |
std::map<std::string, int>::iterator jt = |
612 | 612 |
maps.find(_node_maps[i].first); |
613 | 613 |
if (jt == maps.end()) { |
614 | 614 |
std::ostringstream msg; |
615 | 615 |
msg << "Map not found in file: " << _node_maps[i].first; |
616 | 616 |
throw DataFormatError(msg.str().c_str()); |
617 | 617 |
} |
618 | 618 |
map_index[i] = jt->second; |
619 | 619 |
} |
620 | 620 |
|
621 | 621 |
{ |
622 | 622 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
623 | 623 |
if (jt == maps.end()) |
624 | 624 |
throw DataFormatError("Label map not found in file"); |
625 | 625 |
label_index = jt->second; |
626 | 626 |
} |
627 | 627 |
map_num = maps.size(); |
628 | 628 |
} |
629 | 629 |
|
630 | 630 |
char c; |
631 | 631 |
while (readLine() && line >> c && c != '@') { |
632 | 632 |
line.putback(c); |
633 | 633 |
|
634 | 634 |
std::vector<std::string> tokens(map_num); |
635 | 635 |
for (int i = 0; i < map_num; ++i) { |
636 | 636 |
if (!_reader_bits::readToken(line, tokens[i])) { |
637 | 637 |
std::ostringstream msg; |
638 | 638 |
msg << "Column not found (" << i + 1 << ")"; |
639 | 639 |
throw DataFormatError(msg.str().c_str()); |
640 | 640 |
} |
641 | 641 |
} |
642 | 642 |
if (line >> std::ws >> c) |
643 | 643 |
throw DataFormatError("Extra character on the end of line"); |
644 | 644 |
|
645 | 645 |
Node n; |
646 | 646 |
if (!_use_nodes) { |
647 | 647 |
n = _digraph.addNode(); |
648 | 648 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
649 | 649 |
} else { |
650 | 650 |
typename std::map<std::string, Node>::iterator it = |
651 | 651 |
_node_index.find(tokens[label_index]); |
652 | 652 |
if (it == _node_index.end()) { |
653 | 653 |
std::ostringstream msg; |
654 | 654 |
msg << "Node with label not found: " << tokens[label_index]; |
655 | 655 |
throw DataFormatError(msg.str().c_str()); |
656 | 656 |
} |
657 | 657 |
n = it->second; |
658 | 658 |
} |
659 | 659 |
|
660 | 660 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
661 | 661 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
662 | 662 |
} |
663 | 663 |
|
664 | 664 |
} |
665 | 665 |
if (readSuccess()) { |
666 | 666 |
line.putback(c); |
667 | 667 |
} |
668 | 668 |
} |
669 | 669 |
|
670 | 670 |
void readArcs() { |
671 | 671 |
|
672 | 672 |
std::vector<int> map_index(_arc_maps.size()); |
673 | 673 |
int map_num, label_index; |
674 | 674 |
|
675 | 675 |
if (!readLine()) |
676 | 676 |
throw DataFormatError("Cannot find map captions"); |
677 | 677 |
|
678 | 678 |
{ |
679 | 679 |
std::map<std::string, int> maps; |
680 | 680 |
|
681 | 681 |
std::string map; |
682 | 682 |
int index = 0; |
683 | 683 |
while (_reader_bits::readIdentifier(line, map)) { |
684 | 684 |
if (maps.find(map) != maps.end()) { |
685 | 685 |
std::ostringstream msg; |
686 | 686 |
msg << "Multiple occurence of arc map: " << map; |
687 | 687 |
throw DataFormatError(msg.str().c_str()); |
688 | 688 |
} |
689 | 689 |
maps.insert(std::make_pair(map, index)); |
690 | 690 |
++index; |
691 | 691 |
} |
692 | 692 |
|
693 | 693 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
694 | 694 |
std::map<std::string, int>::iterator jt = |
695 | 695 |
maps.find(_arc_maps[i].first); |
696 | 696 |
if (jt == maps.end()) { |
697 | 697 |
std::ostringstream msg; |
698 | 698 |
msg << "Map not found in file: " << _arc_maps[i].first; |
699 | 699 |
throw DataFormatError(msg.str().c_str()); |
700 | 700 |
} |
701 | 701 |
map_index[i] = jt->second; |
702 | 702 |
} |
703 | 703 |
|
704 | 704 |
{ |
705 | 705 |
std::map<std::string, int>::iterator jt = maps.find("label"); |
706 | 706 |
if (jt == maps.end()) |
707 | 707 |
throw DataFormatError("Label map not found in file"); |
708 | 708 |
label_index = jt->second; |
709 | 709 |
} |
710 | 710 |
map_num = maps.size(); |
711 | 711 |
} |
712 | 712 |
|
713 | 713 |
char c; |
714 | 714 |
while (readLine() && line >> c && c != '@') { |
715 | 715 |
line.putback(c); |
716 | 716 |
|
717 | 717 |
std::string source_token; |
718 | 718 |
std::string target_token; |
719 | 719 |
|
720 | 720 |
if (!_reader_bits::readToken(line, source_token)) |
721 | 721 |
throw DataFormatError("Source not found"); |
722 | 722 |
|
723 | 723 |
if (!_reader_bits::readToken(line, target_token)) |
724 | 724 |
throw DataFormatError("Source not found"); |
725 | 725 |
|
726 | 726 |
std::vector<std::string> tokens(map_num); |
727 | 727 |
for (int i = 0; i < map_num; ++i) { |
728 | 728 |
if (!_reader_bits::readToken(line, tokens[i])) { |
729 | 729 |
std::ostringstream msg; |
730 | 730 |
msg << "Column not found (" << i + 1 << ")"; |
731 | 731 |
throw DataFormatError(msg.str().c_str()); |
732 | 732 |
} |
733 | 733 |
} |
734 | 734 |
if (line >> std::ws >> c) |
735 | 735 |
throw DataFormatError("Extra character on the end of line"); |
736 | 736 |
|
737 | 737 |
Arc a; |
738 | 738 |
if (!_use_arcs) { |
739 | 739 |
|
740 | 740 |
typename NodeIndex::iterator it; |
741 | 741 |
|
742 | 742 |
it = _node_index.find(source_token); |
743 | 743 |
if (it == _node_index.end()) { |
744 | 744 |
std::ostringstream msg; |
745 | 745 |
msg << "Item not found: " << source_token; |
746 | 746 |
throw DataFormatError(msg.str().c_str()); |
747 | 747 |
} |
748 | 748 |
Node source = it->second; |
749 | 749 |
|
750 | 750 |
it = _node_index.find(target_token); |
751 | 751 |
if (it == _node_index.end()) { |
752 | 752 |
std::ostringstream msg; |
753 | 753 |
msg << "Item not found: " << target_token; |
754 | 754 |
throw DataFormatError(msg.str().c_str()); |
755 | 755 |
} |
756 | 756 |
Node target = it->second; |
757 | 757 |
|
758 | 758 |
a = _digraph.addArc(source, target); |
759 | 759 |
_arc_index.insert(std::make_pair(tokens[label_index], a)); |
760 | 760 |
} else { |
761 | 761 |
typename std::map<std::string, Arc>::iterator it = |
762 | 762 |
_arc_index.find(tokens[label_index]); |
763 | 763 |
if (it == _arc_index.end()) { |
764 | 764 |
std::ostringstream msg; |
765 | 765 |
msg << "Arc with label not found: " << tokens[label_index]; |
766 | 766 |
throw DataFormatError(msg.str().c_str()); |
767 | 767 |
} |
768 | 768 |
a = it->second; |
769 | 769 |
} |
770 | 770 |
|
771 | 771 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
772 | 772 |
_arc_maps[i].second->set(a, tokens[map_index[i]]); |
773 | 773 |
} |
774 | 774 |
|
775 | 775 |
} |
776 | 776 |
if (readSuccess()) { |
777 | 777 |
line.putback(c); |
778 | 778 |
} |
779 | 779 |
} |
780 | 780 |
|
781 | 781 |
void readAttributes() { |
782 | 782 |
|
783 | 783 |
std::set<std::string> read_attr; |
784 | 784 |
|
785 | 785 |
char c; |
786 | 786 |
while (readLine() && line >> c && c != '@') { |
787 | 787 |
line.putback(c); |
788 | 788 |
|
789 | 789 |
std::string attr, token; |
790 | 790 |
if (!_reader_bits::readIdentifier(line, attr)) |
791 | 791 |
throw DataFormatError("Attribute name not found"); |
792 | 792 |
if (!_reader_bits::readToken(line, token)) |
793 | 793 |
throw DataFormatError("Attribute value not found"); |
794 | 794 |
if (line >> c) |
795 | 795 |
throw DataFormatError("Extra character on the end of line"); |
796 | 796 |
|
797 | 797 |
{ |
798 | 798 |
std::set<std::string>::iterator it = read_attr.find(attr); |
799 | 799 |
if (it != read_attr.end()) { |
800 | 800 |
std::ostringstream msg; |
801 | 801 |
msg << "Multiple occurence of attribute " << attr; |
802 | 802 |
throw DataFormatError(msg.str().c_str()); |
803 | 803 |
} |
804 | 804 |
read_attr.insert(attr); |
805 | 805 |
} |
806 | 806 |
|
807 | 807 |
{ |
808 | 808 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
809 | 809 |
while (it != _attributes.end() && it->first == attr) { |
810 | 810 |
it->second->set(token); |
811 | 811 |
++it; |
812 | 812 |
} |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
} |
816 | 816 |
if (readSuccess()) { |
817 | 817 |
line.putback(c); |
818 | 818 |
} |
819 | 819 |
for (typename Attributes::iterator it = _attributes.begin(); |
820 | 820 |
it != _attributes.end(); ++it) { |
821 | 821 |
if (read_attr.find(it->first) == read_attr.end()) { |
822 | 822 |
std::ostringstream msg; |
823 | 823 |
msg << "Attribute not found in file: " << it->first; |
824 | 824 |
throw DataFormatError(msg.str().c_str()); |
825 | 825 |
} |
826 | 826 |
} |
827 | 827 |
} |
828 | 828 |
|
829 | 829 |
public: |
830 | 830 |
|
831 | 831 |
/// \e |
832 | 832 |
void run() { |
833 | 833 |
|
834 | 834 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
835 | 835 |
|
836 | 836 |
bool nodes_done = false; |
837 | 837 |
bool arcs_done = false; |
838 | 838 |
bool attributes_done = false; |
839 | 839 |
|
840 | 840 |
line_num = 0; |
841 | 841 |
readLine(); |
842 | 842 |
|
843 | 843 |
while (readSuccess()) { |
844 | 844 |
skipSection(); |
845 | 845 |
try { |
846 | 846 |
char c; |
847 | 847 |
std::string section, caption; |
848 | 848 |
line >> c; |
849 | 849 |
_reader_bits::readIdentifier(line, section); |
850 | 850 |
_reader_bits::readIdentifier(line, caption); |
851 | 851 |
|
852 | 852 |
if (line >> c) |
853 | 853 |
throw DataFormatError("Extra character on the end of line"); |
854 | 854 |
|
855 | 855 |
if (section == "nodes" && !nodes_done) { |
856 | 856 |
if (_nodes_caption.empty() || _nodes_caption == caption) { |
857 | 857 |
readNodes(); |
858 | 858 |
nodes_done = true; |
859 | 859 |
} |
860 | 860 |
} else if ((section == "arcs" || section == "edges") && |
861 | 861 |
!arcs_done) { |
862 | 862 |
if (_arcs_caption.empty() || _arcs_caption == caption) { |
863 | 863 |
readArcs(); |
864 | 864 |
arcs_done = true; |
865 | 865 |
} |
866 | 866 |
} else if (section == "attributes" && !attributes_done) { |
867 | 867 |
if (_attributes_caption.empty() || _attributes_caption == caption) { |
868 | 868 |
readAttributes(); |
869 | 869 |
attributes_done = true; |
870 | 870 |
} |
871 | 871 |
} else { |
872 | 872 |
readLine(); |
873 | 873 |
skipSection(); |
874 | 874 |
} |
875 | 875 |
} catch (DataFormatError& error) { |
876 | 876 |
error.line(line_num); |
877 | 877 |
throw; |
878 | 878 |
} |
879 | 879 |
} |
880 | 880 |
|
881 | 881 |
if (!nodes_done) { |
882 | 882 |
throw DataFormatError("Section @nodes not found"); |
883 | 883 |
} |
884 | 884 |
|
885 | 885 |
if (!arcs_done) { |
886 | 886 |
throw DataFormatError("Section @arcs not found"); |
887 | 887 |
} |
888 | 888 |
|
889 | 889 |
if (!attributes_done && !_attributes.empty()) { |
890 | 890 |
throw DataFormatError("Section @attributes not found"); |
891 | 891 |
} |
892 | 892 |
|
893 | 893 |
} |
894 | 894 |
|
895 | 895 |
}; |
896 | 896 |
|
897 | 897 |
template <typename Digraph> |
898 | 898 |
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) { |
899 | 899 |
return DigraphReader<Digraph>(is, digraph); |
900 | 900 |
} |
901 | 901 |
|
902 | 902 |
template <typename Digraph> |
903 | 903 |
DigraphReader<Digraph> digraphReader(const std::string& fn, |
904 | 904 |
Digraph& digraph) { |
905 | 905 |
return DigraphReader<Digraph>(fn, digraph); |
906 | 906 |
} |
907 | 907 |
|
908 | 908 |
template <typename Digraph> |
909 | 909 |
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) { |
910 | 910 |
return DigraphReader<Digraph>(fn, digraph); |
911 | 911 |
} |
912 | 912 |
} |
913 | 913 |
|
914 | 914 |
#endif |
1 | 1 |
/* -*- C++ -*- |
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 |
///\ingroup lemon_io |
20 | 20 |
///\file |
21 | 21 |
///\brief Lemon Graph Format writer. |
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_LGF_WRITER_H |
25 | 25 |
#define LEMON_LGF_WRITER_H |
26 | 26 |
|
27 | 27 |
#include <iostream> |
28 | 28 |
#include <fstream> |
29 | 29 |
#include <sstream> |
30 | 30 |
|
31 | 31 |
#include <algorithm> |
32 | 32 |
|
33 | 33 |
#include <vector> |
34 | 34 |
#include <functional> |
35 | 35 |
|
36 | 36 |
#include <lemon/assert.h> |
37 | 37 |
#include <lemon/graph_utils.h> |
38 | 38 |
|
39 | 39 |
namespace lemon { |
40 | 40 |
|
41 | 41 |
namespace _writer_bits { |
42 | 42 |
|
43 | 43 |
template <typename Value> |
44 | 44 |
struct DefaultConverter { |
45 | 45 |
std::string operator()(const Value& value) { |
46 | 46 |
std::ostringstream os; |
47 | 47 |
os << value; |
48 | 48 |
return os.str(); |
49 | 49 |
} |
50 | 50 |
}; |
51 | 51 |
|
52 | 52 |
template <typename T> |
53 | 53 |
bool operator<(const T&, const T&) { |
54 | 54 |
throw DataFormatError("Label map is not comparable"); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
template <typename _Map> |
58 | 58 |
class MapLess { |
59 | 59 |
public: |
60 | 60 |
typedef _Map Map; |
61 | 61 |
typedef typename Map::Key Item; |
62 | 62 |
|
63 | 63 |
private: |
64 | 64 |
const Map& _map; |
65 | 65 |
|
66 | 66 |
public: |
67 | 67 |
MapLess(const Map& map) : _map(map) {} |
68 | 68 |
|
69 | 69 |
bool operator()(const Item& left, const Item& right) { |
70 | 70 |
return _map[left] < _map[right]; |
71 | 71 |
} |
72 | 72 |
}; |
73 | 73 |
|
74 | 74 |
template <typename _Item> |
75 | 75 |
class MapStorageBase { |
76 | 76 |
public: |
77 | 77 |
typedef _Item Item; |
78 | 78 |
|
79 | 79 |
public: |
80 | 80 |
MapStorageBase() {} |
81 | 81 |
virtual ~MapStorageBase() {} |
82 | 82 |
|
83 | 83 |
virtual std::string get(const Item& item) = 0; |
84 | 84 |
virtual void sort(std::vector<Item>&) = 0; |
85 | 85 |
}; |
86 | 86 |
|
87 | 87 |
template <typename _Item, typename _Map, |
88 | 88 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
89 | 89 |
class MapStorage : public MapStorageBase<_Item> { |
90 | 90 |
public: |
91 | 91 |
typedef _Map Map; |
92 | 92 |
typedef _Converter Converter; |
93 | 93 |
typedef _Item Item; |
94 | 94 |
|
95 | 95 |
private: |
96 | 96 |
const Map& _map; |
97 | 97 |
Converter _converter; |
98 | 98 |
|
99 | 99 |
public: |
100 | 100 |
MapStorage(const Map& map, const Converter& converter = Converter()) |
101 | 101 |
: _map(map), _converter(converter) {} |
102 | 102 |
virtual ~MapStorage() {} |
103 | 103 |
|
104 | 104 |
virtual std::string get(const Item& item) { |
105 | 105 |
return _converter(_map[item]); |
106 | 106 |
} |
107 | 107 |
virtual void sort(std::vector<Item>& items) { |
108 | 108 |
MapLess<Map> less(_map); |
109 | 109 |
std::sort(items.begin(), items.end(), less); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
class ValueStorageBase { |
114 | 114 |
public: |
115 | 115 |
ValueStorageBase() {} |
116 | 116 |
virtual ~ValueStorageBase() {} |
117 | 117 |
|
118 | 118 |
virtual std::string get() = 0; |
119 | 119 |
}; |
120 | 120 |
|
121 | 121 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
122 | 122 |
class ValueStorage : public ValueStorageBase { |
123 | 123 |
public: |
124 | 124 |
typedef _Value Value; |
125 | 125 |
typedef _Converter Converter; |
126 | 126 |
|
127 | 127 |
private: |
128 | 128 |
const Value& _value; |
129 | 129 |
Converter _converter; |
130 | 130 |
|
131 | 131 |
public: |
132 | 132 |
ValueStorage(const Value& value, const Converter& converter = Converter()) |
133 | 133 |
: _value(value), _converter(converter) {} |
134 | 134 |
|
135 | 135 |
virtual std::string get() { |
136 | 136 |
return _converter(_value); |
137 | 137 |
} |
138 | 138 |
}; |
139 | 139 |
|
140 | 140 |
template <typename Value> |
141 | 141 |
struct MapLookUpConverter { |
142 | 142 |
const std::map<Value, std::string>& _map; |
143 | 143 |
|
144 | 144 |
MapLookUpConverter(const std::map<Value, std::string>& map) |
145 | 145 |
: _map(map) {} |
146 | 146 |
|
147 | 147 |
std::string operator()(const Value& str) { |
148 | 148 |
typename std::map<Value, std::string>::const_iterator it = |
149 | 149 |
_map.find(str); |
150 | 150 |
if (it == _map.end()) { |
151 | 151 |
throw DataFormatError("Item not found"); |
152 | 152 |
} |
153 | 153 |
return it->second; |
154 | 154 |
} |
155 | 155 |
}; |
156 | 156 |
|
157 | 157 |
bool isWhiteSpace(char c) { |
158 | 158 |
return c == ' ' || c == '\t' || c == '\v' || |
159 | 159 |
c == '\n' || c == '\r' || c == '\f'; |
160 | 160 |
} |
161 | 161 |
|
162 | 162 |
bool isEscaped(char c) { |
163 | 163 |
return c == '\\' || c == '\"' || c == '\'' || |
164 | 164 |
c == '\a' || c == '\b'; |
165 | 165 |
} |
166 | 166 |
|
167 | 167 |
static void writeEscape(std::ostream& os, char c) { |
168 | 168 |
switch (c) { |
169 | 169 |
case '\\': |
170 | 170 |
os << "\\\\"; |
171 | 171 |
return; |
172 | 172 |
case '\"': |
173 | 173 |
os << "\\\""; |
174 | 174 |
return; |
175 | 175 |
case '\a': |
176 | 176 |
os << "\\a"; |
177 | 177 |
return; |
178 | 178 |
case '\b': |
179 | 179 |
os << "\\b"; |
180 | 180 |
return; |
181 | 181 |
case '\f': |
182 | 182 |
os << "\\f"; |
183 | 183 |
return; |
184 | 184 |
case '\r': |
185 | 185 |
os << "\\r"; |
186 | 186 |
return; |
187 | 187 |
case '\n': |
188 | 188 |
os << "\\n"; |
189 | 189 |
return; |
190 | 190 |
case '\t': |
191 | 191 |
os << "\\t"; |
192 | 192 |
return; |
193 | 193 |
case '\v': |
194 | 194 |
os << "\\v"; |
195 | 195 |
return; |
196 | 196 |
default: |
197 | 197 |
if (c < 0x20) { |
198 | 198 |
os << '\\' << std::oct << static_cast<int>(c); |
199 | 199 |
} else { |
200 | 200 |
os << c; |
201 | 201 |
} |
202 | 202 |
return; |
203 | 203 |
} |
204 | 204 |
} |
205 | 205 |
|
206 | 206 |
bool requireEscape(const std::string& str) { |
207 | 207 |
std::istringstream is(str); |
208 | 208 |
char c; |
209 | 209 |
while (is.get(c)) { |
210 | 210 |
if (isWhiteSpace(c) || isEscaped(c)) { |
211 | 211 |
return true; |
212 | 212 |
} |
213 | 213 |
} |
214 | 214 |
return false; |
215 | 215 |
} |
216 | 216 |
|
217 | 217 |
std::ostream& writeToken(std::ostream& os, const std::string& str) { |
218 | 218 |
|
219 | 219 |
if (requireEscape(str)) { |
220 | 220 |
os << '\"'; |
221 | 221 |
for (std::string::const_iterator it = str.begin(); |
222 | 222 |
it != str.end(); ++it) { |
223 | 223 |
writeEscape(os, *it); |
224 | 224 |
} |
225 | 225 |
os << '\"'; |
226 | 226 |
} else { |
227 | 227 |
os << str; |
228 | 228 |
} |
229 | 229 |
return os; |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
/// \e |
235 | 235 |
template <typename _Digraph> |
236 | 236 |
class DigraphWriter { |
237 | 237 |
public: |
238 | 238 |
|
239 | 239 |
typedef _Digraph Digraph; |
240 |
|
|
240 |
DIGRAPH_TYPEDEFS(typename Digraph); |
|
241 | 241 |
|
242 | 242 |
private: |
243 | 243 |
|
244 | 244 |
|
245 | 245 |
std::ostream* _os; |
246 | 246 |
bool local_os; |
247 | 247 |
|
248 | 248 |
Digraph& _digraph; |
249 | 249 |
|
250 | 250 |
std::string _nodes_caption; |
251 | 251 |
std::string _arcs_caption; |
252 | 252 |
std::string _attributes_caption; |
253 | 253 |
|
254 | 254 |
typedef std::map<Node, std::string> NodeIndex; |
255 | 255 |
NodeIndex _node_index; |
256 | 256 |
typedef std::map<Arc, std::string> ArcIndex; |
257 | 257 |
ArcIndex _arc_index; |
258 | 258 |
|
259 | 259 |
typedef std::vector<std::pair<std::string, |
260 | 260 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
261 | 261 |
NodeMaps _node_maps; |
262 | 262 |
|
263 | 263 |
typedef std::vector<std::pair<std::string, |
264 | 264 |
_writer_bits::MapStorageBase<Arc>* > >ArcMaps; |
265 | 265 |
ArcMaps _arc_maps; |
266 | 266 |
|
267 | 267 |
typedef std::vector<std::pair<std::string, |
268 | 268 |
_writer_bits::ValueStorageBase*> > Attributes; |
269 | 269 |
Attributes _attributes; |
270 | 270 |
|
271 | 271 |
bool _skip_nodes; |
272 | 272 |
bool _skip_arcs; |
273 | 273 |
|
274 | 274 |
public: |
275 | 275 |
|
276 | 276 |
/// \e |
277 | 277 |
DigraphWriter(std::ostream& is, Digraph& digraph) |
278 | 278 |
: _os(&is), local_os(false), _digraph(digraph), |
279 | 279 |
_skip_nodes(false), _skip_arcs(false) {} |
280 | 280 |
|
281 | 281 |
/// \e |
282 | 282 |
DigraphWriter(const std::string& fn, Digraph& digraph) |
283 | 283 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), |
284 | 284 |
_skip_nodes(false), _skip_arcs(false) {} |
285 | 285 |
|
286 | 286 |
/// \e |
287 | 287 |
DigraphWriter(const char* fn, Digraph& digraph) |
288 | 288 |
: _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), |
289 | 289 |
_skip_nodes(false), _skip_arcs(false) {} |
290 | 290 |
|
291 | 291 |
DigraphWriter(DigraphWriter& other) |
292 | 292 |
: _os(other._os), local_os(other.local_os), _digraph(other._digraph), |
293 | 293 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { |
294 | 294 |
|
295 | 295 |
other.is = 0; |
296 | 296 |
other.local_os = false; |
297 | 297 |
|
298 | 298 |
_node_index.swap(other._node_index); |
299 | 299 |
_arc_index.swap(other._arc_index); |
300 | 300 |
|
301 | 301 |
_node_maps.swap(other._node_maps); |
302 | 302 |
_arc_maps.swap(other._arc_maps); |
303 | 303 |
_attributes.swap(other._attributes); |
304 | 304 |
|
305 | 305 |
_nodes_caption = other._nodes_caption; |
306 | 306 |
_arcs_caption = other._arcs_caption; |
307 | 307 |
_attributes_caption = other._attributes_caption; |
308 | 308 |
} |
309 | 309 |
|
310 | 310 |
/// \e |
311 | 311 |
~DigraphWriter() { |
312 | 312 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
313 | 313 |
it != _node_maps.end(); ++it) { |
314 | 314 |
delete it->second; |
315 | 315 |
} |
316 | 316 |
|
317 | 317 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
318 | 318 |
it != _arc_maps.end(); ++it) { |
319 | 319 |
delete it->second; |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
for (typename Attributes::iterator it = _attributes.begin(); |
323 | 323 |
it != _attributes.end(); ++it) { |
324 | 324 |
delete it->second; |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
if (local_os) { |
328 | 328 |
delete _os; |
329 | 329 |
} |
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
private: |
333 | 333 |
|
334 | 334 |
DigraphWriter& operator=(const DigraphWriter&); |
335 | 335 |
|
336 | 336 |
public: |
337 | 337 |
|
338 | 338 |
/// \e |
339 | 339 |
template <typename Map> |
340 | 340 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map) { |
341 | 341 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
342 | 342 |
_writer_bits::MapStorageBase<Node>* storage = |
343 | 343 |
new _writer_bits::MapStorage<Node, Map>(map); |
344 | 344 |
_node_maps.push_back(std::make_pair(caption, storage)); |
345 | 345 |
return *this; |
346 | 346 |
} |
347 | 347 |
|
348 | 348 |
/// \e |
349 | 349 |
template <typename Map, typename Converter> |
350 | 350 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map, |
351 | 351 |
const Converter& converter = Converter()) { |
352 | 352 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
353 | 353 |
_writer_bits::MapStorageBase<Node>* storage = |
354 | 354 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
355 | 355 |
_node_maps.push_back(std::make_pair(caption, storage)); |
356 | 356 |
return *this; |
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
/// \e |
360 | 360 |
template <typename Map> |
361 | 361 |
DigraphWriter& arcMap(const std::string& caption, const Map& map) { |
362 | 362 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
363 | 363 |
_writer_bits::MapStorageBase<Arc>* storage = |
364 | 364 |
new _writer_bits::MapStorage<Arc, Map>(map); |
365 | 365 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
366 | 366 |
return *this; |
367 | 367 |
} |
368 | 368 |
|
369 | 369 |
/// \e |
370 | 370 |
template <typename Map, typename Converter> |
371 | 371 |
DigraphWriter& arcMap(const std::string& caption, const Map& map, |
372 | 372 |
const Converter& converter = Converter()) { |
373 | 373 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
374 | 374 |
_writer_bits::MapStorageBase<Arc>* storage = |
375 | 375 |
new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter); |
376 | 376 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
377 | 377 |
return *this; |
378 | 378 |
} |
379 | 379 |
|
380 | 380 |
/// \e |
381 | 381 |
template <typename Value> |
382 | 382 |
DigraphWriter& attribute(const std::string& caption, const Value& value) { |
383 | 383 |
_writer_bits::ValueStorageBase* storage = |
384 | 384 |
new _writer_bits::ValueStorage<Value>(value); |
385 | 385 |
_attributes.push_back(std::make_pair(caption, storage)); |
386 | 386 |
return *this; |
387 | 387 |
} |
388 | 388 |
|
389 | 389 |
/// \e |
390 | 390 |
template <typename Value, typename Converter> |
391 | 391 |
DigraphWriter& attribute(const std::string& caption, const Value& value, |
392 | 392 |
const Converter& converter = Converter()) { |
393 | 393 |
_writer_bits::ValueStorageBase* storage = |
394 | 394 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
395 | 395 |
_attributes.push_back(std::make_pair(caption, storage)); |
396 | 396 |
return *this; |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
/// \e |
400 | 400 |
DigraphWriter& node(const std::string& caption, const Node& node) { |
401 | 401 |
typedef _writer_bits::MapLookUpConverter<Node> Converter; |
402 | 402 |
Converter converter(_node_index); |
403 | 403 |
_writer_bits::ValueStorageBase* storage = |
404 | 404 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
405 | 405 |
_attributes.push_back(std::make_pair(caption, storage)); |
406 | 406 |
return *this; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
/// \e |
410 | 410 |
DigraphWriter& arc(const std::string& caption, const Arc& arc) { |
411 | 411 |
typedef _writer_bits::MapLookUpConverter<Arc> Converter; |
412 | 412 |
Converter converter(_arc_index); |
413 | 413 |
_writer_bits::ValueStorageBase* storage = |
414 | 414 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
415 | 415 |
_attributes.push_back(std::make_pair(caption, storage)); |
416 | 416 |
return *this; |
417 | 417 |
} |
418 | 418 |
|
419 | 419 |
/// \e |
420 | 420 |
DigraphWriter& nodes(const std::string& caption) { |
421 | 421 |
_nodes_caption = caption; |
422 | 422 |
return *this; |
423 | 423 |
} |
424 | 424 |
|
425 | 425 |
/// \e |
426 | 426 |
DigraphWriter& arcs(const std::string& caption) { |
427 | 427 |
_arcs_caption = caption; |
428 | 428 |
return *this; |
429 | 429 |
} |
430 | 430 |
|
431 | 431 |
/// \e |
432 | 432 |
DigraphWriter& attributes(const std::string& caption) { |
433 | 433 |
_attributes_caption = caption; |
434 | 434 |
return *this; |
435 | 435 |
} |
436 | 436 |
|
437 | 437 |
DigraphWriter& skipNodes() { |
438 | 438 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
439 | 439 |
return *this; |
440 | 440 |
} |
441 | 441 |
|
442 | 442 |
DigraphWriter& skipArcs() { |
443 | 443 |
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); |
444 | 444 |
return *this; |
445 | 445 |
} |
446 | 446 |
|
447 | 447 |
private: |
448 | 448 |
|
449 | 449 |
void writeNodes() { |
450 | 450 |
_writer_bits::MapStorageBase<Node>* label = 0; |
451 | 451 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
452 | 452 |
it != _node_maps.end(); ++it) { |
453 | 453 |
if (it->first == "label") { |
454 | 454 |
label = it->second; |
455 | 455 |
break; |
456 | 456 |
} |
457 | 457 |
} |
458 | 458 |
|
459 | 459 |
*_os << "@nodes"; |
460 | 460 |
if (!_nodes_caption.empty()) { |
461 | 461 |
*_os << ' ' << _nodes_caption; |
462 | 462 |
} |
463 | 463 |
*_os << std::endl; |
464 | 464 |
|
465 | 465 |
if (label == 0) { |
466 | 466 |
*_os << "label" << '\t'; |
467 | 467 |
} |
468 | 468 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
469 | 469 |
it != _node_maps.end(); ++it) { |
470 | 470 |
*_os << it->first << '\t'; |
471 | 471 |
} |
472 | 472 |
*_os << std::endl; |
473 | 473 |
|
474 | 474 |
std::vector<Node> nodes; |
475 | 475 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
476 | 476 |
nodes.push_back(n); |
477 | 477 |
} |
478 | 478 |
|
479 | 479 |
if (label == 0) { |
480 | 480 |
IdMap<Digraph, Node> id_map(_digraph); |
481 | 481 |
_writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map); |
482 | 482 |
std::sort(nodes.begin(), nodes.end(), id_less); |
483 | 483 |
} else { |
484 | 484 |
label->sort(nodes); |
485 | 485 |
} |
486 | 486 |
|
487 | 487 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) { |
488 | 488 |
Node n = nodes[i]; |
489 | 489 |
if (label == 0) { |
490 | 490 |
std::ostringstream os; |
491 | 491 |
os << _digraph.id(n); |
492 | 492 |
_writer_bits::writeToken(*_os, os.str()); |
493 | 493 |
*_os << '\t'; |
494 | 494 |
_node_index.insert(std::make_pair(n, os.str())); |
495 | 495 |
} |
496 | 496 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
497 | 497 |
it != _node_maps.end(); ++it) { |
498 | 498 |
std::string value = it->second->get(n); |
499 | 499 |
_writer_bits::writeToken(*_os, value); |
500 | 500 |
if (it->first == "label") { |
501 | 501 |
_node_index.insert(std::make_pair(n, value)); |
502 | 502 |
} |
503 | 503 |
*_os << '\t'; |
504 | 504 |
} |
505 | 505 |
*_os << std::endl; |
506 | 506 |
} |
507 | 507 |
} |
508 | 508 |
|
509 | 509 |
void writeArcs() { |
510 | 510 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
511 | 511 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
512 | 512 |
it != _arc_maps.end(); ++it) { |
513 | 513 |
if (it->first == "label") { |
514 | 514 |
label = it->second; |
515 | 515 |
break; |
516 | 516 |
} |
517 | 517 |
} |
518 | 518 |
|
519 | 519 |
*_os << "@arcs"; |
520 | 520 |
if (!_arcs_caption.empty()) { |
521 | 521 |
*_os << ' ' << _arcs_caption; |
522 | 522 |
} |
523 | 523 |
*_os << std::endl; |
524 | 524 |
|
525 | 525 |
*_os << '\t' << '\t'; |
526 | 526 |
if (label == 0) { |
527 | 527 |
*_os << "label" << '\t'; |
528 | 528 |
} |
529 | 529 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
530 | 530 |
it != _arc_maps.end(); ++it) { |
531 | 531 |
*_os << it->first << '\t'; |
532 | 532 |
} |
533 | 533 |
*_os << std::endl; |
534 | 534 |
|
535 | 535 |
std::vector<Arc> arcs; |
536 | 536 |
for (ArcIt n(_digraph); n != INVALID; ++n) { |
537 | 537 |
arcs.push_back(n); |
538 | 538 |
} |
539 | 539 |
|
540 | 540 |
if (label == 0) { |
541 | 541 |
IdMap<Digraph, Arc> id_map(_digraph); |
542 | 542 |
_writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map); |
543 | 543 |
std::sort(arcs.begin(), arcs.end(), id_less); |
544 | 544 |
} else { |
545 | 545 |
label->sort(arcs); |
546 | 546 |
} |
547 | 547 |
|
548 | 548 |
for (int i = 0; i < static_cast<int>(arcs.size()); ++i) { |
549 | 549 |
Arc a = arcs[i]; |
550 | 550 |
_writer_bits::writeToken(*_os, _node_index. |
551 | 551 |
find(_digraph.source(a))->second); |
552 | 552 |
*_os << '\t'; |
553 | 553 |
_writer_bits::writeToken(*_os, _node_index. |
554 | 554 |
find(_digraph.target(a))->second); |
555 | 555 |
*_os << '\t'; |
556 | 556 |
if (label == 0) { |
557 | 557 |
std::ostringstream os; |
558 | 558 |
os << _digraph.id(a); |
559 | 559 |
_writer_bits::writeToken(*_os, os.str()); |
560 | 560 |
*_os << '\t'; |
561 | 561 |
_arc_index.insert(std::make_pair(a, os.str())); |
562 | 562 |
} |
563 | 563 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
564 | 564 |
it != _arc_maps.end(); ++it) { |
565 | 565 |
std::string value = it->second->get(a); |
566 | 566 |
_writer_bits::writeToken(*_os, value); |
567 | 567 |
if (it->first == "label") { |
568 | 568 |
_arc_index.insert(std::make_pair(a, value)); |
569 | 569 |
} |
570 | 570 |
*_os << '\t'; |
571 | 571 |
} |
572 | 572 |
*_os << std::endl; |
573 | 573 |
} |
574 | 574 |
} |
575 | 575 |
|
576 | 576 |
void writeAttributes() { |
577 | 577 |
if (_attributes.empty()) return; |
578 | 578 |
*_os << "@attributes"; |
579 | 579 |
if (!_attributes_caption.empty()) { |
580 | 580 |
*_os << ' ' << _attributes_caption; |
581 | 581 |
} |
582 | 582 |
*_os << std::endl; |
583 | 583 |
for (typename Attributes::iterator it = _attributes.begin(); |
584 | 584 |
it != _attributes.end(); ++it) { |
585 | 585 |
*_os << it->first << ' '; |
586 | 586 |
_writer_bits::writeToken(*_os, it->second->get()); |
587 | 587 |
*_os << std::endl; |
588 | 588 |
} |
589 | 589 |
} |
590 | 590 |
|
591 | 591 |
public: |
592 | 592 |
|
593 | 593 |
/// \e |
594 | 594 |
void run() { |
595 | 595 |
if (!_skip_nodes) { |
596 | 596 |
writeNodes(); |
597 | 597 |
} |
598 | 598 |
if (!_skip_arcs) { |
599 | 599 |
writeArcs(); |
600 | 600 |
} |
601 | 601 |
writeAttributes(); |
602 | 602 |
} |
603 | 603 |
|
604 | 604 |
/// \e |
605 | 605 |
std::ostream& stream() { |
606 | 606 |
return *_os; |
607 | 607 |
} |
608 | 608 |
}; |
609 | 609 |
|
610 | 610 |
template <typename Digraph> |
611 | 611 |
DigraphWriter<Digraph> digraphWriter(std::istream& is, Digraph& digraph) { |
612 | 612 |
return DigraphWriter<Digraph>(is, digraph); |
613 | 613 |
} |
614 | 614 |
|
615 | 615 |
template <typename Digraph> |
616 | 616 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn, |
617 | 617 |
Digraph& digraph) { |
618 | 618 |
return DigraphWriter<Digraph>(fn, digraph); |
619 | 619 |
} |
620 | 620 |
|
621 | 621 |
template <typename Digraph> |
622 | 622 |
DigraphWriter<Digraph> digraphWriter(const char* fn, Digraph& digraph) { |
623 | 623 |
return DigraphWriter<Digraph>(fn, digraph); |
624 | 624 |
} |
625 | 625 |
} |
626 | 626 |
|
627 | 627 |
#endif |
1 | 1 |
/* -*- C++ -*- |
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 |
#ifndef LEMON_SMART_GRAPH_H |
20 | 20 |
#define LEMON_SMART_GRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graphs |
23 | 23 |
///\file |
24 | 24 |
///\brief SmartDigraph and SmartGraph classes. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/bits/base_extender.h> |
31 | 31 |
#include <lemon/bits/graph_extender.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bits/utility.h> |
34 | 34 |
#include <lemon/error.h> |
35 | 35 |
|
36 | 36 |
#include <lemon/bits/graph_extender.h> |
37 | 37 |
|
38 | 38 |
namespace lemon { |
39 | 39 |
|
40 | 40 |
class SmartDigraph; |
41 | 41 |
///Base of SmartDigraph |
42 | 42 |
|
43 | 43 |
///Base of SmartDigraph |
44 | 44 |
/// |
45 | 45 |
class SmartDigraphBase { |
46 | 46 |
protected: |
47 | 47 |
|
48 | 48 |
struct NodeT |
49 | 49 |
{ |
50 | 50 |
int first_in, first_out; |
51 | 51 |
NodeT() {} |
52 | 52 |
}; |
53 | 53 |
struct ArcT |
54 | 54 |
{ |
55 | 55 |
int target, source, next_in, next_out; |
56 | 56 |
ArcT() {} |
57 | 57 |
}; |
58 | 58 |
|
59 | 59 |
std::vector<NodeT> nodes; |
60 | 60 |
std::vector<ArcT> arcs; |
61 | 61 |
|
62 | 62 |
public: |
63 | 63 |
|
64 | 64 |
typedef SmartDigraphBase Graph; |
65 | 65 |
|
66 | 66 |
class Node; |
67 | 67 |
class Arc; |
68 | 68 |
|
69 | 69 |
public: |
70 | 70 |
|
71 | 71 |
SmartDigraphBase() : nodes(), arcs() { } |
72 | 72 |
SmartDigraphBase(const SmartDigraphBase &_g) |
73 | 73 |
: nodes(_g.nodes), arcs(_g.arcs) { } |
74 | 74 |
|
75 | 75 |
typedef True NodeNumTag; |
76 |
typedef True |
|
76 |
typedef True EdgeNumTag; |
|
77 | 77 |
|
78 | 78 |
int nodeNum() const { return nodes.size(); } |
79 | 79 |
int arcNum() const { return arcs.size(); } |
80 | 80 |
|
81 | 81 |
int maxNodeId() const { return nodes.size()-1; } |
82 | 82 |
int maxArcId() const { return arcs.size()-1; } |
83 | 83 |
|
84 | 84 |
Node addNode() { |
85 | 85 |
int n = nodes.size(); |
86 | 86 |
nodes.push_back(NodeT()); |
87 | 87 |
nodes[n].first_in = -1; |
88 | 88 |
nodes[n].first_out = -1; |
89 | 89 |
return Node(n); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
Arc addArc(Node u, Node v) { |
93 | 93 |
int n = arcs.size(); |
94 | 94 |
arcs.push_back(ArcT()); |
95 | 95 |
arcs[n].source = u._id; |
96 | 96 |
arcs[n].target = v._id; |
97 | 97 |
arcs[n].next_out = nodes[u._id].first_out; |
98 | 98 |
arcs[n].next_in = nodes[v._id].first_in; |
99 | 99 |
nodes[u._id].first_out = nodes[v._id].first_in = n; |
100 | 100 |
|
101 | 101 |
return Arc(n); |
102 | 102 |
} |
103 | 103 |
|
104 | 104 |
void clear() { |
105 | 105 |
arcs.clear(); |
106 | 106 |
nodes.clear(); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
Node source(Arc a) const { return Node(arcs[a._id].source); } |
110 | 110 |
Node target(Arc a) const { return Node(arcs[a._id].target); } |
111 | 111 |
|
112 | 112 |
static int id(Node v) { return v._id; } |
113 | 113 |
static int id(Arc a) { return a._id; } |
114 | 114 |
|
115 | 115 |
static Node nodeFromId(int id) { return Node(id);} |
116 | 116 |
static Arc arcFromId(int id) { return Arc(id);} |
117 | 117 |
|
118 | 118 |
class Node { |
119 | 119 |
friend class SmartDigraphBase; |
120 | 120 |
friend class SmartDigraph; |
121 | 121 |
|
122 | 122 |
protected: |
123 | 123 |
int _id; |
124 | 124 |
explicit Node(int id) : _id(id) {} |
125 | 125 |
public: |
126 | 126 |
Node() {} |
127 | 127 |
Node (Invalid) : _id(-1) {} |
128 | 128 |
bool operator==(const Node i) const {return _id == i._id;} |
129 | 129 |
bool operator!=(const Node i) const {return _id != i._id;} |
130 | 130 |
bool operator<(const Node i) const {return _id < i._id;} |
131 | 131 |
}; |
132 | 132 |
|
133 | 133 |
|
134 | 134 |
class Arc { |
135 | 135 |
friend class SmartDigraphBase; |
136 | 136 |
friend class SmartDigraph; |
137 | 137 |
|
138 | 138 |
protected: |
139 | 139 |
int _id; |
140 | 140 |
explicit Arc(int id) : _id(id) {} |
141 | 141 |
public: |
142 | 142 |
Arc() { } |
143 | 143 |
Arc (Invalid) : _id(-1) {} |
144 | 144 |
bool operator==(const Arc i) const {return _id == i._id;} |
145 | 145 |
bool operator!=(const Arc i) const {return _id != i._id;} |
146 | 146 |
bool operator<(const Arc i) const {return _id < i._id;} |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
void first(Node& node) const { |
150 | 150 |
node._id = nodes.size() - 1; |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
static void next(Node& node) { |
154 | 154 |
--node._id; |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
void first(Arc& arc) const { |
158 | 158 |
arc._id = arcs.size() - 1; |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
static void next(Arc& arc) { |
162 | 162 |
--arc._id; |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
void firstOut(Arc& arc, const Node& node) const { |
166 | 166 |
arc._id = nodes[node._id].first_out; |
167 | 167 |
} |
168 | 168 |
|
169 | 169 |
void nextOut(Arc& arc) const { |
170 | 170 |
arc._id = arcs[arc._id].next_out; |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
void firstIn(Arc& arc, const Node& node) const { |
174 | 174 |
arc._id = nodes[node._id].first_in; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
void nextIn(Arc& arc) const { |
178 | 178 |
arc._id = arcs[arc._id].next_in; |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
}; |
182 | 182 |
|
183 | 183 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
184 | 184 |
|
185 | 185 |
///\ingroup graphs |
186 | 186 |
/// |
187 | 187 |
///\brief A smart directed graph class. |
188 | 188 |
/// |
189 | 189 |
///This is a simple and fast digraph implementation. |
190 | 190 |
///It is also quite memory efficient, but at the price |
191 | 191 |
///that <b> it does support only limited (only stack-like) |
192 | 192 |
///node and arc deletions</b>. |
193 | 193 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
194 | 194 |
///an important extra feature that its maps are real \ref |
195 | 195 |
///concepts::ReferenceMap "reference map"s. |
196 | 196 |
/// |
197 | 197 |
///\sa concepts::Digraph. |
198 | 198 |
/// |
199 | 199 |
///\author Alpar Juttner |
200 | 200 |
class SmartDigraph : public ExtendedSmartDigraphBase { |
201 | 201 |
public: |
202 | 202 |
|
203 | 203 |
typedef ExtendedSmartDigraphBase Parent; |
204 | 204 |
|
205 | 205 |
private: |
206 | 206 |
|
207 | 207 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
208 | 208 |
|
209 | 209 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
210 | 210 |
/// |
211 | 211 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {}; |
212 | 212 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
213 | 213 |
///Use DigraphCopy() instead. |
214 | 214 |
|
215 | 215 |
///Assignment of SmartDigraph to another one is \e not allowed. |
216 | 216 |
///Use DigraphCopy() instead. |
217 | 217 |
void operator=(const SmartDigraph &) {} |
218 | 218 |
|
219 | 219 |
public: |
220 | 220 |
|
221 | 221 |
/// Constructor |
222 | 222 |
|
223 | 223 |
/// Constructor. |
224 | 224 |
/// |
225 | 225 |
SmartDigraph() {}; |
226 | 226 |
|
227 | 227 |
///Add a new node to the digraph. |
228 | 228 |
|
229 | 229 |
/// \return the new node. |
230 | 230 |
/// |
231 | 231 |
Node addNode() { return Parent::addNode(); } |
232 | 232 |
|
233 | 233 |
///Add a new arc to the digraph. |
234 | 234 |
|
235 | 235 |
///Add a new arc to the digraph with source node \c s |
236 | 236 |
///and target node \c t. |
237 | 237 |
///\return the new arc. |
238 | 238 |
Arc addArc(const Node& s, const Node& t) { |
239 | 239 |
return Parent::addArc(s, t); |
240 | 240 |
} |
241 | 241 |
|
242 | 242 |
/// \brief Using this it is possible to avoid the superfluous memory |
243 | 243 |
/// allocation. |
244 | 244 |
|
245 | 245 |
/// Using this it is possible to avoid the superfluous memory |
246 | 246 |
/// allocation: if you know that the digraph you want to build will |
247 | 247 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
248 | 248 |
/// then it is worth reserving space for this amount before starting |
249 | 249 |
/// to build the digraph. |
250 | 250 |
/// \sa reserveArc |
251 | 251 |
void reserveNode(int n) { nodes.reserve(n); }; |
252 | 252 |
|
253 | 253 |
/// \brief Using this it is possible to avoid the superfluous memory |
254 | 254 |
/// allocation. |
255 | 255 |
|
256 | 256 |
/// Using this it is possible to avoid the superfluous memory |
257 | 257 |
/// allocation: if you know that the digraph you want to build will |
258 | 258 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
259 | 259 |
/// then it is worth reserving space for this amount before starting |
260 | 260 |
/// to build the digraph. |
261 | 261 |
/// \sa reserveNode |
262 | 262 |
void reserveArc(int m) { arcs.reserve(m); }; |
263 | 263 |
|
264 | 264 |
///Clear the digraph. |
265 | 265 |
|
266 | 266 |
///Erase all the nodes and arcs from the digraph. |
267 | 267 |
/// |
268 | 268 |
void clear() { |
269 | 269 |
Parent::clear(); |
270 | 270 |
} |
271 | 271 |
|
272 | 272 |
///Split a node. |
273 | 273 |
|
274 | 274 |
///This function splits a node. First a new node is added to the digraph, |
275 | 275 |
///then the source of each outgoing arc of \c n is moved to this new node. |
276 | 276 |
///If \c connect is \c true (this is the default value), then a new arc |
277 | 277 |
///from \c n to the newly created node is also added. |
278 | 278 |
///\return The newly created node. |
279 | 279 |
/// |
280 | 280 |
///\note The <tt>Arc</tt>s |
281 | 281 |
///referencing a moved arc remain |
282 | 282 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
283 | 283 |
///may be invalidated. |
284 | 284 |
///\warning This functionality cannot be used together with the Snapshot |
285 | 285 |
///feature. |
286 | 286 |
///\todo It could be implemented in a bit faster way. |
287 | 287 |
Node split(Node n, bool connect = true) |
288 | 288 |
{ |
289 | 289 |
Node b = addNode(); |
290 | 290 |
nodes[b._id].first_out=nodes[n._id].first_out; |
291 | 291 |
nodes[n._id].first_out=-1; |
292 | 292 |
for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id; |
293 | 293 |
if(connect) addArc(n,b); |
294 | 294 |
return b; |
295 | 295 |
} |
296 | 296 |
|
297 | 297 |
public: |
298 | 298 |
|
299 | 299 |
class Snapshot; |
300 | 300 |
|
301 | 301 |
protected: |
302 | 302 |
|
303 | 303 |
void restoreSnapshot(const Snapshot &s) |
304 | 304 |
{ |
305 | 305 |
while(s.arc_num<arcs.size()) { |
306 | 306 |
Arc arc = arcFromId(arcs.size()-1); |
307 | 307 |
Parent::notifier(Arc()).erase(arc); |
308 | 308 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
309 | 309 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
310 | 310 |
arcs.pop_back(); |
311 | 311 |
} |
312 | 312 |
while(s.node_num<nodes.size()) { |
313 | 313 |
Node node = nodeFromId(nodes.size()-1); |
314 | 314 |
Parent::notifier(Node()).erase(node); |
315 | 315 |
nodes.pop_back(); |
316 | 316 |
} |
317 | 317 |
} |
318 | 318 |
|
319 | 319 |
public: |
320 | 320 |
|
321 | 321 |
///Class to make a snapshot of the digraph and to restrore to it later. |
322 | 322 |
|
323 | 323 |
///Class to make a snapshot of the digraph and to restrore to it later. |
324 | 324 |
/// |
325 | 325 |
///The newly added nodes and arcs can be removed using the |
326 | 326 |
///restore() function. |
327 | 327 |
///\note After you restore a state, you cannot restore |
328 | 328 |
///a later state, in other word you cannot add again the arcs deleted |
329 | 329 |
///by restore() using another one Snapshot instance. |
330 | 330 |
/// |
331 | 331 |
///\warning If you do not use correctly the snapshot that can cause |
332 | 332 |
///either broken program, invalid state of the digraph, valid but |
333 | 333 |
///not the restored digraph or no change. Because the runtime performance |
334 | 334 |
///the validity of the snapshot is not stored. |
335 | 335 |
class Snapshot |
336 | 336 |
{ |
337 | 337 |
SmartDigraph *_graph; |
338 | 338 |
protected: |
339 | 339 |
friend class SmartDigraph; |
340 | 340 |
unsigned int node_num; |
341 | 341 |
unsigned int arc_num; |
342 | 342 |
public: |
343 | 343 |
///Default constructor. |
344 | 344 |
|
345 | 345 |
///Default constructor. |
346 | 346 |
///To actually make a snapshot you must call save(). |
347 | 347 |
/// |
348 | 348 |
Snapshot() : _graph(0) {} |
349 | 349 |
///Constructor that immediately makes a snapshot |
350 | 350 |
|
351 | 351 |
///This constructor immediately makes a snapshot of the digraph. |
352 | 352 |
///\param _g The digraph we make a snapshot of. |
353 | 353 |
Snapshot(SmartDigraph &graph) : _graph(&graph) { |
354 | 354 |
node_num=_graph->nodes.size(); |
355 | 355 |
arc_num=_graph->arcs.size(); |
356 | 356 |
} |
357 | 357 |
|
358 | 358 |
///Make a snapshot. |
359 | 359 |
|
360 | 360 |
///Make a snapshot of the digraph. |
361 | 361 |
/// |
362 | 362 |
///This function can be called more than once. In case of a repeated |
363 | 363 |
///call, the previous snapshot gets lost. |
364 | 364 |
///\param _g The digraph we make the snapshot of. |
365 | 365 |
void save(SmartDigraph &graph) |
366 | 366 |
{ |
367 | 367 |
_graph=&graph; |
368 | 368 |
node_num=_graph->nodes.size(); |
369 | 369 |
arc_num=_graph->arcs.size(); |
370 | 370 |
} |
371 | 371 |
|
372 | 372 |
///Undo the changes until a snapshot. |
373 | 373 |
|
374 | 374 |
///Undo the changes until a snapshot created by save(). |
375 | 375 |
/// |
376 | 376 |
///\note After you restored a state, you cannot restore |
377 | 377 |
///a later state, in other word you cannot add again the arcs deleted |
378 | 378 |
///by restore(). |
379 | 379 |
void restore() |
380 | 380 |
{ |
381 | 381 |
_graph->restoreSnapshot(*this); |
382 | 382 |
} |
383 | 383 |
}; |
384 | 384 |
}; |
385 | 385 |
|
386 | 386 |
|
387 | 387 |
class SmartGraphBase { |
388 | 388 |
|
389 | 389 |
protected: |
390 | 390 |
|
391 | 391 |
struct NodeT { |
392 | 392 |
int first_out; |
393 | 393 |
}; |
394 | 394 |
|
395 | 395 |
struct ArcT { |
396 | 396 |
int target; |
397 | 397 |
int next_out; |
398 | 398 |
}; |
399 | 399 |
|
400 | 400 |
std::vector<NodeT> nodes; |
401 | 401 |
std::vector<ArcT> arcs; |
402 | 402 |
|
403 | 403 |
int first_free_arc; |
404 | 404 |
|
405 | 405 |
public: |
406 | 406 |
|
407 | 407 |
typedef SmartGraphBase Digraph; |
408 | 408 |
|
409 | 409 |
class Node; |
410 | 410 |
class Arc; |
411 | 411 |
class Edge; |
412 | 412 |
|
413 | 413 |
class Node { |
414 | 414 |
friend class SmartGraphBase; |
415 | 415 |
protected: |
416 | 416 |
|
417 | 417 |
int _id; |
418 | 418 |
explicit Node(int id) { _id = id;} |
419 | 419 |
|
420 | 420 |
public: |
421 | 421 |
Node() {} |
422 | 422 |
Node (Invalid) { _id = -1; } |
423 | 423 |
bool operator==(const Node& node) const {return _id == node._id;} |
424 | 424 |
bool operator!=(const Node& node) const {return _id != node._id;} |
425 | 425 |
bool operator<(const Node& node) const {return _id < node._id;} |
426 | 426 |
}; |
427 | 427 |
|
428 | 428 |
class Edge { |
429 | 429 |
friend class SmartGraphBase; |
430 | 430 |
protected: |
431 | 431 |
|
432 | 432 |
int _id; |
433 | 433 |
explicit Edge(int id) { _id = id;} |
434 | 434 |
|
435 | 435 |
public: |
436 | 436 |
Edge() {} |
437 | 437 |
Edge (Invalid) { _id = -1; } |
438 | 438 |
bool operator==(const Edge& arc) const {return _id == arc._id;} |
439 | 439 |
bool operator!=(const Edge& arc) const {return _id != arc._id;} |
440 | 440 |
bool operator<(const Edge& arc) const {return _id < arc._id;} |
441 | 441 |
}; |
442 | 442 |
|
443 | 443 |
class Arc { |
444 | 444 |
friend class SmartGraphBase; |
445 | 445 |
protected: |
446 | 446 |
|
447 | 447 |
int _id; |
448 | 448 |
explicit Arc(int id) { _id = id;} |
449 | 449 |
|
450 | 450 |
public: |
451 | 451 |
operator Edge() const { return edgeFromId(_id / 2); } |
452 | 452 |
|
453 | 453 |
Arc() {} |
454 | 454 |
Arc (Invalid) { _id = -1; } |
455 | 455 |
bool operator==(const Arc& arc) const {return _id == arc._id;} |
456 | 456 |
bool operator!=(const Arc& arc) const {return _id != arc._id;} |
457 | 457 |
bool operator<(const Arc& arc) const {return _id < arc._id;} |
458 | 458 |
}; |
459 | 459 |
|
460 | 460 |
|
461 | 461 |
|
462 | 462 |
SmartGraphBase() |
463 | 463 |
: nodes(), arcs() {} |
464 | 464 |
|
465 | 465 |
|
466 | 466 |
int maxNodeId() const { return nodes.size()-1; } |
467 | 467 |
int maxEdgeId() const { return arcs.size() / 2 - 1; } |
468 | 468 |
int maxArcId() const { return arcs.size()-1; } |
469 | 469 |
|
470 | 470 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); } |
471 | 471 |
Node target(Arc e) const { return Node(arcs[e._id].target); } |
472 | 472 |
|
473 | 473 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); } |
474 | 474 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); } |
475 | 475 |
|
476 | 476 |
static bool direction(Arc e) { |
477 | 477 |
return (e._id & 1) == 1; |
478 | 478 |
} |
479 | 479 |
|
480 | 480 |
static Arc direct(Edge e, bool d) { |
481 | 481 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
482 | 482 |
} |
483 | 483 |
|
484 | 484 |
void first(Node& node) const { |
485 | 485 |
node._id = nodes.size() - 1; |
486 | 486 |
} |
487 | 487 |
|
488 | 488 |
void next(Node& node) const { |
489 | 489 |
--node._id; |
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
void first(Arc& arc) const { |
493 | 493 |
arc._id = arcs.size() - 1; |
494 | 494 |
} |
495 | 495 |
|
496 | 496 |
void next(Arc& arc) const { |
497 | 497 |
--arc._id; |
498 | 498 |
} |
499 | 499 |
|
500 | 500 |
void first(Edge& arc) const { |
501 | 501 |
arc._id = arcs.size() / 2 - 1; |
502 | 502 |
} |
503 | 503 |
|
504 | 504 |
void next(Edge& arc) const { |
505 | 505 |
--arc._id; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
void firstOut(Arc &arc, const Node& v) const { |
509 | 509 |
arc._id = nodes[v._id].first_out; |
510 | 510 |
} |
511 | 511 |
void nextOut(Arc &arc) const { |
512 | 512 |
arc._id = arcs[arc._id].next_out; |
513 | 513 |
} |
514 | 514 |
|
515 | 515 |
void firstIn(Arc &arc, const Node& v) const { |
516 | 516 |
arc._id = ((nodes[v._id].first_out) ^ 1); |
517 | 517 |
if (arc._id == -2) arc._id = -1; |
518 | 518 |
} |
519 | 519 |
void nextIn(Arc &arc) const { |
520 | 520 |
arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
521 | 521 |
if (arc._id == -2) arc._id = -1; |
522 | 522 |
} |
523 | 523 |
|
524 | 524 |
void firstInc(Edge &arc, bool& d, const Node& v) const { |
525 | 525 |
int de = nodes[v._id].first_out; |
526 | 526 |
if (de != -1) { |
527 | 527 |
arc._id = de / 2; |
528 | 528 |
d = ((de & 1) == 1); |
529 | 529 |
} else { |
530 | 530 |
arc._id = -1; |
531 | 531 |
d = true; |
532 | 532 |
} |
533 | 533 |
} |
534 | 534 |
void nextInc(Edge &arc, bool& d) const { |
535 | 535 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
536 | 536 |
if (de != -1) { |
537 | 537 |
arc._id = de / 2; |
538 | 538 |
d = ((de & 1) == 1); |
539 | 539 |
} else { |
540 | 540 |
arc._id = -1; |
541 | 541 |
d = true; |
542 | 542 |
} |
543 | 543 |
} |
544 | 544 |
|
545 | 545 |
static int id(Node v) { return v._id; } |
546 | 546 |
static int id(Arc e) { return e._id; } |
547 | 547 |
static int id(Edge e) { return e._id; } |
548 | 548 |
|
549 | 549 |
static Node nodeFromId(int id) { return Node(id);} |
550 | 550 |
static Arc arcFromId(int id) { return Arc(id);} |
551 | 551 |
static Edge edgeFromId(int id) { return Edge(id);} |
552 | 552 |
|
553 | 553 |
Node addNode() { |
554 | 554 |
int n = nodes.size(); |
555 | 555 |
nodes.push_back(NodeT()); |
556 | 556 |
nodes[n].first_out = -1; |
557 | 557 |
|
558 | 558 |
return Node(n); |
559 | 559 |
} |
560 | 560 |
|
561 | 561 |
Edge addEdge(Node u, Node v) { |
562 | 562 |
int n = arcs.size(); |
563 | 563 |
arcs.push_back(ArcT()); |
564 | 564 |
arcs.push_back(ArcT()); |
565 | 565 |
|
566 | 566 |
arcs[n].target = u._id; |
567 | 567 |
arcs[n | 1].target = v._id; |
568 | 568 |
|
569 | 569 |
arcs[n].next_out = nodes[v._id].first_out; |
570 | 570 |
nodes[v._id].first_out = n; |
571 | 571 |
|
572 | 572 |
arcs[n | 1].next_out = nodes[u._id].first_out; |
573 | 573 |
nodes[u._id].first_out = (n | 1); |
574 | 574 |
|
575 | 575 |
return Edge(n / 2); |
576 | 576 |
} |
577 | 577 |
|
578 | 578 |
void clear() { |
579 | 579 |
arcs.clear(); |
580 | 580 |
nodes.clear(); |
581 | 581 |
} |
582 | 582 |
|
583 | 583 |
}; |
584 | 584 |
|
585 | 585 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
586 | 586 |
|
587 | 587 |
/// \ingroup graphs |
588 | 588 |
/// |
589 | 589 |
/// \brief A smart undirected graph class. |
590 | 590 |
/// |
591 | 591 |
/// This is a simple and fast graph implementation. |
592 | 592 |
/// It is also quite memory efficient, but at the price |
593 | 593 |
/// that <b> it does support only limited (only stack-like) |
594 | 594 |
/// node and arc deletions</b>. |
595 | 595 |
/// Except from this it conforms to |
596 | 596 |
/// the \ref concepts::Graph "Graph concept". |
597 | 597 |
/// |
598 | 598 |
/// It also has an |
599 | 599 |
/// important extra feature that |
600 | 600 |
/// its maps are real \ref concepts::ReferenceMap "reference map"s. |
601 | 601 |
/// |
602 | 602 |
/// \sa concepts::Graph. |
603 | 603 |
/// |
604 | 604 |
class SmartGraph : public ExtendedSmartGraphBase { |
605 | 605 |
private: |
606 | 606 |
|
607 | 607 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
608 | 608 |
|
609 | 609 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
610 | 610 |
/// |
611 | 611 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {}; |
612 | 612 |
|
613 | 613 |
///\brief Assignment of SmartGraph to another one is \e not allowed. |
614 | 614 |
///Use GraphCopy() instead. |
615 | 615 |
|
616 | 616 |
///Assignment of SmartGraph to another one is \e not allowed. |
617 | 617 |
///Use GraphCopy() instead. |
618 | 618 |
void operator=(const SmartGraph &) {} |
619 | 619 |
|
620 | 620 |
public: |
621 | 621 |
|
622 | 622 |
typedef ExtendedSmartGraphBase Parent; |
623 | 623 |
|
624 | 624 |
/// Constructor |
625 | 625 |
|
626 | 626 |
/// Constructor. |
627 | 627 |
/// |
628 | 628 |
SmartGraph() {} |
629 | 629 |
|
630 | 630 |
///Add a new node to the graph. |
631 | 631 |
|
632 | 632 |
/// \return the new node. |
633 | 633 |
/// |
634 | 634 |
Node addNode() { return Parent::addNode(); } |
635 | 635 |
|
636 | 636 |
///Add a new edge to the graph. |
637 | 637 |
|
638 | 638 |
///Add a new edge to the graph with node \c s |
639 | 639 |
///and \c t. |
640 | 640 |
///\return the new edge. |
641 | 641 |
Edge addEdge(const Node& s, const Node& t) { |
642 | 642 |
return Parent::addEdge(s, t); |
643 | 643 |
} |
644 | 644 |
|
645 | 645 |
///Clear the graph. |
646 | 646 |
|
647 | 647 |
///Erase all the nodes and edges from the graph. |
648 | 648 |
/// |
649 | 649 |
void clear() { |
650 | 650 |
Parent::clear(); |
651 | 651 |
} |
652 | 652 |
|
653 | 653 |
public: |
654 | 654 |
|
655 | 655 |
class Snapshot; |
656 | 656 |
|
657 | 657 |
protected: |
658 | 658 |
|
659 | 659 |
void saveSnapshot(Snapshot &s) |
660 | 660 |
{ |
661 | 661 |
s._graph = this; |
662 | 662 |
s.node_num = nodes.size(); |
663 | 663 |
s.arc_num = arcs.size(); |
664 | 664 |
} |
665 | 665 |
|
666 | 666 |
void restoreSnapshot(const Snapshot &s) |
667 | 667 |
{ |
668 | 668 |
while(s.arc_num<arcs.size()) { |
669 | 669 |
int n=arcs.size()-1; |
670 | 670 |
Edge arc=edgeFromId(n/2); |
671 | 671 |
Parent::notifier(Edge()).erase(arc); |
672 | 672 |
std::vector<Arc> dir; |
673 | 673 |
dir.push_back(arcFromId(n)); |
674 | 674 |
dir.push_back(arcFromId(n-1)); |
675 | 675 |
Parent::notifier(Arc()).erase(dir); |
676 | 676 |
nodes[arcs[n].target].first_out=arcs[n].next_out; |
677 | 677 |
nodes[arcs[n-1].target].first_out=arcs[n-1].next_out; |
678 | 678 |
arcs.pop_back(); |
679 | 679 |
arcs.pop_back(); |
680 | 680 |
} |
681 | 681 |
while(s.node_num<nodes.size()) { |
682 | 682 |
int n=nodes.size()-1; |
683 | 683 |
Node node = nodeFromId(n); |
684 | 684 |
Parent::notifier(Node()).erase(node); |
685 | 685 |
nodes.pop_back(); |
686 | 686 |
} |
687 | 687 |
} |
688 | 688 |
|
689 | 689 |
public: |
690 | 690 |
|
691 | 691 |
///Class to make a snapshot of the digraph and to restrore to it later. |
692 | 692 |
|
693 | 693 |
///Class to make a snapshot of the digraph and to restrore to it later. |
694 | 694 |
/// |
695 | 695 |
///The newly added nodes and arcs can be removed using the |
696 | 696 |
///restore() function. |
697 | 697 |
/// |
698 | 698 |
///\note After you restore a state, you cannot restore |
699 | 699 |
///a later state, in other word you cannot add again the arcs deleted |
700 | 700 |
///by restore() using another one Snapshot instance. |
701 | 701 |
/// |
702 | 702 |
///\warning If you do not use correctly the snapshot that can cause |
703 | 703 |
///either broken program, invalid state of the digraph, valid but |
704 | 704 |
///not the restored digraph or no change. Because the runtime performance |
705 | 705 |
///the validity of the snapshot is not stored. |
706 | 706 |
class Snapshot |
707 | 707 |
{ |
708 | 708 |
SmartGraph *_graph; |
709 | 709 |
protected: |
710 | 710 |
friend class SmartGraph; |
711 | 711 |
unsigned int node_num; |
712 | 712 |
unsigned int arc_num; |
713 | 713 |
public: |
714 | 714 |
///Default constructor. |
715 | 715 |
|
716 | 716 |
///Default constructor. |
717 | 717 |
///To actually make a snapshot you must call save(). |
718 | 718 |
/// |
719 | 719 |
Snapshot() : _graph(0) {} |
720 | 720 |
///Constructor that immediately makes a snapshot |
721 | 721 |
|
722 | 722 |
///This constructor immediately makes a snapshot of the digraph. |
723 | 723 |
///\param g The digraph we make a snapshot of. |
724 | 724 |
Snapshot(SmartGraph &graph) { |
725 | 725 |
graph.saveSnapshot(*this); |
726 | 726 |
} |
727 | 727 |
|
728 | 728 |
///Make a snapshot. |
729 | 729 |
|
730 | 730 |
///Make a snapshot of the graph. |
731 | 731 |
/// |
732 | 732 |
///This function can be called more than once. In case of a repeated |
733 | 733 |
///call, the previous snapshot gets lost. |
734 | 734 |
///\param g The digraph we make the snapshot of. |
735 | 735 |
void save(SmartGraph &graph) |
736 | 736 |
{ |
737 | 737 |
graph.saveSnapshot(*this); |
738 | 738 |
} |
739 | 739 |
|
740 | 740 |
///Undo the changes until a snapshot. |
741 | 741 |
|
742 | 742 |
///Undo the changes until a snapshot created by save(). |
743 | 743 |
/// |
744 | 744 |
///\note After you restored a state, you cannot restore |
745 | 745 |
///a later state, in other word you cannot add again the arcs deleted |
746 | 746 |
///by restore(). |
747 | 747 |
void restore() |
748 | 748 |
{ |
749 | 749 |
_graph->restoreSnapshot(*this); |
750 | 750 |
} |
751 | 751 |
}; |
752 | 752 |
}; |
753 | 753 |
|
754 | 754 |
} //namespace lemon |
755 | 755 |
|
756 | 756 |
|
757 | 757 |
#endif //LEMON_SMART_GRAPH_H |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
test/Makefile |
3 | 3 |
|
4 | 4 |
noinst_HEADERS += \ |
5 | 5 |
test/digraph_test.h \ |
6 |
test/graph_utils_test.h \ |
|
6 | 7 |
test/heap_test.h \ |
7 | 8 |
test/map_test.h \ |
8 | 9 |
test/test_tools.h |
9 | 10 |
|
10 | 11 |
check_PROGRAMS += \ |
11 | 12 |
test/bfs_test \ |
12 | 13 |
test/counter_test \ |
13 | 14 |
test/dfs_test \ |
14 | 15 |
test/digraph_test \ |
15 | 16 |
test/dim_test \ |
16 | 17 |
test/error_test \ |
17 | 18 |
test/graph_test \ |
19 |
test/graph_utils_test \ |
|
18 | 20 |
test/kruskal_test \ |
19 | 21 |
test/maps_test \ |
20 | 22 |
test/random_test \ |
21 | 23 |
test/path_test \ |
22 | 24 |
test/test_tools_fail \ |
23 | 25 |
test/test_tools_pass \ |
24 | 26 |
test/time_measure_test \ |
25 | 27 |
test/unionfind_test |
26 | 28 |
|
27 | 29 |
TESTS += $(check_PROGRAMS) |
28 | 30 |
XFAIL_TESTS += test/test_tools_fail$(EXEEXT) |
29 | 31 |
|
30 | 32 |
test_bfs_test_SOURCES = test/bfs_test.cc |
31 | 33 |
test_counter_test_SOURCES = test/counter_test.cc |
32 | 34 |
test_dfs_test_SOURCES = test/dfs_test.cc |
33 | 35 |
test_digraph_test_SOURCES = test/digraph_test.cc |
34 | 36 |
test_dim_test_SOURCES = test/dim_test.cc |
35 | 37 |
test_error_test_SOURCES = test/error_test.cc |
36 | 38 |
test_graph_test_SOURCES = test/graph_test.cc |
39 |
test_graph_utils_test_SOURCES = test/graph_utils_test.cc |
|
37 | 40 |
# test_heap_test_SOURCES = test/heap_test.cc |
38 | 41 |
test_kruskal_test_SOURCES = test/kruskal_test.cc |
39 | 42 |
test_maps_test_SOURCES = test/maps_test.cc |
40 | 43 |
test_path_test_SOURCES = test/path_test.cc |
41 | 44 |
test_random_test_SOURCES = test/random_test.cc |
42 | 45 |
test_test_tools_fail_SOURCES = test/test_tools_fail.cc |
43 | 46 |
test_test_tools_pass_SOURCES = test/test_tools_pass.cc |
44 | 47 |
test_time_measure_test_SOURCES = test/time_measure_test.cc |
45 | 48 |
test_unionfind_test_SOURCES = test/unionfind_test.cc |
0 comments (0 inline)