Generated on Tue Apr 18 10:22:05 2017 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: 2017-04-01 20:27:10 +0200 (Sat, 01 Apr 2017) $ by $Author: schulte $
00011  *     $Revision: 15623 $
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 void reschedule(Space& home, Propagator& p);
00086     virtual size_t dispose(Space& home);
00087   };
00088 
00096   template<class View, int n, class Val, unsigned int a,
00097            class Filter, class Print>
00098   class ViewValBrancher : public ViewBrancher<View,Filter,n> {
00099   protected:
00100     using ViewBrancher<View,Filter,n>::vs;
00101     using ViewBrancher<View,Filter,n>::x;
00102     using ViewBrancher<View,Filter,n>::f;
00104     typedef typename View::VarType Var;
00106     ValSelCommitBase<View,Val>* vsc;
00108     Print p;
00110     ViewValBrancher(Space& home, bool share, ViewValBrancher& b);
00112     ViewValBrancher(Home home,
00113                     ViewArray<View>& x,
00114                     ViewSel<View>* vs[n],
00115                     ValSelCommitBase<View,Val>* vsc,
00116                     BranchFilter<Var> bf,
00117                     VarValPrint<Var,Val> vvp);
00118   public:
00120     virtual const Choice* choice(Space& home);
00122     virtual const Choice* choice(const Space& home, Archive& e);
00124     virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
00126     virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
00134     virtual void print(const Space& home, const Choice& c, unsigned int b,
00135                        std::ostream& o) const;
00137     virtual Actor* copy(Space& home, bool share);
00139     virtual size_t dispose(Space& home);
00141     static void post(Home home,
00142                      ViewArray<View>& x,
00143                      ViewSel<View>* vs[n],
00144                      ValSelCommitBase<View,Val>* vsc,
00145                      BranchFilter<Var> bf,
00146                      VarValPrint<Var,Val> vvp);
00147   };
00148 
00150   template<class View, int n, class Val, unsigned int a>
00151   void
00152   postviewvalbrancher(Home home,
00153                       ViewArray<View>& x,
00154                       ViewSel<View>* vs[n],
00155                       ValSelCommitBase<View,Val>* vsc,
00156                       BranchFilter<typename View::VarType> bf,
00157                       VarValPrint<typename View::VarType,Val> vvp);
00159 
00160   /*
00161    * %Choice with position and value
00162    *
00163    */
00164   template<class Val>
00165   forceinline
00166   PosValChoice<Val>::PosValChoice(const Brancher& b, unsigned int a,
00167                                   const Pos& p, const Val& n)
00168     : PosChoice(b,a,p), _val(n) {}
00169 
00170   template<class Val>
00171   forceinline const Val&
00172   PosValChoice<Val>::val(void) const {
00173     return _val;
00174   }
00175 
00176   template<class Val>
00177   forceinline size_t
00178   PosValChoice<Val>::size(void) const {
00179     return sizeof(PosValChoice<Val>);
00180   }
00181 
00182   template<class Val>
00183   forceinline void
00184   PosValChoice<Val>::archive(Archive& e) const {
00185     PosChoice::archive(e);
00186     e << _val;
00187   }
00188 
00189 
00190   /*
00191    * View-value no-good literal
00192    *
00193    */
00194   template<class View, class Val, PropCond pc>
00195   forceinline
00196   ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, View x0, Val n0)
00197     : NGL(home), x(x0), n(n0) {}
00198 
00199   template<class View, class Val, PropCond pc>
00200   forceinline
00201   ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, bool share, ViewValNGL& ngl)
00202     : NGL(home,share,ngl), n(ngl.n) {
00203     x.update(home,share,ngl.x);
00204   }
00205 
00206   template<class View, class Val, PropCond pc>
00207   void
00208   ViewValNGL<View,Val,pc>::subscribe(Space& home, Propagator& p) {
00209     x.subscribe(home,p,pc);
00210   }
00211 
00212   template<class View, class Val, PropCond pc>
00213   void
00214   ViewValNGL<View,Val,pc>::cancel(Space& home, Propagator& p) {
00215     x.cancel(home,p,pc);
00216   }
00217 
00218   template<class View, class Val, PropCond pc>
00219   void
00220   ViewValNGL<View,Val,pc>::reschedule(Space& home, Propagator& p) {
00221     x.reschedule(home,p,pc);
00222   }
00223 
00224   template<class View, class Val, PropCond pc>
00225   size_t
00226   ViewValNGL<View,Val,pc>::dispose(Space& home) {
00227     (void) NGL::dispose(home);
00228     return sizeof(*this);
00229   }
00230 
00231 
00232 
00233   /*
00234    * Generic brancher based on variable/value selection
00235    *
00236    */
00237   template<class View, int n, class Val, unsigned int a,
00238            class Filter, class Print>
00239   forceinline
00240   ViewValBrancher<View,n,Val,a,Filter,Print>::
00241   ViewValBrancher(Home home,
00242                   ViewArray<View>& x,
00243                   ViewSel<View>* vs[n],
00244                   ValSelCommitBase<View,Val>* vsc0,
00245                   BranchFilter<Var> bf,
00246                   VarValPrint<Var,Val> vvp)
00247     : ViewBrancher<View,Filter,n>(home,x,vs,bf), vsc(vsc0), p(vvp) {
00248     if (vsc->notice() || f.notice() || p.notice())
00249       home.notice(*this,AP_DISPOSE,true);
00250   }
00251 
00252   template<class View, int n, class Val, unsigned int a,
00253            class Filter, class Print>
00254   forceinline void
00255   ViewValBrancher<View,n,Val,a,Filter,Print>::
00256   post(Home home, ViewArray<View>& x,
00257        ViewSel<View>* vs[n], ValSelCommitBase<View,Val>* vsc,
00258        BranchFilter<Var> bf,
00259        VarValPrint<Var,Val> vvp) {
00260     (void) new (home) ViewValBrancher<View,n,Val,a,Filter,Print>
00261       (home,x,vs,vsc,bf,vvp);
00262   }
00263 
00264   template<class View, int n, class Val, unsigned int a,
00265            class Filter, class Print>
00266   forceinline
00267   ViewValBrancher<View,n,Val,a,Filter,Print>::
00268   ViewValBrancher(Space& home, bool shared,
00269                   ViewValBrancher<View,n,Val,a,Filter,Print>& b)
00270     : ViewBrancher<View,Filter,n>(home,shared,b),
00271       vsc(b.vsc->copy(home,shared)), p(home,shared,b.p) {}
00272 
00273   template<class View, int n, class Val, unsigned int a,
00274            class Filter, class Print>
00275   Actor*
00276   ViewValBrancher<View,n,Val,a,Filter,Print>::copy(Space& home, bool shared) {
00277     return new (home) ViewValBrancher<View,n,Val,a,Filter,Print>
00278       (home,shared,*this);
00279   }
00280 
00281   template<class View, int n, class Val, unsigned int a,
00282            class Filter, class Print>
00283   const Choice*
00284   ViewValBrancher<View,n,Val,a,Filter,Print>::choice(Space& home) {
00285     Pos p = ViewBrancher<View,Filter,n>::pos(home);
00286     View v = ViewBrancher<View,Filter,n>::view(p);
00287     return new PosValChoice<Val>(*this,a,p,vsc->val(home,v,p.pos));
00288   }
00289 
00290   template<class View, int n, class Val, unsigned int a,
00291            class Filter, class Print>
00292   const Choice*
00293   ViewValBrancher<View,n,Val,a,Filter,Print>::choice(const Space& home,
00294                                                      Archive& e) {
00295     (void) home;
00296     int p; e >> p;
00297     Val v; e >> v;
00298     return new PosValChoice<Val>(*this,a,p,v);
00299   }
00300 
00301   template<class View, int n, class Val, unsigned int a,
00302            class Filter, class Print>
00303   ExecStatus
00304   ViewValBrancher<View,n,Val,a,Filter,Print>
00305   ::commit(Space& home, const Choice& c, unsigned int b) {
00306     const PosValChoice<Val>& pvc
00307       = static_cast<const PosValChoice<Val>&>(c);
00308     return me_failed(vsc->commit(home,b,
00309                                  ViewBrancher<View,Filter,n>::view(pvc.pos()),
00310                                  pvc.pos().pos,
00311                                  pvc.val()))
00312       ? ES_FAILED : ES_OK;
00313   }
00314 
00315   template<class View, int n, class Val, unsigned int a,
00316            class Filter, class Print>
00317   NGL*
00318   ViewValBrancher<View,n,Val,a,Filter,Print>
00319   ::ngl(Space& home, const Choice& c, unsigned int b) const {
00320     const PosValChoice<Val>& pvc
00321       = static_cast<const PosValChoice<Val>&>(c);
00322     return vsc->ngl(home,b,
00323                     ViewBrancher<View,Filter,n>::view(pvc.pos()),pvc.val());
00324   }
00325 
00326   template<class View, int n, class Val, unsigned int a,
00327            class Filter, class Print>
00328   void
00329   ViewValBrancher<View,n,Val,a,Filter,Print>
00330   ::print(const Space& home, const Choice& c, unsigned int b,
00331           std::ostream& o) const {
00332     const PosValChoice<Val>& pvc
00333       = static_cast<const PosValChoice<Val>&>(c);
00334     View xi = ViewBrancher<View,Filter,n>::view(pvc.pos());
00335     if (p)
00336       p(home,*this,b,xi,pvc.pos().pos,pvc.val(),o);
00337     else
00338       vsc->print(home,b,xi,pvc.pos().pos,pvc.val(),o);
00339   }
00340 
00341   template<class View, int n, class Val, unsigned int a,
00342            class Filter, class Print>
00343   forceinline size_t
00344   ViewValBrancher<View,n,Val,a,Filter,Print>::dispose(Space& home) {
00345     if (vsc->notice() || f.notice() || p.notice())
00346       home.ignore(*this,AP_DISPOSE,true);
00347     vsc->dispose(home);
00348     (void) ViewBrancher<View,Filter,n>::dispose(home);
00349     return sizeof(ViewValBrancher<View,n,Val,a,Filter,Print>);
00350   }
00351 
00352   template<class View, int n, class Val, unsigned int a>
00353   forceinline void
00354   postviewvalbrancher(Home home,
00355                       ViewArray<View>& x,
00356                       ViewSel<View>* vs[n],
00357                       ValSelCommitBase<View,Val>* vsc,
00358                       BranchFilter<typename View::VarType> bf,
00359                       VarValPrint<typename View::VarType,Val> vvp) {
00360     if (bf) {
00361       if (vvp) {
00362         ViewValBrancher<View,n,Val,a,
00363           BrancherFilter<View>,BrancherPrint<View,Val> >
00364           ::post(home,x,vs,vsc,bf,vvp);
00365       } else {
00366         ViewValBrancher<View,n,Val,a,
00367           BrancherFilter<View>,BrancherNoPrint<View,Val> >
00368           ::post(home,x,vs,vsc,bf,vvp);
00369         }
00370     } else {
00371       if (vvp)
00372         ViewValBrancher<View,n,Val,a,
00373           BrancherNoFilter<View>,BrancherPrint<View,Val> >
00374           ::post(home,x,vs,vsc,bf,vvp);
00375       else
00376         ViewValBrancher<View,n,Val,a,
00377           BrancherNoFilter<View>,BrancherNoPrint<View,Val> >
00378           ::post(home,x,vs,vsc,bf,vvp);
00379     }
00380   }      
00381 
00382 }
00383 
00384 // STATISTICS: kernel-branch