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 {
00039
00040 template<class View, class A>
00041 forceinline void
00042 SupportValues<View,A>::set(unsigned int i) {
00043 unsigned int p = i / bpui;
00044 bits[p] |= 1 << (i-p*bpui);
00045 }
00046
00047 template<class View, class A>
00048 forceinline bool
00049 SupportValues<View,A>::bit(unsigned int i) const {
00050 unsigned int p = i / bpui;
00051 return (bits[p] & (1 << (i-p*bpui))) != 0;
00052 }
00053
00054 template<class View, class A>
00055 forceinline void
00056 SupportValues<View,A>::reset(void) {
00057 rp = rp_fst; v = rp->min;
00058 max = rp->min + static_cast<int>((rp+1)->pos - rp->pos) - 1;
00059 }
00060
00061 template<class View, class A>
00062 inline
00063 SupportValues<View,A>::SupportValues(A& a0, View x0)
00064 : a(a0), x(x0), sz((x.size() / bpui) + 1),
00065 bits(a.template alloc<unsigned int>(sz)) {
00066 for (unsigned int i = (x.size() / bpui) + 1; i--; )
00067 bits[i] = 0;
00068 unsigned int n = 0;
00069 for (ViewRanges<View> r(x); r(); ++r)
00070 n++;
00071 rp_fst = a.template alloc<RangePos>(n+1);
00072 rp_lst = rp_fst + n;
00073 unsigned int p = 0;
00074 int i = 0;
00075 for (ViewRanges<View> r(x); r(); ++r) {
00076 rp_fst[i].min = r.min();
00077 rp_fst[i].pos = p;
00078 p += r.width(); i++;
00079 }
00080 rp_fst[i].pos=p;
00081 reset();
00082 }
00083
00084 template<class View, class A>
00085 inline
00086 SupportValues<View,A>::~SupportValues(void) {
00087 a.free(bits,sz);
00088 a.free(rp_fst,static_cast<unsigned long int>(rp_lst-rp_fst+1));
00089 }
00090
00091 template<class View, class A>
00092 forceinline void
00093 SupportValues<View,A>::operator ++(void) {
00094 if (++v > max)
00095 if (++rp < rp_lst) {
00096 v = rp->min;
00097 max = rp->min + static_cast<int>((rp+1)->pos - rp->pos) - 1;
00098 }
00099 }
00100
00101 template<class View, class A>
00102 forceinline bool
00103 SupportValues<View,A>::operator ()(void) const {
00104 return rp < rp_lst;
00105 }
00106
00107 template<class View, class A>
00108 forceinline int
00109 SupportValues<View,A>::val(void) const {
00110 return v;
00111 }
00112
00113 template<class View, class A>
00114 forceinline void
00115 SupportValues<View,A>::support(void) {
00116 set(rp->pos + static_cast<unsigned int>(v-rp->min));
00117 }
00118
00119 template<class View, class A>
00120 forceinline bool
00121 SupportValues<View,A>::_support(int n) {
00122 RangePos* l = rp_fst;
00123 RangePos* r = rp_lst-1;
00124 while (true) {
00125 if (l > r) return false;
00126 RangePos* m = l + (r-l)/2;
00127 int max = m->min + static_cast<int>((m+1)->pos - m->pos) - 1;
00128 if ((n >= m->min) && (n <= max)) {
00129 set(m->pos + static_cast<unsigned int>(n-m->min));
00130 return true;
00131 }
00132 if (l == r) return false;
00133 if (n < m->min)
00134 r=m-1;
00135 else
00136 l=m+1;
00137 }
00138 GECODE_NEVER;
00139 return false;
00140 }
00141
00142 template<class View, class A>
00143 forceinline bool
00144 SupportValues<View,A>::support(int n) {
00145 if ((n < x.min()) || (n > x.max()))
00146 return false;
00147 return _support(n);
00148 }
00149
00150 template<class View, class A>
00151 forceinline bool
00152 SupportValues<View,A>::support(double n) {
00153 if ((n < x.min()) || (n > x.max()))
00154 return false;
00155 return _support(static_cast<int>(n));
00156 }
00157
00158 template<class View, class A>
00159 forceinline void
00160 SupportValues<View,A>::Unsupported::find(void) {
00161
00162 while ((p < sv.x.size()) && sv.bit(p))
00163 p++;
00164
00165 while ((rp < sv.rp_lst) && (p >= (rp+1)->pos))
00166 rp++;
00167 }
00168
00169 template<class View, class A>
00170 forceinline
00171 SupportValues<View,A>::Unsupported::Unsupported(SupportValues& sv0)
00172 : rp(sv0.rp_fst), p(0), sv(sv0) {
00173 find();
00174 }
00175
00176 template<class View, class A>
00177 forceinline void
00178 SupportValues<View,A>::Unsupported::operator ++(void) {
00179 p++; find();
00180 }
00181
00182 template<class View, class A>
00183 forceinline bool
00184 SupportValues<View,A>::Unsupported::operator ()(void) const {
00185 return rp < sv.rp_lst;
00186 }
00187
00188 template<class View, class A>
00189 forceinline int
00190 SupportValues<View,A>::Unsupported::val(void) const {
00191 return static_cast<int>(rp->min+(p-rp->pos));
00192 }
00193
00194 template<class View, class A>
00195 inline ModEvent
00196 SupportValues<View,A>::tell(Space& home) {
00197 Unsupported u(*this);
00198 return x.minus_v(home,u,false);
00199 }
00200
00201 }}
00202
00203
00204