Generated on Tue Apr 18 10:21:35 2017 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: 2017-03-01 04:28:36 +0100 (Wed, 01 Mar 2017) $ by $Author: schulte $
00013  *     $Revision: 15541 $
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 
00045   class FunctionBranch : public Brancher {
00046   protected:
00048     class Description : public Choice {
00049     public:
00051       Description(const Brancher& b, unsigned int a);
00053       virtual size_t size(void) const;
00055       virtual void archive(Archive& e) const;
00056     };
00058     SharedData<std::function<void(Space& home)>> f;
00060     bool done;
00062     FunctionBranch(Home home, std::function<void(Space& home)> f0);
00064     FunctionBranch(Space& home, bool share, FunctionBranch& b);
00065   public:
00067     virtual bool status(const Space& home) const;
00069     virtual const Choice* choice(Space& home);
00071     virtual const Choice* choice(const Space& home, Archive& a);
00073     virtual ExecStatus commit(Space& home, const Choice& ch, unsigned int a);
00075     virtual void print(const Space&, const Choice&, unsigned int,
00076                        std::ostream& o) const;
00078     virtual Actor* copy(Space& home, bool share);
00080     static void post(Home home, std::function<void(Space& home)> f);
00082     virtual size_t dispose(Space& home);
00083   };
00084 
00085   forceinline
00086   FunctionBranch::Description::Description(const Brancher& b, unsigned int a)
00087     : Choice(b,a) {}
00088   size_t
00089   FunctionBranch::Description::size(void) const { 
00090     return sizeof(Description); 
00091   }
00092   void
00093   FunctionBranch::Description::archive(Archive& e) const {
00094     Choice::archive(e);
00095   }
00096 
00097   forceinline
00098   FunctionBranch::FunctionBranch(Home home,
00099                                  std::function<void(Space& home)> f0)
00100     : Brancher(home), f(f0), done(false) {
00101     if (!f())
00102       throw InvalidFunction("FunctionBranch::FunctionBranch");
00103    home.notice(*this,AP_DISPOSE);
00104   }
00105   forceinline
00106   FunctionBranch::FunctionBranch(Space& home, bool share, FunctionBranch& b)
00107     : Brancher(home,share,b), done(b.done) {
00108     f.update(home,share,b.f);
00109   }
00110   bool
00111   FunctionBranch::status(const Space&) const {
00112     return !done;
00113   }
00114   const Choice*
00115   FunctionBranch::choice(Space&) {
00116     assert(!done);
00117     return new Description(*this,1);
00118   }
00119   const Choice*
00120   FunctionBranch::choice(const Space&, Archive&) {
00121     return new Description(*this,1);
00122   }
00123   ExecStatus
00124   FunctionBranch::commit(Space& home, const Choice&, unsigned int) {
00125     done = true;
00126     GECODE_VALID_FUNCTION(f());
00127     f()(home);
00128     return home.failed() ? ES_FAILED : ES_OK;
00129   }
00130   void
00131   FunctionBranch::print(const Space&, const Choice&, unsigned int,
00132                         std::ostream& o) const {
00133     o << "FunctionBranch()";
00134   }
00135   Actor*
00136   FunctionBranch::copy(Space& home, bool share) {
00137     return new (home) FunctionBranch(home,share,*this);
00138   }
00139   forceinline void
00140   FunctionBranch::post(Home home, std::function<void(Space& home)> f) {
00141     if (!f)
00142       throw InvalidFunction("FunctionBranch::post");
00143     (void) new (home) FunctionBranch(home,f);
00144   }
00145   size_t
00146   FunctionBranch::dispose(Space& home) {
00147     home.ignore(*this,AP_DISPOSE);
00148     f.~SharedData<std::function<void(Space& home)>>();
00149     (void) Brancher::dispose(home);
00150     return sizeof(*this);
00151   }
00152 
00153   void
00154   branch(Home home, std::function<void(Space& home)> f) {
00155     FunctionBranch::post(home,f);
00156   }
00157 
00158 }
00159 
00160 // STATISTICS: kernel-branch