gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fix processedMap() named parameter for dijkstra() (ticket #140)
0 1 0
default
1 file changed with 10 insertions and 4 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
... ...
@@ -975,299 +975,305 @@
975 975

	
976 976
    ///Instantiates a \ref Heap.
977 977

	
978 978
    ///This function instantiates a \ref Heap.
979 979
    /// \param r is the HeapCrossRef which is used.
980 980
    static Heap *createHeap(HeapCrossRef& r)
981 981
    {
982 982
      return new Heap(r);
983 983
    }
984 984

	
985 985
    ///\brief The type of the map that stores the predecessor
986 986
    ///arcs of the shortest paths.
987 987
    ///
988 988
    ///The type of the map that stores the predecessor
989 989
    ///arcs of the shortest paths.
990 990
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
991 991
    typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;
992 992
    ///Instantiates a \ref PredMap.
993 993

	
994 994
    ///This function instantiates a \ref PredMap.
995 995
    ///\param g is the digraph, to which we would like to define the
996 996
    ///\ref PredMap.
997 997
    ///\todo The digraph alone may be insufficient to initialize
998 998
#ifdef DOXYGEN
999 999
    static PredMap *createPredMap(const Digraph &g)
1000 1000
#else
1001 1001
    static PredMap *createPredMap(const Digraph &)
1002 1002
#endif
1003 1003
    {
1004 1004
      return new PredMap();
1005 1005
    }
1006 1006

	
1007 1007
    ///The type of the map that indicates which nodes are processed.
1008 1008

	
1009 1009
    ///The type of the map that indicates which nodes are processed.
1010 1010
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1011 1011
    ///By default it is a NullMap.
1012 1012
    ///\todo If it is set to a real map,
1013 1013
    ///Dijkstra::processed() should read this.
1014 1014
    ///\todo named parameter to set this type, function to read and write.
1015 1015
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
1016 1016
    ///Instantiates a \ref ProcessedMap.
1017 1017

	
1018 1018
    ///This function instantiates a \ref ProcessedMap.
1019 1019
    ///\param g is the digraph, to which
1020 1020
    ///we would like to define the \ref ProcessedMap.
1021 1021
#ifdef DOXYGEN
1022 1022
    static ProcessedMap *createProcessedMap(const Digraph &g)
1023 1023
#else
1024 1024
    static ProcessedMap *createProcessedMap(const Digraph &)
1025 1025
#endif
1026 1026
    {
1027 1027
      return new ProcessedMap();
1028 1028
    }
1029 1029

	
1030 1030
    ///The type of the map that stores the distances of the nodes.
1031 1031

	
1032 1032
    ///The type of the map that stores the distances of the nodes.
1033 1033
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1034 1034
    typedef NullMap<typename Digraph::Node,Value> DistMap;
1035 1035
    ///Instantiates a \ref DistMap.
1036 1036

	
1037 1037
    ///This function instantiates a \ref DistMap.
1038 1038
    ///\param g is the digraph, to which we would like to define
1039 1039
    ///the \ref DistMap
1040 1040
#ifdef DOXYGEN
1041 1041
    static DistMap *createDistMap(const Digraph &g)
1042 1042
#else
1043 1043
    static DistMap *createDistMap(const Digraph &)
1044 1044
#endif
1045 1045
    {
1046 1046
      return new DistMap();
1047 1047
    }
1048 1048
  };
1049 1049

	
1050 1050
  /// Default traits class used by \ref DijkstraWizard
1051 1051

	
1052 1052
  /// To make it easier to use Dijkstra algorithm
1053 1053
  /// we have created a wizard class.
1054 1054
  /// This \ref DijkstraWizard class needs default traits,
1055 1055
  /// as well as the \ref Dijkstra class.
1056 1056
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
1057 1057
  /// \ref DijkstraWizard class.
1058 1058
  /// \todo More named parameters are required...
1059 1059
  template<class GR,class LM>
1060 1060
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
1061 1061
  {
1062 1062
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
1063 1063
  protected:
1064 1064
    //The type of the nodes in the digraph.
1065 1065
    typedef typename Base::Digraph::Node Node;
1066 1066

	
1067 1067
    //Pointer to the digraph the algorithm runs on.
1068 1068
    void *_g;
1069 1069
    //Pointer to the length map
1070 1070
    void *_length;
1071
    //Pointer to the map of processed nodes.
1072
    void *_processed;
1071 1073
    //Pointer to the map of predecessors arcs.
1072 1074
    void *_pred;
1073 1075
    //Pointer to the map of distances.
1074 1076
    void *_dist;
1075 1077
    //Pointer to the source node.
1076 1078
    Node _source;
1077 1079

	
1078 1080
  public:
1079 1081
    /// Constructor.
1080 1082

	
1081 1083
    /// This constructor does not require parameters, therefore it initiates
1082 1084
    /// all of the attributes to default values (0, INVALID).
1083
    DijkstraWizardBase() : _g(0), _length(0), _pred(0),
1085
    DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
1084 1086
                           _dist(0), _source(INVALID) {}
1085 1087

	
1086 1088
    /// Constructor.
1087 1089

	
1088 1090
    /// This constructor requires some parameters,
1089 1091
    /// listed in the parameters list.
1090 1092
    /// Others are initiated to 0.
1091 1093
    /// \param g The digraph the algorithm runs on.
1092 1094
    /// \param l The length map.
1093 1095
    /// \param s The source node.
1094 1096
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
1095 1097
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
1096 1098
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
1097
      _pred(0), _dist(0), _source(s) {}
1099
      _processed(0), _pred(0), _dist(0), _source(s) {}
1098 1100

	
1099 1101
  };
1100 1102

	
1101 1103
  /// Auxiliary class for the function type interface of Dijkstra algorithm.
1102 1104

	
1103 1105
  /// This auxiliary class is created to implement the function type
1104 1106
  /// interface of \ref Dijkstra algorithm. It uses the functions and features
1105 1107
  /// of the plain \ref Dijkstra, but it is much simpler to use it.
1106 1108
  /// It should only be used through the \ref dijkstra() function, which makes
1107 1109
  /// it easier to use the algorithm.
1108 1110
  ///
1109 1111
  /// Simplicity means that the way to change the types defined
1110 1112
  /// in the traits class is based on functions that returns the new class
1111 1113
  /// and not on templatable built-in classes.
1112 1114
  /// When using the plain \ref Dijkstra
1113 1115
  /// the new class with the modified type comes from
1114 1116
  /// the original class by using the ::
1115 1117
  /// operator. In the case of \ref DijkstraWizard only
1116 1118
  /// a function have to be called, and it will
1117 1119
  /// return the needed class.
1118 1120
  ///
1119 1121
  /// It does not have own \ref run() method. When its \ref run() method
1120 1122
  /// is called, it initiates a plain \ref Dijkstra object, and calls the
1121 1123
  /// \ref Dijkstra::run() method of it.
1122 1124
  template<class TR>
1123 1125
  class DijkstraWizard : public TR
1124 1126
  {
1125 1127
    typedef TR Base;
1126 1128

	
1127 1129
    ///The type of the digraph the algorithm runs on.
1128 1130
    typedef typename TR::Digraph Digraph;
1129 1131

	
1130 1132
    typedef typename Digraph::Node Node;
1131 1133
    typedef typename Digraph::NodeIt NodeIt;
1132 1134
    typedef typename Digraph::Arc Arc;
1133 1135
    typedef typename Digraph::OutArcIt OutArcIt;
1134 1136

	
1135 1137
    ///The type of the map that stores the arc lengths.
1136 1138
    typedef typename TR::LengthMap LengthMap;
1137 1139
    ///The type of the length of the arcs.
1138 1140
    typedef typename LengthMap::Value Value;
1139 1141
    ///\brief The type of the map that stores the predecessor
1140 1142
    ///arcs of the shortest paths.
1141 1143
    typedef typename TR::PredMap PredMap;
1142 1144
    ///The type of the map that stores the distances of the nodes.
1143 1145
    typedef typename TR::DistMap DistMap;
1144 1146
    ///The type of the map that indicates which nodes are processed.
1145 1147
    typedef typename TR::ProcessedMap ProcessedMap;
1146 1148
    ///The heap type used by the dijkstra algorithm.
1147 1149
    typedef typename TR::Heap Heap;
1148 1150

	
1149 1151
  public:
1150 1152

	
1151 1153
    /// Constructor.
1152 1154
    DijkstraWizard() : TR() {}
1153 1155

	
1154 1156
    /// Constructor that requires parameters.
1155 1157

	
1156 1158
    /// Constructor that requires parameters.
1157 1159
    /// These parameters will be the default values for the traits class.
1158 1160
    DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
1159 1161
      TR(g,l,s) {}
1160 1162

	
1161 1163
    ///Copy constructor
1162 1164
    DijkstraWizard(const TR &b) : TR(b) {}
1163 1165

	
1164 1166
    ~DijkstraWizard() {}
1165 1167

	
1166 1168
    ///Runs Dijkstra algorithm from a source node.
1167 1169

	
1168 1170
    ///Runs Dijkstra algorithm from a source node.
1169 1171
    ///The node can be given with the \ref source() function.
1170 1172
    void run()
1171 1173
    {
1172 1174
      if(Base::_source==INVALID) throw UninitializedParameter();
1173 1175
      Dijkstra<Digraph,LengthMap,TR>
1174 1176
        dij(*reinterpret_cast<const Digraph*>(Base::_g),
1175 1177
            *reinterpret_cast<const LengthMap*>(Base::_length));
1176
      if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1177
      if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1178
      if(Base::_processed)
1179
        dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1180
      if(Base::_pred)
1181
        dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1182
      if(Base::_dist)
1183
        dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1178 1184
      dij.run(Base::_source);
1179 1185
    }
1180 1186

	
1181 1187
    ///Runs Dijkstra algorithm from the given node.
1182 1188

	
1183 1189
    ///Runs Dijkstra algorithm from the given node.
1184 1190
    ///\param s is the given source.
1185 1191
    void run(Node s)
1186 1192
    {
1187 1193
      Base::_source=s;
1188 1194
      run();
1189 1195
    }
1190 1196

	
1191 1197
    /// Sets the source node, from which the Dijkstra algorithm runs.
1192 1198

	
1193 1199
    /// Sets the source node, from which the Dijkstra algorithm runs.
1194 1200
    /// \param s is the source node.
1195 1201
    DijkstraWizard<TR> &source(Node s)
1196 1202
    {
1197 1203
      Base::_source=s;
1198 1204
      return *this;
1199 1205
    }
1200 1206

	
1201 1207
    template<class T>
1202 1208
    struct DefPredMapBase : public Base {
1203 1209
      typedef T PredMap;
1204 1210
      static PredMap *createPredMap(const Digraph &) { return 0; };
1205 1211
      DefPredMapBase(const TR &b) : TR(b) {}
1206 1212
    };
1207 1213
    ///\brief \ref named-templ-param "Named parameter"
1208 1214
    ///for setting \ref PredMap object.
1209 1215
    ///
1210 1216
    ///\ref named-templ-param "Named parameter"
1211 1217
    ///for setting \ref PredMap object.
1212 1218
    template<class T>
1213 1219
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
1214 1220
    {
1215 1221
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1216 1222
      return DijkstraWizard<DefPredMapBase<T> >(*this);
1217 1223
    }
1218 1224

	
1219 1225
    template<class T>
1220 1226
    struct DefProcessedMapBase : public Base {
1221 1227
      typedef T ProcessedMap;
1222 1228
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1223 1229
      DefProcessedMapBase(const TR &b) : TR(b) {}
1224 1230
    };
1225 1231
    ///\brief \ref named-templ-param "Named parameter"
1226 1232
    ///for setting \ref ProcessedMap object.
1227 1233
    ///
1228 1234
    /// \ref named-templ-param "Named parameter"
1229 1235
    ///for setting \ref ProcessedMap object.
1230 1236
    template<class T>
1231 1237
    DijkstraWizard<DefProcessedMapBase<T> > processedMap(const T &t)
1232 1238
    {
1233 1239
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1234 1240
      return DijkstraWizard<DefProcessedMapBase<T> >(*this);
1235 1241
    }
1236 1242

	
1237 1243
    template<class T>
1238 1244
    struct DefDistMapBase : public Base {
1239 1245
      typedef T DistMap;
1240 1246
      static DistMap *createDistMap(const Digraph &) { return 0; };
1241 1247
      DefDistMapBase(const TR &b) : TR(b) {}
1242 1248
    };
1243 1249
    ///\brief \ref named-templ-param "Named parameter"
1244 1250
    ///for setting \ref DistMap object.
1245 1251
    ///
1246 1252
    ///\ref named-templ-param "Named parameter"
1247 1253
    ///for setting \ref DistMap object.
1248 1254
    template<class T>
1249 1255
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
1250 1256
    {
1251 1257
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1252 1258
      return DijkstraWizard<DefDistMapBase<T> >(*this);
1253 1259
    }
1254 1260

	
1255 1261
  };
1256 1262

	
1257 1263
  ///Function type interface for Dijkstra algorithm.
1258 1264

	
1259 1265
  /// \ingroup shortest_path
1260 1266
  ///Function type interface for Dijkstra algorithm.
1261 1267
  ///
1262 1268
  ///This function also has several
1263 1269
  ///\ref named-templ-func-param "named parameters",
1264 1270
  ///they are declared as the members of class \ref DijkstraWizard.
1265 1271
  ///The following
1266 1272
  ///example shows how to use these parameters.
1267 1273
  ///\code
1268 1274
  ///  dijkstra(g,length,source).predMap(preds).run();
1269 1275
  ///\endcode
1270 1276
  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1271 1277
  ///to the end of the parameter list.
1272 1278
  ///\sa DijkstraWizard
1273 1279
  ///\sa Dijkstra
0 comments (0 inline)