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); }
00065 };
00067 void (*f)(Space&);
00069 bool done;
00071 FunctionBranch(Home home, void (*f0)(Space&))
00072 : Brancher(home), f(f0), done(false) {}
00074 FunctionBranch(Space& home, bool share, FunctionBranch& b)
00075 : Brancher(home,share,b), f(b.f), done(b.done) {}
00076 public:
00078 virtual bool status(const Space&) const {
00079 return !done;
00080 }
00082 virtual const Choice* choice(Space&) {
00083 assert(!done);
00084 return new Description(*this,1);
00085 }
00087 virtual ExecStatus
00088 commit(Space& home, const Choice&, unsigned int) {
00089 done = true;
00090 f(home);
00091 return home.failed() ? ES_FAILED : ES_OK;
00092 }
00094 virtual Actor* copy(Space& home, bool share) {
00095 return new (home) FunctionBranch(home,share,*this);
00096 }
00098 static void post(Home home, void (*f)(Space&)) {
00099 (void) new (home) FunctionBranch(home,f);
00100 }
00101 };
00102
00103
00104 void
00105 branch(Home home, void (*f)(Space& home)) {
00106 if (home.failed())
00107 return;
00108 FunctionBranch::post(home,f);
00109 }
00110
00111 }
00112
00113