Generated on Thu Mar 22 10:39:33 2012 for Gecode by doxygen 1.6.3

arithmetic.cpp

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, 2002
00008  *
00009  *  Last modified:
00010  *     $Date: 2010-06-03 20:34:20 +0200 (Thu, 03 Jun 2010) $ by $Author: schulte $
00011  *     $Revision: 11017 $
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 <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 // STATISTICS: int-post