/usr/include/gecode/int/distinct/bnd.hpp is in libgecode-dev 4.4.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2003
*
* Last modified:
* $Date: 2013-11-15 14:53:16 +0100 (Fri, 15 Nov 2013) $ by $Author: schulte $
* $Revision: 14077 $
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Gecode { namespace Int { namespace Distinct {
template<class View>
forceinline
Bnd<View>::Bnd(Home home, ViewArray<View>& x0)
: Propagator(home), x(x0), y(home,x0) {
// Both x and y initially contain the same variables
// - x is used for bounds propagation
// - y is used for performing singleton propagation
// They can not be shared as singleton propagation removes
// determined variables still required for bounds propagation.
y.subscribe(home,*this,PC_INT_BND);
int min = x[x.size()-1].min(), max = x[x.size()-1].max();
for (int i=x.size()-1; i--; ) {
min = std::min(min,x[i].min());
max = std::max(max,x[i].max());
}
min_x = min; max_x = max;
}
template<class View>
forceinline size_t
Bnd<View>::dispose(Space& home) {
y.cancel(home,*this,PC_INT_BND);
(void) Propagator::dispose(home);
return sizeof(*this);
}
template<class View>
forceinline
Bnd<View>::Bnd(Space& home, bool share, Bnd<View>& p)
: Propagator(home,share,p), min_x(p.min_x), max_x(p.max_x) {
x.update(home,share,p.x);
y.update(home,share,p.y);
}
template<class View>
Actor*
Bnd<View>::copy(Space& home, bool share) {
return new (home) Bnd<View>(home,share,*this);
}
template<class View>
PropCost
Bnd<View>::cost(const Space&, const ModEventDelta& med) const {
if (View::me(med) == ME_INT_VAL)
return PropCost::linear(PropCost::LO, y.size());
else
return PropCost::quadratic(PropCost::LO, x.size());
}
/// %Rank information
class Rank {
public:
int min, max;
};
/// Sort-order by increasing maximum (by index)
template<class View>
class MaxIncIdx {
protected:
ViewArray<View> x;
public:
forceinline
MaxIncIdx(const ViewArray<View>& x0) : x(x0) {}
forceinline bool
operator ()(const int i, const int j) {
return x[i].max() < x[j].max();
}
};
/// Sort-order by increasing minimum (by index)
template<class View>
class MinIncIdx {
protected:
ViewArray<View> x;
public:
forceinline
MinIncIdx(const ViewArray<View>& x0) : x(x0) {}
forceinline bool
operator ()(const int i, const int j) {
return x[i].min() < x[j].min();
}
};
/// Sort-order by increasing minimum (direct)
template<class View>
class MinInc {
public:
forceinline bool
operator ()(const View& x, const View& y) {
return x.min() < y.min();
}
};
/// Information on Hall intervals
template<class IntType>
class HallInfo {
public:
IntType bounds, t, d, h;
};
template<class IntType>
forceinline void
pathset_t(HallInfo<IntType> hall[],
IntType start, IntType end, IntType to) {
IntType k, l;
for (l=start; (k=l) != end; hall[k].t=to) {
l = hall[k].t;
}
}
template<class IntType>
forceinline void
pathset_h(HallInfo<IntType> hall[],
IntType start, IntType end, IntType to) {
IntType k, l;
for (l=start; (k=l) != end; hall[k].h=to) {
l = hall[k].h;
}
}
template<class IntType>
forceinline IntType
pathmin_h(const HallInfo<IntType> hall[], IntType i) {
while (hall[i].h < i)
i = hall[i].h;
return i;
}
template<class IntType>
forceinline IntType
pathmin_t(const HallInfo<IntType> hall[], IntType i) {
while (hall[i].t < i)
i = hall[i].t;
return i;
}
template<class IntType>
forceinline IntType
pathmax_h(const HallInfo<IntType> hall[], IntType i) {
while (hall[i].h > i)
i = hall[i].h;
return i;
}
template<class IntType>
forceinline IntType
pathmax_t(const HallInfo<IntType> hall[], IntType i) {
while (hall[i].t > i)
i = hall[i].t;
return i;
}
template<class View, class IntType>
forceinline ExecStatus
prop_bnd(Space& home, ViewArray<View>& x,
int* minsorted, int* maxsorted) {
const int n = x.size();
Region r(home);
// Setup rank and bounds info
HallInfo<IntType>* hall = r.alloc<HallInfo<IntType> >(2*n+2);
Rank* rank = r.alloc<Rank>(n);
int nb = 0;
{
IntType min = x[minsorted[0]].min();
IntType max = x[maxsorted[0]].max() + 1;
IntType last = min - 2;
hall[0].bounds = last;
int i = 0;
int j = 0;
while (true) {
if ((i < n) && (min < max)) {
if (min != last)
hall[++nb].bounds = last = min;
rank[minsorted[i]].min = nb;
if (++i < n)
min = x[minsorted[i]].min();
} else {
if (max != last)
hall[++nb].bounds = last = max;
rank[maxsorted[j]].max = nb;
if (++j == n)
break;
max = x[maxsorted[j]].max() + 1;
}
}
hall[nb+1].bounds = hall[nb].bounds + 2;
}
// If tells cross holes, we do not compute a fixpoint
ExecStatus es = ES_FIX;
// Propagate lower bounds
for (int i=nb+2; --i;) {
hall[i].t = hall[i].h = i-1;
hall[i].d = hall[i].bounds - hall[i-1].bounds;
}
for (int i=0; i<n; i++) { // visit intervals in increasing max order
IntType x0 = rank[maxsorted[i]].min;
IntType z = pathmax_t(hall, x0+1);
IntType j = hall[z].t;
if (--hall[z].d == 0)
hall[z = pathmax_t(hall, hall[z].t=z+1)].t = j;
pathset_t(hall, x0+1, z, z); // path compression
IntType y = rank[maxsorted[i]].max;
if (hall[z].d < hall[z].bounds-hall[y].bounds)
return ES_FAILED;
if (hall[x0].h > x0) {
IntType w = pathmax_h(hall, hall[x0].h);
IntType m = hall[w].bounds;
ModEvent me = x[maxsorted[i]].gq(home,m);
if (me_failed(me))
return ES_FAILED;
if ((me == ME_INT_VAL) ||
((me == ME_INT_BND) && (m != x[maxsorted[i]].min())))
es = ES_NOFIX;
pathset_h(hall, x0, w, w); // path compression
}
if (hall[z].d == hall[z].bounds-hall[y].bounds) {
pathset_h(hall, hall[y].h, j-1, y); // mark hall interval
hall[y].h = j-1;
}
}
// Propagate upper bounds
for (int i=nb+1; i--;) {
hall[i].t = hall[i].h = i+1;
hall[i].d = hall[i+1].bounds - hall[i].bounds;
}
for (int i=n; --i>=0; ) { // visit intervals in decreasing min order
IntType x0 = rank[minsorted[i]].max;
IntType z = pathmin_t(hall, x0-1);
IntType j = hall[z].t;
if (--hall[z].d == 0)
hall[z = pathmin_t(hall, hall[z].t=z-1)].t = j;
pathset_t(hall, x0-1, z, z);
IntType y = rank[minsorted[i]].min;
if (hall[z].d < hall[y].bounds-hall[z].bounds)
return ES_FAILED;
if (hall[x0].h < x0) {
IntType w = pathmin_h(hall, hall[x0].h);
IntType m = hall[w].bounds - 1;
ModEvent me = x[minsorted[i]].lq(home,m);
if (me_failed(me))
return ES_FAILED;
if ((me == ME_INT_VAL) ||
((me == ME_INT_BND) && (m != x[maxsorted[i]].min())))
es = ES_NOFIX;
pathset_h(hall, x0, w, w);
}
if (hall[z].d == hall[y].bounds-hall[z].bounds) {
pathset_h(hall, hall[y].h, j+1, y);
hall[y].h = j+1;
}
}
return es;
}
template<class View>
forceinline ExecStatus
prop_bnd(Space& home, ViewArray<View>& x, int& min_x, int& max_x) {
const int n = x.size();
Region r(home);
int* minsorted = r.alloc<int>(n);
int* maxsorted = r.alloc<int>(n);
unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
if (d < static_cast<unsigned int>(n))
return ES_FAILED;
if (d > 2*static_cast<unsigned int>(n)) {
for (int i = n; i--; )
minsorted[i]=maxsorted[i]=i;
MinIncIdx<View> min_inc(x);
Support::quicksort<int,MinIncIdx<View> >(minsorted, n, min_inc);
MaxIncIdx<View> max_inc(x);
Support::quicksort<int,MaxIncIdx<View> >(maxsorted, n, max_inc);
} else {
int* minbucket = r.alloc<int>(d);
int* maxbucket = r.alloc<int>(d);
for (unsigned int i=d; i--; )
minbucket[i]=maxbucket[i]=0;
for (int i=n; i--; ) {
minbucket[x[i].min() - min_x]++;
maxbucket[x[i].max() - min_x]++;
}
int c_min = 0, c_max = 0;
for (unsigned int i=0; i<d; i++) {
int t_min = minbucket[i];
int t_max = maxbucket[i];
minbucket[i] = c_min; c_min += t_min;
maxbucket[i] = c_max; c_max += t_max;
}
assert((c_min == n) && (c_max == n));
for (int i=n; i--; ) {
minsorted[minbucket[x[i].min() - min_x]++] = i;
maxsorted[maxbucket[x[i].max() - min_x]++] = i;
}
}
// Update minimum and maximum information
min_x = x[minsorted[0]].min();
max_x = x[maxsorted[n-1]].max();
if (d > (static_cast<unsigned int>(Int::Limits::max) / 2U))
return prop_bnd<View,long long int>(home,x,minsorted,maxsorted);
else
return prop_bnd<View,int>(home,x,minsorted,maxsorted);
}
template<class View>
ExecStatus
prop_bnd(Space& home, ViewArray<View>& x) {
int min = x[x.size()-1].min(), max = x[x.size()-1].max();
for (int i=x.size()-1; i--; ) {
min = std::min(min,x[i].min());
max = std::max(max,x[i].max());
}
return prop_bnd(home, x, min, max);
}
template<class View>
ExecStatus
Bnd<View>::propagate(Space& home, const ModEventDelta& med) {
assert(x.size() > 1);
if (View::me(med) == ME_INT_VAL) {
ExecStatus es = prop_val<View,false>(home,y);
GECODE_ES_CHECK(es);
if (y.size() < 2)
return home.ES_SUBSUMED(*this);
if (es == ES_FIX)
return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_BND));
}
if (y.size() == 2)
GECODE_REWRITE(*this,Rel::Nq<View>::post(home(*this),y[0],y[1]));
ExecStatus es = prop_bnd<View>(home,x,min_x,max_x);
GECODE_ES_CHECK(es);
if (es == ES_NOFIX && View::me(modeventdelta()) == ME_INT_VAL)
return es;
const int n = x.size();
if ((n > 2*y.size()) && (n > 6)) {
// If there are many assigned views, try to eliminate them
unsigned int d = static_cast<unsigned int>(max_x - min_x) + 1;
if (d > 2*static_cast<unsigned int>(n)) {
MinInc<View> min_inc;
Support::quicksort<View,MinInc<View> >(&x[0], n, min_inc);
} else {
Region r(home);
int* minbucket = r.alloc<int>(d);
View* minsorted = r.alloc<View>(n);
for (unsigned int i=d; i--; )
minbucket[i]=0;
for (int i=n; i--; )
minbucket[x[i].min() - min_x]++;
int c_min = 0;
for (unsigned int i=0; i<d; i++) {
int t_min = minbucket[i];
minbucket[i] = c_min; c_min += t_min;
}
assert(c_min == n);
for (int i=n; i--;)
minsorted[minbucket[x[i].min() - min_x]++] = x[i];
for (int i=n; i--;)
x[i] = minsorted[i];
}
int i = 0;
int j = 0;
int max = x[0].max()-1;
while (i < n-1) {
if (!x[i].assigned() ||
(x[i].val() <= max) || (x[i].val() > x[i+1].min())) {
// Keep view x[i]
max = std::max(max,x[i].max());
x[j++]=x[i];
}
i++;
}
if (!x[i].assigned() || (x[i].val() <= max))
x[j++]=x[i];
x.size(j);
}
if (x.size() < 2)
return home.ES_SUBSUMED(*this);
if (x.size() == 2)
GECODE_REWRITE(*this,Rel::Nq<View>::post(home(*this),x[0],x[1]));
return es;
}
template<class View>
ExecStatus
Bnd<View>::post(Home home, ViewArray<View>& x){
if (x.size() == 2)
return Rel::Nq<View>::post(home,x[0],x[1]);
if (x.size() > 2)
(void) new (home) Bnd<View>(home,x);
return ES_OK;
}
}}}
// STATISTICS: int-prop
|