Generated on Mon May 5 05:53:53 2008 for Gecode by doxygen 1.5.5

sqrt.icc

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, 2008
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-02-25 00:16:01 +0100 (Mon, 25 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6288 $
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 <cmath>
00039 
00040 namespace Gecode { namespace Int { namespace Arithmetic {
00041 
00042   /*
00043    * Positive bounds-consistent squaring
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 // STATISTICS: int-prop
00138