00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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