Generated on Fri Mar 20 15:56:06 2015 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: 2013-07-04 17:03:13 +0200 (Thu, 04 Jul 2013) $ by $Author: schulte $
00011  *     $Revision: 13801 $
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>
00095   forceinline
00096   ViewValuesBrancher<n,min>::
00097   ViewValuesBrancher(Home home, ViewArray<IntView>& x,
00098                      ViewSel<IntView>* vs[n], 
00099                      BranchFilter bf, IntVarValPrint vvp0)
00100     : ViewBrancher<IntView,n>(home,x,vs,bf), vvp(vvp0) {}
00101 
00102   template<int n, bool min>
00103   BrancherHandle
00104   ViewValuesBrancher<n,min>::post(Home home, ViewArray<IntView>& x, 
00105                                   ViewSel<IntView>* vs[n],
00106                                   BranchFilter bf, IntVarValPrint vvp) {
00107     return *new (home) ViewValuesBrancher<n,min>(home,x,vs,bf,vvp);
00108   }
00109 
00110   template<int n, bool min>
00111   forceinline
00112   ViewValuesBrancher<n,min>::
00113   ViewValuesBrancher(Space& home, bool shared, ViewValuesBrancher& b)
00114     : ViewBrancher<IntView,n>(home,shared,b), vvp(b.vvp) {}
00115 
00116   template<int n, bool min>
00117   Actor*
00118   ViewValuesBrancher<n,min>::copy(Space& home, bool shared) {
00119     return new (home) ViewValuesBrancher<n,min>(home,shared,*this);
00120   }
00121 
00122   template<int n, bool min>
00123   const Choice*
00124   ViewValuesBrancher<n,min>::choice(Space& home) {
00125     Pos p = ViewBrancher<IntView,n>::pos(home);
00126     return new PosValuesChoice(*this,p,
00127                                ViewBrancher<IntView,n>::view(p));
00128   }
00129 
00130   template<int n, bool min>
00131   const Choice*
00132   ViewValuesBrancher<n,min>::choice(const Space& home, Archive& e) {
00133     (void) home;
00134     int p;
00135     unsigned int a;
00136     e >> p >> a;
00137     return new PosValuesChoice(*this,a,p,e);
00138   }
00139 
00140   template<int n, bool min>
00141   ExecStatus
00142   ViewValuesBrancher<n,min>::commit(Space& home, const Choice& c, 
00143                                     unsigned int a) {
00144     const PosValuesChoice& pvc
00145       = static_cast<const PosValuesChoice&>(c);
00146     IntView x(ViewBrancher<IntView,n>::view(pvc.pos()));
00147     unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
00148     return me_failed(x.eq(home,pvc.val(b))) ? ES_FAILED : ES_OK;
00149   }
00150 
00151   template<int n, bool min>
00152   NGL*
00153   ViewValuesBrancher<n,min>::ngl(Space& home, const Choice& c, 
00154                                  unsigned int a) const {
00155     const PosValuesChoice& pvc
00156       = static_cast<const PosValuesChoice&>(c);
00157     IntView x(ViewBrancher<IntView,n>::view(pvc.pos()));
00158     unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
00159     return new (home) EqNGL<IntView>(home,x,pvc.val(b));
00160   }
00161 
00162   template<int n, bool min>
00163   void
00164   ViewValuesBrancher<n,min>::print(const Space& home, const Choice& c, 
00165                                    unsigned int a, std::ostream& o) const {
00166     const PosValuesChoice& pvc
00167       = static_cast<const PosValuesChoice&>(c);
00168     IntVar x(ViewBrancher<IntView,n>::view(pvc.pos()).varimp());
00169     unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
00170     int nn = pvc.val(b);
00171     if (vvp != NULL)
00172       vvp(home,*this,a,x,pvc.pos().pos,nn,o);
00173     else
00174       o << "var[" << pvc.pos().pos << "] = " << nn;
00175   }
00176 
00177 }}}
00178 
00179 // STATISTICS: int-branch