Generated on Thu Apr 11 13:59:12 2019 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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/int/rel.hh>
00035 #include <gecode/int/bool.hh>
00036 
00042 namespace Gecode { namespace Int { namespace Unshare {
00043 
00045   template<class Var>
00046   class VarPtrLess {
00047   public:
00048     forceinline bool
00049     operator ()(const Var* a, const Var* b) {
00050       return a->varimp() < b->varimp();
00051     }
00052   };
00053   
00054 
00056   forceinline void
00057   link(Home home, IntVar** x, int n, IntPropLevel ipl) {
00058     if (home.failed()) {
00059       for (int i=1; i<n; i++)
00060         *x[i]=IntVar(home,x[0]->min(),x[0]->min());
00061     } else if (n > 2) {
00062       ViewArray<IntView> y(home,n);
00063       y[0]=*x[0];
00064       for (int i=1; i<n; i++)
00065         y[i]=*x[i]=IntVar(home,x[0]->min(),x[0]->max());
00066       if ((ipl == IPL_DOM) || (ipl == IPL_DEF)) {
00067         ExecStatus es = Rel::NaryEqDom<IntView>::post(home,y);
00068         (void) es; assert(es == ES_OK);
00069       } else {
00070         ExecStatus es = Rel::NaryEqBnd<IntView>::post(home,y);
00071         (void) es; assert(es == ES_OK);
00072       }
00073     } else if (n == 2) {
00074       *x[1]=IntVar(home,x[0]->min(),x[0]->max());
00075       if ((ipl == IPL_DOM) || (ipl == IPL_DEF)) {
00076         ExecStatus es = Rel::EqDom<IntView,IntView>::post(home,*x[0],*x[1]);
00077         (void) es; assert(es == ES_OK);
00078       } else {
00079         ExecStatus es = Rel::EqBnd<IntView,IntView>::post(home,*x[0],*x[1]);
00080         (void) es; assert(es == ES_OK);
00081       }
00082     }
00083   }
00084 
00086   forceinline void
00087   link(Home home, BoolVar** x, int n, IntPropLevel) {
00088     if (home.failed()) {
00089       for (int i=1; i<n; i++)
00090         *x[i]=BoolVar(home,0,0);
00091     } else if (n > 2) {
00092       ViewArray<BoolView> y(home,n);
00093       y[0]=*x[0];
00094       for (int i=1; i<n; i++)
00095         y[i]=*x[i]=BoolVar(home,0,1);
00096       ExecStatus es = Bool::NaryEq<BoolView>::post(home,y);
00097       (void) es; assert(es == ES_OK);
00098     } else if (n == 2) {
00099       *x[1] = BoolVar(home,0,1);
00100       ExecStatus es = Bool::Eq<BoolView,BoolView>::post(home,*x[0],*x[1]);
00101       (void) es; assert(es == ES_OK);
00102     }
00103   }
00104 
00106   template<class Var>
00107   forceinline void
00108   unshare(Home home, VarArgArray<Var>& x, IntPropLevel ipl) {
00109     int n=x.size();
00110     if (n < 2)
00111       return;
00112     
00113     Region r;
00114     Var** y = r.alloc<Var*>(n);
00115     for (int i=0; i<n; i++)
00116       y[i]=&x[i];
00117 
00118     VarPtrLess<Var> vpl;
00119     Support::quicksort<Var*,VarPtrLess<Var> >(y,n,vpl);
00120     
00121     // Replace all shared variables with new and equal variables
00122     for (int i=0; i<n;) {
00123       int j=i++;
00124       while ((i<n) && (y[j]->varimp() == y[i]->varimp()))
00125         i++;
00126       if (!y[j]->assigned())
00127         link(home,&y[j],i-j,ipl);
00128     }
00129   }
00130 
00131 }}}
00132 
00133 namespace Gecode {
00134 
00135   void
00136   unshare(Home home, IntVarArgs& x, IntPropLevel ipl) {
00137     Int::Unshare::unshare<IntVar>(home,x,vbd(ipl));
00138   }
00139 
00140   void
00141   unshare(Home home, BoolVarArgs& x, IntPropLevel) {
00142     Int::Unshare::unshare<BoolVar>(home,x,IPL_DEF);
00143   }
00144 
00145 }
00146 
00147 // STATISTICS: int-post