Generated on Mon Nov 30 23:53:30 2009 for Gecode by doxygen 1.6.1

support-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: 2009-03-06 23:36:27 +0100 (Fri, 06 Mar 2009) $ by $Author: schulte $
00011  *     $Revision: 8384 $
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 {
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     // Skip all supported positions
00162     while ((p < sv.x.size()) && sv.bit(p))
00163       p++;
00164     // Move to matching range
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 // STATISTICS: int-prop
00204