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
00035
00036
00037
00038 #include <cmath>
00039
00040 namespace Gecode { namespace Int { namespace Arithmetic {
00041
00042
00043
00044
00045
00046 template <class View>
00047 forceinline
00048 Sqrt<View>::Sqrt(Space* home, View x0, View x1)
00049 : BinaryPropagator<View,PC_INT_BND>(home,x0,x1) {}
00050
00051 template <class View>
00052 forceinline ExecStatus
00053 Sqrt<View>::post(Space* home, View x0, View x1) {
00054 GECODE_ME_CHECK(x0.gq(home,0));
00055 if (same(x0,x1)) {
00056 GECODE_ME_CHECK(x1.lq(home,1));
00057 } else {
00058 GECODE_ME_CHECK(x1.gq(home,0));
00059 (void) new (home) Sqrt<View>(home,x0,x1);
00060 }
00061 return ES_OK;
00062 }
00063
00064 template <class View>
00065 forceinline void
00066 Sqrt<View>::post(Space* home, Reflection::VarMap& vars,
00067 const Reflection::ActorSpec& spec) {
00068 spec.checkArity(2);
00069 View x0(home, vars, spec[0]);
00070 View x1(home, vars, spec[1]);
00071 (void) new (home) Sqrt<View>(home,x0,x1);
00072 }
00073
00074 template <class View>
00075 forceinline
00076 Sqrt<View>::Sqrt(Space* home, bool share, Sqrt<View>& p)
00077 : BinaryPropagator<View,PC_INT_BND>(home,share,p) {}
00078
00079 template <class View>
00080 Actor*
00081 Sqrt<View>::copy(Space* home, bool share) {
00082 return new (home) Sqrt<View>(home,share,*this);
00083 }
00084
00085 template <class View>
00086 PropCost
00087 Sqrt<View>::cost(ModEventDelta) const {
00088 return PC_BINARY_HI;
00089 }
00090
00091 template <class View>
00092 ExecStatus
00093 Sqrt<View>::propagate(Space* home, ModEventDelta) {
00094 bool mod;
00095 do {
00096 mod = false;
00097 {
00098 ModEvent me = x1.lq(home,floor(::sqrt(static_cast<double>(x0.max()))));
00099 if (me_failed(me)) return ES_FAILED;
00100 mod |= me_modified(me);
00101 }
00102 {
00103 ModEvent me = x1.gq(home,ceil(::sqrt(static_cast<double>(x0.min()))-1.0));
00104 if (me_failed(me)) return ES_FAILED;
00105 mod |= me_modified(me);
00106 }
00107 {
00108 double next = static_cast<double>(x1.max()+1);
00109 ModEvent me = x0.le(home,next*next);
00110 if (me_failed(me)) return ES_FAILED;
00111 mod |= me_modified(me);
00112 }
00113 {
00114 ModEvent me = x0.gq(home,x1.min()*x1.min());
00115 if (me_failed(me)) return ES_FAILED;
00116 mod |= me_modified(me);
00117 }
00118 } while (mod);
00119 return x1.assigned() ? ES_SUBSUMED(this,home) : ES_FIX;
00120 }
00121
00122 template <class View>
00123 Support::Symbol
00124 Sqrt<View>::ati(void) {
00125 return Reflection::mangle<View>("Gecode::Int::Arithmetic::Sqrt");
00126 }
00127
00128 template <class View>
00129 Reflection::ActorSpec
00130 Sqrt<View>::spec(const Space* home, Reflection::VarMap& m) const {
00131 return BinaryPropagator<View,PC_INT_BND>::spec(home, m, ati());
00132 }
00133
00134
00135 }}}
00136
00137
00138