Generated on Thu Mar 22 10:39:35 2012 for Gecode by doxygen 1.6.3

int-eq.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, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-08-29 14:59:24 +0200 (Mon, 29 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12359 $
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 Count {
00039 
00040   template<class VX, class VY>
00041   forceinline
00042   EqInt<VX,VY>::EqInt(Home home, ViewArray<VX>& x, int n_s, VY y, int c)
00043     : IntBase<VX,VY>(home,x,n_s,y,c) {}
00044 
00045   template<class VX, class VY>
00046   ExecStatus
00047   EqInt<VX,VY>::post(Home home, ViewArray<VX>& x, VY y, int c) {
00048     // Eliminate decided views
00049     int n_x = x.size();
00050     for (int i=n_x; i--; )
00051       switch (holds(x[i],y)) {
00052       case RT_FALSE:
00053         x[i] = x[--n_x]; break;
00054       case RT_TRUE:
00055         x[i] = x[--n_x]; c--; break;
00056       case RT_MAYBE:
00057         break;
00058       default:
00059         GECODE_NEVER;
00060       }
00061     x.size(n_x);
00062     // RHS too small or too large
00063     if ((c < 0) || (c > n_x))
00064       return ES_FAILED;
00065     // All views must be different
00066     if (c == 0)
00067       return post_false(home,x,y);
00068     // All views must be equal
00069     if (c == n_x)
00070       return post_true(home,x,y);
00071     // Compute how many subscriptions must be created
00072     int n_s = std::max(c,n_x-c)+1;
00073     assert(n_s <= n_x);
00074     (void) new (home) EqInt<VX,VY>(home,x,n_s,y,c);
00075     return ES_OK;
00076   }
00077 
00078   template<class VX, class VY>
00079   forceinline
00080   EqInt<VX,VY>::EqInt(Space& home, bool share, EqInt<VX,VY>& p)
00081     : IntBase<VX,VY>(home,share,p) {}
00082 
00083   template<class VX, class VY>
00084   Actor*
00085   EqInt<VX,VY>::copy(Space& home, bool share) {
00086     return new (home) EqInt<VX,VY>(home,share,*this);
00087   }
00088 
00089   template<class VX, class VY>
00090   ExecStatus
00091   EqInt<VX,VY>::propagate(Space& home, const ModEventDelta&) {
00092     // Eliminate decided views from subscribed views
00093     int n_x = x.size();
00094     for (int i=n_s; i--; )
00095       switch (holds(x[i],y)) {
00096       case RT_FALSE:
00097         x[i].cancel(home,*this,PC_INT_DOM);
00098         x[i]=x[--n_s]; x[n_s]=x[--n_x];
00099         break;
00100       case RT_TRUE:
00101         x[i].cancel(home,*this,PC_INT_DOM);
00102         x[i]=x[--n_s]; x[n_s]=x[--n_x]; c--;
00103         break;
00104       case RT_MAYBE:
00105         break;
00106       default:
00107         GECODE_NEVER;
00108       }
00109     x.size(n_x);
00110     if ((c < 0) || (c > n_x))
00111       return ES_FAILED;
00112     // Eliminate decided views from unsubscribed views
00113     for (int i=n_x; i-- > n_s; )
00114       switch (holds(x[i],y)) {
00115       case RT_FALSE: x[i]=x[--n_x]; break;
00116       case RT_TRUE:  x[i]=x[--n_x]; c--; break;
00117       case RT_MAYBE: break;
00118       default:       GECODE_NEVER;
00119       }
00120     x.size(n_x);
00121     if ((c < 0) || (c > n_x))
00122       return ES_FAILED;
00123     if (c == 0) {
00124       // All views must be different
00125       GECODE_ES_CHECK(post_false(home,x,y));
00126       return home.ES_SUBSUMED(*this);
00127     }
00128     if (c == n_x) {
00129       // All views must be equal
00130       GECODE_ES_CHECK(post_true(home,x,y));
00131       return home.ES_SUBSUMED(*this);
00132     }
00133     int m = std::max(c,n_x-c)+1;
00134     assert(m <= n_x);
00135     // Now, there must be new subscriptions from x[n_s] up to x[m-1]
00136     while (n_s < m)
00137       x[n_s++].subscribe(home,*this,PC_INT_DOM,false);
00138     return ES_FIX;
00139   }
00140 
00141 }}}
00142 
00143 // STATISTICS: int-prop