00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "test/int.hh"
00039
00040 #include <gecode/minimodel.hh>
00041
00042 namespace Test { namespace Int {
00043
00045 namespace Exec {
00046
00052
00053 class IntWait : public Test {
00054 protected:
00056 bool sf;
00057 public:
00059 IntWait(int n, bool sf0)
00060 : Test("Wait::Int::"+str(n)+"::"+
00061 (sf0 ? "std::function" : "funptr"),n,0,n,false), sf(sf0) {}
00063 virtual bool solution(const Assignment& x) const {
00064 for (int i=0; i<x.size(); i++)
00065 for (int j=i+1; j<x.size(); j++)
00066 if (x[i] == x[j])
00067 return false;
00068 return true;
00069 }
00071 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00072 using namespace Gecode;
00073 auto f = static_cast<std::function<void(Space&)>>
00074 ([](Space& home) { c(home); });
00075 if (x.size() > 1) {
00076 if (sf)
00077 Gecode::wait(home, x, f);
00078 else
00079 Gecode::wait(home, x, &c);
00080 } else {
00081 if (sf)
00082 Gecode::wait(home, x[0], f);
00083 else
00084 Gecode::wait(home, x[0], &c);
00085 }
00086 }
00088 static void c(Gecode::Space& _home) {
00089 TestSpace& home = static_cast<TestSpace&>(_home);
00090 for (int i=0; i<home.x.size(); i++)
00091 for (int j=i+1; j<home.x.size(); j++)
00092 if (home.x[i].val() == home.x[j].val())
00093 home.fail();
00094 }
00095 };
00096
00098 class BoolWait : public Test {
00099 protected:
00101 bool sf;
00102 public:
00104 BoolWait(int n, bool sf0)
00105 : Test("Wait::Bool::"+str(n)+"::"+
00106 (sf0 ? "std::function" : "funptr"),n,0,1,false), sf(sf0) {}
00108 virtual bool solution(const Assignment& x) const {
00109 int t=0;
00110 for (int i=0; i<x.size(); i++)
00111 t += x[i];
00112 return t==2;
00113 }
00115 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00116 using namespace Gecode;
00117 BoolVarArgs b(x.size());
00118 for (int i=b.size(); i--; )
00119 b[i]=channel(home,x[i]);
00120 auto f = static_cast<std::function<void(Space&)>>
00121 ([](Space& home) { c(home); });
00122 if (b.size() > 1) {
00123 if (sf)
00124 Gecode::wait(home, b, f);
00125 else
00126 Gecode::wait(home, b, &c);
00127 } else {
00128 if (sf)
00129 Gecode::wait(home, b[0], f);
00130 else
00131 Gecode::wait(home, b[0], &c);
00132 }
00133 }
00135 static void c(Gecode::Space& _home) {
00136 TestSpace& home = static_cast<TestSpace&>(_home);
00137 int t=0;
00138 for (int i=0; i<home.x.size(); i++)
00139 t += home.x[i].val();
00140 if (t!=2)
00141 home.fail();
00142 }
00143 };
00144
00146 class When : public Test {
00147 protected:
00149 bool sf;
00150 public:
00152 When(bool sf0)
00153 : Test(std::string("When::")+
00154 (sf0 ? "std::function" : "funptr"),1,0,1,false), sf(sf0) {}
00156 virtual bool solution(const Assignment& x) const {
00157 return x[0]==0;
00158 }
00160 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00161 using namespace Gecode;
00162 if (sf) {
00163 auto sft = static_cast<std::function<void(Space&)>>
00164 ([](Space& home) { t(home); });
00165 auto sfe = static_cast<std::function<void(Space&)>>
00166 ([](Space& home) { e(home); });
00167 when(home, channel(home, x[0]), sft, sfe);
00168 } else {
00169 when(home, channel(home, x[0]), &t, &e);
00170 }
00171 }
00173 static void t(Gecode::Space& home) {
00174 home.fail();
00175 }
00177 static void e(Gecode::Space& home) {
00178 (void) home;
00179 }
00180 };
00181
00182 IntWait iw1t(1,true), iw2t(2,true), iw3t(3,true), iw4t(4,true);
00183 IntWait iw1f(1,false), iw2f(2,false), iw3f(3,false), iw4f(4,false);
00184 BoolWait bw1t(1,true), bw2t(2,true), bw3t(3,true), bw4t(4,true);
00185 BoolWait bw1f(1,false), bw2f(2,false), bw3f(3,false), bw4f(4,false);
00186
00187
00188 When whent(true);
00189 When whenf(false);
00191
00192 }
00193
00194 }}
00195
00196