Generated on Mon Aug 25 11:35:36 2008 for Gecode by doxygen 1.5.6

val.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, 2003
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-01-31 18:29:16 +0100 (Thu, 31 Jan 2008) $ by $Author: tack $
00011  *     $Revision: 6017 $
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 namespace Gecode { namespace Int { namespace Distinct {
00039 
00040   /*
00041    * Eliminating singleton variables
00042    *
00043    */
00044   template <class View, bool complete>
00045   ExecStatus
00046   prop_val(Space* home, ViewArray<View>& x) {
00047     assert(x.size() > 1);
00048     int n = x.size();
00049 
00050     GECODE_AUTOARRAY(int, stack, n);
00051     int* c_v = &stack[0];
00052     // c_n is the current number of values on stack
00053     int c_n = 0;
00054 
00055     // Collect all assigned variables on stack
00056     for (int i = n; i--; )
00057       if (x[i].assigned()) {
00058         c_v[c_n++]=x[i].val(); x[i]=x[--n];
00059       }
00060 
00061     // The number of trips
00062     int t = 0;
00063     do {
00064       t++;
00065       if (!complete && (t > 16)) {
00066         // Give up after sixteen iterations, but the values must be
00067         // propagated first
00068         // Maybe we are lucky in that this iteration does the trick...
00069         ExecStatus es = ES_FIX;
00070         while (c_n > 0) {
00071           int v = c_v[--c_n];
00072           // Check whether value is on stack only once
00073           for (int i = c_n; i--; )
00074             if (c_v[i] == v)
00075               goto failed;
00076           // Tell and do not collect new values
00077           for (int i = n; i--; ) {
00078             ModEvent me = x[i].nq(home,v);
00079             if (me_failed(me))
00080               goto failed;
00081             if (me == ME_INT_VAL)
00082               es = ES_NOFIX;
00083           }
00084         }
00085         x.size(n);
00086         return es;
00087       }
00088       if (c_n > 31) {
00089         // Many values, use full domain operation
00090         IntSet d(&c_v[0],c_n);
00091         // Check for failure
00092         unsigned int s = 0;
00093         for (int i = d.size(); i--; )
00094           s += d.width(i);
00095         // If the size s of d is different from the number of values,
00096         // a value must have appeared multiply: failure
00097         if (s != static_cast<unsigned int>(c_n))
00098           goto failed;
00099         // We do not need the values on the stack any longer, reset
00100         c_n = 0;
00101         // Tell and collect new values
00102         for (int i = n; i--; )
00103           if ((d.min() <= x[i].max()) && (d.max() >= x[i].min())) {
00104             IntSetRanges dr(d);
00105             ModEvent me = x[i].minus_r(home,dr,false);
00106             if (me_failed(me))
00107               goto failed;
00108             if (me == ME_INT_VAL) {
00109               c_v[c_n++]=x[i].val(); x[i]=x[--n];
00110             }
00111           }
00112       } else {
00113         // Values for next iteration
00114         int* n_v = &c_v[c_n];
00115         // Stack top for the next iteration
00116         int n_n = 0;
00117         while (c_n > 0) {
00118           int v = c_v[--c_n];
00119           // Check whether value is not on current stack
00120           for (int i = c_n; i--; )
00121             if (c_v[i] == v)
00122               goto failed;
00123           // Check whether value is not on next stack
00124           for (int i = n_n; i--; )
00125             if (n_v[i] == v)
00126               goto failed;
00127           // Tell and collect new values
00128           for (int i = n; i--; ) {
00129             ModEvent me = x[i].nq(home,v);
00130             if (me_failed(me))
00131               goto failed;
00132             if (me == ME_INT_VAL) {
00133               n_v[n_n++]=x[i].val(); x[i]=x[--n];
00134             }
00135           }
00136         }
00137         c_v = n_v; c_n = n_n;
00138       }
00139     } while (c_n > 0);
00140     x.size(n);
00141     return ES_FIX;
00142   failed:
00143     x.size(0);
00144     return ES_FAILED;
00145   }
00146 
00147 
00148   /*
00149    * The propagator proper
00150    *
00151    */
00152   template <class View>
00153   forceinline
00154   Val<View>::Val(Space* home, ViewArray<View>& x)
00155   : NaryPropagator<View,PC_INT_VAL>(home,x) {}
00156 
00157   template <class View>
00158   forceinline
00159   Val<View>::Val(Space* home, bool share, Val<View>& p)
00160     : NaryPropagator<View,PC_INT_VAL>(home,share,p) {}
00161 
00162   template <class View>
00163   Actor*
00164   Val<View>::copy(Space* home, bool share) {
00165     return new (home) Val<View>(home,share,*this);
00166   }
00167 
00168   template <class View>
00169   Support::Symbol
00170   Val<View>::ati(void) {
00171     return Reflection::mangle<View>("Gecode::Int::Distinct::Val");
00172   }
00173 
00174   template <class View>
00175   Reflection::ActorSpec
00176   Val<View>::spec(const Space* home, Reflection::VarMap& m) const {
00177     return NaryPropagator<View,PC_INT_VAL>::spec(home, m, ati());
00178   }
00179 
00180   template <class View>
00181   ExecStatus
00182   Val<View>::propagate(Space* home, ModEventDelta) {
00183     GECODE_ES_CHECK((prop_val<View,true>(home,x)));
00184     return (x.size() < 2) ? ES_SUBSUMED(this,home) : ES_FIX;
00185   }
00186 
00187   template <class View>
00188   ExecStatus
00189   Val<View>::post(Space* home, ViewArray<View>& x) {
00190     if (x.size() == 2)
00191       return Rel::Nq<View>::post(home,x[0],x[1]);
00192     if (x.size() > 2)
00193       (void) new (home) Val<View>(home,x);
00194     return ES_OK;
00195   }
00196 
00197   template <class View>
00198   void
00199   Val<View>::post(Space* home, Reflection::VarMap& vars,
00200                   const Reflection::ActorSpec& spec) {
00201     spec.checkArity(1);
00202     ViewArray<View> x(home, vars, spec[0]);
00203     (void) new (home) Val<View>(home, x);
00204   }
00205 
00206 }}}
00207 
00208 // STATISTICS: int-prop
00209