Generated on Wed Nov 1 15:04:32 2006 for Gecode by doxygen 1.4.5

view.icc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2003
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-09-07 11:57:31 +0200 (Thu, 07 Sep 2006) $ by $Author: schulte $
00010  *     $Revision: 3612 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 namespace Gecode { namespace Int { namespace Count {
00023 
00024   /*
00025    * General baseclass
00026    *
00027    */
00028 
00029   template <class VX, class VY, class VZ, bool shr>
00030   BaseView<VX,VY,VZ,shr>::BaseView(Space* home,
00031                                    ViewArray<VX>& x0, VY y0, VZ z0, int c0)
00032     : Propagator(home), x(x0), y(y0), z(z0), c(c0) {
00033     x.subscribe(home,this,PC_INT_DOM);
00034     y.subscribe(home,this,PC_INT_DOM);
00035     z.subscribe(home,this,PC_INT_BND);
00036   }
00037 
00038   template <class VX, class VY, class VZ, bool shr>
00039   inline
00040   BaseView<VX,VY,VZ,shr>::BaseView(Space* home, bool share,
00041                                    BaseView<VX,VY,VZ,shr>& p)
00042     : Propagator(home,shr,p), c(p.c) {
00043     x.update(home,share,p.x);
00044     y.update(home,share,p.y);
00045     z.update(home,share,p.z);
00046   }
00047 
00048   template <class VX, class VY, class VZ, bool shr>
00049   PropCost
00050   BaseView<VX,VY,VZ,shr>::cost(void) const {
00051     return cost_lo(x.size()+1, PC_LINEAR_LO);
00052   }
00053 
00054   template <class VX, class VY, class VZ, bool shr>
00055   size_t
00056   BaseView<VX,VY,VZ,shr>::dispose(Space* home) {
00057     assert(!home->failed());
00058     x.cancel(home,this,PC_INT_DOM);
00059     y.cancel(home,this,PC_INT_DOM);
00060     z.cancel(home,this,PC_INT_BND);
00061     (void) Propagator::dispose(home);
00062     return sizeof(*this);
00063   }
00064 
00065   template <class VX, class VY, class VZ, bool shr>
00066   forceinline void
00067   BaseView<VX,VY,VZ,shr>::count(Space* home) {
00068     int n = x.size();
00069     for (int i=n; i--; )
00070       switch (holds(x[i],y)) {
00071       case RT_FALSE:
00072         x[i].cancel(home,this,PC_INT_DOM); x[i]=x[--n];
00073         break;
00074       case RT_TRUE:
00075         x[i].cancel(home,this,PC_INT_DOM); x[i]=x[--n];
00076         c--;
00077         break;
00078       case RT_MAYBE:
00079         break;
00080       default: 
00081         GECODE_NEVER;
00082       }
00083     x.size(n);
00084   }
00085 
00086   template <class VX, class VY, class VZ, bool shr>
00087   forceinline int
00088   BaseView<VX,VY,VZ,shr>::atleast(void) const {
00089     return -c;
00090   }
00091 
00092   template <class VX, class VY, class VZ, bool shr>
00093   forceinline int
00094   BaseView<VX,VY,VZ,shr>::atmost(void) const {
00095     return x.size()-c;
00096   }
00097 
00098   template <class VX, class VY, class VZ, bool shr>
00099   inline bool
00100   BaseView<VX,VY,VZ,shr>::sharing(const ViewArray<VX>& x,
00101                                   const VY& y, const VZ& z) {
00102     if (shared(y,z))
00103       return true;
00104     for (int i = x.size(); i--; )
00105       if (shared(x[i],z))
00106         return true;
00107     return false;
00108   }
00109 
00110   /*
00111    * Equality
00112    *
00113    */
00114 
00115   template <class VX, class VY, class VZ, bool shr>
00116   forceinline
00117   EqView<VX,VY,VZ,shr>::EqView(Space* home, 
00118                                ViewArray<VX>& x, VY y, VZ z, int c)
00119     : BaseView<VX,VY,VZ,shr>(home,x,y,z,c) {}
00120 
00121   template <class VX, class VY, class VZ, bool shr>
00122   ExecStatus
00123   EqView<VX,VY,VZ,shr>::post(Space* home, 
00124                              ViewArray<VX>& x, VY y, VZ z, int c) {
00125     if (z.assigned())
00126       return EqInt<VX,VY>::post(home,x,y,z.val()+c);
00127     if (sharing(x,y,z))
00128       (void) new (home) EqView<VX,VY,VZ,true>(home,x,y,z,c);
00129     else
00130       (void) new (home) EqView<VX,VY,VZ,false>(home,x,y,z,c);
00131     return ES_OK;
00132   }
00133 
00134   template <class VX, class VY, class VZ, bool shr>
00135   forceinline
00136   EqView<VX,VY,VZ,shr>::EqView(Space* home, bool share, 
00137                                EqView<VX,VY,VZ,shr>& p)
00138     : BaseView<VX,VY,VZ,shr>(home,share,p) {}
00139 
00140   template <class VX, class VY, class VZ, bool shr>
00141   Actor*
00142   EqView<VX,VY,VZ,shr>::copy(Space* home, bool share) {
00143     return new (home) EqView<VX,VY,VZ,shr>(home,share,*this);
00144   }
00145 
00146   template <class VX, class VY, class VZ, bool shr>
00147   ExecStatus
00148   EqView<VX,VY,VZ,shr>::propagate(Space* home) {
00149     count(home);
00150 
00151     GECODE_ME_CHECK(z.gq(home,atleast()));
00152     GECODE_ME_CHECK(z.lq(home,atmost()));
00153 
00154     if (z.assigned()) {
00155       if (z.val() == atleast())
00156         return post_false(home,x,y);
00157       if (z.val() == atmost())
00158         return post_true(home,x,y);
00159       GECODE_ES_CHECK((EqInt<VX,VY>::post(home,x,y,z.val()+c)));
00160       return ES_SUBSUMED;
00161     }
00162     return shr ? ES_NOFIX : ES_FIX;
00163   }
00164 
00165 
00166 
00167 
00168   /*
00169    * Disequality
00170    *
00171    */
00172 
00173   template <class VX, class VY, class VZ, bool shr>
00174   forceinline
00175   NqView<VX,VY,VZ,shr>::NqView(Space* home, 
00176                                ViewArray<VX>& x, VY y, VZ z, int c)
00177     : BaseView<VX,VY,VZ,shr>(home,x,y,z,c) {}
00178 
00179   template <class VX, class VY, class VZ, bool shr>
00180   ExecStatus
00181   NqView<VX,VY,VZ,shr>::post(Space* home, 
00182                              ViewArray<VX>& x, VY y, VZ z, int c) {
00183     if (z.assigned())
00184       return NqInt<VX,VY>::post(home,x,y,z.val()+c);
00185     (void) new (home) NqView<VX,VY,VZ,shr>(home,x,y,z,c);
00186     return ES_OK;
00187   }
00188 
00189   template <class VX, class VY, class VZ, bool shr>
00190   forceinline
00191   NqView<VX,VY,VZ,shr>::NqView(Space* home, bool share, 
00192                                NqView<VX,VY,VZ,shr>& p)
00193     : BaseView<VX,VY,VZ,shr>(home,share,p) {}
00194 
00195   template <class VX, class VY, class VZ, bool shr>
00196   Actor*
00197   NqView<VX,VY,VZ,shr>::copy(Space* home, bool share) {
00198     return new (home) NqView<VX,VY,VZ,shr>(home,share,*this);
00199   }
00200 
00201   template <class VX, class VY, class VZ, bool shr>
00202   ExecStatus
00203   NqView<VX,VY,VZ,shr>::propagate(Space* home) {
00204     count(home);
00205     if (atleast() == atmost()) {
00206       GECODE_ME_CHECK(z.nq(home,atleast()));
00207       return ES_SUBSUMED;
00208     }
00209     if (z.max() < atleast())
00210       return ES_SUBSUMED;
00211     if (z.min() > atmost())
00212       return ES_SUBSUMED;
00213     if (z.assigned()) {
00214       GECODE_ES_CHECK((NqInt<VX,VY>::post(home,x,y,z.val()+c)));
00215       return ES_SUBSUMED;
00216     }
00217     return ES_FIX;
00218   }
00219 
00220 
00221 
00222   /*
00223    * Less or equal
00224    *
00225    */
00226 
00227   template <class VX, class VY, class VZ, bool shr>
00228   forceinline
00229   LqView<VX,VY,VZ,shr>::LqView(Space* home, ViewArray<VX>& x, 
00230                                VY y, VZ z, int c)
00231     : BaseView<VX,VY,VZ,shr>(home,x,y,z,c) {}
00232 
00233   template <class VX, class VY, class VZ, bool shr>
00234   ExecStatus
00235   LqView<VX,VY,VZ,shr>::post(Space* home, ViewArray<VX>& x, 
00236                              VY y, VZ z, int c) {
00237     if (z.assigned())
00238       return LqInt<VX,VY>::post(home,x,y,z.val()+c);
00239     if (sharing(x,y,z))
00240       (void) new (home) LqView<VX,VY,VZ,true>(home,x,y,z,c);
00241     else
00242       (void) new (home) LqView<VX,VY,VZ,false>(home,x,y,z,c);
00243     return ES_OK;
00244   }
00245 
00246   template <class VX, class VY, class VZ, bool shr>
00247   forceinline
00248   LqView<VX,VY,VZ,shr>::LqView(Space* home, bool share, 
00249                                LqView<VX,VY,VZ,shr>& p)
00250     : BaseView<VX,VY,VZ,shr>(home,share,p) {}
00251 
00252   template <class VX, class VY, class VZ, bool shr>
00253   Actor*
00254   LqView<VX,VY,VZ,shr>::copy(Space* home, bool share) {
00255     return new (home) LqView<VX,VY,VZ,shr>(home,share,*this);
00256   }
00257 
00258   template <class VX, class VY, class VZ, bool shr>
00259   ExecStatus
00260   LqView<VX,VY,VZ,shr>::propagate(Space* home) {
00261     count(home);
00262     GECODE_ME_CHECK(z.gq(home,atleast()));
00263     if (z.max() == atleast())
00264       return post_false(home,x,y);
00265     if (x.size() == 0)
00266       return ES_SUBSUMED;
00267     if (z.assigned()) {
00268       GECODE_ES_CHECK((LqInt<VX,VY>::post(home,x,y,z.val()+c)));
00269       return ES_SUBSUMED;
00270     }
00271     return shr ? ES_NOFIX : ES_FIX;
00272   }
00273 
00274 
00275 
00276   /*
00277    * Greater or equal
00278    *
00279    */
00280 
00281   template <class VX, class VY, class VZ, bool shr>
00282   forceinline
00283   GqView<VX,VY,VZ,shr>::GqView(Space* home, ViewArray<VX>& x, VY y, VZ z, int c)
00284     : BaseView<VX,VY,VZ,shr>(home,x,y,z,c) {}
00285 
00286   template <class VX, class VY, class VZ, bool shr>
00287   ExecStatus
00288   GqView<VX,VY,VZ,shr>::post(Space* home, 
00289                              ViewArray<VX>& x, VY y, VZ z, int c) {
00290     if (z.assigned())
00291       return GqInt<VX,VY>::post(home,x,y,z.val()+c);
00292     if (sharing(x,y,z))
00293       (void) new (home) GqView<VX,VY,VZ,true>(home,x,y,z,c);
00294     else
00295       (void) new (home) GqView<VX,VY,VZ,false>(home,x,y,z,c);
00296     return ES_OK;
00297   }
00298 
00299   template <class VX, class VY, class VZ, bool shr>
00300   forceinline
00301   GqView<VX,VY,VZ,shr>::GqView(Space* home, bool share, 
00302                                GqView<VX,VY,VZ,shr>& p)
00303     : BaseView<VX,VY,VZ,shr>(home,share,p) {}
00304 
00305   template <class VX, class VY, class VZ, bool shr>
00306   Actor*
00307   GqView<VX,VY,VZ,shr>::copy(Space* home, bool share) {
00308     return new (home) GqView<VX,VY,VZ,shr>(home,share,*this);
00309   }
00310 
00311   template <class VX, class VY, class VZ, bool shr>
00312   ExecStatus
00313   GqView<VX,VY,VZ,shr>::propagate(Space* home) {
00314     count(home);
00315 
00316     GECODE_ME_CHECK(z.lq(home,atmost()));
00317 
00318     if (z.min() == atmost())
00319       return post_true(home,x,y);
00320     if (x.size() == 0)
00321       return ES_SUBSUMED;
00322     if (z.assigned()) {
00323       GECODE_ES_CHECK((GqInt<VX,VY>::post(home,x,y,z.val()+c)));
00324       return ES_SUBSUMED;
00325     }
00326     return shr ? ES_NOFIX : ES_FIX;
00327   }
00328 
00329 }}}
00330 
00331 // STATISTICS: int-prop
00332