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 <gecode/int/arithmetic.hh>
00039
00040 namespace Gecode {
00041
00042 using namespace Int;
00043
00044 void
00045 abs(Home home, IntVar x0, IntVar x1, IntConLevel icl) {
00046 if (home.failed()) return;
00047 if (icl == ICL_DOM) {
00048 GECODE_ES_FAIL(Arithmetic::AbsDom<IntView>::post(home,x0,x1));
00049 } else {
00050 GECODE_ES_FAIL(Arithmetic::AbsBnd<IntView>::post(home,x0,x1));
00051 }
00052 }
00053
00054
00055 void
00056 max(Home home, IntVar x0, IntVar x1, IntVar x2,
00057 IntConLevel icl) {
00058 if (home.failed()) return;
00059 if (icl == ICL_DOM) {
00060 GECODE_ES_FAIL(Arithmetic::MaxDom<IntView>::post(home,x0,x1,x2));
00061 } else {
00062 GECODE_ES_FAIL(Arithmetic::MaxBnd<IntView>::post(home,x0,x1,x2));
00063 }
00064 }
00065
00066 void
00067 max(Home home, const IntVarArgs& x, IntVar y,
00068 IntConLevel icl) {
00069 if (x.size() == 0)
00070 throw TooFewArguments("Int::max");
00071 if (home.failed()) return;
00072 ViewArray<IntView> xv(home,x);
00073 if (icl == ICL_DOM) {
00074 GECODE_ES_FAIL(Arithmetic::NaryMaxDom<IntView>::post(home,xv,y));
00075 } else {
00076 GECODE_ES_FAIL(Arithmetic::NaryMaxBnd<IntView>::post(home,xv,y));
00077 }
00078 }
00079
00080
00081 void
00082 min(Home home, IntVar x0, IntVar x1, IntVar x2,
00083 IntConLevel icl) {
00084 if (home.failed()) return;
00085 MinusView m0(x0); MinusView m1(x1); MinusView m2(x2);
00086 if (icl == ICL_DOM) {
00087 GECODE_ES_FAIL(Arithmetic::MaxDom<MinusView>::post(home,m0,m1,m2));
00088 } else {
00089 GECODE_ES_FAIL(Arithmetic::MaxBnd<MinusView>::post(home,m0,m1,m2));
00090 }
00091 }
00092
00093 void
00094 min(Home home, const IntVarArgs& x, IntVar y,
00095 IntConLevel icl) {
00096 if (x.size() == 0)
00097 throw TooFewArguments("Int::min");
00098 if (home.failed()) return;
00099 ViewArray<MinusView> m(home,x.size());
00100 for (int i=x.size(); i--; )
00101 m[i] = MinusView(x[i]);
00102 MinusView my(y);
00103 if (icl == ICL_DOM) {
00104 GECODE_ES_FAIL(Arithmetic::NaryMaxDom<MinusView>::post(home,m,my));
00105 } else {
00106 GECODE_ES_FAIL(Arithmetic::NaryMaxBnd<MinusView>::post(home,m,my));
00107 }
00108 }
00109
00110
00111 void
00112 mult(Home home, IntVar x0, IntVar x1, IntVar x2,
00113 IntConLevel icl) {
00114 if (home.failed()) return;
00115 if (icl == ICL_DOM) {
00116 GECODE_ES_FAIL(Arithmetic::MultDom<IntView>::post(home,x0,x1,x2));
00117 } else {
00118 GECODE_ES_FAIL(Arithmetic::MultBnd<IntView>::post(home,x0,x1,x2));
00119 }
00120 }
00121
00122
00123 void
00124 sqr(Home home, IntVar x0, IntVar x1, IntConLevel icl) {
00125 if (home.failed()) return;
00126 if (icl == ICL_DOM) {
00127 GECODE_ES_FAIL(Arithmetic::SqrDom<IntView>::post(home,x0,x1));
00128 } else {
00129 GECODE_ES_FAIL(Arithmetic::SqrBnd<IntView>::post(home,x0,x1));
00130 }
00131 }
00132
00133 void
00134 sqrt(Home home, IntVar x0, IntVar x1, IntConLevel icl) {
00135 if (home.failed()) return;
00136 if (icl == ICL_DOM) {
00137 GECODE_ES_FAIL(Arithmetic::SqrtDom<IntView>::post(home,x0,x1));
00138 } else {
00139 GECODE_ES_FAIL(Arithmetic::SqrtBnd<IntView>::post(home,x0,x1));
00140 }
00141 }
00142
00143 void
00144 divmod(Home home, IntVar x0, IntVar x1, IntVar x2, IntVar x3,
00145 IntConLevel) {
00146 if (home.failed()) return;
00147
00148 IntVar prod(home, Int::Limits::min, Int::Limits::max);
00149 GECODE_ES_FAIL(
00150 Arithmetic::MultBnd<IntView>::post(home,x1,x2,prod));
00151 Linear::Term<IntView> t[3];
00152 t[0].a = 1; t[0].x = prod;
00153 t[1].a = 1; t[1].x = x3;
00154 int min, max;
00155 Linear::estimate(t,2,0,min,max);
00156 IntView x0v(x0);
00157 GECODE_ME_FAIL( x0v.gq(home,min));
00158 GECODE_ME_FAIL( x0v.lq(home,max));
00159 t[2].a=-1; t[2].x=x0;
00160 Linear::post(home,t,3,IRT_EQ,0);
00161 if (home.failed()) return;
00162 IntView x1v(x1);
00163 GECODE_ES_FAIL(
00164 Arithmetic::DivMod<IntView>::post(home,x0,x1,x3));
00165 }
00166
00167 void
00168 div(Home home, IntVar x0, IntVar x1, IntVar x2,
00169 IntConLevel) {
00170 if (home.failed()) return;
00171 GECODE_ES_FAIL(
00172 (Arithmetic::DivBnd<IntView>::post(home,x0,x1,x2)));
00173 }
00174
00175 void
00176 mod(Home home, IntVar x0, IntVar x1, IntVar x2,
00177 IntConLevel icl) {
00178 if (home.failed()) return;
00179 IntVar _div(home, Int::Limits::min, Int::Limits::max);
00180 divmod(home, x0, x1, _div, x2, icl);
00181 }
00182
00183 }
00184
00185