Generated on Tue Apr 18 10:21:49 2017 for Gecode by doxygen 1.6.3

view-values.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, 2008
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 { namespace Int { namespace Branch {
00039 
00041   class GECODE_VTABLE_EXPORT PosValuesChoice : public PosChoice {
00042   private:
00044     class PosMin {
00045     public:
00047       unsigned int pos;
00049       int min;
00050     };
00052     unsigned int n;
00054     PosMin* pm;
00055   public:
00057     GECODE_INT_EXPORT
00058     PosValuesChoice(const Brancher& b, const Pos& p, IntView x);
00060     GECODE_INT_EXPORT
00061     PosValuesChoice(const Brancher& b, unsigned int alt, Pos p, Archive& e);
00063     int val(unsigned int a) const;
00065     GECODE_INT_EXPORT
00066     virtual size_t size(void) const;
00068     GECODE_INT_EXPORT
00069     virtual ~PosValuesChoice(void);
00071     GECODE_INT_EXPORT
00072     virtual void archive(Archive& e) const;
00073   };
00074 
00075   forceinline int
00076   PosValuesChoice::val(unsigned int a) const {
00077     PosMin* l = &pm[0];
00078     PosMin* r = &pm[n-1];
00079     while (true) {
00080       PosMin* m = l + (r-l)/2;
00081       if (a < m->pos) {
00082         r=m-1;
00083       } else if (a >= (m+1)->pos) {
00084         l=m+1;
00085       } else {
00086         return m->min + static_cast<int>(a - m->pos);
00087       }
00088     }
00089     GECODE_NEVER;
00090     return 0;
00091   }
00092 
00093 
00094   template<int n, bool min, class Filter, class Print>
00095   forceinline
00096   ViewValuesBrancher<n,min,Filter,Print>::
00097   ViewValuesBrancher(Home home, ViewArray<IntView>& x,
00098                      ViewSel<IntView>* vs[n],
00099                      IntBranchFilter bf,
00100                      IntVarValPrint vvp)
00101     : ViewBrancher<IntView,Filter,n>(home,x,vs,bf), p(vvp) {
00102     if (p.notice())
00103       home.notice(*this,AP_DISPOSE,true);
00104   }
00105 
00106   template<int n, bool min, class Filter, class Print>
00107   forceinline void
00108   ViewValuesBrancher<n,min,Filter,Print>::post(Home home, 
00109                                                ViewArray<IntView>& x,
00110                                                ViewSel<IntView>* vs[n],
00111                                                IntBranchFilter bf,
00112                                                IntVarValPrint vvp) {
00113     (void) new (home) ViewValuesBrancher<n,min,Filter,Print>(home,x,vs,bf,vvp);
00114   }
00115 
00116   template<int n, bool min, class Filter, class Print>
00117   forceinline
00118   ViewValuesBrancher<n,min,Filter,Print>::
00119   ViewValuesBrancher(Space& home, bool shared, ViewValuesBrancher& b)
00120     : ViewBrancher<IntView,Filter,n>(home,shared,b), p(home,shared,b.p) {}
00121 
00122   template<int n, bool min, class Filter, class Print>
00123   Actor*
00124   ViewValuesBrancher<n,min,Filter,Print>::copy(Space& home, bool shared) {
00125     return new (home) ViewValuesBrancher<n,min,Filter,Print>
00126       (home,shared,*this);
00127   }
00128 
00129   template<int n, bool min, class Filter, class Print>
00130   const Choice*
00131   ViewValuesBrancher<n,min,Filter,Print>::choice(Space& home) {
00132     Pos p = ViewBrancher<IntView,Filter,n>::pos(home);
00133     return new PosValuesChoice(*this,p,
00134                                ViewBrancher<IntView,Filter,n>::view(p));
00135   }
00136 
00137   template<int n, bool min, class Filter, class Print>
00138   const Choice*
00139   ViewValuesBrancher<n,min,Filter,Print>::choice
00140   (const Space& home, Archive& e) {
00141     (void) home;
00142     int p;
00143     unsigned int a;
00144     e >> p >> a;
00145     return new PosValuesChoice(*this,a,p,e);
00146   }
00147 
00148   template<int n, bool min, class Filter, class Print>
00149   ExecStatus
00150   ViewValuesBrancher<n,min,Filter,Print>::commit(Space& home, const Choice& c,
00151                                                  unsigned int a) {
00152     const PosValuesChoice& pvc
00153       = static_cast<const PosValuesChoice&>(c);
00154     IntView x(ViewBrancher<IntView,Filter,n>::view(pvc.pos()));
00155     unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
00156     return me_failed(x.eq(home,pvc.val(b))) ? ES_FAILED : ES_OK;
00157   }
00158 
00159   template<int n, bool min, class Filter, class Print>
00160   NGL*
00161   ViewValuesBrancher<n,min,Filter,Print>::ngl(Space& home, const Choice& c,
00162                                               unsigned int a) const {
00163     const PosValuesChoice& pvc
00164       = static_cast<const PosValuesChoice&>(c);
00165     IntView x(ViewBrancher<IntView,Filter,n>::view(pvc.pos()));
00166     unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
00167     return new (home) EqNGL<IntView>(home,x,pvc.val(b));
00168   }
00169 
00170   template<int n, bool min, class Filter, class Print>
00171   void
00172   ViewValuesBrancher<n,min,Filter,Print>::print(const Space& home,
00173                                                 const Choice& c,
00174                                                 unsigned int a, 
00175                                                 std::ostream& o) const {
00176     const PosValuesChoice& pvc
00177       = static_cast<const PosValuesChoice&>(c);
00178     IntView x(ViewBrancher<IntView,Filter,n>::view(pvc.pos()));
00179     unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
00180     int nn = pvc.val(b);
00181     if (p)
00182       p(home,*this,a,x,pvc.pos().pos,nn,o);
00183     else
00184       o << "var[" << pvc.pos().pos << "] = " << nn;
00185   }
00186 
00187   template<int n, bool min, class Filter, class Print>
00188   forceinline size_t
00189   ViewValuesBrancher<n,min,Filter,Print>::dispose(Space& home) {
00190     if (p.notice())
00191       home.ignore(*this,AP_DISPOSE,true);
00192     (void) ViewBrancher<IntView,Filter,n>::dispose(home);
00193     return sizeof(ViewValuesBrancher<n,min,Filter,Print>);
00194   }
00195 
00196   template<int n, bool min>
00197   forceinline void
00198   postviewvaluesbrancher(Home home, ViewArray<IntView>& x,
00199                          ViewSel<IntView>* vs[n],
00200                          IntBranchFilter bf,
00201                          IntVarValPrint vvp) {
00202     if (bf) {
00203       if (vvp) {
00204         ViewValuesBrancher<n,min,BrancherFilter<IntView>,
00205           BrancherPrint<IntView,int> >
00206           ::post(home,x,vs,bf,vvp);
00207       } else {
00208         ViewValuesBrancher<n,min,BrancherFilter<IntView>,
00209           BrancherNoPrint<IntView,int> >
00210           ::post(home,x,vs,bf,vvp);
00211       }
00212     } else {
00213       if (vvp) {
00214         ViewValuesBrancher<n,min,BrancherNoFilter<IntView>,
00215           BrancherPrint<IntView,int> >
00216           ::post(home,x,vs,bf,vvp);
00217       } else {
00218         ViewValuesBrancher<n,min,BrancherNoFilter<IntView>,
00219           BrancherNoPrint<IntView,int> >
00220           ::post(home,x,vs,bf,vvp);
00221       }
00222     }
00223   }
00224 
00225 
00226 }}}
00227 
00228 // STATISTICS: int-branch