branch.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * Mikael Lagerkvist <lagerkvist@gecode.org> 00006 * 00007 * Copyright: 00008 * Christian Schulte, 2008 00009 * Mikael Lagerkvist, 2008 00010 * 00011 * Last modified: 00012 * $Date: 2010-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ by $Author: schulte $ 00013 * $Revision: 10684 $ 00014 * 00015 * This file is part of Gecode, the generic constraint 00016 * development environment: 00017 * http://www.gecode.org 00018 * 00019 * Permission is hereby granted, free of charge, to any person obtaining 00020 * a copy of this software and associated documentation files (the 00021 * "Software"), to deal in the Software without restriction, including 00022 * without limitation the rights to use, copy, modify, merge, publish, 00023 * distribute, sublicense, and/or sell copies of the Software, and to 00024 * permit persons to whom the Software is furnished to do so, subject to 00025 * the following conditions: 00026 * 00027 * The above copyright notice and this permission notice shall be 00028 * included in all copies or substantial portions of the Software. 00029 * 00030 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00031 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00032 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00033 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00034 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00035 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00036 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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 * Function brancher 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 // STATISTICS: kernel-branch