src/work/alpar/dijkstra.h
changeset 1116 f97e1cbbd453
parent 1043 52a2201a88e9
child 1117 5767cc417f62
equal deleted inserted replaced
8:78a8852fb663 9:ee45c3a48a92
   141 #else
   141 #else
   142   template <typename GR=ListGraph,
   142   template <typename GR=ListGraph,
   143 	    typename LM=typename GR::template EdgeMap<int>,
   143 	    typename LM=typename GR::template EdgeMap<int>,
   144 	    typename TR=DijkstraDefaultTraits<GR,LM> >
   144 	    typename TR=DijkstraDefaultTraits<GR,LM> >
   145 #endif
   145 #endif
   146   class Dijkstra{
   146   class Dijkstra {
   147   public:
   147   public:
   148     typedef TR Traits;
   148     typedef TR Traits;
   149     ///The type of the underlying graph.
   149     ///The type of the underlying graph.
   150     typedef typename TR::Graph Graph;
   150     typedef typename TR::Graph Graph;
   151     ///\e
   151     ///\e
   211 	distance = Traits::createDistMap(*G);
   211 	distance = Traits::createDistMap(*G);
   212       }
   212       }
   213     }
   213     }
   214     
   214     
   215   public :
   215   public :
   216 
   216  
   217     template <class T>
   217     template <class T>
   218     struct SetPredMapTraits : public Traits {
   218     struct DefPredMapTraits : public Traits {
   219       typedef T PredMap;
   219       typedef T PredMap;
   220       ///\todo An exception should be thrown.
   220       ///\todo An exception should be thrown.
   221       ///
   221       ///
   222       static PredMap *createPredMap(const Graph &G) 
   222       static PredMap *createPredMap(const Graph &G) 
   223       {
   223       {
   229     ///\ref named-templ-param "Named parameter" for setting PredMap type
   229     ///\ref named-templ-param "Named parameter" for setting PredMap type
   230 
   230 
   231     ///\ref named-templ-param "Named parameter" for setting PredMap type
   231     ///\ref named-templ-param "Named parameter" for setting PredMap type
   232     ///
   232     ///
   233     template <class T>
   233     template <class T>
   234     class SetPredMap : public Dijkstra< Graph,
   234     class DefPredMap : public Dijkstra< Graph,
   235 					LengthMap,
   235 					LengthMap,
   236 					SetPredMapTraits<T> > { };
   236 					DefPredMapTraits<T> > { };
   237     
   237     
   238     template <class T>
   238     template <class T>
   239     struct SetPredNodeMapTraits : public Traits {
   239     struct DefPredNodeMapTraits : public Traits {
   240       typedef T PredNodeMap;
   240       typedef T PredNodeMap;
   241       ///\todo An exception should be thrown.
   241       ///\todo An exception should be thrown.
   242       ///
   242       ///
   243       static PredNodeMap *createPredNodeMap(const Graph &G) 
   243       static PredNodeMap *createPredNodeMap(const Graph &G) 
   244       {
   244       {
   250     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
   250     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
   251 
   251 
   252     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
   252     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
   253     ///
   253     ///
   254     template <class T>
   254     template <class T>
   255     class SetPredNodeMap : public Dijkstra< Graph,
   255     class DefPredNodeMap : public Dijkstra< Graph,
   256 					    LengthMap,
   256 					    LengthMap,
   257 					    SetPredNodeMapTraits<T> > { };
   257 					    DefPredNodeMapTraits<T> > { };
   258     
   258     
   259     template <class T>
   259     template <class T>
   260     struct SetDistMapTraits : public Traits {
   260     struct DefDistMapTraits : public Traits {
   261       typedef T DistMap;
   261       typedef T DistMap;
   262       ///\todo An exception should be thrown.
   262       ///\todo An exception should be thrown.
   263       ///
   263       ///
   264       static DistMap *createDistMap(const Graph &G) 
   264       static DistMap *createDistMap(const Graph &G) 
   265       {
   265       {
   271     ///\ref named-templ-param "Named parameter" for setting DistMap type
   271     ///\ref named-templ-param "Named parameter" for setting DistMap type
   272 
   272 
   273     ///\ref named-templ-param "Named parameter" for setting DistMap type
   273     ///\ref named-templ-param "Named parameter" for setting DistMap type
   274     ///
   274     ///
   275     template <class T>
   275     template <class T>
   276     class SetDistMap : public Dijkstra< Graph,
   276     class DefDistMap : public Dijkstra< Graph,
   277 					LengthMap,
   277 					LengthMap,
   278 					SetDistMapTraits<T> > { };
   278 					DefDistMapTraits<T> > { };
   279     
   279     
   280     ///Constructor.
   280     ///Constructor.
   281     
   281     
   282     ///\param _G the graph the algorithm will run on.
   282     ///\param _G the graph the algorithm will run on.
   283     ///\param _length the length map used by the algorithm.
   283     ///\param _length the length map used by the algorithm.
   298 
   298 
   299     ///Sets the length map.
   299     ///Sets the length map.
   300 
   300 
   301     ///Sets the length map.
   301     ///Sets the length map.
   302     ///\return <tt> (*this) </tt>
   302     ///\return <tt> (*this) </tt>
   303     Dijkstra &setLengthMap(const LengthMap &m) 
   303     Dijkstra &lengthMap(const LengthMap &m) 
   304     {
   304     {
   305       length = &m;
   305       length = &m;
   306       return *this;
   306       return *this;
   307     }
   307     }
   308 
   308 
   311     ///Sets the map storing the predecessor edges.
   311     ///Sets the map storing the predecessor edges.
   312     ///If you don't use this function before calling \ref run(),
   312     ///If you don't use this function before calling \ref run(),
   313     ///it will allocate one. The destuctor deallocates this
   313     ///it will allocate one. The destuctor deallocates this
   314     ///automatically allocated map, of course.
   314     ///automatically allocated map, of course.
   315     ///\return <tt> (*this) </tt>
   315     ///\return <tt> (*this) </tt>
   316     Dijkstra &setPredMap(PredMap &m) 
   316     Dijkstra &predMap(PredMap &m) 
   317     {
   317     {
   318       if(local_predecessor) {
   318       if(local_predecessor) {
   319 	delete predecessor;
   319 	delete predecessor;
   320 	local_predecessor=false;
   320 	local_predecessor=false;
   321       }
   321       }
   328     ///Sets the map storing the predecessor nodes.
   328     ///Sets the map storing the predecessor nodes.
   329     ///If you don't use this function before calling \ref run(),
   329     ///If you don't use this function before calling \ref run(),
   330     ///it will allocate one. The destuctor deallocates this
   330     ///it will allocate one. The destuctor deallocates this
   331     ///automatically allocated map, of course.
   331     ///automatically allocated map, of course.
   332     ///\return <tt> (*this) </tt>
   332     ///\return <tt> (*this) </tt>
   333     Dijkstra &setPredNodeMap(PredNodeMap &m) 
   333     Dijkstra &predNodeMap(PredNodeMap &m) 
   334     {
   334     {
   335       if(local_pred_node) {
   335       if(local_pred_node) {
   336 	delete pred_node;
   336 	delete pred_node;
   337 	local_pred_node=false;
   337 	local_pred_node=false;
   338       }
   338       }
   345     ///Sets the map storing the distances calculated by the algorithm.
   345     ///Sets the map storing the distances calculated by the algorithm.
   346     ///If you don't use this function before calling \ref run(),
   346     ///If you don't use this function before calling \ref run(),
   347     ///it will allocate one. The destuctor deallocates this
   347     ///it will allocate one. The destuctor deallocates this
   348     ///automatically allocated map, of course.
   348     ///automatically allocated map, of course.
   349     ///\return <tt> (*this) </tt>
   349     ///\return <tt> (*this) </tt>
   350     Dijkstra &setDistMap(DistMap &m) 
   350     Dijkstra &distMap(DistMap &m) 
   351     {
   351     {
   352       if(local_distance) {
   352       if(local_distance) {
   353 	delete distance;
   353 	delete distance;
   354 	local_distance=false;
   354 	local_distance=false;
   355       }
   355       }
   471     ///
   471     ///
   472     bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
   472     bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
   473     
   473     
   474   };
   474   };
   475 
   475 
       
   476   template<class GR,class LM>
       
   477   class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
       
   478   {
       
   479 
       
   480     typedef DijkstraDefaultTraits<GR,LM> Base;
       
   481   protected:
       
   482     /// Pointer to the underlying graph.
       
   483     void *_g;
       
   484     /// Pointer to the length map
       
   485     void *_length;
       
   486     ///Pointer to the map of predecessors edges.
       
   487     void *_pred;
       
   488     ///Pointer to the map of predecessors nodes.
       
   489     void *_predNode;
       
   490     ///Pointer to the map of distances.
       
   491     void *_dist;
       
   492     ///Pointer to the source node.
       
   493     void *_source;
       
   494 
       
   495     typedef typename Base::Graph::Node Node;
       
   496 
       
   497     public:
       
   498     DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
       
   499 		       _dist(0), _source(INVALID) {}
       
   500 
       
   501     DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
       
   502       _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
       
   503 		  _dist(0), _source((void *)&s) {}
       
   504 
       
   505   };
       
   506   
   476   ///\e
   507   ///\e
   477 
   508 
   478   ///\e
   509   ///\e
   479   ///
   510   ///
   480   template<class TR>
   511   template<class TR>
   481   class _Dijkstra 
   512   class DijkstraWizard : public TR
   482   {
   513   {
   483     typedef TR Traits;
   514     typedef TR Base;
   484 
   515 
   485     ///The type of the underlying graph.
   516     ///The type of the underlying graph.
   486     typedef typename TR::Graph Graph;
   517     typedef typename TR::Graph Graph;
   487     ///\e
   518     ///\e
   488     typedef typename Graph::Node Node;
   519     typedef typename Graph::Node Node;
   506     ///The type of the map that stores the dists of the nodes.
   537     ///The type of the map that stores the dists of the nodes.
   507     typedef typename TR::DistMap DistMap;
   538     typedef typename TR::DistMap DistMap;
   508 
   539 
   509     ///The heap type used by the dijkstra algorithm.
   540     ///The heap type used by the dijkstra algorithm.
   510     typedef typename TR::Heap Heap;
   541     typedef typename TR::Heap Heap;
   511 
       
   512     /// Pointer to the underlying graph.
       
   513     const Graph *G;
       
   514     /// Pointer to the length map
       
   515     const LengthMap *length;
       
   516     ///Pointer to the map of predecessors edges.
       
   517     PredMap *predecessor;
       
   518     ///Pointer to the map of predecessors nodes.
       
   519     PredNodeMap *pred_node;
       
   520     ///Pointer to the map of distances.
       
   521     DistMap *distance;
       
   522     
       
   523     Node source;
       
   524     
       
   525 public:
   542 public:
   526     _Dijkstra() : G(0), length(0), predecessor(0), pred_node(0),
   543     DijkstraWizard() : TR() {}
   527 		  distance(0), source(INVALID) {}
   544 
   528 
   545     DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
   529     _Dijkstra(const Graph &g,const LengthMap &l, Node s) :
   546       TR(g,l,s) {}
   530       G(&g), length(&l), predecessor(0), pred_node(0),
   547 
   531 		  distance(0), source(s) {}
   548     DijkstraWizard(const TR &b) : TR(b) {}
   532 
   549 
   533     ~_Dijkstra() 
   550     ~DijkstraWizard() {}
   534     {
   551 
   535       Dijkstra<Graph,LengthMap,TR> Dij(*G,*length);
   552     ///\e
   536       if(predecessor) Dij.setPredMap(*predecessor);
   553     void run()
   537       if(pred_node) Dij.setPredNodeMap(*pred_node);
   554     {
   538       if(distance) Dij.setDistMap(*distance);
   555       Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
   539       Dij.run(source);
   556       if(_pred) Dij.predMap(*(PredMap*)_pred);
       
   557       if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
       
   558       if(_dist) Dij.distMap(*(DistMap*)_dist);
       
   559       Dij.run(*(Node*)_source);
       
   560     }
       
   561 
       
   562     ///\e
       
   563     void run(Node s)
       
   564     {
       
   565       _source=(void *)&s;
       
   566       run();
   540     }
   567     }
   541 
   568 
   542     template<class T>
   569     template<class T>
   543     struct SetPredMapTraits : public Traits {typedef T PredMap;};
   570     struct DefPredMapBase : public Base {
       
   571       typedef T PredMap;
       
   572       static PredMap *createPredMap(const Graph &G) {};
       
   573     };
   544     
   574     
   545     ///\e
   575     ///\e
   546     template<class T>
   576     template<class T>
   547     _Dijkstra<SetPredMapTraits<T> > setPredMap(const T &t) 
   577     DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
   548     {
   578     {
   549       _Dijkstra<SetPredMapTraits<T> > r;
   579       _pred=(void *)&t;
   550       r.G=G;
   580       return DijkstraWizard<DefPredMapBase<T> >(*this);
   551       r.length=length;
   581     }
   552       r.predecessor=&t;
   582     
   553       r.pred_node=pred_node;
   583 
   554       r.distance=distance;
       
   555       r.source=source;
       
   556       return r;
       
   557     }
       
   558     
       
   559     template<class T>
   584     template<class T>
   560     struct SetPredNodeMapTraits :public Traits {typedef T PredNodeMap;};
   585     struct DefPredNodeMapBase : public Base {
       
   586       typedef T PredNodeMap;
       
   587       static PredNodeMap *createPredNodeMap(const Graph &G) {};
       
   588     };
       
   589     
   561     ///\e
   590     ///\e
   562     template<class T>
   591     template<class T>
   563     _Dijkstra<SetPredNodeMapTraits<T> > setPredNodeMap(const T &t) 
   592     DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
   564     {
   593     {
   565       _Dijkstra<SetPredNodeMapTraits<T> > r;
   594       _predNode=(void *)&t;
   566       r.G=G;
   595       return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
   567       r.length=length;
   596     }
   568       r.predecessor=predecessor;
   597    
   569       r.pred_node=&t;
       
   570       r.distance=distance;
       
   571       r.source=source;
       
   572       return r;
       
   573     }
       
   574     
       
   575     template<class T>
   598     template<class T>
   576     struct SetDistMapTraits : public Traits {typedef T DistMap;};
   599     struct DefDistMapBase : public Base {
       
   600       typedef T DistMap;
       
   601       static DistMap *createDistMap(const Graph &G) {};
       
   602     };
       
   603     
   577     ///\e
   604     ///\e
   578     template<class T>
   605     template<class T>
   579     _Dijkstra<SetDistMapTraits<T> > setDistMap(const T &t) 
   606     DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
   580     {
   607     {
   581       _Dijkstra<SetPredMapTraits<T> > r;
   608       _dist=(void *)&t;
   582       r.G=G;
   609       return DijkstraWizard<DefDistMapBase<T> >(*this);
   583       r.length=length;
   610     }
   584       r.predecessor=predecessor;
   611 
   585       r.pred_node=pred_node;
   612     ///\e
   586       r.distance=&t;
   613     DijkstraWizard<TR> &setSource(Node s) 
   587       r.source=source;
   614     {
   588       return r;
   615       source=(void *)&s;
   589     }
       
   590     
       
   591     ///\e
       
   592     _Dijkstra<TR> &setSource(Node s) 
       
   593     {
       
   594       source=s;
       
   595       return *this;
   616       return *this;
   596     }
   617     }
   597     
   618     
   598   };
   619   };
   599   
   620   
   600   ///\e
   621   ///\e
   601 
   622 
   602   ///\todo Please document...
   623   ///\todo Please document...
   603   ///
   624   ///
   604   template<class GR, class LM>
   625   template<class GR, class LM>
   605   _Dijkstra<DijkstraDefaultTraits<GR,LM> >
   626   DijkstraWizard<DijkstraWizardBase<GR,LM> >
   606   dijkstra(const GR &g,const LM &l,typename GR::Node s)
   627   dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
   607   {
   628   {
   608     return _Dijkstra<DijkstraDefaultTraits<GR,LM> >(g,l,s);
   629     return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
   609   }
   630   }
   610 
   631 
   611 /// @}
   632 /// @}
   612   
   633   
   613 } //END OF NAMESPACE LEMON
   634 } //END OF NAMESPACE LEMON