branch.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <gecode/kernel.hh>
00041
00042 namespace Gecode {
00043
00044 const VarBranchOptions VarBranchOptions::def;
00045
00046 const ValBranchOptions ValBranchOptions::def;
00047
00048 const TieBreakVarBranchOptions TieBreakVarBranchOptions::def;
00049
00050
00051
00052
00053
00054
00056 class GECODE_KERNEL_EXPORT FunctionBranch : public Brancher {
00057 protected:
00059 class GECODE_KERNEL_EXPORT Description : public Choice {
00060 public:
00062 Description(const Brancher& b, unsigned int a) : Choice(b,a) {}
00064 virtual size_t size(void) const { return sizeof(Description); }
00066 virtual void archive(Archive& e) const {
00067 Choice::archive(e);
00068 }
00069 };
00071 void (*f)(Space&);
00073 bool done;
00075 FunctionBranch(Home home, void (*f0)(Space&))
00076 : Brancher(home), f(f0), done(false) {}
00078 FunctionBranch(Space& home, bool share, FunctionBranch& b)
00079 : Brancher(home,share,b), f(b.f), done(b.done) {}
00080 public:
00082 virtual bool status(const Space&) const {
00083 return !done;
00084 }
00086 virtual const Choice* choice(Space&) {
00087 assert(!done);
00088 return new Description(*this,1);
00089 }
00091 virtual const Choice* choice(const Space&, Archive&) {
00092 return new Description(*this,1);
00093 }
00095 virtual ExecStatus
00096 commit(Space& home, const Choice&, unsigned int) {
00097 done = true;
00098 f(home);
00099 return home.failed() ? ES_FAILED : ES_OK;
00100 }
00102 virtual Actor* copy(Space& home, bool share) {
00103 return new (home) FunctionBranch(home,share,*this);
00104 }
00106 static void post(Home home, void (*f)(Space&)) {
00107 (void) new (home) FunctionBranch(home,f);
00108 }
00109 };
00110
00111
00112 void
00113 branch(Home home, void (*f)(Space& home)) {
00114 if (home.failed())
00115 return;
00116 FunctionBranch::post(home,f);
00117 }
00118
00119 }
00120
00121