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

int.cpp

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  *  Copyright:
00008  *     Guido Tack, 2004
00009  *     Christian Schulte, 2004
00010  *
00011  *  Last modified:
00012  *     $Date: 2011-08-18 12:26:27 +0200 (Thu, 18 Aug 2011) $ by $Author: tack $
00013  *     $Revision: 12314 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include <gecode/set.hh>
00041 
00042 #include <gecode/set/int.hh>
00043 #include <gecode/set/rel.hh>
00044 
00045 using namespace Gecode::Int;
00046 
00047 namespace Gecode {
00048 
00049   void
00050   rel(Home home, SetVar s, IntRelType r, IntVar x) {
00051     if (home.failed()) return;
00052     switch (r) {
00053     case IRT_EQ:
00054       {
00055         Gecode::Int::IntView xv(x);
00056         Set::SingletonView xsingle(xv);
00057         GECODE_ES_FAIL(
00058                        (Set::Rel::Eq<Set::SetView,Set::SingletonView>
00059                         ::post(home,s,xsingle)));
00060 
00061       }
00062       break;
00063     case IRT_NQ:
00064       {
00065         Gecode::Set::SetView sv(s);
00066         GECODE_ME_FAIL( sv.cardMin(home, 1));
00067         Gecode::Int::IntView xv(x);
00068         Set::SingletonView xsingle(xv);
00069         GECODE_ES_FAIL(
00070                        (Set::Rel::NoSubset<Set::SingletonView,Set::SetView>
00071                         ::post(home,xsingle,sv)));
00072 
00073       }
00074       break;
00075     case IRT_LQ:
00076       {
00077         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00078         rel(home, tmp, IRT_LQ, x);
00079         GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,tmp));
00080       }
00081       break;
00082     case IRT_LE:
00083       {
00084         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00085         rel(home, tmp, IRT_LE, x);
00086         GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,tmp));
00087       }
00088       break;
00089     case IRT_GQ:
00090       {
00091         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00092         rel(home, tmp, IRT_GQ, x);
00093         GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,tmp));
00094       }
00095       break;
00096     case IRT_GR:
00097       {
00098         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00099         rel(home, tmp, IRT_GR, x);
00100         GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,tmp));
00101       }
00102       break;
00103     default:
00104       throw UnknownRelation("Set::rel");
00105     }
00106 
00107   }
00108 
00109   void
00110   rel(Home home, IntVar x, IntRelType r, SetVar s) {
00111     IntRelType rr;
00112     switch (r) {
00113     case IRT_LE: rr=IRT_GR; break;
00114     case IRT_LQ: rr=IRT_GQ; break;
00115     case IRT_GR: rr=IRT_LE; break;
00116     case IRT_GQ: rr=IRT_LQ; break;
00117     default: rr=r;
00118     }
00119     rel(home, s, rr, x);
00120   }
00121 
00122   void
00123   min(Home home, SetVar s, IntVar x){
00124     if (home.failed()) return;
00125     GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,x));
00126   }
00127   void
00128   notMin(Home home, SetVar s, IntVar x){
00129     if (home.failed()) return;
00130     GECODE_ES_FAIL(Set::Int::NotMinElement<Set::SetView>::post(home,s,x));
00131   }
00132   void
00133   min(Home home, SetVar s, IntVar x, BoolVar b){
00134     if (home.failed()) return;
00135     GECODE_ES_FAIL(
00136                    Set::Int::ReMinElement<Set::SetView>::post(home,s,x,b));
00137   }
00138   void
00139   max(Home home, SetVar s, IntVar x){
00140     if (home.failed()) return;
00141     GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,x));
00142   }
00143   void
00144   notMax(Home home, SetVar s, IntVar x){
00145     if (home.failed()) return;
00146     GECODE_ES_FAIL(Set::Int::NotMaxElement<Set::SetView>::post(home,s,x));
00147   }
00148   void
00149   max(Home home, SetVar s, IntVar x, BoolVar b){
00150     if (home.failed()) return;
00151     GECODE_ES_FAIL(
00152                    Set::Int::ReMaxElement<Set::SetView>::post(home,s,x,b));
00153   }
00154 
00155   void
00156   channelSorted(Home home, const IntVarArgs& x, SetVar y) {
00157     if (home.failed()) return;
00158     ViewArray<IntView> xa(home,x);
00159     GECODE_ES_FAIL(Set::Int::Match<Set::SetView>::post(home,y,xa));
00160   }
00161 
00162   void
00163   channel(Home home, const IntVarArgs& x, const SetVarArgs& y) {
00164     if (home.failed()) return;
00165     ViewArray<Int::CachedView<Int::IntView> > xa(home,x.size());
00166     for (int i=x.size(); i--;)
00167       new (&xa[i]) Int::CachedView<Int::IntView>(x[i]);
00168     ViewArray<Set::CachedView<Set::SetView> > ya(home,y.size());
00169     for (int i=y.size(); i--;)
00170       new (&ya[i]) Set::CachedView<Set::SetView>(y[i]);
00171     GECODE_ES_FAIL((Set::Int::ChannelInt<Set::SetView>::post(home,xa,ya)));
00172   }
00173 
00174   void
00175   channel(Home home, const BoolVarArgs& x, SetVar y) {
00176     if (home.failed()) return;
00177     ViewArray<Int::BoolView> xv(home,x);
00178     GECODE_ES_FAIL((Set::Int::ChannelBool<Set::SetView>
00179                          ::post(home,xv,y)));
00180   }
00181 
00182   void weights(Home home, IntSharedArray elements, IntSharedArray weights,
00183                SetVar x, IntVar y) {
00184     if (home.failed()) return;
00185     GECODE_ES_FAIL(Set::Int::Weights<Set::SetView>::post(home,elements,
00186                                                               weights,x,y));
00187   }
00188 
00189 }
00190 
00191 // STATISTICS: set-post