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