this repo has no description
at develop 5.0 kB view raw
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Copyright: 7 * Christian Schulte, 2008 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34namespace Gecode { namespace Int { 35 36 template<class View, class A> 37 forceinline void 38 SupportValues<View,A>::reset(void) { 39 rp = rp_fst; v = rp->min; 40 max = rp->min + static_cast<int>((rp+1)->pos - rp->pos) - 1; 41 } 42 43 template<class View, class A> 44 inline 45 SupportValues<View,A>::SupportValues(A& a0, View x0) 46 : a(a0), x(x0), bs(a,x.size(),true) { 47 unsigned int n = 0; 48 for (ViewRanges<View> r(x); r(); ++r) 49 n++; 50 rp_fst = a.template alloc<RangePos>(n+1); 51 rp_lst = rp_fst + n; 52 unsigned int p = 0; 53 int i = 0; 54 for (ViewRanges<View> r(x); r(); ++r) { 55 rp_fst[i].min = r.min(); 56 rp_fst[i].pos = p; 57 p += r.width(); i++; 58 } 59 rp_fst[i].pos=p; 60 reset(); 61 } 62 63 template<class View, class A> 64 forceinline 65 SupportValues<View,A>::~SupportValues(void) { 66 bs.dispose(a); 67 a.free(rp_fst,static_cast<unsigned long int>(rp_lst-rp_fst+1)); 68 } 69 70 template<class View, class A> 71 forceinline void 72 SupportValues<View,A>::operator ++(void) { 73 if (++v > max) 74 if (++rp < rp_lst) { 75 v = rp->min; 76 max = rp->min + static_cast<int>((rp+1)->pos - rp->pos) - 1; 77 } 78 } 79 80 template<class View, class A> 81 forceinline bool 82 SupportValues<View,A>::operator ()(void) const { 83 return rp < rp_lst; 84 } 85 86 template<class View, class A> 87 forceinline int 88 SupportValues<View,A>::val(void) const { 89 return v; 90 } 91 92 template<class View, class A> 93 forceinline void 94 SupportValues<View,A>::support(void) { 95 bs.clear(rp->pos + static_cast<unsigned int>(v-rp->min)); 96 } 97 98 template<class View, class A> 99 forceinline bool 100 SupportValues<View,A>::_support(int n) { 101 RangePos* l = rp_fst; 102 RangePos* r = rp_lst-1; 103 while (true) { 104 if (l > r) return false; 105 RangePos* m = l + (r-l)/2; 106 int max = m->min + static_cast<int>((m+1)->pos - m->pos) - 1; 107 if ((n >= m->min) && (n <= max)) { 108 bs.clear(m->pos + static_cast<unsigned int>(n-m->min)); 109 return true; 110 } 111 if (l == r) return false; 112 if (n < m->min) 113 r=m-1; 114 else 115 l=m+1; 116 } 117 GECODE_NEVER; 118 return false; 119 } 120 121 template<class View, class A> 122 forceinline bool 123 SupportValues<View,A>::support(int n) { 124 if ((n < x.min()) || (n > x.max())) 125 return false; 126 return _support(n); 127 } 128 129 template<class View, class A> 130 forceinline bool 131 SupportValues<View,A>::support(long long int n) { 132 if ((n < x.min()) || (n > x.max())) 133 return false; 134 return _support(static_cast<int>(n)); 135 } 136 137 template<class View, class A> 138 forceinline void 139 SupportValues<View,A>::Unsupported::find(void) { 140 // Skip all supported positions 141 while ((p < sv.x.size()) && !sv.bs.get(p)) 142 p = sv.bs.next(p); 143 // Move to matching range 144 while ((rp < sv.rp_lst) && (p >= (rp+1)->pos)) 145 rp++; 146 } 147 148 template<class View, class A> 149 forceinline 150 SupportValues<View,A>::Unsupported::Unsupported(SupportValues& sv0) 151 : rp(sv0.rp_fst), p(0), sv(sv0) { 152 find(); 153 } 154 155 template<class View, class A> 156 forceinline void 157 SupportValues<View,A>::Unsupported::operator ++(void) { 158 p++; find(); 159 } 160 161 template<class View, class A> 162 forceinline bool 163 SupportValues<View,A>::Unsupported::operator ()(void) const { 164 return rp < sv.rp_lst; 165 } 166 167 template<class View, class A> 168 forceinline int 169 SupportValues<View,A>::Unsupported::val(void) const { 170 return static_cast<int>(rp->min+(p-rp->pos)); 171 } 172 173 template<class View, class A> 174 inline ModEvent 175 SupportValues<View,A>::tell(Space& home) { 176 Unsupported u(*this); 177 return x.minus_v(home,u,false); 178 } 179 180}} 181 182// STATISTICS: int-prop 183