/usr/include/gecode/int/var-imp/int.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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Contributing authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Christian Schulte, 2003
* Guido Tack, 2004
*
* Last modified:
* $Date: 2017-02-21 06:45:56 +0100 (Tue, 21 Feb 2017) $ by $Author: schulte $
* $Revision: 15465 $
*
* 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 {
/*
* Range lists
*
*/
#define GECODE_INT_RL2PD(r) reinterpret_cast<ptrdiff_t>(r)
#define GECODE_INT_PD2RL(p) reinterpret_cast<RangeList*>(p)
forceinline
IntVarImp::RangeList::RangeList(void) {}
forceinline
IntVarImp::RangeList::RangeList(int min, int max)
: _min(min), _max(max) {}
forceinline
IntVarImp::RangeList::RangeList(int min, int max, RangeList* p, RangeList* n)
: _min(min), _max(max) {
_next = GECODE_INT_PD2RL(GECODE_INT_RL2PD(p)^GECODE_INT_RL2PD(n));
}
forceinline IntVarImp::RangeList*
IntVarImp::RangeList::next(const RangeList* p) const {
return GECODE_INT_PD2RL(GECODE_INT_RL2PD(_next)^GECODE_INT_RL2PD(p));
}
forceinline IntVarImp::RangeList*
IntVarImp::RangeList::prev(const RangeList* n) const {
return GECODE_INT_PD2RL(GECODE_INT_RL2PD(_next)^GECODE_INT_RL2PD(n));
}
forceinline void
IntVarImp::RangeList::prevnext(RangeList* p, RangeList* n) {
_next = GECODE_INT_PD2RL(GECODE_INT_RL2PD(p)^GECODE_INT_RL2PD(n));
}
forceinline void
IntVarImp::RangeList::next(RangeList* o, RangeList* n) {
_next = GECODE_INT_PD2RL(GECODE_INT_RL2PD(_next)^
(GECODE_INT_RL2PD(o)^GECODE_INT_RL2PD(n)));
}
forceinline void
IntVarImp::RangeList::prev(RangeList* o, RangeList* n) {
_next = GECODE_INT_PD2RL(GECODE_INT_RL2PD(_next)^
(GECODE_INT_RL2PD(o)^GECODE_INT_RL2PD(n)));
}
forceinline void
IntVarImp::RangeList::fix(RangeList* n) {
_next = n;
}
forceinline void
IntVarImp::RangeList::min(int n) {
_min = n;
}
forceinline void
IntVarImp::RangeList::max(int n) {
_max = n;
}
forceinline int
IntVarImp::RangeList::min(void) const {
return _min;
}
forceinline int
IntVarImp::RangeList::max(void) const {
return _max;
}
forceinline unsigned int
IntVarImp::RangeList::width(void) const {
return static_cast<unsigned int>(_max - _min) + 1;
}
forceinline void
IntVarImp::RangeList::operator delete(void*) {}
forceinline void
IntVarImp::RangeList::operator delete(void*, Space&) {
GECODE_NEVER;
}
forceinline void
IntVarImp::RangeList::operator delete(void*, void*) {
GECODE_NEVER;
}
forceinline void*
IntVarImp::RangeList::operator new(size_t, Space& home) {
return home.fl_alloc<sizeof(RangeList)>();
}
forceinline void*
IntVarImp::RangeList::operator new(size_t, void* p) {
return p;
}
forceinline void
IntVarImp::RangeList::dispose(Space& home, RangeList* p, RangeList* l) {
RangeList* c = this;
while (c != l) {
RangeList* n = c->next(p);
c->fix(n);
p=c; c=n;
}
home.fl_dispose<sizeof(RangeList)>(this,l);
}
forceinline void
IntVarImp::RangeList::dispose(Space& home, RangeList* l) {
home.fl_dispose<sizeof(RangeList)>(this,l);
}
forceinline void
IntVarImp::RangeList::dispose(Space& home) {
home.fl_dispose<sizeof(RangeList)>(this,this);
}
#undef GECODE_INT_RL2PD
#undef GECODE_INT_PD2RL
/*
* Mainitaining range lists for variable domain
*
*/
forceinline IntVarImp::RangeList*
IntVarImp::fst(void) const {
return dom.next(NULL);
}
forceinline void
IntVarImp::fst(IntVarImp::RangeList* f) {
dom.prevnext(NULL,f);
}
forceinline IntVarImp::RangeList*
IntVarImp::lst(void) const {
return _lst;
}
forceinline void
IntVarImp::lst(IntVarImp::RangeList* l) {
_lst = l;
}
/*
* Creation of new variable implementations
*
*/
forceinline
IntVarImp::IntVarImp(Space& home, int min, int max)
: IntVarImpBase(home), dom(min,max,NULL,NULL), holes(0) {}
forceinline
IntVarImp::IntVarImp(Space& home, const IntSet& d)
: IntVarImpBase(home), dom(d.min(),d.max()) {
if (d.ranges() > 1) {
int n = d.ranges();
assert(n >= 2);
RangeList* r = home.alloc<RangeList>(n);
fst(r); lst(r+n-1);
unsigned int h = static_cast<unsigned int>(d.max()-d.min())+1;
h -= d.width(0);
r[0].min(d.min(0)); r[0].max(d.max(0));
r[0].prevnext(NULL,&r[1]);
for (int i = 1; i < n-1; i++) {
h -= d.width(i);
r[i].min(d.min(i)); r[i].max(d.max(i));
r[i].prevnext(&r[i-1],&r[i+1]);
}
h -= d.width(n-1);
r[n-1].min(d.min(n-1)); r[n-1].max(d.max(n-1));
r[n-1].prevnext(&r[n-2],NULL);
holes = h;
} else {
fst(NULL); holes = 0;
}
}
/*
* Operations on integer variable implementations
*
*/
forceinline int
IntVarImp::min(void) const {
return dom.min();
}
forceinline int
IntVarImp::max(void) const {
return dom.max();
}
forceinline int
IntVarImp::val(void) const {
assert(dom.min() == dom.max());
return dom.min();
}
forceinline bool
IntVarImp::range(void) const {
return fst() == NULL;
}
forceinline bool
IntVarImp::assigned(void) const {
return dom.min() == dom.max();
}
forceinline unsigned int
IntVarImp::width(void) const {
return dom.width();
}
forceinline unsigned int
IntVarImp::size(void) const {
return dom.width() - holes;
}
forceinline unsigned int
IntVarImp::regret_min(void) const {
if (fst() == NULL) {
return (dom.min() == dom.max()) ? 0U : 1U;
} else if (dom.min() == fst()->max()) {
return static_cast<unsigned int>(fst()->next(NULL)->min()-dom.min());
} else {
return 1U;
}
}
forceinline unsigned int
IntVarImp::regret_max(void) const {
if (fst() == NULL) {
return (dom.min() == dom.max()) ? 0U : 1U;
} else if (dom.max() == lst()->min()) {
return static_cast<unsigned int>(dom.max()-lst()->prev(NULL)->max());
} else {
return 1U;
}
}
/*
* Tests
*
*/
forceinline bool
IntVarImp::in(int n) const {
if ((n < dom.min()) || (n > dom.max()))
return false;
return (fst() == NULL) || in_full(n);
}
forceinline bool
IntVarImp::in(long long int n) const {
if ((n < dom.min()) || (n > dom.max()))
return false;
return (fst() == NULL) || in_full(static_cast<int>(n));
}
/*
* Accessing rangelists for iteration
*
*/
forceinline const IntVarImp::RangeList*
IntVarImp::ranges_fwd(void) const {
return (fst() == NULL) ? &dom : fst();
}
forceinline const IntVarImp::RangeList*
IntVarImp::ranges_bwd(void) const {
return (fst() == NULL) ? &dom : lst();
}
/*
* Support for delta information
*
*/
forceinline int
IntVarImp::min(const Delta& d) {
return static_cast<const IntDelta&>(d).min();
}
forceinline int
IntVarImp::max(const Delta& d) {
return static_cast<const IntDelta&>(d).max();
}
forceinline bool
IntVarImp::any(const Delta& d) {
return static_cast<const IntDelta&>(d).any();
}
/*
* Tell operations (to be inlined: performing bounds checks first)
*
*/
forceinline ModEvent
IntVarImp::gq(Space& home, int n) {
if (n <= dom.min()) return ME_INT_NONE;
if (n > dom.max()) return fail(home);
ModEvent me = gq_full(home,n);
GECODE_ASSUME((me == ME_INT_FAILED) |
(me == ME_INT_VAL) |
(me == ME_INT_BND));
return me;
}
forceinline ModEvent
IntVarImp::gq(Space& home, long long int n) {
if (n <= dom.min()) return ME_INT_NONE;
if (n > dom.max()) return fail(home);
ModEvent me = gq_full(home,static_cast<int>(n));
GECODE_ASSUME((me == ME_INT_FAILED) |
(me == ME_INT_VAL) |
(me == ME_INT_BND));
return me;
}
forceinline ModEvent
IntVarImp::lq(Space& home, int n) {
if (n >= dom.max()) return ME_INT_NONE;
if (n < dom.min()) return fail(home);
ModEvent me = lq_full(home,n);
GECODE_ASSUME((me == ME_INT_FAILED) |
(me == ME_INT_VAL) |
(me == ME_INT_BND));
return me;
}
forceinline ModEvent
IntVarImp::lq(Space& home, long long int n) {
if (n >= dom.max()) return ME_INT_NONE;
if (n < dom.min()) return fail(home);
ModEvent me = lq_full(home,static_cast<int>(n));
GECODE_ASSUME((me == ME_INT_FAILED) |
(me == ME_INT_VAL) |
(me == ME_INT_BND));
return me;
}
forceinline ModEvent
IntVarImp::eq(Space& home, int n) {
if ((n < dom.min()) || (n > dom.max()))
return fail(home);
if ((n == dom.min()) && (n == dom.max()))
return ME_INT_NONE;
ModEvent me = eq_full(home,n);
GECODE_ASSUME((me == ME_INT_FAILED) | (me == ME_INT_VAL));
return me;
}
forceinline ModEvent
IntVarImp::eq(Space& home, long long int m) {
if ((m < dom.min()) || (m > dom.max()))
return fail(home);
int n = static_cast<int>(m);
if ((n == dom.min()) && (n == dom.max()))
return ME_INT_NONE;
ModEvent me = eq_full(home,n);
GECODE_ASSUME((me == ME_INT_FAILED) | (me == ME_INT_VAL));
return me;
}
forceinline ModEvent
IntVarImp::nq(Space& home, int n) {
if ((n < dom.min()) || (n > dom.max()))
return ME_INT_NONE;
return nq_full(home,n);
}
forceinline ModEvent
IntVarImp::nq(Space& home, long long int d) {
if ((d < dom.min()) || (d > dom.max()))
return ME_INT_NONE;
return nq_full(home,static_cast<int>(d));
}
/*
* Forward range iterator for rangelists
*
*/
forceinline
IntVarImpFwd::IntVarImpFwd(void) {}
forceinline
IntVarImpFwd::IntVarImpFwd(const IntVarImp* x)
: p(NULL), c(x->ranges_fwd()) {}
forceinline void
IntVarImpFwd::init(const IntVarImp* x) {
p=NULL; c=x->ranges_fwd();
}
forceinline bool
IntVarImpFwd::operator ()(void) const {
return c != NULL;
}
forceinline void
IntVarImpFwd::operator ++(void) {
const IntVarImp::RangeList* n=c->next(p); p=c; c=n;
}
forceinline int
IntVarImpFwd::min(void) const {
return c->min();
}
forceinline int
IntVarImpFwd::max(void) const {
return c->max();
}
forceinline unsigned int
IntVarImpFwd::width(void) const {
return c->width();
}
/*
* Backward range iterator for rangelists
*
*/
forceinline
IntVarImpBwd::IntVarImpBwd(void) {}
forceinline
IntVarImpBwd::IntVarImpBwd(const IntVarImp* x)
: n(NULL), c(x->ranges_bwd()) {}
forceinline void
IntVarImpBwd::init(const IntVarImp* x) {
n=NULL; c=x->ranges_bwd();
}
forceinline bool
IntVarImpBwd::operator ()(void) const {
return c != NULL;
}
forceinline void
IntVarImpBwd::operator ++(void) {
const IntVarImp::RangeList* p=c->prev(n); n=c; c=p;
}
forceinline int
IntVarImpBwd::min(void) const {
return c->min();
}
forceinline int
IntVarImpBwd::max(void) const {
return c->max();
}
forceinline unsigned int
IntVarImpBwd::width(void) const {
return c->width();
}
/*
* Iterator-based domain operations
*
*/
template<class I>
forceinline ModEvent
IntVarImp::narrow_r(Space& home, I& ri, bool depends) {
// Is new domain empty?
if (!ri())
return fail(home);
int min0 = ri.min();
int max0 = ri.max();
++ri;
ModEvent me;
// Is new domain range?
if (!ri()) {
// Remove possible rangelist (if it was not a range, the domain
// must have been narrowed!)
if (fst()) {
fst()->dispose(home,NULL,lst());
fst(NULL); holes = 0;
}
const int min1 = dom.min(); dom.min(min0);
const int max1 = dom.max(); dom.max(max0);
if ((min0 == min1) && (max0 == max1))
return ME_INT_NONE;
me = (min0 == max0) ? ME_INT_VAL : ME_INT_BND;
goto notify;
}
if (depends || range()) {
// Construct new rangelist
RangeList* f = new (home) RangeList(min0,max0,NULL,NULL);
RangeList* l = f;
unsigned int s = static_cast<unsigned int>(max0-min0+1);
do {
RangeList* n = new (home) RangeList(ri.min(),ri.max(),l,NULL);
l->next(NULL,n);
l = n;
s += ri.width();
++ri;
} while (ri());
if (fst() != NULL)
fst()->dispose(home,NULL,lst());
fst(f); lst(l);
// Check for modification
if (size() == s)
return ME_INT_NONE;
const int min1 = dom.min(); min0 = f->min(); dom.min(min0);
const int max1 = dom.max(); max0 = l->max(); dom.max(max0);
holes = width() - s;
me = ((min0 == min1) && (max0 == max1)) ? ME_INT_DOM : ME_INT_BND;
goto notify;
} else {
// Set up two sentinel elements
RangeList f, l;
// Put all ranges between sentinels
f.prevnext(NULL,fst()); l.prevnext(lst(),NULL);
fst()->prev(NULL,&f); lst()->next(NULL,&l);
// Number of values removed (potential holes)
unsigned int h = 0;
// The previous range
RangeList* p = &f;
// The current range
RangeList* r = f.next(NULL);
while (true) {
assert((r != &f) && (r != &l));
if (r->max() < min0) {
// Entire range removed
h += r->width();
RangeList* n=r->next(p);
p->next(r,n); n->prev(r,p);
r->dispose(home);
r=n;
if (r == &l)
goto done;
} else if ((r->min() == min0) && (r->max() == max0)) {
// Range unchanged
RangeList* n=r->next(p); p=r; r=n;
if (r == &l)
goto done;
if (!ri())
goto done;
min0=ri.min(); max0=ri.max(); ++ri;
} else {
// Range might have been split into many small ranges
assert((r->min() <= min0) && (max0 <= r->max()));
h += r->width();
int end = r->max();
// Copy first range
r->min(min0); r->max(max0);
assert(h > r->width());
h -= r->width();
{
RangeList* n=r->next(p); p=r; r=n;
}
while (true) {
if (!ri())
goto done;
min0=ri.min(); max0=ri.max(); ++ri;
if (max0 > end)
break;
assert(h > static_cast<unsigned int>(max0-min0+1));
h -= max0-min0+1;
RangeList* n = new (home) RangeList(min0,max0,p,r);
p->next(r,n); r->prev(p,n);
p=n;
}
if (r == &l)
goto done;
}
}
done:
// Remove remaining ranges
while (r != &l) {
h += r->width();
RangeList* n=r->next(p);
p->next(r,n); n->prev(r,p);
r->dispose(home);
r=n;
}
assert((r == &l) && !ri());
// New first and last ranges
RangeList* fn = f.next(NULL);
RangeList* ln = l.prev(NULL);
// All ranges pruned?
assert(fn != &l);
// Only a single range left?
assert(fn != ln);
// The number of removed values
holes += h;
// Unlink sentinel ranges
fn->prev(&f,NULL); ln->next(&l,NULL);
// How many values where removed at the bounds
unsigned int b = (static_cast<unsigned int>(fn->min()-dom.min()) +
static_cast<unsigned int>(dom.max()-ln->max()));
// Set new first and last ranges
fst(fn); lst(ln);
if (b > 0) {
assert((dom.min() != fn->min()) || (dom.max() != ln->max()));
dom.min(fn->min()); dom.max(ln->max());
holes -= b;
me = ME_INT_BND; goto notify;
}
if (h > 0) {
assert((dom.min() == fn->min()) && (dom.max() == ln->max()));
me = ME_INT_DOM; goto notify;
}
return ME_INT_NONE;
}
notify:
IntDelta d;
return notify(home,me,d);
}
template<class I>
forceinline ModEvent
IntVarImp::inter_r(Space& home, I& i, bool) {
IntVarImpFwd j(this);
Iter::Ranges::Inter<I,IntVarImpFwd> ij(i,j);
return narrow_r(home,ij,true);
}
template<class I>
forceinline ModEvent
IntVarImp::minus_r(Space& home, I& i, bool depends) {
if (depends) {
IntVarImpFwd j(this);
Iter::Ranges::Diff<IntVarImpFwd,I> ij(j,i);
return narrow_r(home,ij,true);
}
// Skip all ranges that are too small
while (i() && (i.max() < dom.min()))
++i;
// Is there no range left or all are too large?
if (!i() || (i.min() > dom.max()))
return ME_INT_NONE;
int i_min = i.min();
int i_max = i.max();
++i;
if ((i_min <= dom.min()) && (i_max >= dom.max()))
return fail(home);
if ((i_min > dom.min()) && (i_max >= dom.max()))
return lq(home,i_min-1);
if ((i_min <= dom.min()) && (i_max < dom.max()) &&
(!i() || (i.min() > dom.max())))
return gq(home,i_max+1);
// Set up two sentinel elements
RangeList f, l;
// Put all ranges between sentinels
if (range()) {
// Create a new rangelist just for simplicity
RangeList* n = new (home) RangeList(min(),max(),&f,&l);
f.prevnext(NULL,n); l.prevnext(n,NULL);
} else {
// Link the two sentinel elements
f.prevnext(NULL,fst()); l.prevnext(lst(),NULL);
fst()->prev(NULL,&f); lst()->next(NULL,&l);
}
// Number of values removed (potential holes)
unsigned int h = 0;
// The previous range
RangeList* p = &f;
// The current range
RangeList* r = f.next(NULL);
while (true) {
assert((r != &f) && (r != &l));
if (i_min > r->max()) {
RangeList* n=r->next(p); p=r; r=n;
if (r == &l)
break;
} else if (i_max < r->min()) {
if (!i())
break;
i_min = i.min();
i_max = i.max();
++i;
} else if ((i_min <= r->min()) && (r->max() <= i_max)) {
// r is included in i: remove entire range r
h += r->width();
RangeList* n=r->next(p);
p->next(r,n); n->prev(r,p);
r->dispose(home);
r=n;
if (r == &l)
break;
} else if ((i_min > r->min()) && (i_max < r->max())) {
// i is included in r: create new range before the current one
h += static_cast<unsigned int>(i_max - i_min) + 1;
RangeList* n = new (home) RangeList(r->min(),i_min-1,p,r);
r->min(i_max+1);
p->next(r,n); r->prev(p,n);
p=n;
if (!i())
break;
i_min = i.min();
i_max = i.max();
++i;
} else if (i_max < r->max()) {
assert(i_min <= r->min());
// i ends before r: adjust minimum of r
h += i_max-r->min()+1;
r->min(i_max+1);
if (!i())
break;
i_min = i.min();
i_max = i.max();
++i;
} else {
assert((i_max >= r->max()) && (r->min() < i_min));
// r ends before i: adjust maximum of r
h += r->max()-i_min+1;
r->max(i_min-1);
RangeList* n=r->next(p); p=r; r=n;
if (r == &l)
break;
}
}
// New first and last ranges
RangeList* fn = f.next(NULL);
RangeList* ln = l.prev(NULL);
// All ranges pruned?
if (fn == &l) {
fst(NULL); lst(NULL); holes=0;
return fail(home);
}
ModEvent me;
unsigned int b;
// Only a single range left?
if (fn == ln) {
assert(h > 0);
dom.min(fn->min()); dom.max(fn->max());
fn->dispose(home);
fst(NULL); lst(NULL);
holes = 0;
me = assigned() ? ME_INT_VAL : ME_INT_BND;
goto notify;
}
// The number of removed values
holes += h;
// Unlink sentinel ranges
fn->prev(&f,NULL); ln->next(&l,NULL);
// How many values where removed at the bounds
b = (static_cast<unsigned int>(fn->min()-dom.min()) +
static_cast<unsigned int>(dom.max()-ln->max()));
// Set new first and last ranges
fst(fn); lst(ln);
if (b > 0) {
assert((dom.min() != fn->min()) || (dom.max() != ln->max()));
dom.min(fn->min()); dom.max(ln->max());
holes -= b;
me = ME_INT_BND; goto notify;
}
if (h > 0) {
assert((dom.min() == fn->min()) && (dom.max() == ln->max()));
me = ME_INT_DOM; goto notify;
}
return ME_INT_NONE;
notify:
IntDelta d;
return notify(home,me,d);
}
template<class I>
forceinline ModEvent
IntVarImp::narrow_v(Space& home, I& i, bool depends) {
Iter::Values::ToRanges<I> r(i);
return narrow_r(home,r,depends);
}
template<class I>
forceinline ModEvent
IntVarImp::inter_v(Space& home, I& i, bool depends) {
Iter::Values::ToRanges<I> r(i);
return inter_r(home,r,depends);
}
template<class I>
forceinline ModEvent
IntVarImp::minus_v(Space& home, I& i, bool depends) {
if (depends) {
Iter::Values::ToRanges<I> r(i);
return minus_r(home, r, true);
}
// Skip all values that are too small
while (i() && (i.val() < dom.min()))
++i;
// Is there no value left or all are too large?
if (!i() || (i.val() > dom.max()))
return ME_INT_NONE;
int v = i.val();
// Skip values that are the same
do {
++i;
} while (i() && (i.val() == v));
// Is there only a single value to be pruned?
if (!i() || (i.val() > dom.max()))
return nq_full(home,v);
// Set up two sentinel elements
RangeList f, l;
// Put all ranges between sentinels
if (range()) {
// Create a new rangelist just for simplicity
RangeList* n = new (home) RangeList(min(),max(),&f,&l);
f.prevnext(NULL,n); l.prevnext(n,NULL);
} else {
// Link the two sentinel elements
f.prevnext(NULL,fst()); l.prevnext(lst(),NULL);
fst()->prev(NULL,&f); lst()->next(NULL,&l);
}
// Number of values removed (potential holes)
unsigned int h = 0;
// The previous range
RangeList* p = &f;
// The current range
RangeList* r = f.next(NULL);
while (true) {
assert((r != &f) && (r != &l));
if (v > r->max()) {
// Move to next range
RangeList* n=r->next(p); p=r; r=n;
if (r == &l)
break;
} else {
if ((v == r->min()) && (v == r->max())) {
// Remove range
h++;
RangeList* n=r->next(p);
p->next(r,n); n->prev(r,p);
r->dispose(home);
r=n;
if (r == &l)
break;
} else if (v == r->min()) {
h++; r->min(v+1);
} else if (v == r->max()) {
h++; r->max(v-1);
RangeList* n=r->next(p); p=r; r=n;
if (r == &l)
break;
} else if (v > r->min()) {
// Create new range before the current one
assert(v < r->max());
h++;
RangeList* n = new (home) RangeList(r->min(),v-1,p,r);
r->min(v+1);
p->next(r,n); r->prev(p,n);
p=n;
}
if (!i())
break;
// Move to next value
v = i.val(); ++i;
}
}
assert((r == &l) || !i());
// New first and last ranges
RangeList* fn = f.next(NULL);
RangeList* ln = l.prev(NULL);
// All ranges pruned?
if (fn == &l) {
fst(NULL); lst(NULL); holes=0;
return fail(home);
}
IntDelta d;
// Only a single range left?
if (fn == ln) {
assert(h > 0);
dom.min(fn->min()); dom.max(fn->max());
fn->dispose(home);
fst(NULL); lst(NULL);
holes = 0;
if (assigned())
return notify(home,ME_INT_VAL,d);
else
return notify(home,ME_INT_BND,d);
}
// The number of removed values
holes += h;
// Unlink sentinel ranges
fn->prev(&f,NULL); ln->next(&l,NULL);
// How many values where removed at the bounds
unsigned int b = (static_cast<unsigned int>(fn->min()-dom.min()) +
static_cast<unsigned int>(dom.max()-ln->max()));
// Set new first and last ranges
fst(fn); lst(ln);
if (b > 0) {
assert((dom.min() != fn->min()) || (dom.max() != ln->max()));
dom.min(fn->min()); dom.max(ln->max());
holes -= b;
return notify(home,ME_INT_BND,d);
}
if (h > 0) {
assert((dom.min() == fn->min()) && (dom.max() == ln->max()));
return notify(home,ME_INT_DOM,d);
}
return ME_INT_NONE;
}
/*
* Copying a variable
*
*/
forceinline IntVarImp*
IntVarImp::copy(Space& home, bool share) {
return copied() ? static_cast<IntVarImp*>(forward())
: perform_copy(home,share);
}
forceinline ModEventDelta
IntVarImp::med(ModEvent me) {
return IntVarImpBase::med(me);
}
}}
// STATISTICS: int-var
|