0
18
0
2
7
325
309
2
7
1
11
... | ... |
@@ -9,107 +9,105 @@ |
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_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief BFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \ref PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 |
///\todo The digraph alone may be insufficient to initialize |
|
58 | 57 |
static PredMap *createPredMap(const Digraph &g) |
59 | 58 |
{ |
60 | 59 |
return new PredMap(g); |
61 | 60 |
} |
62 | 61 |
|
63 | 62 |
///The type of the map that indicates which nodes are processed. |
64 | 63 |
|
65 | 64 |
///The type of the map that indicates which nodes are processed. |
66 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
67 |
///By default it is a NullMap. |
|
68 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
69 | 67 |
///Instantiates a \ref ProcessedMap. |
70 | 68 |
|
71 | 69 |
///This function instantiates a \ref ProcessedMap. |
72 | 70 |
///\param g is the digraph, to which |
73 | 71 |
///we would like to define the \ref ProcessedMap |
74 | 72 |
#ifdef DOXYGEN |
75 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
76 | 74 |
#else |
77 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
78 | 76 |
#endif |
79 | 77 |
{ |
80 | 78 |
return new ProcessedMap(); |
81 | 79 |
} |
82 | 80 |
|
83 | 81 |
///The type of the map that indicates which nodes are reached. |
84 | 82 |
|
85 | 83 |
///The type of the map that indicates which nodes are reached. |
86 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
87 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
88 | 86 |
///Instantiates a \ref ReachedMap. |
89 | 87 |
|
90 | 88 |
///This function instantiates a \ref ReachedMap. |
91 | 89 |
///\param g is the digraph, to which |
92 | 90 |
///we would like to define the \ref ReachedMap. |
93 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
94 | 92 |
{ |
95 | 93 |
return new ReachedMap(g); |
96 | 94 |
} |
97 | 95 |
|
98 | 96 |
///The type of the map that stores the distances of the nodes. |
99 | 97 |
|
100 | 98 |
///The type of the map that stores the distances of the nodes. |
101 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
102 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
103 | 101 |
///Instantiates a \ref DistMap. |
104 | 102 |
|
105 | 103 |
///This function instantiates a \ref DistMap. |
106 | 104 |
///\param g is the digraph, to which we would like to define the |
107 | 105 |
///\ref DistMap. |
108 | 106 |
static DistMap *createDistMap(const Digraph &g) |
109 | 107 |
{ |
110 | 108 |
return new DistMap(g); |
111 | 109 |
} |
112 | 110 |
}; |
113 | 111 |
|
114 | 112 |
///%BFS algorithm class. |
115 | 113 |
|
... | ... |
@@ -151,98 +149,97 @@ |
151 | 149 |
///The type of the digraph the algorithm runs on. |
152 | 150 |
typedef typename TR::Digraph Digraph; |
153 | 151 |
|
154 | 152 |
///\brief The type of the map that stores the predecessor arcs of the |
155 | 153 |
///shortest paths. |
156 | 154 |
typedef typename TR::PredMap PredMap; |
157 | 155 |
///The type of the map that stores the distances of the nodes. |
158 | 156 |
typedef typename TR::DistMap DistMap; |
159 | 157 |
///The type of the map that indicates which nodes are reached. |
160 | 158 |
typedef typename TR::ReachedMap ReachedMap; |
161 | 159 |
///The type of the map that indicates which nodes are processed. |
162 | 160 |
typedef typename TR::ProcessedMap ProcessedMap; |
163 | 161 |
///The type of the paths. |
164 | 162 |
typedef PredMapPath<Digraph, PredMap> Path; |
165 | 163 |
|
166 | 164 |
///The traits class. |
167 | 165 |
typedef TR Traits; |
168 | 166 |
|
169 | 167 |
private: |
170 | 168 |
|
171 | 169 |
typedef typename Digraph::Node Node; |
172 | 170 |
typedef typename Digraph::NodeIt NodeIt; |
173 | 171 |
typedef typename Digraph::Arc Arc; |
174 | 172 |
typedef typename Digraph::OutArcIt OutArcIt; |
175 | 173 |
|
176 | 174 |
//Pointer to the underlying digraph. |
177 | 175 |
const Digraph *G; |
178 | 176 |
//Pointer to the map of predecessor arcs. |
179 | 177 |
PredMap *_pred; |
180 | 178 |
//Indicates if _pred is locally allocated (true) or not. |
181 | 179 |
bool local_pred; |
182 | 180 |
//Pointer to the map of distances. |
183 | 181 |
DistMap *_dist; |
184 | 182 |
//Indicates if _dist is locally allocated (true) or not. |
185 | 183 |
bool local_dist; |
186 | 184 |
//Pointer to the map of reached status of the nodes. |
187 | 185 |
ReachedMap *_reached; |
188 | 186 |
//Indicates if _reached is locally allocated (true) or not. |
189 | 187 |
bool local_reached; |
190 | 188 |
//Pointer to the map of processed status of the nodes. |
191 | 189 |
ProcessedMap *_processed; |
192 | 190 |
//Indicates if _processed is locally allocated (true) or not. |
193 | 191 |
bool local_processed; |
194 | 192 |
|
195 | 193 |
std::vector<typename Digraph::Node> _queue; |
196 | 194 |
int _queue_head,_queue_tail,_queue_next_dist; |
197 | 195 |
int _curr_dist; |
198 | 196 |
|
199 |
///Creates the maps if necessary. |
|
200 |
///\todo Better memory allocation (instead of new). |
|
197 |
//Creates the maps if necessary. |
|
201 | 198 |
void create_maps() |
202 | 199 |
{ |
203 | 200 |
if(!_pred) { |
204 | 201 |
local_pred = true; |
205 | 202 |
_pred = Traits::createPredMap(*G); |
206 | 203 |
} |
207 | 204 |
if(!_dist) { |
208 | 205 |
local_dist = true; |
209 | 206 |
_dist = Traits::createDistMap(*G); |
210 | 207 |
} |
211 | 208 |
if(!_reached) { |
212 | 209 |
local_reached = true; |
213 | 210 |
_reached = Traits::createReachedMap(*G); |
214 | 211 |
} |
215 | 212 |
if(!_processed) { |
216 | 213 |
local_processed = true; |
217 | 214 |
_processed = Traits::createProcessedMap(*G); |
218 | 215 |
} |
219 | 216 |
} |
220 | 217 |
|
221 | 218 |
protected: |
222 | 219 |
|
223 | 220 |
Bfs() {} |
224 | 221 |
|
225 | 222 |
public: |
226 | 223 |
|
227 | 224 |
typedef Bfs Create; |
228 | 225 |
|
229 | 226 |
///\name Named template parameters |
230 | 227 |
|
231 | 228 |
///@{ |
232 | 229 |
|
233 | 230 |
template <class T> |
234 | 231 |
struct SetPredMapTraits : public Traits { |
235 | 232 |
typedef T PredMap; |
236 | 233 |
static PredMap *createPredMap(const Digraph &) |
237 | 234 |
{ |
238 | 235 |
throw UninitializedParameter(); |
239 | 236 |
} |
240 | 237 |
}; |
241 | 238 |
///\brief \ref named-templ-param "Named parameter" for setting |
242 | 239 |
///\ref PredMap type. |
243 | 240 |
/// |
244 | 241 |
///\ref named-templ-param "Named parameter" for setting |
245 | 242 |
///\ref PredMap type. |
246 | 243 |
template <class T> |
247 | 244 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
248 | 245 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
... | ... |
@@ -803,97 +800,96 @@ |
803 | 800 |
///of the nodes calculated by the algorithm. |
804 | 801 |
/// |
805 | 802 |
///\pre Either \ref run() or \ref init() |
806 | 803 |
///must be called before using this function. |
807 | 804 |
const DistMap &distMap() const { return *_dist;} |
808 | 805 |
|
809 | 806 |
///\brief Returns a const reference to the node map that stores the |
810 | 807 |
///predecessor arcs. |
811 | 808 |
/// |
812 | 809 |
///Returns a const reference to the node map that stores the predecessor |
813 | 810 |
///arcs, which form the shortest path tree. |
814 | 811 |
/// |
815 | 812 |
///\pre Either \ref run() or \ref init() |
816 | 813 |
///must be called before using this function. |
817 | 814 |
const PredMap &predMap() const { return *_pred;} |
818 | 815 |
|
819 | 816 |
///Checks if a node is reachable from the root(s). |
820 | 817 |
|
821 | 818 |
///Returns \c true if \c v is reachable from the root(s). |
822 | 819 |
///\pre Either \ref run() or \ref start() |
823 | 820 |
///must be called before using this function. |
824 | 821 |
bool reached(Node v) const { return (*_reached)[v]; } |
825 | 822 |
|
826 | 823 |
///@} |
827 | 824 |
}; |
828 | 825 |
|
829 | 826 |
///Default traits class of bfs() function. |
830 | 827 |
|
831 | 828 |
///Default traits class of bfs() function. |
832 | 829 |
///\tparam GR Digraph type. |
833 | 830 |
template<class GR> |
834 | 831 |
struct BfsWizardDefaultTraits |
835 | 832 |
{ |
836 | 833 |
///The type of the digraph the algorithm runs on. |
837 | 834 |
typedef GR Digraph; |
838 | 835 |
|
839 | 836 |
///\brief The type of the map that stores the predecessor |
840 | 837 |
///arcs of the shortest paths. |
841 | 838 |
/// |
842 | 839 |
///The type of the map that stores the predecessor |
843 | 840 |
///arcs of the shortest paths. |
844 | 841 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
845 | 842 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
846 | 843 |
///Instantiates a \ref PredMap. |
847 | 844 |
|
848 | 845 |
///This function instantiates a \ref PredMap. |
849 | 846 |
///\param g is the digraph, to which we would like to define the |
850 | 847 |
///\ref PredMap. |
851 |
///\todo The digraph alone may be insufficient to initialize |
|
852 | 848 |
static PredMap *createPredMap(const Digraph &g) |
853 | 849 |
{ |
854 | 850 |
return new PredMap(g); |
855 | 851 |
} |
856 | 852 |
|
857 | 853 |
///The type of the map that indicates which nodes are processed. |
858 | 854 |
|
859 | 855 |
///The type of the map that indicates which nodes are processed. |
860 | 856 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
861 | 857 |
///By default it is a NullMap. |
862 | 858 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
863 | 859 |
///Instantiates a \ref ProcessedMap. |
864 | 860 |
|
865 | 861 |
///This function instantiates a \ref ProcessedMap. |
866 | 862 |
///\param g is the digraph, to which |
867 | 863 |
///we would like to define the \ref ProcessedMap. |
868 | 864 |
#ifdef DOXYGEN |
869 | 865 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
870 | 866 |
#else |
871 | 867 |
static ProcessedMap *createProcessedMap(const Digraph &) |
872 | 868 |
#endif |
873 | 869 |
{ |
874 | 870 |
return new ProcessedMap(); |
875 | 871 |
} |
876 | 872 |
|
877 | 873 |
///The type of the map that indicates which nodes are reached. |
878 | 874 |
|
879 | 875 |
///The type of the map that indicates which nodes are reached. |
880 | 876 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
881 | 877 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
882 | 878 |
///Instantiates a \ref ReachedMap. |
883 | 879 |
|
884 | 880 |
///This function instantiates a \ref ReachedMap. |
885 | 881 |
///\param g is the digraph, to which |
886 | 882 |
///we would like to define the \ref ReachedMap. |
887 | 883 |
static ReachedMap *createReachedMap(const Digraph &g) |
888 | 884 |
{ |
889 | 885 |
return new ReachedMap(g); |
890 | 886 |
} |
891 | 887 |
|
892 | 888 |
///The type of the map that stores the distances of the nodes. |
893 | 889 |
|
894 | 890 |
///The type of the map that stores the distances of the nodes. |
895 | 891 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
896 | 892 |
typedef typename Digraph::template NodeMap<int> DistMap; |
897 | 893 |
///Instantiates a \ref DistMap. |
898 | 894 |
|
899 | 895 |
///This function instantiates a \ref DistMap. |
... | ... |
@@ -1325,98 +1321,97 @@ |
1325 | 1321 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
1326 | 1322 |
#endif |
1327 | 1323 |
class BfsVisit { |
1328 | 1324 |
public: |
1329 | 1325 |
|
1330 | 1326 |
/// \brief \ref Exception for uninitialized parameters. |
1331 | 1327 |
/// |
1332 | 1328 |
/// This error represents problems in the initialization |
1333 | 1329 |
/// of the parameters of the algorithm. |
1334 | 1330 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1335 | 1331 |
public: |
1336 | 1332 |
virtual const char* what() const throw() |
1337 | 1333 |
{ |
1338 | 1334 |
return "lemon::BfsVisit::UninitializedParameter"; |
1339 | 1335 |
} |
1340 | 1336 |
}; |
1341 | 1337 |
|
1342 | 1338 |
///The traits class. |
1343 | 1339 |
typedef _Traits Traits; |
1344 | 1340 |
|
1345 | 1341 |
///The type of the digraph the algorithm runs on. |
1346 | 1342 |
typedef typename Traits::Digraph Digraph; |
1347 | 1343 |
|
1348 | 1344 |
///The visitor type used by the algorithm. |
1349 | 1345 |
typedef _Visitor Visitor; |
1350 | 1346 |
|
1351 | 1347 |
///The type of the map that indicates which nodes are reached. |
1352 | 1348 |
typedef typename Traits::ReachedMap ReachedMap; |
1353 | 1349 |
|
1354 | 1350 |
private: |
1355 | 1351 |
|
1356 | 1352 |
typedef typename Digraph::Node Node; |
1357 | 1353 |
typedef typename Digraph::NodeIt NodeIt; |
1358 | 1354 |
typedef typename Digraph::Arc Arc; |
1359 | 1355 |
typedef typename Digraph::OutArcIt OutArcIt; |
1360 | 1356 |
|
1361 | 1357 |
//Pointer to the underlying digraph. |
1362 | 1358 |
const Digraph *_digraph; |
1363 | 1359 |
//Pointer to the visitor object. |
1364 | 1360 |
Visitor *_visitor; |
1365 | 1361 |
//Pointer to the map of reached status of the nodes. |
1366 | 1362 |
ReachedMap *_reached; |
1367 | 1363 |
//Indicates if _reached is locally allocated (true) or not. |
1368 | 1364 |
bool local_reached; |
1369 | 1365 |
|
1370 | 1366 |
std::vector<typename Digraph::Node> _list; |
1371 | 1367 |
int _list_front, _list_back; |
1372 | 1368 |
|
1373 |
///Creates the maps if necessary. |
|
1374 |
///\todo Better memory allocation (instead of new). |
|
1369 |
//Creates the maps if necessary. |
|
1375 | 1370 |
void create_maps() { |
1376 | 1371 |
if(!_reached) { |
1377 | 1372 |
local_reached = true; |
1378 | 1373 |
_reached = Traits::createReachedMap(*_digraph); |
1379 | 1374 |
} |
1380 | 1375 |
} |
1381 | 1376 |
|
1382 | 1377 |
protected: |
1383 | 1378 |
|
1384 | 1379 |
BfsVisit() {} |
1385 | 1380 |
|
1386 | 1381 |
public: |
1387 | 1382 |
|
1388 | 1383 |
typedef BfsVisit Create; |
1389 | 1384 |
|
1390 | 1385 |
/// \name Named template parameters |
1391 | 1386 |
|
1392 | 1387 |
///@{ |
1393 | 1388 |
template <class T> |
1394 | 1389 |
struct SetReachedMapTraits : public Traits { |
1395 | 1390 |
typedef T ReachedMap; |
1396 | 1391 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1397 | 1392 |
throw UninitializedParameter(); |
1398 | 1393 |
} |
1399 | 1394 |
}; |
1400 | 1395 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1401 | 1396 |
/// ReachedMap type. |
1402 | 1397 |
/// |
1403 | 1398 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1404 | 1399 |
template <class T> |
1405 | 1400 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1406 | 1401 |
SetReachedMapTraits<T> > { |
1407 | 1402 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1408 | 1403 |
}; |
1409 | 1404 |
///@} |
1410 | 1405 |
|
1411 | 1406 |
public: |
1412 | 1407 |
|
1413 | 1408 |
/// \brief Constructor. |
1414 | 1409 |
/// |
1415 | 1410 |
/// Constructor. |
1416 | 1411 |
/// |
1417 | 1412 |
/// \param digraph The digraph the algorithm runs on. |
1418 | 1413 |
/// \param visitor The visitor object of the algorithm. |
1419 | 1414 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1420 | 1415 |
: _digraph(&digraph), _visitor(&visitor), |
1421 | 1416 |
_reached(0), local_reached(false) {} |
1422 | 1417 |
... | ... |
@@ -60,99 +60,96 @@ |
60 | 60 |
Arc() {} |
61 | 61 |
|
62 | 62 |
// Invalid arc constructor |
63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
64 | 64 |
|
65 | 65 |
bool operator==(const Arc &that) const { |
66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
67 | 67 |
} |
68 | 68 |
bool operator!=(const Arc &that) const { |
69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
70 | 70 |
} |
71 | 71 |
bool operator<(const Arc &that) const { |
72 | 72 |
return forward<that.forward || |
73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
/// First node of the edge |
78 | 78 |
Node u(const Edge &e) const { |
79 | 79 |
return Parent::source(e); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
/// Source of the given arc |
83 | 83 |
Node source(const Arc &e) const { |
84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
/// Second node of the edge |
88 | 88 |
Node v(const Edge &e) const { |
89 | 89 |
return Parent::target(e); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
/// Target of the given arc |
93 | 93 |
Node target(const Arc &e) const { |
94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
/// \brief Directed arc from an edge. |
98 | 98 |
/// |
99 | 99 |
/// Returns a directed arc corresponding to the specified edge. |
100 | 100 |
/// If the given bool is true, the first node of the given edge and |
101 | 101 |
/// the source node of the returned arc are the same. |
102 | 102 |
static Arc direct(const Edge &e, bool d) { |
103 | 103 |
return Arc(e, d); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
/// Returns whether the given directed arc has the same orientation |
107 | 107 |
/// as the corresponding edge. |
108 |
/// |
|
109 |
/// \todo reference to the corresponding point of the undirected digraph |
|
110 |
/// concept. "What does the direction of an edge mean?" |
|
111 | 108 |
static bool direction(const Arc &a) { return a.forward; } |
112 | 109 |
|
113 | 110 |
using Parent::first; |
114 | 111 |
using Parent::next; |
115 | 112 |
|
116 | 113 |
void first(Arc &e) const { |
117 | 114 |
Parent::first(e); |
118 | 115 |
e.forward=true; |
119 | 116 |
} |
120 | 117 |
|
121 | 118 |
void next(Arc &e) const { |
122 | 119 |
if( e.forward ) { |
123 | 120 |
e.forward = false; |
124 | 121 |
} |
125 | 122 |
else { |
126 | 123 |
Parent::next(e); |
127 | 124 |
e.forward = true; |
128 | 125 |
} |
129 | 126 |
} |
130 | 127 |
|
131 | 128 |
void firstOut(Arc &e, const Node &n) const { |
132 | 129 |
Parent::firstIn(e,n); |
133 | 130 |
if( Edge(e) != INVALID ) { |
134 | 131 |
e.forward = false; |
135 | 132 |
} |
136 | 133 |
else { |
137 | 134 |
Parent::firstOut(e,n); |
138 | 135 |
e.forward = true; |
139 | 136 |
} |
140 | 137 |
} |
141 | 138 |
void nextOut(Arc &e) const { |
142 | 139 |
if( ! e.forward ) { |
143 | 140 |
Node n = Parent::target(e); |
144 | 141 |
Parent::nextIn(e); |
145 | 142 |
if( Edge(e) == INVALID ) { |
146 | 143 |
Parent::firstOut(e, n); |
147 | 144 |
e.forward = true; |
148 | 145 |
} |
149 | 146 |
} |
150 | 147 |
else { |
151 | 148 |
Parent::nextOut(e); |
152 | 149 |
} |
153 | 150 |
} |
154 | 151 |
|
155 | 152 |
void firstIn(Arc &e, const Node &n) const { |
156 | 153 |
Parent::firstOut(e,n); |
157 | 154 |
if( Edge(e) != INVALID ) { |
158 | 155 |
e.forward = false; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_VECTOR_MAP_H |
20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/core.h> |
26 | 26 |
#include <lemon/bits/alteration_notifier.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
///\ingroup graphbits |
32 | 32 |
/// |
33 | 33 |
///\file |
34 | 34 |
///\brief Vector based graph maps. |
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \ingroup graphbits |
38 | 38 |
/// |
39 | 39 |
/// \brief Graph map based on the std::vector storage. |
40 | 40 |
/// |
41 | 41 |
/// The VectorMap template class is graph map structure what |
42 | 42 |
/// automatically updates the map when a key is added to or erased from |
43 | 43 |
/// the map. This map type uses the std::vector to store the values. |
44 | 44 |
/// |
45 |
/// \tparam |
|
45 |
/// \tparam _Graph The graph this map is attached to. |
|
46 | 46 |
/// \tparam _Item The item type of the graph items. |
47 | 47 |
/// \tparam _Value The value type of the map. |
48 |
/// \todo Fix the doc: there is _Graph parameter instead of _Notifier. |
|
49 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
50 | 49 |
class VectorMap |
51 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
52 | 51 |
private: |
53 | 52 |
|
54 | 53 |
/// The container type of the map. |
55 | 54 |
typedef std::vector<_Value> Container; |
56 | 55 |
|
57 | 56 |
public: |
58 | 57 |
|
59 | 58 |
/// The graph type of the map. |
60 | 59 |
typedef _Graph Graph; |
61 | 60 |
/// The item type of the map. |
62 | 61 |
typedef _Item Item; |
63 | 62 |
/// The reference map tag. |
64 | 63 |
typedef True ReferenceMapTag; |
65 | 64 |
|
66 | 65 |
/// The key type of the map. |
67 | 66 |
typedef _Item Key; |
68 | 67 |
/// The value type of the map. |
69 | 68 |
typedef _Value Value; |
70 | 69 |
|
71 | 70 |
/// The notifier type. |
72 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
73 | 72 |
|
74 | 73 |
/// The map type. |
75 | 74 |
typedef VectorMap Map; |
76 | 75 |
/// The base class of the map. |
77 | 76 |
typedef typename Notifier::ObserverBase Parent; |
78 | 77 |
|
79 | 78 |
/// The reference type of the map; |
80 | 79 |
typedef typename Container::reference Reference; |
81 | 80 |
/// The const reference type of the map; |
82 | 81 |
typedef typename Container::const_reference ConstReference; |
83 | 82 |
|
84 | 83 |
|
85 | 84 |
/// \brief Constructor to attach the new map into the notifier. |
86 | 85 |
/// |
87 | 86 |
/// It constructs a map and attachs it into the notifier. |
88 | 87 |
/// It adds all the items of the graph to the map. |
89 | 88 |
VectorMap(const Graph& graph) { |
90 | 89 |
Parent::attach(graph.notifier(Item())); |
91 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
92 | 91 |
} |
93 | 92 |
|
94 | 93 |
/// \brief Constructor uses given value to initialize the map. |
95 | 94 |
/// |
96 | 95 |
/// It constructs a map uses a given value to initialize the map. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 |
// This file contains a modified version of the concept checking |
|
20 |
// utility from BOOST. |
|
21 |
// See the appropriate copyright notice below. |
|
22 |
|
|
23 |
// (C) Copyright Jeremy Siek 2000. |
|
24 |
// Distributed under the Boost Software License, Version 1.0. (See |
|
25 |
// accompanying file LICENSE_1_0.txt or copy at |
|
26 |
// http://www.boost.org/LICENSE_1_0.txt) |
|
27 |
// |
|
28 |
// Revision History: |
|
29 |
// 05 May 2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek) |
|
30 |
// 02 April 2001: Removed limits header altogether. (Jeremy Siek) |
|
31 |
// 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock) |
|
32 |
// |
|
33 |
|
|
34 |
// See http://www.boost.org/libs/concept_check for documentation. |
|
19 |
// The contents of this file was inspired by the concept checking |
|
20 |
// utility of the BOOST library (http://www.boost.org). |
|
35 | 21 |
|
36 | 22 |
///\file |
37 | 23 |
///\brief Basic utilities for concept checking. |
38 | 24 |
/// |
39 |
///\todo Are we still using BOOST concept checking utility? |
|
40 |
///Is the BOOST copyright notice necessary? |
|
41 | 25 |
|
42 | 26 |
#ifndef LEMON_CONCEPT_CHECK_H |
43 | 27 |
#define LEMON_CONCEPT_CHECK_H |
44 | 28 |
|
45 | 29 |
namespace lemon { |
46 | 30 |
|
47 | 31 |
/* |
48 | 32 |
"inline" is used for ignore_unused_variable_warning() |
49 | 33 |
and function_requires() to make sure there is no |
50 | 34 |
overtarget with g++. |
51 | 35 |
*/ |
52 | 36 |
|
53 | 37 |
template <class T> inline void ignore_unused_variable_warning(const T&) { } |
54 | 38 |
|
55 | 39 |
///\e |
56 | 40 |
template <class Concept> |
57 | 41 |
inline void function_requires() |
58 | 42 |
{ |
59 | 43 |
#if !defined(NDEBUG) |
60 | 44 |
void (Concept::*x)() = & Concept::constraints; |
61 | 45 |
ignore_unused_variable_warning(x); |
62 | 46 |
#endif |
63 | 47 |
} |
64 | 48 |
|
65 | 49 |
///\e |
66 | 50 |
template <typename Concept, typename Type> |
67 | 51 |
inline void checkConcept() { |
68 | 52 |
#if !defined(NDEBUG) |
69 | 53 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
70 | 54 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
71 | 55 |
ignore_unused_variable_warning(x); |
72 | 56 |
#endif |
73 | 57 |
} |
74 | 58 |
|
75 | 59 |
} // namespace lemon |
76 | 60 |
|
77 | 61 |
#endif // LEMON_CONCEPT_CHECK_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 |
///\todo Iterators have obsolete style |
|
24 | 23 |
|
25 | 24 |
#ifndef LEMON_CONCEPT_PATH_H |
26 | 25 |
#define LEMON_CONCEPT_PATH_H |
27 | 26 |
|
28 | 27 |
#include <lemon/core.h> |
29 | 28 |
#include <lemon/concept_check.h> |
30 | 29 |
|
31 | 30 |
namespace lemon { |
32 | 31 |
namespace concepts { |
33 | 32 |
|
34 | 33 |
/// \addtogroup concept |
35 | 34 |
/// @{ |
36 | 35 |
|
37 | 36 |
/// \brief A skeleton structure for representing directed paths in |
38 | 37 |
/// a digraph. |
39 | 38 |
/// |
40 | 39 |
/// A skeleton structure for representing directed paths in a |
41 | 40 |
/// digraph. |
42 | 41 |
/// \tparam _Digraph The digraph type in which the path is. |
43 | 42 |
/// |
44 | 43 |
/// In a sense, the path can be treated as a list of arcs. The |
45 | 44 |
/// lemon path type stores just this list. As a consequence it |
46 | 45 |
/// cannot enumerate the nodes in the path and the zero length |
47 | 46 |
/// paths cannot store the source. |
48 | 47 |
/// |
49 | 48 |
template <typename _Digraph> |
50 | 49 |
class Path { |
51 | 50 |
public: |
52 | 51 |
|
53 | 52 |
/// Type of the underlying digraph. |
54 | 53 |
typedef _Digraph Digraph; |
55 | 54 |
/// Arc type of the underlying digraph. |
56 | 55 |
typedef typename Digraph::Arc Arc; |
57 | 56 |
|
58 | 57 |
class ArcIt; |
59 | 58 |
|
60 | 59 |
/// \brief Default constructor |
61 | 60 |
Path() {} |
62 | 61 |
|
63 | 62 |
/// \brief Template constructor |
64 | 63 |
template <typename CPath> |
65 | 64 |
Path(const CPath& cpath) {} |
66 | 65 |
|
67 | 66 |
/// \brief Template assigment |
68 | 67 |
template <typename CPath> |
69 | 68 |
Path& operator=(const CPath& cpath) { |
70 | 69 |
ignore_unused_variable_warning(cpath); |
71 | 70 |
return *this; |
... | ... |
@@ -13,1221 +13,1244 @@ |
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_CORE_H |
20 | 20 |
#define LEMON_CORE_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/enable_if.h> |
26 | 26 |
#include <lemon/bits/traits.h> |
27 | 27 |
|
28 | 28 |
///\file |
29 | 29 |
///\brief LEMON core utilities. |
30 | 30 |
/// |
31 | 31 |
///This header file contains core utilities for LEMON. |
32 | 32 |
///It is automatically included by all graph types, therefore it usually |
33 | 33 |
///do not have to be included directly. |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Dummy type to make it easier to create invalid iterators. |
38 | 38 |
/// |
39 | 39 |
/// Dummy type to make it easier to create invalid iterators. |
40 | 40 |
/// See \ref INVALID for the usage. |
41 | 41 |
struct Invalid { |
42 | 42 |
public: |
43 | 43 |
bool operator==(Invalid) { return true; } |
44 | 44 |
bool operator!=(Invalid) { return false; } |
45 | 45 |
bool operator< (Invalid) { return false; } |
46 | 46 |
}; |
47 | 47 |
|
48 | 48 |
/// \brief Invalid iterators. |
49 | 49 |
/// |
50 | 50 |
/// \ref Invalid is a global type that converts to each iterator |
51 | 51 |
/// in such a way that the value of the target iterator will be invalid. |
52 | 52 |
#ifdef LEMON_ONLY_TEMPLATES |
53 | 53 |
const Invalid INVALID = Invalid(); |
54 | 54 |
#else |
55 | 55 |
extern const Invalid INVALID; |
56 | 56 |
#endif |
57 | 57 |
|
58 | 58 |
/// \addtogroup gutils |
59 | 59 |
/// @{ |
60 | 60 |
|
61 |
/// |
|
61 |
///Create convenient typedefs for the digraph types and iterators |
|
62 | 62 |
|
63 |
///This \c \#define creates convenience typedefs for the following types |
|
64 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
|
63 |
///This \c \#define creates convenient type definitions for the following |
|
64 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
|
65 | 65 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
66 | 66 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
67 | 67 |
/// |
68 | 68 |
///\note If the graph type is a dependent type, ie. the graph type depend |
69 | 69 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
70 | 70 |
///macro. |
71 | 71 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
72 | 72 |
typedef Digraph::Node Node; \ |
73 | 73 |
typedef Digraph::NodeIt NodeIt; \ |
74 | 74 |
typedef Digraph::Arc Arc; \ |
75 | 75 |
typedef Digraph::ArcIt ArcIt; \ |
76 | 76 |
typedef Digraph::InArcIt InArcIt; \ |
77 | 77 |
typedef Digraph::OutArcIt OutArcIt; \ |
78 | 78 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
79 | 79 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
80 | 80 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
81 | 81 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
82 | 82 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
83 |
typedef Digraph::ArcMap<double> DoubleArcMap |
|
83 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
|
84 | 84 |
|
85 |
/// |
|
85 |
///Create convenient typedefs for the digraph types and iterators |
|
86 | 86 |
|
87 | 87 |
///\see DIGRAPH_TYPEDEFS |
88 | 88 |
/// |
89 | 89 |
///\note Use this macro, if the graph type is a dependent type, |
90 | 90 |
///ie. the graph type depend on a template parameter. |
91 | 91 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
92 | 92 |
typedef typename Digraph::Node Node; \ |
93 | 93 |
typedef typename Digraph::NodeIt NodeIt; \ |
94 | 94 |
typedef typename Digraph::Arc Arc; \ |
95 | 95 |
typedef typename Digraph::ArcIt ArcIt; \ |
96 | 96 |
typedef typename Digraph::InArcIt InArcIt; \ |
97 | 97 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
98 | 98 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
99 | 99 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
100 | 100 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
101 | 101 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
102 | 102 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
103 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
|
103 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap; |
|
104 | 104 |
|
105 |
/// |
|
105 |
///Create convenient typedefs for the graph types and iterators |
|
106 | 106 |
|
107 |
///This \c \#define creates the same |
|
107 |
///This \c \#define creates the same convenient type definitions as defined |
|
108 | 108 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
109 | 109 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
110 | 110 |
///\c DoubleEdgeMap. |
111 | 111 |
/// |
112 | 112 |
///\note If the graph type is a dependent type, ie. the graph type depend |
113 |
///on a template parameter, then use \c |
|
113 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
|
114 | 114 |
///macro. |
115 | 115 |
#define GRAPH_TYPEDEFS(Graph) \ |
116 | 116 |
DIGRAPH_TYPEDEFS(Graph); \ |
117 | 117 |
typedef Graph::Edge Edge; \ |
118 | 118 |
typedef Graph::EdgeIt EdgeIt; \ |
119 | 119 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
120 | 120 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
121 | 121 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
122 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
|
122 |
typedef Graph::EdgeMap<double> DoubleEdgeMap; |
|
123 | 123 |
|
124 |
/// |
|
124 |
///Create convenient typedefs for the graph types and iterators |
|
125 | 125 |
|
126 | 126 |
///\see GRAPH_TYPEDEFS |
127 | 127 |
/// |
128 | 128 |
///\note Use this macro, if the graph type is a dependent type, |
129 | 129 |
///ie. the graph type depend on a template parameter. |
130 | 130 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
131 | 131 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
132 | 132 |
typedef typename Graph::Edge Edge; \ |
133 | 133 |
typedef typename Graph::EdgeIt EdgeIt; \ |
134 | 134 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
135 | 135 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
136 | 136 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
137 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
|
137 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap; |
|
138 | 138 |
|
139 |
/// \brief Function to count the items in |
|
139 |
/// \brief Function to count the items in a graph. |
|
140 | 140 |
/// |
141 |
/// This function counts the items (nodes, arcs etc) in the graph. |
|
142 |
/// The complexity of the function is O(n) because |
|
141 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
|
142 |
/// The complexity of the function is linear because |
|
143 | 143 |
/// it iterates on all of the items. |
144 | 144 |
template <typename Graph, typename Item> |
145 | 145 |
inline int countItems(const Graph& g) { |
146 | 146 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
147 | 147 |
int num = 0; |
148 | 148 |
for (ItemIt it(g); it != INVALID; ++it) { |
149 | 149 |
++num; |
150 | 150 |
} |
151 | 151 |
return num; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
// Node counting: |
155 | 155 |
|
156 | 156 |
namespace _core_bits { |
157 | 157 |
|
158 | 158 |
template <typename Graph, typename Enable = void> |
159 | 159 |
struct CountNodesSelector { |
160 | 160 |
static int count(const Graph &g) { |
161 | 161 |
return countItems<Graph, typename Graph::Node>(g); |
162 | 162 |
} |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
template <typename Graph> |
166 | 166 |
struct CountNodesSelector< |
167 | 167 |
Graph, typename |
168 | 168 |
enable_if<typename Graph::NodeNumTag, void>::type> |
169 | 169 |
{ |
170 | 170 |
static int count(const Graph &g) { |
171 | 171 |
return g.nodeNum(); |
172 | 172 |
} |
173 | 173 |
}; |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
/// \brief Function to count the nodes in the graph. |
177 | 177 |
/// |
178 | 178 |
/// This function counts the nodes in the graph. |
179 |
/// The complexity of the function is O(n) but for some |
|
180 |
/// graph structures it is specialized to run in O(1). |
|
179 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
|
180 |
/// graph structures it is specialized to run in <em>O</em>(1). |
|
181 | 181 |
/// |
182 |
/// If the graph contains a \e nodeNum() member function and a |
|
183 |
/// \e NodeNumTag tag then this function calls directly the member |
|
182 |
/// \note If the graph contains a \c nodeNum() member function and a |
|
183 |
/// \c NodeNumTag tag then this function calls directly the member |
|
184 | 184 |
/// function to query the cardinality of the node set. |
185 | 185 |
template <typename Graph> |
186 | 186 |
inline int countNodes(const Graph& g) { |
187 | 187 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
// Arc counting: |
191 | 191 |
|
192 | 192 |
namespace _core_bits { |
193 | 193 |
|
194 | 194 |
template <typename Graph, typename Enable = void> |
195 | 195 |
struct CountArcsSelector { |
196 | 196 |
static int count(const Graph &g) { |
197 | 197 |
return countItems<Graph, typename Graph::Arc>(g); |
198 | 198 |
} |
199 | 199 |
}; |
200 | 200 |
|
201 | 201 |
template <typename Graph> |
202 | 202 |
struct CountArcsSelector< |
203 | 203 |
Graph, |
204 | 204 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
205 | 205 |
{ |
206 | 206 |
static int count(const Graph &g) { |
207 | 207 |
return g.arcNum(); |
208 | 208 |
} |
209 | 209 |
}; |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
/// \brief Function to count the arcs in the graph. |
213 | 213 |
/// |
214 | 214 |
/// This function counts the arcs in the graph. |
215 |
/// The complexity of the function is O(e) but for some |
|
216 |
/// graph structures it is specialized to run in O(1). |
|
215 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
|
216 |
/// graph structures it is specialized to run in <em>O</em>(1). |
|
217 | 217 |
/// |
218 |
/// If the graph contains a \e arcNum() member function and a |
|
219 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
218 |
/// \note If the graph contains a \c arcNum() member function and a |
|
219 |
/// \c ArcNumTag tag then this function calls directly the member |
|
220 | 220 |
/// function to query the cardinality of the arc set. |
221 | 221 |
template <typename Graph> |
222 | 222 |
inline int countArcs(const Graph& g) { |
223 | 223 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
224 | 224 |
} |
225 | 225 |
|
226 | 226 |
// Edge counting: |
227 |
|
|
227 | 228 |
namespace _core_bits { |
228 | 229 |
|
229 | 230 |
template <typename Graph, typename Enable = void> |
230 | 231 |
struct CountEdgesSelector { |
231 | 232 |
static int count(const Graph &g) { |
232 | 233 |
return countItems<Graph, typename Graph::Edge>(g); |
233 | 234 |
} |
234 | 235 |
}; |
235 | 236 |
|
236 | 237 |
template <typename Graph> |
237 | 238 |
struct CountEdgesSelector< |
238 | 239 |
Graph, |
239 | 240 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
240 | 241 |
{ |
241 | 242 |
static int count(const Graph &g) { |
242 | 243 |
return g.edgeNum(); |
243 | 244 |
} |
244 | 245 |
}; |
245 | 246 |
} |
246 | 247 |
|
247 | 248 |
/// \brief Function to count the edges in the graph. |
248 | 249 |
/// |
249 | 250 |
/// This function counts the edges in the graph. |
250 |
/// The complexity of the function is O(m) but for some |
|
251 |
/// graph structures it is specialized to run in O(1). |
|
251 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
|
252 |
/// graph structures it is specialized to run in <em>O</em>(1). |
|
252 | 253 |
/// |
253 |
/// If the graph contains a \e edgeNum() member function and a |
|
254 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
254 |
/// \note If the graph contains a \c edgeNum() member function and a |
|
255 |
/// \c EdgeNumTag tag then this function calls directly the member |
|
255 | 256 |
/// function to query the cardinality of the edge set. |
256 | 257 |
template <typename Graph> |
257 | 258 |
inline int countEdges(const Graph& g) { |
258 | 259 |
return _core_bits::CountEdgesSelector<Graph>::count(g); |
259 | 260 |
|
260 | 261 |
} |
261 | 262 |
|
262 | 263 |
|
263 | 264 |
template <typename Graph, typename DegIt> |
264 | 265 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
265 | 266 |
int num = 0; |
266 | 267 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
267 | 268 |
++num; |
268 | 269 |
} |
269 | 270 |
return num; |
270 | 271 |
} |
271 | 272 |
|
272 | 273 |
/// \brief Function to count the number of the out-arcs from node \c n. |
273 | 274 |
/// |
274 | 275 |
/// This function counts the number of the out-arcs from node \c n |
275 |
/// in the graph. |
|
276 |
/// in the graph \c g. |
|
276 | 277 |
template <typename Graph> |
277 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
278 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
|
278 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) { |
|
279 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); |
|
279 | 280 |
} |
280 | 281 |
|
281 | 282 |
/// \brief Function to count the number of the in-arcs to node \c n. |
282 | 283 |
/// |
283 | 284 |
/// This function counts the number of the in-arcs to node \c n |
284 |
/// in the graph. |
|
285 |
/// in the graph \c g. |
|
285 | 286 |
template <typename Graph> |
286 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
287 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
|
287 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) { |
|
288 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); |
|
288 | 289 |
} |
289 | 290 |
|
290 | 291 |
/// \brief Function to count the number of the inc-edges to node \c n. |
291 | 292 |
/// |
292 | 293 |
/// This function counts the number of the inc-edges to node \c n |
293 |
/// in the graph. |
|
294 |
/// in the undirected graph \c g. |
|
294 | 295 |
template <typename Graph> |
295 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
|
296 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
|
296 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) { |
|
297 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); |
|
297 | 298 |
} |
298 | 299 |
|
299 | 300 |
namespace _core_bits { |
300 | 301 |
|
301 | 302 |
template <typename Digraph, typename Item, typename RefMap> |
302 | 303 |
class MapCopyBase { |
303 | 304 |
public: |
304 | 305 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
305 | 306 |
|
306 | 307 |
virtual ~MapCopyBase() {} |
307 | 308 |
}; |
308 | 309 |
|
309 | 310 |
template <typename Digraph, typename Item, typename RefMap, |
310 |
typename |
|
311 |
typename FromMap, typename ToMap> |
|
311 | 312 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
312 | 313 |
public: |
313 | 314 |
|
314 |
MapCopy(ToMap& tmap, const FromMap& map) |
|
315 |
: _tmap(tmap), _map(map) {} |
|
315 |
MapCopy(const FromMap& map, ToMap& tmap) |
|
316 |
: _map(map), _tmap(tmap) {} |
|
316 | 317 |
|
317 | 318 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
318 | 319 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
319 | 320 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
320 | 321 |
_tmap.set(refMap[it], _map[it]); |
321 | 322 |
} |
322 | 323 |
} |
323 | 324 |
|
324 | 325 |
private: |
326 |
const FromMap& _map; |
|
325 | 327 |
ToMap& _tmap; |
326 |
const FromMap& _map; |
|
327 | 328 |
}; |
328 | 329 |
|
329 | 330 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
330 | 331 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
331 | 332 |
public: |
332 | 333 |
|
333 |
ItemCopy( |
|
334 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} |
|
334 | 335 |
|
335 | 336 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
336 | 337 |
_it = refMap[_item]; |
337 | 338 |
} |
338 | 339 |
|
339 | 340 |
private: |
341 |
Item _item; |
|
340 | 342 |
It& _it; |
341 |
Item _item; |
|
342 | 343 |
}; |
343 | 344 |
|
344 | 345 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
345 | 346 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
346 | 347 |
public: |
347 | 348 |
|
348 | 349 |
RefCopy(Ref& map) : _map(map) {} |
349 | 350 |
|
350 | 351 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
351 | 352 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
352 | 353 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
353 | 354 |
_map.set(it, refMap[it]); |
354 | 355 |
} |
355 | 356 |
} |
356 | 357 |
|
357 | 358 |
private: |
358 | 359 |
Ref& _map; |
359 | 360 |
}; |
360 | 361 |
|
361 | 362 |
template <typename Digraph, typename Item, typename RefMap, |
362 | 363 |
typename CrossRef> |
363 | 364 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
364 | 365 |
public: |
365 | 366 |
|
366 | 367 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
367 | 368 |
|
368 | 369 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
369 | 370 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
370 | 371 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
371 | 372 |
_cmap.set(refMap[it], it); |
372 | 373 |
} |
373 | 374 |
} |
374 | 375 |
|
375 | 376 |
private: |
376 | 377 |
CrossRef& _cmap; |
377 | 378 |
}; |
378 | 379 |
|
379 | 380 |
template <typename Digraph, typename Enable = void> |
380 | 381 |
struct DigraphCopySelector { |
381 | 382 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
382 |
static void copy( |
|
383 |
static void copy(const From& from, Digraph &to, |
|
383 | 384 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
384 | 385 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
385 | 386 |
nodeRefMap[it] = to.addNode(); |
386 | 387 |
} |
387 | 388 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
388 | 389 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
389 | 390 |
nodeRefMap[from.target(it)]); |
390 | 391 |
} |
391 | 392 |
} |
392 | 393 |
}; |
393 | 394 |
|
394 | 395 |
template <typename Digraph> |
395 | 396 |
struct DigraphCopySelector< |
396 | 397 |
Digraph, |
397 | 398 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
398 | 399 |
{ |
399 | 400 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
400 |
static void copy( |
|
401 |
static void copy(const From& from, Digraph &to, |
|
401 | 402 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
402 | 403 |
to.build(from, nodeRefMap, arcRefMap); |
403 | 404 |
} |
404 | 405 |
}; |
405 | 406 |
|
406 | 407 |
template <typename Graph, typename Enable = void> |
407 | 408 |
struct GraphCopySelector { |
408 | 409 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
409 |
static void copy( |
|
410 |
static void copy(const From& from, Graph &to, |
|
410 | 411 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
411 | 412 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
412 | 413 |
nodeRefMap[it] = to.addNode(); |
413 | 414 |
} |
414 | 415 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
415 | 416 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
416 | 417 |
nodeRefMap[from.v(it)]); |
417 | 418 |
} |
418 | 419 |
} |
419 | 420 |
}; |
420 | 421 |
|
421 | 422 |
template <typename Graph> |
422 | 423 |
struct GraphCopySelector< |
423 | 424 |
Graph, |
424 | 425 |
typename enable_if<typename Graph::BuildTag, void>::type> |
425 | 426 |
{ |
426 | 427 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
427 |
static void copy( |
|
428 |
static void copy(const From& from, Graph &to, |
|
428 | 429 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
429 | 430 |
to.build(from, nodeRefMap, edgeRefMap); |
430 | 431 |
} |
431 | 432 |
}; |
432 | 433 |
|
433 | 434 |
} |
434 | 435 |
|
435 | 436 |
/// \brief Class to copy a digraph. |
436 | 437 |
/// |
437 | 438 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
438 |
/// simplest way of using it is through the \c |
|
439 |
/// simplest way of using it is through the \c digraphCopy() function. |
|
439 | 440 |
/// |
440 |
/// This class not |
|
441 |
/// This class not only make a copy of a digraph, but it can create |
|
441 | 442 |
/// references and cross references between the nodes and arcs of |
442 |
/// the two graphs, it can copy maps for use with the newly created |
|
443 |
/// graph and copy nodes and arcs. |
|
443 |
/// the two digraphs, and it can copy maps to use with the newly created |
|
444 |
/// digraph. |
|
444 | 445 |
/// |
445 |
/// To make a copy from a graph, first an instance of DigraphCopy |
|
446 |
/// should be created, then the data belongs to the graph should |
|
446 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
|
447 |
/// should be created, then the data belongs to the digraph should |
|
447 | 448 |
/// assigned to copy. In the end, the \c run() member should be |
448 | 449 |
/// called. |
449 | 450 |
/// |
450 |
/// The next code copies a |
|
451 |
/// The next code copies a digraph with several data: |
|
451 | 452 |
///\code |
452 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
453 |
/// // create a reference for the nodes |
|
453 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
|
454 |
/// // Create references for the nodes |
|
454 | 455 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
455 |
/// dc.nodeRef(nr); |
|
456 |
/// // create a cross reference (inverse) for the arcs |
|
456 |
/// cg.nodeRef(nr); |
|
457 |
/// // Create cross references (inverse) for the arcs |
|
457 | 458 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
458 |
/// dc.arcCrossRef(acr); |
|
459 |
/// // copy an arc map |
|
459 |
/// cg.arcCrossRef(acr); |
|
460 |
/// // Copy an arc map |
|
460 | 461 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
461 | 462 |
/// NewGraph::ArcMap<double> namap(new_graph); |
462 |
/// dc.arcMap(namap, oamap); |
|
463 |
/// // copy a node |
|
463 |
/// cg.arcMap(oamap, namap); |
|
464 |
/// // Copy a node |
|
464 | 465 |
/// OrigGraph::Node on; |
465 | 466 |
/// NewGraph::Node nn; |
466 |
/// dc.node(nn, on); |
|
467 |
/// // Executions of copy |
|
468 |
/// |
|
467 |
/// cg.node(on, nn); |
|
468 |
/// // Execute copying |
|
469 |
/// cg.run(); |
|
469 | 470 |
///\endcode |
470 |
template <typename |
|
471 |
template <typename From, typename To> |
|
471 | 472 |
class DigraphCopy { |
472 | 473 |
private: |
473 | 474 |
|
474 | 475 |
typedef typename From::Node Node; |
475 | 476 |
typedef typename From::NodeIt NodeIt; |
476 | 477 |
typedef typename From::Arc Arc; |
477 | 478 |
typedef typename From::ArcIt ArcIt; |
478 | 479 |
|
479 | 480 |
typedef typename To::Node TNode; |
480 | 481 |
typedef typename To::Arc TArc; |
481 | 482 |
|
482 | 483 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
483 | 484 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
484 | 485 |
|
485 |
|
|
486 | 486 |
public: |
487 | 487 |
|
488 |
|
|
489 |
/// \brief Constructor for the DigraphCopy. |
|
488 |
/// \brief Constructor of DigraphCopy. |
|
490 | 489 |
/// |
491 |
/// It copies the content of the \c _from digraph into the |
|
492 |
/// \c _to digraph. |
|
493 |
|
|
490 |
/// Constructor of DigraphCopy for copying the content of the |
|
491 |
/// \c from digraph into the \c to digraph. |
|
492 |
DigraphCopy(const From& from, To& to) |
|
494 | 493 |
: _from(from), _to(to) {} |
495 | 494 |
|
496 |
/// \brief Destructor of |
|
495 |
/// \brief Destructor of DigraphCopy |
|
497 | 496 |
/// |
498 |
/// Destructor of |
|
497 |
/// Destructor of DigraphCopy. |
|
499 | 498 |
~DigraphCopy() { |
500 | 499 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
501 | 500 |
delete _node_maps[i]; |
502 | 501 |
} |
503 | 502 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
504 | 503 |
delete _arc_maps[i]; |
505 | 504 |
} |
506 | 505 |
|
507 | 506 |
} |
508 | 507 |
|
509 |
/// \brief |
|
508 |
/// \brief Copy the node references into the given map. |
|
510 | 509 |
/// |
511 |
/// Copies the node references into the given map. The parameter |
|
512 |
/// should be a map, which key type is the Node type of the source |
|
513 |
/// graph, while the value type is the Node type of the |
|
514 |
/// destination graph. |
|
510 |
/// This function copies the node references into the given map. |
|
511 |
/// The parameter should be a map, whose key type is the Node type of |
|
512 |
/// the source digraph, while the value type is the Node type of the |
|
513 |
/// destination digraph. |
|
515 | 514 |
template <typename NodeRef> |
516 | 515 |
DigraphCopy& nodeRef(NodeRef& map) { |
517 | 516 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
518 | 517 |
NodeRefMap, NodeRef>(map)); |
519 | 518 |
return *this; |
520 | 519 |
} |
521 | 520 |
|
522 |
/// \brief |
|
521 |
/// \brief Copy the node cross references into the given map. |
|
523 | 522 |
/// |
524 |
/// Copies the node cross references (reverse references) into |
|
525 |
/// the given map. The parameter should be a map, which key type |
|
526 |
/// is the Node type of the destination graph, while the value type is |
|
527 |
/// the Node type of the source graph. |
|
523 |
/// This function copies the node cross references (reverse references) |
|
524 |
/// into the given map. The parameter should be a map, whose key type |
|
525 |
/// is the Node type of the destination digraph, while the value type is |
|
526 |
/// the Node type of the source digraph. |
|
528 | 527 |
template <typename NodeCrossRef> |
529 | 528 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
530 | 529 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
531 | 530 |
NodeRefMap, NodeCrossRef>(map)); |
532 | 531 |
return *this; |
533 | 532 |
} |
534 | 533 |
|
535 |
/// \brief Make copy of the given map. |
|
534 |
/// \brief Make a copy of the given node map. |
|
536 | 535 |
/// |
537 |
/// Makes copy of the given map for the newly created digraph. |
|
538 |
/// The new map's key type is the destination graph's node type, |
|
539 |
/// and the copied map's key type is the source graph's node type. |
|
540 |
template <typename ToMap, typename FromMap> |
|
541 |
|
|
536 |
/// This function makes a copy of the given node map for the newly |
|
537 |
/// created digraph. |
|
538 |
/// The key type of the new map \c tmap should be the Node type of the |
|
539 |
/// destination digraph, and the key type of the original map \c map |
|
540 |
/// should be the Node type of the source digraph. |
|
541 |
template <typename FromMap, typename ToMap> |
|
542 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
|
542 | 543 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
543 |
NodeRefMap, |
|
544 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
|
544 | 545 |
return *this; |
545 | 546 |
} |
546 | 547 |
|
547 | 548 |
/// \brief Make a copy of the given node. |
548 | 549 |
/// |
549 |
/// Make a copy of the given node. |
|
550 |
DigraphCopy& node(TNode& tnode, const Node& snode) { |
|
550 |
/// This function makes a copy of the given node. |
|
551 |
DigraphCopy& node(const Node& node, TNode& tnode) { |
|
551 | 552 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
552 |
NodeRefMap, TNode>( |
|
553 |
NodeRefMap, TNode>(node, tnode)); |
|
553 | 554 |
return *this; |
554 | 555 |
} |
555 | 556 |
|
556 |
/// \brief |
|
557 |
/// \brief Copy the arc references into the given map. |
|
557 | 558 |
/// |
558 |
/// |
|
559 |
/// This function copies the arc references into the given map. |
|
560 |
/// The parameter should be a map, whose key type is the Arc type of |
|
561 |
/// the source digraph, while the value type is the Arc type of the |
|
562 |
/// destination digraph. |
|
559 | 563 |
template <typename ArcRef> |
560 | 564 |
DigraphCopy& arcRef(ArcRef& map) { |
561 | 565 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
562 | 566 |
ArcRefMap, ArcRef>(map)); |
563 | 567 |
return *this; |
564 | 568 |
} |
565 | 569 |
|
566 |
/// \brief |
|
570 |
/// \brief Copy the arc cross references into the given map. |
|
567 | 571 |
/// |
568 |
/// Copies the arc cross references (reverse references) into |
|
569 |
/// the given map. |
|
572 |
/// This function copies the arc cross references (reverse references) |
|
573 |
/// into the given map. The parameter should be a map, whose key type |
|
574 |
/// is the Arc type of the destination digraph, while the value type is |
|
575 |
/// the Arc type of the source digraph. |
|
570 | 576 |
template <typename ArcCrossRef> |
571 | 577 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
572 | 578 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
573 | 579 |
ArcRefMap, ArcCrossRef>(map)); |
574 | 580 |
return *this; |
575 | 581 |
} |
576 | 582 |
|
577 |
/// \brief Make copy of the given map. |
|
583 |
/// \brief Make a copy of the given arc map. |
|
578 | 584 |
/// |
579 |
/// Makes copy of the given map for the newly created digraph. |
|
580 |
/// The new map's key type is the to digraph's arc type, |
|
581 |
/// and the copied map's key type is the from digraph's arc |
|
582 |
/// type. |
|
583 |
template <typename ToMap, typename FromMap> |
|
584 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
585 |
/// This function makes a copy of the given arc map for the newly |
|
586 |
/// created digraph. |
|
587 |
/// The key type of the new map \c tmap should be the Arc type of the |
|
588 |
/// destination digraph, and the key type of the original map \c map |
|
589 |
/// should be the Arc type of the source digraph. |
|
590 |
template <typename FromMap, typename ToMap> |
|
591 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
|
585 | 592 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
586 |
ArcRefMap, |
|
593 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
|
587 | 594 |
return *this; |
588 | 595 |
} |
589 | 596 |
|
590 | 597 |
/// \brief Make a copy of the given arc. |
591 | 598 |
/// |
592 |
/// Make a copy of the given arc. |
|
593 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
599 |
/// This function makes a copy of the given arc. |
|
600 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) { |
|
594 | 601 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
595 |
ArcRefMap, TArc>( |
|
602 |
ArcRefMap, TArc>(arc, tarc)); |
|
596 | 603 |
return *this; |
597 | 604 |
} |
598 | 605 |
|
599 |
/// \brief |
|
606 |
/// \brief Execute copying. |
|
600 | 607 |
/// |
601 |
/// |
|
608 |
/// This function executes the copying of the digraph along with the |
|
609 |
/// copying of the assigned data. |
|
602 | 610 |
void run() { |
603 | 611 |
NodeRefMap nodeRefMap(_from); |
604 | 612 |
ArcRefMap arcRefMap(_from); |
605 | 613 |
_core_bits::DigraphCopySelector<To>:: |
606 |
copy( |
|
614 |
copy(_from, _to, nodeRefMap, arcRefMap); |
|
607 | 615 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
608 | 616 |
_node_maps[i]->copy(_from, nodeRefMap); |
609 | 617 |
} |
610 | 618 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
611 | 619 |
_arc_maps[i]->copy(_from, arcRefMap); |
612 | 620 |
} |
613 | 621 |
} |
614 | 622 |
|
615 | 623 |
protected: |
616 | 624 |
|
617 |
|
|
618 | 625 |
const From& _from; |
619 | 626 |
To& _to; |
620 | 627 |
|
621 | 628 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
622 |
_node_maps; |
|
629 |
_node_maps; |
|
623 | 630 |
|
624 | 631 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
625 |
_arc_maps; |
|
632 |
_arc_maps; |
|
626 | 633 |
|
627 | 634 |
}; |
628 | 635 |
|
629 | 636 |
/// \brief Copy a digraph to another digraph. |
630 | 637 |
/// |
631 |
/// Copy a digraph to another digraph. The complete usage of the |
|
632 |
/// function is detailed in the DigraphCopy class, but a short |
|
633 |
/// |
|
638 |
/// This function copies a digraph to another digraph. |
|
639 |
/// The complete usage of it is detailed in the DigraphCopy class, but |
|
640 |
/// a short example shows a basic work: |
|
634 | 641 |
///\code |
635 |
/// |
|
642 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
|
636 | 643 |
///\endcode |
637 | 644 |
/// |
638 | 645 |
/// After the copy the \c nr map will contain the mapping from the |
639 | 646 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
640 |
/// \c |
|
647 |
/// \c acr will contain the mapping from the arcs of the \c to digraph |
|
641 | 648 |
/// to the arcs of the \c from digraph. |
642 | 649 |
/// |
643 | 650 |
/// \see DigraphCopy |
644 |
template <typename To, typename From> |
|
645 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) { |
|
646 |
|
|
651 |
template <typename From, typename To> |
|
652 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) { |
|
653 |
return DigraphCopy<From, To>(from, to); |
|
647 | 654 |
} |
648 | 655 |
|
649 | 656 |
/// \brief Class to copy a graph. |
650 | 657 |
/// |
651 | 658 |
/// Class to copy a graph to another graph (duplicate a graph). The |
652 |
/// simplest way of using it is through the \c |
|
659 |
/// simplest way of using it is through the \c graphCopy() function. |
|
653 | 660 |
/// |
654 |
/// This class not |
|
661 |
/// This class not only make a copy of a graph, but it can create |
|
655 | 662 |
/// references and cross references between the nodes, edges and arcs of |
656 |
/// the two graphs, it can copy maps for use with the newly created |
|
657 |
/// graph and copy nodes, edges and arcs. |
|
663 |
/// the two graphs, and it can copy maps for using with the newly created |
|
664 |
/// graph. |
|
658 | 665 |
/// |
659 | 666 |
/// To make a copy from a graph, first an instance of GraphCopy |
660 | 667 |
/// should be created, then the data belongs to the graph should |
661 | 668 |
/// assigned to copy. In the end, the \c run() member should be |
662 | 669 |
/// called. |
663 | 670 |
/// |
664 | 671 |
/// The next code copies a graph with several data: |
665 | 672 |
///\code |
666 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
667 |
/// // create a reference for the nodes |
|
673 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
|
674 |
/// // Create references for the nodes |
|
668 | 675 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
669 |
/// dc.nodeRef(nr); |
|
670 |
/// // create a cross reference (inverse) for the edges |
|
671 |
/// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph); |
|
672 |
/// dc.edgeCrossRef(ecr); |
|
673 |
/// // copy an arc map |
|
674 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
675 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
676 |
/// dc.arcMap(namap, oamap); |
|
677 |
/// |
|
676 |
/// cg.nodeRef(nr); |
|
677 |
/// // Create cross references (inverse) for the edges |
|
678 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); |
|
679 |
/// cg.edgeCrossRef(ecr); |
|
680 |
/// // Copy an edge map |
|
681 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph); |
|
682 |
/// NewGraph::EdgeMap<double> nemap(new_graph); |
|
683 |
/// cg.edgeMap(oemap, nemap); |
|
684 |
/// // Copy a node |
|
678 | 685 |
/// OrigGraph::Node on; |
679 | 686 |
/// NewGraph::Node nn; |
680 |
/// dc.node(nn, on); |
|
681 |
/// // Executions of copy |
|
682 |
/// |
|
687 |
/// cg.node(on, nn); |
|
688 |
/// // Execute copying |
|
689 |
/// cg.run(); |
|
683 | 690 |
///\endcode |
684 |
template <typename |
|
691 |
template <typename From, typename To> |
|
685 | 692 |
class GraphCopy { |
686 | 693 |
private: |
687 | 694 |
|
688 | 695 |
typedef typename From::Node Node; |
689 | 696 |
typedef typename From::NodeIt NodeIt; |
690 | 697 |
typedef typename From::Arc Arc; |
691 | 698 |
typedef typename From::ArcIt ArcIt; |
692 | 699 |
typedef typename From::Edge Edge; |
693 | 700 |
typedef typename From::EdgeIt EdgeIt; |
694 | 701 |
|
695 | 702 |
typedef typename To::Node TNode; |
696 | 703 |
typedef typename To::Arc TArc; |
697 | 704 |
typedef typename To::Edge TEdge; |
698 | 705 |
|
699 | 706 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
700 | 707 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
701 | 708 |
|
702 | 709 |
struct ArcRefMap { |
703 |
ArcRefMap(const To& to, |
|
710 |
ArcRefMap(const From& from, const To& to, |
|
704 | 711 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
705 |
: _to(to), |
|
712 |
: _from(from), _to(to), |
|
706 | 713 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
707 | 714 |
|
708 | 715 |
typedef typename From::Arc Key; |
709 | 716 |
typedef typename To::Arc Value; |
710 | 717 |
|
711 | 718 |
Value operator[](const Key& key) const { |
712 | 719 |
bool forward = _from.u(key) != _from.v(key) ? |
713 | 720 |
_node_ref[_from.source(key)] == |
714 | 721 |
_to.source(_to.direct(_edge_ref[key], true)) : |
715 | 722 |
_from.direction(key); |
716 | 723 |
return _to.direct(_edge_ref[key], forward); |
717 | 724 |
} |
718 | 725 |
|
726 |
const From& _from; |
|
719 | 727 |
const To& _to; |
720 |
const From& _from; |
|
721 | 728 |
const EdgeRefMap& _edge_ref; |
722 | 729 |
const NodeRefMap& _node_ref; |
723 | 730 |
}; |
724 | 731 |
|
725 |
|
|
726 | 732 |
public: |
727 | 733 |
|
728 |
|
|
729 |
/// \brief Constructor for the GraphCopy. |
|
734 |
/// \brief Constructor of GraphCopy. |
|
730 | 735 |
/// |
731 |
/// It copies the content of the \c _from graph into the |
|
732 |
/// \c _to graph. |
|
733 |
|
|
736 |
/// Constructor of GraphCopy for copying the content of the |
|
737 |
/// \c from graph into the \c to graph. |
|
738 |
GraphCopy(const From& from, To& to) |
|
734 | 739 |
: _from(from), _to(to) {} |
735 | 740 |
|
736 |
/// \brief Destructor of |
|
741 |
/// \brief Destructor of GraphCopy |
|
737 | 742 |
/// |
738 |
/// Destructor of |
|
743 |
/// Destructor of GraphCopy. |
|
739 | 744 |
~GraphCopy() { |
740 | 745 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
741 | 746 |
delete _node_maps[i]; |
742 | 747 |
} |
743 | 748 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
744 | 749 |
delete _arc_maps[i]; |
745 | 750 |
} |
746 | 751 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
747 | 752 |
delete _edge_maps[i]; |
748 | 753 |
} |
749 |
|
|
750 | 754 |
} |
751 | 755 |
|
752 |
/// \brief |
|
756 |
/// \brief Copy the node references into the given map. |
|
753 | 757 |
/// |
754 |
/// |
|
758 |
/// This function copies the node references into the given map. |
|
759 |
/// The parameter should be a map, whose key type is the Node type of |
|
760 |
/// the source graph, while the value type is the Node type of the |
|
761 |
/// destination graph. |
|
755 | 762 |
template <typename NodeRef> |
756 | 763 |
GraphCopy& nodeRef(NodeRef& map) { |
757 | 764 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
758 | 765 |
NodeRefMap, NodeRef>(map)); |
759 | 766 |
return *this; |
760 | 767 |
} |
761 | 768 |
|
762 |
/// \brief |
|
769 |
/// \brief Copy the node cross references into the given map. |
|
763 | 770 |
/// |
764 |
/// Copies the node cross references (reverse references) into |
|
765 |
/// the given map. |
|
771 |
/// This function copies the node cross references (reverse references) |
|
772 |
/// into the given map. The parameter should be a map, whose key type |
|
773 |
/// is the Node type of the destination graph, while the value type is |
|
774 |
/// the Node type of the source graph. |
|
766 | 775 |
template <typename NodeCrossRef> |
767 | 776 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
768 | 777 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
769 | 778 |
NodeRefMap, NodeCrossRef>(map)); |
770 | 779 |
return *this; |
771 | 780 |
} |
772 | 781 |
|
773 |
/// \brief Make copy of the given map. |
|
782 |
/// \brief Make a copy of the given node map. |
|
774 | 783 |
/// |
775 |
/// Makes copy of the given map for the newly created graph. |
|
776 |
/// The new map's key type is the to graph's node type, |
|
777 |
/// and the copied map's key type is the from graph's node |
|
778 |
/// type. |
|
779 |
template <typename ToMap, typename FromMap> |
|
780 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
|
784 |
/// This function makes a copy of the given node map for the newly |
|
785 |
/// created graph. |
|
786 |
/// The key type of the new map \c tmap should be the Node type of the |
|
787 |
/// destination graph, and the key type of the original map \c map |
|
788 |
/// should be the Node type of the source graph. |
|
789 |
template <typename FromMap, typename ToMap> |
|
790 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
|
781 | 791 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
782 |
NodeRefMap, |
|
792 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
|
783 | 793 |
return *this; |
784 | 794 |
} |
785 | 795 |
|
786 | 796 |
/// \brief Make a copy of the given node. |
787 | 797 |
/// |
788 |
/// Make a copy of the given node. |
|
789 |
GraphCopy& node(TNode& tnode, const Node& snode) { |
|
798 |
/// This function makes a copy of the given node. |
|
799 |
GraphCopy& node(const Node& node, TNode& tnode) { |
|
790 | 800 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
791 |
NodeRefMap, TNode>( |
|
801 |
NodeRefMap, TNode>(node, tnode)); |
|
792 | 802 |
return *this; |
793 | 803 |
} |
794 | 804 |
|
795 |
/// \brief |
|
805 |
/// \brief Copy the arc references into the given map. |
|
796 | 806 |
/// |
797 |
/// |
|
807 |
/// This function copies the arc references into the given map. |
|
808 |
/// The parameter should be a map, whose key type is the Arc type of |
|
809 |
/// the source graph, while the value type is the Arc type of the |
|
810 |
/// destination graph. |
|
798 | 811 |
template <typename ArcRef> |
799 | 812 |
GraphCopy& arcRef(ArcRef& map) { |
800 | 813 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
801 | 814 |
ArcRefMap, ArcRef>(map)); |
802 | 815 |
return *this; |
803 | 816 |
} |
804 | 817 |
|
805 |
/// \brief |
|
818 |
/// \brief Copy the arc cross references into the given map. |
|
806 | 819 |
/// |
807 |
/// Copies the arc cross references (reverse references) into |
|
808 |
/// the given map. |
|
820 |
/// This function copies the arc cross references (reverse references) |
|
821 |
/// into the given map. The parameter should be a map, whose key type |
|
822 |
/// is the Arc type of the destination graph, while the value type is |
|
823 |
/// the Arc type of the source graph. |
|
809 | 824 |
template <typename ArcCrossRef> |
810 | 825 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
811 | 826 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
812 | 827 |
ArcRefMap, ArcCrossRef>(map)); |
813 | 828 |
return *this; |
814 | 829 |
} |
815 | 830 |
|
816 |
/// \brief Make copy of the given map. |
|
831 |
/// \brief Make a copy of the given arc map. |
|
817 | 832 |
/// |
818 |
/// Makes copy of the given map for the newly created graph. |
|
819 |
/// The new map's key type is the to graph's arc type, |
|
820 |
/// and the copied map's key type is the from graph's arc |
|
821 |
/// type. |
|
822 |
template <typename ToMap, typename FromMap> |
|
823 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
833 |
/// This function makes a copy of the given arc map for the newly |
|
834 |
/// created graph. |
|
835 |
/// The key type of the new map \c tmap should be the Arc type of the |
|
836 |
/// destination graph, and the key type of the original map \c map |
|
837 |
/// should be the Arc type of the source graph. |
|
838 |
template <typename FromMap, typename ToMap> |
|
839 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
|
824 | 840 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
825 |
ArcRefMap, |
|
841 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
|
826 | 842 |
return *this; |
827 | 843 |
} |
828 | 844 |
|
829 | 845 |
/// \brief Make a copy of the given arc. |
830 | 846 |
/// |
831 |
/// Make a copy of the given arc. |
|
832 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
847 |
/// This function makes a copy of the given arc. |
|
848 |
GraphCopy& arc(const Arc& arc, TArc& tarc) { |
|
833 | 849 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
834 |
ArcRefMap, TArc>( |
|
850 |
ArcRefMap, TArc>(arc, tarc)); |
|
835 | 851 |
return *this; |
836 | 852 |
} |
837 | 853 |
|
838 |
/// \brief |
|
854 |
/// \brief Copy the edge references into the given map. |
|
839 | 855 |
/// |
840 |
/// |
|
856 |
/// This function copies the edge references into the given map. |
|
857 |
/// The parameter should be a map, whose key type is the Edge type of |
|
858 |
/// the source graph, while the value type is the Edge type of the |
|
859 |
/// destination graph. |
|
841 | 860 |
template <typename EdgeRef> |
842 | 861 |
GraphCopy& edgeRef(EdgeRef& map) { |
843 | 862 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
844 | 863 |
EdgeRefMap, EdgeRef>(map)); |
845 | 864 |
return *this; |
846 | 865 |
} |
847 | 866 |
|
848 |
/// \brief |
|
867 |
/// \brief Copy the edge cross references into the given map. |
|
849 | 868 |
/// |
850 |
/// Copies the edge cross references (reverse |
|
851 |
/// references) into the given map. |
|
869 |
/// This function copies the edge cross references (reverse references) |
|
870 |
/// into the given map. The parameter should be a map, whose key type |
|
871 |
/// is the Edge type of the destination graph, while the value type is |
|
872 |
/// the Edge type of the source graph. |
|
852 | 873 |
template <typename EdgeCrossRef> |
853 | 874 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
854 | 875 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
855 | 876 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
856 | 877 |
return *this; |
857 | 878 |
} |
858 | 879 |
|
859 |
/// \brief Make copy of the given map. |
|
880 |
/// \brief Make a copy of the given edge map. |
|
860 | 881 |
/// |
861 |
/// Makes copy of the given map for the newly created graph. |
|
862 |
/// The new map's key type is the to graph's edge type, |
|
863 |
/// and the copied map's key type is the from graph's edge |
|
864 |
/// type. |
|
865 |
template <typename ToMap, typename FromMap> |
|
866 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
|
882 |
/// This function makes a copy of the given edge map for the newly |
|
883 |
/// created graph. |
|
884 |
/// The key type of the new map \c tmap should be the Edge type of the |
|
885 |
/// destination graph, and the key type of the original map \c map |
|
886 |
/// should be the Edge type of the source graph. |
|
887 |
template <typename FromMap, typename ToMap> |
|
888 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) { |
|
867 | 889 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
868 |
EdgeRefMap, |
|
890 |
EdgeRefMap, FromMap, ToMap>(map, tmap)); |
|
869 | 891 |
return *this; |
870 | 892 |
} |
871 | 893 |
|
872 | 894 |
/// \brief Make a copy of the given edge. |
873 | 895 |
/// |
874 |
/// Make a copy of the given edge. |
|
875 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
|
896 |
/// This function makes a copy of the given edge. |
|
897 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) { |
|
876 | 898 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
877 |
EdgeRefMap, TEdge>( |
|
899 |
EdgeRefMap, TEdge>(edge, tedge)); |
|
878 | 900 |
return *this; |
879 | 901 |
} |
880 | 902 |
|
881 |
/// \brief |
|
903 |
/// \brief Execute copying. |
|
882 | 904 |
/// |
883 |
/// |
|
905 |
/// This function executes the copying of the graph along with the |
|
906 |
/// copying of the assigned data. |
|
884 | 907 |
void run() { |
885 | 908 |
NodeRefMap nodeRefMap(_from); |
886 | 909 |
EdgeRefMap edgeRefMap(_from); |
887 |
ArcRefMap arcRefMap( |
|
910 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
|
888 | 911 |
_core_bits::GraphCopySelector<To>:: |
889 |
copy( |
|
912 |
copy(_from, _to, nodeRefMap, edgeRefMap); |
|
890 | 913 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
891 | 914 |
_node_maps[i]->copy(_from, nodeRefMap); |
892 | 915 |
} |
893 | 916 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
894 | 917 |
_edge_maps[i]->copy(_from, edgeRefMap); |
895 | 918 |
} |
896 | 919 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
897 | 920 |
_arc_maps[i]->copy(_from, arcRefMap); |
898 | 921 |
} |
899 | 922 |
} |
900 | 923 |
|
901 | 924 |
private: |
902 | 925 |
|
903 | 926 |
const From& _from; |
904 | 927 |
To& _to; |
905 | 928 |
|
906 | 929 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
907 |
_node_maps; |
|
930 |
_node_maps; |
|
908 | 931 |
|
909 | 932 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
910 |
_arc_maps; |
|
933 |
_arc_maps; |
|
911 | 934 |
|
912 | 935 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
913 |
_edge_maps; |
|
936 |
_edge_maps; |
|
914 | 937 |
|
915 | 938 |
}; |
916 | 939 |
|
917 | 940 |
/// \brief Copy a graph to another graph. |
918 | 941 |
/// |
919 |
/// Copy a graph to another graph. The complete usage of the |
|
920 |
/// function is detailed in the GraphCopy class, but a short |
|
921 |
/// |
|
942 |
/// This function copies a graph to another graph. |
|
943 |
/// The complete usage of it is detailed in the GraphCopy class, |
|
944 |
/// but a short example shows a basic work: |
|
922 | 945 |
///\code |
923 |
/// |
|
946 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
|
924 | 947 |
///\endcode |
925 | 948 |
/// |
926 | 949 |
/// After the copy the \c nr map will contain the mapping from the |
927 | 950 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
928 |
/// \c ecr will contain the mapping from the arcs of the \c to graph |
|
929 |
/// to the arcs of the \c from graph. |
|
951 |
/// \c ecr will contain the mapping from the edges of the \c to graph |
|
952 |
/// to the edges of the \c from graph. |
|
930 | 953 |
/// |
931 | 954 |
/// \see GraphCopy |
932 |
template <typename To, typename From> |
|
933 |
GraphCopy<To, From> |
|
934 |
copyGraph(To& to, const From& from) { |
|
935 |
return GraphCopy<To, From>(to, from); |
|
955 |
template <typename From, typename To> |
|
956 |
GraphCopy<From, To> |
|
957 |
graphCopy(const From& from, To& to) { |
|
958 |
return GraphCopy<From, To>(from, to); |
|
936 | 959 |
} |
937 | 960 |
|
938 | 961 |
namespace _core_bits { |
939 | 962 |
|
940 | 963 |
template <typename Graph, typename Enable = void> |
941 | 964 |
struct FindArcSelector { |
942 | 965 |
typedef typename Graph::Node Node; |
943 | 966 |
typedef typename Graph::Arc Arc; |
944 | 967 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
945 | 968 |
if (e == INVALID) { |
946 | 969 |
g.firstOut(e, u); |
947 | 970 |
} else { |
948 | 971 |
g.nextOut(e); |
949 | 972 |
} |
950 | 973 |
while (e != INVALID && g.target(e) != v) { |
951 | 974 |
g.nextOut(e); |
952 | 975 |
} |
953 | 976 |
return e; |
954 | 977 |
} |
955 | 978 |
}; |
956 | 979 |
|
957 | 980 |
template <typename Graph> |
958 | 981 |
struct FindArcSelector< |
959 | 982 |
Graph, |
960 |
typename enable_if<typename Graph:: |
|
983 |
typename enable_if<typename Graph::FindArcTag, void>::type> |
|
961 | 984 |
{ |
962 | 985 |
typedef typename Graph::Node Node; |
963 | 986 |
typedef typename Graph::Arc Arc; |
964 | 987 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
965 | 988 |
return g.findArc(u, v, prev); |
966 | 989 |
} |
967 | 990 |
}; |
968 | 991 |
} |
969 | 992 |
|
970 |
/// \brief |
|
993 |
/// \brief Find an arc between two nodes of a digraph. |
|
971 | 994 |
/// |
972 |
/// |
|
995 |
/// This function finds an arc from node \c u to node \c v in the |
|
996 |
/// digraph \c g. |
|
973 | 997 |
/// |
974 | 998 |
/// If \c prev is \ref INVALID (this is the default value), then |
975 | 999 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
976 | 1000 |
/// the next arc from \c u to \c v after \c prev. |
977 | 1001 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
978 | 1002 |
/// |
979 | 1003 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
980 | 1004 |
///\code |
981 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) { |
|
1005 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) { |
|
982 | 1006 |
/// ... |
983 | 1007 |
/// } |
984 | 1008 |
///\endcode |
985 | 1009 |
/// |
986 |
///\sa ArcLookUp |
|
987 |
///\sa AllArcLookUp |
|
988 |
///\ |
|
1010 |
/// \note \ref ConArcIt provides iterator interface for the same |
|
1011 |
/// functionality. |
|
1012 |
/// |
|
989 | 1013 |
///\sa ConArcIt |
1014 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
|
990 | 1015 |
template <typename Graph> |
991 | 1016 |
inline typename Graph::Arc |
992 | 1017 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
993 | 1018 |
typename Graph::Arc prev = INVALID) { |
994 | 1019 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
995 | 1020 |
} |
996 | 1021 |
|
997 |
/// \brief Iterator for iterating on arcs |
|
1022 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
|
998 | 1023 |
/// |
999 |
/// Iterator for iterating on arcs connected the same nodes. It is |
|
1000 |
/// higher level interface for the findArc() function. You can |
|
1024 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
|
1025 |
/// a higher level interface for the \ref findArc() function. You can |
|
1001 | 1026 |
/// use it the following way: |
1002 | 1027 |
///\code |
1003 | 1028 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
1004 | 1029 |
/// ... |
1005 | 1030 |
/// } |
1006 | 1031 |
///\endcode |
1007 | 1032 |
/// |
1008 | 1033 |
///\sa findArc() |
1009 |
///\sa ArcLookUp |
|
1010 |
///\sa AllArcLookUp |
|
1011 |
///\sa DynArcLookUp |
|
1034 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
|
1012 | 1035 |
template <typename _Graph> |
1013 | 1036 |
class ConArcIt : public _Graph::Arc { |
1014 | 1037 |
public: |
1015 | 1038 |
|
1016 | 1039 |
typedef _Graph Graph; |
1017 | 1040 |
typedef typename Graph::Arc Parent; |
1018 | 1041 |
|
1019 | 1042 |
typedef typename Graph::Arc Arc; |
1020 | 1043 |
typedef typename Graph::Node Node; |
1021 | 1044 |
|
1022 | 1045 |
/// \brief Constructor. |
1023 | 1046 |
/// |
1024 |
/// Construct a new ConArcIt iterating on the arcs which |
|
1025 |
/// connects the \c u and \c v node. |
|
1047 |
/// Construct a new ConArcIt iterating on the arcs that |
|
1048 |
/// connects nodes \c u and \c v. |
|
1026 | 1049 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
1027 | 1050 |
Parent::operator=(findArc(_graph, u, v)); |
1028 | 1051 |
} |
1029 | 1052 |
|
1030 | 1053 |
/// \brief Constructor. |
1031 | 1054 |
/// |
1032 |
/// Construct a new ConArcIt which continues the iterating from |
|
1033 |
/// the \c e arc. |
|
1055 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
|
1034 | 1056 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
1035 | 1057 |
|
1036 | 1058 |
/// \brief Increment operator. |
1037 | 1059 |
/// |
1038 | 1060 |
/// It increments the iterator and gives back the next arc. |
1039 | 1061 |
ConArcIt& operator++() { |
1040 | 1062 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
1041 | 1063 |
_graph.target(*this), *this)); |
1042 | 1064 |
return *this; |
1043 | 1065 |
} |
1044 | 1066 |
private: |
1045 | 1067 |
const Graph& _graph; |
1046 | 1068 |
}; |
1047 | 1069 |
|
1048 | 1070 |
namespace _core_bits { |
1049 | 1071 |
|
1050 | 1072 |
template <typename Graph, typename Enable = void> |
1051 | 1073 |
struct FindEdgeSelector { |
1052 | 1074 |
typedef typename Graph::Node Node; |
1053 | 1075 |
typedef typename Graph::Edge Edge; |
1054 | 1076 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
1055 | 1077 |
bool b; |
1056 | 1078 |
if (u != v) { |
1057 | 1079 |
if (e == INVALID) { |
1058 | 1080 |
g.firstInc(e, b, u); |
1059 | 1081 |
} else { |
1060 | 1082 |
b = g.u(e) == u; |
1061 | 1083 |
g.nextInc(e, b); |
1062 | 1084 |
} |
1063 | 1085 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
1064 | 1086 |
g.nextInc(e, b); |
1065 | 1087 |
} |
1066 | 1088 |
} else { |
1067 | 1089 |
if (e == INVALID) { |
1068 | 1090 |
g.firstInc(e, b, u); |
1069 | 1091 |
} else { |
1070 | 1092 |
b = true; |
1071 | 1093 |
g.nextInc(e, b); |
1072 | 1094 |
} |
1073 | 1095 |
while (e != INVALID && (!b || g.v(e) != v)) { |
1074 | 1096 |
g.nextInc(e, b); |
1075 | 1097 |
} |
1076 | 1098 |
} |
1077 | 1099 |
return e; |
1078 | 1100 |
} |
1079 | 1101 |
}; |
1080 | 1102 |
|
1081 | 1103 |
template <typename Graph> |
1082 | 1104 |
struct FindEdgeSelector< |
1083 | 1105 |
Graph, |
1084 | 1106 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
1085 | 1107 |
{ |
1086 | 1108 |
typedef typename Graph::Node Node; |
1087 | 1109 |
typedef typename Graph::Edge Edge; |
1088 | 1110 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
1089 | 1111 |
return g.findEdge(u, v, prev); |
1090 | 1112 |
} |
1091 | 1113 |
}; |
1092 | 1114 |
} |
1093 | 1115 |
|
1094 |
/// \brief |
|
1116 |
/// \brief Find an edge between two nodes of a graph. |
|
1095 | 1117 |
/// |
1096 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
|
1097 |
/// If the node \c u and node \c v is equal then each loop edge |
|
1118 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
|
1119 |
/// If node \c u and node \c v is equal then each loop edge |
|
1098 | 1120 |
/// will be enumerated once. |
1099 | 1121 |
/// |
1100 | 1122 |
/// If \c prev is \ref INVALID (this is the default value), then |
1101 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
1102 |
/// the next arc from \c u to \c v after \c prev. |
|
1103 |
/// |
|
1123 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
|
1124 |
/// the next edge from \c u to \c v after \c prev. |
|
1125 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
|
1104 | 1126 |
/// |
1105 |
/// Thus you can iterate through each |
|
1127 |
/// Thus you can iterate through each edge between \c u and \c v |
|
1128 |
/// as it follows. |
|
1106 | 1129 |
///\code |
1107 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
|
1108 |
/// e = findEdge(g,u,v,e)) { |
|
1130 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) { |
|
1109 | 1131 |
/// ... |
1110 | 1132 |
/// } |
1111 | 1133 |
///\endcode |
1112 | 1134 |
/// |
1135 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
|
1136 |
/// functionality. |
|
1137 |
/// |
|
1113 | 1138 |
///\sa ConEdgeIt |
1114 |
|
|
1115 | 1139 |
template <typename Graph> |
1116 | 1140 |
inline typename Graph::Edge |
1117 | 1141 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1118 | 1142 |
typename Graph::Edge p = INVALID) { |
1119 | 1143 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
1120 | 1144 |
} |
1121 | 1145 |
|
1122 |
/// \brief Iterator for iterating on edges |
|
1146 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
|
1123 | 1147 |
/// |
1124 |
/// Iterator for iterating on edges connected the same nodes. It is |
|
1125 |
/// higher level interface for the findEdge() function. You can |
|
1148 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
|
1149 |
/// It is a higher level interface for the findEdge() function. You can |
|
1126 | 1150 |
/// use it the following way: |
1127 | 1151 |
///\code |
1128 |
/// for (ConEdgeIt<Graph> it(g, |
|
1152 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { |
|
1129 | 1153 |
/// ... |
1130 | 1154 |
/// } |
1131 | 1155 |
///\endcode |
1132 | 1156 |
/// |
1133 | 1157 |
///\sa findEdge() |
1134 | 1158 |
template <typename _Graph> |
1135 | 1159 |
class ConEdgeIt : public _Graph::Edge { |
1136 | 1160 |
public: |
1137 | 1161 |
|
1138 | 1162 |
typedef _Graph Graph; |
1139 | 1163 |
typedef typename Graph::Edge Parent; |
1140 | 1164 |
|
1141 | 1165 |
typedef typename Graph::Edge Edge; |
1142 | 1166 |
typedef typename Graph::Node Node; |
1143 | 1167 |
|
1144 | 1168 |
/// \brief Constructor. |
1145 | 1169 |
/// |
1146 |
/// Construct a new ConEdgeIt iterating on the edges which |
|
1147 |
/// connects the \c u and \c v node. |
|
1170 |
/// Construct a new ConEdgeIt iterating on the edges that |
|
1171 |
/// connects nodes \c u and \c v. |
|
1148 | 1172 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) { |
1149 | 1173 |
Parent::operator=(findEdge(_graph, u, v)); |
1150 | 1174 |
} |
1151 | 1175 |
|
1152 | 1176 |
/// \brief Constructor. |
1153 | 1177 |
/// |
1154 |
/// Construct a new ConEdgeIt which continues the iterating from |
|
1155 |
/// the \c e edge. |
|
1178 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
|
1156 | 1179 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
1157 | 1180 |
|
1158 | 1181 |
/// \brief Increment operator. |
1159 | 1182 |
/// |
1160 | 1183 |
/// It increments the iterator and gives back the next edge. |
1161 | 1184 |
ConEdgeIt& operator++() { |
1162 | 1185 |
Parent::operator=(findEdge(_graph, _graph.u(*this), |
1163 | 1186 |
_graph.v(*this), *this)); |
1164 | 1187 |
return *this; |
1165 | 1188 |
} |
1166 | 1189 |
private: |
1167 | 1190 |
const Graph& _graph; |
1168 | 1191 |
}; |
1169 | 1192 |
|
1170 | 1193 |
|
1171 |
///Dynamic arc look |
|
1194 |
///Dynamic arc look-up between given endpoints. |
|
1172 | 1195 |
|
1173 | 1196 |
///Using this class, you can find an arc in a digraph from a given |
1174 |
///source to a given target in amortized time <em>O |
|
1197 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
|
1175 | 1198 |
///where <em>d</em> is the out-degree of the source node. |
1176 | 1199 |
/// |
1177 | 1200 |
///It is possible to find \e all parallel arcs between two nodes with |
1178 | 1201 |
///the \c operator() member. |
1179 | 1202 |
/// |
1180 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
|
1181 |
///digraph is not changed so frequently. |
|
1203 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
|
1204 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
|
1182 | 1205 |
/// |
1183 |
///This class uses a self-adjusting binary search tree, Sleator's |
|
1184 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
|
1185 |
/// |
|
1206 |
///This class uses a self-adjusting binary search tree, the Splay tree |
|
1207 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
|
1208 |
///time bound for arc look-ups. This class also guarantees the |
|
1186 | 1209 |
///optimal time bound in a constant factor for any distribution of |
1187 | 1210 |
///queries. |
1188 | 1211 |
/// |
1189 | 1212 |
///\tparam G The type of the underlying digraph. |
1190 | 1213 |
/// |
1191 | 1214 |
///\sa ArcLookUp |
1192 | 1215 |
///\sa AllArcLookUp |
1193 | 1216 |
template<class G> |
1194 | 1217 |
class DynArcLookUp |
1195 | 1218 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
1196 | 1219 |
{ |
1197 | 1220 |
public: |
1198 | 1221 |
typedef typename ItemSetTraits<G, typename G::Arc> |
1199 | 1222 |
::ItemNotifier::ObserverBase Parent; |
1200 | 1223 |
|
1201 | 1224 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1202 | 1225 |
typedef G Digraph; |
1203 | 1226 |
|
1204 | 1227 |
protected: |
1205 | 1228 |
|
1206 | 1229 |
class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type { |
1207 | 1230 |
public: |
1208 | 1231 |
|
1209 | 1232 |
typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent; |
1210 | 1233 |
|
1211 | 1234 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
1212 | 1235 |
|
1213 | 1236 |
virtual void add(const Node& node) { |
1214 | 1237 |
Parent::add(node); |
1215 | 1238 |
Parent::set(node, INVALID); |
1216 | 1239 |
} |
1217 | 1240 |
|
1218 | 1241 |
virtual void add(const std::vector<Node>& nodes) { |
1219 | 1242 |
Parent::add(nodes); |
1220 | 1243 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1221 | 1244 |
Parent::set(nodes[i], INVALID); |
1222 | 1245 |
} |
1223 | 1246 |
} |
1224 | 1247 |
|
1225 | 1248 |
virtual void build() { |
1226 | 1249 |
Parent::build(); |
1227 | 1250 |
Node it; |
1228 | 1251 |
typename Parent::Notifier* nf = Parent::notifier(); |
1229 | 1252 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1230 | 1253 |
Parent::set(it, INVALID); |
1231 | 1254 |
} |
1232 | 1255 |
} |
1233 | 1256 |
}; |
... | ... |
@@ -1462,367 +1485,360 @@ |
1462 | 1485 |
if (_left[_parent[v]] == w) { |
1463 | 1486 |
_left.set(_parent[v], v); |
1464 | 1487 |
} else { |
1465 | 1488 |
_right.set(_parent[v], v); |
1466 | 1489 |
} |
1467 | 1490 |
} |
1468 | 1491 |
if (_right[w] != INVALID){ |
1469 | 1492 |
_parent.set(_right[w], w); |
1470 | 1493 |
} |
1471 | 1494 |
} |
1472 | 1495 |
|
1473 | 1496 |
void splay(Arc v) { |
1474 | 1497 |
while (_parent[v] != INVALID) { |
1475 | 1498 |
if (v == _left[_parent[v]]) { |
1476 | 1499 |
if (_parent[_parent[v]] == INVALID) { |
1477 | 1500 |
zig(v); |
1478 | 1501 |
} else { |
1479 | 1502 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1480 | 1503 |
zig(_parent[v]); |
1481 | 1504 |
zig(v); |
1482 | 1505 |
} else { |
1483 | 1506 |
zig(v); |
1484 | 1507 |
zag(v); |
1485 | 1508 |
} |
1486 | 1509 |
} |
1487 | 1510 |
} else { |
1488 | 1511 |
if (_parent[_parent[v]] == INVALID) { |
1489 | 1512 |
zag(v); |
1490 | 1513 |
} else { |
1491 | 1514 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1492 | 1515 |
zag(v); |
1493 | 1516 |
zig(v); |
1494 | 1517 |
} else { |
1495 | 1518 |
zag(_parent[v]); |
1496 | 1519 |
zag(v); |
1497 | 1520 |
} |
1498 | 1521 |
} |
1499 | 1522 |
} |
1500 | 1523 |
} |
1501 | 1524 |
_head[_g.source(v)] = v; |
1502 | 1525 |
} |
1503 | 1526 |
|
1504 | 1527 |
|
1505 | 1528 |
public: |
1506 | 1529 |
|
1507 | 1530 |
///Find an arc between two nodes. |
1508 | 1531 |
|
1509 | 1532 |
///Find an arc between two nodes. |
1510 |
///\param s The source node |
|
1511 |
///\param t The target node |
|
1533 |
///\param s The source node. |
|
1534 |
///\param t The target node. |
|
1512 | 1535 |
///\param p The previous arc between \c s and \c t. It it is INVALID or |
1513 | 1536 |
///not given, the operator finds the first appropriate arc. |
1514 | 1537 |
///\return An arc from \c s to \c t after \c p or |
1515 | 1538 |
///\ref INVALID if there is no more. |
1516 | 1539 |
/// |
1517 | 1540 |
///For example, you can count the number of arcs from \c u to \c v in the |
1518 | 1541 |
///following way. |
1519 | 1542 |
///\code |
1520 | 1543 |
///DynArcLookUp<ListDigraph> ae(g); |
1521 | 1544 |
///... |
1522 |
///int n=0; |
|
1523 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
1545 |
///int n = 0; |
|
1546 |
///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++; |
|
1524 | 1547 |
///\endcode |
1525 | 1548 |
/// |
1526 |
///Finding the arcs take at most <em>O |
|
1549 |
///Finding the arcs take at most <em>O</em>(log<em>d</em>) |
|
1527 | 1550 |
///amortized time, specifically, the time complexity of the lookups |
1528 | 1551 |
///is equal to the optimal search tree implementation for the |
1529 | 1552 |
///current query distribution in a constant factor. |
1530 | 1553 |
/// |
1531 | 1554 |
///\note This is a dynamic data structure, therefore the data |
1532 |
///structure is updated after each graph alteration. However, |
|
1533 |
///theoretically this data structure is faster than \c ArcLookUp |
|
1534 |
/// |
|
1555 |
///structure is updated after each graph alteration. Thus although |
|
1556 |
///this data structure is theoretically faster than \ref ArcLookUp |
|
1557 |
///and \ref AllArcLookup, it often provides worse performance than |
|
1535 | 1558 |
///them. |
1536 |
/// |
|
1537 | 1559 |
Arc operator()(Node s, Node t, Arc p = INVALID) const { |
1538 | 1560 |
if (p == INVALID) { |
1539 | 1561 |
Arc a = _head[s]; |
1540 | 1562 |
if (a == INVALID) return INVALID; |
1541 | 1563 |
Arc r = INVALID; |
1542 | 1564 |
while (true) { |
1543 | 1565 |
if (_g.target(a) < t) { |
1544 | 1566 |
if (_right[a] == INVALID) { |
1545 | 1567 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1546 | 1568 |
return r; |
1547 | 1569 |
} else { |
1548 | 1570 |
a = _right[a]; |
1549 | 1571 |
} |
1550 | 1572 |
} else { |
1551 | 1573 |
if (_g.target(a) == t) { |
1552 | 1574 |
r = a; |
1553 | 1575 |
} |
1554 | 1576 |
if (_left[a] == INVALID) { |
1555 | 1577 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1556 | 1578 |
return r; |
1557 | 1579 |
} else { |
1558 | 1580 |
a = _left[a]; |
1559 | 1581 |
} |
1560 | 1582 |
} |
1561 | 1583 |
} |
1562 | 1584 |
} else { |
1563 | 1585 |
Arc a = p; |
1564 | 1586 |
if (_right[a] != INVALID) { |
1565 | 1587 |
a = _right[a]; |
1566 | 1588 |
while (_left[a] != INVALID) { |
1567 | 1589 |
a = _left[a]; |
1568 | 1590 |
} |
1569 | 1591 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1570 | 1592 |
} else { |
1571 | 1593 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
1572 | 1594 |
a = _parent[a]; |
1573 | 1595 |
} |
1574 | 1596 |
if (_parent[a] == INVALID) { |
1575 | 1597 |
return INVALID; |
1576 | 1598 |
} else { |
1577 | 1599 |
a = _parent[a]; |
1578 | 1600 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1579 | 1601 |
} |
1580 | 1602 |
} |
1581 | 1603 |
if (_g.target(a) == t) return a; |
1582 | 1604 |
else return INVALID; |
1583 | 1605 |
} |
1584 | 1606 |
} |
1585 | 1607 |
|
1586 | 1608 |
}; |
1587 | 1609 |
|
1588 |
///Fast arc look |
|
1610 |
///Fast arc look-up between given endpoints. |
|
1589 | 1611 |
|
1590 | 1612 |
///Using this class, you can find an arc in a digraph from a given |
1591 |
///source to a given target in time <em>O(log |
|
1613 |
///source to a given target in time <em>O</em>(log<em>d</em>), |
|
1592 | 1614 |
///where <em>d</em> is the out-degree of the source node. |
1593 | 1615 |
/// |
1594 | 1616 |
///It is not possible to find \e all parallel arcs between two nodes. |
1595 | 1617 |
///Use \ref AllArcLookUp for this purpose. |
1596 | 1618 |
/// |
1597 |
///\warning This class is static, so you should refresh() (or at least |
|
1598 |
///refresh(Node)) this data structure |
|
1599 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
1600 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
1619 |
///\warning This class is static, so you should call refresh() (or at |
|
1620 |
///least refresh(Node)) to refresh this data structure whenever the |
|
1621 |
///digraph changes. This is a time consuming (superlinearly proportional |
|
1622 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
|
1601 | 1623 |
/// |
1602 | 1624 |
///\tparam G The type of the underlying digraph. |
1603 | 1625 |
/// |
1604 | 1626 |
///\sa DynArcLookUp |
1605 | 1627 |
///\sa AllArcLookUp |
1606 | 1628 |
template<class G> |
1607 | 1629 |
class ArcLookUp |
1608 | 1630 |
{ |
1609 | 1631 |
public: |
1610 | 1632 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1611 | 1633 |
typedef G Digraph; |
1612 | 1634 |
|
1613 | 1635 |
protected: |
1614 | 1636 |
const Digraph &_g; |
1615 | 1637 |
typename Digraph::template NodeMap<Arc> _head; |
1616 | 1638 |
typename Digraph::template ArcMap<Arc> _left; |
1617 | 1639 |
typename Digraph::template ArcMap<Arc> _right; |
1618 | 1640 |
|
1619 | 1641 |
class ArcLess { |
1620 | 1642 |
const Digraph &g; |
1621 | 1643 |
public: |
1622 | 1644 |
ArcLess(const Digraph &_g) : g(_g) {} |
1623 | 1645 |
bool operator()(Arc a,Arc b) const |
1624 | 1646 |
{ |
1625 | 1647 |
return g.target(a)<g.target(b); |
1626 | 1648 |
} |
1627 | 1649 |
}; |
1628 | 1650 |
|
1629 | 1651 |
public: |
1630 | 1652 |
|
1631 | 1653 |
///Constructor |
1632 | 1654 |
|
1633 | 1655 |
///Constructor. |
1634 | 1656 |
/// |
1635 | 1657 |
///It builds up the search database, which remains valid until the digraph |
1636 | 1658 |
///changes. |
1637 | 1659 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
1638 | 1660 |
|
1639 | 1661 |
private: |
1640 | 1662 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1641 | 1663 |
{ |
1642 | 1664 |
int m=(a+b)/2; |
1643 | 1665 |
Arc me=v[m]; |
1644 | 1666 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
1645 | 1667 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
1646 | 1668 |
return me; |
1647 | 1669 |
} |
1648 | 1670 |
public: |
1649 |
///Refresh the data structure at a node. |
|
1671 |
///Refresh the search data structure at a node. |
|
1650 | 1672 |
|
1651 | 1673 |
///Build up the search database of node \c n. |
1652 | 1674 |
/// |
1653 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
1654 |
///the number of the outgoing arcs of \c n. |
|
1675 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> |
|
1676 |
///is the number of the outgoing arcs of \c n. |
|
1655 | 1677 |
void refresh(Node n) |
1656 | 1678 |
{ |
1657 | 1679 |
std::vector<Arc> v; |
1658 | 1680 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
1659 | 1681 |
if(v.size()) { |
1660 | 1682 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1661 | 1683 |
_head[n]=refreshRec(v,0,v.size()-1); |
1662 | 1684 |
} |
1663 | 1685 |
else _head[n]=INVALID; |
1664 | 1686 |
} |
1665 | 1687 |
///Refresh the full data structure. |
1666 | 1688 |
|
1667 | 1689 |
///Build up the full search database. In fact, it simply calls |
1668 | 1690 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1669 | 1691 |
/// |
1670 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
1671 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
1692 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
|
1693 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
|
1672 | 1694 |
///out-degree of the digraph. |
1673 |
|
|
1674 | 1695 |
void refresh() |
1675 | 1696 |
{ |
1676 | 1697 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
1677 | 1698 |
} |
1678 | 1699 |
|
1679 | 1700 |
///Find an arc between two nodes. |
1680 | 1701 |
|
1681 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
1682 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
1683 |
///\param s The source node |
|
1684 |
///\param t The target node |
|
1702 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), where |
|
1703 |
///<em>d</em> is the number of outgoing arcs of \c s. |
|
1704 |
///\param s The source node. |
|
1705 |
///\param t The target node. |
|
1685 | 1706 |
///\return An arc from \c s to \c t if there exists, |
1686 | 1707 |
///\ref INVALID otherwise. |
1687 | 1708 |
/// |
1688 | 1709 |
///\warning If you change the digraph, refresh() must be called before using |
1689 | 1710 |
///this operator. If you change the outgoing arcs of |
1690 |
///a single node \c n, then |
|
1691 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
1692 |
/// |
|
1711 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
|
1693 | 1712 |
Arc operator()(Node s, Node t) const |
1694 | 1713 |
{ |
1695 | 1714 |
Arc e; |
1696 | 1715 |
for(e=_head[s]; |
1697 | 1716 |
e!=INVALID&&_g.target(e)!=t; |
1698 | 1717 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
1699 | 1718 |
return e; |
1700 | 1719 |
} |
1701 | 1720 |
|
1702 | 1721 |
}; |
1703 | 1722 |
|
1704 |
///Fast look |
|
1723 |
///Fast look-up of all arcs between given endpoints. |
|
1705 | 1724 |
|
1706 | 1725 |
///This class is the same as \ref ArcLookUp, with the addition |
1707 |
///that it makes it possible to find all arcs between given |
|
1726 |
///that it makes it possible to find all parallel arcs between given |
|
1727 |
///endpoints. |
|
1708 | 1728 |
/// |
1709 |
///\warning This class is static, so you should refresh() (or at least |
|
1710 |
///refresh(Node)) this data structure |
|
1711 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
1712 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
1729 |
///\warning This class is static, so you should call refresh() (or at |
|
1730 |
///least refresh(Node)) to refresh this data structure whenever the |
|
1731 |
///digraph changes. This is a time consuming (superlinearly proportional |
|
1732 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
|
1713 | 1733 |
/// |
1714 | 1734 |
///\tparam G The type of the underlying digraph. |
1715 | 1735 |
/// |
1716 | 1736 |
///\sa DynArcLookUp |
1717 | 1737 |
///\sa ArcLookUp |
1718 | 1738 |
template<class G> |
1719 | 1739 |
class AllArcLookUp : public ArcLookUp<G> |
1720 | 1740 |
{ |
1721 | 1741 |
using ArcLookUp<G>::_g; |
1722 | 1742 |
using ArcLookUp<G>::_right; |
1723 | 1743 |
using ArcLookUp<G>::_left; |
1724 | 1744 |
using ArcLookUp<G>::_head; |
1725 | 1745 |
|
1726 | 1746 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1727 | 1747 |
typedef G Digraph; |
1728 | 1748 |
|
1729 | 1749 |
typename Digraph::template ArcMap<Arc> _next; |
1730 | 1750 |
|
1731 | 1751 |
Arc refreshNext(Arc head,Arc next=INVALID) |
1732 | 1752 |
{ |
1733 | 1753 |
if(head==INVALID) return next; |
1734 | 1754 |
else { |
1735 | 1755 |
next=refreshNext(_right[head],next); |
1736 |
// _next[head]=next; |
|
1737 | 1756 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
1738 | 1757 |
? next : INVALID; |
1739 | 1758 |
return refreshNext(_left[head],head); |
1740 | 1759 |
} |
1741 | 1760 |
} |
1742 | 1761 |
|
1743 | 1762 |
void refreshNext() |
1744 | 1763 |
{ |
1745 | 1764 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
1746 | 1765 |
} |
1747 | 1766 |
|
1748 | 1767 |
public: |
1749 | 1768 |
///Constructor |
1750 | 1769 |
|
1751 | 1770 |
///Constructor. |
1752 | 1771 |
/// |
1753 | 1772 |
///It builds up the search database, which remains valid until the digraph |
1754 | 1773 |
///changes. |
1755 | 1774 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
1756 | 1775 |
|
1757 | 1776 |
///Refresh the data structure at a node. |
1758 | 1777 |
|
1759 | 1778 |
///Build up the search database of node \c n. |
1760 | 1779 |
/// |
1761 |
///It runs in time <em>O(d</em>log<em>d |
|
1780 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is |
|
1762 | 1781 |
///the number of the outgoing arcs of \c n. |
1763 |
|
|
1764 | 1782 |
void refresh(Node n) |
1765 | 1783 |
{ |
1766 | 1784 |
ArcLookUp<G>::refresh(n); |
1767 | 1785 |
refreshNext(_head[n]); |
1768 | 1786 |
} |
1769 | 1787 |
|
1770 | 1788 |
///Refresh the full data structure. |
1771 | 1789 |
|
1772 | 1790 |
///Build up the full search database. In fact, it simply calls |
1773 | 1791 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1774 | 1792 |
/// |
1775 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
1776 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
1793 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
|
1794 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
|
1777 | 1795 |
///out-degree of the digraph. |
1778 |
|
|
1779 | 1796 |
void refresh() |
1780 | 1797 |
{ |
1781 | 1798 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
1782 | 1799 |
} |
1783 | 1800 |
|
1784 | 1801 |
///Find an arc between two nodes. |
1785 | 1802 |
|
1786 | 1803 |
///Find an arc between two nodes. |
1787 |
///\param s The source node |
|
1788 |
///\param t The target node |
|
1804 |
///\param s The source node. |
|
1805 |
///\param t The target node. |
|
1789 | 1806 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
1790 | 1807 |
///not given, the operator finds the first appropriate arc. |
1791 | 1808 |
///\return An arc from \c s to \c t after \c prev or |
1792 | 1809 |
///\ref INVALID if there is no more. |
1793 | 1810 |
/// |
1794 | 1811 |
///For example, you can count the number of arcs from \c u to \c v in the |
1795 | 1812 |
///following way. |
1796 | 1813 |
///\code |
1797 | 1814 |
///AllArcLookUp<ListDigraph> ae(g); |
1798 | 1815 |
///... |
1799 |
///int n=0; |
|
1800 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
1816 |
///int n = 0; |
|
1817 |
///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; |
|
1801 | 1818 |
///\endcode |
1802 | 1819 |
/// |
1803 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where |
|
1804 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
1820 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time, where |
|
1821 |
///<em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
1805 | 1822 |
///consecutive arcs are found in constant time. |
1806 | 1823 |
/// |
1807 | 1824 |
///\warning If you change the digraph, refresh() must be called before using |
1808 | 1825 |
///this operator. If you change the outgoing arcs of |
1809 |
///a single node \c n, then |
|
1810 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
1826 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
|
1811 | 1827 |
/// |
1812 | 1828 |
#ifdef DOXYGEN |
1813 | 1829 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
1814 | 1830 |
#else |
1815 | 1831 |
using ArcLookUp<G>::operator() ; |
1816 | 1832 |
Arc operator()(Node s, Node t, Arc prev) const |
1817 | 1833 |
{ |
1818 | 1834 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
1819 | 1835 |
} |
1820 | 1836 |
#endif |
1821 | 1837 |
|
1822 | 1838 |
}; |
1823 | 1839 |
|
1824 | 1840 |
/// @} |
1825 | 1841 |
|
1826 | 1842 |
} //namespace lemon |
1827 | 1843 |
|
1828 | 1844 |
#endif |
... | ... |
@@ -10,107 +10,105 @@ |
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_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/assert.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
#include <lemon/path.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
///Default traits class of Dfs class. |
37 | 37 |
|
38 | 38 |
///Default traits class of Dfs class. |
39 | 39 |
///\tparam GR Digraph type. |
40 | 40 |
template<class GR> |
41 | 41 |
struct DfsDefaultTraits |
42 | 42 |
{ |
43 | 43 |
///The type of the digraph the algorithm runs on. |
44 | 44 |
typedef GR Digraph; |
45 | 45 |
|
46 | 46 |
///\brief The type of the map that stores the predecessor |
47 | 47 |
///arcs of the %DFS paths. |
48 | 48 |
/// |
49 | 49 |
///The type of the map that stores the predecessor |
50 | 50 |
///arcs of the %DFS paths. |
51 | 51 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
52 | 52 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
53 | 53 |
///Instantiates a \ref PredMap. |
54 | 54 |
|
55 | 55 |
///This function instantiates a \ref PredMap. |
56 | 56 |
///\param g is the digraph, to which we would like to define the |
57 | 57 |
///\ref PredMap. |
58 |
///\todo The digraph alone may be insufficient to initialize |
|
59 | 58 |
static PredMap *createPredMap(const Digraph &g) |
60 | 59 |
{ |
61 | 60 |
return new PredMap(g); |
62 | 61 |
} |
63 | 62 |
|
64 | 63 |
///The type of the map that indicates which nodes are processed. |
65 | 64 |
|
66 | 65 |
///The type of the map that indicates which nodes are processed. |
67 | 66 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
68 |
///By default it is a NullMap. |
|
69 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
70 | 68 |
///Instantiates a \ref ProcessedMap. |
71 | 69 |
|
72 | 70 |
///This function instantiates a \ref ProcessedMap. |
73 | 71 |
///\param g is the digraph, to which |
74 | 72 |
///we would like to define the \ref ProcessedMap |
75 | 73 |
#ifdef DOXYGEN |
76 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
77 | 75 |
#else |
78 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
79 | 77 |
#endif |
80 | 78 |
{ |
81 | 79 |
return new ProcessedMap(); |
82 | 80 |
} |
83 | 81 |
|
84 | 82 |
///The type of the map that indicates which nodes are reached. |
85 | 83 |
|
86 | 84 |
///The type of the map that indicates which nodes are reached. |
87 | 85 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
88 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
89 | 87 |
///Instantiates a \ref ReachedMap. |
90 | 88 |
|
91 | 89 |
///This function instantiates a \ref ReachedMap. |
92 | 90 |
///\param g is the digraph, to which |
93 | 91 |
///we would like to define the \ref ReachedMap. |
94 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
95 | 93 |
{ |
96 | 94 |
return new ReachedMap(g); |
97 | 95 |
} |
98 | 96 |
|
99 | 97 |
///The type of the map that stores the distances of the nodes. |
100 | 98 |
|
101 | 99 |
///The type of the map that stores the distances of the nodes. |
102 | 100 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
103 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
104 | 102 |
///Instantiates a \ref DistMap. |
105 | 103 |
|
106 | 104 |
///This function instantiates a \ref DistMap. |
107 | 105 |
///\param g is the digraph, to which we would like to define the |
108 | 106 |
///\ref DistMap. |
109 | 107 |
static DistMap *createDistMap(const Digraph &g) |
110 | 108 |
{ |
111 | 109 |
return new DistMap(g); |
112 | 110 |
} |
113 | 111 |
}; |
114 | 112 |
|
115 | 113 |
///%DFS algorithm class. |
116 | 114 |
|
... | ... |
@@ -151,98 +149,97 @@ |
151 | 149 |
|
152 | 150 |
///The type of the digraph the algorithm runs on. |
153 | 151 |
typedef typename TR::Digraph Digraph; |
154 | 152 |
|
155 | 153 |
///\brief The type of the map that stores the predecessor arcs of the |
156 | 154 |
///DFS paths. |
157 | 155 |
typedef typename TR::PredMap PredMap; |
158 | 156 |
///The type of the map that stores the distances of the nodes. |
159 | 157 |
typedef typename TR::DistMap DistMap; |
160 | 158 |
///The type of the map that indicates which nodes are reached. |
161 | 159 |
typedef typename TR::ReachedMap ReachedMap; |
162 | 160 |
///The type of the map that indicates which nodes are processed. |
163 | 161 |
typedef typename TR::ProcessedMap ProcessedMap; |
164 | 162 |
///The type of the paths. |
165 | 163 |
typedef PredMapPath<Digraph, PredMap> Path; |
166 | 164 |
|
167 | 165 |
///The traits class. |
168 | 166 |
typedef TR Traits; |
169 | 167 |
|
170 | 168 |
private: |
171 | 169 |
|
172 | 170 |
typedef typename Digraph::Node Node; |
173 | 171 |
typedef typename Digraph::NodeIt NodeIt; |
174 | 172 |
typedef typename Digraph::Arc Arc; |
175 | 173 |
typedef typename Digraph::OutArcIt OutArcIt; |
176 | 174 |
|
177 | 175 |
//Pointer to the underlying digraph. |
178 | 176 |
const Digraph *G; |
179 | 177 |
//Pointer to the map of predecessor arcs. |
180 | 178 |
PredMap *_pred; |
181 | 179 |
//Indicates if _pred is locally allocated (true) or not. |
182 | 180 |
bool local_pred; |
183 | 181 |
//Pointer to the map of distances. |
184 | 182 |
DistMap *_dist; |
185 | 183 |
//Indicates if _dist is locally allocated (true) or not. |
186 | 184 |
bool local_dist; |
187 | 185 |
//Pointer to the map of reached status of the nodes. |
188 | 186 |
ReachedMap *_reached; |
189 | 187 |
//Indicates if _reached is locally allocated (true) or not. |
190 | 188 |
bool local_reached; |
191 | 189 |
//Pointer to the map of processed status of the nodes. |
192 | 190 |
ProcessedMap *_processed; |
193 | 191 |
//Indicates if _processed is locally allocated (true) or not. |
194 | 192 |
bool local_processed; |
195 | 193 |
|
196 | 194 |
std::vector<typename Digraph::OutArcIt> _stack; |
197 | 195 |
int _stack_head; |
198 | 196 |
|
199 |
///Creates the maps if necessary. |
|
200 |
///\todo Better memory allocation (instead of new). |
|
197 |
//Creates the maps if necessary. |
|
201 | 198 |
void create_maps() |
202 | 199 |
{ |
203 | 200 |
if(!_pred) { |
204 | 201 |
local_pred = true; |
205 | 202 |
_pred = Traits::createPredMap(*G); |
206 | 203 |
} |
207 | 204 |
if(!_dist) { |
208 | 205 |
local_dist = true; |
209 | 206 |
_dist = Traits::createDistMap(*G); |
210 | 207 |
} |
211 | 208 |
if(!_reached) { |
212 | 209 |
local_reached = true; |
213 | 210 |
_reached = Traits::createReachedMap(*G); |
214 | 211 |
} |
215 | 212 |
if(!_processed) { |
216 | 213 |
local_processed = true; |
217 | 214 |
_processed = Traits::createProcessedMap(*G); |
218 | 215 |
} |
219 | 216 |
} |
220 | 217 |
|
221 | 218 |
protected: |
222 | 219 |
|
223 | 220 |
Dfs() {} |
224 | 221 |
|
225 | 222 |
public: |
226 | 223 |
|
227 | 224 |
typedef Dfs Create; |
228 | 225 |
|
229 | 226 |
///\name Named template parameters |
230 | 227 |
|
231 | 228 |
///@{ |
232 | 229 |
|
233 | 230 |
template <class T> |
234 | 231 |
struct SetPredMapTraits : public Traits { |
235 | 232 |
typedef T PredMap; |
236 | 233 |
static PredMap *createPredMap(const Digraph &) |
237 | 234 |
{ |
238 | 235 |
throw UninitializedParameter(); |
239 | 236 |
} |
240 | 237 |
}; |
241 | 238 |
///\brief \ref named-templ-param "Named parameter" for setting |
242 | 239 |
///\ref PredMap type. |
243 | 240 |
/// |
244 | 241 |
///\ref named-templ-param "Named parameter" for setting |
245 | 242 |
///\ref PredMap type. |
246 | 243 |
template <class T> |
247 | 244 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
248 | 245 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
... | ... |
@@ -737,97 +734,96 @@ |
737 | 734 |
///distances of the nodes calculated by the algorithm. |
738 | 735 |
/// |
739 | 736 |
///\pre Either \ref run() or \ref init() |
740 | 737 |
///must be called before using this function. |
741 | 738 |
const DistMap &distMap() const { return *_dist;} |
742 | 739 |
|
743 | 740 |
///\brief Returns a const reference to the node map that stores the |
744 | 741 |
///predecessor arcs. |
745 | 742 |
/// |
746 | 743 |
///Returns a const reference to the node map that stores the predecessor |
747 | 744 |
///arcs, which form the DFS tree. |
748 | 745 |
/// |
749 | 746 |
///\pre Either \ref run() or \ref init() |
750 | 747 |
///must be called before using this function. |
751 | 748 |
const PredMap &predMap() const { return *_pred;} |
752 | 749 |
|
753 | 750 |
///Checks if a node is reachable from the root(s). |
754 | 751 |
|
755 | 752 |
///Returns \c true if \c v is reachable from the root(s). |
756 | 753 |
///\pre Either \ref run() or \ref start() |
757 | 754 |
///must be called before using this function. |
758 | 755 |
bool reached(Node v) const { return (*_reached)[v]; } |
759 | 756 |
|
760 | 757 |
///@} |
761 | 758 |
}; |
762 | 759 |
|
763 | 760 |
///Default traits class of dfs() function. |
764 | 761 |
|
765 | 762 |
///Default traits class of dfs() function. |
766 | 763 |
///\tparam GR Digraph type. |
767 | 764 |
template<class GR> |
768 | 765 |
struct DfsWizardDefaultTraits |
769 | 766 |
{ |
770 | 767 |
///The type of the digraph the algorithm runs on. |
771 | 768 |
typedef GR Digraph; |
772 | 769 |
|
773 | 770 |
///\brief The type of the map that stores the predecessor |
774 | 771 |
///arcs of the %DFS paths. |
775 | 772 |
/// |
776 | 773 |
///The type of the map that stores the predecessor |
777 | 774 |
///arcs of the %DFS paths. |
778 | 775 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
779 | 776 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
780 | 777 |
///Instantiates a \ref PredMap. |
781 | 778 |
|
782 | 779 |
///This function instantiates a \ref PredMap. |
783 | 780 |
///\param g is the digraph, to which we would like to define the |
784 | 781 |
///\ref PredMap. |
785 |
///\todo The digraph alone may be insufficient to initialize |
|
786 | 782 |
static PredMap *createPredMap(const Digraph &g) |
787 | 783 |
{ |
788 | 784 |
return new PredMap(g); |
789 | 785 |
} |
790 | 786 |
|
791 | 787 |
///The type of the map that indicates which nodes are processed. |
792 | 788 |
|
793 | 789 |
///The type of the map that indicates which nodes are processed. |
794 | 790 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
795 | 791 |
///By default it is a NullMap. |
796 | 792 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
797 | 793 |
///Instantiates a \ref ProcessedMap. |
798 | 794 |
|
799 | 795 |
///This function instantiates a \ref ProcessedMap. |
800 | 796 |
///\param g is the digraph, to which |
801 | 797 |
///we would like to define the \ref ProcessedMap. |
802 | 798 |
#ifdef DOXYGEN |
803 | 799 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
804 | 800 |
#else |
805 | 801 |
static ProcessedMap *createProcessedMap(const Digraph &) |
806 | 802 |
#endif |
807 | 803 |
{ |
808 | 804 |
return new ProcessedMap(); |
809 | 805 |
} |
810 | 806 |
|
811 | 807 |
///The type of the map that indicates which nodes are reached. |
812 | 808 |
|
813 | 809 |
///The type of the map that indicates which nodes are reached. |
814 | 810 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
815 | 811 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
816 | 812 |
///Instantiates a \ref ReachedMap. |
817 | 813 |
|
818 | 814 |
///This function instantiates a \ref ReachedMap. |
819 | 815 |
///\param g is the digraph, to which |
820 | 816 |
///we would like to define the \ref ReachedMap. |
821 | 817 |
static ReachedMap *createReachedMap(const Digraph &g) |
822 | 818 |
{ |
823 | 819 |
return new ReachedMap(g); |
824 | 820 |
} |
825 | 821 |
|
826 | 822 |
///The type of the map that stores the distances of the nodes. |
827 | 823 |
|
828 | 824 |
///The type of the map that stores the distances of the nodes. |
829 | 825 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
830 | 826 |
typedef typename Digraph::template NodeMap<int> DistMap; |
831 | 827 |
///Instantiates a \ref DistMap. |
832 | 828 |
|
833 | 829 |
///This function instantiates a \ref DistMap. |
... | ... |
@@ -1272,98 +1268,97 @@ |
1272 | 1268 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
1273 | 1269 |
#endif |
1274 | 1270 |
class DfsVisit { |
1275 | 1271 |
public: |
1276 | 1272 |
|
1277 | 1273 |
/// \brief \ref Exception for uninitialized parameters. |
1278 | 1274 |
/// |
1279 | 1275 |
/// This error represents problems in the initialization |
1280 | 1276 |
/// of the parameters of the algorithm. |
1281 | 1277 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1282 | 1278 |
public: |
1283 | 1279 |
virtual const char* what() const throw() |
1284 | 1280 |
{ |
1285 | 1281 |
return "lemon::DfsVisit::UninitializedParameter"; |
1286 | 1282 |
} |
1287 | 1283 |
}; |
1288 | 1284 |
|
1289 | 1285 |
///The traits class. |
1290 | 1286 |
typedef _Traits Traits; |
1291 | 1287 |
|
1292 | 1288 |
///The type of the digraph the algorithm runs on. |
1293 | 1289 |
typedef typename Traits::Digraph Digraph; |
1294 | 1290 |
|
1295 | 1291 |
///The visitor type used by the algorithm. |
1296 | 1292 |
typedef _Visitor Visitor; |
1297 | 1293 |
|
1298 | 1294 |
///The type of the map that indicates which nodes are reached. |
1299 | 1295 |
typedef typename Traits::ReachedMap ReachedMap; |
1300 | 1296 |
|
1301 | 1297 |
private: |
1302 | 1298 |
|
1303 | 1299 |
typedef typename Digraph::Node Node; |
1304 | 1300 |
typedef typename Digraph::NodeIt NodeIt; |
1305 | 1301 |
typedef typename Digraph::Arc Arc; |
1306 | 1302 |
typedef typename Digraph::OutArcIt OutArcIt; |
1307 | 1303 |
|
1308 | 1304 |
//Pointer to the underlying digraph. |
1309 | 1305 |
const Digraph *_digraph; |
1310 | 1306 |
//Pointer to the visitor object. |
1311 | 1307 |
Visitor *_visitor; |
1312 | 1308 |
//Pointer to the map of reached status of the nodes. |
1313 | 1309 |
ReachedMap *_reached; |
1314 | 1310 |
//Indicates if _reached is locally allocated (true) or not. |
1315 | 1311 |
bool local_reached; |
1316 | 1312 |
|
1317 | 1313 |
std::vector<typename Digraph::Arc> _stack; |
1318 | 1314 |
int _stack_head; |
1319 | 1315 |
|
1320 |
///Creates the maps if necessary. |
|
1321 |
///\todo Better memory allocation (instead of new). |
|
1316 |
//Creates the maps if necessary. |
|
1322 | 1317 |
void create_maps() { |
1323 | 1318 |
if(!_reached) { |
1324 | 1319 |
local_reached = true; |
1325 | 1320 |
_reached = Traits::createReachedMap(*_digraph); |
1326 | 1321 |
} |
1327 | 1322 |
} |
1328 | 1323 |
|
1329 | 1324 |
protected: |
1330 | 1325 |
|
1331 | 1326 |
DfsVisit() {} |
1332 | 1327 |
|
1333 | 1328 |
public: |
1334 | 1329 |
|
1335 | 1330 |
typedef DfsVisit Create; |
1336 | 1331 |
|
1337 | 1332 |
/// \name Named template parameters |
1338 | 1333 |
|
1339 | 1334 |
///@{ |
1340 | 1335 |
template <class T> |
1341 | 1336 |
struct SetReachedMapTraits : public Traits { |
1342 | 1337 |
typedef T ReachedMap; |
1343 | 1338 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1344 | 1339 |
throw UninitializedParameter(); |
1345 | 1340 |
} |
1346 | 1341 |
}; |
1347 | 1342 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1348 | 1343 |
/// ReachedMap type. |
1349 | 1344 |
/// |
1350 | 1345 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1351 | 1346 |
template <class T> |
1352 | 1347 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1353 | 1348 |
SetReachedMapTraits<T> > { |
1354 | 1349 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1355 | 1350 |
}; |
1356 | 1351 |
///@} |
1357 | 1352 |
|
1358 | 1353 |
public: |
1359 | 1354 |
|
1360 | 1355 |
/// \brief Constructor. |
1361 | 1356 |
/// |
1362 | 1357 |
/// Constructor. |
1363 | 1358 |
/// |
1364 | 1359 |
/// \param digraph The digraph the algorithm runs on. |
1365 | 1360 |
/// \param visitor The visitor object of the algorithm. |
1366 | 1361 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1367 | 1362 |
: _digraph(&digraph), _visitor(&visitor), |
1368 | 1363 |
_reached(0), local_reached(false) {} |
1369 | 1364 |
... | ... |
@@ -99,109 +99,106 @@ |
99 | 99 |
/// Operation traits for Dijkstra algorithm. |
100 | 100 |
|
101 | 101 |
/// This class defines the operations that are used in the algorithm. |
102 | 102 |
/// \see DijkstraDefaultOperationTraits |
103 | 103 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
104 | 104 |
|
105 | 105 |
/// The cross reference type used by the heap. |
106 | 106 |
|
107 | 107 |
/// The cross reference type used by the heap. |
108 | 108 |
/// Usually it is \c Digraph::NodeMap<int>. |
109 | 109 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
110 | 110 |
///Instantiates a \ref HeapCrossRef. |
111 | 111 |
|
112 | 112 |
///This function instantiates a \ref HeapCrossRef. |
113 | 113 |
/// \param g is the digraph, to which we would like to define the |
114 | 114 |
/// \ref HeapCrossRef. |
115 | 115 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
116 | 116 |
{ |
117 | 117 |
return new HeapCrossRef(g); |
118 | 118 |
} |
119 | 119 |
|
120 | 120 |
///The heap type used by the Dijkstra algorithm. |
121 | 121 |
|
122 | 122 |
///The heap type used by the Dijkstra algorithm. |
123 | 123 |
/// |
124 | 124 |
///\sa BinHeap |
125 | 125 |
///\sa Dijkstra |
126 | 126 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
127 | 127 |
///Instantiates a \ref Heap. |
128 | 128 |
|
129 | 129 |
///This function instantiates a \ref Heap. |
130 | 130 |
static Heap *createHeap(HeapCrossRef& r) |
131 | 131 |
{ |
132 | 132 |
return new Heap(r); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
///\brief The type of the map that stores the predecessor |
136 | 136 |
///arcs of the shortest paths. |
137 | 137 |
/// |
138 | 138 |
///The type of the map that stores the predecessor |
139 | 139 |
///arcs of the shortest paths. |
140 | 140 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
141 | 141 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
142 | 142 |
///Instantiates a \ref PredMap. |
143 | 143 |
|
144 | 144 |
///This function instantiates a \ref PredMap. |
145 | 145 |
///\param g is the digraph, to which we would like to define the |
146 | 146 |
///\ref PredMap. |
147 |
///\todo The digraph alone may be insufficient for the initialization |
|
148 | 147 |
static PredMap *createPredMap(const Digraph &g) |
149 | 148 |
{ |
150 | 149 |
return new PredMap(g); |
151 | 150 |
} |
152 | 151 |
|
153 | 152 |
///The type of the map that indicates which nodes are processed. |
154 | 153 |
|
155 | 154 |
///The type of the map that indicates which nodes are processed. |
156 | 155 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
157 | 156 |
///By default it is a NullMap. |
158 |
///\todo If it is set to a real map, |
|
159 |
///Dijkstra::processed() should read this. |
|
160 | 157 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
161 | 158 |
///Instantiates a \ref ProcessedMap. |
162 | 159 |
|
163 | 160 |
///This function instantiates a \ref ProcessedMap. |
164 | 161 |
///\param g is the digraph, to which |
165 | 162 |
///we would like to define the \ref ProcessedMap |
166 | 163 |
#ifdef DOXYGEN |
167 | 164 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
168 | 165 |
#else |
169 | 166 |
static ProcessedMap *createProcessedMap(const Digraph &) |
170 | 167 |
#endif |
171 | 168 |
{ |
172 | 169 |
return new ProcessedMap(); |
173 | 170 |
} |
174 | 171 |
|
175 | 172 |
///The type of the map that stores the distances of the nodes. |
176 | 173 |
|
177 | 174 |
///The type of the map that stores the distances of the nodes. |
178 | 175 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
179 | 176 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
180 | 177 |
///Instantiates a \ref DistMap. |
181 | 178 |
|
182 | 179 |
///This function instantiates a \ref DistMap. |
183 | 180 |
///\param g is the digraph, to which we would like to define |
184 | 181 |
///the \ref DistMap |
185 | 182 |
static DistMap *createDistMap(const Digraph &g) |
186 | 183 |
{ |
187 | 184 |
return new DistMap(g); |
188 | 185 |
} |
189 | 186 |
}; |
190 | 187 |
|
191 | 188 |
///%Dijkstra algorithm class. |
192 | 189 |
|
193 | 190 |
/// \ingroup shortest_path |
194 | 191 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
195 | 192 |
/// |
196 | 193 |
///The arc lengths are passed to the algorithm using a |
197 | 194 |
///\ref concepts::ReadMap "ReadMap", |
198 | 195 |
///so it is easy to change it to any kind of length. |
199 | 196 |
///The type of the length is determined by the |
200 | 197 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
201 | 198 |
///It is also possible to change the underlying priority heap. |
202 | 199 |
/// |
203 | 200 |
///There is also a \ref dijkstra() "function-type interface" for the |
204 | 201 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
205 | 202 |
///it can be used easier. |
206 | 203 |
/// |
207 | 204 |
///\tparam GR The type of the digraph the algorithm runs on. |
... | ... |
@@ -252,98 +249,97 @@ |
252 | 249 |
///The type of the map that stores the distances of the nodes. |
253 | 250 |
typedef typename TR::DistMap DistMap; |
254 | 251 |
///The type of the map that indicates which nodes are processed. |
255 | 252 |
typedef typename TR::ProcessedMap ProcessedMap; |
256 | 253 |
///The type of the paths. |
257 | 254 |
typedef PredMapPath<Digraph, PredMap> Path; |
258 | 255 |
///The cross reference type used for the current heap. |
259 | 256 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
260 | 257 |
///The heap type used by the algorithm. |
261 | 258 |
typedef typename TR::Heap Heap; |
262 | 259 |
///The operation traits class. |
263 | 260 |
typedef typename TR::OperationTraits OperationTraits; |
264 | 261 |
|
265 | 262 |
///The traits class. |
266 | 263 |
typedef TR Traits; |
267 | 264 |
|
268 | 265 |
private: |
269 | 266 |
|
270 | 267 |
typedef typename Digraph::Node Node; |
271 | 268 |
typedef typename Digraph::NodeIt NodeIt; |
272 | 269 |
typedef typename Digraph::Arc Arc; |
273 | 270 |
typedef typename Digraph::OutArcIt OutArcIt; |
274 | 271 |
|
275 | 272 |
//Pointer to the underlying digraph. |
276 | 273 |
const Digraph *G; |
277 | 274 |
//Pointer to the length map. |
278 | 275 |
const LengthMap *length; |
279 | 276 |
//Pointer to the map of predecessors arcs. |
280 | 277 |
PredMap *_pred; |
281 | 278 |
//Indicates if _pred is locally allocated (true) or not. |
282 | 279 |
bool local_pred; |
283 | 280 |
//Pointer to the map of distances. |
284 | 281 |
DistMap *_dist; |
285 | 282 |
//Indicates if _dist is locally allocated (true) or not. |
286 | 283 |
bool local_dist; |
287 | 284 |
//Pointer to the map of processed status of the nodes. |
288 | 285 |
ProcessedMap *_processed; |
289 | 286 |
//Indicates if _processed is locally allocated (true) or not. |
290 | 287 |
bool local_processed; |
291 | 288 |
//Pointer to the heap cross references. |
292 | 289 |
HeapCrossRef *_heap_cross_ref; |
293 | 290 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
294 | 291 |
bool local_heap_cross_ref; |
295 | 292 |
//Pointer to the heap. |
296 | 293 |
Heap *_heap; |
297 | 294 |
//Indicates if _heap is locally allocated (true) or not. |
298 | 295 |
bool local_heap; |
299 | 296 |
|
300 |
///Creates the maps if necessary. |
|
301 |
///\todo Better memory allocation (instead of new). |
|
297 |
//Creates the maps if necessary. |
|
302 | 298 |
void create_maps() |
303 | 299 |
{ |
304 | 300 |
if(!_pred) { |
305 | 301 |
local_pred = true; |
306 | 302 |
_pred = Traits::createPredMap(*G); |
307 | 303 |
} |
308 | 304 |
if(!_dist) { |
309 | 305 |
local_dist = true; |
310 | 306 |
_dist = Traits::createDistMap(*G); |
311 | 307 |
} |
312 | 308 |
if(!_processed) { |
313 | 309 |
local_processed = true; |
314 | 310 |
_processed = Traits::createProcessedMap(*G); |
315 | 311 |
} |
316 | 312 |
if (!_heap_cross_ref) { |
317 | 313 |
local_heap_cross_ref = true; |
318 | 314 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
319 | 315 |
} |
320 | 316 |
if (!_heap) { |
321 | 317 |
local_heap = true; |
322 | 318 |
_heap = Traits::createHeap(*_heap_cross_ref); |
323 | 319 |
} |
324 | 320 |
} |
325 | 321 |
|
326 | 322 |
public: |
327 | 323 |
|
328 | 324 |
typedef Dijkstra Create; |
329 | 325 |
|
330 | 326 |
///\name Named template parameters |
331 | 327 |
|
332 | 328 |
///@{ |
333 | 329 |
|
334 | 330 |
template <class T> |
335 | 331 |
struct SetPredMapTraits : public Traits { |
336 | 332 |
typedef T PredMap; |
337 | 333 |
static PredMap *createPredMap(const Digraph &) |
338 | 334 |
{ |
339 | 335 |
throw UninitializedParameter(); |
340 | 336 |
} |
341 | 337 |
}; |
342 | 338 |
///\brief \ref named-templ-param "Named parameter" for setting |
343 | 339 |
///\ref PredMap type. |
344 | 340 |
/// |
345 | 341 |
///\ref named-templ-param "Named parameter" for setting |
346 | 342 |
///\ref PredMap type. |
347 | 343 |
template <class T> |
348 | 344 |
struct SetPredMap |
349 | 345 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
... | ... |
@@ -920,192 +916,186 @@ |
920 | 916 |
|
921 | 917 |
///Returns the current distance of a node from the root(s). |
922 | 918 |
///It may be decreased in the following processes. |
923 | 919 |
///\pre Either \ref run() or \ref init() |
924 | 920 |
///must be called before using this function and |
925 | 921 |
///node \c v must be reached but not necessarily processed. |
926 | 922 |
Value currentDist(Node v) const { |
927 | 923 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
928 | 924 |
} |
929 | 925 |
|
930 | 926 |
///@} |
931 | 927 |
}; |
932 | 928 |
|
933 | 929 |
|
934 | 930 |
///Default traits class of dijkstra() function. |
935 | 931 |
|
936 | 932 |
///Default traits class of dijkstra() function. |
937 | 933 |
///\tparam GR The type of the digraph. |
938 | 934 |
///\tparam LM The type of the length map. |
939 | 935 |
template<class GR, class LM> |
940 | 936 |
struct DijkstraWizardDefaultTraits |
941 | 937 |
{ |
942 | 938 |
///The type of the digraph the algorithm runs on. |
943 | 939 |
typedef GR Digraph; |
944 | 940 |
///The type of the map that stores the arc lengths. |
945 | 941 |
|
946 | 942 |
///The type of the map that stores the arc lengths. |
947 | 943 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
948 | 944 |
typedef LM LengthMap; |
949 | 945 |
///The type of the length of the arcs. |
950 | 946 |
typedef typename LM::Value Value; |
951 | 947 |
|
952 | 948 |
/// Operation traits for Dijkstra algorithm. |
953 | 949 |
|
954 | 950 |
/// This class defines the operations that are used in the algorithm. |
955 | 951 |
/// \see DijkstraDefaultOperationTraits |
956 | 952 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
957 | 953 |
|
958 | 954 |
/// The cross reference type used by the heap. |
959 | 955 |
|
960 | 956 |
/// The cross reference type used by the heap. |
961 | 957 |
/// Usually it is \c Digraph::NodeMap<int>. |
962 | 958 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
963 | 959 |
///Instantiates a \ref HeapCrossRef. |
964 | 960 |
|
965 | 961 |
///This function instantiates a \ref HeapCrossRef. |
966 | 962 |
/// \param g is the digraph, to which we would like to define the |
967 | 963 |
/// HeapCrossRef. |
968 |
/// \todo The digraph alone may be insufficient for the initialization |
|
969 | 964 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
970 | 965 |
{ |
971 | 966 |
return new HeapCrossRef(g); |
972 | 967 |
} |
973 | 968 |
|
974 | 969 |
///The heap type used by the Dijkstra algorithm. |
975 | 970 |
|
976 | 971 |
///The heap type used by the Dijkstra algorithm. |
977 | 972 |
/// |
978 | 973 |
///\sa BinHeap |
979 | 974 |
///\sa Dijkstra |
980 | 975 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
981 | 976 |
std::less<Value> > Heap; |
982 | 977 |
|
983 | 978 |
///Instantiates a \ref Heap. |
984 | 979 |
|
985 | 980 |
///This function instantiates a \ref Heap. |
986 | 981 |
/// \param r is the HeapCrossRef which is used. |
987 | 982 |
static Heap *createHeap(HeapCrossRef& r) |
988 | 983 |
{ |
989 | 984 |
return new Heap(r); |
990 | 985 |
} |
991 | 986 |
|
992 | 987 |
///\brief The type of the map that stores the predecessor |
993 | 988 |
///arcs of the shortest paths. |
994 | 989 |
/// |
995 | 990 |
///The type of the map that stores the predecessor |
996 | 991 |
///arcs of the shortest paths. |
997 | 992 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
998 | 993 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
999 | 994 |
///Instantiates a \ref PredMap. |
1000 | 995 |
|
1001 | 996 |
///This function instantiates a \ref PredMap. |
1002 | 997 |
///\param g is the digraph, to which we would like to define the |
1003 | 998 |
///\ref PredMap. |
1004 |
///\todo The digraph alone may be insufficient to initialize |
|
1005 | 999 |
static PredMap *createPredMap(const Digraph &g) |
1006 | 1000 |
{ |
1007 | 1001 |
return new PredMap(g); |
1008 | 1002 |
} |
1009 | 1003 |
|
1010 | 1004 |
///The type of the map that indicates which nodes are processed. |
1011 | 1005 |
|
1012 | 1006 |
///The type of the map that indicates which nodes are processed. |
1013 | 1007 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1014 | 1008 |
///By default it is a NullMap. |
1015 |
///\todo If it is set to a real map, |
|
1016 |
///Dijkstra::processed() should read this. |
|
1017 |
///\todo named parameter to set this type, function to read and write. |
|
1018 | 1009 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1019 | 1010 |
///Instantiates a \ref ProcessedMap. |
1020 | 1011 |
|
1021 | 1012 |
///This function instantiates a \ref ProcessedMap. |
1022 | 1013 |
///\param g is the digraph, to which |
1023 | 1014 |
///we would like to define the \ref ProcessedMap. |
1024 | 1015 |
#ifdef DOXYGEN |
1025 | 1016 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1026 | 1017 |
#else |
1027 | 1018 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1028 | 1019 |
#endif |
1029 | 1020 |
{ |
1030 | 1021 |
return new ProcessedMap(); |
1031 | 1022 |
} |
1032 | 1023 |
|
1033 | 1024 |
///The type of the map that stores the distances of the nodes. |
1034 | 1025 |
|
1035 | 1026 |
///The type of the map that stores the distances of the nodes. |
1036 | 1027 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1037 | 1028 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
1038 | 1029 |
///Instantiates a \ref DistMap. |
1039 | 1030 |
|
1040 | 1031 |
///This function instantiates a \ref DistMap. |
1041 | 1032 |
///\param g is the digraph, to which we would like to define |
1042 | 1033 |
///the \ref DistMap |
1043 | 1034 |
static DistMap *createDistMap(const Digraph &g) |
1044 | 1035 |
{ |
1045 | 1036 |
return new DistMap(g); |
1046 | 1037 |
} |
1047 | 1038 |
|
1048 | 1039 |
///The type of the shortest paths. |
1049 | 1040 |
|
1050 | 1041 |
///The type of the shortest paths. |
1051 | 1042 |
///It must meet the \ref concepts::Path "Path" concept. |
1052 | 1043 |
typedef lemon::Path<Digraph> Path; |
1053 | 1044 |
}; |
1054 | 1045 |
|
1055 | 1046 |
/// Default traits class used by \ref DijkstraWizard |
1056 | 1047 |
|
1057 | 1048 |
/// To make it easier to use Dijkstra algorithm |
1058 | 1049 |
/// we have created a wizard class. |
1059 | 1050 |
/// This \ref DijkstraWizard class needs default traits, |
1060 | 1051 |
/// as well as the \ref Dijkstra class. |
1061 | 1052 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1062 | 1053 |
/// \ref DijkstraWizard class. |
1063 |
/// \todo More named parameters are required... |
|
1064 | 1054 |
template<class GR,class LM> |
1065 | 1055 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1066 | 1056 |
{ |
1067 | 1057 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1068 | 1058 |
protected: |
1069 | 1059 |
//The type of the nodes in the digraph. |
1070 | 1060 |
typedef typename Base::Digraph::Node Node; |
1071 | 1061 |
|
1072 | 1062 |
//Pointer to the digraph the algorithm runs on. |
1073 | 1063 |
void *_g; |
1074 | 1064 |
//Pointer to the length map. |
1075 | 1065 |
void *_length; |
1076 | 1066 |
//Pointer to the map of processed nodes. |
1077 | 1067 |
void *_processed; |
1078 | 1068 |
//Pointer to the map of predecessors arcs. |
1079 | 1069 |
void *_pred; |
1080 | 1070 |
//Pointer to the map of distances. |
1081 | 1071 |
void *_dist; |
1082 | 1072 |
//Pointer to the shortest path to the target node. |
1083 | 1073 |
void *_path; |
1084 | 1074 |
//Pointer to the distance of the target node. |
1085 | 1075 |
void *_di; |
1086 | 1076 |
|
1087 | 1077 |
public: |
1088 | 1078 |
/// Constructor. |
1089 | 1079 |
|
1090 | 1080 |
/// This constructor does not require parameters, therefore it initiates |
1091 | 1081 |
/// all of the attributes to \c 0. |
1092 | 1082 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1093 | 1083 |
_dist(0), _path(0), _di(0) {} |
1094 | 1084 |
|
1095 | 1085 |
/// Constructor. |
1096 | 1086 |
|
1097 | 1087 |
/// This constructor requires two parameters, |
1098 | 1088 |
/// others are initiated to \c 0. |
1099 | 1089 |
/// \param g The digraph the algorithm runs on. |
1100 | 1090 |
/// \param l The length map. |
1101 | 1091 |
DijkstraWizardBase(const GR &g,const LM &l) : |
1102 | 1092 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1103 | 1093 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1104 | 1094 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1105 | 1095 |
|
1106 | 1096 |
}; |
1107 | 1097 |
|
1108 | 1098 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1109 | 1099 |
|
1110 | 1100 |
/// This auxiliary class is created to implement the |
1111 | 1101 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
... | ... |
@@ -57,98 +57,96 @@ |
57 | 57 |
} catch (...) {} |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
ExceptionMember(const ExceptionMember& copy) throw() { |
61 | 61 |
try { |
62 | 62 |
if (!copy.valid()) return; |
63 | 63 |
ptr.reset(new Type()); |
64 | 64 |
if (ptr.get() == 0) return; |
65 | 65 |
*ptr = copy.get(); |
66 | 66 |
} catch (...) {} |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
ExceptionMember& operator=(const ExceptionMember& copy) throw() { |
70 | 70 |
if (ptr.get() == 0) return; |
71 | 71 |
try { |
72 | 72 |
if (!copy.valid()) return; |
73 | 73 |
*ptr = copy.get(); |
74 | 74 |
} catch (...) {} |
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
void set(const Type& type) throw() { |
78 | 78 |
if (ptr.get() == 0) return; |
79 | 79 |
try { |
80 | 80 |
*ptr = type; |
81 | 81 |
} catch (...) {} |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
const Type& get() const { |
85 | 85 |
return *ptr; |
86 | 86 |
} |
87 | 87 |
|
88 | 88 |
bool valid() const throw() { |
89 | 89 |
return ptr.get() != 0; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
private: |
93 | 93 |
std::auto_ptr<_Type> ptr; |
94 | 94 |
}; |
95 | 95 |
|
96 | 96 |
/// Exception-safe convenient error message builder class. |
97 | 97 |
|
98 | 98 |
/// Helper class which provides a convenient ostream-like (operator << |
99 | 99 |
/// based) interface to create a string message. Mostly useful in |
100 | 100 |
/// exception classes (therefore the name). |
101 | 101 |
class ErrorMessage { |
102 | 102 |
protected: |
103 | 103 |
///\e |
104 | 104 |
|
105 |
///\todo The good solution is boost::shared_ptr... |
|
106 |
/// |
|
107 | 105 |
mutable std::auto_ptr<std::ostringstream> buf; |
108 | 106 |
|
109 | 107 |
///\e |
110 | 108 |
bool init() throw() { |
111 | 109 |
try { |
112 | 110 |
buf.reset(new std::ostringstream); |
113 | 111 |
} |
114 | 112 |
catch(...) { |
115 | 113 |
buf.reset(); |
116 | 114 |
} |
117 | 115 |
return buf.get(); |
118 | 116 |
} |
119 | 117 |
|
120 | 118 |
public: |
121 | 119 |
|
122 | 120 |
///\e |
123 | 121 |
ErrorMessage() throw() { init(); } |
124 | 122 |
|
125 | 123 |
ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { } |
126 | 124 |
|
127 | 125 |
///\e |
128 | 126 |
ErrorMessage(const char *msg) throw() { |
129 | 127 |
init(); |
130 | 128 |
*this << msg; |
131 | 129 |
} |
132 | 130 |
|
133 | 131 |
///\e |
134 | 132 |
ErrorMessage(const std::string &msg) throw() { |
135 | 133 |
init(); |
136 | 134 |
*this << msg; |
137 | 135 |
} |
138 | 136 |
|
139 | 137 |
///\e |
140 | 138 |
template <typename T> |
141 | 139 |
ErrorMessage& operator<<(const T &t) throw() { |
142 | 140 |
if( ! buf.get() ) return *this; |
143 | 141 |
|
144 | 142 |
try { |
145 | 143 |
*buf << t; |
146 | 144 |
} |
147 | 145 |
catch(...) { |
148 | 146 |
buf.reset(); |
149 | 147 |
} |
150 | 148 |
return *this; |
151 | 149 |
} |
152 | 150 |
|
153 | 151 |
///\e |
154 | 152 |
const char* message() throw() { |
... | ... |
@@ -621,148 +621,145 @@ |
621 | 621 |
|
622 | 622 |
///Sets whether the graph is directed. |
623 | 623 |
///Use it to show the edges as a pair of directed ones. |
624 | 624 |
/// |
625 | 625 |
///This setting is the default for digraphs. |
626 | 626 |
/// |
627 | 627 |
///\sa undirected() |
628 | 628 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
629 | 629 |
|
630 | 630 |
///Sets the title. |
631 | 631 |
|
632 | 632 |
///Sets the title of the generated image, |
633 | 633 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
634 | 634 |
///the EPS file. |
635 | 635 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
636 | 636 |
///Sets the copyright statement. |
637 | 637 |
|
638 | 638 |
///Sets the copyright statement of the generated image, |
639 | 639 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
640 | 640 |
///the EPS file. |
641 | 641 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
642 | 642 |
|
643 | 643 |
protected: |
644 | 644 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
645 | 645 |
{ |
646 | 646 |
switch(t) { |
647 | 647 |
case CIRCLE: |
648 | 648 |
case MALE: |
649 | 649 |
case FEMALE: |
650 | 650 |
return p.normSquare()<=r*r; |
651 | 651 |
case SQUARE: |
652 | 652 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
653 | 653 |
case DIAMOND: |
654 | 654 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
655 | 655 |
} |
656 | 656 |
return false; |
657 | 657 |
} |
658 | 658 |
|
659 | 659 |
public: |
660 | 660 |
~GraphToEps() { } |
661 | 661 |
|
662 | 662 |
///Draws the graph. |
663 | 663 |
|
664 | 664 |
///Like other functions using |
665 | 665 |
///\ref named-templ-func-param "named template parameters", |
666 | 666 |
///this function calls the algorithm itself, i.e. in this case |
667 | 667 |
///it draws the graph. |
668 | 668 |
void run() { |
669 |
//\todo better 'epsilon' would be nice here. |
|
670 | 669 |
const double EPSILON=1e-9; |
671 | 670 |
if(dontPrint) return; |
672 | 671 |
|
673 | 672 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
674 | 673 |
mycoords(_coords,_negY); |
675 | 674 |
|
676 | 675 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
677 | 676 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
678 | 677 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
679 | 678 |
os << "%%Creator: LEMON, graphToEps()\n"; |
680 | 679 |
|
681 | 680 |
{ |
682 | 681 |
#ifndef WIN32 |
683 | 682 |
timeval tv; |
684 | 683 |
gettimeofday(&tv, 0); |
685 | 684 |
|
686 | 685 |
char cbuf[26]; |
687 | 686 |
ctime_r(&tv.tv_sec,cbuf); |
688 | 687 |
os << "%%CreationDate: " << cbuf; |
689 | 688 |
#else |
690 | 689 |
SYSTEMTIME time; |
691 | 690 |
char buf1[11], buf2[9], buf3[5]; |
692 | 691 |
|
693 | 692 |
GetSystemTime(&time); |
694 | 693 |
if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
695 | 694 |
"ddd MMM dd", buf1, 11) && |
696 | 695 |
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time, |
697 | 696 |
"HH':'mm':'ss", buf2, 9) && |
698 | 697 |
GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
699 | 698 |
"yyyy", buf3, 5)) { |
700 | 699 |
os << "%%CreationDate: " << buf1 << ' ' |
701 | 700 |
<< buf2 << ' ' << buf3 << std::endl; |
702 | 701 |
} |
703 | 702 |
#endif |
704 | 703 |
} |
705 | 704 |
|
706 | 705 |
if (_autoArcWidthScale) { |
707 | 706 |
double max_w=0; |
708 | 707 |
for(ArcIt e(g);e!=INVALID;++e) |
709 | 708 |
max_w=std::max(double(_arcWidths[e]),max_w); |
710 |
//\todo better 'epsilon' would be nice here. |
|
711 | 709 |
if(max_w>EPSILON) { |
712 | 710 |
_arcWidthScale/=max_w; |
713 | 711 |
} |
714 | 712 |
} |
715 | 713 |
|
716 | 714 |
if (_autoNodeScale) { |
717 | 715 |
double max_s=0; |
718 | 716 |
for(NodeIt n(g);n!=INVALID;++n) |
719 | 717 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
720 |
//\todo better 'epsilon' would be nice here. |
|
721 | 718 |
if(max_s>EPSILON) { |
722 | 719 |
_nodeScale/=max_s; |
723 | 720 |
} |
724 | 721 |
} |
725 | 722 |
|
726 | 723 |
double diag_len = 1; |
727 | 724 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
728 | 725 |
dim2::Box<double> bb; |
729 | 726 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
730 | 727 |
if (bb.empty()) { |
731 | 728 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
732 | 729 |
} |
733 | 730 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
734 | 731 |
if(diag_len<EPSILON) diag_len = 1; |
735 | 732 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
736 | 733 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
737 | 734 |
} |
738 | 735 |
|
739 | 736 |
dim2::Box<double> bb; |
740 | 737 |
for(NodeIt n(g);n!=INVALID;++n) { |
741 | 738 |
double ns=_nodeSizes[n]*_nodeScale; |
742 | 739 |
dim2::Point<double> p(ns,ns); |
743 | 740 |
switch(_nodeShapes[n]) { |
744 | 741 |
case CIRCLE: |
745 | 742 |
case SQUARE: |
746 | 743 |
case DIAMOND: |
747 | 744 |
bb.add(p+mycoords[n]); |
748 | 745 |
bb.add(-p+mycoords[n]); |
749 | 746 |
break; |
750 | 747 |
case MALE: |
751 | 748 |
bb.add(-p+mycoords[n]); |
752 | 749 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
753 | 750 |
break; |
754 | 751 |
case FEMALE: |
755 | 752 |
bb.add(p+mycoords[n]); |
756 | 753 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
757 | 754 |
break; |
758 | 755 |
} |
759 | 756 |
} |
760 | 757 |
if (bb.empty()) { |
761 | 758 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
762 | 759 |
} |
763 | 760 |
|
764 | 761 |
if(_scaleToA4) |
765 | 762 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
766 | 763 |
else { |
767 | 764 |
if(_preScale) { |
768 | 765 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
... | ... |
@@ -828,130 +825,128 @@ |
828 | 825 |
<< " newpath 5 index 5 index moveto\n" |
829 | 826 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
830 | 827 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
831 | 828 |
<< " 1 index 1 index lineto\n" |
832 | 829 |
<< " 1 index 1 index 7 index sub moveto\n" |
833 | 830 |
<< " 1 index 1 index lineto\n" |
834 | 831 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
835 | 832 |
<< " lineto\n" |
836 | 833 |
<< " stroke\n" |
837 | 834 |
<< " 5 index 5 index 5 index c fill\n" |
838 | 835 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
839 | 836 |
<< " } bind def\n"; |
840 | 837 |
|
841 | 838 |
|
842 | 839 |
os << "/arrl " << _arrowLength << " def\n"; |
843 | 840 |
os << "/arrw " << _arrowWidth << " def\n"; |
844 | 841 |
// l dx_norm dy_norm |
845 | 842 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
846 | 843 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
847 | 844 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx " |
848 | 845 |
<< "exch def\n" |
849 | 846 |
<< " /w exch def /len exch def\n" |
850 | 847 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
851 | 848 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
852 | 849 |
<< " len w sub arrl sub dx dy lrl\n" |
853 | 850 |
<< " arrw dy dx neg lrl\n" |
854 | 851 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
855 | 852 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
856 | 853 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
857 | 854 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
858 | 855 |
<< " arrw dy dx neg lrl\n" |
859 | 856 |
<< " len w sub arrl sub neg dx dy lrl\n" |
860 | 857 |
<< " closepath fill } bind def\n"; |
861 | 858 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
862 | 859 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
863 | 860 |
|
864 | 861 |
os << "\ngsave\n"; |
865 | 862 |
if(_scaleToA4) |
866 | 863 |
if(bb.height()>bb.width()) { |
867 | 864 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
868 | 865 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
869 | 866 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
870 | 867 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
871 | 868 |
<< " translate\n" |
872 | 869 |
<< sc << " dup scale\n" |
873 | 870 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
874 | 871 |
} |
875 | 872 |
else { |
876 |
//\todo Verify centering |
|
877 | 873 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
878 | 874 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
879 | 875 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
880 | 876 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
881 | 877 |
<< " translate\n" |
882 | 878 |
<< sc << " dup scale\n90 rotate\n" |
883 | 879 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
884 | 880 |
} |
885 | 881 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
886 | 882 |
|
887 | 883 |
if(_showArcs) { |
888 | 884 |
os << "%Arcs:\ngsave\n"; |
889 | 885 |
if(_enableParallel) { |
890 | 886 |
std::vector<Arc> el; |
891 | 887 |
for(ArcIt e(g);e!=INVALID;++e) |
892 | 888 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
893 | 889 |
&&g.source(e)!=g.target(e)) |
894 | 890 |
el.push_back(e); |
895 | 891 |
std::sort(el.begin(),el.end(),arcLess(g)); |
896 | 892 |
|
897 | 893 |
typename std::vector<Arc>::iterator j; |
898 | 894 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
899 | 895 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
900 | 896 |
|
901 | 897 |
double sw=0; |
902 | 898 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
903 | 899 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
904 | 900 |
sw-=_parArcDist; |
905 | 901 |
sw/=-2.0; |
906 | 902 |
dim2::Point<double> |
907 | 903 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
908 | 904 |
double l=std::sqrt(dvec.normSquare()); |
909 |
//\todo better 'epsilon' would be nice here. |
|
910 | 905 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
911 | 906 |
dim2::Point<double> m; |
912 | 907 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
913 | 908 |
// mycoords[g.source(*i)])/2.0; |
914 | 909 |
|
915 | 910 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
916 | 911 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
917 | 912 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
918 | 913 |
|
919 | 914 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
920 | 915 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
921 | 916 |
|
922 | 917 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
923 | 918 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
924 | 919 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
925 | 920 |
if(_drawArrows) { |
926 | 921 |
int node_shape; |
927 | 922 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
928 | 923 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
929 | 924 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
930 | 925 |
node_shape=_nodeShapes[g.target(*e)]; |
931 | 926 |
dim2::Bezier3 bez(s,mm,mm,t); |
932 | 927 |
double t1=0,t2=1; |
933 | 928 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
934 | 929 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
935 | 930 |
else t1=(t1+t2)/2; |
936 | 931 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
937 | 932 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
938 | 933 |
rn*=rn; |
939 | 934 |
t2=(t1+t2)/2;t1=0; |
940 | 935 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
941 | 936 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
942 | 937 |
else t2=(t1+t2)/2; |
943 | 938 |
dim2::Point<double> linend=bez((t1+t2)/2); |
944 | 939 |
bez=bez.before((t1+t2)/2); |
945 | 940 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
946 | 941 |
// node_shape=_nodeShapes[g.source(*e)]; |
947 | 942 |
// t1=0;t2=1; |
948 | 943 |
// for(int i=0;i<INTERPOL_PREC;++i) |
949 | 944 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
950 | 945 |
// t1=(t1+t2)/2; |
951 | 946 |
// else t2=(t1+t2)/2; |
952 | 947 |
// bez=bez.after((t1+t2)/2); |
953 | 948 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
954 | 949 |
<< _arcColors[*e].red() << ' ' |
955 | 950 |
<< _arcColors[*e].green() << ' ' |
956 | 951 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
957 | 952 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
... | ... |
@@ -456,100 +456,98 @@ |
456 | 456 |
/// \sa reserveNode |
457 | 457 |
void reserveArc(int m) { arcs.reserve(m); }; |
458 | 458 |
|
459 | 459 |
///Contract two nodes. |
460 | 460 |
|
461 | 461 |
///This function contracts two nodes. |
462 | 462 |
///Node \p b will be removed but instead of deleting |
463 | 463 |
///incident arcs, they will be joined to \p a. |
464 | 464 |
///The last parameter \p r controls whether to remove loops. \c true |
465 | 465 |
///means that loops will be removed. |
466 | 466 |
/// |
467 | 467 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
468 | 468 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s |
469 | 469 |
///may be invalidated. |
470 | 470 |
/// |
471 | 471 |
///\warning This functionality cannot be used together with the Snapshot |
472 | 472 |
///feature. |
473 | 473 |
void contract(Node a, Node b, bool r = true) |
474 | 474 |
{ |
475 | 475 |
for(OutArcIt e(*this,b);e!=INVALID;) { |
476 | 476 |
OutArcIt f=e; |
477 | 477 |
++f; |
478 | 478 |
if(r && target(e)==a) erase(e); |
479 | 479 |
else changeSource(e,a); |
480 | 480 |
e=f; |
481 | 481 |
} |
482 | 482 |
for(InArcIt e(*this,b);e!=INVALID;) { |
483 | 483 |
InArcIt f=e; |
484 | 484 |
++f; |
485 | 485 |
if(r && source(e)==a) erase(e); |
486 | 486 |
else changeTarget(e,a); |
487 | 487 |
e=f; |
488 | 488 |
} |
489 | 489 |
erase(b); |
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
///Split a node. |
493 | 493 |
|
494 | 494 |
///This function splits a node. First a new node is added to the digraph, |
495 | 495 |
///then the source of each outgoing arc of \c n is moved to this new node. |
496 | 496 |
///If \c connect is \c true (this is the default value), then a new arc |
497 | 497 |
///from \c n to the newly created node is also added. |
498 | 498 |
///\return The newly created node. |
499 | 499 |
/// |
500 | 500 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
501 | 501 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may |
502 | 502 |
///be invalidated. |
503 | 503 |
/// |
504 |
///\warning This functionality cannot be used |
|
504 |
///\warning This functionality cannot be used in conjunction with the |
|
505 | 505 |
///Snapshot feature. |
506 |
/// |
|
507 |
///\todo It could be implemented in a bit faster way. |
|
508 | 506 |
Node split(Node n, bool connect = true) { |
509 | 507 |
Node b = addNode(); |
510 | 508 |
for(OutArcIt e(*this,n);e!=INVALID;) { |
511 | 509 |
OutArcIt f=e; |
512 | 510 |
++f; |
513 | 511 |
changeSource(e,b); |
514 | 512 |
e=f; |
515 | 513 |
} |
516 | 514 |
if (connect) addArc(n,b); |
517 | 515 |
return b; |
518 | 516 |
} |
519 | 517 |
|
520 | 518 |
///Split an arc. |
521 | 519 |
|
522 | 520 |
///This function splits an arc. First a new node \c b is added to |
523 | 521 |
///the digraph, then the original arc is re-targeted to \c |
524 | 522 |
///b. Finally an arc from \c b to the original target is added. |
525 | 523 |
/// |
526 | 524 |
///\return The newly created node. |
527 | 525 |
/// |
528 | 526 |
///\warning This functionality cannot be used together with the |
529 | 527 |
///Snapshot feature. |
530 | 528 |
Node split(Arc e) { |
531 | 529 |
Node b = addNode(); |
532 | 530 |
addArc(b,target(e)); |
533 | 531 |
changeTarget(e,b); |
534 | 532 |
return b; |
535 | 533 |
} |
536 | 534 |
|
537 | 535 |
/// \brief Class to make a snapshot of the digraph and restore |
538 | 536 |
/// it later. |
539 | 537 |
/// |
540 | 538 |
/// Class to make a snapshot of the digraph and restore it later. |
541 | 539 |
/// |
542 | 540 |
/// The newly added nodes and arcs can be removed using the |
543 | 541 |
/// restore() function. |
544 | 542 |
/// |
545 | 543 |
/// \warning Arc and node deletions and other modifications (e.g. |
546 | 544 |
/// contracting, splitting, reversing arcs or nodes) cannot be |
547 | 545 |
/// restored. These events invalidate the snapshot. |
548 | 546 |
class Snapshot { |
549 | 547 |
protected: |
550 | 548 |
|
551 | 549 |
typedef Parent::NodeNotifier NodeNotifier; |
552 | 550 |
|
553 | 551 |
class NodeObserverProxy : public NodeNotifier::ObserverBase { |
554 | 552 |
public: |
555 | 553 |
... | ... |
@@ -439,154 +439,150 @@ |
439 | 439 |
/// default value. |
440 | 440 |
/// \relates SparseMap |
441 | 441 |
template<typename K, typename V, typename Compare> |
442 | 442 |
inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) { |
443 | 443 |
return SparseMap<K, V, Compare>(value); |
444 | 444 |
} |
445 | 445 |
|
446 | 446 |
template<typename K, typename V> |
447 | 447 |
inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) { |
448 | 448 |
return SparseMap<K, V, std::less<K> >(value); |
449 | 449 |
} |
450 | 450 |
|
451 | 451 |
/// \brief Returns a \ref SparseMap class created from an appropriate |
452 | 452 |
/// \c std::map |
453 | 453 |
|
454 | 454 |
/// This function just returns a \ref SparseMap class created from an |
455 | 455 |
/// appropriate \c std::map. |
456 | 456 |
/// \relates SparseMap |
457 | 457 |
template<typename K, typename V, typename Compare> |
458 | 458 |
inline SparseMap<K, V, Compare> |
459 | 459 |
sparseMap(const std::map<K, V, Compare> &map, const V& value = V()) |
460 | 460 |
{ |
461 | 461 |
return SparseMap<K, V, Compare>(map, value); |
462 | 462 |
} |
463 | 463 |
|
464 | 464 |
/// @} |
465 | 465 |
|
466 | 466 |
/// \addtogroup map_adaptors |
467 | 467 |
/// @{ |
468 | 468 |
|
469 | 469 |
/// Composition of two maps |
470 | 470 |
|
471 | 471 |
/// This \ref concepts::ReadMap "read-only map" returns the |
472 | 472 |
/// composition of two given maps. That is to say, if \c m1 is of |
473 | 473 |
/// type \c M1 and \c m2 is of \c M2, then for |
474 | 474 |
/// \code |
475 | 475 |
/// ComposeMap<M1, M2> cm(m1,m2); |
476 | 476 |
/// \endcode |
477 | 477 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
478 | 478 |
/// |
479 | 479 |
/// The \c Key type of the map is inherited from \c M2 and the |
480 | 480 |
/// \c Value type is from \c M1. |
481 | 481 |
/// \c M2::Value must be convertible to \c M1::Key. |
482 | 482 |
/// |
483 | 483 |
/// The simplest way of using this map is through the composeMap() |
484 | 484 |
/// function. |
485 | 485 |
/// |
486 | 486 |
/// \sa CombineMap |
487 |
/// |
|
488 |
/// \todo Check the requirements. |
|
489 | 487 |
template <typename M1, typename M2> |
490 | 488 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
491 | 489 |
const M1 &_m1; |
492 | 490 |
const M2 &_m2; |
493 | 491 |
public: |
494 | 492 |
typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
495 | 493 |
typedef typename Parent::Key Key; |
496 | 494 |
typedef typename Parent::Value Value; |
497 | 495 |
|
498 | 496 |
/// Constructor |
499 | 497 |
ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
500 | 498 |
|
501 | 499 |
/// \e |
502 | 500 |
typename MapTraits<M1>::ConstReturnValue |
503 | 501 |
operator[](const Key &k) const { return _m1[_m2[k]]; } |
504 | 502 |
}; |
505 | 503 |
|
506 | 504 |
/// Returns a \ref ComposeMap class |
507 | 505 |
|
508 | 506 |
/// This function just returns a \ref ComposeMap class. |
509 | 507 |
/// |
510 | 508 |
/// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is |
511 | 509 |
/// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt> |
512 | 510 |
/// will be equal to <tt>m1[m2[x]]</tt>. |
513 | 511 |
/// |
514 | 512 |
/// \relates ComposeMap |
515 | 513 |
template <typename M1, typename M2> |
516 | 514 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) { |
517 | 515 |
return ComposeMap<M1, M2>(m1, m2); |
518 | 516 |
} |
519 | 517 |
|
520 | 518 |
|
521 | 519 |
/// Combination of two maps using an STL (binary) functor. |
522 | 520 |
|
523 | 521 |
/// This \ref concepts::ReadMap "read-only map" takes two maps and a |
524 | 522 |
/// binary functor and returns the combination of the two given maps |
525 | 523 |
/// using the functor. |
526 | 524 |
/// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2 |
527 | 525 |
/// and \c f is of \c F, then for |
528 | 526 |
/// \code |
529 | 527 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
530 | 528 |
/// \endcode |
531 | 529 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>. |
532 | 530 |
/// |
533 | 531 |
/// The \c Key type of the map is inherited from \c M1 (\c M1::Key |
534 | 532 |
/// must be convertible to \c M2::Key) and the \c Value type is \c V. |
535 | 533 |
/// \c M2::Value and \c M1::Value must be convertible to the |
536 | 534 |
/// corresponding input parameter of \c F and the return type of \c F |
537 | 535 |
/// must be convertible to \c V. |
538 | 536 |
/// |
539 | 537 |
/// The simplest way of using this map is through the combineMap() |
540 | 538 |
/// function. |
541 | 539 |
/// |
542 | 540 |
/// \sa ComposeMap |
543 |
/// |
|
544 |
/// \todo Check the requirements. |
|
545 | 541 |
template<typename M1, typename M2, typename F, |
546 | 542 |
typename V = typename F::result_type> |
547 | 543 |
class CombineMap : public MapBase<typename M1::Key, V> { |
548 | 544 |
const M1 &_m1; |
549 | 545 |
const M2 &_m2; |
550 | 546 |
F _f; |
551 | 547 |
public: |
552 | 548 |
typedef MapBase<typename M1::Key, V> Parent; |
553 | 549 |
typedef typename Parent::Key Key; |
554 | 550 |
typedef typename Parent::Value Value; |
555 | 551 |
|
556 | 552 |
/// Constructor |
557 | 553 |
CombineMap(const M1 &m1, const M2 &m2, const F &f = F()) |
558 | 554 |
: _m1(m1), _m2(m2), _f(f) {} |
559 | 555 |
/// \e |
560 | 556 |
Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); } |
561 | 557 |
}; |
562 | 558 |
|
563 | 559 |
/// Returns a \ref CombineMap class |
564 | 560 |
|
565 | 561 |
/// This function just returns a \ref CombineMap class. |
566 | 562 |
/// |
567 | 563 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
568 | 564 |
/// values, then |
569 | 565 |
/// \code |
570 | 566 |
/// combineMap(m1,m2,std::plus<double>()) |
571 | 567 |
/// \endcode |
572 | 568 |
/// is equivalent to |
573 | 569 |
/// \code |
574 | 570 |
/// addMap(m1,m2) |
575 | 571 |
/// \endcode |
576 | 572 |
/// |
577 | 573 |
/// This function is specialized for adaptable binary function |
578 | 574 |
/// classes and C++ functions. |
579 | 575 |
/// |
580 | 576 |
/// \relates CombineMap |
581 | 577 |
template<typename M1, typename M2, typename F, typename V> |
582 | 578 |
inline CombineMap<M1, M2, F, V> |
583 | 579 |
combineMap(const M1 &m1, const M2 &m2, const F &f) { |
584 | 580 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
585 | 581 |
} |
586 | 582 |
|
587 | 583 |
template<typename M1, typename M2, typename F> |
588 | 584 |
inline CombineMap<M1, M2, F, typename F::result_type> |
589 | 585 |
combineMap(const M1 &m1, const M2 &m2, const F &f) { |
590 | 586 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
591 | 587 |
} |
592 | 588 |
... | ... |
@@ -776,97 +776,96 @@ |
776 | 776 |
unsigned int uinteger() { |
777 | 777 |
return uinteger<unsigned int>(); |
778 | 778 |
} |
779 | 779 |
|
780 | 780 |
/// \brief Returns a random integer |
781 | 781 |
/// |
782 | 782 |
/// It returns a random integer uniformly from the whole range of |
783 | 783 |
/// the current \c Number type. The default result type of this |
784 | 784 |
/// function is \c int. |
785 | 785 |
template <typename Number> |
786 | 786 |
Number integer() { |
787 | 787 |
static const int nb = std::numeric_limits<Number>::digits + |
788 | 788 |
(std::numeric_limits<Number>::is_signed ? 1 : 0); |
789 | 789 |
return _random_bits::IntConversion<Number, Word, nb>::convert(core); |
790 | 790 |
} |
791 | 791 |
|
792 | 792 |
int integer() { |
793 | 793 |
return integer<int>(); |
794 | 794 |
} |
795 | 795 |
|
796 | 796 |
/// \brief Returns a random bool |
797 | 797 |
/// |
798 | 798 |
/// It returns a random bool. The generator holds a buffer for |
799 | 799 |
/// random bits. Every time when it become empty the generator makes |
800 | 800 |
/// a new random word and fill the buffer up. |
801 | 801 |
bool boolean() { |
802 | 802 |
return bool_producer.convert(core); |
803 | 803 |
} |
804 | 804 |
|
805 | 805 |
/// @} |
806 | 806 |
|
807 | 807 |
///\name Non-uniform distributions |
808 | 808 |
/// |
809 | 809 |
|
810 | 810 |
///@{ |
811 | 811 |
|
812 | 812 |
/// \brief Returns a random bool |
813 | 813 |
/// |
814 | 814 |
/// It returns a random bool with given probability of true result. |
815 | 815 |
bool boolean(double p) { |
816 | 816 |
return operator()() < p; |
817 | 817 |
} |
818 | 818 |
|
819 | 819 |
/// Standard Gauss distribution |
820 | 820 |
|
821 | 821 |
/// Standard Gauss distribution. |
822 | 822 |
/// \note The Cartesian form of the Box-Muller |
823 | 823 |
/// transformation is used to generate a random normal distribution. |
824 |
/// \todo Consider using the "ziggurat" method instead. |
|
825 | 824 |
double gauss() |
826 | 825 |
{ |
827 | 826 |
double V1,V2,S; |
828 | 827 |
do { |
829 | 828 |
V1=2*real<double>()-1; |
830 | 829 |
V2=2*real<double>()-1; |
831 | 830 |
S=V1*V1+V2*V2; |
832 | 831 |
} while(S>=1); |
833 | 832 |
return std::sqrt(-2*std::log(S)/S)*V1; |
834 | 833 |
} |
835 | 834 |
/// Gauss distribution with given mean and standard deviation |
836 | 835 |
|
837 | 836 |
/// Gauss distribution with given mean and standard deviation. |
838 | 837 |
/// \sa gauss() |
839 | 838 |
double gauss(double mean,double std_dev) |
840 | 839 |
{ |
841 | 840 |
return gauss()*std_dev+mean; |
842 | 841 |
} |
843 | 842 |
|
844 | 843 |
/// Exponential distribution with given mean |
845 | 844 |
|
846 | 845 |
/// This function generates an exponential distribution random number |
847 | 846 |
/// with mean <tt>1/lambda</tt>. |
848 | 847 |
/// |
849 | 848 |
double exponential(double lambda=1.0) |
850 | 849 |
{ |
851 | 850 |
return -std::log(1.0-real<double>())/lambda; |
852 | 851 |
} |
853 | 852 |
|
854 | 853 |
/// Gamma distribution with given integer shape |
855 | 854 |
|
856 | 855 |
/// This function generates a gamma distribution random number. |
857 | 856 |
/// |
858 | 857 |
///\param k shape parameter (<tt>k>0</tt> integer) |
859 | 858 |
double gamma(int k) |
860 | 859 |
{ |
861 | 860 |
double s = 0; |
862 | 861 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>()); |
863 | 862 |
return s; |
864 | 863 |
} |
865 | 864 |
|
866 | 865 |
/// Gamma distribution with given shape and scale parameter |
867 | 866 |
|
868 | 867 |
/// This function generates a gamma distribution random number. |
869 | 868 |
/// |
870 | 869 |
///\param k shape parameter (<tt>k>0</tt>) |
871 | 870 |
///\param theta scale parameter |
872 | 871 |
/// |
... | ... |
@@ -255,97 +255,96 @@ |
255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
256 | 256 |
/// allocation: if you know that the digraph you want to build will |
257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
258 | 258 |
/// then it is worth reserving space for this amount before starting |
259 | 259 |
/// to build the digraph. |
260 | 260 |
/// \sa reserveNode |
261 | 261 |
void reserveArc(int m) { arcs.reserve(m); }; |
262 | 262 |
|
263 | 263 |
/// \brief Node validity check |
264 | 264 |
/// |
265 | 265 |
/// This function gives back true if the given node is valid, |
266 | 266 |
/// ie. it is a real node of the graph. |
267 | 267 |
/// |
268 | 268 |
/// \warning A removed node (using Snapshot) could become valid again |
269 | 269 |
/// when new nodes are added to the graph. |
270 | 270 |
bool valid(Node n) const { return Parent::valid(n); } |
271 | 271 |
|
272 | 272 |
/// \brief Arc validity check |
273 | 273 |
/// |
274 | 274 |
/// This function gives back true if the given arc is valid, |
275 | 275 |
/// ie. it is a real arc of the graph. |
276 | 276 |
/// |
277 | 277 |
/// \warning A removed arc (using Snapshot) could become valid again |
278 | 278 |
/// when new arcs are added to the graph. |
279 | 279 |
bool valid(Arc a) const { return Parent::valid(a); } |
280 | 280 |
|
281 | 281 |
///Clear the digraph. |
282 | 282 |
|
283 | 283 |
///Erase all the nodes and arcs from the digraph. |
284 | 284 |
/// |
285 | 285 |
void clear() { |
286 | 286 |
Parent::clear(); |
287 | 287 |
} |
288 | 288 |
|
289 | 289 |
///Split a node. |
290 | 290 |
|
291 | 291 |
///This function splits a node. First a new node is added to the digraph, |
292 | 292 |
///then the source of each outgoing arc of \c n is moved to this new node. |
293 | 293 |
///If \c connect is \c true (this is the default value), then a new arc |
294 | 294 |
///from \c n to the newly created node is also added. |
295 | 295 |
///\return The newly created node. |
296 | 296 |
/// |
297 | 297 |
///\note The <tt>Arc</tt>s |
298 | 298 |
///referencing a moved arc remain |
299 | 299 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
300 | 300 |
///may be invalidated. |
301 | 301 |
///\warning This functionality cannot be used together with the Snapshot |
302 | 302 |
///feature. |
303 |
///\todo It could be implemented in a bit faster way. |
|
304 | 303 |
Node split(Node n, bool connect = true) |
305 | 304 |
{ |
306 | 305 |
Node b = addNode(); |
307 | 306 |
nodes[b._id].first_out=nodes[n._id].first_out; |
308 | 307 |
nodes[n._id].first_out=-1; |
309 | 308 |
for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id; |
310 | 309 |
if(connect) addArc(n,b); |
311 | 310 |
return b; |
312 | 311 |
} |
313 | 312 |
|
314 | 313 |
public: |
315 | 314 |
|
316 | 315 |
class Snapshot; |
317 | 316 |
|
318 | 317 |
protected: |
319 | 318 |
|
320 | 319 |
void restoreSnapshot(const Snapshot &s) |
321 | 320 |
{ |
322 | 321 |
while(s.arc_num<arcs.size()) { |
323 | 322 |
Arc arc = arcFromId(arcs.size()-1); |
324 | 323 |
Parent::notifier(Arc()).erase(arc); |
325 | 324 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
326 | 325 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
327 | 326 |
arcs.pop_back(); |
328 | 327 |
} |
329 | 328 |
while(s.node_num<nodes.size()) { |
330 | 329 |
Node node = nodeFromId(nodes.size()-1); |
331 | 330 |
Parent::notifier(Node()).erase(node); |
332 | 331 |
nodes.pop_back(); |
333 | 332 |
} |
334 | 333 |
} |
335 | 334 |
|
336 | 335 |
public: |
337 | 336 |
|
338 | 337 |
///Class to make a snapshot of the digraph and to restrore to it later. |
339 | 338 |
|
340 | 339 |
///Class to make a snapshot of the digraph and to restrore to it later. |
341 | 340 |
/// |
342 | 341 |
///The newly added nodes and arcs can be removed using the |
343 | 342 |
///restore() function. |
344 | 343 |
///\note After you restore a state, you cannot restore |
345 | 344 |
///a later state, in other word you cannot add again the arcs deleted |
346 | 345 |
///by restore() using another one Snapshot instance. |
347 | 346 |
/// |
348 | 347 |
///\warning If you do not use correctly the snapshot that can cause |
349 | 348 |
///either broken program, invalid state of the digraph, valid but |
350 | 349 |
///not the restored digraph or no change. Because the runtime performance |
351 | 350 |
///the validity of the snapshot is not stored. |
... | ... |
@@ -247,97 +247,96 @@ |
247 | 247 |
{ |
248 | 248 |
os << "u: " << t.userTime() << |
249 | 249 |
"s, s: " << t.systemTime() << |
250 | 250 |
"s, cu: " << t.cUserTime() << |
251 | 251 |
"s, cs: " << t.cSystemTime() << |
252 | 252 |
"s, real: " << t.realTime() << "s"; |
253 | 253 |
return os; |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
///Class for measuring the cpu time and real time usage of the process |
257 | 257 |
|
258 | 258 |
///Class for measuring the cpu time and real time usage of the process. |
259 | 259 |
///It is quite easy-to-use, here is a short example. |
260 | 260 |
///\code |
261 | 261 |
/// #include<lemon/time_measure.h> |
262 | 262 |
/// #include<iostream> |
263 | 263 |
/// |
264 | 264 |
/// int main() |
265 | 265 |
/// { |
266 | 266 |
/// |
267 | 267 |
/// ... |
268 | 268 |
/// |
269 | 269 |
/// Timer t; |
270 | 270 |
/// doSomething(); |
271 | 271 |
/// std::cout << t << '\n'; |
272 | 272 |
/// t.restart(); |
273 | 273 |
/// doSomethingElse(); |
274 | 274 |
/// std::cout << t << '\n'; |
275 | 275 |
/// |
276 | 276 |
/// ... |
277 | 277 |
/// |
278 | 278 |
/// } |
279 | 279 |
///\endcode |
280 | 280 |
/// |
281 | 281 |
///The \ref Timer can also be \ref stop() "stopped" and |
282 | 282 |
///\ref start() "started" again, so it is possible to compute collected |
283 | 283 |
///running times. |
284 | 284 |
/// |
285 | 285 |
///\warning Depending on the operation system and its actual configuration |
286 | 286 |
///the time counters have a certain (10ms on a typical Linux system) |
287 | 287 |
///granularity. |
288 | 288 |
///Therefore this tool is not appropriate to measure very short times. |
289 | 289 |
///Also, if you start and stop the timer very frequently, it could lead to |
290 | 290 |
///distorted results. |
291 | 291 |
/// |
292 | 292 |
///\note If you want to measure the running time of the execution of a certain |
293 | 293 |
///function, consider the usage of \ref TimeReport instead. |
294 | 294 |
/// |
295 |
///\todo This shouldn't be Unix (Linux) specific. |
|
296 | 295 |
///\sa TimeReport |
297 | 296 |
class Timer |
298 | 297 |
{ |
299 | 298 |
int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
300 | 299 |
TimeStamp start_time; //This is the relativ start-time if the timer |
301 | 300 |
//is _running, the collected _running time otherwise. |
302 | 301 |
|
303 | 302 |
void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
304 | 303 |
|
305 | 304 |
public: |
306 | 305 |
///Constructor. |
307 | 306 |
|
308 | 307 |
///\param run indicates whether or not the timer starts immediately. |
309 | 308 |
/// |
310 | 309 |
Timer(bool run=true) :_running(run) {_reset();} |
311 | 310 |
|
312 | 311 |
///\name Control the state of the timer |
313 | 312 |
///Basically a Timer can be either running or stopped, |
314 | 313 |
///but it provides a bit finer control on the execution. |
315 | 314 |
///The \ref Timer also counts the number of \ref start() |
316 | 315 |
///executions, and is stops only after the same amount (or more) |
317 | 316 |
///\ref stop() "stop()"s. This can be useful e.g. to compute |
318 | 317 |
///the running time |
319 | 318 |
///of recursive functions. |
320 | 319 |
/// |
321 | 320 |
|
322 | 321 |
///@{ |
323 | 322 |
|
324 | 323 |
///Reset and stop the time counters |
325 | 324 |
|
326 | 325 |
///This function resets and stops the time counters |
327 | 326 |
///\sa restart() |
328 | 327 |
void reset() |
329 | 328 |
{ |
330 | 329 |
_running=0; |
331 | 330 |
_reset(); |
332 | 331 |
} |
333 | 332 |
|
334 | 333 |
///Start the time counters |
335 | 334 |
|
336 | 335 |
///This function starts the time counters. |
337 | 336 |
/// |
338 | 337 |
///If the timer is started more than ones, it will remain running |
339 | 338 |
///until the same amount of \ref stop() is called. |
340 | 339 |
///\sa stop() |
341 | 340 |
void start() |
342 | 341 |
{ |
343 | 342 |
if(_running) _running++; |
... | ... |
@@ -442,97 +441,96 @@ |
442 | 441 |
} |
443 | 442 |
///Gives back the ellapsed user time of the process' children |
444 | 443 |
|
445 | 444 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
446 | 445 |
/// |
447 | 446 |
double cSystemTime() const |
448 | 447 |
{ |
449 | 448 |
return operator TimeStamp().cSystemTime(); |
450 | 449 |
} |
451 | 450 |
///Gives back the ellapsed real time |
452 | 451 |
double realTime() const |
453 | 452 |
{ |
454 | 453 |
return operator TimeStamp().realTime(); |
455 | 454 |
} |
456 | 455 |
///Computes the ellapsed time |
457 | 456 |
|
458 | 457 |
///This conversion computes the ellapsed time, therefore you can print |
459 | 458 |
///the ellapsed time like this. |
460 | 459 |
///\code |
461 | 460 |
/// Timer t; |
462 | 461 |
/// doSomething(); |
463 | 462 |
/// std::cout << t << '\n'; |
464 | 463 |
///\endcode |
465 | 464 |
operator TimeStamp () const |
466 | 465 |
{ |
467 | 466 |
TimeStamp t; |
468 | 467 |
t.stamp(); |
469 | 468 |
return _running?t-start_time:start_time; |
470 | 469 |
} |
471 | 470 |
|
472 | 471 |
|
473 | 472 |
///@} |
474 | 473 |
}; |
475 | 474 |
|
476 | 475 |
///Same as \ref Timer but prints a report on destruction. |
477 | 476 |
|
478 | 477 |
///Same as \ref Timer but prints a report on destruction. |
479 | 478 |
///This example shows its usage. |
480 | 479 |
///\code |
481 | 480 |
/// void myAlg(ListGraph &g,int n) |
482 | 481 |
/// { |
483 | 482 |
/// TimeReport tr("Running time of myAlg: "); |
484 | 483 |
/// ... //Here comes the algorithm |
485 | 484 |
/// } |
486 | 485 |
///\endcode |
487 | 486 |
/// |
488 | 487 |
///\sa Timer |
489 | 488 |
///\sa NoTimeReport |
490 |
///\todo There is no test case for this |
|
491 | 489 |
class TimeReport : public Timer |
492 | 490 |
{ |
493 | 491 |
std::string _title; |
494 | 492 |
std::ostream &_os; |
495 | 493 |
public: |
496 | 494 |
///\e |
497 | 495 |
|
498 | 496 |
///\param title This text will be printed before the ellapsed time. |
499 | 497 |
///\param os The stream to print the report to. |
500 | 498 |
///\param run Sets whether the timer should start immediately. |
501 | 499 |
|
502 | 500 |
TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
503 | 501 |
: Timer(run), _title(title), _os(os){} |
504 | 502 |
///\e Prints the ellapsed time on destruction. |
505 | 503 |
~TimeReport() |
506 | 504 |
{ |
507 | 505 |
_os << _title << *this << std::endl; |
508 | 506 |
} |
509 | 507 |
}; |
510 | 508 |
|
511 | 509 |
///'Do nothing' version of \ref TimeReport |
512 | 510 |
|
513 | 511 |
///\sa TimeReport |
514 | 512 |
/// |
515 | 513 |
class NoTimeReport |
516 | 514 |
{ |
517 | 515 |
public: |
518 | 516 |
///\e |
519 | 517 |
NoTimeReport(std::string,std::ostream &,bool) {} |
520 | 518 |
///\e |
521 | 519 |
NoTimeReport(std::string,std::ostream &) {} |
522 | 520 |
///\e |
523 | 521 |
NoTimeReport(std::string) {} |
524 | 522 |
///\e Do nothing. |
525 | 523 |
~NoTimeReport() {} |
526 | 524 |
|
527 | 525 |
operator TimeStamp () const { return TimeStamp(); } |
528 | 526 |
void reset() {} |
529 | 527 |
void start() {} |
530 | 528 |
void stop() {} |
531 | 529 |
void halt() {} |
532 | 530 |
int running() { return 0; } |
533 | 531 |
void restart() {} |
534 | 532 |
double userTime() const { return 0; } |
535 | 533 |
double systemTime() const { return 0; } |
536 | 534 |
double cUserTime() const { return 0; } |
537 | 535 |
double cSystemTime() const { return 0; } |
538 | 536 |
double realTime() const { return 0; } |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_TOLERANCE_H |
20 | 20 |
#define LEMON_TOLERANCE_H |
21 | 21 |
|
22 | 22 |
///\ingroup misc |
23 | 23 |
///\file |
24 | 24 |
///\brief A basic tool to handle the anomalies of calculation with |
25 | 25 |
///floating point numbers. |
26 | 26 |
/// |
27 |
///\todo It should be in a module like "Basic tools" |
|
28 |
|
|
29 | 27 |
|
30 | 28 |
namespace lemon { |
31 | 29 |
|
32 | 30 |
/// \addtogroup misc |
33 | 31 |
/// @{ |
34 | 32 |
|
35 | 33 |
///\brief A class to provide a basic way to |
36 | 34 |
///handle the comparison of numbers that are obtained |
37 | 35 |
///as a result of a probably inexact computation. |
38 | 36 |
/// |
39 | 37 |
///\ref Tolerance is a class to provide a basic way to |
40 | 38 |
///handle the comparison of numbers that are obtained |
41 | 39 |
///as a result of a probably inexact computation. |
42 | 40 |
/// |
43 | 41 |
///This is an abstract class, it should be specialized for all |
44 | 42 |
///numerical data types. These specialized classes like |
45 | 43 |
///Tolerance<double> may offer additional tuning parameters. |
46 | 44 |
/// |
47 | 45 |
///\sa Tolerance<float> |
48 | 46 |
///\sa Tolerance<double> |
49 | 47 |
///\sa Tolerance<long double> |
50 | 48 |
///\sa Tolerance<int> |
51 | 49 |
///\sa Tolerance<long long int> |
52 | 50 |
///\sa Tolerance<unsigned int> |
53 | 51 |
///\sa Tolerance<unsigned long long int> |
54 | 52 |
|
55 | 53 |
template<class T> |
56 | 54 |
class Tolerance |
57 | 55 |
{ |
58 | 56 |
public: |
59 | 57 |
typedef T Value; |
60 | 58 |
|
61 | 59 |
///\name Comparisons |
62 | 60 |
///The concept is that these bool functions return \c true only if |
63 | 61 |
///the related comparisons hold even if some numerical error appeared |
64 | 62 |
///during the computations. |
65 | 63 |
|
66 | 64 |
///@{ |
67 | 65 |
|
68 | 66 |
///Returns \c true if \c a is \e surely strictly less than \c b |
69 | 67 |
static bool less(Value a,Value b) {return false;} |
70 | 68 |
///Returns \c true if \c a is \e surely different from \c b |
71 | 69 |
static bool different(Value a,Value b) {return false;} |
72 | 70 |
///Returns \c true if \c a is \e surely positive |
73 | 71 |
static bool positive(Value a) {return false;} |
74 | 72 |
///Returns \c true if \c a is \e surely negative |
75 | 73 |
static bool negative(Value a) {return false;} |
76 | 74 |
///Returns \c true if \c a is \e surely non-zero |
1 | 1 |
#! /usr/bin/env python |
2 | 2 |
|
3 | 3 |
import sys |
4 | 4 |
import os |
5 | 5 |
|
6 | 6 |
if len(sys.argv)>1 and sys.argv[1] in ["-h","--help"]: |
7 | 7 |
print """ |
8 | 8 |
This utility just prints the length of the longest path |
9 | 9 |
in the revision graph from revison 0 to the current one. |
10 | 10 |
""" |
11 | 11 |
exit(0) |
12 |
plist = os.popen("hg parents --template='{rev}\n'").readlines() |
|
12 |
plist = os.popen("HGRCPATH='' hg parents --template='{rev}\n'").readlines() |
|
13 | 13 |
if len(plist)>1: |
14 | 14 |
print "You are in the process of merging" |
15 | 15 |
exit(1) |
16 | 16 |
PAR = int(plist[0]) |
17 | 17 |
|
18 |
f = os.popen("hg log -r 0:tip --template='{rev} {parents}\n'"). |
|
18 |
f = os.popen("HGRCPATH='' hg log -r 0:tip --template='{rev} {parents}\n'").\ |
|
19 |
readlines() |
|
19 | 20 |
REV = -1 |
20 | 21 |
lengths=[] |
21 | 22 |
for l in f: |
22 | 23 |
REV+=1 |
23 | 24 |
s = l.split() |
24 | 25 |
rev = int(s[0]) |
25 | 26 |
if REV != rev: |
26 | 27 |
print "Something is seriously wrong" |
27 | 28 |
exit(1) |
28 | 29 |
if len(s) == 1: |
29 | 30 |
par1 = par2 = rev - 1 |
30 | 31 |
elif len(s) == 2: |
31 | 32 |
par1 = par2 = int(s[1].split(":")[0]) |
32 | 33 |
else: |
33 | 34 |
par1 = int(s[1].split(":")[0]) |
34 | 35 |
par2 = int(s[2].split(":")[0]) |
35 | 36 |
if rev == 0: |
36 | 37 |
lengths.append(0) |
37 | 38 |
else: |
38 | 39 |
lengths.append(max(lengths[par1],lengths[par2])+1) |
39 | 40 |
print lengths[PAR] |
... | ... |
@@ -18,174 +18,174 @@ |
18 | 18 |
|
19 | 19 |
#include <lemon/smart_graph.h> |
20 | 20 |
#include <lemon/list_graph.h> |
21 | 21 |
#include <lemon/lgf_reader.h> |
22 | 22 |
#include <lemon/error.h> |
23 | 23 |
|
24 | 24 |
#include "test_tools.h" |
25 | 25 |
|
26 | 26 |
using namespace std; |
27 | 27 |
using namespace lemon; |
28 | 28 |
|
29 | 29 |
void digraph_copy_test() { |
30 | 30 |
const int nn = 10; |
31 | 31 |
|
32 | 32 |
SmartDigraph from; |
33 | 33 |
SmartDigraph::NodeMap<int> fnm(from); |
34 | 34 |
SmartDigraph::ArcMap<int> fam(from); |
35 | 35 |
SmartDigraph::Node fn = INVALID; |
36 | 36 |
SmartDigraph::Arc fa = INVALID; |
37 | 37 |
|
38 | 38 |
std::vector<SmartDigraph::Node> fnv; |
39 | 39 |
for (int i = 0; i < nn; ++i) { |
40 | 40 |
SmartDigraph::Node node = from.addNode(); |
41 | 41 |
fnv.push_back(node); |
42 | 42 |
fnm[node] = i * i; |
43 | 43 |
if (i == 0) fn = node; |
44 | 44 |
} |
45 | 45 |
|
46 | 46 |
for (int i = 0; i < nn; ++i) { |
47 | 47 |
for (int j = 0; j < nn; ++j) { |
48 | 48 |
SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); |
49 | 49 |
fam[arc] = i + j * j; |
50 | 50 |
if (i == 0 && j == 0) fa = arc; |
51 | 51 |
} |
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
ListDigraph to; |
55 | 55 |
ListDigraph::NodeMap<int> tnm(to); |
56 | 56 |
ListDigraph::ArcMap<int> tam(to); |
57 | 57 |
ListDigraph::Node tn; |
58 | 58 |
ListDigraph::Arc ta; |
59 | 59 |
|
60 | 60 |
SmartDigraph::NodeMap<ListDigraph::Node> nr(from); |
61 | 61 |
SmartDigraph::ArcMap<ListDigraph::Arc> er(from); |
62 | 62 |
|
63 | 63 |
ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
64 | 64 |
ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
65 | 65 |
|
66 |
DigraphCopy<ListDigraph, SmartDigraph>(to, from). |
|
67 |
nodeMap(tnm, fnm).arcMap(tam, fam). |
|
66 |
digraphCopy(from, to). |
|
67 |
nodeMap(fnm, tnm).arcMap(fam, tam). |
|
68 | 68 |
nodeRef(nr).arcRef(er). |
69 | 69 |
nodeCrossRef(ncr).arcCrossRef(ecr). |
70 |
node( |
|
70 |
node(fn, tn).arc(fa, ta).run(); |
|
71 | 71 |
|
72 | 72 |
for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) { |
73 | 73 |
check(ncr[nr[it]] == it, "Wrong copy."); |
74 | 74 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) { |
78 | 78 |
check(ecr[er[it]] == it, "Wrong copy."); |
79 | 79 |
check(fam[it] == tam[er[it]], "Wrong copy."); |
80 | 80 |
check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); |
81 | 81 |
check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
for (ListDigraph::NodeIt it(to); it != INVALID; ++it) { |
85 | 85 |
check(nr[ncr[it]] == it, "Wrong copy."); |
86 | 86 |
} |
87 | 87 |
|
88 | 88 |
for (ListDigraph::ArcIt it(to); it != INVALID; ++it) { |
89 | 89 |
check(er[ecr[it]] == it, "Wrong copy."); |
90 | 90 |
} |
91 | 91 |
check(tn == nr[fn], "Wrong copy."); |
92 | 92 |
check(ta == er[fa], "Wrong copy."); |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
void graph_copy_test() { |
96 | 96 |
const int nn = 10; |
97 | 97 |
|
98 | 98 |
SmartGraph from; |
99 | 99 |
SmartGraph::NodeMap<int> fnm(from); |
100 | 100 |
SmartGraph::ArcMap<int> fam(from); |
101 | 101 |
SmartGraph::EdgeMap<int> fem(from); |
102 | 102 |
SmartGraph::Node fn = INVALID; |
103 | 103 |
SmartGraph::Arc fa = INVALID; |
104 | 104 |
SmartGraph::Edge fe = INVALID; |
105 | 105 |
|
106 | 106 |
std::vector<SmartGraph::Node> fnv; |
107 | 107 |
for (int i = 0; i < nn; ++i) { |
108 | 108 |
SmartGraph::Node node = from.addNode(); |
109 | 109 |
fnv.push_back(node); |
110 | 110 |
fnm[node] = i * i; |
111 | 111 |
if (i == 0) fn = node; |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
for (int i = 0; i < nn; ++i) { |
115 | 115 |
for (int j = 0; j < nn; ++j) { |
116 | 116 |
SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); |
117 | 117 |
fem[edge] = i * i + j * j; |
118 | 118 |
fam[from.direct(edge, true)] = i + j * j; |
119 | 119 |
fam[from.direct(edge, false)] = i * i + j; |
120 | 120 |
if (i == 0 && j == 0) fa = from.direct(edge, true); |
121 | 121 |
if (i == 0 && j == 0) fe = edge; |
122 | 122 |
} |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
ListGraph to; |
126 | 126 |
ListGraph::NodeMap<int> tnm(to); |
127 | 127 |
ListGraph::ArcMap<int> tam(to); |
128 | 128 |
ListGraph::EdgeMap<int> tem(to); |
129 | 129 |
ListGraph::Node tn; |
130 | 130 |
ListGraph::Arc ta; |
131 | 131 |
ListGraph::Edge te; |
132 | 132 |
|
133 | 133 |
SmartGraph::NodeMap<ListGraph::Node> nr(from); |
134 | 134 |
SmartGraph::ArcMap<ListGraph::Arc> ar(from); |
135 | 135 |
SmartGraph::EdgeMap<ListGraph::Edge> er(from); |
136 | 136 |
|
137 | 137 |
ListGraph::NodeMap<SmartGraph::Node> ncr(to); |
138 | 138 |
ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
139 | 139 |
ListGraph::EdgeMap<SmartGraph::Edge> ecr(to); |
140 | 140 |
|
141 |
GraphCopy<ListGraph, SmartGraph>(to, from). |
|
142 |
nodeMap(tnm, fnm).arcMap(tam, fam).edgeMap(tem, fem). |
|
141 |
graphCopy(from, to). |
|
142 |
nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
|
143 | 143 |
nodeRef(nr).arcRef(ar).edgeRef(er). |
144 | 144 |
nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
145 |
node( |
|
145 |
node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
|
146 | 146 |
|
147 | 147 |
for (SmartGraph::NodeIt it(from); it != INVALID; ++it) { |
148 | 148 |
check(ncr[nr[it]] == it, "Wrong copy."); |
149 | 149 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
for (SmartGraph::ArcIt it(from); it != INVALID; ++it) { |
153 | 153 |
check(acr[ar[it]] == it, "Wrong copy."); |
154 | 154 |
check(fam[it] == tam[ar[it]], "Wrong copy."); |
155 | 155 |
check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); |
156 | 156 |
check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) { |
160 | 160 |
check(ecr[er[it]] == it, "Wrong copy."); |
161 | 161 |
check(fem[it] == tem[er[it]], "Wrong copy."); |
162 | 162 |
check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), |
163 | 163 |
"Wrong copy."); |
164 | 164 |
check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), |
165 | 165 |
"Wrong copy."); |
166 | 166 |
check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), |
167 | 167 |
"Wrong copy."); |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
for (ListGraph::NodeIt it(to); it != INVALID; ++it) { |
171 | 171 |
check(nr[ncr[it]] == it, "Wrong copy."); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
for (ListGraph::ArcIt it(to); it != INVALID; ++it) { |
175 | 175 |
check(ar[acr[it]] == it, "Wrong copy."); |
176 | 176 |
} |
177 | 177 |
for (ListGraph::EdgeIt it(to); it != INVALID; ++it) { |
178 | 178 |
check(er[ecr[it]] == it, "Wrong copy."); |
179 | 179 |
} |
180 | 180 |
check(tn == nr[fn], "Wrong copy."); |
181 | 181 |
check(ta == ar[fa], "Wrong copy."); |
182 | 182 |
check(te == er[fe], "Wrong copy."); |
183 | 183 |
} |
184 | 184 |
|
185 | 185 |
|
186 | 186 |
int main() { |
187 | 187 |
digraph_copy_test(); |
188 | 188 |
graph_copy_test(); |
189 | 189 |
|
190 | 190 |
return 0; |
191 | 191 |
} |
0 comments (0 inline)