Generated on Mon May 10 06:46:40 2010 for Gecode by doxygen 1.6.3

unshare.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, 2007
00008  *
00009  *  Last modified:
00010  *     $Date: 2010-03-03 17:40:32 +0100 (Wed, 03 Mar 2010) $ by $Author: schulte $
00011  *     $Revision: 10365 $
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/rel.hh>
00039 #include <gecode/int/bool.hh>
00040 
00046 namespace Gecode {
00047 
00048  namespace Int { namespace Unshare {
00049 
00051     template<class Var>
00052     class VarPtrLess {
00053     public:
00054       forceinline bool
00055       operator ()(const Var* a, const Var* b) {
00056         return a->before(*b);
00057       }
00058     };
00059 
00060 
00062     forceinline ExecStatus
00063     link(Home home, IntVar** x, int n, IntConLevel icl) {
00064       if (n > 2) {
00065         ViewArray<IntView> y(home,n);
00066         y[0]=*x[0];
00067         for (int i=1; i<n; i++) {
00068           x[i]->init(home,x[0]->min(),x[0]->max()); y[i]=*x[i];
00069         }
00070         if ((icl == ICL_DOM) || (icl == ICL_DEF)) {
00071           GECODE_ES_CHECK(Rel::NaryEqDom<IntView>::post(home,y));
00072         } else {
00073           GECODE_ES_CHECK(Rel::NaryEqBnd<IntView>::post(home,y));
00074         }
00075       } else if (n == 2) {
00076         IntVar z(home,x[0]->min(),x[0]->max());
00077         *x[1]=z;
00078         if ((icl == ICL_DOM) || (icl == ICL_DEF)) {
00079           GECODE_ES_CHECK((Rel::EqDom<IntView,IntView>::post
00080                            (home,*x[0],*x[1])));
00081         } else {
00082           GECODE_ES_CHECK((Rel::EqBnd<IntView,IntView>::post
00083                            (home,*x[0],*x[1])));
00084         }
00085       }
00086       return ES_OK;
00087     }
00088 
00090     forceinline ExecStatus
00091     link(Home home, BoolVar** x, int n, IntConLevel) {
00092       if (n > 2) {
00093         ViewArray<BoolView> y(home,n);
00094         y[0]=*x[0];
00095         for (int i=1; i<n; i++) {
00096           x[i]->init(home,0,1); y[i]=*x[i];
00097         }
00098         GECODE_ES_CHECK(Bool::NaryEq<BoolView>::post(home,y));
00099       } else if (n == 2) {
00100         x[1]->init(home,0,1);
00101         GECODE_ES_CHECK((Bool::Eq<BoolView,BoolView>::post(home,*x[0],*x[1])));
00102       }
00103       return ES_OK;
00104     }
00105 
00107     template<class Var>
00108     forceinline ExecStatus
00109     unshare(Home home, VarArgArray<Var>& x, IntConLevel icl) {
00110       int n=x.size();
00111       if (n < 2)
00112         return ES_OK;
00113 
00114       Region r(home);
00115       Var** y = r.alloc<Var*>(n);
00116       for (int i=n; i--; )
00117         y[i]=&x[i];
00118 
00119       VarPtrLess<Var> vpl;
00120       Support::quicksort<Var*,VarPtrLess<Var> >(y,n,vpl);
00121 
00122       // Replace all shared variables with new and equal variables
00123       for (int i=0; i<n;) {
00124         int j=i++;
00125         while ((i<n) && y[j]->same(*y[i]))
00126           i++;
00127         if (!y[j]->assigned())
00128           link(home,&y[j],i-j,icl);
00129       }
00130       return ES_OK;
00131     }
00132 
00133   }}
00134 
00135   void
00136   unshare(Home home, IntVarArgs& x, IntConLevel icl) {
00137     if (home.failed()) return;
00138     GECODE_ES_FAIL(Int::Unshare::unshare<IntVar>(home,x,icl));
00139   }
00140 
00141   void
00142   unshare(Home home, BoolVarArgs& x, IntConLevel) {
00143     if (home.failed()) return;
00144     GECODE_ES_FAIL(Int::Unshare::unshare<BoolVar>(home,x,ICL_DEF));
00145   }
00146 
00147 }
00148 
00149 // STATISTICS: int-post