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

set.hh

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, 2005
00009  *     Christian Schulte, 2005
00010  *
00011  *  Last modified:
00012  *     $Date: 2008-02-20 10:27:10 +0100 (Wed, 20 Feb 2008) $ by $Author: tack $
00013  *     $Revision: 6241 $
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 #ifndef __GECODE_TEST_SET_HH__
00041 #define __GECODE_TEST_SET_HH__
00042 
00043 #include "gecode/set.hh"
00044 #include "test/test.hh"
00045 #include "test/int.hh"
00046 
00047 namespace Test {
00048   
00050   namespace Set {
00051 
00062 
00064     class CountableSetValues {
00065     private:
00066       Gecode::IntSetValues dv;
00067       int cur;
00068       int i;
00069     public:
00071       CountableSetValues(void) {}
00073       CountableSetValues(const Gecode::IntSet& d0, int cur0)
00074       : dv(d0), cur(cur0), i(1) {
00075         if (! (i & cur))
00076           operator++();
00077       }
00079       void init(const Gecode::IntSet& d0, int cur0) {
00080         dv = d0;
00081         cur = cur0;
00082         i = 1;
00083         if (! (i & cur))
00084           operator++();
00085       }
00087       bool operator()(void) const {
00088         return i<=cur;
00089       }
00091       void operator++(void) {
00092         do {
00093           ++dv;
00094           i = i<<1;
00095         } while (! (i & cur) && i<cur);
00096       }
00098       int val(void) const { return dv.val(); }
00099     };
00100 
00102     class CountableSetRanges
00103     : public Gecode::Iter::Values::ToRanges<CountableSetValues> {
00104     private:
00106       CountableSetValues v;
00107     public:
00109       CountableSetRanges(void) {}
00111       CountableSetRanges(const Gecode::IntSet& d, int cur) : v(d, cur) {
00112         Gecode::Iter::Values::ToRanges<CountableSetValues>::init(v);
00113       }
00115       void init(const Gecode::IntSet& d, int cur) {
00116         v.init(d, cur);
00117         Gecode::Iter::Values::ToRanges<CountableSetValues>::init(v);
00118       }
00119     };
00120 
00122     class CountableSet {
00123     private:
00125       Gecode::IntSet d;
00127       unsigned int cur;
00129       unsigned int lubmax;
00130     public:
00132       CountableSet(const Gecode::IntSet& s);
00134       CountableSet(void) {}
00136       void init(const Gecode::IntSet& s);
00138       bool operator()(void) const { return cur<lubmax; }
00140       void operator++(void);
00142       int val(void) const;
00143     };
00144 
00146     class SetAssignment {
00147     private:
00149       int n;
00151       CountableSet* dsv;
00153       Test::Int::CpltAssignment ir;
00155       bool done;
00156     public:
00158       Gecode::IntSet lub;
00160       int withInt;
00162       SetAssignment(int n, const Gecode::IntSet& d, int i = 0);
00164       bool operator()(void) const { return !done; }
00166       void operator++(void);
00168       int operator[](int i) const {
00169         assert((i>=0) && (i<n));
00170         return dsv[i].val();
00171       }
00173       int intval(void) const { return ir[0]; }
00175       const Test::Int::Assignment& ints(void) const { return ir; }
00177       int size(void) const { return n; }
00179       ~SetAssignment(void) { delete [] dsv; }
00180     };
00181 
00186     class SetTest : public Base {
00187     private:
00189       int     arity;
00191       Gecode::IntSet  lub;
00193       bool    reified;
00195       int    withInt;
00196 
00198       void removeFromLub(int v, Gecode::SetVar& x, int i,
00199                          const Gecode::IntSet& a);
00201       void addToGlb(int v, Gecode::SetVar& x, int i, const Gecode::IntSet& a);
00202       SetAssignment* make_assignment(void);
00203     public:
00211       SetTest(const std::string& s,
00212               int a, const Gecode::IntSet& d, bool r=false, int w=0)
00213         : Base("Set::"+s), arity(a), lub(d), reified(r), withInt(w)  {}
00215       virtual bool solution(const SetAssignment&) const = 0;
00217       virtual void post(Gecode::Space* home, Gecode::SetVarArray& x, 
00218                         Gecode::IntVarArray& y) = 0;
00220       virtual void post(Gecode::Space*, Gecode::SetVarArray&,
00221                         Gecode::IntVarArray&, Gecode::BoolVar) {}
00223       virtual bool run(void);
00224 
00226 
00227 
00228       static std::string str(Gecode::SetRelType srt);
00230       static std::string str(Gecode::SetOpType srt);
00232       static std::string str(int i);
00234     };
00236 
00238     class SetRelTypes {
00239     private:
00241       static const Gecode::SetRelType srts[6];
00243       int i; 
00244     public:
00246       SetRelTypes(void);
00248       bool operator()(void) const;
00250       void operator++(void);
00252       Gecode::SetRelType srt(void) const;
00253     };
00254     
00256     class SetOpTypes {
00257     private:
00259       static const Gecode::SetOpType sots[4];
00261       int i; 
00262     public:
00264       SetOpTypes(void);
00266       bool operator()(void) const;
00268       void operator++(void);
00270       Gecode::SetOpType sot(void) const;
00271     };
00272 
00273 }}
00274 
00279 std::ostream&
00280 operator<<(std::ostream&, const Test::Set::SetAssignment& a);
00281 
00282 #include "test/set.icc"
00283 
00284 #endif
00285 
00286 // STATISTICS: test-set