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, 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