You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

944 lines
25 KiB

  1. /**CFile**********************************************************************
  2. FileName [dddmpDdNodeCnf.c]
  3. PackageName [dddmp]
  4. Synopsis [Functions to handle BDD node infos and numbering
  5. while storing a CNF formula from a BDD or an array of BDDs]
  6. Description [Functions to handle BDD node infos and numbering
  7. while storing a CNF formula from a BDD or an array of BDDs.
  8. ]
  9. Author [Gianpiero Cabodi and Stefano Quer]
  10. Copyright [
  11. Copyright (c) 2004 by Politecnico di Torino.
  12. All Rights Reserved. This software is for educational purposes only.
  13. Permission is given to academic institutions to use, copy, and modify
  14. this software and its documentation provided that this introductory
  15. message is not removed, that this software and its documentation is
  16. used for the institutions' internal research and educational purposes,
  17. and that no monies are exchanged. No guarantee is expressed or implied
  18. by the distribution of this code.
  19. Send bug-reports and/or questions to:
  20. {gianpiero.cabodi,stefano.quer}@polito.it.
  21. ]
  22. ******************************************************************************/
  23. #include "dddmpInt.h"
  24. /*---------------------------------------------------------------------------*/
  25. /* Stucture declarations */
  26. /*---------------------------------------------------------------------------*/
  27. /*---------------------------------------------------------------------------*/
  28. /* Type declarations */
  29. /*---------------------------------------------------------------------------*/
  30. /*---------------------------------------------------------------------------*/
  31. /* Variable declarations */
  32. /*---------------------------------------------------------------------------*/
  33. #define DDDMP_DEBUG_CNF 0
  34. /*---------------------------------------------------------------------------*/
  35. /* Macro declarations */
  36. /*---------------------------------------------------------------------------*/
  37. /**AutomaticStart*************************************************************/
  38. /*---------------------------------------------------------------------------*/
  39. /* Static function prototypes */
  40. /*---------------------------------------------------------------------------*/
  41. static int DddmpWriteNodeIndexCnf(DdNode *f, int *cnfIds, int id);
  42. static int DddmpReadNodeIndexCnf(DdNode *f);
  43. static int DddmpClearVisitedCnfRecur(DdNode *f);
  44. static int DddmpVisitedCnf(DdNode *f);
  45. static void DddmpSetVisitedCnf(DdNode *f);
  46. static void DddmpClearVisitedCnf(DdNode *f);
  47. static int NumberNodeRecurCnf(DdNode *f, int *cnfIds, int id);
  48. static void DddmpDdNodesCheckIncomingAndScanPath(DdNode *f, int pathLengthCurrent, int edgeInTh, int pathLengthTh);
  49. static int DddmpDdNodesNumberEdgesRecur(DdNode *f, int *cnfIds, int id);
  50. static int DddmpDdNodesResetCountRecur(DdNode *f);
  51. static int DddmpDdNodesCountEdgesRecur(DdNode *f);
  52. static void RemoveFromUniqueRecurCnf(DdManager *ddMgr, DdNode *f);
  53. static void RestoreInUniqueRecurCnf(DdManager *ddMgr, DdNode *f);
  54. static int DddmpPrintBddAndNextRecur(DdManager *ddMgr, DdNode *f);
  55. /**AutomaticEnd***************************************************************/
  56. /*---------------------------------------------------------------------------*/
  57. /* Definition of exported functions */
  58. /*---------------------------------------------------------------------------*/
  59. /*---------------------------------------------------------------------------*/
  60. /* Definition of internal functions */
  61. /*---------------------------------------------------------------------------*/
  62. /**Function********************************************************************
  63. Synopsis [Removes nodes from unique table and numbers them]
  64. Description [Node numbering is required to convert pointers to integers.
  65. Since nodes are removed from unique table, no new nodes should
  66. be generated before re-inserting nodes in the unique table
  67. (DddmpUnnumberDdNodesCnf()).
  68. ]
  69. SideEffects [Nodes are temporarily removed from unique table]
  70. SeeAlso [RemoveFromUniqueRecurCnf(), NumberNodeRecurCnf(),
  71. DddmpUnnumberDdNodesCnf()]
  72. ******************************************************************************/
  73. int
  74. DddmpNumberDdNodesCnf (
  75. DdManager *ddMgr /* IN: DD Manager */,
  76. DdNode **f /* IN: array of BDDs */,
  77. int rootN /* IN: number of BDD roots in the array of BDDs */,
  78. int *cnfIds /* OUT: CNF identifiers for variables */,
  79. int id /* OUT: number of Temporary Variables Introduced */
  80. )
  81. {
  82. int i;
  83. for (i=0; i<rootN; i++) {
  84. RemoveFromUniqueRecurCnf (ddMgr, f[i]);
  85. }
  86. for (i=0; i<rootN; i++) {
  87. id = NumberNodeRecurCnf (f[i], cnfIds, id);
  88. }
  89. return (id);
  90. }
  91. /**Function********************************************************************
  92. Synopsis [Removes nodes from unique table and numbers each node according
  93. to the number of its incoming BDD edges.
  94. ]
  95. Description [Removes nodes from unique table and numbers each node according
  96. to the number of its incoming BDD edges.
  97. ]
  98. SideEffects [Nodes are temporarily removed from unique table]
  99. SeeAlso [RemoveFromUniqueRecurCnf()]
  100. ******************************************************************************/
  101. int
  102. DddmpDdNodesCountEdgesAndNumber (
  103. DdManager *ddMgr /* IN: DD Manager */,
  104. DdNode **f /* IN: Array of BDDs */,
  105. int rootN /* IN: Number of BDD roots in the array of BDDs */,
  106. int edgeInTh /* IN: Max # In-Edges, after a Insert Cut Point */,
  107. int pathLengthTh /* IN: Max Path Length (after, Insert a Cut Point) */,
  108. int *cnfIds /* OUT: CNF identifiers for variables */,
  109. int id /* OUT: Number of Temporary Variables Introduced */
  110. )
  111. {
  112. int retValue, i;
  113. /*-------------------------- Remove From Unique ---------------------------*/
  114. for (i=0; i<rootN; i++) {
  115. RemoveFromUniqueRecurCnf (ddMgr, f[i]);
  116. }
  117. /*-------------------- Reset Counter and Reset Visited --------------------*/
  118. for (i=0; i<rootN; i++) {
  119. retValue = DddmpDdNodesResetCountRecur (f[i]);
  120. }
  121. /* Here we must have:
  122. * cnfIndex = 0
  123. * visitedFlag = 0
  124. * FOR ALL nodes
  125. */
  126. #if DDDMP_DEBUG_CNF
  127. fprintf (stdout, "###---> BDDs After Count Reset:\n");
  128. DddmpPrintBddAndNext (ddMgr, f, rootN);
  129. #endif
  130. /*----------------------- Count Incoming Edges ----------------------------*/
  131. for (i=0; i<rootN; i++) {
  132. retValue = DddmpDdNodesCountEdgesRecur (f[i]);
  133. }
  134. /* Here we must have:
  135. * cnfIndex = incoming edge count
  136. * visitedFlag = 0 (AGAIN ... remains untouched)
  137. * FOR ALL nodes
  138. */
  139. #if DDDMP_DEBUG_CNF
  140. fprintf (stdout, "###---> BDDs After Count Recur:\n");
  141. DddmpPrintBddAndNext (ddMgr, f, rootN);
  142. #endif
  143. /*------------------------- Count Path Length ----------------------------*/
  144. for (i=0; i<rootN; i++) {
  145. DddmpDdNodesCheckIncomingAndScanPath (f[i], 0, edgeInTh,
  146. pathLengthTh);
  147. }
  148. /* Here we must have:
  149. * cnfIndex = 1 if we want to insert there a cut point
  150. * 0 if we do NOT want to insert there a cut point
  151. * visitedFlag = 1
  152. * FOR ALL nodes
  153. */
  154. #if DDDMP_DEBUG_CNF
  155. fprintf (stdout, "###---> BDDs After Check Incoming And Scan Path:\n");
  156. DddmpPrintBddAndNext (ddMgr, f, rootN);
  157. #endif
  158. /*-------------------- Number Nodes and Set Visited -----------------------*/
  159. for (i=0; i<rootN; i++) {
  160. id = DddmpDdNodesNumberEdgesRecur (f[i], cnfIds, id);
  161. }
  162. /* Here we must have:
  163. * cnfIndex = CNF auxiliary variable enumeration
  164. * visitedFlag = 0
  165. * FOR ALL nodes
  166. */
  167. #if DDDMP_DEBUG_CNF
  168. fprintf (stdout, "###---> BDDs After Count Edges Recur:\n");
  169. DddmpPrintBddAndNext (ddMgr, f, rootN);
  170. #endif
  171. /*---------------------------- Clear Visited ------------------------------*/
  172. #if DDDMP_DEBUG_CNF
  173. for (i=0; i<rootN; i++) {
  174. retValue = DddmpClearVisitedCnfRecur (f[i]);
  175. }
  176. #if DDDMP_DEBUG_CNF
  177. fprintf (stdout, "###---> BDDs After All Numbering Process:\n");
  178. DddmpPrintBddAndNext (ddMgr, f, rootN);
  179. #endif
  180. #endif
  181. return (id);
  182. }
  183. /**Function********************************************************************
  184. Synopsis [Restores nodes in unique table, loosing numbering]
  185. Description [Node indexes are no more needed. Nodes are re-linked in the
  186. unique table.
  187. ]
  188. SideEffects [None]
  189. SeeAlso [DddmpNumberDdNode()]
  190. ******************************************************************************/
  191. void
  192. DddmpUnnumberDdNodesCnf(
  193. DdManager *ddMgr /* IN: DD Manager */,
  194. DdNode **f /* IN: array of BDDs */,
  195. int rootN /* IN: number of BDD roots in the array of BDDs */
  196. )
  197. {
  198. int i;
  199. for (i=0; i<rootN; i++) {
  200. RestoreInUniqueRecurCnf (ddMgr, f[i]);
  201. }
  202. return;
  203. }
  204. /**Function********************************************************************
  205. Synopsis [Prints debug information]
  206. Description [Prints debug information for an array of BDDs on the screen]
  207. SideEffects [None]
  208. SeeAlso []
  209. ******************************************************************************/
  210. int
  211. DddmpPrintBddAndNext (
  212. DdManager *ddMgr /* IN: DD Manager */,
  213. DdNode **f /* IN: Array of BDDs to be displayed */,
  214. int rootN /* IN: Number of BDD roots in the array of BDDs */
  215. )
  216. {
  217. int i;
  218. for (i=0; i<rootN; i++) {
  219. fprintf (stdout, "---> Bdd %d:\n", i);
  220. fflush (stdout);
  221. DddmpPrintBddAndNextRecur (ddMgr, f[i]);
  222. }
  223. return (DDDMP_SUCCESS);
  224. }
  225. /**Function********************************************************************
  226. Synopsis [Write index to node]
  227. Description [The index of the node is written in the "next" field of
  228. a DdNode struct. LSB is not used (set to 0). It is used as
  229. "visited" flag in DD traversals.
  230. ]
  231. SideEffects [None]
  232. SeeAlso [DddmpReadNodeIndexCnf(), DddmpSetVisitedCnf (),
  233. DddmpVisitedCnf ()
  234. ]
  235. ******************************************************************************/
  236. int
  237. DddmpWriteNodeIndexCnfBis (
  238. DdNode *f /* IN: BDD node */,
  239. int id /* IN: index to be written */
  240. )
  241. {
  242. if (!Cudd_IsConstant (f)) {
  243. f->next = (struct DdNode *)((ptruint)((id)<<1));
  244. }
  245. return (DDDMP_SUCCESS);
  246. }
  247. /*---------------------------------------------------------------------------*/
  248. /* Definition of static functions */
  249. /*---------------------------------------------------------------------------*/
  250. /**Function********************************************************************
  251. Synopsis [Write index to node]
  252. Description [The index of the node is written in the "next" field of
  253. a DdNode struct. LSB is not used (set to 0). It is used as
  254. "visited" flag in DD traversals. The index corresponds to
  255. the BDD node variable if both the node's children are a
  256. constant node, otherwise a new CNF variable is used.
  257. ]
  258. SideEffects [None]
  259. SeeAlso [DddmpReadNodeIndexCnf(), DddmpSetVisitedCnf (),
  260. DddmpVisitedCnf ()]
  261. *****************************************************************************/
  262. static int
  263. DddmpWriteNodeIndexCnf (
  264. DdNode *f /* IN: BDD node */,
  265. int *cnfIds /* IN: possible source for the index to be written */,
  266. int id /* IN: possible source for the index to be written */
  267. )
  268. {
  269. if (!Cudd_IsConstant (f)) {
  270. if (Cudd_IsConstant (cuddT (f)) && Cudd_IsConstant (cuddE (f))) {
  271. /* If Variable SET ID as Variable ID */
  272. f->next = (struct DdNode *)((ptruint)((cnfIds[f->index])<<1));
  273. } else {
  274. f->next = (struct DdNode *)((ptruint)((id)<<1));
  275. id++;
  276. }
  277. }
  278. return(id);
  279. }
  280. /**Function********************************************************************
  281. Synopsis [Reads the index of a node]
  282. Description [Reads the index of a node. LSB is skipped (used as visited
  283. flag).
  284. ]
  285. SideEffects [None]
  286. SeeAlso [DddmpWriteNodeIndexCnf(), DddmpSetVisitedCnf (),
  287. DddmpVisitedCnf ()]
  288. ******************************************************************************/
  289. static int
  290. DddmpReadNodeIndexCnf (
  291. DdNode *f /* IN: BDD node */
  292. )
  293. {
  294. if (!Cudd_IsConstant (f)) {
  295. return ((int)(((ptruint)(f->next))>>1));
  296. } else {
  297. return (1);
  298. }
  299. }
  300. /**Function********************************************************************
  301. Synopsis [Mark ALL nodes as not visited]
  302. Description [Mark ALL nodes as not visited (it recurs on the node children)]
  303. SideEffects [None]
  304. SeeAlso [DddmpVisitedCnf (), DddmpSetVisitedCnf ()]
  305. ******************************************************************************/
  306. static int
  307. DddmpClearVisitedCnfRecur (
  308. DdNode *f /* IN: root of the BDD to be marked */
  309. )
  310. {
  311. int retValue;
  312. f = Cudd_Regular(f);
  313. if (cuddIsConstant (f)) {
  314. return (DDDMP_SUCCESS);
  315. }
  316. if (!DddmpVisitedCnf (f)) {
  317. return (DDDMP_SUCCESS);
  318. }
  319. retValue = DddmpClearVisitedCnfRecur (cuddT (f));
  320. retValue = DddmpClearVisitedCnfRecur (cuddE (f));
  321. DddmpClearVisitedCnf (f);
  322. return (DDDMP_SUCCESS);
  323. }
  324. /**Function********************************************************************
  325. Synopsis [Returns true if node is visited]
  326. Description [Returns true if node is visited]
  327. SideEffects [None]
  328. SeeAlso [DddmpSetVisitedCnf (), DddmpClearVisitedCnf ()]
  329. ******************************************************************************/
  330. static int
  331. DddmpVisitedCnf (
  332. DdNode *f /* IN: BDD node to be tested */
  333. )
  334. {
  335. f = Cudd_Regular(f);
  336. return ((int)((ptruint)(f->next)) & (01));
  337. }
  338. /**Function********************************************************************
  339. Synopsis [Marks a node as visited]
  340. Description [Marks a node as visited]
  341. SideEffects [None]
  342. SeeAlso [DddmpVisitedCnf (), DddmpClearVisitedCnf ()]
  343. ******************************************************************************/
  344. static void
  345. DddmpSetVisitedCnf (
  346. DdNode *f /* IN: BDD node to be marked (as visited) */
  347. )
  348. {
  349. f = Cudd_Regular(f);
  350. f->next = (DdNode *)(ptruint)((int)((ptruint)(f->next))|01);
  351. return;
  352. }
  353. /**Function********************************************************************
  354. Synopsis [Marks a node as not visited]
  355. Description [Marks a node as not visited]
  356. SideEffects [None]
  357. SeeAlso [DddmpVisitedCnf (), DddmpSetVisitedCnf ()]
  358. ******************************************************************************/
  359. static void
  360. DddmpClearVisitedCnf (
  361. DdNode *f /* IN: BDD node to be marked (as not visited) */
  362. )
  363. {
  364. f = Cudd_Regular (f);
  365. f->next = (DdNode *)(ptruint)((int)((ptruint)(f->next)) & (~01));
  366. return;
  367. }
  368. /**Function********************************************************************
  369. Synopsis [Number nodes recursively in post-order]
  370. Description [Number nodes recursively in post-order.
  371. The "visited" flag is used with inverse polarity, because all nodes
  372. were set "visited" when removing them from unique.
  373. ]
  374. SideEffects ["visited" flags are reset.]
  375. SeeAlso []
  376. ******************************************************************************/
  377. static int
  378. NumberNodeRecurCnf(
  379. DdNode *f /* IN: root of the BDD to be numbered */,
  380. int *cnfIds /* IN: possible source for numbering */,
  381. int id /* IN/OUT: possible source for numbering */
  382. )
  383. {
  384. f = Cudd_Regular(f);
  385. if (!DddmpVisitedCnf (f)) {
  386. return (id);
  387. }
  388. if (!cuddIsConstant (f)) {
  389. id = NumberNodeRecurCnf (cuddT (f), cnfIds, id);
  390. id = NumberNodeRecurCnf (cuddE (f), cnfIds, id);
  391. }
  392. id = DddmpWriteNodeIndexCnf (f, cnfIds, id);
  393. DddmpClearVisitedCnf (f);
  394. return (id);
  395. }
  396. /**Function********************************************************************
  397. Synopsis [Number nodes recursively in post-order]
  398. Description [Number nodes recursively in post-order.
  399. The "visited" flag is used with the right polarity.
  400. The node is assigned to a new CNF variable only if it is a "shared"
  401. node (i.e. the number of its incoming edges is greater than 1).
  402. ]
  403. SideEffects ["visited" flags are set.]
  404. SeeAlso []
  405. ******************************************************************************/
  406. static void
  407. DddmpDdNodesCheckIncomingAndScanPath (
  408. DdNode *f /* IN: BDD node to be numbered */,
  409. int pathLengthCurrent /* IN: Current Path Length */,
  410. int edgeInTh /* IN: Max # In-Edges, after a Insert Cut Point */,
  411. int pathLengthTh /* IN: Max Path Length (after, Insert a Cut Point) */
  412. )
  413. {
  414. int retValue;
  415. f = Cudd_Regular(f);
  416. if (DddmpVisitedCnf (f)) {
  417. return;
  418. }
  419. if (cuddIsConstant (f)) {
  420. return;
  421. }
  422. pathLengthCurrent++;
  423. retValue = DddmpReadNodeIndexCnf (f);
  424. if ( ((edgeInTh >= 0) && (retValue > edgeInTh)) ||
  425. ((pathLengthTh >= 0) && (pathLengthCurrent > pathLengthTh))
  426. ) {
  427. DddmpWriteNodeIndexCnfBis (f, 1);
  428. pathLengthCurrent = 0;
  429. } else {
  430. DddmpWriteNodeIndexCnfBis (f, 0);
  431. }
  432. DddmpDdNodesCheckIncomingAndScanPath (cuddT (f), pathLengthCurrent,
  433. edgeInTh, pathLengthTh);
  434. DddmpDdNodesCheckIncomingAndScanPath (cuddE (f), pathLengthCurrent,
  435. edgeInTh, pathLengthTh);
  436. DddmpSetVisitedCnf (f);
  437. return;
  438. }
  439. /**Function********************************************************************
  440. Synopsis [Number nodes recursively in post-order]
  441. Description [Number nodes recursively in post-order.
  442. The "visited" flag is used with the inverse polarity.
  443. Numbering follows the subsequent strategy:
  444. * if the index = 0 it remains so
  445. * if the index >= 1 it gets enumerated.
  446. This implies that the node is assigned to a new CNF variable only if
  447. it is not a terminal node otherwise it is assigned the index of
  448. the BDD variable.
  449. ]
  450. SideEffects ["visited" flags are reset.]
  451. SeeAlso []
  452. ******************************************************************************/
  453. static int
  454. DddmpDdNodesNumberEdgesRecur (
  455. DdNode *f /* IN: BDD node to be numbered */,
  456. int *cnfIds /* IN: possible source for numbering */,
  457. int id /* IN/OUT: possible source for numbering */
  458. )
  459. {
  460. int retValue;
  461. f = Cudd_Regular(f);
  462. if (!DddmpVisitedCnf (f)) {
  463. return (id);
  464. }
  465. if (cuddIsConstant (f)) {
  466. return (id);
  467. }
  468. id = DddmpDdNodesNumberEdgesRecur (cuddT (f), cnfIds, id);
  469. id = DddmpDdNodesNumberEdgesRecur (cuddE (f), cnfIds, id);
  470. retValue = DddmpReadNodeIndexCnf (f);
  471. if (retValue >= 1) {
  472. id = DddmpWriteNodeIndexCnf (f, cnfIds, id);
  473. } else {
  474. DddmpWriteNodeIndexCnfBis (f, 0);
  475. }
  476. DddmpClearVisitedCnf (f);
  477. return (id);
  478. }
  479. /**Function********************************************************************
  480. Synopsis [Resets counter and visited flag for ALL nodes of a BDD]
  481. Description [Resets counter and visited flag for ALL nodes of a BDD (it
  482. recurs on the node children). The index field of the node is
  483. used as counter.
  484. ]
  485. SideEffects []
  486. SeeAlso []
  487. ******************************************************************************/
  488. static int
  489. DddmpDdNodesResetCountRecur (
  490. DdNode *f /* IN: root of the BDD whose counters are reset */
  491. )
  492. {
  493. int retValue;
  494. f = Cudd_Regular (f);
  495. if (!DddmpVisitedCnf (f)) {
  496. return (DDDMP_SUCCESS);
  497. }
  498. if (!cuddIsConstant (f)) {
  499. retValue = DddmpDdNodesResetCountRecur (cuddT (f));
  500. retValue = DddmpDdNodesResetCountRecur (cuddE (f));
  501. }
  502. DddmpWriteNodeIndexCnfBis (f, 0);
  503. DddmpClearVisitedCnf (f);
  504. return (DDDMP_SUCCESS);
  505. }
  506. /**Function********************************************************************
  507. Synopsis [Counts the number of incoming edges for each node of a BDD]
  508. Description [Counts (recursively) the number of incoming edges for each
  509. node of a BDD. This number is stored in the index field.
  510. ]
  511. SideEffects ["visited" flags remain untouched.]
  512. SeeAlso []
  513. ******************************************************************************/
  514. static int
  515. DddmpDdNodesCountEdgesRecur (
  516. DdNode *f /* IN: root of the BDD */
  517. )
  518. {
  519. int indexValue, retValue;
  520. f = Cudd_Regular (f);
  521. if (cuddIsConstant (f)) {
  522. return (DDDMP_SUCCESS);
  523. }
  524. if (Cudd_IsConstant (cuddT (f)) && Cudd_IsConstant (cuddE (f))) {
  525. return (DDDMP_SUCCESS);
  526. }
  527. indexValue = DddmpReadNodeIndexCnf (f);
  528. /* IF (first time) THEN recur */
  529. if (indexValue == 0) {
  530. retValue = DddmpDdNodesCountEdgesRecur (cuddT (f));
  531. retValue = DddmpDdNodesCountEdgesRecur (cuddE (f));
  532. }
  533. /* Increment Incoming-Edge Count Flag */
  534. indexValue++;
  535. DddmpWriteNodeIndexCnfBis (f, indexValue);
  536. return (DDDMP_SUCCESS);
  537. }
  538. /**Function********************************************************************
  539. Synopsis [Removes a node from unique table]
  540. Description [Removes a node from the unique table by locating the proper
  541. subtable and unlinking the node from it. It recurs on son nodes.
  542. ]
  543. SideEffects [Nodes are left with the "visited" flag true.]
  544. SeeAlso [RestoreInUniqueRecurCnf()]
  545. ******************************************************************************/
  546. static void
  547. RemoveFromUniqueRecurCnf (
  548. DdManager *ddMgr /* IN: DD Manager */,
  549. DdNode *f /* IN: root of the BDD to be extracted */
  550. )
  551. {
  552. DdNode *node, *last, *next;
  553. DdNode *sentinel = &(ddMgr->sentinel);
  554. DdNodePtr *nodelist;
  555. DdSubtable *subtable;
  556. int pos, level;
  557. f = Cudd_Regular (f);
  558. if (DddmpVisitedCnf (f)) {
  559. return;
  560. }
  561. if (!cuddIsConstant (f)) {
  562. RemoveFromUniqueRecurCnf (ddMgr, cuddT (f));
  563. RemoveFromUniqueRecurCnf (ddMgr, cuddE (f));
  564. level = ddMgr->perm[f->index];
  565. subtable = &(ddMgr->subtables[level]);
  566. nodelist = subtable->nodelist;
  567. pos = ddHash (cuddT (f), cuddE (f), subtable->shift);
  568. node = nodelist[pos];
  569. last = NULL;
  570. while (node != sentinel) {
  571. next = node->next;
  572. if (node == f) {
  573. if (last != NULL)
  574. last->next = next;
  575. else
  576. nodelist[pos] = next;
  577. break;
  578. } else {
  579. last = node;
  580. node = next;
  581. }
  582. }
  583. f->next = NULL;
  584. }
  585. DddmpSetVisitedCnf (f);
  586. return;
  587. }
  588. /**Function********************************************************************
  589. Synopsis [Restores a node in unique table]
  590. Description [Restores a node in unique table (recursive)]
  591. SideEffects [Nodes are not restored in the same order as before removal]
  592. SeeAlso [RemoveFromUnique()]
  593. ******************************************************************************/
  594. static void
  595. RestoreInUniqueRecurCnf (
  596. DdManager *ddMgr /* IN: DD Manager */,
  597. DdNode *f /* IN: root of the BDD to be restored */
  598. )
  599. {
  600. DdNodePtr *nodelist;
  601. DdNode *T, *E, *looking;
  602. DdNodePtr *previousP;
  603. DdSubtable *subtable;
  604. int pos, level;
  605. #ifdef DDDMP_DEBUG
  606. DdNode *node;
  607. DdNode *sentinel = &(ddMgr->sentinel);
  608. #endif
  609. f = Cudd_Regular(f);
  610. if (!Cudd_IsComplement (f->next)) {
  611. return;
  612. }
  613. if (cuddIsConstant (f)) {
  614. DddmpClearVisitedCnf (f);
  615. /*f->next = NULL;*/
  616. return;
  617. }
  618. RestoreInUniqueRecurCnf (ddMgr, cuddT (f));
  619. RestoreInUniqueRecurCnf (ddMgr, cuddE (f));
  620. level = ddMgr->perm[f->index];
  621. subtable = &(ddMgr->subtables[level]);
  622. nodelist = subtable->nodelist;
  623. pos = ddHash (cuddT (f), cuddE (f), subtable->shift);
  624. #ifdef DDDMP_DEBUG
  625. /* verify uniqueness to avoid duplicate nodes in unique table */
  626. for (node=nodelist[pos]; node != sentinel; node=node->next)
  627. assert(node!=f);
  628. #endif
  629. T = cuddT (f);
  630. E = cuddE (f);
  631. previousP = &(nodelist[pos]);
  632. looking = *previousP;
  633. while (T < cuddT (looking)) {
  634. previousP = &(looking->next);
  635. looking = *previousP;
  636. }
  637. while (T == cuddT (looking) && E < cuddE (looking)) {
  638. previousP = &(looking->next);
  639. looking = *previousP;
  640. }
  641. f->next = *previousP;
  642. *previousP = f;
  643. return;
  644. }
  645. /**Function********************************************************************
  646. Synopsis [Prints debug info]
  647. Description [Prints debug info for a BDD on the screen. It recurs on
  648. node's children.
  649. ]
  650. SideEffects []
  651. SeeAlso []
  652. ******************************************************************************/
  653. static int
  654. DddmpPrintBddAndNextRecur (
  655. DdManager *ddMgr /* IN: DD Manager */,
  656. DdNode *f /* IN: root of the BDD to be displayed */
  657. )
  658. {
  659. int retValue;
  660. DdNode *fPtr, *tPtr, *ePtr;
  661. fPtr = Cudd_Regular (f);
  662. if (Cudd_IsComplement (f)) {
  663. fprintf (stdout, "sign=- ptr=%ld ", ((long int) fPtr));
  664. } else {
  665. fprintf (stdout, "sign=+ ptr=%ld ", ((long int) fPtr));
  666. }
  667. if (cuddIsConstant (fPtr)) {
  668. fprintf (stdout, "one\n");
  669. fflush (stdout);
  670. return (DDDMP_SUCCESS);
  671. }
  672. fprintf (stdout,
  673. "thenPtr=%ld elsePtr=%ld BddId=%d CnfId=%d Visited=%d\n",
  674. ((long int) cuddT (fPtr)), ((long int) cuddE (fPtr)),
  675. fPtr->index, DddmpReadNodeIndexCnf (fPtr),
  676. DddmpVisitedCnf (fPtr));
  677. tPtr = cuddT (fPtr);
  678. ePtr = cuddE (fPtr);
  679. if (Cudd_IsComplement (f)) {
  680. tPtr = Cudd_Not (tPtr);
  681. ePtr = Cudd_Not (ePtr);
  682. }
  683. retValue = DddmpPrintBddAndNextRecur (ddMgr, tPtr);
  684. retValue = DddmpPrintBddAndNextRecur (ddMgr, ePtr);
  685. return (DDDMP_SUCCESS);
  686. }