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.

932 lines
25 KiB

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