~ubuntu-branches/ubuntu/natty/gecode/natty

« back to all changes in this revision

Viewing changes to test/set/select.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2005-12-24 07:51:25 UTC
  • Revision ID: james.westby@ubuntu.com-20051224075125-klkiqofvbfvusfvt
Tags: upstream-1.0.0.dfsg.1
ImportĀ upstreamĀ versionĀ 1.0.0.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Main authors:
 
3
 *     Guido Tack <tack@gecode.org>
 
4
 *
 
5
 *  Copyright:
 
6
 *     Guido Tack, 2005
 
7
 *
 
8
 *  Last modified:
 
9
 *     $Date: 2005-11-01 16:01:21 +0100 (Tue, 01 Nov 2005) $ by $Author: zayenz $
 
10
 *     $Revision: 2465 $
 
11
 *
 
12
 *  This file is part of Gecode, the generic constraint
 
13
 *  development environment:
 
14
 *     http://www.gecode.org
 
15
 *
 
16
 *  See the file "LICENSE" for information on usage and
 
17
 *  redistribution of this file, and for a
 
18
 *     DISCLAIMER OF ALL WARRANTIES.
 
19
 *
 
20
 */
 
21
 
 
22
#include "test/set.hh"
 
23
 
 
24
static IntSet ds_012(-1,2);
 
25
 
 
26
class SelectUnion : public SetTest {
 
27
public:
 
28
  SelectUnion(const char* t) 
 
29
    : SetTest(t,5,ds_012,false) {}
 
30
  virtual bool solution(const SetAssignment& x) const {
 
31
    int selected = 0;
 
32
    for (CountableSetValues sel2(x.lub, x[3]); sel2(); ++sel2, selected++);
 
33
    CountableSetValues x4v(x.lub, x[4]);
 
34
    if (selected==0)
 
35
      return !x4v();
 
36
    GECODE_AUTOARRAY(CountableSetRanges, sel, selected);
 
37
    CountableSetValues selector(x.lub, x[3]);
 
38
    for (int i=selected; i--;++selector) {
 
39
      if (selector.val()>=3 || selector.val()<0)
 
40
        return false;
 
41
      sel[i].init(x.lub, x[selector.val()]);
 
42
    }
 
43
    Iter::Ranges::NaryUnion<CountableSetRanges> u(sel, selected);
 
44
 
 
45
    CountableSetRanges z(x.lub, x[4]);
 
46
    return Iter::Ranges::equal(u, z);
 
47
  }
 
48
  virtual void post(Space* home, SetVarArray& x, IntVarArray&) {
 
49
    SetVarArgs xs(x.size()-2);
 
50
    for (int i=x.size()-2; i--;)
 
51
      xs[i]=x[i];
 
52
    Gecode::selectUnion(home, xs, x[x.size()-2], x[x.size()-1]);
 
53
  }
 
54
};
 
55
SelectUnion _selectunion("Select::Union");
 
56
 
 
57
class SelectInter : public SetTest {
 
58
public:
 
59
  SelectInter(const char* t) 
 
60
    : SetTest(t,5,ds_012,false) {}
 
61
  virtual bool solution(const SetAssignment& x) const {
 
62
    int selected = 0;
 
63
    for (CountableSetValues sel2(x.lub, x[3]); sel2(); ++sel2, selected++);
 
64
    CountableSetRanges x4r(x.lub, x[4]);
 
65
    if (selected==0)
 
66
      return Iter::Ranges::size(x4r)==Limits::Set::card_max;
 
67
    GECODE_AUTOARRAY(CountableSetRanges, sel, selected);
 
68
    CountableSetValues selector(x.lub, x[3]);
 
69
    for (int i=selected; i--;++selector) {
 
70
      if (selector.val()>=3 || selector.val()<0)
 
71
        return false;
 
72
      sel[i].init(x.lub, x[selector.val()]);
 
73
    }
 
74
    Iter::Ranges::NaryInter<CountableSetRanges> u(sel, selected);
 
75
 
 
76
    CountableSetRanges z(x.lub, x[4]);
 
77
    return Iter::Ranges::equal(u, z);
 
78
  }
 
79
  virtual void post(Space* home, SetVarArray& x, IntVarArray&) {
 
80
    SetVarArgs xs(x.size()-2);
 
81
    for (int i=x.size()-2; i--;)
 
82
      xs[i]=x[i];
 
83
    Gecode::selectInter(home, xs, x[x.size()-2], x[x.size()-1]);
 
84
  }
 
85
};
 
86
SelectInter _selectinter("Select::Inter");
 
87
 
 
88
class SelectSets : public SetTest {
 
89
public:
 
90
  SelectSets(const char* t) 
 
91
    : SetTest(t,4,ds_012,false,true) {}
 
92
  virtual bool solution(const SetAssignment& x) const {
 
93
    if (x.intval() < 0 || x.intval() > 2)
 
94
      return false;
 
95
    CountableSetRanges z(x.lub, x[3]);
 
96
    CountableSetRanges y(x.lub, x[x.intval()]);
 
97
    return Iter::Ranges::equal(y, z);
 
98
  }
 
99
  virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
 
100
    SetVarArgs xs(x.size()-1);
 
101
    for (int i=x.size()-1; i--;)
 
102
      xs[i]=x[i];
 
103
    Gecode::selectSets(home, xs, y[0], x[x.size()-1]);
 
104
  }
 
105
};
 
106
SelectSets _selectsets("Select::Sets");
 
107
 
 
108
// STATISTICS: test-set