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

set.cc

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: 2008-07-11 09:39:08 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00013  *     $Revision: 7297 $
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 
00043 namespace Gecode { namespace Set {
00044 
00045   /*
00046    * "Standard" tell operations
00047    *
00048    */
00049   ModEvent
00050   SetVarImp::cardMin_full(Space* home) {
00051     ModEvent me = ME_SET_CARD;
00052     if (_cardMin == lub.size()) {
00053       glb.become(home, lub);
00054       me = ME_SET_VAL;
00055     }
00056     SetDelta d;
00057     return notify(home, me, &d);
00058   }
00059 
00060   ModEvent
00061   SetVarImp::cardMax_full(Space* home) {
00062     ModEvent me = ME_SET_CARD;
00063     if (_cardMax == glb.size()) {
00064       lub.become(home, glb);
00065       me = ME_SET_VAL;
00066     }
00067     SetDelta d;
00068     return notify(home, me, &d);
00069   }
00070 
00071   ModEvent
00072   SetVarImp::processLubChange(Space* home, SetDelta& d) {
00073     ModEvent me = ME_SET_LUB;
00074     if (_cardMax > lub.size()) {
00075       _cardMax = lub.size();
00076       if (_cardMin > _cardMax) {
00077         glb.become(home, lub);
00078         _cardMin = glb.size();
00079         _cardMax = glb.size();
00080         return ME_SET_FAILED;
00081       }
00082       me = ME_SET_CLUB;
00083     }
00084     if (_cardMax == lub.size() && _cardMin == _cardMax) {
00085       glb.become(home, lub);
00086       me = ME_SET_VAL;
00087       assert(d.glbMin() == 1);
00088       assert(d.glbMax() == 0);
00089     }
00090     return notify(home, me, &d);
00091   }
00092 
00093   ModEvent
00094   SetVarImp::processGlbChange(Space* home, SetDelta& d) {
00095     ModEvent me = ME_SET_GLB;
00096     if (_cardMin < glb.size()) {
00097       _cardMin = glb.size();
00098       if (_cardMin > _cardMax) {
00099         glb.become(home, lub);
00100         _cardMin = glb.size();
00101         _cardMax = glb.size();
00102         return ME_SET_FAILED;
00103       }
00104       me = ME_SET_CGLB;
00105     }
00106     if (_cardMin == glb.size() && _cardMin == _cardMax) {
00107       lub.become(home, glb);
00108       me = ME_SET_VAL;
00109       assert(d.lubMin() == 1);
00110       assert(d.lubMax() == 0);
00111     }
00112     return notify(home, me, &d);
00113   }
00114 
00115   /*
00116    * Copying variables
00117    *
00118    */
00119 
00120   forceinline
00121   SetVarImp::SetVarImp(Space* home, bool share, SetVarImp& x)
00122     : SetVarImpBase(home,share,x),
00123       _cardMin(x._cardMin), _cardMax(x._cardMax) {
00124     lub.update(home, x.lub);
00125     if (x.assigned()) {
00126       glb.become(home,lub);
00127     } else {
00128       glb.update(home,x.glb);
00129     }
00130   }
00131 
00132 
00133   SetVarImp*
00134   SetVarImp::perform_copy(Space* home, bool share) {
00135     return new (home) SetVarImp(home,share,*this);
00136   }
00137 
00138   Reflection::Arg*
00139   SetVarImp::spec(const Space*, Reflection::VarMap& m) const {
00140     int specIndex = m.index(this);
00141     if (specIndex != -1)
00142       return Reflection::Arg::newVar(specIndex);
00143 
00144     int glbsize = 0;
00145     {
00146       BndSetRanges lrs(glb);
00147       while (lrs()) { glbsize++; ++lrs; }
00148     }
00149     
00150     Reflection::IntArrayArg* glbdom = Reflection::Arg::newIntArray(glbsize*2);
00151     BndSetRanges lr(glb);
00152     for (int count=0; lr(); ++lr) {
00153       (*glbdom)[count++] = lr.min();
00154       (*glbdom)[count++] = lr.max();
00155     }
00156 
00157     int lubsize = 0;
00158     {
00159       BndSetRanges urs(lub);
00160       while (urs()) { lubsize++; ++urs; }
00161     }
00162     
00163     BndSetRanges ur(lub);
00164     Reflection::IntArrayArg* lubdom = Reflection::Arg::newIntArray(lubsize*2);
00165     for (int count=0; ur(); ++ur) {
00166       (*lubdom)[count++] = ur.min();
00167       (*lubdom)[count++] = ur.max();
00168     }
00169 
00170     Reflection::Arg* pair =
00171       Reflection::Arg::newPair(
00172         Reflection::Arg::newPair(glbdom, Reflection::Arg::newInt(_cardMin)),
00173         Reflection::Arg::newPair(lubdom, Reflection::Arg::newInt(_cardMax)));
00174 
00175     Reflection::VarSpec* spec = new Reflection::VarSpec(vti, pair, 
00176                                                         assigned());
00177     return (Reflection::Arg::newVar(m.put(this, spec)));
00178   }
00179 
00180   VarImpBase*
00181   SetVarImp::create(Space* home, Reflection::VarSpec& spec) {
00182     int cardMin = spec.dom()->first()->second()->toInt();
00183     int cardMax = spec.dom()->second()->second()->toInt();
00184     Reflection::IntArrayArgRanges 
00185       glb(spec.dom()->first()->first()->toIntArray());
00186     Reflection::IntArrayArgRanges 
00187       lub(spec.dom()->second()->first()->toIntArray());
00188     return new (home) SetVarImp(home, IntSet(glb), IntSet(lub),
00189                                 cardMin, cardMax);
00190   }
00191 
00192   void
00193   SetVarImp::constrain(Space* home, VarImpBase* v,
00194                        Reflection::VarSpec& spec) {
00195     int cardMin = spec.dom()->first()->second()->toInt();
00196     int cardMax = spec.dom()->second()->second()->toInt();
00197     Reflection::IntArrayArgRanges 
00198       glb(spec.dom()->first()->first()->toIntArray());
00199     Reflection::IntArrayArgRanges 
00200       lub(spec.dom()->second()->first()->toIntArray());
00201     SetVarImp* s = static_cast<SetVarImp*>(v);
00202     GECODE_ME_FAIL(home, s->cardMin(home, cardMin));
00203     GECODE_ME_FAIL(home, s->cardMax(home, cardMax));
00204     GECODE_ME_FAIL(home, s->includeI(home, glb));
00205     GECODE_ME_FAIL(home, s->intersectI(home, lub));
00206   }
00207 
00208   namespace {
00209     Reflection::VarImpRegistrar<SetVarImp> registerSetVarImp;
00210   }
00211 
00212 }}
00213 
00214 // STATISTICS: set-var
00215