Generated on Thu Mar 22 10:39:34 2012 for Gecode by doxygen 1.6.3

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: 2011-05-11 12:44:17 +0200 (Wed, 11 May 2011) $ by $Author: tack $
00013  *     $Revision: 12001 $
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); }
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 // STATISTICS: kernel-branch