sorted.cpp
Go to the documentation of this file.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 namespace Test { namespace Int {
00041
00043 namespace Sorted {
00044
00050
00052 class SortIntMin {
00053 public:
00055 bool operator()(const int x, const int y) {
00056 return x<y;
00057 }
00058 };
00059
00061 class NoVar : public Test {
00062 protected:
00064 static const int n = 3;
00065 public:
00067 NoVar(void) : Test("Sorted::NoVar",2*n,0,3) {}
00069 virtual bool solution(const Assignment& xy) const {
00070 int x[n], y[n];
00071 for (int i=0;i<3; i++) {
00072 x[i]=xy[i]; y[i]=xy[n+i];
00073 }
00074
00075 for (int i=0; i<n-1; i++)
00076 if (y[i]>y[i+1])
00077 return false;
00078
00079 SortIntMin sim;
00080 Gecode::Support::quicksort<int,SortIntMin>(x,n,sim);
00081
00082 for (int i=0; i<n; i++)
00083 if (x[i] != y[i])
00084 return false;
00085 return true;
00086 }
00088 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00089 Gecode::IntVarArgs x(n), y(n);
00090 for (int i=0; i<n; i++) {
00091 x[i]=xy[i]; y[i]=xy[n+i];
00092 }
00093 Gecode::sorted(home,x,y);
00094 }
00095 };
00096
00097
00099 class PermVar : public Test {
00100 protected:
00102 static const int n = 3;
00103 public:
00105 PermVar(void) : Test("Sorted::PermVar",3*n,0,2) {}
00107 virtual bool solution(const Assignment& xyz) const {
00108 int x[n], y[n], z[n];
00109 for (int i=0; i<n; i++) {
00110 x[i]=xyz[i]; y[i]=xyz[n+i]; z[i]=xyz[2*n+i];
00111 }
00112
00113 for (int i=0; i<n; i++)
00114 for (int j=i+1; j<n; j++)
00115 if (z[i]==z[j])
00116 return false;
00117
00118
00119 for (int i=0; i<n-1; i++)
00120 if (y[i]>y[i+1])
00121 return false;
00122
00123
00124 for (int i=0; i<n; i++)
00125 if ((z[i] < 0) || (z[i] >= n))
00126 return false;
00127
00128
00129 for (int i=0; i<n; i++)
00130 if (x[i] != y[z[i]])
00131 return false;
00132
00133
00134 SortIntMin sim;
00135 Gecode::Support::quicksort<int,SortIntMin>(x,n,sim);
00136 for (int i=0; i<n; i++)
00137 if (x[i] != y[i])
00138 return false;
00139
00140 return true;
00141 }
00143 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xyz) {
00144 Gecode::IntVarArgs x(n), y(n), z(n);
00145 for (int i=0; i<n; i++) {
00146 x[i]=xyz[i]; y[i]=xyz[n+i]; z[i]=xyz[2*n+i];
00147 }
00148 Gecode::sorted(home,x,y,z);
00149 }
00150 };
00151
00152
00153 NoVar novar;
00154 PermVar permvar;
00156
00157 }
00158 }}
00159
00160
00161