Generated on Fri Mar 20 15:56:16 2015 for Gecode by doxygen 1.6.3

brancher-view-val.hpp

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  *
00006  *  Copyright:
00007  *     Christian Schulte, 2012
00008  *
00009  *  Last modified:
00010  *     $Date: 2015-03-20 15:37:34 +0100 (Fri, 20 Mar 2015) $ by $Author: schulte $
00011  *     $Revision: 14471 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode {
00039 
00048 
00049   template<class Val>
00050   class GECODE_VTABLE_EXPORT PosValChoice : public PosChoice {
00051   private:
00053     const Val _val;
00054   public:
00056     PosValChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n);
00058     const Val& val(void) const;
00060     virtual size_t size(void) const;
00062     virtual void archive(Archive& e) const;
00063   };
00064 
00065 
00067   template<class View, class Val, PropCond pc>
00068   class ViewValNGL : public NGL {
00069   protected:
00071     View x;
00073     Val n;
00074   public:
00076     ViewValNGL(Space& home, View x, Val n);
00078     ViewValNGL(Space& home, bool share, ViewValNGL& ngl);
00080     virtual void subscribe(Space& home, Propagator& p);
00082     virtual void cancel(Space& home, Propagator& p);
00084     virtual size_t dispose(Space& home);
00085   };
00086 
00094   template<class View, int n, class Val, unsigned int a>
00095   class ViewValBrancher : public ViewBrancher<View,n> {
00096     typedef typename ViewBrancher<View,n>::BranchFilter BranchFilter;
00097   protected:
00098     using ViewBrancher<View,n>::vs;
00099     using ViewBrancher<View,n>::x;
00101     ValSelCommitBase<View,Val>* vsc;
00103     typedef void (*VarValPrint)(const Space& home, const BrancherHandle& bh,
00104                                 unsigned int b,
00105                                 typename View::VarType x, int i,
00106                                 const Val& m,
00107                                 std::ostream& o);
00109     VarValPrint vvp;
00111     ViewValBrancher(Space& home, bool share, ViewValBrancher& b);
00113     ViewValBrancher(Home home, 
00114                     ViewArray<View>& x,
00115                     ViewSel<View>* vs[n], 
00116                     ValSelCommitBase<View,Val>* vsc,
00117                     BranchFilter bf,
00118                     VarValPrint vvp);
00119   public:
00121     virtual const Choice* choice(Space& home);
00123     virtual const Choice* choice(const Space& home, Archive& e);
00125     virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
00127     virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
00135     virtual void print(const Space& home, const Choice& c, unsigned int b,
00136                        std::ostream& o) const;
00138     virtual Actor* copy(Space& home, bool share);
00140     virtual size_t dispose(Space& home);
00142     static BrancherHandle post(Home home, 
00143                                ViewArray<View>& x,
00144                                ViewSel<View>* vs[n], 
00145                                ValSelCommitBase<View,Val>* vsc, 
00146                                BranchFilter bf,
00147                                VarValPrint vvp);
00148   };
00150 
00151   /*
00152    * %Choice with position and value
00153    *
00154    */
00155   template<class Val>
00156   forceinline
00157   PosValChoice<Val>::PosValChoice(const Brancher& b, unsigned int a,
00158                                   const Pos& p, const Val& n)
00159     : PosChoice(b,a,p), _val(n) {}
00160 
00161   template<class Val>
00162   forceinline const Val&
00163   PosValChoice<Val>::val(void) const {
00164     return _val;
00165   }
00166 
00167   template<class Val>
00168   forceinline size_t
00169   PosValChoice<Val>::size(void) const {
00170     return sizeof(PosValChoice<Val>);
00171   }
00172 
00173   template<class Val>
00174   forceinline void
00175   PosValChoice<Val>::archive(Archive& e) const {
00176     PosChoice::archive(e);
00177     e << _val;
00178   }
00179 
00180 
00181   /*
00182    * View-value no-good literal
00183    *
00184    */
00185   template<class View, class Val, PropCond pc>
00186   forceinline
00187   ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, View x0, Val n0)
00188     : NGL(home), x(x0), n(n0) {}
00189 
00190   template<class View, class Val, PropCond pc>
00191   forceinline
00192   ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, bool share, ViewValNGL& ngl)
00193     : NGL(home,share,ngl), n(ngl.n) {
00194     x.update(home,share,ngl.x);
00195   }
00196   
00197   template<class View, class Val, PropCond pc>
00198   void
00199   ViewValNGL<View,Val,pc>::subscribe(Space& home, Propagator& p) {
00200     x.subscribe(home,p,pc);
00201   }
00202   
00203   template<class View, class Val, PropCond pc>
00204   void
00205   ViewValNGL<View,Val,pc>::cancel(Space& home, Propagator& p) {
00206     x.cancel(home,p,pc);
00207   }
00208   
00209   template<class View, class Val, PropCond pc>
00210   size_t
00211   ViewValNGL<View,Val,pc>::dispose(Space& home) {
00212     (void) NGL::dispose(home);
00213     return sizeof(*this);
00214   }
00215   
00216 
00217 
00218   /*
00219    * Generic brancher based on variable/value selection
00220    *
00221    */
00222   template<class View, int n, class Val, unsigned int a>
00223   forceinline
00224   ViewValBrancher<View,n,Val,a>::
00225   ViewValBrancher(Home home, 
00226                   ViewArray<View>& x,
00227                   ViewSel<View>* vs[n], 
00228                   ValSelCommitBase<View,Val>* vsc0,
00229                   BranchFilter bf,
00230                   VarValPrint vvp0)
00231     : ViewBrancher<View,n>(home,x,vs,bf), vsc(vsc0), vvp(vvp0) {
00232     if (vsc->notice())
00233       home.notice(*this,AP_DISPOSE,true);
00234   }
00235 
00236   template<class View, int n, class Val, unsigned int a>
00237   inline BrancherHandle
00238   ViewValBrancher<View,n,Val,a>::
00239   post(Home home, ViewArray<View>& x,
00240        ViewSel<View>* vs[n], ValSelCommitBase<View,Val>* vsc,
00241        BranchFilter bf,
00242        VarValPrint vvp) {
00243     return *new (home) ViewValBrancher<View,n,Val,a>(home,x,vs,vsc,bf,vvp);
00244   }
00245 
00246   template<class View, int n, class Val, unsigned int a>
00247   forceinline
00248   ViewValBrancher<View,n,Val,a>::
00249   ViewValBrancher(Space& home, bool shared, ViewValBrancher<View,n,Val,a>& b)
00250     : ViewBrancher<View,n>(home,shared,b), 
00251       vsc(b.vsc->copy(home,shared)), vvp(b.vvp) {}
00252   
00253   template<class View, int n, class Val, unsigned int a>
00254   Actor*
00255   ViewValBrancher<View,n,Val,a>::copy(Space& home, bool shared) {
00256     return new (home) ViewValBrancher<View,n,Val,a>(home,shared,*this);
00257   }
00258 
00259   template<class View, int n, class Val, unsigned int a>
00260   const Choice*
00261   ViewValBrancher<View,n,Val,a>::choice(Space& home) {
00262     Pos p = ViewBrancher<View,n>::pos(home);
00263     View v = ViewBrancher<View,n>::view(p);
00264     return new PosValChoice<Val>(*this,a,p,vsc->val(home,v,p.pos));
00265   }
00266 
00267   template<class View, int n, class Val, unsigned int a>
00268   const Choice*
00269   ViewValBrancher<View,n,Val,a>::choice(const Space& home, Archive& e) {
00270     (void) home;
00271     int p; e >> p;
00272     Val v; e >> v;
00273     return new PosValChoice<Val>(*this,a,p,v);
00274   }
00275 
00276   template<class View, int n, class Val, unsigned int a>
00277   ExecStatus
00278   ViewValBrancher<View,n,Val,a>
00279   ::commit(Space& home, const Choice& c, unsigned int b) {
00280     const PosValChoice<Val>& pvc
00281       = static_cast<const PosValChoice<Val>&>(c);
00282     return me_failed(vsc->commit(home,b,
00283                                  ViewBrancher<View,n>::view(pvc.pos()),
00284                                  pvc.pos().pos,
00285                                  pvc.val())) 
00286       ? ES_FAILED : ES_OK;
00287   }
00288 
00289   template<class View, int n, class Val, unsigned int a>
00290   NGL*
00291   ViewValBrancher<View,n,Val,a>
00292   ::ngl(Space& home, const Choice& c, unsigned int b) const {
00293     const PosValChoice<Val>& pvc
00294       = static_cast<const PosValChoice<Val>&>(c);
00295     return vsc->ngl(home,b,
00296                     ViewBrancher<View,n>::view(pvc.pos()),pvc.val());
00297   }
00298 
00299   template<class View, int n, class Val, unsigned int a>
00300   void
00301   ViewValBrancher<View,n,Val,a>
00302   ::print(const Space& home, const Choice& c, unsigned int b,
00303           std::ostream& o) const {
00304     const PosValChoice<Val>& pvc
00305       = static_cast<const PosValChoice<Val>&>(c);
00306     View xi = ViewBrancher<View,n>::view(pvc.pos());
00307     typename View::VarType y(ViewBrancher<View,n>::view(pvc.pos()).varimp());
00308     if (vvp != NULL)
00309       vvp(home,*this,b,y,pvc.pos().pos,pvc.val(),o);
00310     else
00311       vsc->print(home,b,xi,pvc.pos().pos,pvc.val(),o);
00312   }
00313 
00314   template<class View, int n, class Val, unsigned int a>
00315   forceinline size_t
00316   ViewValBrancher<View,n,Val,a>::dispose(Space& home) {
00317     if (vsc->notice())
00318       home.ignore(*this,AP_DISPOSE,true);
00319     vsc->dispose(home);
00320     (void) ViewBrancher<View,n>::dispose(home);
00321     return sizeof(ViewValBrancher<View,n,Val,a>);
00322   }
00323 
00324 }
00325 
00326 // STATISTICS: kernel-branch