Changes in lemon/dfs.h [252:66644b9cd9eb:319:5e12d7734036] in lemon-1.2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/dfs.h
r252 r319 28 28 #include <lemon/core.h> 29 29 #include <lemon/error.h> 30 #include <lemon/assert.h>31 30 #include <lemon/maps.h> 31 #include <lemon/path.h> 32 32 33 33 namespace lemon { … … 50 50 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 51 51 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 52 ///Instantiates a \refPredMap.53 54 ///This function instantiates a \refPredMap.52 ///Instantiates a PredMap. 53 54 ///This function instantiates a PredMap. 55 55 ///\param g is the digraph, to which we would like to define the 56 ///\ref PredMap. 57 ///\todo The digraph alone may be insufficient to initialize 56 ///PredMap. 58 57 static PredMap *createPredMap(const Digraph &g) 59 58 { … … 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 ///Instantiates a \refProcessedMap.70 71 ///This function instantiates a \refProcessedMap.67 ///Instantiates a ProcessedMap. 68 69 ///This function instantiates a ProcessedMap. 72 70 ///\param g is the digraph, to which 73 ///we would like to define the \refProcessedMap71 ///we would like to define the ProcessedMap 74 72 #ifdef DOXYGEN 75 73 static ProcessedMap *createProcessedMap(const Digraph &g) … … 86 84 ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 87 85 typedef typename Digraph::template NodeMap<bool> ReachedMap; 88 ///Instantiates a \refReachedMap.89 90 ///This function instantiates a \refReachedMap.86 ///Instantiates a ReachedMap. 87 88 ///This function instantiates a ReachedMap. 91 89 ///\param g is the digraph, to which 92 ///we would like to define the \refReachedMap.90 ///we would like to define the ReachedMap. 93 91 static ReachedMap *createReachedMap(const Digraph &g) 94 92 { … … 101 99 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 102 100 typedef typename Digraph::template NodeMap<int> DistMap; 103 ///Instantiates a \refDistMap.104 105 ///This function instantiates a \refDistMap.101 ///Instantiates a DistMap. 102 103 ///This function instantiates a DistMap. 106 104 ///\param g is the digraph, to which we would like to define the 107 /// \refDistMap.105 ///DistMap. 108 106 static DistMap *createDistMap(const Digraph &g) 109 107 { … … 117 115 ///This class provides an efficient implementation of the %DFS algorithm. 118 116 /// 119 ///There is also a \ref dfs() "function 117 ///There is also a \ref dfs() "function-type interface" for the DFS 120 118 ///algorithm, which is convenient in the simplier cases and it can be 121 119 ///used easier. … … 138 136 class Dfs { 139 137 public: 140 ///\ref Exception for uninitialized parameters.141 142 ///This error represents problems in the initialization of the143 ///parameters of the algorithm.144 class UninitializedParameter : public lemon::UninitializedParameter {145 public:146 virtual const char* what() const throw() {147 return "lemon::Dfs::UninitializedParameter";148 }149 };150 138 151 139 ///The type of the digraph the algorithm runs on. … … 196 184 int _stack_head; 197 185 198 ///Creates the maps if necessary. 199 ///\todo Better memory allocation (instead of new). 186 //Creates the maps if necessary. 200 187 void create_maps() 201 188 { … … 231 218 232 219 template <class T> 233 struct DefPredMapTraits : public Traits {220 struct SetPredMapTraits : public Traits { 234 221 typedef T PredMap; 235 222 static PredMap *createPredMap(const Digraph &) 236 223 { 237 throw UninitializedParameter(); 224 LEMON_ASSERT(false, "PredMap is not initialized"); 225 return 0; // ignore warnings 238 226 } 239 227 }; 240 228 ///\brief \ref named-templ-param "Named parameter" for setting 241 /// \refPredMap type.229 ///PredMap type. 242 230 /// 243 231 ///\ref named-templ-param "Named parameter" for setting 244 /// \refPredMap type.232 ///PredMap type. 245 233 template <class T> 246 struct DefPredMap : public Dfs<Digraph, DefPredMapTraits<T> > {247 typedef Dfs<Digraph, DefPredMapTraits<T> > Create;234 struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { 235 typedef Dfs<Digraph, SetPredMapTraits<T> > Create; 248 236 }; 249 237 250 238 template <class T> 251 struct DefDistMapTraits : public Traits {239 struct SetDistMapTraits : public Traits { 252 240 typedef T DistMap; 253 241 static DistMap *createDistMap(const Digraph &) 254 242 { 255 throw UninitializedParameter(); 243 LEMON_ASSERT(false, "DistMap is not initialized"); 244 return 0; // ignore warnings 256 245 } 257 246 }; 258 247 ///\brief \ref named-templ-param "Named parameter" for setting 259 /// \refDistMap type.248 ///DistMap type. 260 249 /// 261 250 ///\ref named-templ-param "Named parameter" for setting 262 /// \refDistMap type.251 ///DistMap type. 263 252 template <class T> 264 struct DefDistMap : public Dfs< Digraph, DefDistMapTraits<T> > {265 typedef Dfs<Digraph, DefDistMapTraits<T> > Create;253 struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { 254 typedef Dfs<Digraph, SetDistMapTraits<T> > Create; 266 255 }; 267 256 268 257 template <class T> 269 struct DefReachedMapTraits : public Traits {258 struct SetReachedMapTraits : public Traits { 270 259 typedef T ReachedMap; 271 260 static ReachedMap *createReachedMap(const Digraph &) 272 261 { 273 throw UninitializedParameter(); 262 LEMON_ASSERT(false, "ReachedMap is not initialized"); 263 return 0; // ignore warnings 274 264 } 275 265 }; 276 266 ///\brief \ref named-templ-param "Named parameter" for setting 277 /// \refReachedMap type.267 ///ReachedMap type. 278 268 /// 279 269 ///\ref named-templ-param "Named parameter" for setting 280 /// \refReachedMap type.270 ///ReachedMap type. 281 271 template <class T> 282 struct DefReachedMap : public Dfs< Digraph, DefReachedMapTraits<T> > {283 typedef Dfs< Digraph, DefReachedMapTraits<T> > Create;272 struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { 273 typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; 284 274 }; 285 275 286 276 template <class T> 287 struct DefProcessedMapTraits : public Traits {277 struct SetProcessedMapTraits : public Traits { 288 278 typedef T ProcessedMap; 289 279 static ProcessedMap *createProcessedMap(const Digraph &) 290 280 { 291 throw UninitializedParameter(); 281 LEMON_ASSERT(false, "ProcessedMap is not initialized"); 282 return 0; // ignore warnings 292 283 } 293 284 }; 294 285 ///\brief \ref named-templ-param "Named parameter" for setting 295 /// \refProcessedMap type.286 ///ProcessedMap type. 296 287 /// 297 288 ///\ref named-templ-param "Named parameter" for setting 298 /// \refProcessedMap type.289 ///ProcessedMap type. 299 290 template <class T> 300 struct DefProcessedMap : public Dfs< Digraph, DefProcessedMapTraits<T> > {301 typedef Dfs< Digraph, DefProcessedMapTraits<T> > Create;302 }; 303 304 struct DefDigraphProcessedMapTraits : public Traits {291 struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { 292 typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; 293 }; 294 295 struct SetStandardProcessedMapTraits : public Traits { 305 296 typedef typename Digraph::template NodeMap<bool> ProcessedMap; 306 297 static ProcessedMap *createProcessedMap(const Digraph &g) … … 310 301 }; 311 302 ///\brief \ref named-templ-param "Named parameter" for setting 312 /// \refProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.303 ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. 313 304 /// 314 305 ///\ref named-templ-param "Named parameter" for setting 315 /// \refProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.306 ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. 316 307 ///If you don't set it explicitly, it will be automatically allocated. 317 template <class T> 318 struct DefProcessedMapToBeDefaultMap : 319 public Dfs< Digraph, DefDigraphProcessedMapTraits> { 320 typedef Dfs< Digraph, DefDigraphProcessedMapTraits> Create; 308 struct SetStandardProcessedMap : 309 public Dfs< Digraph, SetStandardProcessedMapTraits > { 310 typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; 321 311 }; 322 312 … … 559 549 /// 560 550 ///This method runs the %DFS algorithm from the root node 561 ///in order to compute the DFS path to \c dest.551 ///in order to compute the DFS path to \c t. 562 552 /// 563 553 ///The algorithm computes 564 ///- the %DFS path to \c dest,565 ///- the distance of \c dest from the root in the %DFS tree.554 ///- the %DFS path to \c t, 555 ///- the distance of \c t from the root in the %DFS tree. 566 556 /// 567 557 ///\pre init() must be called and a root node should be 568 558 ///added with addSource() before using this function. 569 void start(Node dest)570 { 571 while ( !emptyQueue() && G->target(_stack[_stack_head])!= dest )559 void start(Node t) 560 { 561 while ( !emptyQueue() && G->target(_stack[_stack_head])!=t ) 572 562 processNextArc(); 573 563 } … … 599 589 } 600 590 601 ///Runs the algorithm from the given node.591 ///Runs the algorithm from the given source node. 602 592 603 593 ///This method runs the %DFS algorithm from node \c s … … 623 613 624 614 ///This method runs the %DFS algorithm from node \c s 625 ///in order to compute the DFS path to \c t.626 /// 627 /// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path,628 /// if \c t is reachable form \c s, \c 0 otherwise.615 ///in order to compute the DFS path to node \c t 616 ///(it stops searching when \c t is processed) 617 /// 618 ///\return \c true if \c t is reachable form \c s. 629 619 /// 630 620 ///\note Apart from the return value, <tt>d.run(s,t)</tt> is … … 635 625 /// d.start(t); 636 626 ///\endcode 637 intrun(Node s,Node t) {627 bool run(Node s,Node t) { 638 628 init(); 639 629 addSource(s); 640 630 start(t); 641 return reached(t) ?_stack_head+1:0;631 return reached(t); 642 632 } 643 633 … … 777 767 ///arcs of the %DFS paths. 778 768 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 779 /// 780 typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; 781 ///Instantiates a \ref PredMap. 782 783 ///This function instantiates a \ref PredMap. 769 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; 770 ///Instantiates a PredMap. 771 772 ///This function instantiates a PredMap. 784 773 ///\param g is the digraph, to which we would like to define the 785 ///\ref PredMap. 786 ///\todo The digraph alone may be insufficient to initialize 787 #ifdef DOXYGEN 774 ///PredMap. 788 775 static PredMap *createPredMap(const Digraph &g) 789 #else 790 static PredMap *createPredMap(const Digraph &) 791 #endif 792 { 793 return new PredMap(); 776 { 777 return new PredMap(g); 794 778 } 795 779 … … 798 782 ///The type of the map that indicates which nodes are processed. 799 783 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 784 ///By default it is a NullMap. 800 785 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; 801 ///Instantiates a \refProcessedMap.802 803 ///This function instantiates a \refProcessedMap.786 ///Instantiates a ProcessedMap. 787 788 ///This function instantiates a ProcessedMap. 804 789 ///\param g is the digraph, to which 805 ///we would like to define the \refProcessedMap.790 ///we would like to define the ProcessedMap. 806 791 #ifdef DOXYGEN 807 792 static ProcessedMap *createProcessedMap(const Digraph &g) … … 818 803 ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. 819 804 typedef typename Digraph::template NodeMap<bool> ReachedMap; 820 ///Instantiates a \refReachedMap.821 822 ///This function instantiates a \refReachedMap.805 ///Instantiates a ReachedMap. 806 807 ///This function instantiates a ReachedMap. 823 808 ///\param g is the digraph, to which 824 ///we would like to define the \refReachedMap.809 ///we would like to define the ReachedMap. 825 810 static ReachedMap *createReachedMap(const Digraph &g) 826 811 { … … 832 817 ///The type of the map that stores the distances of the nodes. 833 818 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. 834 /// 835 typedef NullMap<typename Digraph::Node,int> DistMap; 836 ///Instantiates a \ref DistMap. 837 838 ///This function instantiates a \ref DistMap. 819 typedef typename Digraph::template NodeMap<int> DistMap; 820 ///Instantiates a DistMap. 821 822 ///This function instantiates a DistMap. 839 823 ///\param g is the digraph, to which we would like to define 840 ///the \ref DistMap 841 #ifdef DOXYGEN 824 ///the DistMap 842 825 static DistMap *createDistMap(const Digraph &g) 843 #else 844 static DistMap *createDistMap(const Digraph &) 845 #endif 846 { 847 return new DistMap(); 848 } 826 { 827 return new DistMap(g); 828 } 829 830 ///The type of the DFS paths. 831 832 ///The type of the DFS paths. 833 ///It must meet the \ref concepts::Path "Path" concept. 834 typedef lemon::Path<Digraph> Path; 849 835 }; 850 836 851 /// Default traits class used by \refDfsWizard837 /// Default traits class used by DfsWizard 852 838 853 839 /// To make it easier to use Dfs algorithm … … 876 862 //Pointer to the map of distances. 877 863 void *_dist; 878 //Pointer to the source node. 879 Node _source; 864 //Pointer to the DFS path to the target node. 865 void *_path; 866 //Pointer to the distance of the target node. 867 int *_di; 880 868 881 869 public: … … 883 871 884 872 /// This constructor does not require parameters, therefore it initiates 885 /// all of the attributes to default values (0, INVALID).873 /// all of the attributes to \c 0. 886 874 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), 887 _dist(0), _ source(INVALID) {}875 _dist(0), _path(0), _di(0) {} 888 876 889 877 /// Constructor. 890 878 891 /// This constructor requires some parameters, 892 /// listed in the parameters list. 893 /// Others are initiated to 0. 879 /// This constructor requires one parameter, 880 /// others are initiated to \c 0. 894 881 /// \param g The digraph the algorithm runs on. 895 /// \param s The source node. 896 DfsWizardBase(const GR &g, Node s=INVALID) : 882 DfsWizardBase(const GR &g) : 897 883 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 898 _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}884 _reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} 899 885 900 886 }; 901 887 902 /// Auxiliary class for the function type interface of DFS algorithm. 903 904 /// This auxiliary class is created to implement the function type 905 /// interface of \ref Dfs algorithm. It uses the functions and features 906 /// of the plain \ref Dfs, but it is much simpler to use it. 907 /// It should only be used through the \ref dfs() function, which makes 908 /// it easier to use the algorithm. 888 /// Auxiliary class for the function-type interface of DFS algorithm. 889 890 /// This auxiliary class is created to implement the 891 /// \ref dfs() "function-type interface" of \ref Dfs algorithm. 892 /// It does not have own \ref run() method, it uses the functions 893 /// and features of the plain \ref Dfs. 909 894 /// 910 /// Simplicity means that the way to change the types defined 911 /// in the traits class is based on functions that returns the new class 912 /// and not on templatable built-in classes. 913 /// When using the plain \ref Dfs 914 /// the new class with the modified type comes from 915 /// the original class by using the :: 916 /// operator. In the case of \ref DfsWizard only 917 /// a function have to be called, and it will 918 /// return the needed class. 919 /// 920 /// It does not have own \ref run() method. When its \ref run() method 921 /// is called, it initiates a plain \ref Dfs object, and calls the 922 /// \ref Dfs::run() method of it. 895 /// This class should only be used through the \ref dfs() function, 896 /// which makes it easier to use the algorithm. 923 897 template<class TR> 924 898 class DfsWizard : public TR … … 935 909 936 910 ///\brief The type of the map that stores the predecessor 937 ///arcs of the shortestpaths.911 ///arcs of the DFS paths. 938 912 typedef typename TR::PredMap PredMap; 939 913 ///\brief The type of the map that stores the distances of the nodes. … … 943 917 ///\brief The type of the map that indicates which nodes are processed. 944 918 typedef typename TR::ProcessedMap ProcessedMap; 919 ///The type of the DFS paths 920 typedef typename TR::Path Path; 945 921 946 922 public: … … 953 929 /// Constructor that requires parameters. 954 930 /// These parameters will be the default values for the traits class. 955 DfsWizard(const Digraph &g, Node s=INVALID) : 956 TR(g,s) {} 931 /// \param g The digraph the algorithm runs on. 932 DfsWizard(const Digraph &g) : 933 TR(g) {} 957 934 958 935 ///Copy constructor … … 961 938 ~DfsWizard() {} 962 939 963 ///Runs DFS algorithm from a source node. 964 965 ///Runs DFS algorithm from a source node. 966 ///The node can be given with the \ref source() function. 940 ///Runs DFS algorithm from the given source node. 941 942 ///This method runs DFS algorithm from node \c s 943 ///in order to compute the DFS path to each node. 944 void run(Node s) 945 { 946 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); 947 if (Base::_pred) 948 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); 949 if (Base::_dist) 950 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 951 if (Base::_reached) 952 alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); 953 if (Base::_processed) 954 alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); 955 if (s!=INVALID) 956 alg.run(s); 957 else 958 alg.run(); 959 } 960 961 ///Finds the DFS path between \c s and \c t. 962 963 ///This method runs DFS algorithm from node \c s 964 ///in order to compute the DFS path to node \c t 965 ///(it stops searching when \c t is processed). 966 /// 967 ///\return \c true if \c t is reachable form \c s. 968 bool run(Node s, Node t) 969 { 970 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); 971 if (Base::_pred) 972 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); 973 if (Base::_dist) 974 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 975 if (Base::_reached) 976 alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); 977 if (Base::_processed) 978 alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); 979 alg.run(s,t); 980 if (Base::_path) 981 *reinterpret_cast<Path*>(Base::_path) = alg.path(t); 982 if (Base::_di) 983 *Base::_di = alg.dist(t); 984 return alg.reached(t); 985 } 986 987 ///Runs DFS algorithm to visit all nodes in the digraph. 988 989 ///This method runs DFS algorithm in order to compute 990 ///the DFS path to each node. 967 991 void run() 968 992 { 969 if(Base::_source==INVALID) throw UninitializedParameter(); 970 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); 971 if(Base::_reached) 972 alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); 973 if(Base::_processed) 974 alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); 975 if(Base::_pred) 976 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); 977 if(Base::_dist) 978 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); 979 alg.run(Base::_source); 980 } 981 982 ///Runs DFS algorithm from the given node. 983 984 ///Runs DFS algorithm from the given node. 985 ///\param s is the given source. 986 void run(Node s) 987 { 988 Base::_source=s; 989 run(); 990 } 991 992 /// Sets the source node, from which the Dfs algorithm runs. 993 994 /// Sets the source node, from which the Dfs algorithm runs. 995 /// \param s is the source node. 996 DfsWizard<TR> &source(Node s) 997 { 998 Base::_source=s; 999 return *this; 993 run(INVALID); 1000 994 } 1001 995 1002 996 template<class T> 1003 struct DefPredMapBase : public Base {997 struct SetPredMapBase : public Base { 1004 998 typedef T PredMap; 1005 999 static PredMap *createPredMap(const Digraph &) { return 0; }; 1006 DefPredMapBase(const TR &b) : TR(b) {}1007 }; 1008 ///\brief \ref named- templ-param "Named parameter"1009 ///for setting \refPredMap object.1010 /// 1011 ///\ref named- templ-param "Named parameter"1012 ///for setting \refPredMap object.1000 SetPredMapBase(const TR &b) : TR(b) {} 1001 }; 1002 ///\brief \ref named-func-param "Named parameter" 1003 ///for setting PredMap object. 1004 /// 1005 ///\ref named-func-param "Named parameter" 1006 ///for setting PredMap object. 1013 1007 template<class T> 1014 DfsWizard< DefPredMapBase<T> > predMap(const T &t)1008 DfsWizard<SetPredMapBase<T> > predMap(const T &t) 1015 1009 { 1016 1010 Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); 1017 return DfsWizard< DefPredMapBase<T> >(*this);1011 return DfsWizard<SetPredMapBase<T> >(*this); 1018 1012 } 1019 1013 1020 1014 template<class T> 1021 struct DefReachedMapBase : public Base {1015 struct SetReachedMapBase : public Base { 1022 1016 typedef T ReachedMap; 1023 1017 static ReachedMap *createReachedMap(const Digraph &) { return 0; }; 1024 DefReachedMapBase(const TR &b) : TR(b) {}1025 }; 1026 ///\brief \ref named- templ-param "Named parameter"1027 ///for setting \refReachedMap object.1028 /// 1029 /// \ref named- templ-param "Named parameter"1030 ///for setting \refReachedMap object.1018 SetReachedMapBase(const TR &b) : TR(b) {} 1019 }; 1020 ///\brief \ref named-func-param "Named parameter" 1021 ///for setting ReachedMap object. 1022 /// 1023 /// \ref named-func-param "Named parameter" 1024 ///for setting ReachedMap object. 1031 1025 template<class T> 1032 DfsWizard< DefReachedMapBase<T> > reachedMap(const T &t)1026 DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) 1033 1027 { 1034 1028 Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); 1035 return DfsWizard< DefReachedMapBase<T> >(*this);1029 return DfsWizard<SetReachedMapBase<T> >(*this); 1036 1030 } 1037 1031 1038 1032 template<class T> 1039 struct DefProcessedMapBase : public Base { 1033 struct SetDistMapBase : public Base { 1034 typedef T DistMap; 1035 static DistMap *createDistMap(const Digraph &) { return 0; }; 1036 SetDistMapBase(const TR &b) : TR(b) {} 1037 }; 1038 ///\brief \ref named-func-param "Named parameter" 1039 ///for setting DistMap object. 1040 /// 1041 /// \ref named-func-param "Named parameter" 1042 ///for setting DistMap object. 1043 template<class T> 1044 DfsWizard<SetDistMapBase<T> > distMap(const T &t) 1045 { 1046 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1047 return DfsWizard<SetDistMapBase<T> >(*this); 1048 } 1049 1050 template<class T> 1051 struct SetProcessedMapBase : public Base { 1040 1052 typedef T ProcessedMap; 1041 1053 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; 1042 DefProcessedMapBase(const TR &b) : TR(b) {}1043 }; 1044 ///\brief \ref named- templ-param "Named parameter"1045 ///for setting \refProcessedMap object.1046 /// 1047 /// \ref named- templ-param "Named parameter"1048 ///for setting \refProcessedMap object.1054 SetProcessedMapBase(const TR &b) : TR(b) {} 1055 }; 1056 ///\brief \ref named-func-param "Named parameter" 1057 ///for setting ProcessedMap object. 1058 /// 1059 /// \ref named-func-param "Named parameter" 1060 ///for setting ProcessedMap object. 1049 1061 template<class T> 1050 DfsWizard< DefProcessedMapBase<T> > processedMap(const T &t)1062 DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) 1051 1063 { 1052 1064 Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); 1053 return DfsWizard< DefProcessedMapBase<T> >(*this);1065 return DfsWizard<SetProcessedMapBase<T> >(*this); 1054 1066 } 1055 1067 1056 1068 template<class T> 1057 struct DefDistMapBase : public Base { 1058 typedef T DistMap; 1059 static DistMap *createDistMap(const Digraph &) { return 0; }; 1060 DefDistMapBase(const TR &b) : TR(b) {} 1061 }; 1062 ///\brief \ref named-templ-param "Named parameter" 1063 ///for setting \ref DistMap object. 1064 /// 1065 ///\ref named-templ-param "Named parameter" 1066 ///for setting \ref DistMap object. 1069 struct SetPathBase : public Base { 1070 typedef T Path; 1071 SetPathBase(const TR &b) : TR(b) {} 1072 }; 1073 ///\brief \ref named-func-param "Named parameter" 1074 ///for getting the DFS path to the target node. 1075 /// 1076 ///\ref named-func-param "Named parameter" 1077 ///for getting the DFS path to the target node. 1067 1078 template<class T> 1068 DfsWizard<DefDistMapBase<T> > distMap(const T &t) 1069 { 1070 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); 1071 return DfsWizard<DefDistMapBase<T> >(*this); 1079 DfsWizard<SetPathBase<T> > path(const T &t) 1080 { 1081 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); 1082 return DfsWizard<SetPathBase<T> >(*this); 1083 } 1084 1085 ///\brief \ref named-func-param "Named parameter" 1086 ///for getting the distance of the target node. 1087 /// 1088 ///\ref named-func-param "Named parameter" 1089 ///for getting the distance of the target node. 1090 DfsWizard dist(const int &d) 1091 { 1092 Base::_di=const_cast<int*>(&d); 1093 return *this; 1072 1094 } 1073 1095 1074 1096 }; 1075 1097 1076 ///Function type interface for Dfsalgorithm.1098 ///Function-type interface for DFS algorithm. 1077 1099 1078 1100 ///\ingroup search 1079 ///Function type interface for Dfsalgorithm.1101 ///Function-type interface for DFS algorithm. 1080 1102 /// 1081 ///This function also has several 1082 ///\ref named-templ-func-param "named parameters", 1103 ///This function also has several \ref named-func-param "named parameters", 1083 1104 ///they are declared as the members of class \ref DfsWizard. 1084 ///The following 1085 ///example shows how to use these parameters. 1105 ///The following examples show how to use these parameters. 1086 1106 ///\code 1087 /// dfs(g,source).predMap(preds).run(); 1107 /// // Compute the DFS tree 1108 /// dfs(g).predMap(preds).distMap(dists).run(s); 1109 /// 1110 /// // Compute the DFS path from s to t 1111 /// bool reached = dfs(g).path(p).dist(d).run(s,t); 1088 1112 ///\endcode 1113 1089 1114 ///\warning Don't forget to put the \ref DfsWizard::run() "run()" 1090 1115 ///to the end of the parameter list. … … 1093 1118 template<class GR> 1094 1119 DfsWizard<DfsWizardBase<GR> > 1095 dfs(const GR & g,typename GR::Node s=INVALID)1120 dfs(const GR &digraph) 1096 1121 { 1097 return DfsWizard<DfsWizardBase<GR> >( g,s);1122 return DfsWizard<DfsWizardBase<GR> >(digraph); 1098 1123 } 1099 1124 … … 1188 1213 typedef typename Digraph::template NodeMap<bool> ReachedMap; 1189 1214 1190 /// \brief Instantiates a \refReachedMap.1191 /// 1192 /// This function instantiates a \refReachedMap.1215 /// \brief Instantiates a ReachedMap. 1216 /// 1217 /// This function instantiates a ReachedMap. 1193 1218 /// \param digraph is the digraph, to which 1194 /// we would like to define the \refReachedMap.1219 /// we would like to define the ReachedMap. 1195 1220 static ReachedMap *createReachedMap(const Digraph &digraph) { 1196 1221 return new ReachedMap(digraph); … … 1233 1258 template <typename _Digraph = ListDigraph, 1234 1259 typename _Visitor = DfsVisitor<_Digraph>, 1235 typename _Traits = Dfs DefaultTraits<_Digraph> >1260 typename _Traits = DfsVisitDefaultTraits<_Digraph> > 1236 1261 #endif 1237 1262 class DfsVisit { 1238 1263 public: 1239 1240 /// \brief \ref Exception for uninitialized parameters.1241 ///1242 /// This error represents problems in the initialization1243 /// of the parameters of the algorithm.1244 class UninitializedParameter : public lemon::UninitializedParameter {1245 public:1246 virtual const char* what() const throw()1247 {1248 return "lemon::DfsVisit::UninitializedParameter";1249 }1250 };1251 1264 1252 1265 ///The traits class. … … 1281 1294 int _stack_head; 1282 1295 1283 ///Creates the maps if necessary. 1284 ///\todo Better memory allocation (instead of new). 1296 //Creates the maps if necessary. 1285 1297 void create_maps() { 1286 1298 if(!_reached) { … … 1302 1314 ///@{ 1303 1315 template <class T> 1304 struct DefReachedMapTraits : public Traits {1316 struct SetReachedMapTraits : public Traits { 1305 1317 typedef T ReachedMap; 1306 1318 static ReachedMap *createReachedMap(const Digraph &digraph) { 1307 throw UninitializedParameter(); 1319 LEMON_ASSERT(false, "ReachedMap is not initialized"); 1320 return 0; // ignore warnings 1308 1321 } 1309 1322 }; … … 1313 1326 /// \ref named-templ-param "Named parameter" for setting ReachedMap type. 1314 1327 template <class T> 1315 struct DefReachedMap : public DfsVisit< Digraph, Visitor,1316 DefReachedMapTraits<T> > {1317 typedef DfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;1328 struct SetReachedMap : public DfsVisit< Digraph, Visitor, 1329 SetReachedMapTraits<T> > { 1330 typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; 1318 1331 }; 1319 1332 ///@} … … 1490 1503 /// 1491 1504 /// This method runs the %DFS algorithm from the root node 1492 /// in order to compute the DFS path to \c dest.1505 /// in order to compute the DFS path to \c t. 1493 1506 /// 1494 1507 /// The algorithm computes 1495 /// - the %DFS path to \c dest,1496 /// - the distance of \c dest from the root in the %DFS tree.1508 /// - the %DFS path to \c t, 1509 /// - the distance of \c t from the root in the %DFS tree. 1497 1510 /// 1498 1511 /// \pre init() must be called and a root node should be added 1499 1512 /// with addSource() before using this function. 1500 void start(Node dest) {1501 while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest )1513 void start(Node t) { 1514 while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t ) 1502 1515 processNextArc(); 1503 1516 } … … 1528 1541 } 1529 1542 1530 /// \brief Runs the algorithm from the given node.1543 /// \brief Runs the algorithm from the given source node. 1531 1544 /// 1532 1545 /// This method runs the %DFS algorithm from node \c s. … … 1552 1565 1553 1566 /// This method runs the %DFS algorithm from node \c s 1554 /// in order to compute the DFS path to \c t.1555 /// 1556 /// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path,1557 /// if \c t is reachable form \c s, \c 0 otherwise.1567 /// in order to compute the DFS path to node \c t 1568 /// (it stops searching when \c t is processed). 1569 /// 1570 /// \return \c true if \c t is reachable form \c s. 1558 1571 /// 1559 1572 /// \note Apart from the return value, <tt>d.run(s,t)</tt> is … … 1564 1577 /// d.start(t); 1565 1578 ///\endcode 1566 intrun(Node s,Node t) {1579 bool run(Node s,Node t) { 1567 1580 init(); 1568 1581 addSource(s); 1569 1582 start(t); 1570 return reached(t) ?_stack_head+1:0;1583 return reached(t); 1571 1584 } 1572 1585
Note: See TracChangeset
for help on using the changeset viewer.