Generated on Thu Mar 22 10:39:41 2012 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-06-04 16:13:00 +0200 (Fri, 04 Jun 2010) $ by $Author: schulte $
00011  *     $Revision: 11028 $
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           y[i]=*x[i]=IntVar(home,x[0]->min(),x[0]->max());
00069         if ((icl == ICL_DOM) || (icl == ICL_DEF)) {
00070           GECODE_ES_CHECK(Rel::NaryEqDom<IntView>::post(home,y));
00071         } else {
00072           GECODE_ES_CHECK(Rel::NaryEqBnd<IntView>::post(home,y));
00073         }
00074       } else if (n == 2) {
00075         *x[1]=IntVar(home,x[0]->min(),x[0]->max());
00076         if ((icl == ICL_DOM) || (icl == ICL_DEF)) {
00077           GECODE_ES_CHECK((Rel::EqDom<IntView,IntView>::post
00078                            (home,*x[0],*x[1])));
00079         } else {
00080           GECODE_ES_CHECK((Rel::EqBnd<IntView,IntView>::post
00081                            (home,*x[0],*x[1])));
00082         }
00083       }
00084       return ES_OK;
00085     }
00086 
00088     forceinline ExecStatus
00089     link(Home home, BoolVar** x, int n, IntConLevel) {
00090       if (n > 2) {
00091         ViewArray<BoolView> y(home,n);
00092         y[0]=*x[0];
00093         for (int i=1; i<n; i++)
00094           y[i]=*x[i]=BoolVar(home,0,1);
00095         GECODE_ES_CHECK(Bool::NaryEq<BoolView>::post(home,y));
00096       } else if (n == 2) {
00097         *x[1] = BoolVar(home,0,1);
00098         GECODE_ES_CHECK((Bool::Eq<BoolView,BoolView>::post(home,*x[0],*x[1])));
00099       }
00100       return ES_OK;
00101     }
00102 
00104     template<class Var>
00105     forceinline ExecStatus
00106     unshare(Home home, VarArgArray<Var>& x, IntConLevel icl) {
00107       int n=x.size();
00108       if (n < 2)
00109         return ES_OK;
00110 
00111       Region r(home);
00112       Var** y = r.alloc<Var*>(n);
00113       for (int i=n; i--; )
00114         y[i]=&x[i];
00115 
00116       VarPtrLess<Var> vpl;
00117       Support::quicksort<Var*,VarPtrLess<Var> >(y,n,vpl);
00118 
00119       // Replace all shared variables with new and equal variables
00120       for (int i=0; i<n;) {
00121         int j=i++;
00122         while ((i<n) && y[j]->same(*y[i]))
00123           i++;
00124         if (!y[j]->assigned())
00125           link(home,&y[j],i-j,icl);
00126       }
00127       return ES_OK;
00128     }
00129 
00130   }}
00131 
00132   void
00133   unshare(Home home, IntVarArgs& x, IntConLevel icl) {
00134     if (home.failed()) return;
00135     GECODE_ES_FAIL(Int::Unshare::unshare<IntVar>(home,x,icl));
00136   }
00137 
00138   void
00139   unshare(Home home, BoolVarArgs& x, IntConLevel) {
00140     if (home.failed()) return;
00141     GECODE_ES_FAIL(Int::Unshare::unshare<BoolVar>(home,x,ICL_DEF));
00142   }
00143 
00144 }
00145 
00146 // STATISTICS: int-post