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
00039
00040
00041
00042 #include <gecode/int/sequence.hh>
00043
00044 #include <algorithm>
00045
00046 namespace Gecode {
00047
00048 using namespace Int;
00049
00050 void
00051 sequence(Home home, const IntVarArgs& x, const IntSet &s,
00052 int q, int l, int u, IntPropLevel) {
00053 Limits::check(s.min(),"Int::sequence");
00054 Limits::check(s.max(),"Int::sequence");
00055
00056 if (x.size() == 0)
00057 throw TooFewArguments("Int::sequence");
00058
00059 Limits::check(q,"Int::sequence");
00060 Limits::check(l,"Int::sequence");
00061 Limits::check(u,"Int::sequence");
00062
00063 if (x.same(home))
00064 throw ArgumentSame("Int::sequence");
00065
00066 if ((q < 1) || (q > x.size()))
00067 throw OutOfLimits("Int::sequence");
00068
00069 GECODE_POST;
00070
00071
00072 l=std::max(0,l); u=std::min(q,u);
00073
00074
00075 if (u < l) {
00076 home.fail(); return;
00077 }
00078
00079
00080 if ((0 == l) && (q == u))
00081 return;
00082
00083
00084 if (l == q) {
00085 for (int i=x.size(); i--; ) {
00086 IntView xv(x[i]);
00087 IntSetRanges ris(s);
00088 GECODE_ME_FAIL(xv.inter_r(home,ris,false));
00089 }
00090 return;
00091 }
00092
00093
00094 if (0 == u) {
00095 for (int i=x.size(); i--; ) {
00096 IntView xv(x[i]);
00097 IntSetRanges ris(s);
00098 GECODE_ME_FAIL(xv.minus_r(home,ris,false));
00099 }
00100 return;
00101 }
00102
00103 ViewArray<IntView> xv(home,x);
00104 if (s.size() == 1) {
00105 GECODE_ES_FAIL(
00106 (Sequence::Sequence<IntView,int>::post
00107 (home,xv,s.min(),q,l,u)));
00108 } else {
00109 GECODE_ES_FAIL(
00110 (Sequence::Sequence<IntView,IntSet>::post
00111 (home,xv,s,q,l,u)));
00112 }
00113 }
00114
00115 void
00116 sequence(Home home, const BoolVarArgs& x, const IntSet& s,
00117 int q, int l, int u, IntPropLevel) {
00118 if ((s.min() < 0) || (s.max() > 1))
00119 throw NotZeroOne("Int::sequence");
00120
00121 if (x.size() == 0)
00122 throw TooFewArguments("Int::sequence");
00123
00124 Limits::check(q,"Int::sequence");
00125 Limits::check(l,"Int::sequence");
00126 Limits::check(u,"Int::sequence");
00127
00128 if (x.same(home))
00129 throw ArgumentSame("Int::sequence");
00130
00131 if ((q < 1) || (q > x.size()))
00132 throw OutOfLimits("Int::sequence");
00133
00134 GECODE_POST;
00135
00136
00137 l=std::max(0,l); u=std::min(q,u);
00138
00139
00140 if (u < l) {
00141 home.fail(); return;
00142 }
00143
00144
00145 if ((0 == l) && (q == u))
00146 return;
00147
00148
00149 if ((s.min() == 0) && (s.max() == 1)) {
00150 if ((l > 0) || (u < q))
00151 home.fail();
00152 return;
00153 }
00154 assert(s.min() == s.max());
00155
00156
00157 if (l == q) {
00158 if (s.min() == 0) {
00159 for (int i=x.size(); i--; ) {
00160 BoolView xv(x[i]); GECODE_ME_FAIL(xv.zero(home));
00161 }
00162 } else {
00163 assert(s.min() == 1);
00164 for (int i=x.size(); i--; ) {
00165 BoolView xv(x[i]); GECODE_ME_FAIL(xv.one(home));
00166 }
00167 }
00168 return;
00169 }
00170
00171
00172 if (0 == u) {
00173 if (s.min() == 0) {
00174 for (int i=x.size(); i--; ) {
00175 BoolView xv(x[i]); GECODE_ME_FAIL(xv.one(home));
00176 }
00177 } else {
00178 assert(s.min() == 1);
00179 for (int i=x.size(); i--; ) {
00180 BoolView xv(x[i]); GECODE_ME_FAIL(xv.zero(home));
00181 }
00182 }
00183 return;
00184 }
00185
00186 ViewArray<BoolView> xv(home,x);
00187
00188 GECODE_ES_FAIL(
00189 (Sequence::Sequence<BoolView,int>::post
00190 (home,xv,s.min(),q,l,u)));
00191 }
00192
00193 }
00194
00195