tclbdd

Check-in [75bd8ee9ed]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Shorten parameter lists of internal routines by keeping state in the BDD_System object. Start drafting ApplyAndQuantify - but it's not right, so define it away.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:75bd8ee9edade22e67ca856933f890873716c349
User & Date: kbk 2013-12-10 04:30:26
Context
2013-12-10
12:25
Broke off the easy cases from Apply (to be used in ApplyAndQuantify) check-in: 3016cbe3d8 user: kbk tags: trunk
04:30
Shorten parameter lists of internal routines by keeping state in the BDD_System object. Start drafting ApplyAndQuantify - but it's not right, so define it away. check-in: 75bd8ee9ed user: kbk tags: trunk
2013-12-09
03:38
Finished and debugged implementation and test suite of 'compose', plus fixed several additional memory management bugs. check-in: fec3a9366b user: kbk tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/bdd.c.

613
614
615
616
617
618
619
620

621
622
623
624
625
626
627
...
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
....
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
....
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087


1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
....
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
....
1147
1148
1149
1150
1151
1152
1153


















1154
1155
1156
1157
1158
1159
1160
....
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
....
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
....
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252



1253
1254
1255
1256
1257
1258
1259
1260
1261
....
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
....
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
....
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
....
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
....
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
....
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
....
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
....
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
....
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
....
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
....
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
....
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
....
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
....
1795
1796
1797
1798
1799
1800
1801
1802

1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
....
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
....
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095


2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
....
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155


2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
 * Side effects:
 *	None.
 *
 *-----------------------------------------------------------------------------
 */

BDD_VariableIndex
BDD_GetVariableCount(BDD_System* sysPtr)

{
    return sysPtr->beads[0].level;
}
 
/*
 *-----------------------------------------------------------------------------
 *
................................................................................
 *	unbridled longjmp'ing. For the moment, we ignore the problem.
 *
 *-----------------------------------------------------------------------------
 */

static inline int
GrowSystemAndRehash(
    BDD_System* sysPtr)
{
    BDD_BeadIndex oldAlloc = sysPtr->beadsAlloc;
				/* Former size of the bead table */
    BDD_BeadIndex newAlloc = 2 * oldAlloc;
				/* New size of the bead table */
    Bead* beads;		/* New bead table */
    BDD_BeadIndex hashSize = sysPtr->hashSize * 2;
................................................................................
 *	vice versa.
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Negate(
    Tcl_HashTable* H,		/* Negations already computed */
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u)		/* BDD to negate */
{
    Tcl_HashEntry* entryPtr;	/* Hash entry for precomputed negation */
    int newFlag;		/* Flag == 1 if no precomputed negation found */
    Bead* uPtr = sysPtr->beads+u;
				/* Pointer to the bead data */
................................................................................
    BDD_BeadIndex l, h;		/* Low and high transitions of the result */
    BDD_BeadIndex result;	/* Resulting BDD */

    /*
     * Handle constants
     */

    entryPtr = Tcl_CreateHashEntry(H, u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[result].refCount;
    } else if (u <= 1) {
	result = !u;
	++sysPtr->beads[result].refCount;
    } else {
	l = Negate(H, sysPtr, uPtr->low);
	h = Negate(H, sysPtr, uPtr->high);
	result = BDD_MakeBead(sysPtr, uPtr->level, l, h);
	BDD_UnrefBead(sysPtr, l);
	BDD_UnrefBead(sysPtr, h);
    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    return result;
}
BDD_BeadIndex
BDD_Negate(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u)		/* BDD to negate */
{
    Tcl_HashTable H;		/* Hash table of precomputed results */
    BDD_BeadIndex result;	/* Negation of the given BDD */
    Tcl_InitHashTable(&H, TCL_ONE_WORD_KEYS);
    result = Negate(&H, sysPtr, u);
    Tcl_DeleteHashTable(&H);
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Apply --
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Apply(
    BDD_System* sysPtr,		/* System of BDD's */
    Tcl_HashTable* G,		/* Hash table of partial results */
    BDD_BinOp op,		/* Operation to apply */
    BDD_BeadIndex u1,		/* Left operand */
    BDD_BeadIndex u2) 		/* Right operand */
{


    Bead* beads = sysPtr->beads; /* Bead table */
    BDD_BeadIndex u[2];		 /* Bead indices for left- and right-hand 
				  * sides */
    Bead* u1Ptr = beads + u1;	/* Pointer to the left-hand bead */
    Bead* u2Ptr = beads + u2;	/* Pointer to the right-hand bead */
    BDD_VariableIndex level;	/* Level of the result */
    BDD_BeadIndex low1, high1;	/* Low and high transitions of the 
				 * left-hand bead */
    BDD_BeadIndex low2, high2;	/* Low and high transitions of the
				 * right-hand bead*/
................................................................................
				 * cache of beads for this operation */
    BDD_BinOp opmask;		/* Mask of relevant bits for this operation */

    /* Check if the result is already hashed */

    u[0] = u1;
    u[1] = u2;
    entry = Tcl_CreateHashEntry(G, u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
    } else {

	/* 
	 * Check if the result is constant or a copy of one of the operands
................................................................................
	    ++sysPtr->beads[result].refCount;
	} else if ((op & opmask) == (0xA & opmask)) {
	    /*
	     * Result is the right operand
	     */
	    result = u2;
	    ++sysPtr->beads[result].refCount;


















	} else {
	    
	    /*
	     * Result is not constant. Apply recursively to the subexpression
	     * with the earlier top variable.
	     */
	    if (u1Ptr->level == u2Ptr->level) {
................................................................................
		 * Both subexpressions have the same top variable
		 */
		level = u1Ptr->level;
		low1 = u1Ptr->low;
		high1 = u1Ptr->high;
		low2 = u2Ptr->low;
		high2 = u2Ptr->high;
		l = Apply(sysPtr, G, op, low1, low2);
		h = Apply(sysPtr, G, op, high1, high2);
	    } else if (u1Ptr->level < u2Ptr->level) {
		/*
		 * Apply first to the left-hand operand
		 */
		level = u1Ptr->level;
		low1 = u1Ptr->low;
		high1 = u1Ptr->high;
		l = Apply(sysPtr, G, op, low1, u2);
		h = Apply(sysPtr, G, op, high1, u2);
	    } else /* u1Ptr->level > u2Ptr->level */ {
		/*
		 * Apply first to the right-hand operand
		 */
		level = u2Ptr->level;
		low2 = u2Ptr->low;
		high2 = u2Ptr->high;
		l = Apply(sysPtr, G, op, u1, low2);
		h = Apply(sysPtr, G, op, u1, high2);
	    }
	    /*
	     * Compose the subexpressions
	     */
	    result = BDD_MakeBead(sysPtr, level, l, h);
	    BDD_UnrefBead(sysPtr, l);
	    BDD_UnrefBead(sysPtr, h);
................................................................................
BDD_BeadIndex
BDD_Apply(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BinOp op,		/* Operation to apply */
    BDD_BeadIndex u1,		/* Left operand */
    BDD_BeadIndex u2) 		/* Right operand */
{
    Tcl_HashTable G;		/* Cache of partial results */
    Tcl_HashSearch search;	/* Search for clearing the cache */
    Tcl_HashEntry* entryPtr;	/* Hash entyr for clearing the cache */

    Tcl_InitCustomHashTable(&G, TCL_CUSTOM_TYPE_KEYS, &Bead2KeyType);

    BDD_BeadIndex result = Apply(sysPtr, &G, op, u1, u2);
    for (entryPtr = Tcl_FirstHashEntry(&G, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&G);
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Apply3 --
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Apply3(
    BDD_System* sysPtr,		/* System of BDD's */
    Tcl_HashTable* G,		/* Hash table of partial results */
    BDD_TernOp op,		/* Operation to apply */
    BDD_BeadIndex u1,		/* First operand */
    BDD_BeadIndex u2,		/* Second operand */
    BDD_BeadIndex u3) 		/* Third operand */
{
    Bead* beads = sysPtr->beads; /* Bead table */



    BDD_BeadIndex u[3];		 /* Bead indices for left- and right-hand 
				  * sides */
    Bead* u1Ptr = beads + u1;	/* Pointer to the left-hand bead */
    Bead* u2Ptr = beads + u2;	/* Pointer to the right-hand bead */
    Bead* u3Ptr = beads + u3;	/* Pointer to the right-hand bead */
    BDD_VariableIndex level;	/* Level of the result */
    BDD_BeadIndex low1, high1;	/* Low and high transitions of the first bead */
    BDD_BeadIndex low2, high2;	/* Low and high transitions of the
				 * second bead */
................................................................................
    BDD_TernOp opfiltered;	/* Operation masked with 'opmask' */

    /* Check if the result is already hashed */

    u[0] = u1;
    u[1] = u2;
    u[2] = u3;
    entry = Tcl_CreateHashEntry(G, u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
    } else {
	/* 
	 * Check if the result is constant or equal to one of the operands
	 */
................................................................................
	    } else {
		low3 = u3Ptr->low;
		high3 = u3Ptr->high;
	    }
	    /*
	     * Compute the two branches from the top variable
	     */
	    l = Apply3(sysPtr, G, op, low1, low2, low3);
	    h = Apply3(sysPtr, G, op, high1, high2, high3);
	    /*
	     * Compose the subexpressions
	     */
	    result = BDD_MakeBead(sysPtr, level, l, h);
	    BDD_UnrefBead(sysPtr, l);
	    BDD_UnrefBead(sysPtr, h);
	}
................................................................................
BDD_Apply3(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_TernOp op,		/* Operation to apply */
    BDD_BeadIndex u1,		/* First operand */
    BDD_BeadIndex u2, 		/* Second operand */
    BDD_BeadIndex u3)		/* Third operand */
{
    Tcl_HashTable G;		/* Cache of partial results */
    Tcl_HashSearch search;	/* Search for clearing the cache */
    Tcl_HashEntry* entryPtr;	/* Hash entyr for clearing the cache */

    Tcl_InitCustomHashTable(&G, TCL_CUSTOM_TYPE_KEYS, &Bead3KeyType);

    BDD_BeadIndex result = Apply3(sysPtr, &G, op, u1, u2, u3);
    for (entryPtr = Tcl_FirstHashEntry(&G, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&G);
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Restrict --
................................................................................
 *	The restrictions must be listed in ascending order by variable
 *	number. The values of r[].value must be Boolean constants.
 *
 *-----------------------------------------------------------------------------
 */
static BDD_BeadIndex
Restrict(
    Tcl_HashTable* H,		/* Hash table of precomputed values */
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to restrict */
    const BDD_ValueAssignment r[], 
				/* Restrictions to apply */
    BDD_VariableIndex n)	/* Number of restrictions */
{
    int newFlag;		/* Flag = 1 if we have not seen u yet */
................................................................................
    if (n == 0 || u <= 1) {
	++sysPtr->beads[u].refCount;
	return u;
    }

    /* Has this value been computed already? */

    entryPtr = Tcl_CreateHashEntry(H, u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex)(size_t)Tcl_GetHashValue(entryPtr);
	if (result != !(BDD_BeadIndex)0) {
	    ++sysPtr->beads[result].refCount;
	    return result;
	}
    } else {
................................................................................
		      rvar, nextvar);
	}
    }
    if (rvar < uvar) {
	/*
	 * r[0] is an irrelevant variable in u
	 */
	result = Restrict(H, sysPtr, u, r+1, n-1);
    } else if (rvar == uvar) {
	/*
	 * r[0] appears in u. Bind it.
	 */
	if (r[0].value == 0) {
	    result = Restrict(H, sysPtr, sysPtr->beads[u].low, r+1, n-1);
	} else if (r[0].value == 1) {
	    result = Restrict(H, sysPtr, sysPtr->beads[u].high, r+1, n-1);
	} else {
	    Tcl_Panic("BDD_Restrict called with non-constant value for "
		      "variable %lu\n", rvar);
	    result = 0;		/* silence compiler warnings */
	}
    } else /* rvar > uvar */ {
	/*
	 * u's first variable is unrestricted. Apply the restriction to both
	 * successors of u, and make a bead for the restricted expression.
	 */
	l = Restrict(H, sysPtr, sysPtr->beads[u].low, r, n);
	h = Restrict(H, sysPtr, sysPtr->beads[u].high, r, n);
	result = BDD_MakeBead(sysPtr, uvar, l, h);
	BDD_UnrefBead(sysPtr, l);
	BDD_UnrefBead(sysPtr, h);
    }

    /*
     * Cache the result
................................................................................
BDD_Restrict(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to restrict */
    const BDD_ValueAssignment r[],
				/* Restrictions to apply */
    BDD_VariableIndex n)	/* Count of restrictions */
{
    Tcl_HashTable H;		/* Cache of partial results */
    BDD_BeadIndex result;	/* Bead index of the result */
    Tcl_InitHashTable(&H, TCL_ONE_WORD_KEYS);
    result = Restrict(&H, sysPtr, u, r, n);
    Tcl_DeleteHashTable(&H);
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Quantify --
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Quantify(
    BDD_System* sysPtr,		/* Pointer to the system of BDD's */
    Tcl_HashTable* G,	        /* Cached partial results of Apply */
    Tcl_HashTable* H,		/* Cached partial results of Quantify */
    BDD_Quantifier q,		/* Quantifier to apply */
    const BDD_VariableIndex* v,	/* Variables to quantify */
    BDD_VariableIndex n,	/* Number of quantified variables */
    BDD_BeadIndex u)		/* Expression to quantify */
{
    BDD_BeadIndex l;		/* Low transition of the result */
    BDD_BeadIndex h;		/* High transition of the result */
    BDD_BeadIndex r;		/* Return value */
    int newFlag;		/* Flag == 1 iff the result was not cached */


    Tcl_HashEntry* entryPtr;	/* Pointer to the hash entry for
				 * a cached result */

    /* Check for a cached result */

    entryPtr = Tcl_CreateHashEntry(H, (ClientData) u, &newFlag);
    if (!newFlag) {
	r = (BDD_BeadIndex) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[r].refCount;
	return r;
    }

    for (;;) {
................................................................................
	    r = u;
	    break;
	} else if (beadPtr->level < *v) {
	    /*
	     * The current variable in the expression is unquantified.
	     * Quantify the two subexpressions and make the result
	     */
	    l = Quantify(sysPtr, G, H, q, v, n, beadPtr->low);
	    h = Quantify(sysPtr, G, H, q, v, n, beadPtr->high);
	    r = BDD_MakeBead(sysPtr, beadPtr->level, l, h);
	    BDD_UnrefBead(sysPtr,h);
	    BDD_UnrefBead(sysPtr,l);
	    break;
	} else if (beadPtr->level == *v) {
	    /*
	     * The current variable in the expression is quantified.
	     * Quantify the two subexpressions with respect to the
	     * remaining variables and then apply the combining operation.
	     */
	    l = Quantify(sysPtr, G, H, q, v+1, n-1, beadPtr->low);
	    h = Quantify(sysPtr, G, H, q, v+1, n-1, beadPtr->high);

	    r = Apply(sysPtr, G, q, l, h);
	    BDD_UnrefBead(sysPtr,h);
	    BDD_UnrefBead(sysPtr,l);
	    break;
	} else {
	    /* 
	     * The current variable does not appear free in the expression,
	     * The quantification is trivially satisfied with respect to the
	     * variable in question. Advance to the next variable.



	     */
	    ++v;
	    --n;
	}
    }
    
    /*
................................................................................
    Tcl_SetHashValue(entryPtr, (ClientData) r);
    return r;
}
BDD_BeadIndex
BDD_Quantify(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_Quantifier q,		/* Quantifier to apply */
    const BDD_VariableIndex* v,	/* List of variables to quantify */
    BDD_VariableIndex n,	/* Number of variables to quantify */

    BDD_BeadIndex e)		/* Expression to quantify */
{
    Tcl_HashTable G;	        /* Hash table to cache partial results
				 * of Apply */
    Tcl_HashTable H;		/* Hash table to cache partial results
				 * of Quantify */
    Tcl_HashSearch search;	/* Search state for clearing the hash */
    Tcl_HashEntry* entryPtr;	/* Hash table entry to be cleared */

    BDD_BeadIndex r;		/* Return value */


    Tcl_InitCustomHashTable(&G, TCL_CUSTOM_TYPE_KEYS, &Bead2KeyType);

    Tcl_InitHashTable(&H, TCL_ONE_WORD_KEYS);

    r = Quantify(sysPtr, &G, &H, q, v, n, e);

    for (entryPtr = Tcl_FirstHashEntry(&H, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&H);
    for (entryPtr = Tcl_FirstHashEntry(&G, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&G);

    return r;
}
 




















































































































































































/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Compose --
 *
 *	Computes a BDD representing an input BDD with a selection of variables
 *	replaced simultaneously with other expressions.
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Compose(
    BDD_System* sysPtr,		/* System of BDD's */
    Tcl_HashTable* G,		/* Hash table to cache partial results 
				 * of Apply3 */
    Tcl_HashTable* H,		/* Hash table to cache partial results
				 * of Compose */
    BDD_BeadIndex u,		/* Input BDD */
    BDD_VariableIndex n,	/* Number of variables in the vector */
    BDD_BeadIndex r[n])		/* Replacement terms for variables
				 * 0..n */
{
    BDD_BeadIndex result;	/* Return value */
    int newFlag;
    Tcl_HashEntry* entryPtr = Tcl_CreateHashEntry(H, (ClientData)u, &newFlag);
    Bead* uPtr = sysPtr->beads + u;
    BDD_VariableIndex level = uPtr->level;
    BDD_BeadIndex low = uPtr->low;
    BDD_BeadIndex high = uPtr->high;
    if (!newFlag) {
	result = (BDD_BeadIndex) (size_t) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[result].refCount;
	return result;
    } else if (sysPtr->beads[u].level >= n) {
	result = u;
	++sysPtr->beads[result].refCount;
    } else {
	low = Compose(sysPtr, G, H, low, n, r);
	high = Compose(sysPtr, G, H, high, n, r);
	result = Apply3(sysPtr, G, BDD_TERNOP_IFTHENELSE, r[level], high, low);
	BDD_UnrefBead(sysPtr, low);
	BDD_UnrefBead(sysPtr, high);
    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    ++sysPtr->beads[result].refCount;
    return result;
}
................................................................................
BDD_Compose(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to compose */
    BDD_VariableIndex n,	/* Number of leading variables specified */
    BDD_BeadIndex replacements[])
				/* Replacement values for first n variables */
{
    Tcl_HashTable G;		/* Partial results for Apply3 */
    Tcl_HashTable H;		/* Partial results for Compose */

    Tcl_HashSearch search;	/* Search state for clearing the hash */
    Tcl_HashEntry* entryPtr;	/* Hash table entry to be cleared */
    BDD_BeadIndex r;	/* Return value */


    Tcl_InitCustomHashTable(&G, TCL_CUSTOM_TYPE_KEYS, &Bead3KeyType);

    Tcl_InitHashTable(&H, TCL_ONE_WORD_KEYS);



    r = Compose(sysPtr, &G, &H, u, n, replacements);

    for (entryPtr = Tcl_FirstHashEntry(&H, &search);





	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    for (entryPtr = Tcl_FirstHashEntry(&G, &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&H);
    Tcl_DeleteHashTable(&G);

    return r;
}
 
/*
 *-----------------------------------------------------------------------------
 *
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static int
SatCount(
    BDD_System* sysPtr,		/* System of BDD's */
    Tcl_HashTable* hashPtr,	/* Hash table for memoized results */
    BDD_BeadIndex x,		/* BDD to enumerate */
    mp_int* count)		/* Count of satisfying variable assignments */
{
    Bead* xPtr;			/* Pointer to the bead being counted */
    Bead* lowPtr;		/* Pointer to the 0 successor */
    Bead* highPtr;		/* Pointer to the 1 successor */
    mp_int lresult;		/* Count of satisfying assignments of the
................................................................................

    /* Is the expression constant? */
    if (x <= 1) {
	return mp_set_int(count, x);
    }

    /* Is the result cached? */
    Tcl_HashEntry* entryPtr = Tcl_CreateHashEntry(hashPtr, (void*) x, &new);

    if (!new) {
	mp_int* cachedResult = Tcl_GetHashValue(entryPtr);
	return mp_copy(cachedResult, count);
    }

    mp_init_multi(&lresult, &hresult, NULL);
    xPtr = sysPtr->beads + x;
    lowPtr = sysPtr->beads + xPtr->low;
    highPtr = sysPtr->beads + xPtr->high;
    if ((status = SatCount(sysPtr, hashPtr, xPtr->low,
			   &lresult)) != MP_OKAY
	|| (status = SatCount(sysPtr, hashPtr, xPtr->high,
			      &hresult)) != MP_OKAY
	|| (status = mp_mul_2d(&lresult, lowPtr->level-xPtr->level-1,
			       &lresult)) != MP_OKAY
	|| (status = mp_mul_2d(&hresult, highPtr->level-xPtr->level-1,
			       &hresult)) != MP_OKAY
	|| (status = mp_add(&lresult, &hresult, count)) != MP_OKAY) {
	Tcl_DeleteHashEntry(entryPtr);
................................................................................
}
int
BDD_SatCount(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex x,		/* BDD to enumerate */
    mp_int* count)		/* Count of satisfying variable assignments */
{
    Tcl_HashTable hash;		/* Hash table of cached results */
    Tcl_HashEntry* cleanup;	/* Hash entry used in cleanup loop */
    Tcl_HashSearch search;	/* State of a hash table iteration */
    mp_int* v;			/* Dead value in the cache */
    int status;			/* Status return from tommath */
    Tcl_InitHashTable(&hash, TCL_ONE_WORD_KEYS);
    status = SatCount(sysPtr, &hash, x, count);
    if (status == MP_OKAY) {
	status = mp_mul_2d(count, sysPtr->beads[x].level, count);
    }
    for (cleanup = Tcl_FirstHashEntry(&hash, &search);
	 cleanup != NULL;
	 cleanup = Tcl_NextHashEntry(&search)) {
	v = (mp_int*) Tcl_GetHashValue(cleanup);
	mp_clear(v);
	ckfree(v);
	Tcl_SetHashValue(cleanup, NULL);
    }
    Tcl_DeleteHashTable(&hash);
    return status;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_AllSatStart --
................................................................................
 *	from the initial state.
 *
 *-----------------------------------------------------------------------------
 */

static int
Dump(
    Tcl_HashTable* hash,	/* Hash table of visited states */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_Obj* output,		/* Output object */
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex beadIndex)	/* Index of the bead being dumped */
{


    int newFlag;		/* Has this object been seen before? */
    Tcl_CreateHashEntry(hash, (void*) beadIndex, &newFlag);

    if (newFlag) {

	/* 
	 * Make the triple {level ifFalse ifTrue} and store it in the
	 * dictionary keyed by the bead index.
	 */
	Tcl_Obj* content = Tcl_NewObj();
................................................................................
	    return TCL_ERROR;
	}

	/*
	 * If this bead isn't a leaf, dump its successors
	 */
	if (beadIndex > 1) {
	    if (Dump(hash, interp, output, sysPtr, low) != TCL_OK
		|| Dump(hash, interp, output, sysPtr, high) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
    }
    return TCL_OK;
}
int
BDD_Dump(
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_Obj* output,		/* Output object */
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex beadIndex)	/* Index of the bead to dump */
{
    Tcl_HashTable hashTable;	/* Hash table of beads that have been seen */
    int result;			/* Tcl status return */

    /*
     * We haven't seen any beads yet.
     */
    Tcl_InitHashTable(&hashTable, TCL_ONE_WORD_KEYS);

    /*
     * Dump the BDD to the given object
     */
    Tcl_SetStringObj(output, NULL, 0);


    result = Dump(&hashTable, interp, output, sysPtr, beadIndex);

    /*
     * Clean up the hashtable
     */
    Tcl_DeleteHashTable(&hashTable);
    return result;
}
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * End:
 */







|
>







 







|







 







<







 







|







|
|












<

|
|
|







 







<
<



>
>

|
|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|







|
|







|
|







 







<


>
|
>
|
|




|







 







<
<




|
>
>
>
|
|







 







|







 







|
|







 







<


>
|
>
|
|




|







 







<







 







|







 







|





|

|










|
|







 







<

|
|
|







 







|
<
<

<






>
>




>
|







 







|
|










|
|
>
|





|
|
|
>
>
>







 







<

>


<
<
<
<





>
|
>
|

|

|




|
|




|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
|
|
|
|
|
|
|
|
|
<












|
|
|







 







<
<
<


|

>
|
>
|
>
>

|

|
>
>
>
>
>




<
<
<
<
<
|
|







 







<







 







|
>









|

|







 







<




|
|



|







|







 







<
<
<



>
>

|
<







 







|
|













<





|





>
>
|




|








613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
...
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
....
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
....
1075
1076
1077
1078
1079
1080
1081


1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
....
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
....
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
....
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
....
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
....
1257
1258
1259
1260
1261
1262
1263


1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
....
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
....
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
....
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
....
1445
1446
1447
1448
1449
1450
1451

1452
1453
1454
1455
1456
1457
1458
....
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
....
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
....
1535
1536
1537
1538
1539
1540
1541

1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
....
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
....
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
....
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
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
....
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899

1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
....
1923
1924
1925
1926
1927
1928
1929



1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952





1953
1954
1955
1956
1957
1958
1959
1960
1961
....
1974
1975
1976
1977
1978
1979
1980

1981
1982
1983
1984
1985
1986
1987
....
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
....
2032
2033
2034
2035
2036
2037
2038

2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
....
2282
2283
2284
2285
2286
2287
2288



2289
2290
2291
2292
2293
2294
2295

2296
2297
2298
2299
2300
2301
2302
....
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340

2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
 * Side effects:
 *	None.
 *
 *-----------------------------------------------------------------------------
 */

BDD_VariableIndex
BDD_GetVariableCount(
    BDD_System* sysPtr)		/* System of BDD's */
{
    return sysPtr->beads[0].level;
}
 
/*
 *-----------------------------------------------------------------------------
 *
................................................................................
 *	unbridled longjmp'ing. For the moment, we ignore the problem.
 *
 *-----------------------------------------------------------------------------
 */

static inline int
GrowSystemAndRehash(
    BDD_System* sysPtr)		/* System of BDD's */
{
    BDD_BeadIndex oldAlloc = sysPtr->beadsAlloc;
				/* Former size of the bead table */
    BDD_BeadIndex newAlloc = 2 * oldAlloc;
				/* New size of the bead table */
    Bead* beads;		/* New bead table */
    BDD_BeadIndex hashSize = sysPtr->hashSize * 2;
................................................................................
 *	vice versa.
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Negate(

    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u)		/* BDD to negate */
{
    Tcl_HashEntry* entryPtr;	/* Hash entry for precomputed negation */
    int newFlag;		/* Flag == 1 if no precomputed negation found */
    Bead* uPtr = sysPtr->beads+u;
				/* Pointer to the bead data */
................................................................................
    BDD_BeadIndex l, h;		/* Low and high transitions of the result */
    BDD_BeadIndex result;	/* Resulting BDD */

    /*
     * Handle constants
     */

    entryPtr = Tcl_CreateHashEntry(&(sysPtr->negateCache), u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[result].refCount;
    } else if (u <= 1) {
	result = !u;
	++sysPtr->beads[result].refCount;
    } else {
	l = Negate(sysPtr, uPtr->low);
	h = Negate(sysPtr, uPtr->high);
	result = BDD_MakeBead(sysPtr, uPtr->level, l, h);
	BDD_UnrefBead(sysPtr, l);
	BDD_UnrefBead(sysPtr, h);
    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    return result;
}
BDD_BeadIndex
BDD_Negate(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u)		/* BDD to negate */
{

    BDD_BeadIndex result;	/* Negation of the given BDD */
    Tcl_InitHashTable(&(sysPtr->negateCache), TCL_ONE_WORD_KEYS);
    result = Negate(sysPtr, u);
    Tcl_DeleteHashTable(&(sysPtr->negateCache));
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Apply --
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Apply(
    BDD_System* sysPtr,		/* System of BDD's */


    BDD_BeadIndex u1,		/* Left operand */
    BDD_BeadIndex u2) 		/* Right operand */
{
    BDD_BinOp op = sysPtr->applyOp;
				/* Operation to apply */
    Bead* beads = sysPtr->beads; /* Bead table */
    BDD_BeadIndex u[2];		/* Bead indices for left- and right-hand 
				 * sides */
    Bead* u1Ptr = beads + u1;	/* Pointer to the left-hand bead */
    Bead* u2Ptr = beads + u2;	/* Pointer to the right-hand bead */
    BDD_VariableIndex level;	/* Level of the result */
    BDD_BeadIndex low1, high1;	/* Low and high transitions of the 
				 * left-hand bead */
    BDD_BeadIndex low2, high2;	/* Low and high transitions of the
				 * right-hand bead*/
................................................................................
				 * cache of beads for this operation */
    BDD_BinOp opmask;		/* Mask of relevant bits for this operation */

    /* Check if the result is already hashed */

    u[0] = u1;
    u[1] = u2;
    entry = Tcl_CreateHashEntry(&(sysPtr->applyCache), u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
    } else {

	/* 
	 * Check if the result is constant or a copy of one of the operands
................................................................................
	    ++sysPtr->beads[result].refCount;
	} else if ((op & opmask) == (0xA & opmask)) {
	    /*
	     * Result is the right operand
	     */
	    result = u2;
	    ++sysPtr->beads[result].refCount;

	    /*
	     * Special cases for 'x OP x':
	     */

	} else if ((op == BDD_BINOP_AND || op == BDD_BINOP_OR) && (u1 == u2)) {
	    result = u2;
	    ++sysPtr->beads[result].refCount;
	} else if ((op == BDD_BINOP_XOR 
		    || op == BDD_BINOP_LT
		    || op == BDD_BINOP_GT) && (u1 == u2)) {
	    result = 0;
	    ++sysPtr->beads[result].refCount;
	} else if ((op == BDD_BINOP_EQ
		    || op == BDD_BINOP_LE
		    || op == BDD_BINOP_GE) && (u1 == u2)) {
	    result = 1;
	    ++sysPtr->beads[result].refCount;
	} else {
	    
	    /*
	     * Result is not constant. Apply recursively to the subexpression
	     * with the earlier top variable.
	     */
	    if (u1Ptr->level == u2Ptr->level) {
................................................................................
		 * Both subexpressions have the same top variable
		 */
		level = u1Ptr->level;
		low1 = u1Ptr->low;
		high1 = u1Ptr->high;
		low2 = u2Ptr->low;
		high2 = u2Ptr->high;
		l = Apply(sysPtr, low1, low2);
		h = Apply(sysPtr, high1, high2);
	    } else if (u1Ptr->level < u2Ptr->level) {
		/*
		 * Apply first to the left-hand operand
		 */
		level = u1Ptr->level;
		low1 = u1Ptr->low;
		high1 = u1Ptr->high;
		l = Apply(sysPtr, low1, u2);
		h = Apply(sysPtr, high1, u2);
	    } else /* u1Ptr->level > u2Ptr->level */ {
		/*
		 * Apply first to the right-hand operand
		 */
		level = u2Ptr->level;
		low2 = u2Ptr->low;
		high2 = u2Ptr->high;
		l = Apply(sysPtr, u1, low2);
		h = Apply(sysPtr, u1, high2);
	    }
	    /*
	     * Compose the subexpressions
	     */
	    result = BDD_MakeBead(sysPtr, level, l, h);
	    BDD_UnrefBead(sysPtr, l);
	    BDD_UnrefBead(sysPtr, h);
................................................................................
BDD_BeadIndex
BDD_Apply(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BinOp op,		/* Operation to apply */
    BDD_BeadIndex u1,		/* Left operand */
    BDD_BeadIndex u2) 		/* Right operand */
{

    Tcl_HashSearch search;	/* Search for clearing the cache */
    Tcl_HashEntry* entryPtr;	/* Hash entyr for clearing the cache */
    Tcl_InitCustomHashTable(&(sysPtr->applyCache),
			    TCL_CUSTOM_TYPE_KEYS, &Bead2KeyType);
    sysPtr->applyOp = op;
    BDD_BeadIndex result = Apply(sysPtr, u1, u2);
    for (entryPtr = Tcl_FirstHashEntry(&(sysPtr->applyCache), &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&(sysPtr->applyCache));
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Apply3 --
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Apply3(
    BDD_System* sysPtr,		/* System of BDD's */


    BDD_BeadIndex u1,		/* First operand */
    BDD_BeadIndex u2,		/* Second operand */
    BDD_BeadIndex u3) 		/* Third operand */
{
    Bead* beads = sysPtr->beads; 
				/* Bead table */
    BDD_TernOp op = sysPtr->apply3Op;
				/* Operation to apply */
    BDD_BeadIndex u[3];		/* Bead indices for left- and right-hand 
				 * sides */
    Bead* u1Ptr = beads + u1;	/* Pointer to the left-hand bead */
    Bead* u2Ptr = beads + u2;	/* Pointer to the right-hand bead */
    Bead* u3Ptr = beads + u3;	/* Pointer to the right-hand bead */
    BDD_VariableIndex level;	/* Level of the result */
    BDD_BeadIndex low1, high1;	/* Low and high transitions of the first bead */
    BDD_BeadIndex low2, high2;	/* Low and high transitions of the
				 * second bead */
................................................................................
    BDD_TernOp opfiltered;	/* Operation masked with 'opmask' */

    /* Check if the result is already hashed */

    u[0] = u1;
    u[1] = u2;
    u[2] = u3;
    entry = Tcl_CreateHashEntry(&(sysPtr->apply3Cache), u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
    } else {
	/* 
	 * Check if the result is constant or equal to one of the operands
	 */
................................................................................
	    } else {
		low3 = u3Ptr->low;
		high3 = u3Ptr->high;
	    }
	    /*
	     * Compute the two branches from the top variable
	     */
	    l = Apply3(sysPtr, low1, low2, low3);
	    h = Apply3(sysPtr, high1, high2, high3);
	    /*
	     * Compose the subexpressions
	     */
	    result = BDD_MakeBead(sysPtr, level, l, h);
	    BDD_UnrefBead(sysPtr, l);
	    BDD_UnrefBead(sysPtr, h);
	}
................................................................................
BDD_Apply3(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_TernOp op,		/* Operation to apply */
    BDD_BeadIndex u1,		/* First operand */
    BDD_BeadIndex u2, 		/* Second operand */
    BDD_BeadIndex u3)		/* Third operand */
{

    Tcl_HashSearch search;	/* Search for clearing the cache */
    Tcl_HashEntry* entryPtr;	/* Hash entyr for clearing the cache */
    Tcl_InitCustomHashTable(&(sysPtr->apply3Cache),
			    TCL_CUSTOM_TYPE_KEYS, &Bead3KeyType);
    sysPtr->apply3Op = op;
    BDD_BeadIndex result = Apply3(sysPtr, u1, u2, u3);
    for (entryPtr = Tcl_FirstHashEntry(&(sysPtr->apply3Cache), &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&(sysPtr->apply3Cache));
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Restrict --
................................................................................
 *	The restrictions must be listed in ascending order by variable
 *	number. The values of r[].value must be Boolean constants.
 *
 *-----------------------------------------------------------------------------
 */
static BDD_BeadIndex
Restrict(

    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to restrict */
    const BDD_ValueAssignment r[], 
				/* Restrictions to apply */
    BDD_VariableIndex n)	/* Number of restrictions */
{
    int newFlag;		/* Flag = 1 if we have not seen u yet */
................................................................................
    if (n == 0 || u <= 1) {
	++sysPtr->beads[u].refCount;
	return u;
    }

    /* Has this value been computed already? */

    entryPtr = Tcl_CreateHashEntry(&(sysPtr->restrictCache), u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex)(size_t)Tcl_GetHashValue(entryPtr);
	if (result != !(BDD_BeadIndex)0) {
	    ++sysPtr->beads[result].refCount;
	    return result;
	}
    } else {
................................................................................
		      rvar, nextvar);
	}
    }
    if (rvar < uvar) {
	/*
	 * r[0] is an irrelevant variable in u
	 */
	result = Restrict(sysPtr, u, r+1, n-1);
    } else if (rvar == uvar) {
	/*
	 * r[0] appears in u. Bind it.
	 */
	if (r[0].value == 0) {
	    result = Restrict(sysPtr, sysPtr->beads[u].low, r+1, n-1);
	} else if (r[0].value == 1) {
	    result = Restrict(sysPtr, sysPtr->beads[u].high, r+1, n-1);
	} else {
	    Tcl_Panic("BDD_Restrict called with non-constant value for "
		      "variable %lu\n", rvar);
	    result = 0;		/* silence compiler warnings */
	}
    } else /* rvar > uvar */ {
	/*
	 * u's first variable is unrestricted. Apply the restriction to both
	 * successors of u, and make a bead for the restricted expression.
	 */
	l = Restrict(sysPtr, sysPtr->beads[u].low, r, n);
	h = Restrict(sysPtr, sysPtr->beads[u].high, r, n);
	result = BDD_MakeBead(sysPtr, uvar, l, h);
	BDD_UnrefBead(sysPtr, l);
	BDD_UnrefBead(sysPtr, h);
    }

    /*
     * Cache the result
................................................................................
BDD_Restrict(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to restrict */
    const BDD_ValueAssignment r[],
				/* Restrictions to apply */
    BDD_VariableIndex n)	/* Count of restrictions */
{

    BDD_BeadIndex result;	/* Bead index of the result */
    Tcl_InitHashTable(&(sysPtr->restrictCache), TCL_ONE_WORD_KEYS);
    result = Restrict(sysPtr, u, r, n);
    Tcl_DeleteHashTable(&(sysPtr->restrictCache));
    return result;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Quantify --
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Quantify(
    BDD_System* sysPtr,		/* Pointer to the system of BDD's */
    BDD_VariableIndex n,	/* Number of quantified variables */


    const BDD_VariableIndex* v,	/* Variables to quantify */

    BDD_BeadIndex u)		/* Expression to quantify */
{
    BDD_BeadIndex l;		/* Low transition of the result */
    BDD_BeadIndex h;		/* High transition of the result */
    BDD_BeadIndex r;		/* Return value */
    int newFlag;		/* Flag == 1 iff the result was not cached */
    BDD_Quantifier q = sysPtr->quantifier;
				/* Quantifier to apply */
    Tcl_HashEntry* entryPtr;	/* Pointer to the hash entry for
				 * a cached result */

    /* Check for a cached result */
    entryPtr = Tcl_CreateHashEntry(&(sysPtr->quantifyCache),
				   (ClientData) u, &newFlag);
    if (!newFlag) {
	r = (BDD_BeadIndex) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[r].refCount;
	return r;
    }

    for (;;) {
................................................................................
	    r = u;
	    break;
	} else if (beadPtr->level < *v) {
	    /*
	     * The current variable in the expression is unquantified.
	     * Quantify the two subexpressions and make the result
	     */
	    l = Quantify(sysPtr, n, v, beadPtr->low);
	    h = Quantify(sysPtr, n, v, beadPtr->high);
	    r = BDD_MakeBead(sysPtr, beadPtr->level, l, h);
	    BDD_UnrefBead(sysPtr,h);
	    BDD_UnrefBead(sysPtr,l);
	    break;
	} else if (beadPtr->level == *v) {
	    /*
	     * The current variable in the expression is quantified.
	     * Quantify the two subexpressions with respect to the
	     * remaining variables and then apply the combining operation.
	     */
	    l = Quantify(sysPtr, n-1, v+1, beadPtr->low);
	    h = Quantify(sysPtr, n-1, v+1, beadPtr->high);
	    sysPtr->applyOp = q;
	    r = Apply(sysPtr, l, h);
	    BDD_UnrefBead(sysPtr,h);
	    BDD_UnrefBead(sysPtr,l);
	    break;
	} else {
	    /* 
	     * The current quantified variable does not appear free in 
	     * the expression. The quantification is trivially satisfied 
	     * with respect to the variable in question. Advance to the next 
	     * variable.
	     *
	     * TODO - This is wrong for UNIQUE
	     */
	    ++v;
	    --n;
	}
    }
    
    /*
................................................................................
    Tcl_SetHashValue(entryPtr, (ClientData) r);
    return r;
}
BDD_BeadIndex
BDD_Quantify(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_Quantifier q,		/* Quantifier to apply */

    BDD_VariableIndex n,	/* Number of variables to quantify */
    const BDD_VariableIndex* v,	/* List of variables to quantify */
    BDD_BeadIndex e)		/* Expression to quantify */
{




    Tcl_HashSearch search;	/* Search state for clearing the hash */
    Tcl_HashEntry* entryPtr;	/* Hash table entry to be cleared */

    BDD_BeadIndex r;		/* Return value */

    Tcl_InitCustomHashTable(&(sysPtr->applyCache),
			    TCL_CUSTOM_TYPE_KEYS, &Bead2KeyType);
    sysPtr->quantifier = q;
    Tcl_InitHashTable(&(sysPtr->quantifyCache), TCL_ONE_WORD_KEYS);

    r = Quantify(sysPtr, n, v, e);

    for (entryPtr = Tcl_FirstHashEntry(&(sysPtr->quantifyCache), &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&(sysPtr->quantifyCache));
    for (entryPtr = Tcl_FirstHashEntry(&(sysPtr->applyCache), &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    Tcl_DeleteHashTable(&(sysPtr->applyCache));

    return r;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_ApplyAndQuantify --
 *
 *	Applies an EXISTS or FORALL quantification to the results of applying
 *	a binary operator to two BDD's.
 *
 * Results:
 *	Returns the result of the combined applications.
 *
 * This procedure exists primarily to execute relational products, which
 * consist of computing the AND of two functions, and then quantifying away
 * the variables that were used to join them:
 *
 *	relprod(p,q) = exists[join_variables] (p AND q)
 *
 * Combining the two operations into a single call saves one complete
 * traversal of the functions.
 *
 *-----------------------------------------------------------------------------
 */

#if 0 /* This needs work - and it's a performance hack that
       * might not be needed? */
static BDD_BeadIndex
ApplyAndQuantify(
    BDD_System* sysPtr,	 	/* System of BDD's */
    Tcl_HashTable* F,		/* Partial results of Quantify */
    Tcl_HashTable* G,	 	/* Partial results of Quantify */
    Tcl_HashTable* H,	 	/* Partial results of Apply */
    Tcl_HashTable* J,		/* Final partial results of ApplyAndQuantify */
    BDD_Quantifier q,	 	/* Quantifier to apply */
    BDD_VariableIndex n,	/* Number of variables to quantify */
    const BDD_VariableIndex* v,	/* Variables to quantify */
    BDD_BinOp op,		/* Operation to apply */
    BDD_BeadIndex u1,		/* Left operand */
    BDD_BeadIndex u2)		/* Right operand */
{
    BDD_BeadIndex u[2];	        /* Hash key */
    Tcl_HashEntry* entry;       /* Cached partial result */
    int newFlag;	        /* Flag whether cached result was found */
    BDD_BinOp opmask;		/* Mask for matching the operator */
    BDD_BeadIndex temp;
    BDD_BeadIndex result = ~(BDD_BeadIndex) 0;
    Bead* beadPtr1, * beadPtr2;	/* Left and right operands */
    BDD_VariableIndex level1, level2;
				/* Left and right variables */
    BDD_VariableIndex level;	/* Lowest variable of the two operands */
    BDD_BeadIndex l, h;		/* Left and right operands for recursive
				 * ApplyAndQuantify */

    /* Check if the result is already hashed */

    u[0] = u1;
    u[1] = u2;
    entry = Tcl_CreateHashEntry(J, u, &newFlag);
    if (!newFlag) {
	result = (BDD_BeadIndex) Tcl_GetHashValue(entry);
	++sysPtr->beads[result].refCount;
	return result;
    } 

    /* 
     * Check if the result of the operation is constant or a copy
     * of one of the operands
     */
    opmask = 0xF;
    if (u1 == 0) {
	opmask &= 0x3;
    } else if (u1 == 1) {
	opmask &= 0xC;
    }
    if (u2 == 0) {
	opmask &= 0x5;
    } else if (u2 == 1) {
	opmask &= 0xA;
    }
    if ((op & opmask) == 0) {
	/* 
	 * Result is constant zero 
	 */
	result = 0;
	++sysPtr->beads[result].refCount;
    } else if ((op & opmask) == opmask) {
	/* 
	 * Result is constant one 
	 */
	result = 1;
	++sysPtr->beads[result].refCount;
    } else if ((op & opmask) == (0xC & opmask)) {
	/*
	 * Result is the left operand
	 */
	result = u1;
	++sysPtr->beads[result].refCount;
    } else if ((op & opmask) == (0xA & opmask)) {
	/*
	 * Result is the right operand
	 */
	result = u2;
	++sysPtr->beads[result].refCount;
	
	/*
	 * Also check for special cases where both operands are
	 * identical.
	 */
    } else if ((op == BDD_BINOP_AND || op == BDD_BINOP_OR) && (u1 == u2)) {
	result = u2;
	++sysPtr->beads[result].refCount;
    } else if ((op == BDD_BINOP_XOR
		|| op == BDD_BINOP_LT
		|| op == BDD_BINOP_GT) && (u1 == u2)) {
	result = 0;
	++sysPtr->beads[result].refCount;
    } else if ((op == BDD_BINOP_EQ
		|| op == BDD_BINOP_LE
		|| op == BDD_BINOP_GE) && (u1 == u2)) {
	result = 1;
	++sysPtr->beads[result].refCount;
    }

    /*
     * If we've eliminated either operand, finish by doing the quantification
     * on the other operand. If we've reduced the expression to a constant,
     * just cache and return the result.
     */

    if (result != ~(BDD_BeadIndex) 0) {
	if (result > 1) {
	    temp = result;
	    result = Quantify(sysPtr, F, G, q, n, v, temp);
	    BDD_UnrefBead(sysPtr,temp);
	}
    } else {

	beadPtr1 = sysPtr->beads + u1;
	beadPtr2 = sysPtr->beads + u2;
	level1 = beadPtr1->level;
	level2 = beadPtr2->level;
	if (n == 0 || (level1 > v[n-1] && level2 > v[n-1])) {
	    /*
	     * We've run out of variables to quantify
	     */
	    result = Apply(sysPtr, u1, u2);
	} else if (level1 < level2) {
	    level = level1;
	    l = ApplyAndQuantify(sysPtr, F, G, J, q, n, v, op,
				 beadPtr1->low, u2);
	    h = ApplyAndQuantify(sysPtr, F, G, J, q, n, v, op,
				 beadPtr1->high, u2);
	} else if (level1 == level2) {
	    level = level1;
	    l = ApplyAndQuantify(sysPtr, F, G, H, J, q, n, v, op,
				 beadPtr1->low, beadPtr2->high);
	    h = ApplyAndQuantify(sysPtr, F, G, H, J, q, n, v, op,
				 beadPtr1->high, beadPtr2->high);
	} else /* level1 > level2 */ {
	    level = level2;
	    l = ApplyAndQuantify(sysPtr, F, G, H, J, q, n, v, op,
				 u1, beadPtr2->high);
	    h = ApplyAndQuantify(sysPtr, F, G, H, J, q, n, v, op,
				 u1, beadPtr2->high);
	}
	if (level < v[0]) {
	    
	    /* FIXME FINISH THIS */
	}

    }

    /* Cache the result */
    Tcl_SetHashValue(entry, (ClientData) result);
    ++sysPtr->beads[result].refCount;

    return result;
}
#endif
		 
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_Compose --
 *
 *	Computes a BDD representing an input BDD with a selection of variables
 *	replaced simultaneously with other expressions.
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static BDD_BeadIndex
Compose(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u)		/* Input BDD */
{

    BDD_VariableIndex n = sysPtr->composeCount;
				/* Number of variables in the vector */
    BDD_BeadIndex* r = sysPtr->composeReplacements;
				/* Replacement terms for variables 0..n */
    BDD_BeadIndex result;	/* Return value */
    int newFlag;
    Tcl_HashEntry* entryPtr = Tcl_CreateHashEntry(&(sysPtr->composeCache),
						  (ClientData)u, &newFlag);

    Bead* uPtr = sysPtr->beads + u;
    BDD_VariableIndex level = uPtr->level;
    BDD_BeadIndex low = uPtr->low;
    BDD_BeadIndex high = uPtr->high;
    if (!newFlag) {
	result = (BDD_BeadIndex) (size_t) Tcl_GetHashValue(entryPtr);
	++sysPtr->beads[result].refCount;
	return result;
    } else if (sysPtr->beads[u].level >= n) {
	result = u;
	++sysPtr->beads[result].refCount;
    } else {
	low = Compose(sysPtr, low);
	high = Compose(sysPtr, high);
	result = Apply3(sysPtr, r[level], high, low);
	BDD_UnrefBead(sysPtr, low);
	BDD_UnrefBead(sysPtr, high);
    }
    Tcl_SetHashValue(entryPtr, (ClientData) result);
    ++sysPtr->beads[result].refCount;
    return result;
}
................................................................................
BDD_Compose(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex u,		/* BDD to compose */
    BDD_VariableIndex n,	/* Number of leading variables specified */
    BDD_BeadIndex replacements[])
				/* Replacement values for first n variables */
{



    Tcl_HashSearch search;	/* Search state for clearing the hash */
    Tcl_HashEntry* entryPtr;	/* Hash table entry to be cleared */
    BDD_BeadIndex r;		/* Return value */

    Tcl_InitCustomHashTable(&(sysPtr->apply3Cache),
			    TCL_CUSTOM_TYPE_KEYS, &Bead3KeyType);
    sysPtr->apply3Op = BDD_TERNOP_IFTHENELSE;
    Tcl_InitHashTable(&(sysPtr->composeCache), TCL_ONE_WORD_KEYS);
    sysPtr->composeCount = n;
    sysPtr->composeReplacements = replacements;

    r = Compose(sysPtr, u);

    for (entryPtr = Tcl_FirstHashEntry(&(sysPtr->composeCache), &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }
    for (entryPtr = Tcl_FirstHashEntry(&(sysPtr->apply3Cache), &search);
	 entryPtr != NULL;
	 entryPtr = Tcl_NextHashEntry(&search)) {
	BDD_UnrefBead(sysPtr, (BDD_BeadIndex)Tcl_GetHashValue(entryPtr));
    }





    Tcl_DeleteHashTable(&(sysPtr->composeCache));
    Tcl_DeleteHashTable(&(sysPtr->apply3Cache));

    return r;
}
 
/*
 *-----------------------------------------------------------------------------
 *
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

static int
SatCount(
    BDD_System* sysPtr,		/* System of BDD's */

    BDD_BeadIndex x,		/* BDD to enumerate */
    mp_int* count)		/* Count of satisfying variable assignments */
{
    Bead* xPtr;			/* Pointer to the bead being counted */
    Bead* lowPtr;		/* Pointer to the 0 successor */
    Bead* highPtr;		/* Pointer to the 1 successor */
    mp_int lresult;		/* Count of satisfying assignments of the
................................................................................

    /* Is the expression constant? */
    if (x <= 1) {
	return mp_set_int(count, x);
    }

    /* Is the result cached? */
    Tcl_HashEntry* entryPtr = Tcl_CreateHashEntry(&(sysPtr->satCountCache),
						  (void*) x, &new);
    if (!new) {
	mp_int* cachedResult = Tcl_GetHashValue(entryPtr);
	return mp_copy(cachedResult, count);
    }

    mp_init_multi(&lresult, &hresult, NULL);
    xPtr = sysPtr->beads + x;
    lowPtr = sysPtr->beads + xPtr->low;
    highPtr = sysPtr->beads + xPtr->high;
    if ((status = SatCount(sysPtr, xPtr->low,
			   &lresult)) != MP_OKAY
	|| (status = SatCount(sysPtr, xPtr->high,
			      &hresult)) != MP_OKAY
	|| (status = mp_mul_2d(&lresult, lowPtr->level-xPtr->level-1,
			       &lresult)) != MP_OKAY
	|| (status = mp_mul_2d(&hresult, highPtr->level-xPtr->level-1,
			       &hresult)) != MP_OKAY
	|| (status = mp_add(&lresult, &hresult, count)) != MP_OKAY) {
	Tcl_DeleteHashEntry(entryPtr);
................................................................................
}
int
BDD_SatCount(
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex x,		/* BDD to enumerate */
    mp_int* count)		/* Count of satisfying variable assignments */
{

    Tcl_HashEntry* cleanup;	/* Hash entry used in cleanup loop */
    Tcl_HashSearch search;	/* State of a hash table iteration */
    mp_int* v;			/* Dead value in the cache */
    int status;			/* Status return from tommath */
    Tcl_InitHashTable(&(sysPtr->satCountCache), TCL_ONE_WORD_KEYS);
    status = SatCount(sysPtr, x, count);
    if (status == MP_OKAY) {
	status = mp_mul_2d(count, sysPtr->beads[x].level, count);
    }
    for (cleanup = Tcl_FirstHashEntry(&(sysPtr->satCountCache), &search);
	 cleanup != NULL;
	 cleanup = Tcl_NextHashEntry(&search)) {
	v = (mp_int*) Tcl_GetHashValue(cleanup);
	mp_clear(v);
	ckfree(v);
	Tcl_SetHashValue(cleanup, NULL);
    }
    Tcl_DeleteHashTable(&(sysPtr->satCountCache));
    return status;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * BDD_AllSatStart --
................................................................................
 *	from the initial state.
 *
 *-----------------------------------------------------------------------------
 */

static int
Dump(



    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex beadIndex)	/* Index of the bead being dumped */
{
    Tcl_Interp* interp = sysPtr->dumpInterp;
    Tcl_Obj* output = sysPtr->dumpOutput;
    int newFlag;		/* Has this object been seen before? */
    Tcl_CreateHashEntry(&(sysPtr->dumpCache), (void*) beadIndex, &newFlag);

    if (newFlag) {

	/* 
	 * Make the triple {level ifFalse ifTrue} and store it in the
	 * dictionary keyed by the bead index.
	 */
	Tcl_Obj* content = Tcl_NewObj();
................................................................................
	    return TCL_ERROR;
	}

	/*
	 * If this bead isn't a leaf, dump its successors
	 */
	if (beadIndex > 1) {
	    if (Dump(sysPtr, low) != TCL_OK
		|| Dump(sysPtr, high) != TCL_OK) {
		return TCL_ERROR;
	    }
	}
    }
    return TCL_OK;
}
int
BDD_Dump(
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_Obj* output,		/* Output object */
    BDD_System* sysPtr,		/* System of BDD's */
    BDD_BeadIndex beadIndex)	/* Index of the bead to dump */
{

    int result;			/* Tcl status return */

    /*
     * We haven't seen any beads yet.
     */
    Tcl_InitHashTable(&(sysPtr->dumpCache), TCL_ONE_WORD_KEYS);

    /*
     * Dump the BDD to the given object
     */
    Tcl_SetStringObj(output, NULL, 0);
    sysPtr->dumpInterp = interp;
    sysPtr->dumpOutput = output;
    result = Dump(sysPtr, beadIndex);

    /*
     * Clean up the hashtable
     */
    Tcl_DeleteHashTable(&(sysPtr->dumpCache));
    return result;
}
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * End:
 */

Changes to generic/bdd.h.

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
				       BDD_BeadIndex u3);
extern BDDAPI BDD_BeadIndex BDD_Restrict(BDD_System* sysPtr,
					 BDD_BeadIndex u,
					 const BDD_ValueAssignment r[],
					 BDD_VariableIndex n);
extern BDDAPI BDD_BeadIndex BDD_Quantify(BDD_System* sysPtr,
					 BDD_Quantifier q,
					 const BDD_VariableIndex v[],
					 BDD_VariableIndex n,
					 BDD_BeadIndex u);
extern BDDAPI BDD_BeadIndex BDD_Compose(BDD_System* sysPtr,
					BDD_BeadIndex u,
					BDD_VariableIndex nVars,
					BDD_BeadIndex replacements[]);
extern BDDAPI int BDD_SatCount(BDD_System* sysPtr, BDD_BeadIndex x,
			       mp_int* count);







|
|







145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
				       BDD_BeadIndex u3);
extern BDDAPI BDD_BeadIndex BDD_Restrict(BDD_System* sysPtr,
					 BDD_BeadIndex u,
					 const BDD_ValueAssignment r[],
					 BDD_VariableIndex n);
extern BDDAPI BDD_BeadIndex BDD_Quantify(BDD_System* sysPtr,
					 BDD_Quantifier q,
					 BDD_VariableIndex n,
					 const BDD_VariableIndex v[],
					 BDD_BeadIndex u);
extern BDDAPI BDD_BeadIndex BDD_Compose(BDD_System* sysPtr,
					BDD_BeadIndex u,
					BDD_VariableIndex nVars,
					BDD_BeadIndex replacements[]);
extern BDDAPI int BDD_SatCount(BDD_System* sysPtr, BDD_BeadIndex x,
			       mp_int* count);

Changes to generic/bddInt.h.

34
35
36
37
38
39
40
41























42
43
44
45
46
47
48
				 * which in turn points to the next-least
				 * recently-used, and so on in a circular
				 * queue. */
    BDD_BeadIndex* hashes;	/* Array of indices for the first bead in each
				 * hash bucket. The hash table looks up beads
				 * by 'level', 'low' and 'high.*/
    BDD_BeadIndex hashSize;	/* Number of buckets in the hash table */
  























};

/*
 * State of an AllSat computation
 */
typedef enum BDD_AllSatSEnum {
    BDD_ALLSAT_START,		/* Initial entry */







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
				 * which in turn points to the next-least
				 * recently-used, and so on in a circular
				 * queue. */
    BDD_BeadIndex* hashes;	/* Array of indices for the first bead in each
				 * hash bucket. The hash table looks up beads
				 * by 'level', 'low' and 'high.*/
    BDD_BeadIndex hashSize;	/* Number of buckets in the hash table */

    Tcl_HashTable applyCache;	/* Cache of partial results for BDD_Apply */
    BDD_BinOp applyOp;		/* Operator for BDD_Apply */
    Tcl_HashTable apply3Cache;	/* Cache of partial results for BDD_Apply3 */
    BDD_TernOp apply3Op;	/* Operator for BDD_Apply3 */
    Tcl_HashTable composeCache;	/* Cache of partial results for BDD_Compose */
    BDD_VariableIndex composeCount;
				/* Number of variables being substituted
				 * in BDD_Compose */
    BDD_BeadIndex* composeReplacements;
				/* Replacement expressions for variables
				 * being substituted in BDD_Compose */
    Tcl_HashTable dumpCache;	/* Cache of visited nodes for BDD_Dump */
    Tcl_Interp* dumpInterp;	/* Tcl interpreter where BDD_Dump stores
				 * results */
    Tcl_Obj* dumpOutput;        /* Tcl object where BDD_Dump returns the dump */
    Tcl_HashTable negateCache;	/* Cache of partial results for BDD_Negate */
    BDD_Quantifier quantifier;	/* Quantifier for BDD_Quantify */
    Tcl_HashTable quantifyCache;
				/* Cache of partial results for BDD_Quantify */
    Tcl_HashTable restrictCache; 
				/* Cache of partial results for BDD_Restrict */
    Tcl_HashTable satCountCache;
				/* Cache of partial results for BDD_SatCount */
};

/*
 * State of an AllSat computation
 */
typedef enum BDD_AllSatSEnum {
    BDD_ALLSAT_START,		/* Initial entry */

Changes to generic/tclBdd.c.

1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
     * Order the literals
     */
    qsort(v, varc, sizeof(BDD_VariableIndex), CompareVariableIndices);

    /*
     * Quantify the formula
     */
    result = BDD_Quantify(sdata->system, q, v, varc, u);

    ckfree(v);
    SetNamedExpression(sdata, objv[skipped], result);
    BDD_UnrefBead(sdata->system, result);
    return TCL_OK;
}
 







|







1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
     * Order the literals
     */
    qsort(v, varc, sizeof(BDD_VariableIndex), CompareVariableIndices);

    /*
     * Quantify the formula
     */
    result = BDD_Quantify(sdata->system, q, varc, v, u);

    ckfree(v);
    SetNamedExpression(sdata, objv[skipped], result);
    BDD_UnrefBead(sdata->system, result);
    return TCL_OK;
}
 

Changes to library/tclbdd.tcl.

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------------

package require grammar::aycock;	# TEMP - Build parser in advance!
# package require grammar::aycock::runtime

namespace eval bdd {
    namespace export system
}

oo::class create bdd::system {
    method === {exprName1 exprName2} {







|







6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------------

package require grammar::aycock;	# TEMP - Build parser in advance!
package require grammar::aycock::runtime

namespace eval bdd {
    namespace export system
}

oo::class create bdd::system {
    method === {exprName1 exprName2} {

Changes to tests/bdd.test.

1
2
3
4
5
6
7
8
9
10
....
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
....
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
# tdbc.test --
#
#       Tests for convenience commands in TDBC
#
# Copyright (c) 2013 by Kevin B. Kenny.

package require tcltest 2
namespace import -force ::tcltest::*
tcltest::loadTestedCommands
package require tclbdd
................................................................................

test bdd-17.14 {compose - interchange variables} {
    -setup {
	bdd::system create sys
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar b 2; sys notnthvar !b 2
	sys nthvar c 3; sys notnthvar !c 3
	sys < x a b
	sys < t b c
	sys & x x t
	sys < y b c
	sys < t c a
	sys & y y t
    } 
    -body {
	sys compose z x a b b c c a
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1
................................................................................

test bdd-17.15 {compose - interchange variables} {
    -setup {
	bdd::system create sys
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar b 2; sys notnthvar !b 2
	sys nthvar c 3; sys notnthvar !c 3
	sys < x a b
	sys < t b c
	sys & x x t
	sys < y c a
	sys < t a b
	sys & y y t
    } 
    -body {
	sys compose z x b a c b a c
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1
|

|







 







|
|

|
|







 







|
|

|
|







1
2
3
4
5
6
7
8
9
10
....
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
....
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
# bdd.test --
#
#       Tests for Binary Decision Diagrams (BDD's)
#
# Copyright (c) 2013 by Kevin B. Kenny.

package require tcltest 2
namespace import -force ::tcltest::*
tcltest::loadTestedCommands
package require tclbdd
................................................................................

test bdd-17.14 {compose - interchange variables} {
    -setup {
	bdd::system create sys
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar b 2; sys notnthvar !b 2
	sys nthvar c 3; sys notnthvar !c 3
	sys <= x a b
	sys <= t b c
	sys & x x t
	sys <= y b c
	sys <= t c a
	sys & y y t
    } 
    -body {
	sys compose z x a b b c c a
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1
................................................................................

test bdd-17.15 {compose - interchange variables} {
    -setup {
	bdd::system create sys
	sys nthvar a 1; sys notnthvar !a 1
	sys nthvar b 2; sys notnthvar !b 2
	sys nthvar c 3; sys notnthvar !c 3
	sys <= x a b
	sys <= t b c
	sys & x x t
	sys <= y c a
	sys <= t a b
	sys & y y t
    } 
    -body {
	sys compose z x b a c b a c
	expr {[sys beadindex z] == [sys beadindex y]}
    }
    -result 1