/usr/include/gecode/int/rel/lq-le.hpp is in libgecode-dev 5.1.0-2build1.
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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
* Gabor Szokoli <szokoli@gecode.org>
*
* Copyright:
* Christian Schulte, 2003
* Gabor Szokoli, 2003
*
* Last modified:
* $Date: 2017-02-22 03:37:18 +0100 (Wed, 22 Feb 2017) $ by $Author: schulte $
* $Revision: 15468 $
*
* 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 Rel {
/*
* Less or equal propagator
*
*/
template<class V0, class V1>
forceinline
Lq<V0,V1>::Lq(Home home, V0 x0, V1 x1)
: MixBinaryPropagator<V0,PC_INT_BND,V1,PC_INT_BND>(home,x0,x1) {}
template<class V0, class V1>
forceinline bool
Lq<V0,V1>::same(V0 x0, V1 x1) {
(void) x0; (void) x1;
return false;
}
template<>
forceinline bool
Lq<IntView,IntView>::same(IntView x0, IntView x1) {
return Gecode::same(x0,x1);
}
template<>
forceinline bool
Lq<BoolView,BoolView>::same(BoolView x0, BoolView x1) {
return Gecode::same(x0,x1);
}
template<class V0, class V1>
ExecStatus
Lq<V0,V1>::post(Home home, V0 x0, V1 x1) {
GECODE_ME_CHECK(x0.lq(home,x1.max()));
GECODE_ME_CHECK(x1.gq(home,x0.min()));
if (!same(x0,x1) && (x0.max() > x1.min()))
(void) new (home) Lq<V0,V1>(home,x0,x1);
return ES_OK;
}
template<class V0, class V1>
forceinline
Lq<V0,V1>::Lq(Space& home, bool share, Lq<V0,V1>& p)
: MixBinaryPropagator<V0,PC_INT_BND,V1,PC_INT_BND>(home,share,p) {}
template<class V0, class V1>
Actor*
Lq<V0,V1>::copy(Space& home, bool share) {
return new (home) Lq<V0,V1>(home,share,*this);
}
template<class V0, class V1>
ExecStatus
Lq<V0,V1>::propagate(Space& home, const ModEventDelta&) {
GECODE_ME_CHECK(x0.lq(home,x1.max()));
GECODE_ME_CHECK(x1.gq(home,x0.min()));
return (x0.max() <= x1.min()) ? home.ES_SUBSUMED(*this) : ES_FIX;
}
/*
* Less propagator
*
*/
template<class V0, class V1>
forceinline
Le<V0,V1>::Le(Home home, V0 x0, V1 x1)
: MixBinaryPropagator<V0,PC_INT_BND,V1,PC_INT_BND>(home,x0,x1) {}
template<class V0, class V1>
forceinline bool
Le<V0,V1>::same(V0 x0, V1 x1) {
(void) x0; (void) x1;
return false;
}
template<>
forceinline bool
Le<IntView,IntView>::same(IntView x0, IntView x1) {
return Gecode::same(x0,x1);
}
template<>
forceinline bool
Le<MinusView,MinusView>::same(MinusView x0, MinusView x1) {
return Gecode::same(x0,x1);
}
template<>
forceinline bool
Le<BoolView,BoolView>::same(BoolView x0, BoolView x1) {
return Gecode::same(x0,x1);
}
template<class V0, class V1>
ExecStatus
Le<V0,V1>::post(Home home, V0 x0, V1 x1) {
if (same(x0,x1))
return ES_FAILED;
GECODE_ME_CHECK(x0.le(home,x1.max()));
GECODE_ME_CHECK(x1.gr(home,x0.min()));
if (x0.max() >= x1.min())
(void) new (home) Le<V0,V1>(home,x0,x1);
return ES_OK;
}
template<class V0, class V1>
forceinline
Le<V0,V1>::Le(Space& home, bool share, Le<V0,V1>& p)
: MixBinaryPropagator<V0,PC_INT_BND,V1,PC_INT_BND>(home,share,p) {}
template<class V0, class V1>
Actor*
Le<V0,V1>::copy(Space& home, bool share) {
return new (home) Le<V0,V1>(home,share,*this);
}
template<class V0, class V1>
ExecStatus
Le<V0,V1>::propagate(Space& home, const ModEventDelta&) {
GECODE_ME_CHECK(x0.le(home,x1.max()));
GECODE_ME_CHECK(x1.gr(home,x0.min()));
return (x0.max() < x1.min()) ? home.ES_SUBSUMED(*this) : ES_FIX;
}
/*
* Nary less and less or equal propagator
*
*/
template<class View, int o>
forceinline
NaryLqLe<View,o>::Index::Index(Space& home, Propagator& p,
Council<Index>& c, int i0)
: Advisor(home,p,c), i(i0) {}
template<class View, int o>
forceinline
NaryLqLe<View,o>::Index::Index(Space& home, bool share, Index& a)
: Advisor(home,share,a), i(a.i) {}
template<class View, int o>
forceinline
NaryLqLe<View,o>::Pos::Pos(int p0, Pos* n)
: FreeList(n), p(p0) {}
template<class View, int o>
forceinline typename NaryLqLe<View,o>::Pos*
NaryLqLe<View,o>::Pos::next(void) const {
return static_cast<Pos*>(FreeList::next());
}
template<class View, int o>
forceinline void
NaryLqLe<View,o>::Pos::operator delete(void*) {}
template<class View, int o>
forceinline void
NaryLqLe<View,o>::Pos::operator delete(void*, Space&) {
GECODE_NEVER;
}
template<class View, int o>
forceinline void*
NaryLqLe<View,o>::Pos::operator new(size_t, Space& home) {
return home.fl_alloc<sizeof(Pos)>();
}
template<class View, int o>
forceinline void
NaryLqLe<View,o>::Pos::dispose(Space& home) {
home.fl_dispose<sizeof(Pos)>(this,this);
}
template<class View, int o>
forceinline bool
NaryLqLe<View,o>::empty(void) const {
return pos == NULL;
}
template<class View, int o>
forceinline void
NaryLqLe<View,o>::push(Space& home, int p) {
// Try to avoid entering same position twice
if ((pos != NULL) && (pos->p == p))
return;
pos = new (home) Pos(p,pos);
}
template<class View, int o>
forceinline int
NaryLqLe<View,o>::pop(Space& home) {
Pos* t = pos;
int p = t->p;
pos = pos->next();
t->dispose(home);
return p;
}
template<class View, int o>
forceinline
NaryLqLe<View,o>::NaryLqLe(Home home, ViewArray<View>& x)
: NaryPropagator<View,PC_INT_NONE>(home,x),
c(home), pos(NULL), run(false), n_subsumed(0) {
for (int i=x.size(); i--; )
x[i].subscribe(home, *new (home) Index(home,*this,c,i));
}
template<class View, int o>
ExecStatus
NaryLqLe<View,o>::post(Home home, ViewArray<View>& x) {
assert((o == 0) || (o == 1));
// Check for sharing
if (x.same(home)) {
if (o == 1)
return ES_FAILED;
/*
* Eliminate sharing: if a view occurs twice, all views in between
* must be equal.
*/
int n = x.size();
for (int i=0; i<n; i++)
for (int j=n-1; j>i; j--)
if (same(x[i],x[j])) {
if (i+1 != j) {
// Create equality propagator for elements i+1 ... j
ViewArray<View> y(home,j-i);
for (int k=j-i; k--; )
y[k] = x[i+1+k];
GECODE_ES_CHECK(NaryEqBnd<View>::post(home,y));
}
for (int k=0; k<n-1-j-1+1; k++)
x[i+1+k]=x[j+1+k];
n -= j-i;
break;
}
x.size(n);
}
// Propagate one round
for (int i=1; i<x.size(); i++)
GECODE_ME_CHECK(x[i].gq(home,x[i-1].min()+o));
for (int i=x.size()-1; i--;)
GECODE_ME_CHECK(x[i].lq(home,x[i+1].max()-o));
// Eliminate redundant variables
{
// Eliminate at beginning
{
int i=0;
while ((i+1 < x.size()) && (x[i].max()+o <= x[i+1].min()))
i++;
x.drop_fst(i);
}
// Eliminate at end
{
int i=x.size()-1;
while ((i > 0) && (x[i-1].max()+o <= x[i].min()))
i--;
x.drop_lst(i);
}
// Eliminate in the middle
if (x.size() > 1) {
int j=1;
for (int i=1; i+1<x.size(); i++)
if ((x[j-1].max()+o > x[i].min()) ||
(x[i].max()+o > x[i+1].min()))
x[j++]=x[i];
x[j++]=x[x.size()-1];
x.size(j);
}
}
if (x.size() == 2) {
if (o == 0)
return Lq<View,View>::post(home,x[0],x[1]);
else
return Le<View,View>::post(home,x[0],x[1]);
} else if (x.size() >= 2) {
(void) new (home) NaryLqLe<View,o>(home,x);
}
return ES_OK;
}
template<class View, int o>
forceinline
NaryLqLe<View,o>::NaryLqLe(Space& home, bool share, NaryLqLe<View,o>& p)
: NaryPropagator<View,PC_INT_NONE>(home,share,p),
pos(NULL), run(false), n_subsumed(p.n_subsumed) {
assert(p.pos == NULL);
c.update(home, share, p.c);
}
template<class View, int o>
Actor*
NaryLqLe<View,o>::copy(Space& home, bool share) {
if (n_subsumed > n_threshold) {
Region r(home);
// Record for which views there is an advisor
Support::BitSet<Region> a(r,static_cast<unsigned int>(x.size()));
for (Advisors<Index> as(c); as(); ++as)
a.set(static_cast<unsigned int>(as.advisor().i));
// Compact view array and compute map for advisors
int* m = r.alloc<int>(x.size());
int j=0;
for (int i=0; i<x.size(); i++)
if (a.get(static_cast<unsigned int>(i))) {
m[i] = j; x[j++] = x[i];
}
x.size(j);
// Remap advisors
for (Advisors<Index> as(c); as(); ++as)
as.advisor().i = m[as.advisor().i];
n_subsumed = 0;
}
return new (home) NaryLqLe<View,o>(home,share,*this);
}
template<class View, int o>
PropCost
NaryLqLe<View,o>::cost(const Space&, const ModEventDelta&) const {
return PropCost::binary(PropCost::HI);
}
template<class View, int o>
forceinline size_t
NaryLqLe<View,o>::dispose(Space& home) {
for (Advisors<Index> as(c); as(); ++as)
x[as.advisor().i].cancel(home,as.advisor());
c.dispose(home);
while (!empty())
(void) pop(home);
(void) NaryPropagator<View,PC_INT_NONE>::dispose(home);
return sizeof(*this);
}
template<class View, int o>
ExecStatus
NaryLqLe<View,o>::advise(Space& home, Advisor& _a, const Delta& d) {
Index& a = static_cast<Index&>(_a);
const int i = a.i;
switch (View::modevent(d)) {
case ME_INT_VAL:
a.dispose(home,c);
n_subsumed++;
break;
case ME_INT_BND:
if (((i == 0) || (x[i-1].max()+o <= x[i].min())) &&
((i == x.size()-1) || (x[i].max()+o <= x[i+1].min()))) {
x[i].cancel(home,a);
a.dispose(home,c);
n_subsumed++;
return (run || (n_subsumed + 1 < x.size())) ? ES_FIX : ES_NOFIX;
}
break;
default:
return ES_FIX;
}
if (run)
return ES_FIX;
if (((i < x.size()-1) && (x[i+1].min() < x[i].min()+o)) ||
((i > 0) && (x[i-1].max() > x[i].max()-o))) {
push(home,i);
return ES_NOFIX;
}
return (n_subsumed+1 >= x.size()) ? ES_NOFIX : ES_FIX;
}
template<class View, int o>
void
NaryLqLe<View,o>::reschedule(Space& home) {
View::schedule(home, *this, ME_INT_BND);
}
template<class View, int o>
ExecStatus
NaryLqLe<View,o>::propagate(Space& home, const ModEventDelta&) {
run = true;
int n = x.size();
while (!empty()) {
int p = pop(home);
for (int i=p; i<n-1; i++) {
ModEvent me = x[i+1].gq(home,x[i].min()+o);
if (me_failed(me))
return ES_FAILED;
if (!me_modified(me))
break;
}
for (int i=p; i>0; i--) {
ModEvent me = x[i-1].lq(home,x[i].max()-o);
if (me_failed(me))
return ES_FAILED;
if (!me_modified(me))
break;
}
}
#ifdef GECODE_AUDIT
for (int i=0; i<n-1; i++)
assert(!me_modified(x[i+1].gq(home,x[i].min()+o)));
for (int i=n-1; i>0; i--)
assert(!me_modified(x[i-1].lq(home,x[i].max()-o)));
#endif
if (n_subsumed+1 >= n)
return home.ES_SUBSUMED(*this);
run = false;
return ES_FIX;
}
/*
* Reified less or equal propagator
*
*/
template<class View, class CtrlView, ReifyMode rm>
forceinline
ReLq<View,CtrlView,rm>::ReLq(Home home, View x0, View x1, CtrlView b)
: ReBinaryPropagator<View,PC_INT_BND,CtrlView>(home,x0,x1,b) {}
template<class View, class CtrlView, ReifyMode rm>
ExecStatus
ReLq<View,CtrlView,rm>::post(Home home, View x0, View x1, CtrlView b) {
if (b.one()) {
if (rm == RM_PMI)
return ES_OK;
return Lq<View,View>::post(home,x0,x1);
}
if (b.zero()) {
if (rm == RM_IMP)
return ES_OK;
return Le<View,View>::post(home,x1,x0);
}
if (!same(x0,x1)) {
switch (rtest_lq(x0,x1)) {
case RT_TRUE:
if (rm != RM_IMP)
GECODE_ME_CHECK(b.one_none(home));
break;
case RT_FALSE:
if (rm != RM_PMI)
GECODE_ME_CHECK(b.zero_none(home));
break;
case RT_MAYBE:
(void) new (home) ReLq<View,CtrlView,rm>(home,x0,x1,b);
break;
default: GECODE_NEVER;
}
} else if (rm != RM_IMP) {
GECODE_ME_CHECK(b.one_none(home));
}
return ES_OK;
}
template<class View, class CtrlView, ReifyMode rm>
forceinline
ReLq<View,CtrlView,rm>::ReLq(Space& home, bool share, ReLq& p)
: ReBinaryPropagator<View,PC_INT_BND,CtrlView>(home,share,p) {}
template<class View, class CtrlView, ReifyMode rm>
Actor*
ReLq<View,CtrlView,rm>::copy(Space& home, bool share) {
return new (home) ReLq<View,CtrlView,rm>(home,share,*this);
}
template<class View, class CtrlView, ReifyMode rm>
ExecStatus
ReLq<View,CtrlView,rm>::propagate(Space& home, const ModEventDelta&) {
if (b.one()) {
if (rm != RM_PMI)
GECODE_REWRITE(*this,(Lq<View,View>::post(home(*this),x0,x1)));
} else if (b.zero()) {
if (rm != RM_IMP)
GECODE_REWRITE(*this,(Le<View,View>::post(home(*this),x1,x0)));
} else {
switch (rtest_lq(x0,x1)) {
case RT_TRUE:
if (rm != RM_IMP)
GECODE_ME_CHECK(b.one_none(home));
break;
case RT_FALSE:
if (rm != RM_PMI)
GECODE_ME_CHECK(b.zero_none(home));
break;
case RT_MAYBE:
return ES_FIX;
default: GECODE_NEVER;
}
}
return home.ES_SUBSUMED(*this);
}
/*
* Reified less or equal propagator involving one variable
*
*/
template<class View, class CtrlView, ReifyMode rm>
forceinline
ReLqInt<View,CtrlView,rm>::ReLqInt(Home home, View x, int c0, CtrlView b)
: ReUnaryPropagator<View,PC_INT_BND,CtrlView>(home,x,b), c(c0) {}
template<class View, class CtrlView, ReifyMode rm>
ExecStatus
ReLqInt<View,CtrlView,rm>::post(Home home, View x, int c, CtrlView b) {
if (b.one()) {
if (rm != RM_PMI)
GECODE_ME_CHECK(x.lq(home,c));
} else if (b.zero()) {
if (rm != RM_IMP)
GECODE_ME_CHECK(x.gr(home,c));
} else {
switch (rtest_lq(x,c)) {
case RT_TRUE:
if (rm != RM_IMP)
GECODE_ME_CHECK(b.one_none(home));
break;
case RT_FALSE:
if (rm != RM_PMI)
GECODE_ME_CHECK(b.zero_none(home));
break;
case RT_MAYBE:
(void) new (home) ReLqInt<View,CtrlView,rm>(home,x,c,b);
break;
default: GECODE_NEVER;
}
}
return ES_OK;
}
template<class View, class CtrlView, ReifyMode rm>
forceinline
ReLqInt<View,CtrlView,rm>::ReLqInt(Space& home, bool share, ReLqInt& p)
: ReUnaryPropagator<View,PC_INT_BND,CtrlView>(home,share,p), c(p.c) {}
template<class View, class CtrlView, ReifyMode rm>
Actor*
ReLqInt<View,CtrlView,rm>::copy(Space& home, bool share) {
return new (home) ReLqInt<View,CtrlView,rm>(home,share,*this);
}
template<class View, class CtrlView, ReifyMode rm>
ExecStatus
ReLqInt<View,CtrlView,rm>::propagate(Space& home, const ModEventDelta&) {
if (b.one()) {
if (rm != RM_PMI)
GECODE_ME_CHECK(x0.lq(home,c));
} else if (b.zero()) {
if (rm != RM_IMP)
GECODE_ME_CHECK(x0.gr(home,c));
} else {
switch (rtest_lq(x0,c)) {
case RT_TRUE:
if (rm != RM_IMP)
GECODE_ME_CHECK(b.one_none(home));
break;
case RT_FALSE:
if (rm != RM_PMI)
GECODE_ME_CHECK(b.zero_none(home));
break;
case RT_MAYBE:
return ES_FIX;
default: GECODE_NEVER;
}
}
return home.ES_SUBSUMED(*this);
}
}}}
// STATISTICS: int-prop
|