nq.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Guido Tack <tack@gecode.org> 00005 * Christian Schulte <schulte@gecode.org> 00006 * 00007 * Contributing authors: 00008 * Gabor Szokoli <szokoli@gecode.org> 00009 * 00010 * Copyright: 00011 * Guido Tack, 2004 00012 * Christian Schulte, 2004 00013 * Gabor Szokoli, 2004 00014 * 00015 * Last modified: 00016 * $Date: 2010-06-07 14:18:14 +0200 (Mon, 07 Jun 2010) $ by $Author: tack $ 00017 * $Revision: 11048 $ 00018 * 00019 * This file is part of Gecode, the generic constraint 00020 * development environment: 00021 * http://www.gecode.org 00022 * 00023 * Permission is hereby granted, free of charge, to any person obtaining 00024 * a copy of this software and associated documentation files (the 00025 * "Software"), to deal in the Software without restriction, including 00026 * without limitation the rights to use, copy, modify, merge, publish, 00027 * distribute, sublicense, and/or sell copies of the Software, and to 00028 * permit persons to whom the Software is furnished to do so, subject to 00029 * the following conditions: 00030 * 00031 * The above copyright notice and this permission notice shall be 00032 * included in all copies or substantial portions of the Software. 00033 * 00034 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00035 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00036 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00037 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00038 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00039 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00040 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00041 * 00042 */ 00043 00044 namespace Gecode { namespace Set { namespace Rel { 00045 00046 template<class View0, class View1> 00047 forceinline 00048 Distinct<View0,View1>::Distinct(Home home, View0 x, View1 y) 00049 : MixBinaryPropagator<View0, PC_SET_VAL, View1, PC_SET_VAL>(home,x,y) {} 00050 00051 template<class View0, class View1> 00052 forceinline 00053 Distinct<View0,View1>::Distinct(Space& home, bool share, Distinct& p) 00054 : MixBinaryPropagator<View0, PC_SET_VAL, View1, PC_SET_VAL> 00055 (home,share,p) {} 00056 00057 template<class View0, class View1> 00058 ExecStatus 00059 Distinct<View0,View1>::post(Home home, View0 x, View1 y) { 00060 if (x.assigned()) { 00061 GlbRanges<View0> xr(x); 00062 IntSet xs(xr); 00063 ConstSetView cv(home, xs); 00064 GECODE_ES_CHECK((DistinctDoit<View1>::post(home,y,cv))); 00065 } 00066 if (y.assigned()) { 00067 GlbRanges<View1> yr(y); 00068 IntSet ys(yr); 00069 ConstSetView cv(home, ys); 00070 GECODE_ES_CHECK((DistinctDoit<View0>::post(home,x,cv))); 00071 } 00072 (void) new (home) Distinct<View0,View1>(home,x,y); 00073 return ES_OK; 00074 } 00075 00076 template<class View0, class View1> 00077 Actor* 00078 Distinct<View0,View1>::copy(Space& home, bool share) { 00079 return new (home) Distinct<View0,View1>(home,share,*this); 00080 } 00081 00082 template<class View0, class View1> 00083 ExecStatus 00084 Distinct<View0,View1>::propagate(Space& home, const ModEventDelta&) { 00085 assert(x0.assigned()||x1.assigned()); 00086 if (x0.assigned()) { 00087 GlbRanges<View0> xr(x0); 00088 IntSet xs(xr); 00089 ConstSetView cv(home, xs); 00090 GECODE_REWRITE(*this,(DistinctDoit<View1>::post(home(*this),x1,cv))); 00091 } else { 00092 GlbRanges<View1> yr(x1); 00093 IntSet ys(yr); 00094 ConstSetView cv(home, ys); 00095 GECODE_REWRITE(*this,(DistinctDoit<View0>::post(home(*this),x0,cv))); 00096 } 00097 } 00098 00099 template<class View0> 00100 ExecStatus 00101 DistinctDoit<View0>::post(Home home, View0 x, ConstSetView y) { 00102 (void) new (home) DistinctDoit<View0>(home,x,y); 00103 return ES_OK; 00104 } 00105 00106 template<class View0> 00107 Actor* 00108 DistinctDoit<View0>::copy(Space& home, bool share) { 00109 return new (home) DistinctDoit<View0>(home,share,*this); 00110 } 00111 00112 template<class View0> 00113 ExecStatus 00114 DistinctDoit<View0>::propagate(Space& home, const ModEventDelta&) { 00115 if (x0.assigned()) { 00116 GlbRanges<View0> xi(x0); 00117 GlbRanges<ConstSetView> yi(y); 00118 if (Iter::Ranges::equal(xi,yi)) { return ES_FAILED; } 00119 else { return home.ES_SUBSUMED(*this); } 00120 } 00121 assert(x0.lubSize()-x0.glbSize() >0); 00122 if (x0.cardMin()>y.cardMax()) { return home.ES_SUBSUMED(*this); } 00123 if (x0.cardMax()<y.cardMin()) { return home.ES_SUBSUMED(*this); } 00124 //These tests are too expensive, we should only do them 00125 //in the 1 unknown left case. 00126 GlbRanges<View0> xi1(x0); 00127 LubRanges<ConstSetView> yi1(y); 00128 if (!Iter::Ranges::subset(xi1,yi1)){ return home.ES_SUBSUMED(*this); } 00129 LubRanges<View0> xi2(x0); 00130 GlbRanges<ConstSetView> yi2(y); 00131 if (!Iter::Ranges::subset(yi2,xi2)){ return home.ES_SUBSUMED(*this); } 00132 // from here, we know y\subseteq lub(x) and glb(x)\subseteq y 00133 00134 if (x0.lubSize() == y.cardMin() && x0.lubSize() > 0) { 00135 GECODE_ME_CHECK(x0.cardMax(home, x0.lubSize() - 1)); 00136 return home.ES_SUBSUMED(*this); 00137 } 00138 if (x0.glbSize() == y.cardMin()) { 00139 GECODE_ME_CHECK(x0.cardMin(home, x0.glbSize() + 1)); 00140 return home.ES_SUBSUMED(*this); 00141 } 00142 return ES_FIX; 00143 } 00144 00145 template<class View0> 00146 forceinline 00147 DistinctDoit<View0>::DistinctDoit(Home home, View0 _x, ConstSetView _y) 00148 : UnaryPropagator<View0, PC_SET_ANY>(home,_x), y(_y) {} 00149 00150 template<class View0> 00151 forceinline 00152 DistinctDoit<View0>::DistinctDoit(Space& home, bool share, 00153 DistinctDoit<View0>& p) 00154 : UnaryPropagator<View0, PC_SET_ANY>(home,share,p) { 00155 y.update(home,share,p.y); 00156 } 00157 00158 }}} 00159 00160 // STATISTICS: set-prop