Generated on Thu Apr 11 13:59:15 2019 for Gecode by doxygen 1.6.3

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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 namespace Gecode {
00035 
00044 
00045   template<class Val>
00046   class GECODE_VTABLE_EXPORT PosValChoice : public PosChoice {
00047   private:
00049     const Val _val;
00050   public:
00052     PosValChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n);
00053     const Val& val(void) const;
00055     virtual void archive(Archive& e) const;
00056   };
00057 
00058 
00060   template<class View, class Val, PropCond pc>
00061   class ViewValNGL : public NGL {
00062   protected:
00064     View x;
00066     Val n;
00067   public:
00069     ViewValNGL(Space& home, View x, Val n);
00071     ViewValNGL(Space& home, ViewValNGL& ngl);
00073     virtual void subscribe(Space& home, Propagator& p);
00075     virtual void cancel(Space& home, Propagator& p);
00077     virtual void reschedule(Space& home, Propagator& p);
00079     virtual size_t dispose(Space& home);
00080   };
00081 
00089   template<class View, int n, class Val, unsigned int a,
00090            class Filter, class Print>
00091   class ViewValBrancher : public ViewBrancher<View,Filter,n> {
00092   protected:
00093     using ViewBrancher<View,Filter,n>::vs;
00094     using ViewBrancher<View,Filter,n>::x;
00095     using ViewBrancher<View,Filter,n>::f;
00097     typedef typename View::VarType Var;
00099     ValSelCommitBase<View,Val>* vsc;
00101     Print p;
00103     ViewValBrancher(Space& home, ViewValBrancher& b);
00105     ViewValBrancher(Home home,
00106                     ViewArray<View>& x,
00107                     ViewSel<View>* vs[n],
00108                     ValSelCommitBase<View,Val>* vsc,
00109                     BranchFilter<Var> bf,
00110                     VarValPrint<Var,Val> vvp);
00111   public:
00113     virtual const Choice* choice(Space& home);
00115     virtual const Choice* choice(const Space& home, Archive& e);
00117     virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
00119     virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
00127     virtual void print(const Space& home, const Choice& c, unsigned int b,
00128                        std::ostream& o) const;
00130     virtual Actor* copy(Space& home);
00132     virtual size_t dispose(Space& home);
00134     static void post(Home home,
00135                      ViewArray<View>& x,
00136                      ViewSel<View>* vs[n],
00137                      ValSelCommitBase<View,Val>* vsc,
00138                      BranchFilter<Var> bf,
00139                      VarValPrint<Var,Val> vvp);
00140   };
00141 
00143   template<class View, int n, class Val, unsigned int a>
00144   void
00145   postviewvalbrancher(Home home,
00146                       ViewArray<View>& x,
00147                       ViewSel<View>* vs[n],
00148                       ValSelCommitBase<View,Val>* vsc,
00149                       BranchFilter<typename View::VarType> bf,
00150                       VarValPrint<typename View::VarType,Val> vvp);
00152 
00153   /*
00154    * %Choice with position and value
00155    *
00156    */
00157   template<class Val>
00158   forceinline
00159   PosValChoice<Val>::PosValChoice(const Brancher& b, unsigned int a,
00160                                   const Pos& p, const Val& n)
00161     : PosChoice(b,a,p), _val(n) {}
00162 
00163   template<class Val>
00164   forceinline const Val&
00165   PosValChoice<Val>::val(void) const {
00166     return _val;
00167   }
00168 
00169   template<class Val>
00170   forceinline void
00171   PosValChoice<Val>::archive(Archive& e) const {
00172     PosChoice::archive(e);
00173     e << _val;
00174   }
00175 
00176 
00177   /*
00178    * View-value no-good literal
00179    *
00180    */
00181   template<class View, class Val, PropCond pc>
00182   forceinline
00183   ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, View x0, Val n0)
00184     : NGL(home), x(x0), n(n0) {}
00185 
00186   template<class View, class Val, PropCond pc>
00187   forceinline
00188   ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, ViewValNGL& ngl)
00189     : NGL(home,ngl), n(ngl.n) {
00190     x.update(home,ngl.x);
00191   }
00192 
00193   template<class View, class Val, PropCond pc>
00194   void
00195   ViewValNGL<View,Val,pc>::subscribe(Space& home, Propagator& p) {
00196     x.subscribe(home,p,pc);
00197   }
00198 
00199   template<class View, class Val, PropCond pc>
00200   void
00201   ViewValNGL<View,Val,pc>::cancel(Space& home, Propagator& p) {
00202     x.cancel(home,p,pc);
00203   }
00204 
00205   template<class View, class Val, PropCond pc>
00206   void
00207   ViewValNGL<View,Val,pc>::reschedule(Space& home, Propagator& p) {
00208     x.reschedule(home,p,pc);
00209   }
00210 
00211   template<class View, class Val, PropCond pc>
00212   size_t
00213   ViewValNGL<View,Val,pc>::dispose(Space& home) {
00214     (void) NGL::dispose(home);
00215     return sizeof(*this);
00216   }
00217 
00218 
00219 
00220   /*
00221    * Generic brancher based on variable/value selection
00222    *
00223    */
00224   template<class View, int n, class Val, unsigned int a,
00225            class Filter, class Print>
00226   forceinline
00227   ViewValBrancher<View,n,Val,a,Filter,Print>::
00228   ViewValBrancher(Home home,
00229                   ViewArray<View>& x,
00230                   ViewSel<View>* vs[n],
00231                   ValSelCommitBase<View,Val>* vsc0,
00232                   BranchFilter<Var> bf,
00233                   VarValPrint<Var,Val> vvp)
00234     : ViewBrancher<View,Filter,n>(home,x,vs,bf), vsc(vsc0), p(vvp) {
00235     if (vsc->notice() || f.notice() || p.notice())
00236       home.notice(*this,AP_DISPOSE,true);
00237   }
00238 
00239   template<class View, int n, class Val, unsigned int a,
00240            class Filter, class Print>
00241   forceinline void
00242   ViewValBrancher<View,n,Val,a,Filter,Print>::
00243   post(Home home, ViewArray<View>& x,
00244        ViewSel<View>* vs[n], ValSelCommitBase<View,Val>* vsc,
00245        BranchFilter<Var> bf,
00246        VarValPrint<Var,Val> vvp) {
00247     (void) new (home) ViewValBrancher<View,n,Val,a,Filter,Print>
00248       (home,x,vs,vsc,bf,vvp);
00249   }
00250 
00251   template<class View, int n, class Val, unsigned int a,
00252            class Filter, class Print>
00253   forceinline
00254   ViewValBrancher<View,n,Val,a,Filter,Print>::
00255   ViewValBrancher(Space& home,
00256                   ViewValBrancher<View,n,Val,a,Filter,Print>& b)
00257     : ViewBrancher<View,Filter,n>(home,b),
00258       vsc(b.vsc->copy(home)), p(b.p) {}
00259 
00260   template<class View, int n, class Val, unsigned int a,
00261            class Filter, class Print>
00262   Actor*
00263   ViewValBrancher<View,n,Val,a,Filter,Print>::copy(Space& home) {
00264     return new (home) ViewValBrancher<View,n,Val,a,Filter,Print>
00265       (home,*this);
00266   }
00267 
00268   template<class View, int n, class Val, unsigned int a,
00269            class Filter, class Print>
00270   const Choice*
00271   ViewValBrancher<View,n,Val,a,Filter,Print>::choice(Space& home) {
00272     Pos p = ViewBrancher<View,Filter,n>::pos(home);
00273     View v = ViewBrancher<View,Filter,n>::view(p);
00274     return new PosValChoice<Val>(*this,a,p,vsc->val(home,v,p.pos));
00275   }
00276 
00277   template<class View, int n, class Val, unsigned int a,
00278            class Filter, class Print>
00279   const Choice*
00280   ViewValBrancher<View,n,Val,a,Filter,Print>::choice(const Space& home,
00281                                                      Archive& e) {
00282     (void) home;
00283     int p; e >> p;
00284     Val v; e >> v;
00285     return new PosValChoice<Val>(*this,a,p,v);
00286   }
00287 
00288   template<class View, int n, class Val, unsigned int a,
00289            class Filter, class Print>
00290   ExecStatus
00291   ViewValBrancher<View,n,Val,a,Filter,Print>
00292   ::commit(Space& home, const Choice& c, unsigned int b) {
00293     const PosValChoice<Val>& pvc
00294       = static_cast<const PosValChoice<Val>&>(c);
00295     return me_failed(vsc->commit(home,b,
00296                                  ViewBrancher<View,Filter,n>::view(pvc.pos()),
00297                                  pvc.pos().pos,
00298                                  pvc.val()))
00299       ? ES_FAILED : ES_OK;
00300   }
00301 
00302   template<class View, int n, class Val, unsigned int a,
00303            class Filter, class Print>
00304   NGL*
00305   ViewValBrancher<View,n,Val,a,Filter,Print>
00306   ::ngl(Space& home, const Choice& c, unsigned int b) const {
00307     const PosValChoice<Val>& pvc
00308       = static_cast<const PosValChoice<Val>&>(c);
00309     return vsc->ngl(home,b,
00310                     ViewBrancher<View,Filter,n>::view(pvc.pos()),pvc.val());
00311   }
00312 
00313   template<class View, int n, class Val, unsigned int a,
00314            class Filter, class Print>
00315   void
00316   ViewValBrancher<View,n,Val,a,Filter,Print>
00317   ::print(const Space& home, const Choice& c, unsigned int b,
00318           std::ostream& o) const {
00319     const PosValChoice<Val>& pvc
00320       = static_cast<const PosValChoice<Val>&>(c);
00321     View xi = ViewBrancher<View,Filter,n>::view(pvc.pos());
00322     if (p)
00323       p(home,*this,b,xi,pvc.pos().pos,pvc.val(),o);
00324     else
00325       vsc->print(home,b,xi,pvc.pos().pos,pvc.val(),o);
00326   }
00327 
00328   template<class View, int n, class Val, unsigned int a,
00329            class Filter, class Print>
00330   forceinline size_t
00331   ViewValBrancher<View,n,Val,a,Filter,Print>::dispose(Space& home) {
00332     if (vsc->notice() || f.notice() || p.notice())
00333       home.ignore(*this,AP_DISPOSE,true);
00334     vsc->dispose(home);
00335     (void) ViewBrancher<View,Filter,n>::dispose(home);
00336     return sizeof(ViewValBrancher<View,n,Val,a,Filter,Print>);
00337   }
00338 
00339   template<class View, int n, class Val, unsigned int a>
00340   forceinline void
00341   postviewvalbrancher(Home home,
00342                       ViewArray<View>& x,
00343                       ViewSel<View>* vs[n],
00344                       ValSelCommitBase<View,Val>* vsc,
00345                       BranchFilter<typename View::VarType> bf,
00346                       VarValPrint<typename View::VarType,Val> vvp) {
00347     if (bf) {
00348       if (vvp) {
00349         ViewValBrancher<View,n,Val,a,
00350           BrancherFilter<View>,BrancherPrint<View,Val> >
00351           ::post(home,x,vs,vsc,bf,vvp);
00352       } else {
00353         ViewValBrancher<View,n,Val,a,
00354           BrancherFilter<View>,BrancherNoPrint<View,Val> >
00355           ::post(home,x,vs,vsc,bf,vvp);
00356         }
00357     } else {
00358       if (vvp)
00359         ViewValBrancher<View,n,Val,a,
00360           BrancherNoFilter<View>,BrancherPrint<View,Val> >
00361           ::post(home,x,vs,vsc,bf,vvp);
00362       else
00363         ViewValBrancher<View,n,Val,a,
00364           BrancherNoFilter<View>,BrancherNoPrint<View,Val> >
00365           ::post(home,x,vs,vsc,bf,vvp);
00366     }
00367   }      
00368 
00369 }
00370 
00371 // STATISTICS: kernel-branch