gecode » int » view

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, 2003 00008 * 00009 * Last modified: 00010 * $Date: 2009-11-23 15:38:32 +0100 (Mon, 23 Nov 2009) $ by $Author: schulte $ 00011 * $Revision: 10098 $ 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 { 00039 00040 namespace Int { 00041 00042 /* 00043 * Constructors and initialization 00044 * 00045 */ 00046 forceinline 00047 ConstIntView::ConstIntView(void) {} 00048 forceinline 00049 ConstIntView::ConstIntView(int n) : x(n) {} 00050 forceinline void 00051 ConstIntView::init(int n) { 00052 x=n; 00053 } 00054 00055 00056 /* 00057 * Value access 00058 * 00059 */ 00060 forceinline int 00061 ConstIntView::min(void) const { 00062 return x; 00063 } 00064 forceinline int 00065 ConstIntView::max(void) const { 00066 return x; 00067 } 00068 forceinline int 00069 ConstIntView::med(void) const { 00070 return x; 00071 } 00072 forceinline int 00073 ConstIntView::val(void) const { 00074 return x; 00075 } 00076 00077 forceinline unsigned int 00078 ConstIntView::size(void) const { 00079 return 1; 00080 } 00081 forceinline unsigned int 00082 ConstIntView::width(void) const { 00083 return 1; 00084 } 00085 forceinline unsigned int 00086 ConstIntView::regret_min(void) const { 00087 return 0; 00088 } 00089 forceinline unsigned int 00090 ConstIntView::regret_max(void) const { 00091 return 0; 00092 } 00093 00094 00095 /* 00096 * Domain tests 00097 * 00098 */ 00099 forceinline bool 00100 ConstIntView::range(void) const { 00101 return true; 00102 } 00103 forceinline bool 00104 ConstIntView::assigned(void) const { 00105 return true; 00106 } 00107 00108 forceinline bool 00109 ConstIntView::in(int n) const { 00110 return n == x; 00111 } 00112 forceinline bool 00113 ConstIntView::in(double n) const { 00114 return n == x; 00115 } 00116 00117 00118 /* 00119 * Domain update by value 00120 * 00121 */ 00122 forceinline ModEvent 00123 ConstIntView::lq(Space&, int n) { 00124 return (x <= n) ? ME_INT_NONE : ME_INT_FAILED; 00125 } 00126 forceinline ModEvent 00127 ConstIntView::lq(Space&, double n) { 00128 return (x <= n) ? ME_INT_NONE : ME_INT_FAILED; 00129 } 00130 00131 forceinline ModEvent 00132 ConstIntView::le(Space&, int n) { 00133 return (x < n) ? ME_INT_NONE : ME_INT_FAILED; 00134 } 00135 forceinline ModEvent 00136 ConstIntView::le(Space&, double n) { 00137 return (x < n) ? ME_INT_NONE : ME_INT_FAILED; 00138 } 00139 00140 forceinline ModEvent 00141 ConstIntView::gq(Space&, int n) { 00142 return (x >= n) ? ME_INT_NONE : ME_INT_FAILED; 00143 } 00144 forceinline ModEvent 00145 ConstIntView::gq(Space&, double n) { 00146 return (x >= n) ? ME_INT_NONE : ME_INT_FAILED; 00147 } 00148 00149 forceinline ModEvent 00150 ConstIntView::gr(Space&, int n) { 00151 return (x > n) ? ME_INT_NONE : ME_INT_FAILED; 00152 } 00153 forceinline ModEvent 00154 ConstIntView::gr(Space&, double n) { 00155 return (x > n) ? ME_INT_NONE : ME_INT_FAILED; 00156 } 00157 00158 forceinline ModEvent 00159 ConstIntView::nq(Space&, int n) { 00160 return (x != n) ? ME_INT_NONE : ME_INT_FAILED; 00161 } 00162 forceinline ModEvent 00163 ConstIntView::nq(Space&, double n) { 00164 return (x != n) ? ME_INT_NONE : ME_INT_FAILED; 00165 } 00166 00167 forceinline ModEvent 00168 ConstIntView::eq(Space&, int n) { 00169 return (x == n) ? ME_INT_NONE : ME_INT_FAILED; 00170 } 00171 forceinline ModEvent 00172 ConstIntView::eq(Space&, double n) { 00173 return (x == n) ? ME_INT_NONE : ME_INT_FAILED; 00174 } 00175 00176 00177 00178 /* 00179 * Iterator-based domain update 00180 * 00181 */ 00182 template<class I> 00183 forceinline ModEvent 00184 ConstIntView::narrow_r(Space&, I& i, bool) { 00185 Iter::Ranges::IsRangeIter<I>(); 00186 return i() ? ME_INT_NONE : ME_INT_FAILED; 00187 } 00188 template<class I> 00189 forceinline ModEvent 00190 ConstIntView::inter_r(Space&, I& i, bool) { 00191 Iter::Ranges::IsRangeIter<I>(); 00192 while (i() && (i.max() < x)) 00193 ++i; 00194 return (i() && (i.min() <= x)) ? ME_INT_NONE : ME_INT_FAILED; 00195 } 00196 template<class I> 00197 forceinline ModEvent 00198 ConstIntView::minus_r(Space&, I& i, bool) { 00199 Iter::Ranges::IsRangeIter<I>(); 00200 while (i() && (i.max() < x)) 00201 ++i; 00202 return (i() && (i.min() <= x)) ? ME_INT_FAILED : ME_INT_NONE; 00203 } 00204 template<class I> 00205 forceinline ModEvent 00206 ConstIntView::narrow_v(Space&, I& i, bool) { 00207 Iter::Values::IsValueIter<I>(); 00208 return i() ? ME_INT_NONE : ME_INT_FAILED; 00209 } 00210 template<class I> 00211 forceinline ModEvent 00212 ConstIntView::inter_v(Space&, I& i, bool) { 00213 Iter::Values::IsValueIter<I>(); 00214 while (i() && (i.val() < x)) 00215 ++i; 00216 return (i() && (i.val() == x)) ? ME_INT_NONE : ME_INT_FAILED; 00217 } 00218 template<class I> 00219 forceinline ModEvent 00220 ConstIntView::minus_v(Space&, I& i, bool) { 00221 Iter::Values::IsValueIter<I>(); 00222 while (i() && (i.val() < x)) 00223 ++i; 00224 return (i() && (i.val() == x)) ? ME_INT_FAILED : ME_INT_NONE; 00225 } 00226 00227 00228 00229 /* 00230 * Propagator modification events 00231 * 00232 */ 00233 forceinline void 00234 ConstIntView::schedule(Space& home, Propagator& p, ModEvent me) { 00235 return IntView::schedule(home,p,me); 00236 } 00237 forceinline ModEvent 00238 ConstIntView::me(const ModEventDelta&) { 00239 return ME_INT_NONE; 00240 } 00241 forceinline ModEventDelta 00242 ConstIntView::med(ModEvent me) { 00243 return static_cast<ModEventDelta>(me); 00244 } 00245 00246 00247 /* 00248 * Dependencies 00249 * 00250 */ 00251 forceinline void 00252 ConstIntView::subscribe(Space& home, Propagator& p, PropCond, 00253 bool process) { 00254 if (process) 00255 schedule(home,p,ME_INT_VAL); 00256 } 00257 forceinline void 00258 ConstIntView::cancel(Space&,Propagator&,PropCond) {} 00259 forceinline void 00260 ConstIntView::subscribe(Space&, Advisor&) {} 00261 forceinline void 00262 ConstIntView::cancel(Space&,Advisor&) {} 00263 00264 00265 00266 /* 00267 * Delta information for advisors 00268 * 00269 */ 00270 forceinline ModEvent 00271 ConstIntView::modevent(const Delta&) { 00272 return ME_INT_NONE; 00273 } 00274 forceinline int 00275 ConstIntView::min(const Delta&) const { 00276 return 1; 00277 } 00278 forceinline int 00279 ConstIntView::max(const Delta&) const { 00280 return 0; 00281 } 00282 forceinline bool 00283 ConstIntView::any(const Delta&) const { 00284 return true; 00285 } 00286 00287 00288 00289 /* 00290 * Cloning 00291 * 00292 */ 00293 forceinline void 00294 ConstIntView::update(Space&, bool, ConstIntView& y) { 00295 x = y.x; 00296 } 00297 00298 00303 template<> 00304 class ViewRanges<ConstIntView> { 00305 private: 00307 int n; 00308 public: 00310 00311 00312 ViewRanges(void); 00314 ViewRanges(const ConstIntView& x); 00316 void init(const ConstIntView& x); 00318 00320 00321 00322 bool operator ()(void) const; 00324 void operator ++(void); 00326 00328 00329 00330 int min(void) const; 00332 int max(void) const; 00334 unsigned int width(void) const; 00336 }; 00337 00338 forceinline 00339 ViewRanges<ConstIntView>::ViewRanges(void) {} 00340 00341 forceinline 00342 ViewRanges<ConstIntView>::ViewRanges(const ConstIntView& x) 00343 : n(x.val()) {} 00344 00345 forceinline bool 00346 ViewRanges<ConstIntView>::operator ()(void) const { 00347 return n <= Limits::max; 00348 } 00349 forceinline void 00350 ViewRanges<ConstIntView>::operator ++(void) { 00351 n = Limits::max+1; 00352 } 00353 00354 forceinline int 00355 ViewRanges<ConstIntView>::min(void) const { 00356 return n; 00357 } 00358 forceinline int 00359 ViewRanges<ConstIntView>::max(void) const { 00360 return n; 00361 } 00362 forceinline unsigned int 00363 ViewRanges<ConstIntView>::width(void) const { 00364 return 1; 00365 } 00366 00367 } 00368 00369 /* 00370 * View comparison 00371 * 00372 */ 00373 forceinline bool 00374 same(const Int::ConstIntView& x, const Int::ConstIntView& y) { 00375 return x.min() == y.min(); 00376 } 00377 forceinline bool 00378 same(const Int::IntView&, const Int::ConstIntView&) { 00379 return false; 00380 } 00381 forceinline bool 00382 same(const Int::BoolView&, const Int::ConstIntView&) { 00383 return false; 00384 } 00385 forceinline bool 00386 before(const Int::ConstIntView& x, const Int::ConstIntView& y) { 00387 return x.min() < y.min(); 00388 } 00389 00390 } 00391 00392 // STATISTICS: int-var 00393