view.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, 2005 00008 * 00009 * Last modified: 00010 * $Date: 2010-06-29 10:39:13 +0200 (Tue, 29 Jun 2010) $ by $Author: schulte $ 00011 * $Revision: 11118 $ 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 #include <iostream> 00039 00040 #if (-17 / 7) == -2 00041 #define GECODE_INT_RND_TWDS_ZERO 1 00042 #else 00043 #define GECODE_INT_RND_TWDS_ZERO 0 00044 #endif 00045 00046 namespace Gecode { namespace Int { 00047 00059 template<class View> 00060 class ViewRanges { 00061 public: 00063 00064 00065 ViewRanges(void); 00067 ViewRanges(const View& x); 00069 void init(const View& x); 00071 00073 00074 00075 bool operator ()(void) const; 00077 void operator ++(void); 00079 00081 00082 00083 int min(void) const; 00085 int max(void) const; 00087 unsigned int width(void) const; 00089 }; 00090 00099 template<class View> 00100 class ViewValues : public Iter::Ranges::ToValues<ViewRanges<View> > { 00101 public: 00103 00104 00105 ViewValues(void); 00107 ViewValues(const View& x); 00109 void init(const View& x); 00111 }; 00112 00113 }} 00114 00115 #include <gecode/int/view/iter.hpp> 00116 00117 namespace Gecode { namespace Int { 00118 00135 class IntView : public VarImpView<IntVarImp> { 00136 protected: 00137 using VarImpView<IntVarImp>::x; 00138 public: 00140 00141 00142 IntView(void); 00144 IntView(const IntVar& y); 00146 IntView(IntVarImp* y); 00148 00150 00151 00152 int min(void) const; 00154 int max(void) const; 00156 int med(void) const; 00158 int val(void) const; 00159 00161 unsigned int size(void) const; 00163 unsigned int width(void) const; 00165 unsigned int regret_min(void) const; 00167 unsigned int regret_max(void) const; 00169 00171 00172 00173 bool range(void) const; 00174 00176 bool in(int n) const; 00178 bool in(double n) const; 00180 00182 00183 00184 ModEvent lq(Space& home, int n); 00186 ModEvent lq(Space& home, double n); 00187 00189 ModEvent le(Space& home, int n); 00191 ModEvent le(Space& home, double n); 00192 00194 ModEvent gq(Space& home, int n); 00196 ModEvent gq(Space& home, double n); 00197 00199 ModEvent gr(Space& home, int n); 00201 ModEvent gr(Space& home, double n); 00203 ModEvent nq(Space& home, int n); 00205 ModEvent nq(Space& home, double n); 00206 00208 ModEvent eq(Space& home, int n); 00210 ModEvent eq(Space& home, double n); 00212 00228 00229 template<class I> 00230 ModEvent narrow_r(Space& home, I& i, bool depends=true); 00232 template<class I> 00233 ModEvent inter_r(Space& home, I& i, bool depends=true); 00235 template<class I> 00236 ModEvent minus_r(Space& home, I& i, bool depends=true); 00238 template<class I> 00239 ModEvent narrow_v(Space& home, I& i, bool depends=true); 00241 template<class I> 00242 ModEvent inter_v(Space& home, I& i, bool depends=true); 00244 template<class I> 00245 ModEvent minus_v(Space& home, I& i, bool depends=true); 00247 00249 00250 00251 int min(const Delta& d) const; 00253 int max(const Delta& d) const; 00255 bool any(const Delta& d) const; 00257 00259 00260 00261 static ModEventDelta med(ModEvent me); 00263 }; 00264 00269 template<class Char, class Traits> 00270 std::basic_ostream<Char,Traits>& 00271 operator <<(std::basic_ostream<Char,Traits>& os, const IntView& x); 00272 00273 00281 class MinusView : public DerivedView<IntView> { 00282 protected: 00283 using DerivedView<IntView>::x; 00284 public: 00286 00287 00288 MinusView(void); 00290 explicit MinusView(const IntView& y); 00292 00294 00295 00296 int min(void) const; 00298 int max(void) const; 00300 int med(void) const; 00302 int val(void) const; 00303 00305 unsigned int size(void) const; 00307 unsigned int width(void) const; 00309 unsigned int regret_min(void) const; 00311 unsigned int regret_max(void) const; 00313 00315 00316 00317 bool range(void) const; 00318 00320 bool in(int n) const; 00322 bool in(double n) const; 00324 00326 00327 00328 ModEvent lq(Space& home, int n); 00330 ModEvent lq(Space& home, double n); 00332 ModEvent le(Space& home, int n); 00334 ModEvent le(Space& home, double n); 00336 ModEvent gq(Space& home, int n); 00338 ModEvent gq(Space& home, double n); 00340 ModEvent gr(Space& home, int n); 00342 ModEvent gr(Space& home, double n); 00344 ModEvent nq(Space& home, int n); 00346 ModEvent nq(Space& home, double n); 00348 ModEvent eq(Space& home, int n); 00350 ModEvent eq(Space& home, double n); 00352 00368 00369 template<class I> 00370 ModEvent narrow_r(Space& home, I& i, bool depends=true); 00372 template<class I> 00373 ModEvent inter_r(Space& home, I& i, bool depends=true); 00375 template<class I> 00376 ModEvent minus_r(Space& home, I& i, bool depends=true); 00378 template<class I> 00379 ModEvent narrow_v(Space& home, I& i, bool depends=true); 00381 template<class I> 00382 ModEvent inter_v(Space& home, I& i, bool depends=true); 00384 template<class I> 00385 ModEvent minus_v(Space& home, I& i, bool depends=true); 00387 00389 00390 00391 static ModEventDelta med(ModEvent me); 00393 00395 00396 00397 int min(const Delta& d) const; 00399 int max(const Delta& d) const; 00401 bool any(const Delta& d) const; 00403 }; 00404 00409 template<class Char, class Traits> 00410 std::basic_ostream<Char,Traits>& 00411 operator <<(std::basic_ostream<Char,Traits>& os, const MinusView& x); 00412 00413 00422 class OffsetView : public DerivedView<IntView> { 00423 protected: 00425 int c; 00426 using DerivedView<IntView>::x; 00427 public: 00429 00430 00431 OffsetView(void); 00433 OffsetView(const IntView& y, int c); 00435 00437 00438 00439 int offset(void) const; 00441 int min(void) const; 00443 int max(void) const; 00445 int med(void) const; 00447 int val(void) const; 00448 00450 unsigned int size(void) const; 00452 unsigned int width(void) const; 00454 unsigned int regret_min(void) const; 00456 unsigned int regret_max(void) const; 00458 00460 00461 00462 bool range(void) const; 00463 00465 bool in(int n) const; 00467 bool in(double n) const; 00469 00471 00472 00473 ModEvent lq(Space& home, int n); 00475 ModEvent lq(Space& home, double n); 00477 ModEvent le(Space& home, int n); 00479 ModEvent le(Space& home, double n); 00481 ModEvent gq(Space& home, int n); 00483 ModEvent gq(Space& home, double n); 00485 ModEvent gr(Space& home, int n); 00487 ModEvent gr(Space& home, double n); 00489 ModEvent nq(Space& home, int n); 00491 ModEvent nq(Space& home, double n); 00493 ModEvent eq(Space& home, int n); 00495 ModEvent eq(Space& home, double n); 00497 00513 00514 template<class I> 00515 ModEvent narrow_r(Space& home, I& i, bool depends=true); 00517 template<class I> 00518 ModEvent inter_r(Space& home, I& i, bool depends=true); 00520 template<class I> 00521 ModEvent minus_r(Space& home, I& i, bool depends=true); 00523 template<class I> 00524 ModEvent narrow_v(Space& home, I& i, bool depends=true); 00526 template<class I> 00527 ModEvent inter_v(Space& home, I& i, bool depends=true); 00529 template<class I> 00530 ModEvent minus_v(Space& home, I& i, bool depends=true); 00532 00534 00535 00536 static ModEventDelta med(ModEvent me); 00538 00540 00541 00542 int min(const Delta& d) const; 00544 int max(const Delta& d) const; 00546 bool any(const Delta& d) const; 00548 00550 00551 00552 void update(Space& home, bool share, OffsetView& y); 00554 }; 00555 00560 template<class Char, class Traits> 00561 std::basic_ostream<Char,Traits>& 00562 operator <<(std::basic_ostream<Char,Traits>& os, const OffsetView& x); 00563 00568 00569 bool same(const OffsetView& x, const OffsetView& y); 00571 bool before(const OffsetView& x, const OffsetView& y); 00573 00574 00600 template<class Val, class UnsVal> 00601 class ScaleView : public DerivedView<IntView> { 00602 protected: 00603 using DerivedView<IntView>::x; 00605 int a; 00607 00608 00609 int floor_div(double y) const; 00611 int ceil_div(double y) const; 00613 int exact_div(double y, bool& exact) const; 00614 #if GECODE_INT_RND_TWDS_ZERO 00615 00616 int floor_div(int y) const; 00618 int ceil_div(int y) const; 00620 int exact_div(int y, bool& exact) const; 00621 #endif 00622 00623 00624 public: 00626 00627 00628 ScaleView(void); 00630 ScaleView(int b, const IntView& y); 00632 00634 00635 00636 int scale(void) const; 00638 Val min(void) const; 00640 Val max(void) const; 00642 Val med(void) const; 00644 Val val(void) const; 00645 00647 UnsVal size(void) const; 00649 UnsVal width(void) const; 00651 UnsVal regret_min(void) const; 00653 UnsVal regret_max(void) const; 00655 00657 00658 00659 bool range(void) const; 00661 bool in(Val n) const; 00663 00665 00666 00667 ModEvent lq(Space& home, Val n); 00669 ModEvent le(Space& home, Val n); 00671 ModEvent gq(Space& home, Val n); 00673 ModEvent gr(Space& home, Val n); 00675 ModEvent nq(Space& home, Val n); 00677 ModEvent eq(Space& home, Val n); 00679 00681 00682 00683 static ModEventDelta med(ModEvent me); 00685 00687 00688 00689 Val min(const Delta& d) const; 00691 Val max(const Delta& d) const; 00693 bool any(const Delta& d) const; 00695 00697 00698 00699 void update(Space& home, bool share, ScaleView<Val,UnsVal>& y); 00701 }; 00702 00707 typedef ScaleView<int,unsigned int> IntScaleView; 00708 00713 typedef ScaleView<double,double> DoubleScaleView; 00714 00719 template<class Char, class Traits> 00720 std::basic_ostream<Char,Traits>& 00721 operator <<(std::basic_ostream<Char,Traits>& os, const IntScaleView& x); 00722 00727 template<class Char, class Traits> 00728 std::basic_ostream<Char,Traits>& 00729 operator <<(std::basic_ostream<Char,Traits>& os, const DoubleScaleView& x); 00730 00735 00736 template<class Val, class UnsVal> 00737 bool same(const ScaleView<Val,UnsVal>& x, const ScaleView<Val,UnsVal>& y); 00739 template<class Val, class UnsVal> 00740 bool before(const ScaleView<Val,UnsVal>& x, const ScaleView<Val,UnsVal>& y); 00742 00743 00744 00752 class ConstIntView : public ConstView<IntView> { 00753 protected: 00754 int x; 00755 public: 00757 00758 00759 ConstIntView(void); 00761 ConstIntView(int n); 00763 00765 00766 00767 int min(void) const; 00769 int max(void) const; 00771 int med(void) const; 00773 int val(void) const; 00774 00776 unsigned int size(void) const; 00778 unsigned int width(void) const; 00780 unsigned int regret_min(void) const; 00782 unsigned int regret_max(void) const; 00784 00786 00787 00788 bool range(void) const; 00790 bool in(int n) const; 00792 bool in(double n) const; 00794 00796 00797 00798 ModEvent lq(Space& home, int n); 00800 ModEvent lq(Space& home, double n); 00802 ModEvent le(Space& home, int n); 00804 ModEvent le(Space& home, double n); 00806 ModEvent gq(Space& home, int n); 00808 ModEvent gq(Space& home, double n); 00810 ModEvent gr(Space& home, int n); 00812 ModEvent gr(Space& home, double n); 00814 ModEvent nq(Space& home, int n); 00816 ModEvent nq(Space& home, double n); 00818 ModEvent eq(Space& home, int n); 00820 ModEvent eq(Space& home, double n); 00822 00838 00839 template<class I> 00840 ModEvent narrow_r(Space& home, I& i, bool depends=true); 00842 template<class I> 00843 ModEvent inter_r(Space& home, I& i, bool depends=true); 00845 template<class I> 00846 ModEvent minus_r(Space& home, I& i, bool depends=true); 00848 template<class I> 00849 ModEvent narrow_v(Space& home, I& i, bool depends=true); 00851 template<class I> 00852 ModEvent inter_v(Space& home, I& i, bool depends=true); 00854 template<class I> 00855 ModEvent minus_v(Space& home, I& i, bool depends=true); 00857 00859 00860 00861 int min(const Delta& d) const; 00863 int max(const Delta& d) const; 00865 bool any(const Delta& d) const; 00867 00869 00870 00871 void update(Space& home, bool share, ConstIntView& y); 00873 }; 00874 00879 template<class Char, class Traits> 00880 std::basic_ostream<Char,Traits>& 00881 operator <<(std::basic_ostream<Char,Traits>& os, const ConstIntView& x); 00882 00888 00889 bool same(const ConstIntView& x, const ConstIntView& y); 00891 bool before(const ConstIntView& x, const ConstIntView& y); 00893 00894 00902 class ZeroIntView : public ConstView<IntView> { 00903 public: 00905 00906 00907 ZeroIntView(void); 00909 00911 00912 00913 int min(void) const; 00915 int max(void) const; 00917 int med(void) const; 00919 int val(void) const; 00920 00922 unsigned int size(void) const; 00924 unsigned int width(void) const; 00926 unsigned int regret_min(void) const; 00928 unsigned int regret_max(void) const; 00930 00932 00933 00934 bool range(void) const; 00936 bool in(int n) const; 00938 bool in(double n) const; 00940 00942 00943 00944 ModEvent lq(Space& home, int n); 00946 ModEvent lq(Space& home, double n); 00948 ModEvent le(Space& home, int n); 00950 ModEvent le(Space& home, double n); 00952 ModEvent gq(Space& home, int n); 00954 ModEvent gq(Space& home, double n); 00956 ModEvent gr(Space& home, int n); 00958 ModEvent gr(Space& home, double n); 00960 ModEvent nq(Space& home, int n); 00962 ModEvent nq(Space& home, double n); 00964 ModEvent eq(Space& home, int n); 00966 ModEvent eq(Space& home, double n); 00968 00984 00985 template<class I> 00986 ModEvent narrow_r(Space& home, I& i, bool depends=true); 00988 template<class I> 00989 ModEvent inter_r(Space& home, I& i, bool depends=true); 00991 template<class I> 00992 ModEvent minus_r(Space& home, I& i, bool depends=true); 00994 template<class I> 00995 ModEvent narrow_v(Space& home, I& i, bool depends=true); 00997 template<class I> 00998 ModEvent inter_v(Space& home, I& i, bool depends=true); 01000 template<class I> 01001 ModEvent minus_v(Space& home, I& i, bool depends=true); 01003 01005 01006 01007 int min(const Delta& d) const; 01009 int max(const Delta& d) const; 01011 bool any(const Delta& d) const; 01013 }; 01014 01019 template<class Char, class Traits> 01020 std::basic_ostream<Char,Traits>& 01021 operator <<(std::basic_ostream<Char,Traits>& os, const ZeroIntView& x); 01022 01028 01029 bool same(const ZeroIntView& x, const ZeroIntView& y); 01031 01032 01033 01040 class BoolView : public VarImpView<BoolVarImp> { 01041 protected: 01042 using VarImpView<BoolVarImp>::x; 01043 public: 01045 01046 01047 BoolView(void); 01049 BoolView(const BoolVar& y); 01051 BoolView(BoolVarImp* y); 01053 01055 01056 01057 static const int BITS = BoolVarImp::BITS; 01059 static const BoolStatus ZERO = BoolVarImp::ZERO; 01061 static const BoolStatus ONE = BoolVarImp::ONE; 01063 static const BoolStatus NONE = BoolVarImp::NONE; 01065 BoolStatus status(void) const; 01067 01069 01070 01071 int min(void) const; 01073 int max(void) const; 01075 int med(void) const; 01077 int val(void) const; 01078 01080 unsigned int size(void) const; 01082 unsigned int width(void) const; 01084 unsigned int regret_min(void) const; 01086 unsigned int regret_max(void) const; 01088 01090 01091 01092 bool range(void) const; 01094 bool in(int n) const; 01096 bool in(double n) const; 01098 01100 01101 01102 bool zero(void) const; 01104 bool one(void) const; 01106 bool none(void) const; 01108 01110 01111 01112 ModEvent one(Space& home); 01114 ModEvent zero(Space& home); 01116 ModEvent one_none(Space& home); 01118 ModEvent zero_none(Space& home); 01120 01122 01123 01124 ModEvent lq(Space& home, int n); 01126 ModEvent lq(Space& home, double n); 01127 01129 ModEvent le(Space& home, int n); 01131 ModEvent le(Space& home, double n); 01132 01134 ModEvent gq(Space& home, int n); 01136 ModEvent gq(Space& home, double n); 01137 01139 ModEvent gr(Space& home, int n); 01141 ModEvent gr(Space& home, double n); 01142 01144 ModEvent nq(Space& home, int n); 01146 ModEvent nq(Space& home, double n); 01147 01149 ModEvent eq(Space& home, int n); 01151 ModEvent eq(Space& home, double n); 01153 01169 01170 template<class I> 01171 ModEvent narrow_r(Space& home, I& i, bool depends=true); 01173 template<class I> 01174 ModEvent inter_r(Space& home, I& i, bool depends=true); 01176 template<class I> 01177 ModEvent minus_r(Space& home, I& i, bool depends=true); 01179 template<class I> 01180 ModEvent narrow_v(Space& home, I& i, bool depends=true); 01182 template<class I> 01183 ModEvent inter_v(Space& home, I& i, bool depends=true); 01185 template<class I> 01186 ModEvent minus_v(Space& home, I& i, bool depends=true); 01188 01190 01191 01192 int min(const Delta& d) const; 01194 int max(const Delta& d) const; 01196 bool any(const Delta& d) const; 01198 static bool zero(const Delta& d); 01200 static bool one(const Delta& d); 01202 01204 01205 01206 static ModEventDelta med(ModEvent me); 01208 }; 01209 01214 template<class Char, class Traits> 01215 std::basic_ostream<Char,Traits>& 01216 operator <<(std::basic_ostream<Char,Traits>& os, const BoolView& x); 01217 01218 01219 01228 class NegBoolView : public DerivedView<BoolView> { 01229 protected: 01230 using DerivedView<BoolView>::x; 01231 public: 01233 01234 01235 NegBoolView(void); 01237 explicit NegBoolView(const BoolView& y); 01239 01241 01242 01243 static const int BITS = BoolView::BITS; 01245 static const BoolStatus ZERO = BoolView::ONE; 01247 static const BoolStatus ONE = BoolView::ZERO; 01249 static const BoolStatus NONE = BoolView::NONE; 01251 BoolStatus status(void) const; 01253 01255 01256 01257 bool zero(void) const; 01259 bool one(void) const; 01261 bool none(void) const; 01263 01265 01266 01267 ModEvent one(Space& home); 01269 ModEvent zero(Space& home); 01271 ModEvent one_none(Space& home); 01273 ModEvent zero_none(Space& home); 01275 01277 01278 01279 int min(void) const; 01281 int max(void) const; 01283 int val(void) const; 01285 01287 01288 01289 int min(const Delta& d) const; 01291 int max(const Delta& d) const; 01293 bool any(const Delta& d) const; 01295 static bool zero(const Delta& d); 01297 static bool one(const Delta& d); 01299 }; 01300 01305 template<class Char, class Traits> 01306 std::basic_ostream<Char,Traits>& 01307 operator <<(std::basic_ostream<Char,Traits>& os, const NegBoolView& x); 01308 01309 }} 01310 01311 #include <gecode/int/var/int.hpp> 01312 #include <gecode/int/var/bool.hpp> 01313 01314 #include <gecode/int/view/int.hpp> 01315 01316 #include <gecode/int/view/constint.hpp> 01317 #include <gecode/int/view/zero.hpp> 01318 #include <gecode/int/view/minus.hpp> 01319 #include <gecode/int/view/offset.hpp> 01320 #include <gecode/int/view/scale.hpp> 01321 01322 #include <gecode/int/view/bool.hpp> 01323 01324 #include <gecode/int/view/neg-bool.hpp> 01325 01326 #include <gecode/int/view/print.hpp> 01327 #include <gecode/int/var/print.hpp> 01328 01329 namespace Gecode { namespace Int { 01330 01337 01338 enum RelTest { 01339 RT_FALSE = 0, 01340 RT_MAYBE = 1, 01341 RT_TRUE = 2 01342 }; 01343 01345 template<class View> RelTest rtest_eq_bnd(View x, View y); 01347 template<class View> RelTest rtest_eq_dom(View x, View y); 01349 template<class View> RelTest rtest_eq_bnd(View x, int n); 01351 template<class View> RelTest rtest_eq_dom(View x, int n); 01352 01354 template<class View> RelTest rtest_nq_bnd(View x, View y); 01356 template<class View> RelTest rtest_nq_dom(View x, View y); 01358 template<class View> RelTest rtest_nq_bnd(View x, int n); 01360 template<class View> RelTest rtest_nq_dom(View x, int n); 01361 01363 template<class View> RelTest rtest_lq(View x, View y); 01365 template<class View> RelTest rtest_lq(View x, int n); 01366 01368 template<class View> RelTest rtest_le(View x, View y); 01370 template<class View> RelTest rtest_le(View x, int n); 01371 01373 template<class View> RelTest rtest_gq(View x, View y); 01375 template<class View> RelTest rtest_gq(View x, int n); 01376 01378 template<class View> RelTest rtest_gr(View x, View y); 01380 template<class View> RelTest rtest_gr(View x, int n); 01382 01383 01388 enum BoolTest { 01389 BT_NONE, 01390 BT_SAME, 01391 BT_COMP 01392 }; 01393 01399 01400 BoolTest bool_test(const BoolView& b0, const BoolView& b1); 01402 BoolTest bool_test(const BoolView& b0, const NegBoolView& b1); 01404 BoolTest bool_test(const NegBoolView& b0, const BoolView& b1); 01406 BoolTest bool_test(const NegBoolView& b0, const NegBoolView& b1); 01408 01409 }} 01410 01411 #include <gecode/int/view/rel-test.hpp> 01412 #include <gecode/int/view/bool-test.hpp> 01413 01414 // STATISTICS: int-var