0
15
0
2
7
2
7
1
11
| ... | ... |
@@ -8,107 +8,105 @@ |
| 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_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 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
///Default traits class of Bfs class. |
| 35 | 35 |
|
| 36 | 36 |
///Default traits class of Bfs class. |
| 37 | 37 |
///\tparam GR Digraph type. |
| 38 | 38 |
template<class GR> |
| 39 | 39 |
struct BfsDefaultTraits |
| 40 | 40 |
{
|
| 41 | 41 |
///The type of the digraph the algorithm runs on. |
| 42 | 42 |
typedef GR Digraph; |
| 43 | 43 |
|
| 44 | 44 |
///\brief The type of the map that stores the predecessor |
| 45 | 45 |
///arcs of the shortest paths. |
| 46 | 46 |
/// |
| 47 | 47 |
///The type of the map that stores the predecessor |
| 48 | 48 |
///arcs of the shortest paths. |
| 49 | 49 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 50 | 50 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 51 | 51 |
///Instantiates a \ref PredMap. |
| 52 | 52 |
|
| 53 | 53 |
///This function instantiates a \ref PredMap. |
| 54 | 54 |
///\param g is the digraph, to which we would like to define the |
| 55 | 55 |
///\ref PredMap. |
| 56 |
///\todo The digraph alone may be insufficient to initialize |
|
| 57 | 56 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 57 |
{
|
| 59 | 58 |
return new PredMap(g); |
| 60 | 59 |
} |
| 61 | 60 |
|
| 62 | 61 |
///The type of the map that indicates which nodes are processed. |
| 63 | 62 |
|
| 64 | 63 |
///The type of the map that indicates which nodes are processed. |
| 65 | 64 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 |
///By default it is a NullMap. |
|
| 67 | 65 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 68 | 66 |
///Instantiates a \ref ProcessedMap. |
| 69 | 67 |
|
| 70 | 68 |
///This function instantiates a \ref ProcessedMap. |
| 71 | 69 |
///\param g is the digraph, to which |
| 72 | 70 |
///we would like to define the \ref ProcessedMap |
| 73 | 71 |
#ifdef DOXYGEN |
| 74 | 72 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 75 | 73 |
#else |
| 76 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 77 | 75 |
#endif |
| 78 | 76 |
{
|
| 79 | 77 |
return new ProcessedMap(); |
| 80 | 78 |
} |
| 81 | 79 |
|
| 82 | 80 |
///The type of the map that indicates which nodes are reached. |
| 83 | 81 |
|
| 84 | 82 |
///The type of the map that indicates which nodes are reached. |
| 85 | 83 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 86 | 84 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 87 | 85 |
///Instantiates a \ref ReachedMap. |
| 88 | 86 |
|
| 89 | 87 |
///This function instantiates a \ref ReachedMap. |
| 90 | 88 |
///\param g is the digraph, to which |
| 91 | 89 |
///we would like to define the \ref ReachedMap. |
| 92 | 90 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 93 | 91 |
{
|
| 94 | 92 |
return new ReachedMap(g); |
| 95 | 93 |
} |
| 96 | 94 |
|
| 97 | 95 |
///The type of the map that stores the distances of the nodes. |
| 98 | 96 |
|
| 99 | 97 |
///The type of the map that stores the distances of the nodes. |
| 100 | 98 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 101 | 99 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 102 | 100 |
///Instantiates a \ref DistMap. |
| 103 | 101 |
|
| 104 | 102 |
///This function instantiates a \ref DistMap. |
| 105 | 103 |
///\param g is the digraph, to which we would like to define the |
| 106 | 104 |
///\ref DistMap. |
| 107 | 105 |
static DistMap *createDistMap(const Digraph &g) |
| 108 | 106 |
{
|
| 109 | 107 |
return new DistMap(g); |
| 110 | 108 |
} |
| 111 | 109 |
}; |
| 112 | 110 |
|
| 113 | 111 |
///%BFS algorithm class. |
| 114 | 112 |
|
| ... | ... |
@@ -150,98 +148,97 @@ |
| 150 | 148 |
///The type of the digraph the algorithm runs on. |
| 151 | 149 |
typedef typename TR::Digraph Digraph; |
| 152 | 150 |
|
| 153 | 151 |
///\brief The type of the map that stores the predecessor arcs of the |
| 154 | 152 |
///shortest paths. |
| 155 | 153 |
typedef typename TR::PredMap PredMap; |
| 156 | 154 |
///The type of the map that stores the distances of the nodes. |
| 157 | 155 |
typedef typename TR::DistMap DistMap; |
| 158 | 156 |
///The type of the map that indicates which nodes are reached. |
| 159 | 157 |
typedef typename TR::ReachedMap ReachedMap; |
| 160 | 158 |
///The type of the map that indicates which nodes are processed. |
| 161 | 159 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 162 | 160 |
///The type of the paths. |
| 163 | 161 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 164 | 162 |
|
| 165 | 163 |
///The traits class. |
| 166 | 164 |
typedef TR Traits; |
| 167 | 165 |
|
| 168 | 166 |
private: |
| 169 | 167 |
|
| 170 | 168 |
typedef typename Digraph::Node Node; |
| 171 | 169 |
typedef typename Digraph::NodeIt NodeIt; |
| 172 | 170 |
typedef typename Digraph::Arc Arc; |
| 173 | 171 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 174 | 172 |
|
| 175 | 173 |
//Pointer to the underlying digraph. |
| 176 | 174 |
const Digraph *G; |
| 177 | 175 |
//Pointer to the map of predecessor arcs. |
| 178 | 176 |
PredMap *_pred; |
| 179 | 177 |
//Indicates if _pred is locally allocated (true) or not. |
| 180 | 178 |
bool local_pred; |
| 181 | 179 |
//Pointer to the map of distances. |
| 182 | 180 |
DistMap *_dist; |
| 183 | 181 |
//Indicates if _dist is locally allocated (true) or not. |
| 184 | 182 |
bool local_dist; |
| 185 | 183 |
//Pointer to the map of reached status of the nodes. |
| 186 | 184 |
ReachedMap *_reached; |
| 187 | 185 |
//Indicates if _reached is locally allocated (true) or not. |
| 188 | 186 |
bool local_reached; |
| 189 | 187 |
//Pointer to the map of processed status of the nodes. |
| 190 | 188 |
ProcessedMap *_processed; |
| 191 | 189 |
//Indicates if _processed is locally allocated (true) or not. |
| 192 | 190 |
bool local_processed; |
| 193 | 191 |
|
| 194 | 192 |
std::vector<typename Digraph::Node> _queue; |
| 195 | 193 |
int _queue_head,_queue_tail,_queue_next_dist; |
| 196 | 194 |
int _curr_dist; |
| 197 | 195 |
|
| 198 |
///Creates the maps if necessary. |
|
| 199 |
///\todo Better memory allocation (instead of new). |
|
| 196 |
//Creates the maps if necessary. |
|
| 200 | 197 |
void create_maps() |
| 201 | 198 |
{
|
| 202 | 199 |
if(!_pred) {
|
| 203 | 200 |
local_pred = true; |
| 204 | 201 |
_pred = Traits::createPredMap(*G); |
| 205 | 202 |
} |
| 206 | 203 |
if(!_dist) {
|
| 207 | 204 |
local_dist = true; |
| 208 | 205 |
_dist = Traits::createDistMap(*G); |
| 209 | 206 |
} |
| 210 | 207 |
if(!_reached) {
|
| 211 | 208 |
local_reached = true; |
| 212 | 209 |
_reached = Traits::createReachedMap(*G); |
| 213 | 210 |
} |
| 214 | 211 |
if(!_processed) {
|
| 215 | 212 |
local_processed = true; |
| 216 | 213 |
_processed = Traits::createProcessedMap(*G); |
| 217 | 214 |
} |
| 218 | 215 |
} |
| 219 | 216 |
|
| 220 | 217 |
protected: |
| 221 | 218 |
|
| 222 | 219 |
Bfs() {}
|
| 223 | 220 |
|
| 224 | 221 |
public: |
| 225 | 222 |
|
| 226 | 223 |
typedef Bfs Create; |
| 227 | 224 |
|
| 228 | 225 |
///\name Named template parameters |
| 229 | 226 |
|
| 230 | 227 |
///@{
|
| 231 | 228 |
|
| 232 | 229 |
template <class T> |
| 233 | 230 |
struct SetPredMapTraits : public Traits {
|
| 234 | 231 |
typedef T PredMap; |
| 235 | 232 |
static PredMap *createPredMap(const Digraph &) |
| 236 | 233 |
{
|
| 237 | 234 |
throw UninitializedParameter(); |
| 238 | 235 |
} |
| 239 | 236 |
}; |
| 240 | 237 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 241 | 238 |
///\ref PredMap type. |
| 242 | 239 |
/// |
| 243 | 240 |
///\ref named-templ-param "Named parameter" for setting |
| 244 | 241 |
///\ref PredMap type. |
| 245 | 242 |
template <class T> |
| 246 | 243 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
|
| 247 | 244 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
| ... | ... |
@@ -802,97 +799,96 @@ |
| 802 | 799 |
///of the nodes calculated by the algorithm. |
| 803 | 800 |
/// |
| 804 | 801 |
///\pre Either \ref run() or \ref init() |
| 805 | 802 |
///must be called before using this function. |
| 806 | 803 |
const DistMap &distMap() const { return *_dist;}
|
| 807 | 804 |
|
| 808 | 805 |
///\brief Returns a const reference to the node map that stores the |
| 809 | 806 |
///predecessor arcs. |
| 810 | 807 |
/// |
| 811 | 808 |
///Returns a const reference to the node map that stores the predecessor |
| 812 | 809 |
///arcs, which form the shortest path tree. |
| 813 | 810 |
/// |
| 814 | 811 |
///\pre Either \ref run() or \ref init() |
| 815 | 812 |
///must be called before using this function. |
| 816 | 813 |
const PredMap &predMap() const { return *_pred;}
|
| 817 | 814 |
|
| 818 | 815 |
///Checks if a node is reachable from the root(s). |
| 819 | 816 |
|
| 820 | 817 |
///Returns \c true if \c v is reachable from the root(s). |
| 821 | 818 |
///\pre Either \ref run() or \ref start() |
| 822 | 819 |
///must be called before using this function. |
| 823 | 820 |
bool reached(Node v) const { return (*_reached)[v]; }
|
| 824 | 821 |
|
| 825 | 822 |
///@} |
| 826 | 823 |
}; |
| 827 | 824 |
|
| 828 | 825 |
///Default traits class of bfs() function. |
| 829 | 826 |
|
| 830 | 827 |
///Default traits class of bfs() function. |
| 831 | 828 |
///\tparam GR Digraph type. |
| 832 | 829 |
template<class GR> |
| 833 | 830 |
struct BfsWizardDefaultTraits |
| 834 | 831 |
{
|
| 835 | 832 |
///The type of the digraph the algorithm runs on. |
| 836 | 833 |
typedef GR Digraph; |
| 837 | 834 |
|
| 838 | 835 |
///\brief The type of the map that stores the predecessor |
| 839 | 836 |
///arcs of the shortest paths. |
| 840 | 837 |
/// |
| 841 | 838 |
///The type of the map that stores the predecessor |
| 842 | 839 |
///arcs of the shortest paths. |
| 843 | 840 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 844 | 841 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
| 845 | 842 |
///Instantiates a \ref PredMap. |
| 846 | 843 |
|
| 847 | 844 |
///This function instantiates a \ref PredMap. |
| 848 | 845 |
///\param g is the digraph, to which we would like to define the |
| 849 | 846 |
///\ref PredMap. |
| 850 |
///\todo The digraph alone may be insufficient to initialize |
|
| 851 | 847 |
#ifdef DOXYGEN |
| 852 | 848 |
static PredMap *createPredMap(const Digraph &g) |
| 853 | 849 |
#else |
| 854 | 850 |
static PredMap *createPredMap(const Digraph &) |
| 855 | 851 |
#endif |
| 856 | 852 |
{
|
| 857 | 853 |
return new PredMap(); |
| 858 | 854 |
} |
| 859 | 855 |
|
| 860 | 856 |
///The type of the map that indicates which nodes are processed. |
| 861 | 857 |
|
| 862 | 858 |
///The type of the map that indicates which nodes are processed. |
| 863 | 859 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 864 | 860 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 865 | 861 |
///Instantiates a \ref ProcessedMap. |
| 866 | 862 |
|
| 867 | 863 |
///This function instantiates a \ref ProcessedMap. |
| 868 | 864 |
///\param g is the digraph, to which |
| 869 | 865 |
///we would like to define the \ref ProcessedMap. |
| 870 | 866 |
#ifdef DOXYGEN |
| 871 | 867 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 872 | 868 |
#else |
| 873 | 869 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 874 | 870 |
#endif |
| 875 | 871 |
{
|
| 876 | 872 |
return new ProcessedMap(); |
| 877 | 873 |
} |
| 878 | 874 |
|
| 879 | 875 |
///The type of the map that indicates which nodes are reached. |
| 880 | 876 |
|
| 881 | 877 |
///The type of the map that indicates which nodes are reached. |
| 882 | 878 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 883 | 879 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 884 | 880 |
///Instantiates a \ref ReachedMap. |
| 885 | 881 |
|
| 886 | 882 |
///This function instantiates a \ref ReachedMap. |
| 887 | 883 |
///\param g is the digraph, to which |
| 888 | 884 |
///we would like to define the \ref ReachedMap. |
| 889 | 885 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 890 | 886 |
{
|
| 891 | 887 |
return new ReachedMap(g); |
| 892 | 888 |
} |
| 893 | 889 |
|
| 894 | 890 |
///The type of the map that stores the distances of the nodes. |
| 895 | 891 |
|
| 896 | 892 |
///The type of the map that stores the distances of the nodes. |
| 897 | 893 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 898 | 894 |
/// |
| ... | ... |
@@ -1287,98 +1283,97 @@ |
| 1287 | 1283 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
| 1288 | 1284 |
#endif |
| 1289 | 1285 |
class BfsVisit {
|
| 1290 | 1286 |
public: |
| 1291 | 1287 |
|
| 1292 | 1288 |
/// \brief \ref Exception for uninitialized parameters. |
| 1293 | 1289 |
/// |
| 1294 | 1290 |
/// This error represents problems in the initialization |
| 1295 | 1291 |
/// of the parameters of the algorithm. |
| 1296 | 1292 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
| 1297 | 1293 |
public: |
| 1298 | 1294 |
virtual const char* what() const throw() |
| 1299 | 1295 |
{
|
| 1300 | 1296 |
return "lemon::BfsVisit::UninitializedParameter"; |
| 1301 | 1297 |
} |
| 1302 | 1298 |
}; |
| 1303 | 1299 |
|
| 1304 | 1300 |
///The traits class. |
| 1305 | 1301 |
typedef _Traits Traits; |
| 1306 | 1302 |
|
| 1307 | 1303 |
///The type of the digraph the algorithm runs on. |
| 1308 | 1304 |
typedef typename Traits::Digraph Digraph; |
| 1309 | 1305 |
|
| 1310 | 1306 |
///The visitor type used by the algorithm. |
| 1311 | 1307 |
typedef _Visitor Visitor; |
| 1312 | 1308 |
|
| 1313 | 1309 |
///The type of the map that indicates which nodes are reached. |
| 1314 | 1310 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1315 | 1311 |
|
| 1316 | 1312 |
private: |
| 1317 | 1313 |
|
| 1318 | 1314 |
typedef typename Digraph::Node Node; |
| 1319 | 1315 |
typedef typename Digraph::NodeIt NodeIt; |
| 1320 | 1316 |
typedef typename Digraph::Arc Arc; |
| 1321 | 1317 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1322 | 1318 |
|
| 1323 | 1319 |
//Pointer to the underlying digraph. |
| 1324 | 1320 |
const Digraph *_digraph; |
| 1325 | 1321 |
//Pointer to the visitor object. |
| 1326 | 1322 |
Visitor *_visitor; |
| 1327 | 1323 |
//Pointer to the map of reached status of the nodes. |
| 1328 | 1324 |
ReachedMap *_reached; |
| 1329 | 1325 |
//Indicates if _reached is locally allocated (true) or not. |
| 1330 | 1326 |
bool local_reached; |
| 1331 | 1327 |
|
| 1332 | 1328 |
std::vector<typename Digraph::Node> _list; |
| 1333 | 1329 |
int _list_front, _list_back; |
| 1334 | 1330 |
|
| 1335 |
///Creates the maps if necessary. |
|
| 1336 |
///\todo Better memory allocation (instead of new). |
|
| 1331 |
//Creates the maps if necessary. |
|
| 1337 | 1332 |
void create_maps() {
|
| 1338 | 1333 |
if(!_reached) {
|
| 1339 | 1334 |
local_reached = true; |
| 1340 | 1335 |
_reached = Traits::createReachedMap(*_digraph); |
| 1341 | 1336 |
} |
| 1342 | 1337 |
} |
| 1343 | 1338 |
|
| 1344 | 1339 |
protected: |
| 1345 | 1340 |
|
| 1346 | 1341 |
BfsVisit() {}
|
| 1347 | 1342 |
|
| 1348 | 1343 |
public: |
| 1349 | 1344 |
|
| 1350 | 1345 |
typedef BfsVisit Create; |
| 1351 | 1346 |
|
| 1352 | 1347 |
/// \name Named template parameters |
| 1353 | 1348 |
|
| 1354 | 1349 |
///@{
|
| 1355 | 1350 |
template <class T> |
| 1356 | 1351 |
struct SetReachedMapTraits : public Traits {
|
| 1357 | 1352 |
typedef T ReachedMap; |
| 1358 | 1353 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1359 | 1354 |
throw UninitializedParameter(); |
| 1360 | 1355 |
} |
| 1361 | 1356 |
}; |
| 1362 | 1357 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1363 | 1358 |
/// ReachedMap type. |
| 1364 | 1359 |
/// |
| 1365 | 1360 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1366 | 1361 |
template <class T> |
| 1367 | 1362 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
| 1368 | 1363 |
SetReachedMapTraits<T> > {
|
| 1369 | 1364 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1370 | 1365 |
}; |
| 1371 | 1366 |
///@} |
| 1372 | 1367 |
|
| 1373 | 1368 |
public: |
| 1374 | 1369 |
|
| 1375 | 1370 |
/// \brief Constructor. |
| 1376 | 1371 |
/// |
| 1377 | 1372 |
/// Constructor. |
| 1378 | 1373 |
/// |
| 1379 | 1374 |
/// \param digraph The digraph the algorithm runs on. |
| 1380 | 1375 |
/// \param visitor The visitor object of the algorithm. |
| 1381 | 1376 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1382 | 1377 |
: _digraph(&digraph), _visitor(&visitor), |
| 1383 | 1378 |
_reached(0), local_reached(false) {}
|
| 1384 | 1379 |
| ... | ... |
@@ -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 | 19 |
// This file contains a modified version of the concept checking |
| 20 | 20 |
// utility from BOOST. |
| 21 | 21 |
// See the appropriate copyright notice below. |
| 22 | 22 |
|
| 23 | 23 |
// (C) Copyright Jeremy Siek 2000. |
| 24 | 24 |
// Distributed under the Boost Software License, Version 1.0. (See |
| 25 | 25 |
// accompanying file LICENSE_1_0.txt or copy at |
| 26 | 26 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 27 | 27 |
// |
| 28 | 28 |
// Revision History: |
| 29 | 29 |
// 05 May 2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek) |
| 30 | 30 |
// 02 April 2001: Removed limits header altogether. (Jeremy Siek) |
| 31 | 31 |
// 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock) |
| 32 | 32 |
// |
| 33 | 33 |
|
| 34 | 34 |
// See http://www.boost.org/libs/concept_check for documentation. |
| 35 | 35 |
|
| 36 | 36 |
///\file |
| 37 | 37 |
///\brief Basic utilities for concept checking. |
| 38 | 38 |
/// |
| 39 |
///\todo Are we still using BOOST concept checking utility? |
|
| 40 |
///Is the BOOST copyright notice necessary? |
|
| 41 | 39 |
|
| 42 | 40 |
#ifndef LEMON_CONCEPT_CHECK_H |
| 43 | 41 |
#define LEMON_CONCEPT_CHECK_H |
| 44 | 42 |
|
| 45 | 43 |
namespace lemon {
|
| 46 | 44 |
|
| 47 | 45 |
/* |
| 48 | 46 |
"inline" is used for ignore_unused_variable_warning() |
| 49 | 47 |
and function_requires() to make sure there is no |
| 50 | 48 |
overtarget with g++. |
| 51 | 49 |
*/ |
| 52 | 50 |
|
| 53 | 51 |
template <class T> inline void ignore_unused_variable_warning(const T&) { }
|
| 54 | 52 |
|
| 55 | 53 |
///\e |
| 56 | 54 |
template <class Concept> |
| 57 | 55 |
inline void function_requires() |
| 58 | 56 |
{
|
| 59 | 57 |
#if !defined(NDEBUG) |
| 60 | 58 |
void (Concept::*x)() = & Concept::constraints; |
| 61 | 59 |
ignore_unused_variable_warning(x); |
| 62 | 60 |
#endif |
| 63 | 61 |
} |
| 64 | 62 |
|
| 65 | 63 |
///\e |
| 66 | 64 |
template <typename Concept, typename Type> |
| 67 | 65 |
inline void checkConcept() {
|
| 68 | 66 |
#if !defined(NDEBUG) |
| 69 | 67 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
| 70 | 68 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
| 71 | 69 |
ignore_unused_variable_warning(x); |
| 72 | 70 |
#endif |
| 73 | 71 |
} |
| 74 | 72 |
|
| 75 | 73 |
} // namespace lemon |
| 76 | 74 |
|
| 77 | 75 |
#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 |
|
| 71 | 70 |
/// Length of the path ie. the number of arcs in the path. |
| ... | ... |
@@ -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_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 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Dfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Dfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct DfsDefaultTraits |
| 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 %DFS paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the %DFS 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 |
///%DFS algorithm class. |
| 115 | 113 |
|
| ... | ... |
@@ -150,98 +148,97 @@ |
| 150 | 148 |
|
| 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 |
///DFS 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::OutArcIt> _stack; |
| 196 | 194 |
int _stack_head; |
| 197 | 195 |
|
| 198 |
///Creates the maps if necessary. |
|
| 199 |
///\todo Better memory allocation (instead of new). |
|
| 196 |
//Creates the maps if necessary. |
|
| 200 | 197 |
void create_maps() |
| 201 | 198 |
{
|
| 202 | 199 |
if(!_pred) {
|
| 203 | 200 |
local_pred = true; |
| 204 | 201 |
_pred = Traits::createPredMap(*G); |
| 205 | 202 |
} |
| 206 | 203 |
if(!_dist) {
|
| 207 | 204 |
local_dist = true; |
| 208 | 205 |
_dist = Traits::createDistMap(*G); |
| 209 | 206 |
} |
| 210 | 207 |
if(!_reached) {
|
| 211 | 208 |
local_reached = true; |
| 212 | 209 |
_reached = Traits::createReachedMap(*G); |
| 213 | 210 |
} |
| 214 | 211 |
if(!_processed) {
|
| 215 | 212 |
local_processed = true; |
| 216 | 213 |
_processed = Traits::createProcessedMap(*G); |
| 217 | 214 |
} |
| 218 | 215 |
} |
| 219 | 216 |
|
| 220 | 217 |
protected: |
| 221 | 218 |
|
| 222 | 219 |
Dfs() {}
|
| 223 | 220 |
|
| 224 | 221 |
public: |
| 225 | 222 |
|
| 226 | 223 |
typedef Dfs Create; |
| 227 | 224 |
|
| 228 | 225 |
///\name Named template parameters |
| 229 | 226 |
|
| 230 | 227 |
///@{
|
| 231 | 228 |
|
| 232 | 229 |
template <class T> |
| 233 | 230 |
struct SetPredMapTraits : public Traits {
|
| 234 | 231 |
typedef T PredMap; |
| 235 | 232 |
static PredMap *createPredMap(const Digraph &) |
| 236 | 233 |
{
|
| 237 | 234 |
throw UninitializedParameter(); |
| 238 | 235 |
} |
| 239 | 236 |
}; |
| 240 | 237 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 241 | 238 |
///\ref PredMap type. |
| 242 | 239 |
/// |
| 243 | 240 |
///\ref named-templ-param "Named parameter" for setting |
| 244 | 241 |
///\ref PredMap type. |
| 245 | 242 |
template <class T> |
| 246 | 243 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
|
| 247 | 244 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
| ... | ... |
@@ -737,97 +734,96 @@ |
| 737 | 734 |
/// |
| 738 | 735 |
///\pre Either \ref run() or \ref init() |
| 739 | 736 |
///must be called before using this function. |
| 740 | 737 |
const DistMap &distMap() const { return *_dist;}
|
| 741 | 738 |
|
| 742 | 739 |
///\brief Returns a const reference to the node map that stores the |
| 743 | 740 |
///predecessor arcs. |
| 744 | 741 |
/// |
| 745 | 742 |
///Returns a const reference to the node map that stores the predecessor |
| 746 | 743 |
///arcs, which form the DFS tree. |
| 747 | 744 |
/// |
| 748 | 745 |
///\pre Either \ref run() or \ref init() |
| 749 | 746 |
///must be called before using this function. |
| 750 | 747 |
const PredMap &predMap() const { return *_pred;}
|
| 751 | 748 |
|
| 752 | 749 |
///Checks if a node is reachable from the root(s). |
| 753 | 750 |
|
| 754 | 751 |
///Returns \c true if \c v is reachable from the root(s). |
| 755 | 752 |
///\pre Either \ref run() or \ref start() |
| 756 | 753 |
///must be called before using this function. |
| 757 | 754 |
bool reached(Node v) const { return (*_reached)[v]; }
|
| 758 | 755 |
|
| 759 | 756 |
///@} |
| 760 | 757 |
}; |
| 761 | 758 |
|
| 762 | 759 |
///Default traits class of dfs() function. |
| 763 | 760 |
|
| 764 | 761 |
///Default traits class of dfs() function. |
| 765 | 762 |
///\tparam GR Digraph type. |
| 766 | 763 |
template<class GR> |
| 767 | 764 |
struct DfsWizardDefaultTraits |
| 768 | 765 |
{
|
| 769 | 766 |
///The type of the digraph the algorithm runs on. |
| 770 | 767 |
typedef GR Digraph; |
| 771 | 768 |
|
| 772 | 769 |
///\brief The type of the map that stores the predecessor |
| 773 | 770 |
///arcs of the %DFS paths. |
| 774 | 771 |
/// |
| 775 | 772 |
///The type of the map that stores the predecessor |
| 776 | 773 |
///arcs of the %DFS paths. |
| 777 | 774 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 778 | 775 |
/// |
| 779 | 776 |
typedef NullMap<typename Digraph::Node,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 |
#ifdef DOXYGEN |
| 787 | 783 |
static PredMap *createPredMap(const Digraph &g) |
| 788 | 784 |
#else |
| 789 | 785 |
static PredMap *createPredMap(const Digraph &) |
| 790 | 786 |
#endif |
| 791 | 787 |
{
|
| 792 | 788 |
return new PredMap(); |
| 793 | 789 |
} |
| 794 | 790 |
|
| 795 | 791 |
///The type of the map that indicates which nodes are processed. |
| 796 | 792 |
|
| 797 | 793 |
///The type of the map that indicates which nodes are processed. |
| 798 | 794 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 799 | 795 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 800 | 796 |
///Instantiates a \ref ProcessedMap. |
| 801 | 797 |
|
| 802 | 798 |
///This function instantiates a \ref ProcessedMap. |
| 803 | 799 |
///\param g is the digraph, to which |
| 804 | 800 |
///we would like to define the \ref ProcessedMap. |
| 805 | 801 |
#ifdef DOXYGEN |
| 806 | 802 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 807 | 803 |
#else |
| 808 | 804 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 809 | 805 |
#endif |
| 810 | 806 |
{
|
| 811 | 807 |
return new ProcessedMap(); |
| 812 | 808 |
} |
| 813 | 809 |
|
| 814 | 810 |
///The type of the map that indicates which nodes are reached. |
| 815 | 811 |
|
| 816 | 812 |
///The type of the map that indicates which nodes are reached. |
| 817 | 813 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 818 | 814 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 819 | 815 |
///Instantiates a \ref ReachedMap. |
| 820 | 816 |
|
| 821 | 817 |
///This function instantiates a \ref ReachedMap. |
| 822 | 818 |
///\param g is the digraph, to which |
| 823 | 819 |
///we would like to define the \ref ReachedMap. |
| 824 | 820 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 825 | 821 |
{
|
| 826 | 822 |
return new ReachedMap(g); |
| 827 | 823 |
} |
| 828 | 824 |
|
| 829 | 825 |
///The type of the map that stores the distances of the nodes. |
| 830 | 826 |
|
| 831 | 827 |
///The type of the map that stores the distances of the nodes. |
| 832 | 828 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 833 | 829 |
/// |
| ... | ... |
@@ -1234,98 +1230,97 @@ |
| 1234 | 1230 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
| 1235 | 1231 |
#endif |
| 1236 | 1232 |
class DfsVisit {
|
| 1237 | 1233 |
public: |
| 1238 | 1234 |
|
| 1239 | 1235 |
/// \brief \ref Exception for uninitialized parameters. |
| 1240 | 1236 |
/// |
| 1241 | 1237 |
/// This error represents problems in the initialization |
| 1242 | 1238 |
/// of the parameters of the algorithm. |
| 1243 | 1239 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
| 1244 | 1240 |
public: |
| 1245 | 1241 |
virtual const char* what() const throw() |
| 1246 | 1242 |
{
|
| 1247 | 1243 |
return "lemon::DfsVisit::UninitializedParameter"; |
| 1248 | 1244 |
} |
| 1249 | 1245 |
}; |
| 1250 | 1246 |
|
| 1251 | 1247 |
///The traits class. |
| 1252 | 1248 |
typedef _Traits Traits; |
| 1253 | 1249 |
|
| 1254 | 1250 |
///The type of the digraph the algorithm runs on. |
| 1255 | 1251 |
typedef typename Traits::Digraph Digraph; |
| 1256 | 1252 |
|
| 1257 | 1253 |
///The visitor type used by the algorithm. |
| 1258 | 1254 |
typedef _Visitor Visitor; |
| 1259 | 1255 |
|
| 1260 | 1256 |
///The type of the map that indicates which nodes are reached. |
| 1261 | 1257 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1262 | 1258 |
|
| 1263 | 1259 |
private: |
| 1264 | 1260 |
|
| 1265 | 1261 |
typedef typename Digraph::Node Node; |
| 1266 | 1262 |
typedef typename Digraph::NodeIt NodeIt; |
| 1267 | 1263 |
typedef typename Digraph::Arc Arc; |
| 1268 | 1264 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1269 | 1265 |
|
| 1270 | 1266 |
//Pointer to the underlying digraph. |
| 1271 | 1267 |
const Digraph *_digraph; |
| 1272 | 1268 |
//Pointer to the visitor object. |
| 1273 | 1269 |
Visitor *_visitor; |
| 1274 | 1270 |
//Pointer to the map of reached status of the nodes. |
| 1275 | 1271 |
ReachedMap *_reached; |
| 1276 | 1272 |
//Indicates if _reached is locally allocated (true) or not. |
| 1277 | 1273 |
bool local_reached; |
| 1278 | 1274 |
|
| 1279 | 1275 |
std::vector<typename Digraph::Arc> _stack; |
| 1280 | 1276 |
int _stack_head; |
| 1281 | 1277 |
|
| 1282 |
///Creates the maps if necessary. |
|
| 1283 |
///\todo Better memory allocation (instead of new). |
|
| 1278 |
//Creates the maps if necessary. |
|
| 1284 | 1279 |
void create_maps() {
|
| 1285 | 1280 |
if(!_reached) {
|
| 1286 | 1281 |
local_reached = true; |
| 1287 | 1282 |
_reached = Traits::createReachedMap(*_digraph); |
| 1288 | 1283 |
} |
| 1289 | 1284 |
} |
| 1290 | 1285 |
|
| 1291 | 1286 |
protected: |
| 1292 | 1287 |
|
| 1293 | 1288 |
DfsVisit() {}
|
| 1294 | 1289 |
|
| 1295 | 1290 |
public: |
| 1296 | 1291 |
|
| 1297 | 1292 |
typedef DfsVisit Create; |
| 1298 | 1293 |
|
| 1299 | 1294 |
/// \name Named template parameters |
| 1300 | 1295 |
|
| 1301 | 1296 |
///@{
|
| 1302 | 1297 |
template <class T> |
| 1303 | 1298 |
struct SetReachedMapTraits : public Traits {
|
| 1304 | 1299 |
typedef T ReachedMap; |
| 1305 | 1300 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1306 | 1301 |
throw UninitializedParameter(); |
| 1307 | 1302 |
} |
| 1308 | 1303 |
}; |
| 1309 | 1304 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1310 | 1305 |
/// ReachedMap type. |
| 1311 | 1306 |
/// |
| 1312 | 1307 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1313 | 1308 |
template <class T> |
| 1314 | 1309 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
| 1315 | 1310 |
SetReachedMapTraits<T> > {
|
| 1316 | 1311 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1317 | 1312 |
}; |
| 1318 | 1313 |
///@} |
| 1319 | 1314 |
|
| 1320 | 1315 |
public: |
| 1321 | 1316 |
|
| 1322 | 1317 |
/// \brief Constructor. |
| 1323 | 1318 |
/// |
| 1324 | 1319 |
/// Constructor. |
| 1325 | 1320 |
/// |
| 1326 | 1321 |
/// \param digraph The digraph the algorithm runs on. |
| 1327 | 1322 |
/// \param visitor The visitor object of the algorithm. |
| 1328 | 1323 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1329 | 1324 |
: _digraph(&digraph), _visitor(&visitor), |
| 1330 | 1325 |
_reached(0), local_reached(false) {}
|
| 1331 | 1326 |
| ... | ... |
@@ -98,109 +98,106 @@ |
| 98 | 98 |
/// Operation traits for Dijkstra algorithm. |
| 99 | 99 |
|
| 100 | 100 |
/// This class defines the operations that are used in the algorithm. |
| 101 | 101 |
/// \see DijkstraDefaultOperationTraits |
| 102 | 102 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
| 103 | 103 |
|
| 104 | 104 |
/// The cross reference type used by the heap. |
| 105 | 105 |
|
| 106 | 106 |
/// The cross reference type used by the heap. |
| 107 | 107 |
/// Usually it is \c Digraph::NodeMap<int>. |
| 108 | 108 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 109 | 109 |
///Instantiates a \ref HeapCrossRef. |
| 110 | 110 |
|
| 111 | 111 |
///This function instantiates a \ref HeapCrossRef. |
| 112 | 112 |
/// \param g is the digraph, to which we would like to define the |
| 113 | 113 |
/// \ref HeapCrossRef. |
| 114 | 114 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
| 115 | 115 |
{
|
| 116 | 116 |
return new HeapCrossRef(g); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
///The heap type used by the Dijkstra algorithm. |
| 120 | 120 |
|
| 121 | 121 |
///The heap type used by the Dijkstra algorithm. |
| 122 | 122 |
/// |
| 123 | 123 |
///\sa BinHeap |
| 124 | 124 |
///\sa Dijkstra |
| 125 | 125 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
| 126 | 126 |
///Instantiates a \ref Heap. |
| 127 | 127 |
|
| 128 | 128 |
///This function instantiates a \ref Heap. |
| 129 | 129 |
static Heap *createHeap(HeapCrossRef& r) |
| 130 | 130 |
{
|
| 131 | 131 |
return new Heap(r); |
| 132 | 132 |
} |
| 133 | 133 |
|
| 134 | 134 |
///\brief The type of the map that stores the predecessor |
| 135 | 135 |
///arcs of the shortest paths. |
| 136 | 136 |
/// |
| 137 | 137 |
///The type of the map that stores the predecessor |
| 138 | 138 |
///arcs of the shortest paths. |
| 139 | 139 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 140 | 140 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 141 | 141 |
///Instantiates a \ref PredMap. |
| 142 | 142 |
|
| 143 | 143 |
///This function instantiates a \ref PredMap. |
| 144 | 144 |
///\param g is the digraph, to which we would like to define the |
| 145 | 145 |
///\ref PredMap. |
| 146 |
///\todo The digraph alone may be insufficient for the initialization |
|
| 147 | 146 |
static PredMap *createPredMap(const Digraph &g) |
| 148 | 147 |
{
|
| 149 | 148 |
return new PredMap(g); |
| 150 | 149 |
} |
| 151 | 150 |
|
| 152 | 151 |
///The type of the map that indicates which nodes are processed. |
| 153 | 152 |
|
| 154 | 153 |
///The type of the map that indicates which nodes are processed. |
| 155 | 154 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 156 | 155 |
///By default it is a NullMap. |
| 157 |
///\todo If it is set to a real map, |
|
| 158 |
///Dijkstra::processed() should read this. |
|
| 159 | 156 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 160 | 157 |
///Instantiates a \ref ProcessedMap. |
| 161 | 158 |
|
| 162 | 159 |
///This function instantiates a \ref ProcessedMap. |
| 163 | 160 |
///\param g is the digraph, to which |
| 164 | 161 |
///we would like to define the \ref ProcessedMap |
| 165 | 162 |
#ifdef DOXYGEN |
| 166 | 163 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 167 | 164 |
#else |
| 168 | 165 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 169 | 166 |
#endif |
| 170 | 167 |
{
|
| 171 | 168 |
return new ProcessedMap(); |
| 172 | 169 |
} |
| 173 | 170 |
|
| 174 | 171 |
///The type of the map that stores the distances of the nodes. |
| 175 | 172 |
|
| 176 | 173 |
///The type of the map that stores the distances of the nodes. |
| 177 | 174 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 178 | 175 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
| 179 | 176 |
///Instantiates a \ref DistMap. |
| 180 | 177 |
|
| 181 | 178 |
///This function instantiates a \ref DistMap. |
| 182 | 179 |
///\param g is the digraph, to which we would like to define |
| 183 | 180 |
///the \ref DistMap |
| 184 | 181 |
static DistMap *createDistMap(const Digraph &g) |
| 185 | 182 |
{
|
| 186 | 183 |
return new DistMap(g); |
| 187 | 184 |
} |
| 188 | 185 |
}; |
| 189 | 186 |
|
| 190 | 187 |
///%Dijkstra algorithm class. |
| 191 | 188 |
|
| 192 | 189 |
/// \ingroup shortest_path |
| 193 | 190 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
| 194 | 191 |
/// |
| 195 | 192 |
///The arc lengths are passed to the algorithm using a |
| 196 | 193 |
///\ref concepts::ReadMap "ReadMap", |
| 197 | 194 |
///so it is easy to change it to any kind of length. |
| 198 | 195 |
///The type of the length is determined by the |
| 199 | 196 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
| 200 | 197 |
///It is also possible to change the underlying priority heap. |
| 201 | 198 |
/// |
| 202 | 199 |
///There is also a \ref dijkstra() "function type interface" for the |
| 203 | 200 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
| 204 | 201 |
///it can be used easier. |
| 205 | 202 |
/// |
| 206 | 203 |
///\tparam GR The type of the digraph the algorithm runs on. |
| ... | ... |
@@ -251,98 +248,97 @@ |
| 251 | 248 |
///The type of the map that stores the distances of the nodes. |
| 252 | 249 |
typedef typename TR::DistMap DistMap; |
| 253 | 250 |
///The type of the map that indicates which nodes are processed. |
| 254 | 251 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 255 | 252 |
///The type of the paths. |
| 256 | 253 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 257 | 254 |
///The cross reference type used for the current heap. |
| 258 | 255 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
| 259 | 256 |
///The heap type used by the algorithm. |
| 260 | 257 |
typedef typename TR::Heap Heap; |
| 261 | 258 |
///The operation traits class. |
| 262 | 259 |
typedef typename TR::OperationTraits OperationTraits; |
| 263 | 260 |
|
| 264 | 261 |
///The traits class. |
| 265 | 262 |
typedef TR Traits; |
| 266 | 263 |
|
| 267 | 264 |
private: |
| 268 | 265 |
|
| 269 | 266 |
typedef typename Digraph::Node Node; |
| 270 | 267 |
typedef typename Digraph::NodeIt NodeIt; |
| 271 | 268 |
typedef typename Digraph::Arc Arc; |
| 272 | 269 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 273 | 270 |
|
| 274 | 271 |
//Pointer to the underlying digraph. |
| 275 | 272 |
const Digraph *G; |
| 276 | 273 |
//Pointer to the length map. |
| 277 | 274 |
const LengthMap *length; |
| 278 | 275 |
//Pointer to the map of predecessors arcs. |
| 279 | 276 |
PredMap *_pred; |
| 280 | 277 |
//Indicates if _pred is locally allocated (true) or not. |
| 281 | 278 |
bool local_pred; |
| 282 | 279 |
//Pointer to the map of distances. |
| 283 | 280 |
DistMap *_dist; |
| 284 | 281 |
//Indicates if _dist is locally allocated (true) or not. |
| 285 | 282 |
bool local_dist; |
| 286 | 283 |
//Pointer to the map of processed status of the nodes. |
| 287 | 284 |
ProcessedMap *_processed; |
| 288 | 285 |
//Indicates if _processed is locally allocated (true) or not. |
| 289 | 286 |
bool local_processed; |
| 290 | 287 |
//Pointer to the heap cross references. |
| 291 | 288 |
HeapCrossRef *_heap_cross_ref; |
| 292 | 289 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
| 293 | 290 |
bool local_heap_cross_ref; |
| 294 | 291 |
//Pointer to the heap. |
| 295 | 292 |
Heap *_heap; |
| 296 | 293 |
//Indicates if _heap is locally allocated (true) or not. |
| 297 | 294 |
bool local_heap; |
| 298 | 295 |
|
| 299 |
///Creates the maps if necessary. |
|
| 300 |
///\todo Better memory allocation (instead of new). |
|
| 296 |
//Creates the maps if necessary. |
|
| 301 | 297 |
void create_maps() |
| 302 | 298 |
{
|
| 303 | 299 |
if(!_pred) {
|
| 304 | 300 |
local_pred = true; |
| 305 | 301 |
_pred = Traits::createPredMap(*G); |
| 306 | 302 |
} |
| 307 | 303 |
if(!_dist) {
|
| 308 | 304 |
local_dist = true; |
| 309 | 305 |
_dist = Traits::createDistMap(*G); |
| 310 | 306 |
} |
| 311 | 307 |
if(!_processed) {
|
| 312 | 308 |
local_processed = true; |
| 313 | 309 |
_processed = Traits::createProcessedMap(*G); |
| 314 | 310 |
} |
| 315 | 311 |
if (!_heap_cross_ref) {
|
| 316 | 312 |
local_heap_cross_ref = true; |
| 317 | 313 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
| 318 | 314 |
} |
| 319 | 315 |
if (!_heap) {
|
| 320 | 316 |
local_heap = true; |
| 321 | 317 |
_heap = Traits::createHeap(*_heap_cross_ref); |
| 322 | 318 |
} |
| 323 | 319 |
} |
| 324 | 320 |
|
| 325 | 321 |
public: |
| 326 | 322 |
|
| 327 | 323 |
typedef Dijkstra Create; |
| 328 | 324 |
|
| 329 | 325 |
///\name Named template parameters |
| 330 | 326 |
|
| 331 | 327 |
///@{
|
| 332 | 328 |
|
| 333 | 329 |
template <class T> |
| 334 | 330 |
struct SetPredMapTraits : public Traits {
|
| 335 | 331 |
typedef T PredMap; |
| 336 | 332 |
static PredMap *createPredMap(const Digraph &) |
| 337 | 333 |
{
|
| 338 | 334 |
throw UninitializedParameter(); |
| 339 | 335 |
} |
| 340 | 336 |
}; |
| 341 | 337 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 342 | 338 |
///\ref PredMap type. |
| 343 | 339 |
/// |
| 344 | 340 |
///\ref named-templ-param "Named parameter" for setting |
| 345 | 341 |
///\ref PredMap type. |
| 346 | 342 |
template <class T> |
| 347 | 343 |
struct SetPredMap |
| 348 | 344 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
|
| ... | ... |
@@ -912,194 +908,188 @@ |
| 912 | 908 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
|
| 913 | 909 |
Heap::POST_HEAP; } |
| 914 | 910 |
|
| 915 | 911 |
///The current distance of a node from the root(s). |
| 916 | 912 |
|
| 917 | 913 |
///Returns the current distance of a node from the root(s). |
| 918 | 914 |
///It may be decreased in the following processes. |
| 919 | 915 |
///\pre \c v should be reached but not processed. |
| 920 | 916 |
Value currentDist(Node v) const { return (*_heap)[v]; }
|
| 921 | 917 |
|
| 922 | 918 |
///@} |
| 923 | 919 |
}; |
| 924 | 920 |
|
| 925 | 921 |
|
| 926 | 922 |
///Default traits class of dijkstra() function. |
| 927 | 923 |
|
| 928 | 924 |
///Default traits class of dijkstra() function. |
| 929 | 925 |
///\tparam GR The type of the digraph. |
| 930 | 926 |
///\tparam LM The type of the length map. |
| 931 | 927 |
template<class GR, class LM> |
| 932 | 928 |
struct DijkstraWizardDefaultTraits |
| 933 | 929 |
{
|
| 934 | 930 |
///The type of the digraph the algorithm runs on. |
| 935 | 931 |
typedef GR Digraph; |
| 936 | 932 |
///The type of the map that stores the arc lengths. |
| 937 | 933 |
|
| 938 | 934 |
///The type of the map that stores the arc lengths. |
| 939 | 935 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 940 | 936 |
typedef LM LengthMap; |
| 941 | 937 |
///The type of the length of the arcs. |
| 942 | 938 |
typedef typename LM::Value Value; |
| 943 | 939 |
|
| 944 | 940 |
/// Operation traits for Dijkstra algorithm. |
| 945 | 941 |
|
| 946 | 942 |
/// This class defines the operations that are used in the algorithm. |
| 947 | 943 |
/// \see DijkstraDefaultOperationTraits |
| 948 | 944 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
| 949 | 945 |
|
| 950 | 946 |
/// The cross reference type used by the heap. |
| 951 | 947 |
|
| 952 | 948 |
/// The cross reference type used by the heap. |
| 953 | 949 |
/// Usually it is \c Digraph::NodeMap<int>. |
| 954 | 950 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 955 | 951 |
///Instantiates a \ref HeapCrossRef. |
| 956 | 952 |
|
| 957 | 953 |
///This function instantiates a \ref HeapCrossRef. |
| 958 | 954 |
/// \param g is the digraph, to which we would like to define the |
| 959 | 955 |
/// HeapCrossRef. |
| 960 |
/// \todo The digraph alone may be insufficient for the initialization |
|
| 961 | 956 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
| 962 | 957 |
{
|
| 963 | 958 |
return new HeapCrossRef(g); |
| 964 | 959 |
} |
| 965 | 960 |
|
| 966 | 961 |
///The heap type used by the Dijkstra algorithm. |
| 967 | 962 |
|
| 968 | 963 |
///The heap type used by the Dijkstra algorithm. |
| 969 | 964 |
/// |
| 970 | 965 |
///\sa BinHeap |
| 971 | 966 |
///\sa Dijkstra |
| 972 | 967 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
| 973 | 968 |
std::less<Value> > Heap; |
| 974 | 969 |
|
| 975 | 970 |
///Instantiates a \ref Heap. |
| 976 | 971 |
|
| 977 | 972 |
///This function instantiates a \ref Heap. |
| 978 | 973 |
/// \param r is the HeapCrossRef which is used. |
| 979 | 974 |
static Heap *createHeap(HeapCrossRef& r) |
| 980 | 975 |
{
|
| 981 | 976 |
return new Heap(r); |
| 982 | 977 |
} |
| 983 | 978 |
|
| 984 | 979 |
///\brief The type of the map that stores the predecessor |
| 985 | 980 |
///arcs of the shortest paths. |
| 986 | 981 |
/// |
| 987 | 982 |
///The type of the map that stores the predecessor |
| 988 | 983 |
///arcs of the shortest paths. |
| 989 | 984 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 990 | 985 |
typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap; |
| 991 | 986 |
///Instantiates a \ref PredMap. |
| 992 | 987 |
|
| 993 | 988 |
///This function instantiates a \ref PredMap. |
| 994 | 989 |
///\param g is the digraph, to which we would like to define the |
| 995 | 990 |
///\ref PredMap. |
| 996 |
///\todo The digraph alone may be insufficient to initialize |
|
| 997 | 991 |
#ifdef DOXYGEN |
| 998 | 992 |
static PredMap *createPredMap(const Digraph &g) |
| 999 | 993 |
#else |
| 1000 | 994 |
static PredMap *createPredMap(const Digraph &) |
| 1001 | 995 |
#endif |
| 1002 | 996 |
{
|
| 1003 | 997 |
return new PredMap(); |
| 1004 | 998 |
} |
| 1005 | 999 |
|
| 1006 | 1000 |
///The type of the map that indicates which nodes are processed. |
| 1007 | 1001 |
|
| 1008 | 1002 |
///The type of the map that indicates which nodes are processed. |
| 1009 | 1003 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 1010 | 1004 |
///By default it is a NullMap. |
| 1011 |
///\todo If it is set to a real map, |
|
| 1012 |
///Dijkstra::processed() should read this. |
|
| 1013 |
///\todo named parameter to set this type, function to read and write. |
|
| 1014 | 1005 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 1015 | 1006 |
///Instantiates a \ref ProcessedMap. |
| 1016 | 1007 |
|
| 1017 | 1008 |
///This function instantiates a \ref ProcessedMap. |
| 1018 | 1009 |
///\param g is the digraph, to which |
| 1019 | 1010 |
///we would like to define the \ref ProcessedMap. |
| 1020 | 1011 |
#ifdef DOXYGEN |
| 1021 | 1012 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 1022 | 1013 |
#else |
| 1023 | 1014 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 1024 | 1015 |
#endif |
| 1025 | 1016 |
{
|
| 1026 | 1017 |
return new ProcessedMap(); |
| 1027 | 1018 |
} |
| 1028 | 1019 |
|
| 1029 | 1020 |
///The type of the map that stores the distances of the nodes. |
| 1030 | 1021 |
|
| 1031 | 1022 |
///The type of the map that stores the distances of the nodes. |
| 1032 | 1023 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 1033 | 1024 |
typedef NullMap<typename Digraph::Node,Value> DistMap; |
| 1034 | 1025 |
///Instantiates a \ref DistMap. |
| 1035 | 1026 |
|
| 1036 | 1027 |
///This function instantiates a \ref DistMap. |
| 1037 | 1028 |
///\param g is the digraph, to which we would like to define |
| 1038 | 1029 |
///the \ref DistMap |
| 1039 | 1030 |
#ifdef DOXYGEN |
| 1040 | 1031 |
static DistMap *createDistMap(const Digraph &g) |
| 1041 | 1032 |
#else |
| 1042 | 1033 |
static DistMap *createDistMap(const Digraph &) |
| 1043 | 1034 |
#endif |
| 1044 | 1035 |
{
|
| 1045 | 1036 |
return new DistMap(); |
| 1046 | 1037 |
} |
| 1047 | 1038 |
}; |
| 1048 | 1039 |
|
| 1049 | 1040 |
/// Default traits class used by \ref DijkstraWizard |
| 1050 | 1041 |
|
| 1051 | 1042 |
/// To make it easier to use Dijkstra algorithm |
| 1052 | 1043 |
/// we have created a wizard class. |
| 1053 | 1044 |
/// This \ref DijkstraWizard class needs default traits, |
| 1054 | 1045 |
/// as well as the \ref Dijkstra class. |
| 1055 | 1046 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
| 1056 | 1047 |
/// \ref DijkstraWizard class. |
| 1057 |
/// \todo More named parameters are required... |
|
| 1058 | 1048 |
template<class GR,class LM> |
| 1059 | 1049 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
| 1060 | 1050 |
{
|
| 1061 | 1051 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
| 1062 | 1052 |
protected: |
| 1063 | 1053 |
//The type of the nodes in the digraph. |
| 1064 | 1054 |
typedef typename Base::Digraph::Node Node; |
| 1065 | 1055 |
|
| 1066 | 1056 |
//Pointer to the digraph the algorithm runs on. |
| 1067 | 1057 |
void *_g; |
| 1068 | 1058 |
//Pointer to the length map |
| 1069 | 1059 |
void *_length; |
| 1070 | 1060 |
//Pointer to the map of processed nodes. |
| 1071 | 1061 |
void *_processed; |
| 1072 | 1062 |
//Pointer to the map of predecessors arcs. |
| 1073 | 1063 |
void *_pred; |
| 1074 | 1064 |
//Pointer to the map of distances. |
| 1075 | 1065 |
void *_dist; |
| 1076 | 1066 |
//Pointer to the source node. |
| 1077 | 1067 |
Node _source; |
| 1078 | 1068 |
|
| 1079 | 1069 |
public: |
| 1080 | 1070 |
/// Constructor. |
| 1081 | 1071 |
|
| 1082 | 1072 |
/// This constructor does not require parameters, therefore it initiates |
| 1083 | 1073 |
/// all of the attributes to default values (0, INVALID). |
| 1084 | 1074 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
| 1085 | 1075 |
_dist(0), _source(INVALID) {}
|
| 1086 | 1076 |
|
| 1087 | 1077 |
/// Constructor. |
| 1088 | 1078 |
|
| 1089 | 1079 |
/// This constructor requires some parameters, |
| 1090 | 1080 |
/// listed in the parameters list. |
| 1091 | 1081 |
/// Others are initiated to 0. |
| 1092 | 1082 |
/// \param g The digraph the algorithm runs on. |
| 1093 | 1083 |
/// \param l The length map. |
| 1094 | 1084 |
/// \param s The source node. |
| 1095 | 1085 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
| 1096 | 1086 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 1097 | 1087 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
| 1098 | 1088 |
_processed(0), _pred(0), _dist(0), _source(s) {}
|
| 1099 | 1089 |
|
| 1100 | 1090 |
}; |
| 1101 | 1091 |
|
| 1102 | 1092 |
/// Auxiliary class for the function type interface of Dijkstra algorithm. |
| 1103 | 1093 |
|
| 1104 | 1094 |
/// This auxiliary class is created to implement the function type |
| 1105 | 1095 |
/// interface of \ref Dijkstra algorithm. It uses the functions and features |
| ... | ... |
@@ -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 |
0 comments (0 inline)