Ticket #96: dijk_work_e8e874ee792b.patch
File dijk_work_e8e874ee792b.patch, 9.8 KB (added by , 16 years ago) |
---|
-
lemon/concepts/path.h
# HG changeset patch # User Peter Kovacs <kpeter@inf.elte.hu> # Date 1221525397 -7200 # Node ID e8e874ee792b40f2d967afb3e494f7e3f71848a9 # Parent c691064dfd4f2e3d4e230a1fd9845a1d96de2a5d Modify the function interface of dijkstra (ticket #96) - bug fix in concepts/path.h - make the source node a mandatory argument of dijkstra() - support s-t searches with dijkstra() diff -r c691064dfd4f -r e8e874ee792b lemon/concepts/path.h
a b 66 66 67 67 /// \brief Template assigment 68 68 template <typename CPath> 69 Path& operator=(const CPath& cpath) {} 69 Path& operator=(const CPath& cpath) { 70 ignore_unused_variable_warning(cpath); 71 return *this; 72 } 70 73 71 74 /// Length of the path ie. the number of arcs in the path. 72 75 int length() const { return 0;} -
lemon/dijkstra.h
diff -r c691064dfd4f -r e8e874ee792b lemon/dijkstra.h
a b 30 30 #include <lemon/core.h> 31 31 #include <lemon/error.h> 32 32 #include <lemon/maps.h> 33 #include <lemon/path.h> 33 34 34 35 namespace lemon { 35 36 … … 987 988 ///The type of the map that stores the predecessor 988 989 ///arcs of the shortest paths. 989 990 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 990 typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;991 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 991 992 ///Instantiates a \ref PredMap. 992 993 993 994 ///This function instantiates a \ref PredMap. 994 995 ///\param g is the digraph, to which we would like to define the 995 996 ///\ref PredMap. 996 997 ///\todo The digraph alone may be insufficient to initialize 997 #ifdef DOXYGEN998 998 static PredMap *createPredMap(const Digraph &g) 999 #else1000 static PredMap *createPredMap(const Digraph &)1001 #endif1002 999 { 1003 return new PredMap( );1000 return new PredMap(g); 1004 1001 } 1005 1002 1006 1003 ///The type of the map that indicates which nodes are processed. … … 1030 1027 1031 1028 ///The type of the map that stores the distances of the nodes. 1032 1029 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 1033 typedef NullMap<typename Digraph::Node,Value> DistMap;1030 typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; 1034 1031 ///Instantiates a \ref DistMap. 1035 1032 1036 1033 ///This function instantiates a \ref DistMap. 1037 1034 ///\param g is the digraph, to which we would like to define 1038 1035 ///the \ref DistMap 1039 #ifdef DOXYGEN1040 1036 static DistMap *createDistMap(const Digraph &g) 1041 #else1042 static DistMap *createDistMap(const Digraph &)1043 #endif1044 1037 { 1045 return new DistMap( );1038 return new DistMap(g); 1046 1039 } 1040 1041 ///The type of the shortest paths. 1042 1043 ///The type of the shortest paths. 1044 ///It must meet the \ref concepts::Path "Path" concept. 1045 typedef lemon::Path<Digraph> Path; 1047 1046 }; 1048 1047 1049 1048 /// Default traits class used by \ref DijkstraWizard … … 1065 1064 1066 1065 //Pointer to the digraph the algorithm runs on. 1067 1066 void *_g; 1068 //Pointer to the length map 1067 //Pointer to the length map. 1069 1068 void *_length; 1070 1069 //Pointer to the map of processed nodes. 1071 1070 void *_processed; … … 1073 1072 void *_pred; 1074 1073 //Pointer to the map of distances. 1075 1074 void *_dist; 1075 //Pointer to the shortest path to the target node. 1076 void *_path; 1077 //Pointer to the distance of the target node. 1078 void *_di; 1076 1079 //Pointer to the source node. 1077 1080 Node _source; 1081 //Pointer to the target node. 1082 Node _target; 1078 1083 1079 1084 public: 1080 1085 /// Constructor. … … 1082 1087 /// This constructor does not require parameters, therefore it initiates 1083 1088 /// all of the attributes to default values (0, INVALID). 1084 1089 DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), 1085 _dist(0), _source(INVALID) {} 1090 _dist(0), _path(0), _di(0), 1091 _source(INVALID), _target(INVALID) {} 1086 1092 1087 1093 /// Constructor. 1088 1094 … … 1092 1098 /// \param g The digraph the algorithm runs on. 1093 1099 /// \param l The length map. 1094 1100 /// \param s The source node. 1095 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : 1101 /// \param t The optional target node. 1102 DijkstraWizardBase(const GR &g,const LM &l, Node s, Node t=INVALID) : 1096 1103 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 1097 1104 _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), 1098 _processed(0), _pred(0), _dist(0), _source(s) {} 1105 _processed(0), _pred(0), _dist(0), _path(0), _di(0), 1106 _source(s), _target(t) {} 1099 1107 1100 1108 }; 1101 1109 … … 1144 1152 typedef typename TR::DistMap DistMap; 1145 1153 ///The type of the map that indicates which nodes are processed. 1146 1154 typedef typename TR::ProcessedMap ProcessedMap; 1155 ///The type of the shortest paths 1156 typedef typename TR::Path Path; 1147 1157 ///The heap type used by the dijkstra algorithm. 1148 1158 typedef typename TR::Heap Heap; 1149 1159 … … 1156 1166 1157 1167 /// Constructor that requires parameters. 1158 1168 /// These parameters will be the default values for the traits class. 1159 DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : 1160 TR(g,l,s) {} 1169 DijkstraWizard(const Digraph &g,const LengthMap &l, 1170 Node s, Node t=INVALID) : 1171 TR(g,l,s,t) {} 1161 1172 1162 1173 ///Copy constructor 1163 1174 DijkstraWizard(const TR &b) : TR(b) {} 1164 1175 1165 1176 ~DijkstraWizard() {} 1166 1177 1167 ///Runs Dijkstra algorithm from asource node.1178 ///Runs Dijkstra algorithm from the given source node. 1168 1179 1169 ///Runs Dijkstra algorithm from asource node.1170 /// The node can be given with the \ref source() function.1171 void run( )1180 ///Runs Dijkstra algorithm from the given source node. 1181 ///\param t The optional target node. 1182 void run(Node t=INVALID) 1172 1183 { 1184 if(t!=INVALID) Base::_target=t; 1173 1185 if(Base::_source==INVALID) throw UninitializedParameter(); 1174 1186 Dijkstra<Digraph,LengthMap,TR> 1175 1187 dij(*reinterpret_cast<const Digraph*>(Base::_g), … … 1180 1192 dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); 1181 1193 if(Base::_dist) 1182 1194 dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 1183 dij.run(Base::_source); 1184 } 1185 1186 ///Runs Dijkstra algorithm from the given node. 1187 1188 ///Runs Dijkstra algorithm from the given node. 1189 ///\param s is the given source. 1190 void run(Node s) 1191 { 1192 Base::_source=s; 1193 run(); 1194 } 1195 1196 /// Sets the source node, from which the Dijkstra algorithm runs. 1197 1198 /// Sets the source node, from which the Dijkstra algorithm runs. 1199 /// \param s is the source node. 1200 DijkstraWizard<TR> &source(Node s) 1201 { 1202 Base::_source=s; 1203 return *this; 1195 if(Base::_target!=INVALID) { 1196 dij.run(Base::_source, Base::_target); 1197 if (Base::_path) 1198 *reinterpret_cast<Path*>(Base::_path) = dij.path(Base::_target); 1199 if (Base::_di) 1200 *reinterpret_cast<Value*>(Base::_di) = dij.dist(Base::_target); 1201 } else { 1202 dij.run(Base::_source); 1203 } 1204 1204 } 1205 1205 1206 1206 template<class T> … … 1257 1257 return DijkstraWizard<SetDistMapBase<T> >(*this); 1258 1258 } 1259 1259 1260 template<class T> 1261 struct SetPathBase : public Base { 1262 typedef T Path; 1263 SetPathBase(const TR &b) : TR(b) {} 1264 }; 1265 ///\brief \ref named-templ-param "Named parameter" 1266 ///for getting the shortest path to the target node. 1267 /// 1268 ///\ref named-templ-param "Named parameter" 1269 ///for getting the shortest path to the target node. 1270 template<class T> 1271 DijkstraWizard<SetPathBase<T> > path(const T &t) 1272 { 1273 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); 1274 return DijkstraWizard<SetPathBase<T> >(*this); 1275 } 1276 1277 ///\brief \ref named-templ-param "Named parameter" 1278 ///for getting the distance of the target node. 1279 /// 1280 ///\ref named-templ-param "Named parameter" 1281 ///for getting the distance of the target node. 1282 DijkstraWizard dist(const Value &d) 1283 { 1284 Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); 1285 return *this; 1286 } 1287 1260 1288 }; 1261 1289 1262 1290 ///Function type interface for Dijkstra algorithm. … … 1278 1306 ///\sa Dijkstra 1279 1307 template<class GR, class LM> 1280 1308 DijkstraWizard<DijkstraWizardBase<GR,LM> > 1281 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) 1309 dijkstra(const GR &g, const LM &l, 1310 typename GR::Node s, typename GR::Node t=INVALID) 1282 1311 { 1283 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s );1312 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s,t); 1284 1313 } 1285 1314 1286 1315 } //END OF NAMESPACE LEMON -
test/dijkstra_test.cc
diff -r c691064dfd4f -r e8e874ee792b test/dijkstra_test.cc
a b 92 92 93 93 Digraph g; 94 94 dijkstra(g,LengthMap(),Node()).run(); 95 dijkstra(g,LengthMap()).source(Node()).run(); 96 dijkstra(g,LengthMap()) 97 .predMap(concepts::WriteMap<Node,Arc>()) 98 .distMap(concepts::WriteMap<Node,VType>()) 95 dijkstra(g,LengthMap(),Node()) 96 .predMap(concepts::ReadWriteMap<Node,Arc>()) 97 .distMap(concepts::ReadWriteMap<Node,VType>()) 98 .processedMap(concepts::WriteMap<Node,bool>()) 99 .run(); 100 dijkstra(g,LengthMap(),Node(),Node()) 101 .predMap(concepts::ReadWriteMap<Node,Arc>()) 102 .distMap(concepts::ReadWriteMap<Node,VType>()) 103 .processedMap(concepts::WriteMap<Node,bool>()) 104 .path(concepts::Path<Digraph>()).dist(VType()) 105 .run(); 106 dijkstra(g,LengthMap(),Node()) 107 .predMap(concepts::ReadWriteMap<Node,Arc>()) 108 .distMap(concepts::ReadWriteMap<Node,VType>()) 109 .processedMap(concepts::WriteMap<Node,bool>()) 110 .path(concepts::Path<Digraph>()).dist(VType()) 99 111 .run(Node()); 100 112 } 101 113 … … 152 164 153 165 { 154 166 NullMap<Node,Arc> myPredMap; 155 dijkstra(G,length ).predMap(myPredMap).run(s);167 dijkstra(G,length,s).predMap(myPredMap).run(); 156 168 } 157 169 } 158 170