Generated on Tue May 22 09:39:45 2018 for Gecode by doxygen 1.6.3

val.hpp

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