/usr/include/gecode/int/gcc/dom-sup.hpp is in libgecode-dev 4.2.1-1.
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 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Patrick Pekczynski <pekczynski@ps.uni-sb.de>
*
* Contributing authors:
* Christian Schulte <schulte@gecode.org>
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Patrick Pekczynski, 2005
* Christian Schulte, 2009
* Guido Tack, 2009
*
* Last modified:
* $Date: 2013-03-12 20:10:44 +0100 (Tue, 12 Mar 2013) $ by $Author: schulte $
* $Revision: 13511 $
*
* 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 GCC {
/**
* \brief Bounds constraint (BC) type
*
* If BC = UBC, then we argue about the Upper Bounds Constraint
* else we use the functions for the Lower Bounds Constraint
*/
enum BC {UBC = 1, LBC = 0};
class Edge;
/// Base class for nodes in the variable-value-graph
class Node {
protected:
/// Stores all incident edges on the node
Edge* e;
/// First edge
Edge* fst;
/// Last edge
Edge* lst;
/// Single incoming edge used for storing a path in the algorithms
Edge* ie;
/// Index
int idx;
/// Flags for nodes
enum NodeFlag {
/// No flags set
NF_NONE = 0,
/// Whether node is a value node
NF_VAL = 1 << 0,
/// Whether matched for LBC
NF_M_LBC = 1 << 1,
/// Whether matched for UBC
NF_M_UBC = 1 << 2
};
/// Flags for node
unsigned char nf;
public:
/// stores the number of incident edges on the node
int noe;
/// \name Constructors and initialization
//@{
/// Default constructor
Node(void);
/// Constructor for index \a i that sets type to \a t
Node(NodeFlag nf, int i);
//@}
/// \name Access
//@{
/// Return the type of the node (false for a variable node)
bool type(void) const;
/// Return reference to the incident edges
Edge** adj(void);
/// Return pointer to the first incident edge
Edge* first(void) const;
/// Return pointer to the last incident edge
Edge* last(void) const;
/// Return pointer to the node's inedge
Edge* inedge(void) const;
/// Get index of either variable or value
int index(void) const;
/// check whether a node has been removed from the graph
bool removed(void) const;
//@}
/// \name Update
//@{
/// Set the first edge pointer to \a p
void first(Edge* p);
/// Set the last edge pointer to \a p
void last(Edge* p);
/// Set the inedge pointer to \a p
void inedge(Edge* p);
/// Set index of either variable or value
void index(int i);
//@}
/// \name Memory management
//@{
/// Allocate memory from space
static void* operator new(size_t s, Space& home);
/// Free memory (unused)
static void operator delete(void*, Space&) {};
/// Needed for exceptions
static void operator delete(void*) {};
//@}
};
/// %Variable node
class VarNode : public Node {
protected:
/// Stores the matching edge on this node in the UBC
Edge* ubm;
/// Stores the matching edge on this node in the LBC
Edge* lbm;
public:
/// \name Constructors and initialization
//@{
/// Default constructor
VarNode(void);
/// Creates a variable node with index \a i
VarNode(int i);
//@}
/// \name Access
//@{
/// Return the matching edge on the node
Edge* get_match(BC bc) const;
/// tests whether the node is matched or not
bool matched(BC bc) const;
//@}
/// \name Update
//@{
/// Set the pointer of the matching edge to m
void set_match(BC bc, Edge* m);
/// Set node to matched
void match(BC bc);
/// Unmatch the node
void unmatch(BC bc);
//@}
};
/// Value node
class ValNode : public Node {
protected:
/// Minimal required occurence of the value as stored in k
int _klb;
/// Maximal required occurence of the value as stored in k
int _kub;
/// Index to acces the value via cardinality array k
int _kidx;
/// Stores the current number of occurences of the value
int _kcount;
/// Store numbre of conflicting matching edges
int noc;
/// Minimal capacity of the value node
int lb;
/// Smallest maximal capacity of the value node
int ublow;
/// Maximal capacity of the value node
int ub;
public:
/// Stores the value of the node
int val;
/// \name Constructors and destructors
//@{
/// Default constructor
ValNode(void);
/**
* \brief Constructor for value node
*
* with minimal capacity \a min,
* maximal capacity \a max,
* the value \a value and the index \a k_access in \a k
*/
ValNode(int min, int max, int value, int kidx, int kshift, int count);
//@}
/// \name Access
//@{
/// get max cap for LBC
int maxlow(void) const;
/// Mark the value node as conflicting in case of variable cardinalities
void card_conflict(int c);
/// Check whether the value node is conflicting
int card_conflict(void) const;
/// Reduce the conflict counter
void red_conflict(void);
/// increases the value counter
void inc(void);
/// returns the current number of occurences of the value
int kcount(void) const;
/// returns the number of incident matching edges on a value node
int incid_match(BC bc) const;
/// returns the index in cardinality array k
int kindex(void) const;
/// returns \a true if the node is matched in BC, \a false otherwise
bool matched(BC bc) const;
/// tests whether the node is a sink
bool sink(void) const;
/// tests whether the node is a source
bool source(void) const;
/// return the minimal node capacity as stored in \a k
int kmin(void) const;
/// return the maximal node capacity as stored in \a k
int kmax(void) const;
/// return minimal or maximal capacity
int kbound(BC bc) const;
//@}
/// \name Update
//@{
/// set the max cap for LBC
void maxlow(int i);
/// Set how often value occurs
void kcount(int);
/// changes the index in the cardinality array k
void kindex(int);
/// decrease the node-capacity
void dec(BC bc);
/// increase the node-capacity
void inc(BC bc);
/// return the the node-capacity
int cap(BC bc) const;
/// set the node-capacity to \a c
void cap(BC bc, int c);
/// match the node
void match(BC bc);
/// unmatch the node
void unmatch(BC bc);
/// node reset to original capacity values
void reset(void);
/// set the minimal k-capacity to min
void kmin(int min);
/// set the maximal k-capacity to max
void kmax(int max);
//@}
};
/// Class for edges \f$ e(x,v) \f$ in the variable-value-graph
class Edge {
private:
/// pointer to the variable node
VarNode* x;
/// pointer to the value node
ValNode* v;
/// pointer to the next edge incident on the same variable node
Edge* next_edge;
/// pointer to the previous edge incident on the same variable node
Edge* prev_edge;
/// pointer to the next edge on the same value node
Edge* next_vedge;
/// pointer to the previous edge on the same value node
Edge* prev_vedge;
/// Flags for edges
enum EdgeFlag {
/// No flags set
EF_NONE = 0,
/// Whether edge is used in LBC
EF_MRKLB = 1 << 0,
/// Whether edge is used in UBC
EF_MRKUB = 1 << 1,
/// Whether edge is matched in LBC
EF_LM = 1 << 2,
/// Whether edge is matched in UBC
EF_UM = 1 << 3,
/// Whether edge has been deleted
EF_DEL = 1 << 4
};
/// Flags for edges
unsigned char ef;
public:
/// \name Constructors
//@{
/// Default constructor
Edge(void) {}
/**
* \brief Construct edge \f$e(x,v)\f$ from variable node \a x
* and value node \a y
*/
Edge(VarNode* x, ValNode* v);
//@}
/// \name Access
//@{
/// Whether the edge is used
bool used(BC bc) const;
/// return whether the edge is matched
bool matched(BC bc) const;
/// return whether the edge has been deleted from the graph
bool deleted(void) const;
/**
* \brief return a pointer to the next edge
* If \a t is false the function returns the next edge incident on \a x
* otherwise it returns the next edge incident on \a v
*/
Edge* next(bool t) const;
/// return the pointer to the next edge incident on \a x
Edge* next(void) const;
/// return the pointer to the previous edge incident on \a x
Edge* prev(void) const;
/// return the pointer to the next edge incident on \a v
Edge* vnext(void) const;
/// return the pointer to the previous edge incident on \a v
Edge* vprev(void) const;
/// return the pointer to the variable node \a x of this edge
VarNode* getVar(void) const;
/// return the pointer to the value node \a v of this edge
ValNode* getVal(void) const;
/**
* \brief return pointer to \a x if \a t = true otherwise return \a v
*
*/
Node* getMate(bool t) const;
//@}
/// Update
//@{
/// Mark the edge as used
void use(BC bc);
/// Mark the edge as unused
void free(BC bc);
/// Reset the edge (free the edge, and unmatch the edge)
void reset(BC bc);
/// Match the edge
void match(BC bc);
/// Unmatch the edge and the incident nodes
void unmatch(BC bc);
/// Unmatch the edge and ( \a x if t=false, \a v otherwise )
void unmatch(BC bc, bool t);
/// Unlink the edge from the linked list of edges
void unlink(void);
/// Mark the edge as deleted during synchronization
void del_edge(void);
/// Insert the edge again
void insert_edge(void);
/// return the reference to the next edge incident on \a x
Edge** next_ref(void);
/// return the reference to the previous edge incident on \a x
Edge** prev_ref(void);
/// return the reference to the next edge incident on \a v
Edge** vnext_ref(void);
/// return the reference to the previous edge incident on \a v
Edge** vprev_ref(void);
//@}
/// \name Memory management
//@{
/// Allocate memory from space
static void* operator new(size_t s, Space& home);
/// Free memory (unused)
static void operator delete(void*, Space&) {};
/// Needed for exceptions
static void operator delete(void*) {};
//@}
};
/**
* \brief Variable-value-graph used during propagation
*
*/
template<class Card>
class VarValGraph {
private:
/// Temporary stack for nodes
typedef Support::StaticStack<Node*,Region> NodeStack;
/// Bitset
typedef Support::BitSet<Region> BitSet;
/// Variable partition representing the problem variables
VarNode** vars;
/**
* \brief Value partition
* For each value
* \f$ v_i\in V=\left(\bigcup_\{0, \dots, |x|-1\}\right) D_i \f$
* in the domains of the
* problem variables there is a node in the graph.
*/
ValNode** vals;
/// Cardinality of the variable partition
int n_var;
/**
* \brief Cardinality of the value partition
*
* Computed as \f$ |V| = \left(\bigcup_\{0, \dots, |x|-1\}\right) D_i \f$
*/
int n_val;
/// Total number of nodes in the graph
int n_node;
/**
* \brief The sum over the minimal capacities of all value nodes
*
* \f$sum_min = \sum_{v_i \in V} l_i= k[i].min() \f$
*/
int sum_min;
/**
* \brief The sum over the maximal capacities of all value nodes
*
* \f$sum_max = \sum_{v_i \in V} l_i= k[i].max() \f$
*/
int sum_max;
public:
/// \name Constructors and Destructors
//@{
/**
* \brief Constructor for the variable-value-graph
*
* The variable parition is initialized with the variables from \a x,
* the value partition is initialized with the values from \a k.
**/
VarValGraph(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k,
int smin, int smax);
//@}
/// \name Graph-interface
//@{
/// Check whether minimum requirements shrink variable domains
ExecStatus min_require(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k);
/**
* \brief Synchronization of the graph
*
* If the graph has already been constructed and some edges have
* been removed during propagation, this function removes those edges
* that do not longer belong to the graph associated with the current
* variable domains.
*/
ExecStatus sync(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k);
/// Remove edges that do not belong to any maximal matching
template<BC>
ExecStatus narrow(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k);
/** \brief Compute a maximum matching M on the graph
*
* - If BC=UBC then \f$|M|= |X|\f$
* - If BC=LBC then \f$|M|= \sum_{i\in \{ 0, \dots, |X|-1\}}
* k[i].min()\f$
*/
template<BC>
ExecStatus maximum_matching(Space& home);
/// Compute possible free alternating paths in the graph
template<BC>
void free_alternating_paths(Space& home);
/// Compute possible strongly connected components of the graph
template<BC>
void strongly_connected_components(Space& home);
/**
* \brief Test whether the current maximal matching on the graph
* can be augmented by an alternating path starting and ending with
* a free node.
*/
template<BC>
bool augmenting_path(Space& home, Node*);
protected:
/**
* \brief Perform depth-first search on the graph
*
* Depth first search used to compute the
* strongly connected components of the graph.
*/
template<BC>
void dfs(Node*, BitSet&, BitSet&, int[],
NodeStack&, NodeStack&, int&);
//@}
public:
/// Allocate memory for the graph
void* operator new(size_t t, Space& home);
/// Deallocation (void)
void operator delete(void*, Space&) {}
};
/*
* Nodes
*
*/
forceinline
Node::Node(void) {}
forceinline
Node::Node(NodeFlag nf0, int i)
: e(NULL), fst(NULL), lst(NULL), ie(NULL), idx(i),
nf(static_cast<unsigned char>(nf0)), noe(0) {}
forceinline Edge**
Node::adj(void) {
return &e;
}
forceinline Edge*
Node::first(void) const {
return fst;
}
forceinline Edge*
Node::last(void) const {
return lst;
}
forceinline void
Node::first(Edge* p) {
fst = p;
}
forceinline void
Node::last(Edge* p) {
lst = p;
}
forceinline bool
Node::type(void) const {
return (nf & NF_VAL) != 0;
}
forceinline Edge*
Node::inedge(void) const {
return ie;
}
forceinline void
Node::inedge(Edge* p) {
ie = p;
}
forceinline bool
Node::removed(void) const {
return noe == 0;
}
forceinline void
Node::index(int i) {
idx = i;
}
forceinline int
Node::index(void) const {
return idx;
}
forceinline void*
Node::operator new(size_t s, Space& home) {
return home.ralloc(s);
}
/*
* Variable nodes
*
*/
forceinline
VarNode::VarNode(void) {}
forceinline
VarNode::VarNode(int x) :
Node(NF_NONE,x), ubm(NULL), lbm(NULL) {}
forceinline bool
VarNode::matched(BC bc) const {
if (bc == UBC)
return (nf & NF_M_UBC) != 0;
else
return (nf & NF_M_LBC) != 0;
}
forceinline void
VarNode::match(BC bc) {
if (bc == UBC)
nf |= NF_M_UBC;
else
nf |= NF_M_LBC;
}
forceinline void
VarNode::set_match(BC bc, Edge* p) {
if (bc == UBC)
ubm = p;
else
lbm = p;
}
forceinline void
VarNode::unmatch(BC bc) {
if (bc == UBC) {
nf &= ~NF_M_UBC; ubm = NULL;
} else {
nf &= ~NF_M_LBC; lbm = NULL;
}
}
forceinline Edge*
VarNode::get_match(BC bc) const {
if (bc == UBC)
return ubm;
else
return lbm;
}
/*
* Value nodes
*
*/
forceinline
ValNode::ValNode(void) {}
forceinline
ValNode::ValNode(int min, int max, int value,
int kidx, int kshift, int count) :
Node(NF_VAL,kshift), _klb(min), _kub(max), _kidx(kidx), _kcount(count),
noc(0),
lb(min), ublow(max), ub(max),
val(value) {}
forceinline void
ValNode::maxlow(int i) {
assert(i >= lb);
ublow = i;
}
forceinline int
ValNode::maxlow(void) const {
if (_klb == _kub) {
assert(ublow == lb);
}
return ublow;
}
forceinline void
ValNode::card_conflict(int c) {
noc = c;
}
forceinline void
ValNode::red_conflict(void) {
noc--;
assert(noc >= 0);
}
forceinline int
ValNode::card_conflict(void) const {
return noc;
}
forceinline int
ValNode::cap(BC bc) const {
if (bc == UBC)
return ub;
else
return lb;
}
forceinline bool
ValNode::matched(BC bc) const {
return cap(bc) == 0;
}
forceinline void
ValNode::reset(void) {
lb = _klb;
ublow = _kub;
ub = _kub;
noe = 0;
}
forceinline int
ValNode::kbound(BC bc) const {
if (bc == UBC) {
return _kub;
} else {
return _klb;
}
}
forceinline int
ValNode::kmax(void) const {
return _kub;
}
forceinline int
ValNode::kmin(void) const {
return _klb;
}
forceinline void
ValNode::kmin(int klb) {
_klb = klb;
}
forceinline void
ValNode::kmax(int kub) {
_kub = kub;
}
forceinline void
ValNode::dec(BC bc) {
if (bc == UBC) {
ub--;
} else {
lb--; ublow--;
}
}
forceinline void
ValNode::inc(BC bc) {
if (bc == UBC) {
ub++;
} else {
lb++; ublow++;
}
}
forceinline void
ValNode::match(BC bc) {
dec(bc);
}
forceinline void
ValNode::unmatch(BC bc) {
inc(bc);
}
forceinline void
ValNode::cap(BC bc, int c) {
if (bc == UBC)
ub = c;
else
lb = c;
}
forceinline void
ValNode::inc(void) {
_kcount++;
}
forceinline int
ValNode::kcount(void) const {
return _kcount;
}
forceinline void
ValNode::kcount(int c) {
_kcount = c;
}
forceinline void
ValNode::kindex(int i) {
_kidx = i;
}
forceinline int
ValNode::kindex(void) const {
return _kidx;
}
/// Returs the number of incident matching edges on the node
forceinline int
ValNode::incid_match(BC bc) const {
if (bc == LBC)
return _kub - ublow + _kcount;
else
return _kub - ub + _kcount;
}
forceinline bool
ValNode::sink(void) const {
// there are only incoming edges
// in case of the UBC-matching
return _kub - ub == noe;
}
forceinline bool
ValNode::source(void) const {
// there are only incoming edges
// in case of the UBC-matching
return _klb - lb == noe;
}
/*
* Edges
*
*/
forceinline void
Edge::unlink(void) {
// unlink from variable side
Edge* p = prev_edge;
Edge* n = next_edge;
if (p != NULL)
*p->next_ref() = n;
if (n != NULL)
*n->prev_ref() = p;
if (this == x->first()) {
Edge** ref = x->adj();
*ref = n;
x->first(n);
}
if (this == x->last())
x->last(p);
// unlink from value side
Edge* pv = prev_vedge;
Edge* nv = next_vedge;
if (pv != NULL)
*pv->vnext_ref() = nv;
if (nv != NULL)
*nv->vprev_ref() = pv;
if (this == v->first()) {
Edge** ref = v->adj();
*ref = nv;
v->first(nv);
}
if (this == v->last())
v->last(pv);
}
forceinline
Edge::Edge(VarNode* var, ValNode* val) :
x(var), v(val),
next_edge(NULL), prev_edge(NULL),
next_vedge(NULL), prev_vedge(NULL), ef(EF_NONE) {}
forceinline void
Edge::use(BC bc) {
if (bc == UBC)
ef |= EF_MRKUB;
else
ef |= EF_MRKLB;
}
forceinline void
Edge::free(BC bc) {
if (bc == UBC)
ef &= ~EF_MRKUB;
else
ef &= ~EF_MRKLB;
}
forceinline bool
Edge::used(BC bc) const {
if (bc == UBC)
return (ef & EF_MRKUB) != 0;
else
return (ef & EF_MRKLB) != 0;
}
forceinline Edge*
Edge::next(void) const {
return next_edge;
}
forceinline Edge*
Edge::next(bool t) const {
if (t) {
return next_vedge;
} else {
return next_edge;
}
}
forceinline Edge*
Edge::vnext(void) const {
return next_vedge;
}
forceinline Edge**
Edge::vnext_ref(void) {
return &next_vedge;
}
forceinline Edge*
Edge::prev(void) const {
return prev_edge;
}
forceinline Edge**
Edge::prev_ref(void) {
return &prev_edge;
}
forceinline Edge*
Edge::vprev(void) const {
return prev_vedge;
}
forceinline Edge**
Edge::vprev_ref(void) {
return &prev_vedge;
}
forceinline Edge**
Edge::next_ref(void) {
return &next_edge;
}
forceinline VarNode*
Edge::getVar(void) const {
assert(x != NULL);
return x;
}
forceinline ValNode*
Edge::getVal(void) const {
assert(v != NULL);
return v;
}
forceinline Node*
Edge::getMate(bool type) const {
if (type)
return x;
else
return v;
}
forceinline void
Edge::unmatch(BC bc) {
if (bc == UBC)
ef &= ~EF_UM;
else
ef &= ~EF_LM;
x->unmatch(bc); v->unmatch(bc);
}
forceinline void
Edge::unmatch(BC bc, bool node) {
if (bc == UBC)
ef &= ~EF_UM;
else
ef &= ~EF_LM;
if (node)
v->unmatch(bc);
else
x->unmatch(bc);
}
forceinline void
Edge::reset(BC bc) {
free(bc); unmatch(bc);
}
forceinline void
Edge::match(BC bc) {
if (bc == UBC)
ef |= EF_UM;
else
ef |= EF_LM;
x->match(bc);
x->set_match(bc,this);
v->match(bc);
}
forceinline bool
Edge::matched(BC bc) const {
if (bc == UBC)
return (ef & EF_UM) != 0;
else
return (ef & EF_LM) != 0;
}
forceinline void
Edge::del_edge(void) {
ef |= EF_DEL;
}
forceinline void
Edge::insert_edge(void) {
ef &= ~EF_DEL;
}
forceinline bool
Edge::deleted(void) const {
return (ef & EF_DEL) != 0;
}
forceinline void*
Edge::operator new(size_t s, Space& home) {
return home.ralloc(s);
}
/*
* Variable value graph
*
*/
template<class Card>
VarValGraph<Card>::VarValGraph(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k,
int smin, int smax)
: n_var(x.size()),
n_val(k.size()),
n_node(n_var + n_val),
sum_min(smin),
sum_max(smax) {
unsigned int noe = 0;
for (int i=x.size(); i--; )
noe += x[i].size();
vars = home.alloc<VarNode*>(n_var);
vals = home.alloc<ValNode*>(n_val);
for (int i = n_val; i--; ) {
int kmi = k[i].min();
int kma = k[i].max();
int kc = k[i].counter();
if (kc != kma) {
if (kmi >= kc) {
kmi -=kc;
assert(kmi >=0);
} else {
kmi = 0;
}
kma -= kc;
assert (kma > 0);
vals[i] = new (home)
ValNode(kmi, kma, k[i].card(), i, i + n_var, kc);
} else {
vals[i] = new (home)
ValNode(0, 0, k[i].card(), i, i + n_var, kc);
}
}
for (int i = n_var; i--; ) {
vars[i] = new (home) VarNode(i);
// get the space for the edges of the varnode
Edge** xadjacent = vars[i]->adj();
int j = 0;
for (ViewValues<IntView> xi(x[i]); xi(); ++xi) {
// get the correct index for the value
while(vals[j]->val < xi.val())
j++;
*xadjacent = new (home) Edge(vars[i],vals[j]);
vars[i]->noe++;
if (vars[i]->first() == NULL)
vars[i]->first(*xadjacent);
Edge* oldprev = vars[i]->last();
vars[i]->last(*xadjacent);
*vars[i]->last()->prev_ref() = oldprev;
if (vals[j]->first() == NULL) {
vals[j]->first(*xadjacent);
vals[j]->last(*xadjacent);
} else {
Edge* old = vals[j]->first();
vals[j]->first(*xadjacent);
*vals[j]->first()->vnext_ref() = old;
*old->vprev_ref() = vals[j]->first();
}
vals[j]->noe++;
xadjacent = (*xadjacent)->next_ref();
}
*xadjacent = NULL;
}
}
template<class Card>
inline ExecStatus
VarValGraph<Card>::min_require(Space& home,
ViewArray<IntView>& x,
ViewArray<Card>& k) {
for (int i = n_val; i--; ) {
ValNode* vln = vals[i];
if (vln->noe > 0) {
if (k[i].min() == vln->noe) {
// all variable nodes reachable from vln should be equal to vln->val
for (Edge* e = vln->first(); e != NULL; e = e->vnext()) {
VarNode* vrn = e->getVar();
for (Edge* f = vrn->first(); f != NULL; f = f->next())
if (f != e) {
ValNode* w = f->getVal();
w->noe--;
vrn->noe--;
f->del_edge();
f->unlink();
}
assert(vrn->noe == 1);
int vi = vrn->index();
GECODE_ME_CHECK(x[vi].eq(home, vln->val));
vars[vi] = vars[--n_var];
vars[vi]->index(vi);
x.move_lst(vi);
n_node--;
vln->noe--;
}
int vidx = vln->kindex();
if (Card::propagate)
GECODE_ME_CHECK(k[vidx].eq(home, k[vidx].min()));
k[vidx].counter(k[vidx].min());
vln->cap(UBC,0);
vln->cap(LBC,0);
vln->maxlow(0);
if (sum_min >= k[vidx].min())
sum_min -= k[vidx].min();
if (sum_max >= k[vidx].max())
sum_max -= k[vidx].max();
}
} else {
vals[i]->cap(UBC,0);
vals[i]->cap(LBC,0);
vals[i]->maxlow(0);
vals[i]->kmax(0);
vals[i]->kmin(0);
}
if (Card::propagate && (k[i].counter() == 0))
GECODE_ME_CHECK(k[i].lq(home, vals[i]->noe));
}
for (int i = n_val; i--; )
vals[i]->index(n_var + i);
return ES_OK;
}
template<class Card>
inline ExecStatus
VarValGraph<Card>::sync(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k) {
Region r(home);
// A node can be pushed twice (once when checking cardinality and later again)
NodeStack re(r,2*n_node);
// synchronize cardinality variables
if (Card::propagate) {
for (int i = n_val; i--; ) {
ValNode* v = vals[i];
int inc_ubc = v->incid_match(UBC);
int inc_lbc = v->incid_match(LBC);
if (v->noe == 0) {
inc_ubc = 0;
inc_lbc = 0;
}
int rm = v->kmax() - k[i].max();
// the cardinality bounds have been modified
if ((k[i].max() < v->kmax()) || (k[i].min() > v->kmin())) {
if ((k[i].max() != k[i].counter()) || (k[i].max() == 0)) {
// update the bounds
v->kmax(k[i].max());
v->kmin(k[i].min());
//everything is fine
if (inc_ubc <= k[i].max()) {
// adjust capacities
v->cap(UBC, k[i].max() - inc_ubc);
v->maxlow(k[i].max() - inc_lbc);
if (v->kmin() == v->kmax())
v->cap(LBC, k[i].max() - inc_lbc);
} else {
// set cap to max and resolve conflicts on view side
// set to full capacity for later rescheduling
if (v->cap(UBC))
v->cap(UBC,k[i].max());
v->maxlow(k[i].max() - (inc_lbc));
if (v->kmin() == v->kmax())
v->cap(LBC,k[i].max() - (inc_lbc));
v->card_conflict(rm);
}
}
}
if (inc_lbc < k[i].min() && v->noe > 0) {
v->cap(LBC, k[i].min() - inc_lbc);
re.push(v);
}
}
for (int i = n_var; i--; ) {
Edge* mub = vars[i]->get_match(UBC);
if (mub != NULL) {
ValNode* vu = mub->getVal();
if ((vars[i]->noe != 1) && vu->card_conflict()) {
vu->red_conflict();
mub->unmatch(UBC,vars[i]->type());
re.push(vars[i]);
}
}
}
}
// go on with synchronization
assert(x.size() == n_var);
for (int i = n_var; i--; ) {
VarNode* vrn = vars[i];
if (static_cast<int>(x[i].size()) != vrn->noe) {
// if the variable is already assigned
if (x[i].assigned()) {
int v = x[i].val();
Edge* mub = vrn->get_match(UBC);
if ((mub != NULL) && (v != mub->getVal()->val)) {
mub->unmatch(UBC);
re.push(vars[i]);
}
Edge* mlb = vrn->get_match(LBC);
if (mlb != NULL) {
ValNode* vln = mlb->getVal();
if (v != vln->val) {
mlb->unmatch(LBC);
if (vln->incid_match(LBC) < vln->kmin())
re.push(vln);
}
}
for (Edge* e = vrn->first(); e != NULL; e = e->next()) {
ValNode* vln = e->getVal();
if (vln->val != v) {
vrn->noe--;
e->getVal()->noe--;
e->del_edge();
e->unlink();
}
}
} else {
// delete the edge
ViewValues<IntView> xiter(x[i]);
Edge* mub = vrn->get_match(UBC);
Edge* mlb = vrn->get_match(LBC);
Edge** p = vrn->adj();
Edge* e = *p;
do {
// search the edge that has to be deleted
while (e != NULL && (e->getVal()->val < xiter.val())) {
// Skip edge
e->getVal()->noe--;
vrn->noe--;
e->del_edge();
e->unlink();
e = e ->next();
*p = e;
}
assert(xiter.val() == e->getVal()->val);
// This edge must be kept
e->free(UBC);
e->free(LBC);
++xiter;
p = e->next_ref();
e = e->next();
} while (xiter());
*p = NULL;
while (e) {
e->getVar()->noe--;
e->getVal()->noe--;
e->del_edge();
e->unlink();
e = e->next();
}
if ((mub != NULL) && mub->deleted()) {
mub->unmatch(UBC);
re.push(vars[i]);
}
//lower bound matching can be zero
if ((mlb != NULL) && mlb->deleted()) {
ValNode* vln = mlb->getVal();
mlb->unmatch(LBC);
if (vln->incid_match(LBC) < vln->kmin())
re.push(vln);
}
}
}
vars[i]->index(i);
}
for (int i = n_val; i--; ) {
if ((k[i].min() > vals[i]->noe) && (k[i].counter() == 0))
return ES_FAILED;
vals[i]->index(n_var + i);
}
// start repair
while (!re.empty()) {
Node* n = re.pop();
if (!n->removed()) {
if (!n->type()) {
VarNode* vrn = static_cast<VarNode*>(n);
if (!vrn->matched(UBC) && !augmenting_path<UBC>(home,vrn))
return ES_FAILED;
} else {
ValNode* vln = static_cast<ValNode*>(n);
while (!vln->matched(LBC))
if (!augmenting_path<LBC>(home,vln))
return ES_FAILED;
}
}
}
return ES_OK;
}
template<class Card> template<BC bc>
inline ExecStatus
VarValGraph<Card>::narrow(Space& home,
ViewArray<IntView>& x, ViewArray<Card>& k) {
for (int i = n_var; i--; )
if (vars[i]->noe == 1) {
ValNode* v = vars[i]->first()->getVal();
vars[i]->first()->free(bc);
GECODE_ME_CHECK(x[i].eq(home, v->val));
v->inc();
}
for (int i = n_val; i--; ) {
ValNode* v = vals[i];
if (Card::propagate && (k[i].counter() == 0))
GECODE_ME_CHECK(k[i].lq(home, v->noe));
if (v->noe > 0) {
if (Card::propagate)
GECODE_ME_CHECK(k[i].lq(home, v->noe));
// If the maximum number of occurences of a value is reached
// it cannot be consumed by another view
if (v->kcount() == v->kmax()) {
int vidx = v->kindex();
k[i].counter(v->kcount());
if (Card::propagate)
GECODE_ME_CHECK(k[i].eq(home, k[i].counter()));
bool delall = v->card_conflict() && (v->noe > v->kmax());
for (Edge* e = v->last(); e != NULL; e = e->vprev()) {
VarNode* vrn = e->getVar();
if (vrn->noe == 1) {
vrn->noe--;
v->noe--;
int vi= vrn->index();
x.move_lst(vi);
vars[vi] = vars[--n_var];
vars[vi]->index(vi);
n_node--;
e->del_edge();
e->unlink();
} else if (delall) {
GECODE_ME_CHECK(x[vrn->index()].nq(home, v->val));
vrn->noe--;
v->noe--;
e->del_edge();
e->unlink();
}
}
v->cap(UBC,0);
v->cap(LBC,0);
v->maxlow(0);
if (sum_min >= k[vidx].min())
sum_min -= k[vidx].min();
if (sum_max >= k[vidx].max())
sum_max -= k[vidx].max();
} else if (v->kcount() > 0) {
v->kcount(0);
}
}
}
for (int i = n_var; i--; )
vars[i]->index(i);
for (int i = n_val; i--; ) {
if (vals[i]->noe == 0) {
vals[i]->cap(UBC,0);
vals[i]->cap(LBC,0);
vals[i]->maxlow(0);
}
vals[i]->index(n_var + i);
}
for (int i = n_var; i--; ) {
if (vars[i]->noe > 1) {
for (Edge* e = vars[i]->first(); e != NULL; e = e->next()) {
if (!e->matched(bc) && !e->used(bc)) {
GECODE_ME_CHECK(x[i].nq(home, e->getVal()->val));
} else {
e->free(bc);
}
}
}
}
return ES_OK;
}
template<class Card> template<BC bc>
forceinline bool
VarValGraph<Card>::augmenting_path(Space& home, Node* v) {
Region r(home);
NodeStack ns(r,n_node);
BitSet visited(r,static_cast<unsigned int>(n_node));
Edge** start = r.alloc<Edge*>(n_node);
// keep track of the nodes that have already been visited
Node* sn = v;
// mark the start partition
bool sp = sn->type();
// nodes in sp only follow free edges
// nodes in V - sp only follow matched edges
for (int i = n_node; i--; )
if (i >= n_var) {
vals[i-n_var]->inedge(NULL);
start[i] = vals[i-n_var]->first();
} else {
vars[i]->inedge(NULL);
start[i] = vars[i]->first();
}
v->inedge(NULL);
ns.push(v);
visited.set(static_cast<unsigned int>(v->index()));
while (!ns.empty()) {
Node* v = ns.top();
Edge* e = NULL;
if (v->type() == sp) {
e = start[v->index()];
while ((e != NULL) && e->matched(bc))
e = e->next(v->type());
} else {
e = start[v->index()];
while ((e != NULL) && !e->matched(bc))
e = e->next(v->type());
start[v->index()] = e;
}
if (e != NULL) {
start[v->index()] = e->next(v->type());
Node* w = e->getMate(v->type());
if (!visited.get(static_cast<unsigned int>(w->index()))) {
// unexplored path
bool m = w->type() ?
static_cast<ValNode*>(w)->matched(bc) :
static_cast<VarNode*>(w)->matched(bc);
if (!m && w->type() != sp) {
if (v->inedge() != NULL) {
// augmenting path of length l > 1
e->match(bc);
break;
} else {
// augmenting path of length l = 1
e->match(bc);
ns.pop();
return true;
}
} else {
w->inedge(e);
visited.set(static_cast<unsigned int>(w->index()));
// find matching edge m incident with w
ns.push(w);
}
}
} else {
// tried all outgoing edges without finding an augmenting path
ns.pop();
}
}
bool pathfound = !ns.empty();
while (!ns.empty()) {
Node* t = ns.pop();
if (t != sn) {
Edge* in = t->inedge();
if (t->type() != sp) {
in->match(bc);
} else if (!sp) {
in->unmatch(bc,!sp);
} else {
in->unmatch(bc);
}
}
}
return pathfound;
}
template<class Card> template<BC bc>
inline ExecStatus
VarValGraph<Card>::maximum_matching(Space& home) {
int card_match = 0;
// find an intial matching in O(n*d)
// greedy algorithm
for (int i = n_val; i--; )
for (Edge* e = vals[i]->first(); e != NULL ; e = e->vnext())
if (!e->getVar()->matched(bc) && !vals[i]->matched(bc)) {
e->match(bc); card_match++;
}
Region r(home);
switch (bc) {
case LBC:
if (card_match < sum_min) {
Support::StaticStack<ValNode*,Region> free(r,n_val);
// find failed nodes
for (int i = n_val; i--; )
if (!vals[i]->matched(LBC))
free.push(vals[i]);
while (!free.empty()) {
ValNode* v = free.pop();
while (!v->matched(LBC))
if (augmenting_path<LBC>(home,v))
card_match++;
else
break;
}
return (card_match >= sum_min) ? ES_OK : ES_FAILED;
} else {
return ES_OK;
}
break;
case UBC:
if (card_match < n_var) {
Support::StaticStack<VarNode*,Region> free(r,n_var);
// find failed nodes
for (int i = n_var; i--; )
if (!vars[i]->matched(UBC))
free.push(vars[i]);
while (!free.empty()) {
VarNode* v = free.pop();
if (!v->matched(UBC) && augmenting_path<UBC>(home,v))
card_match++;
}
return (card_match >= n_var) ? ES_OK : ES_FAILED;
} else {
return ES_OK;
}
break;
default: GECODE_NEVER;
}
GECODE_NEVER;
return ES_FAILED;
}
template<class Card> template<BC bc>
forceinline void
VarValGraph<Card>::free_alternating_paths(Space& home) {
Region r(home);
NodeStack ns(r,n_node);
BitSet visited(r,static_cast<unsigned int>(n_node));
switch (bc) {
case LBC:
// after a maximum matching on the value nodes there still can be
// free value nodes, hence we have to consider ALL nodes whether
// they are the starting point of an even alternating path in G
for (int i = n_var; i--; )
if (!vars[i]->matched(LBC)) {
ns.push(vars[i]);
visited.set(static_cast<unsigned int>(vars[i]->index()));
}
for (int i = n_val; i--; )
if (!vals[i]->matched(LBC)) {
ns.push(vals[i]);
visited.set(static_cast<unsigned int>(vals[i]->index()));
}
break;
case UBC:
// clearly, after a maximum matching on the x variables
// corresponding to a set cover on x there are NO free var nodes
for (int i = n_val; i--; )
if (!vals[i]->matched(UBC)) {
ns.push(vals[i]);
visited.set(static_cast<unsigned int>(vals[i]->index()));
}
break;
default: GECODE_NEVER;
}
while (!ns.empty()) {
Node* node = ns.pop();
if (node->type()) {
// ValNode
ValNode* vln = static_cast<ValNode*>(node);
for (Edge* cur = vln->first(); cur != NULL; cur = cur->vnext()) {
VarNode* mate = cur->getVar();
switch (bc) {
case LBC:
if (cur->matched(LBC)) {
// mark the edge
cur->use(LBC);
if (!visited.get(static_cast<unsigned int>(mate->index()))) {
ns.push(mate);
visited.set(static_cast<unsigned int>(mate->index()));
}
}
break;
case UBC:
if (!cur->matched(UBC)) {
// mark the edge
cur->use(UBC);
if (!visited.get(static_cast<unsigned int>(mate->index()))) {
ns.push(mate);
visited.set(static_cast<unsigned int>(mate->index()));
}
}
break;
default: GECODE_NEVER;
}
}
} else {
// VarNode
VarNode* vrn = static_cast<VarNode*>(node);
switch (bc) {
case LBC:
// after LBC-matching we can follow every unmatched edge
for (Edge* cur = vrn->first(); cur != NULL; cur = cur->next()) {
ValNode* mate = cur->getVal();
if (!cur->matched(LBC)) {
cur->use(LBC);
if (!visited.get(static_cast<unsigned int>(mate->index()))) {
ns.push(mate);
visited.set(static_cast<unsigned int>(mate->index()));
}
}
}
break;
case UBC:
// after UBC-matching we can only follow a matched edge
{
Edge* cur = vrn->get_match(UBC);
if (cur != NULL) {
cur->use(UBC);
ValNode* mate = cur->getVal();
if (!visited.get(static_cast<unsigned int>(mate->index()))) {
ns.push(mate);
visited.set(static_cast<unsigned int>(mate->index()));
}
}
}
break;
default: GECODE_NEVER;
}
}
}
}
template<class Card> template<BC bc>
void
VarValGraph<Card>::dfs(Node* v,
BitSet& inscc, BitSet& in_unfinished, int dfsnum[],
NodeStack& roots, NodeStack& unfinished,
int& count) {
count++;
int v_index = v->index();
dfsnum[v_index] = count;
inscc.set(static_cast<unsigned int>(v_index));
in_unfinished.set(static_cast<unsigned int>(v_index));
unfinished.push(v);
roots.push(v);
for (Edge* e = v->first(); e != NULL; e = e->next(v->type())) {
bool m;
switch (bc) {
case LBC:
m = v->type() ? e->matched(LBC) : !e->matched(LBC);
break;
case UBC:
m = v->type() ? !e->matched(UBC) : e->matched(UBC);
break;
default: GECODE_NEVER;
}
if (m) {
Node* w = e->getMate(v->type());
int w_index = w->index();
assert(w_index < n_node);
if (!inscc.get(static_cast<unsigned int>(w_index))) {
// w is an uncompleted scc
w->inedge(e);
dfs<bc>(w, inscc, in_unfinished, dfsnum,
roots, unfinished, count);
} else if (in_unfinished.get(static_cast<unsigned int>(w_index))) {
// even alternating cycle found mark the edge closing the cycle,
// completing the scc
e->use(bc);
// if w belongs to an scc we detected earlier
// merge components
assert(roots.top()->index() < n_node);
while (dfsnum[roots.top()->index()] > dfsnum[w_index]) {
roots.pop();
}
}
}
}
if (v == roots.top()) {
while (v != unfinished.top()) {
// w belongs to the scc with root v
Node* w = unfinished.top();
w->inedge()->use(bc);
in_unfinished.clear(static_cast<unsigned int>(w->index()));
unfinished.pop();
}
assert(v == unfinished.top());
in_unfinished.clear(static_cast<unsigned int>(v_index));
roots.pop();
unfinished.pop();
}
}
template<class Card> template<BC bc>
forceinline void
VarValGraph<Card>::strongly_connected_components(Space& home) {
Region r(home);
BitSet inscc(r,static_cast<unsigned int>(n_node));
BitSet in_unfinished(r,static_cast<unsigned int>(n_node));
int* dfsnum = r.alloc<int>(n_node);
for (int i = n_node; i--; )
dfsnum[i]=0;
int count = 0;
NodeStack roots(r,n_node);
NodeStack unfinished(r,n_node);
for (int i = n_var; i--; )
dfs<bc>(vars[i], inscc, in_unfinished, dfsnum,
roots, unfinished, count);
}
template<class Card>
forceinline void*
VarValGraph<Card>::operator new(size_t t, Space& home) {
return home.ralloc(t);
}
}}}
// STATISTICS: int-prop
|