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 namespace Gecode { namespace Int { namespace NoOverlap {
00039
00040 template<class Dim, int d>
00041 forceinline
00042 OptProp<Dim,d>::OptProp(Home home, OptBox<Dim,d>* b, int n, int m0)
00043 : Base<OptBox<Dim,d> >(home,b,n), m(m0) {
00044 for (int i=m; i--; )
00045 b[n+i].subscribe(home, *this);
00046 }
00047
00048 template<class Dim, int d>
00049 ExecStatus
00050 OptProp<Dim,d>::post(Home home, OptBox<Dim,d>* b, int n) {
00051
00052 if (n > 1) {
00053 int p = Base<OptBox<Dim,d> >::partition(b, 0, n);
00054 (void) new (home) OptProp<Dim,d>(home,b,p,n-p);
00055 }
00056 return ES_OK;
00057 }
00058
00059 template<class Dim, int d>
00060 forceinline size_t
00061 OptProp<Dim,d>::dispose(Space& home) {
00062 for (int i=m; i--; )
00063 b[n+i].cancel(home, *this);
00064 (void) Base<OptBox<Dim,d> >::dispose(home);
00065 return sizeof(*this);
00066 }
00067
00068
00069 template<class Dim, int d>
00070 forceinline
00071 OptProp<Dim,d>::OptProp(Space& home, bool shared, OptProp<Dim,d>& p)
00072 : Base<OptBox<Dim,d> >(home, shared, p, p.n + p.m), m(p.m) {}
00073
00074 template<class Dim, int d>
00075 Actor*
00076 OptProp<Dim,d>::copy(Space& home, bool share) {
00077 return new (home) OptProp<Dim,d>(home,share,*this);
00078 }
00079
00080 template<class Dim, int d>
00081 ExecStatus
00082 OptProp<Dim,d>::propagate(Space& home, const ModEventDelta& med) {
00083 Region r(home);
00084
00085 if (BoolView::me(med) == ME_BOOL_VAL) {
00086
00087 for (int i=m; i--; )
00088 if (b[n+i].excluded()) {
00089 b[n+i].cancel(home,*this);
00090 b[n+i] = b[n+(--m)];
00091 }
00092
00093 if (m > 0) {
00094 int p = Base<OptBox<Dim,d> >::partition(b+n, 0, m);
00095 n += p; m -= p;
00096 }
00097 }
00098
00099
00100 int* db = r.alloc<int>(n);
00101 for (int i=n; i--; )
00102 db[i] = n-1;
00103
00104
00105 int e = 0;
00106 for (int i=n; i--; ) {
00107 assert(b[i].mandatory());
00108 for (int j=i; j--; )
00109 if (b[i].nooverlap(b[j])) {
00110 assert(db[i] > 0); assert(db[j] > 0);
00111 if (--db[i] == 0) e++;
00112 if (--db[j] == 0) e++;
00113 continue;
00114 } else {
00115 GECODE_ES_CHECK(b[i].nooverlap(home,b[j]));
00116 }
00117 }
00118
00119 if (m == 0) {
00120 if (e == n)
00121 return home.ES_SUBSUMED(*this);
00122 int i = n-1;
00123 while (e > 0) {
00124
00125 while (db[i] > 0)
00126 i--;
00127 b[i].cancel(home, *this);
00128 b[i] = b[--n]; b[n] = b[n+m];
00129 e--; i--;
00130 }
00131 if (n < 2)
00132 return home.ES_SUBSUMED(*this);
00133 }
00134
00135
00136 for (int i=m; i--; ) {
00137 assert(b[n+i].optional());
00138 for (int j=n; j--; )
00139 if (b[n+i].overlap(b[j])) {
00140 GECODE_ES_CHECK(b[n+i].exclude(home));
00141 b[n+i].cancel(home,*this);
00142 b[n+i] = b[n+(--m)];
00143 break;
00144 }
00145 }
00146
00147 return ES_NOFIX;
00148 }
00149
00150 }}}
00151
00152
00153