Generated on Fri Mar 20 15:55:57 2015 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: 2013-11-14 14:35:45 +0100 (Thu, 14 Nov 2013) $ by $Author: schulte $
00013  *     $Revision: 14073 $
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 GECODE_KERNEL_EXPORT FunctionBranch : public Brancher {
00046   protected:
00048     class GECODE_KERNEL_EXPORT Description : public Choice {
00049     public:
00051       Description(const Brancher& b, unsigned int a) : Choice(b,a) {}
00053       virtual size_t size(void) const { return sizeof(Description); }
00055       virtual void archive(Archive& e) const {
00056         Choice::archive(e);
00057       }
00058     };
00060     void (*f)(Space&);
00062     bool done;
00064     FunctionBranch(Home home, void (*f0)(Space&))
00065       : Brancher(home), f(f0), done(false) {}
00067     FunctionBranch(Space& home, bool share, FunctionBranch& b)
00068       : Brancher(home,share,b), f(b.f), done(b.done) {}
00069   public:
00071     virtual bool status(const Space&) const {
00072       return !done;
00073     }
00075     virtual const Choice* choice(Space&) {
00076       assert(!done);
00077       return new Description(*this,1);
00078     }
00080     virtual const Choice* choice(const Space&, Archive&) {
00081       return new Description(*this,1);
00082     }
00084     virtual ExecStatus 
00085     commit(Space& home, const Choice&, unsigned int) {
00086       done = true;
00087       f(home);
00088       return home.failed() ? ES_FAILED : ES_OK;
00089     }
00091     virtual void
00092     print(const Space&, const Choice&, unsigned int, 
00093           std::ostream& o) const {
00094       o << "FunctionBranch(" << f << ")";
00095     }
00097     virtual Actor* copy(Space& home, bool share) {
00098       return new (home) FunctionBranch(home,share,*this);
00099     }
00101     static BrancherHandle post(Home home, void (*f)(Space&)) {
00102       return *new (home) FunctionBranch(home,f);
00103     }
00104   };
00105 
00106 
00107   BrancherHandle
00108   branch(Home home, void (*f)(Space& home)) {
00109     if (home.failed())
00110       return BrancherHandle();
00111     return FunctionBranch::post(home,f);
00112   }
00113 
00114 }
00115 
00116 // STATISTICS: kernel-branch