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.

2137 lines
58 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddReorder.c]
  3. PackageName [cudd]
  4. Synopsis [Functions for dynamic variable reordering.]
  5. Description [External procedures included in this file:
  6. <ul>
  7. <li> Cudd_ReduceHeap()
  8. <li> Cudd_ShuffleHeap()
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. <li> cuddDynamicAllocNode()
  13. <li> cuddSifting()
  14. <li> cuddSwapping()
  15. <li> cuddNextHigh()
  16. <li> cuddNextLow()
  17. <li> cuddSwapInPlace()
  18. <li> cuddBddAlignToZdd()
  19. </ul>
  20. Static procedures included in this module:
  21. <ul>
  22. <li> ddUniqueCompare()
  23. <li> ddSwapAny()
  24. <li> ddSiftingAux()
  25. <li> ddSiftingUp()
  26. <li> ddSiftingDown()
  27. <li> ddSiftingBackward()
  28. <li> ddReorderPreprocess()
  29. <li> ddReorderPostprocess()
  30. <li> ddShuffle()
  31. <li> ddSiftUp()
  32. <li> bddFixTree()
  33. </ul>]
  34. Author [Shipra Panda, Bernard Plessier, Fabio Somenzi]
  35. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  36. All rights reserved.
  37. Redistribution and use in source and binary forms, with or without
  38. modification, are permitted provided that the following conditions
  39. are met:
  40. Redistributions of source code must retain the above copyright
  41. notice, this list of conditions and the following disclaimer.
  42. Redistributions in binary form must reproduce the above copyright
  43. notice, this list of conditions and the following disclaimer in the
  44. documentation and/or other materials provided with the distribution.
  45. Neither the name of the University of Colorado nor the names of its
  46. contributors may be used to endorse or promote products derived from
  47. this software without specific prior written permission.
  48. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  51. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  52. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  53. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  54. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  55. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  58. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  59. POSSIBILITY OF SUCH DAMAGE.]
  60. ******************************************************************************/
  61. #include "util.h"
  62. #include "cuddInt.h"
  63. /*---------------------------------------------------------------------------*/
  64. /* Constant declarations */
  65. /*---------------------------------------------------------------------------*/
  66. #define DD_MAX_SUBTABLE_SPARSITY 8
  67. #define DD_SHRINK_FACTOR 2
  68. /*---------------------------------------------------------------------------*/
  69. /* Stucture declarations */
  70. /*---------------------------------------------------------------------------*/
  71. /*---------------------------------------------------------------------------*/
  72. /* Type declarations */
  73. /*---------------------------------------------------------------------------*/
  74. /*---------------------------------------------------------------------------*/
  75. /* Variable declarations */
  76. /*---------------------------------------------------------------------------*/
  77. #ifndef lint
  78. static char rcsid[] DD_UNUSED = "$Id: cuddReorder.c,v 1.71 2012/02/05 01:07:19 fabio Exp $";
  79. #endif
  80. static int *entry;
  81. int ddTotalNumberSwapping;
  82. #ifdef DD_STATS
  83. int ddTotalNISwaps;
  84. #endif
  85. /*---------------------------------------------------------------------------*/
  86. /* Macro declarations */
  87. /*---------------------------------------------------------------------------*/
  88. /**AutomaticStart*************************************************************/
  89. /*---------------------------------------------------------------------------*/
  90. /* Static function prototypes */
  91. /*---------------------------------------------------------------------------*/
  92. static int ddUniqueCompare (int *ptrX, int *ptrY);
  93. static Move * ddSwapAny (DdManager *table, int x, int y);
  94. static int ddSiftingAux (DdManager *table, int x, int xLow, int xHigh);
  95. static Move * ddSiftingUp (DdManager *table, int y, int xLow);
  96. static Move * ddSiftingDown (DdManager *table, int x, int xHigh);
  97. static int ddSiftingBackward (DdManager *table, int size, Move *moves);
  98. static int ddReorderPreprocess (DdManager *table);
  99. static int ddReorderPostprocess (DdManager *table);
  100. static int ddShuffle (DdManager *table, int *permutation);
  101. static int ddSiftUp (DdManager *table, int x, int xLow);
  102. static void bddFixTree (DdManager *table, MtrNode *treenode);
  103. static int ddUpdateMtrTree (DdManager *table, MtrNode *treenode, int *perm, int *invperm);
  104. static int ddCheckPermuation (DdManager *table, MtrNode *treenode, int *perm, int *invperm);
  105. /**AutomaticEnd***************************************************************/
  106. /*---------------------------------------------------------------------------*/
  107. /* Definition of exported functions */
  108. /*---------------------------------------------------------------------------*/
  109. /**Function********************************************************************
  110. Synopsis [Main dynamic reordering routine.]
  111. Description [Main dynamic reordering routine.
  112. Calls one of the possible reordering procedures:
  113. <ul>
  114. <li>Swapping
  115. <li>Sifting
  116. <li>Symmetric Sifting
  117. <li>Group Sifting
  118. <li>Window Permutation
  119. <li>Simulated Annealing
  120. <li>Genetic Algorithm
  121. <li>Dynamic Programming (exact)
  122. </ul>
  123. For sifting, symmetric sifting, group sifting, and window
  124. permutation it is possible to request reordering to convergence.<p>
  125. The core of all methods is the reordering procedure
  126. cuddSwapInPlace() which swaps two adjacent variables and is based
  127. on Rudell's paper.
  128. Returns 1 in case of success; 0 otherwise. In the case of symmetric
  129. sifting (with and without convergence) returns 1 plus the number of
  130. symmetric variables, in case of success.]
  131. SideEffects [Changes the variable order for all diagrams and clears
  132. the cache.]
  133. ******************************************************************************/
  134. int
  135. Cudd_ReduceHeap(
  136. DdManager * table /* DD manager */,
  137. Cudd_ReorderingType heuristic /* method used for reordering */,
  138. int minsize /* bound below which no reordering occurs */)
  139. {
  140. DdHook *hook;
  141. int result;
  142. unsigned int nextDyn;
  143. #ifdef DD_STATS
  144. unsigned int initialSize;
  145. unsigned int finalSize;
  146. #endif
  147. unsigned long localTime;
  148. /* Don't reorder if there are too many dead nodes. */
  149. if (table->keys - table->dead < (unsigned) minsize)
  150. return(1);
  151. if (heuristic == CUDD_REORDER_SAME) {
  152. heuristic = table->autoMethod;
  153. }
  154. if (heuristic == CUDD_REORDER_NONE) {
  155. return(1);
  156. }
  157. /* This call to Cudd_ReduceHeap does initiate reordering. Therefore
  158. ** we count it.
  159. */
  160. table->reorderings++;
  161. localTime = util_cpu_time();
  162. /* Run the hook functions. */
  163. hook = table->preReorderingHook;
  164. while (hook != NULL) {
  165. int res = (hook->f)(table, "BDD", (void *)heuristic);
  166. if (res == 0) return(0);
  167. hook = hook->next;
  168. }
  169. if (!ddReorderPreprocess(table)) return(0);
  170. ddTotalNumberSwapping = 0;
  171. if (table->keys > table->peakLiveNodes) {
  172. table->peakLiveNodes = table->keys;
  173. }
  174. #ifdef DD_STATS
  175. initialSize = table->keys - table->isolated;
  176. ddTotalNISwaps = 0;
  177. switch(heuristic) {
  178. case CUDD_REORDER_RANDOM:
  179. case CUDD_REORDER_RANDOM_PIVOT:
  180. (void) fprintf(table->out,"#:I_RANDOM ");
  181. break;
  182. case CUDD_REORDER_SIFT:
  183. case CUDD_REORDER_SIFT_CONVERGE:
  184. case CUDD_REORDER_SYMM_SIFT:
  185. case CUDD_REORDER_SYMM_SIFT_CONV:
  186. case CUDD_REORDER_GROUP_SIFT:
  187. case CUDD_REORDER_GROUP_SIFT_CONV:
  188. (void) fprintf(table->out,"#:I_SIFTING ");
  189. break;
  190. case CUDD_REORDER_WINDOW2:
  191. case CUDD_REORDER_WINDOW3:
  192. case CUDD_REORDER_WINDOW4:
  193. case CUDD_REORDER_WINDOW2_CONV:
  194. case CUDD_REORDER_WINDOW3_CONV:
  195. case CUDD_REORDER_WINDOW4_CONV:
  196. (void) fprintf(table->out,"#:I_WINDOW ");
  197. break;
  198. case CUDD_REORDER_ANNEALING:
  199. (void) fprintf(table->out,"#:I_ANNEAL ");
  200. break;
  201. case CUDD_REORDER_GENETIC:
  202. (void) fprintf(table->out,"#:I_GENETIC ");
  203. break;
  204. case CUDD_REORDER_LINEAR:
  205. case CUDD_REORDER_LINEAR_CONVERGE:
  206. (void) fprintf(table->out,"#:I_LINSIFT ");
  207. break;
  208. case CUDD_REORDER_EXACT:
  209. (void) fprintf(table->out,"#:I_EXACT ");
  210. break;
  211. default:
  212. return(0);
  213. }
  214. (void) fprintf(table->out,"%8d: initial size",initialSize);
  215. #endif
  216. /* See if we should use alternate threshold for maximum growth. */
  217. if (table->reordCycle && table->reorderings % table->reordCycle == 0) {
  218. double saveGrowth = table->maxGrowth;
  219. table->maxGrowth = table->maxGrowthAlt;
  220. result = cuddTreeSifting(table,heuristic);
  221. table->maxGrowth = saveGrowth;
  222. } else {
  223. result = cuddTreeSifting(table,heuristic);
  224. }
  225. #ifdef DD_STATS
  226. (void) fprintf(table->out,"\n");
  227. finalSize = table->keys - table->isolated;
  228. (void) fprintf(table->out,"#:F_REORDER %8d: final size\n",finalSize);
  229. (void) fprintf(table->out,"#:T_REORDER %8g: total time (sec)\n",
  230. ((double)(util_cpu_time() - localTime)/1000.0));
  231. (void) fprintf(table->out,"#:N_REORDER %8d: total swaps\n",
  232. ddTotalNumberSwapping);
  233. (void) fprintf(table->out,"#:M_REORDER %8d: NI swaps\n",ddTotalNISwaps);
  234. #endif
  235. if (result == 0)
  236. return(0);
  237. if (!ddReorderPostprocess(table))
  238. return(0);
  239. if (table->realign) {
  240. if (!cuddZddAlignToBdd(table))
  241. return(0);
  242. }
  243. nextDyn = (table->keys - table->constants.keys + 1) *
  244. DD_DYN_RATIO + table->constants.keys;
  245. if (table->reorderings < 20 || nextDyn > table->nextDyn)
  246. table->nextDyn = nextDyn;
  247. else
  248. table->nextDyn += 20;
  249. if (table->randomizeOrder != 0) {
  250. table->nextDyn += Cudd_Random() & table->randomizeOrder;
  251. }
  252. table->reordered = 1;
  253. /* Run hook functions. */
  254. hook = table->postReorderingHook;
  255. while (hook != NULL) {
  256. int res = (hook->f)(table, "BDD", (void *)localTime);
  257. if (res == 0) return(0);
  258. hook = hook->next;
  259. }
  260. /* Update cumulative reordering time. */
  261. table->reordTime += util_cpu_time() - localTime;
  262. return(result);
  263. } /* end of Cudd_ReduceHeap */
  264. /**Function********************************************************************
  265. Synopsis [Reorders variables according to given permutation.]
  266. Description [Reorders variables according to given permutation.
  267. The i-th entry of the permutation array contains the index of the variable
  268. that should be brought to the i-th level. The size of the array should be
  269. equal or greater to the number of variables currently in use.
  270. Returns 1 in case of success; 0 otherwise.]
  271. SideEffects [Changes the variable order for all diagrams and clears
  272. the cache.]
  273. SeeAlso [Cudd_ReduceHeap]
  274. ******************************************************************************/
  275. int
  276. Cudd_ShuffleHeap(
  277. DdManager * table /* DD manager */,
  278. int * permutation /* required variable permutation */)
  279. {
  280. int result;
  281. int i;
  282. int identity = 1;
  283. int *perm;
  284. /* Don't waste time in case of identity permutation. */
  285. for (i = 0; i < table->size; i++) {
  286. if (permutation[i] != table->invperm[i]) {
  287. identity = 0;
  288. break;
  289. }
  290. }
  291. if (identity == 1) {
  292. return(1);
  293. }
  294. if (!ddReorderPreprocess(table)) return(0);
  295. if (table->keys > table->peakLiveNodes) {
  296. table->peakLiveNodes = table->keys;
  297. }
  298. perm = ALLOC(int, table->size);
  299. for (i = 0; i < table->size; i++)
  300. perm[permutation[i]] = i;
  301. if (!ddCheckPermuation(table,table->tree,perm,permutation)) {
  302. FREE(perm);
  303. return(0);
  304. }
  305. if (!ddUpdateMtrTree(table,table->tree,perm,permutation)) {
  306. FREE(perm);
  307. return(0);
  308. }
  309. FREE(perm);
  310. result = ddShuffle(table,permutation);
  311. if (!ddReorderPostprocess(table)) return(0);
  312. return(result);
  313. } /* end of Cudd_ShuffleHeap */
  314. /*---------------------------------------------------------------------------*/
  315. /* Definition of internal functions */
  316. /*---------------------------------------------------------------------------*/
  317. /**Function********************************************************************
  318. Synopsis [Dynamically allocates a Node.]
  319. Description [Dynamically allocates a Node. This procedure is similar
  320. to cuddAllocNode in Cudd_Table.c, but it does not attempt garbage
  321. collection, because during reordering there are no dead nodes.
  322. Returns a pointer to a new node if successful; NULL is memory is
  323. full.]
  324. SideEffects [None]
  325. SeeAlso [cuddAllocNode]
  326. ******************************************************************************/
  327. DdNode *
  328. cuddDynamicAllocNode(
  329. DdManager * table)
  330. {
  331. int i;
  332. DdNodePtr *mem;
  333. DdNode *list, *node;
  334. extern DD_OOMFP MMoutOfMemory;
  335. DD_OOMFP saveHandler;
  336. if (table->nextFree == NULL) { /* free list is empty */
  337. /* Try to allocate a new block. */
  338. saveHandler = MMoutOfMemory;
  339. MMoutOfMemory = Cudd_OutOfMem;
  340. mem = (DdNodePtr *) ALLOC(DdNode, DD_MEM_CHUNK + 1);
  341. MMoutOfMemory = saveHandler;
  342. if (mem == NULL && table->stash != NULL) {
  343. FREE(table->stash);
  344. table->stash = NULL;
  345. /* Inhibit resizing of tables. */
  346. table->maxCacheHard = table->cacheSlots - 1;
  347. table->cacheSlack = - (int) (table->cacheSlots + 1);
  348. for (i = 0; i < table->size; i++) {
  349. table->subtables[i].maxKeys <<= 2;
  350. }
  351. mem = (DdNodePtr *) ALLOC(DdNode,DD_MEM_CHUNK + 1);
  352. }
  353. if (mem == NULL) {
  354. /* Out of luck. Call the default handler to do
  355. ** whatever it specifies for a failed malloc. If this
  356. ** handler returns, then set error code, print
  357. ** warning, and return. */
  358. (*MMoutOfMemory)(sizeof(DdNode)*(DD_MEM_CHUNK + 1));
  359. table->errorCode = CUDD_MEMORY_OUT;
  360. #ifdef DD_VERBOSE
  361. (void) fprintf(table->err,
  362. "cuddDynamicAllocNode: out of memory");
  363. (void) fprintf(table->err,"Memory in use = %lu\n",
  364. table->memused);
  365. #endif
  366. return(NULL);
  367. } else { /* successful allocation; slice memory */
  368. unsigned long offset;
  369. table->memused += (DD_MEM_CHUNK + 1) * sizeof(DdNode);
  370. mem[0] = (DdNode *) table->memoryList;
  371. table->memoryList = mem;
  372. /* Here we rely on the fact that the size of a DdNode is a
  373. ** power of 2 and a multiple of the size of a pointer.
  374. ** If we align one node, all the others will be aligned
  375. ** as well. */
  376. offset = (unsigned long) mem & (sizeof(DdNode) - 1);
  377. mem += (sizeof(DdNode) - offset) / sizeof(DdNodePtr);
  378. #ifdef DD_DEBUG
  379. assert(((unsigned long) mem & (sizeof(DdNode) - 1)) == 0);
  380. #endif
  381. list = (DdNode *) mem;
  382. i = 1;
  383. do {
  384. list[i - 1].ref = 0;
  385. list[i - 1].next = &list[i];
  386. } while (++i < DD_MEM_CHUNK);
  387. list[DD_MEM_CHUNK-1].ref = 0;
  388. list[DD_MEM_CHUNK - 1].next = NULL;
  389. table->nextFree = &list[0];
  390. }
  391. } /* if free list empty */
  392. node = table->nextFree;
  393. table->nextFree = node->next;
  394. return (node);
  395. } /* end of cuddDynamicAllocNode */
  396. /**Function********************************************************************
  397. Synopsis [Implementation of Rudell's sifting algorithm.]
  398. Description [Implementation of Rudell's sifting algorithm.
  399. Assumes that no dead nodes are present.
  400. <ol>
  401. <li> Order all the variables according to the number of entries
  402. in each unique table.
  403. <li> Sift the variable up and down, remembering each time the
  404. total size of the DD heap.
  405. <li> Select the best permutation.
  406. <li> Repeat 3 and 4 for all variables.
  407. </ol>
  408. Returns 1 if successful; 0 otherwise.]
  409. SideEffects [None]
  410. ******************************************************************************/
  411. int
  412. cuddSifting(
  413. DdManager * table,
  414. int lower,
  415. int upper)
  416. {
  417. int i;
  418. int *var;
  419. int size;
  420. int x;
  421. int result;
  422. #ifdef DD_STATS
  423. int previousSize;
  424. #endif
  425. size = table->size;
  426. /* Find order in which to sift variables. */
  427. var = NULL;
  428. entry = ALLOC(int,size);
  429. if (entry == NULL) {
  430. table->errorCode = CUDD_MEMORY_OUT;
  431. goto cuddSiftingOutOfMem;
  432. }
  433. var = ALLOC(int,size);
  434. if (var == NULL) {
  435. table->errorCode = CUDD_MEMORY_OUT;
  436. goto cuddSiftingOutOfMem;
  437. }
  438. for (i = 0; i < size; i++) {
  439. x = table->perm[i];
  440. entry[i] = table->subtables[x].keys;
  441. var[i] = i;
  442. }
  443. qsort((void *)var,size,sizeof(int),(DD_QSFP)ddUniqueCompare);
  444. /* Now sift. */
  445. for (i = 0; i < ddMin(table->siftMaxVar,size); i++) {
  446. if (ddTotalNumberSwapping >= table->siftMaxSwap)
  447. break;
  448. if (util_cpu_time() - table->startTime + table->reordTime
  449. > table->timeLimit) {
  450. table->autoDyn = 0; /* prevent further reordering */
  451. break;
  452. }
  453. x = table->perm[var[i]];
  454. if (x < lower || x > upper || table->subtables[x].bindVar == 1)
  455. continue;
  456. #ifdef DD_STATS
  457. previousSize = table->keys - table->isolated;
  458. #endif
  459. result = ddSiftingAux(table, x, lower, upper);
  460. if (!result) goto cuddSiftingOutOfMem;
  461. #ifdef DD_STATS
  462. if (table->keys < (unsigned) previousSize + table->isolated) {
  463. (void) fprintf(table->out,"-");
  464. } else if (table->keys > (unsigned) previousSize + table->isolated) {
  465. (void) fprintf(table->out,"+"); /* should never happen */
  466. (void) fprintf(table->err,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keys - table->isolated, var[i]);
  467. } else {
  468. (void) fprintf(table->out,"=");
  469. }
  470. fflush(table->out);
  471. #endif
  472. }
  473. FREE(var);
  474. FREE(entry);
  475. return(1);
  476. cuddSiftingOutOfMem:
  477. if (entry != NULL) FREE(entry);
  478. if (var != NULL) FREE(var);
  479. return(0);
  480. } /* end of cuddSifting */
  481. /**Function********************************************************************
  482. Synopsis [Reorders variables by a sequence of (non-adjacent) swaps.]
  483. Description [Implementation of Plessier's algorithm that reorders
  484. variables by a sequence of (non-adjacent) swaps.
  485. <ol>
  486. <li> Select two variables (RANDOM or HEURISTIC).
  487. <li> Permute these variables.
  488. <li> If the nodes have decreased accept the permutation.
  489. <li> Otherwise reconstruct the original heap.
  490. <li> Loop.
  491. </ol>
  492. Returns 1 in case of success; 0 otherwise.]
  493. SideEffects [None]
  494. ******************************************************************************/
  495. int
  496. cuddSwapping(
  497. DdManager * table,
  498. int lower,
  499. int upper,
  500. Cudd_ReorderingType heuristic)
  501. {
  502. int i, j;
  503. int max, keys;
  504. int nvars;
  505. int x, y;
  506. int iterate;
  507. int previousSize;
  508. Move *moves, *move;
  509. int pivot;
  510. int modulo;
  511. int result;
  512. #ifdef DD_DEBUG
  513. /* Sanity check */
  514. assert(lower >= 0 && upper < table->size && lower <= upper);
  515. #endif
  516. nvars = upper - lower + 1;
  517. iterate = nvars;
  518. for (i = 0; i < iterate; i++) {
  519. if (ddTotalNumberSwapping >= table->siftMaxSwap)
  520. break;
  521. if (heuristic == CUDD_REORDER_RANDOM_PIVOT) {
  522. max = -1;
  523. for (j = lower; j <= upper; j++) {
  524. if ((keys = table->subtables[j].keys) > max) {
  525. max = keys;
  526. pivot = j;
  527. }
  528. }
  529. modulo = upper - pivot;
  530. if (modulo == 0) {
  531. y = pivot;
  532. } else{
  533. y = pivot + 1 + ((int) Cudd_Random() % modulo);
  534. }
  535. modulo = pivot - lower - 1;
  536. if (modulo < 1) {
  537. x = lower;
  538. } else{
  539. do {
  540. x = (int) Cudd_Random() % modulo;
  541. } while (x == y);
  542. }
  543. } else {
  544. x = ((int) Cudd_Random() % nvars) + lower;
  545. do {
  546. y = ((int) Cudd_Random() % nvars) + lower;
  547. } while (x == y);
  548. }
  549. previousSize = table->keys - table->isolated;
  550. moves = ddSwapAny(table,x,y);
  551. if (moves == NULL) goto cuddSwappingOutOfMem;
  552. result = ddSiftingBackward(table,previousSize,moves);
  553. if (!result) goto cuddSwappingOutOfMem;
  554. while (moves != NULL) {
  555. move = moves->next;
  556. cuddDeallocMove(table, moves);
  557. moves = move;
  558. }
  559. #ifdef DD_STATS
  560. if (table->keys < (unsigned) previousSize + table->isolated) {
  561. (void) fprintf(table->out,"-");
  562. } else if (table->keys > (unsigned) previousSize + table->isolated) {
  563. (void) fprintf(table->out,"+"); /* should never happen */
  564. } else {
  565. (void) fprintf(table->out,"=");
  566. }
  567. fflush(table->out);
  568. #endif
  569. #if 0
  570. (void) fprintf(table->out,"#:t_SWAPPING %8d: tmp size\n",
  571. table->keys - table->isolated);
  572. #endif
  573. }
  574. return(1);
  575. cuddSwappingOutOfMem:
  576. while (moves != NULL) {
  577. move = moves->next;
  578. cuddDeallocMove(table, moves);
  579. moves = move;
  580. }
  581. return(0);
  582. } /* end of cuddSwapping */
  583. /**Function********************************************************************
  584. Synopsis [Finds the next subtable with a larger index.]
  585. Description [Finds the next subtable with a larger index. Returns the
  586. index.]
  587. SideEffects [None]
  588. SeeAlso [cuddNextLow]
  589. ******************************************************************************/
  590. int
  591. cuddNextHigh(
  592. DdManager * table,
  593. int x)
  594. {
  595. return(x+1);
  596. } /* end of cuddNextHigh */
  597. /**Function********************************************************************
  598. Synopsis [Finds the next subtable with a smaller index.]
  599. Description [Finds the next subtable with a smaller index. Returns the
  600. index.]
  601. SideEffects [None]
  602. SeeAlso [cuddNextHigh]
  603. ******************************************************************************/
  604. int
  605. cuddNextLow(
  606. DdManager * table,
  607. int x)
  608. {
  609. return(x-1);
  610. } /* end of cuddNextLow */
  611. /**Function********************************************************************
  612. Synopsis [Swaps two adjacent variables.]
  613. Description [Swaps two adjacent variables. It assumes that no dead
  614. nodes are present on entry to this procedure. The procedure then
  615. guarantees that no dead nodes will be present when it terminates.
  616. cuddSwapInPlace assumes that x &lt; y. Returns the number of keys in
  617. the table if successful; 0 otherwise.]
  618. SideEffects [None]
  619. ******************************************************************************/
  620. int
  621. cuddSwapInPlace(
  622. DdManager * table,
  623. int x,
  624. int y)
  625. {
  626. DdNodePtr *xlist, *ylist;
  627. int xindex, yindex;
  628. int xslots, yslots;
  629. int xshift, yshift;
  630. int oldxkeys, oldykeys;
  631. int newxkeys, newykeys;
  632. int comple, newcomplement;
  633. int i;
  634. Cudd_VariableType varType;
  635. Cudd_LazyGroupType groupType;
  636. int posn;
  637. int isolated;
  638. DdNode *f,*f0,*f1,*f01,*f00,*f11,*f10,*newf1,*newf0;
  639. DdNode *g,*next;
  640. DdNodePtr *previousP;
  641. DdNode *tmp;
  642. DdNode *sentinel = &(table->sentinel);
  643. extern DD_OOMFP MMoutOfMemory;
  644. DD_OOMFP saveHandler;
  645. #ifdef DD_DEBUG
  646. int count,idcheck;
  647. #endif
  648. #ifdef DD_DEBUG
  649. assert(x < y);
  650. assert(cuddNextHigh(table,x) == y);
  651. assert(table->subtables[x].keys != 0);
  652. assert(table->subtables[y].keys != 0);
  653. assert(table->subtables[x].dead == 0);
  654. assert(table->subtables[y].dead == 0);
  655. #endif
  656. ddTotalNumberSwapping++;
  657. /* Get parameters of x subtable. */
  658. xindex = table->invperm[x];
  659. xlist = table->subtables[x].nodelist;
  660. oldxkeys = table->subtables[x].keys;
  661. xslots = table->subtables[x].slots;
  662. xshift = table->subtables[x].shift;
  663. /* Get parameters of y subtable. */
  664. yindex = table->invperm[y];
  665. ylist = table->subtables[y].nodelist;
  666. oldykeys = table->subtables[y].keys;
  667. yslots = table->subtables[y].slots;
  668. yshift = table->subtables[y].shift;
  669. if (!cuddTestInteract(table,xindex,yindex)) {
  670. #ifdef DD_STATS
  671. ddTotalNISwaps++;
  672. #endif
  673. newxkeys = oldxkeys;
  674. newykeys = oldykeys;
  675. } else {
  676. newxkeys = 0;
  677. newykeys = oldykeys;
  678. /* Check whether the two projection functions involved in this
  679. ** swap are isolated. At the end, we'll be able to tell how many
  680. ** isolated projection functions are there by checking only these
  681. ** two functions again. This is done to eliminate the isolated
  682. ** projection functions from the node count.
  683. */
  684. isolated = - ((table->vars[xindex]->ref == 1) +
  685. (table->vars[yindex]->ref == 1));
  686. /* The nodes in the x layer that do not depend on
  687. ** y will stay there; the others are put in a chain.
  688. ** The chain is handled as a LIFO; g points to the beginning.
  689. */
  690. g = NULL;
  691. if ((oldxkeys >= xslots || (unsigned) xslots == table->initSlots) &&
  692. oldxkeys <= DD_MAX_SUBTABLE_DENSITY * xslots) {
  693. for (i = 0; i < xslots; i++) {
  694. previousP = &(xlist[i]);
  695. f = *previousP;
  696. while (f != sentinel) {
  697. next = f->next;
  698. f1 = cuddT(f); f0 = cuddE(f);
  699. if (f1->index != (DdHalfWord) yindex &&
  700. Cudd_Regular(f0)->index != (DdHalfWord) yindex) {
  701. /* stays */
  702. newxkeys++;
  703. *previousP = f;
  704. previousP = &(f->next);
  705. } else {
  706. f->index = yindex;
  707. f->next = g;
  708. g = f;
  709. }
  710. f = next;
  711. } /* while there are elements in the collision chain */
  712. *previousP = sentinel;
  713. } /* for each slot of the x subtable */
  714. } else { /* resize xlist */
  715. DdNode *h = NULL;
  716. DdNodePtr *newxlist;
  717. unsigned int newxslots;
  718. int newxshift;
  719. /* Empty current xlist. Nodes that stay go to list h;
  720. ** nodes that move go to list g. */
  721. for (i = 0; i < xslots; i++) {
  722. f = xlist[i];
  723. while (f != sentinel) {
  724. next = f->next;
  725. f1 = cuddT(f); f0 = cuddE(f);
  726. if (f1->index != (DdHalfWord) yindex &&
  727. Cudd_Regular(f0)->index != (DdHalfWord) yindex) {
  728. /* stays */
  729. f->next = h;
  730. h = f;
  731. newxkeys++;
  732. } else {
  733. f->index = yindex;
  734. f->next = g;
  735. g = f;
  736. }
  737. f = next;
  738. } /* while there are elements in the collision chain */
  739. } /* for each slot of the x subtable */
  740. /* Decide size of new subtable. */
  741. newxshift = xshift;
  742. newxslots = xslots;
  743. while ((unsigned) oldxkeys > DD_MAX_SUBTABLE_DENSITY * newxslots) {
  744. newxshift--;
  745. newxslots <<= 1;
  746. }
  747. while ((unsigned) oldxkeys < newxslots &&
  748. newxslots > table->initSlots) {
  749. newxshift++;
  750. newxslots >>= 1;
  751. }
  752. /* Try to allocate new table. Be ready to back off. */
  753. saveHandler = MMoutOfMemory;
  754. MMoutOfMemory = Cudd_OutOfMem;
  755. newxlist = ALLOC(DdNodePtr, newxslots);
  756. MMoutOfMemory = saveHandler;
  757. if (newxlist == NULL) {
  758. (void) fprintf(table->err, "Unable to resize subtable %d for lack of memory\n", i);
  759. newxlist = xlist;
  760. newxslots = xslots;
  761. newxshift = xshift;
  762. } else {
  763. table->slots += ((int) newxslots - xslots);
  764. table->minDead = (unsigned)
  765. (table->gcFrac * (double) table->slots);
  766. table->cacheSlack = (int)
  767. ddMin(table->maxCacheHard, DD_MAX_CACHE_TO_SLOTS_RATIO
  768. * table->slots) - 2 * (int) table->cacheSlots;
  769. table->memused +=
  770. ((int) newxslots - xslots) * sizeof(DdNodePtr);
  771. FREE(xlist);
  772. xslots = newxslots;
  773. xshift = newxshift;
  774. xlist = newxlist;
  775. }
  776. /* Initialize new subtable. */
  777. for (i = 0; i < xslots; i++) {
  778. xlist[i] = sentinel;
  779. }
  780. /* Move nodes that were parked in list h to their new home. */
  781. f = h;
  782. while (f != NULL) {
  783. next = f->next;
  784. f1 = cuddT(f);
  785. f0 = cuddE(f);
  786. /* Check xlist for pair (f11,f01). */
  787. posn = ddHash(f1, f0, xshift);
  788. /* For each element tmp in collision list xlist[posn]. */
  789. previousP = &(xlist[posn]);
  790. tmp = *previousP;
  791. while (f1 < cuddT(tmp)) {
  792. previousP = &(tmp->next);
  793. tmp = *previousP;
  794. }
  795. while (f1 == cuddT(tmp) && f0 < cuddE(tmp)) {
  796. previousP = &(tmp->next);
  797. tmp = *previousP;
  798. }
  799. f->next = *previousP;
  800. *previousP = f;
  801. f = next;
  802. }
  803. }
  804. #ifdef DD_COUNT
  805. table->swapSteps += oldxkeys - newxkeys;
  806. #endif
  807. /* Take care of the x nodes that must be re-expressed.
  808. ** They form a linked list pointed by g. Their index has been
  809. ** already changed to yindex.
  810. */
  811. f = g;
  812. while (f != NULL) {
  813. next = f->next;
  814. /* Find f1, f0, f11, f10, f01, f00. */
  815. f1 = cuddT(f);
  816. #ifdef DD_DEBUG
  817. assert(!(Cudd_IsComplement(f1)));
  818. #endif
  819. if ((int) f1->index == yindex) {
  820. f11 = cuddT(f1); f10 = cuddE(f1);
  821. } else {
  822. f11 = f10 = f1;
  823. }
  824. #ifdef DD_DEBUG
  825. assert(!(Cudd_IsComplement(f11)));
  826. #endif
  827. f0 = cuddE(f);
  828. comple = Cudd_IsComplement(f0);
  829. f0 = Cudd_Regular(f0);
  830. if ((int) f0->index == yindex) {
  831. f01 = cuddT(f0); f00 = cuddE(f0);
  832. } else {
  833. f01 = f00 = f0;
  834. }
  835. if (comple) {
  836. f01 = Cudd_Not(f01);
  837. f00 = Cudd_Not(f00);
  838. }
  839. /* Decrease ref count of f1. */
  840. cuddSatDec(f1->ref);
  841. /* Create the new T child. */
  842. if (f11 == f01) {
  843. newf1 = f11;
  844. cuddSatInc(newf1->ref);
  845. } else {
  846. /* Check xlist for triple (xindex,f11,f01). */
  847. posn = ddHash(f11, f01, xshift);
  848. /* For each element newf1 in collision list xlist[posn]. */
  849. previousP = &(xlist[posn]);
  850. newf1 = *previousP;
  851. while (f11 < cuddT(newf1)) {
  852. previousP = &(newf1->next);
  853. newf1 = *previousP;
  854. }
  855. while (f11 == cuddT(newf1) && f01 < cuddE(newf1)) {
  856. previousP = &(newf1->next);
  857. newf1 = *previousP;
  858. }
  859. if (cuddT(newf1) == f11 && cuddE(newf1) == f01) {
  860. cuddSatInc(newf1->ref);
  861. } else { /* no match */
  862. newf1 = cuddDynamicAllocNode(table);
  863. if (newf1 == NULL)
  864. goto cuddSwapOutOfMem;
  865. newf1->index = xindex; newf1->ref = 1;
  866. cuddT(newf1) = f11;
  867. cuddE(newf1) = f01;
  868. /* Insert newf1 in the collision list xlist[posn];
  869. ** increase the ref counts of f11 and f01.
  870. */
  871. newxkeys++;
  872. newf1->next = *previousP;
  873. *previousP = newf1;
  874. cuddSatInc(f11->ref);
  875. tmp = Cudd_Regular(f01);
  876. cuddSatInc(tmp->ref);
  877. }
  878. }
  879. cuddT(f) = newf1;
  880. #ifdef DD_DEBUG
  881. assert(!(Cudd_IsComplement(newf1)));
  882. #endif
  883. /* Do the same for f0, keeping complement dots into account. */
  884. /* Decrease ref count of f0. */
  885. tmp = Cudd_Regular(f0);
  886. cuddSatDec(tmp->ref);
  887. /* Create the new E child. */
  888. if (f10 == f00) {
  889. newf0 = f00;
  890. tmp = Cudd_Regular(newf0);
  891. cuddSatInc(tmp->ref);
  892. } else {
  893. /* make sure f10 is regular */
  894. newcomplement = Cudd_IsComplement(f10);
  895. if (newcomplement) {
  896. f10 = Cudd_Not(f10);
  897. f00 = Cudd_Not(f00);
  898. }
  899. /* Check xlist for triple (xindex,f10,f00). */
  900. posn = ddHash(f10, f00, xshift);
  901. /* For each element newf0 in collision list xlist[posn]. */
  902. previousP = &(xlist[posn]);
  903. newf0 = *previousP;
  904. while (f10 < cuddT(newf0)) {
  905. previousP = &(newf0->next);
  906. newf0 = *previousP;
  907. }
  908. while (f10 == cuddT(newf0) && f00 < cuddE(newf0)) {
  909. previousP = &(newf0->next);
  910. newf0 = *previousP;
  911. }
  912. if (cuddT(newf0) == f10 && cuddE(newf0) == f00) {
  913. cuddSatInc(newf0->ref);
  914. } else { /* no match */
  915. newf0 = cuddDynamicAllocNode(table);
  916. if (newf0 == NULL)
  917. goto cuddSwapOutOfMem;
  918. newf0->index = xindex; newf0->ref = 1;
  919. cuddT(newf0) = f10;
  920. cuddE(newf0) = f00;
  921. /* Insert newf0 in the collision list xlist[posn];
  922. ** increase the ref counts of f10 and f00.
  923. */
  924. newxkeys++;
  925. newf0->next = *previousP;
  926. *previousP = newf0;
  927. cuddSatInc(f10->ref);
  928. tmp = Cudd_Regular(f00);
  929. cuddSatInc(tmp->ref);
  930. }
  931. if (newcomplement) {
  932. newf0 = Cudd_Not(newf0);
  933. }
  934. }
  935. cuddE(f) = newf0;
  936. /* Insert the modified f in ylist.
  937. ** The modified f does not already exists in ylist.
  938. ** (Because of the uniqueness of the cofactors.)
  939. */
  940. posn = ddHash(newf1, newf0, yshift);
  941. newykeys++;
  942. previousP = &(ylist[posn]);
  943. tmp = *previousP;
  944. while (newf1 < cuddT(tmp)) {
  945. previousP = &(tmp->next);
  946. tmp = *previousP;
  947. }
  948. while (newf1 == cuddT(tmp) && newf0 < cuddE(tmp)) {
  949. previousP = &(tmp->next);
  950. tmp = *previousP;
  951. }
  952. f->next = *previousP;
  953. *previousP = f;
  954. f = next;
  955. } /* while f != NULL */
  956. /* GC the y layer. */
  957. /* For each node f in ylist. */
  958. for (i = 0; i < yslots; i++) {
  959. previousP = &(ylist[i]);
  960. f = *previousP;
  961. while (f != sentinel) {
  962. next = f->next;
  963. if (f->ref == 0) {
  964. tmp = cuddT(f);
  965. cuddSatDec(tmp->ref);
  966. tmp = Cudd_Regular(cuddE(f));
  967. cuddSatDec(tmp->ref);
  968. cuddDeallocNode(table,f);
  969. newykeys--;
  970. } else {
  971. *previousP = f;
  972. previousP = &(f->next);
  973. }
  974. f = next;
  975. } /* while f */
  976. *previousP = sentinel;
  977. } /* for i */
  978. #ifdef DD_DEBUG
  979. #if 0
  980. (void) fprintf(table->out,"Swapping %d and %d\n",x,y);
  981. #endif
  982. count = 0;
  983. idcheck = 0;
  984. for (i = 0; i < yslots; i++) {
  985. f = ylist[i];
  986. while (f != sentinel) {
  987. count++;
  988. if (f->index != (DdHalfWord) yindex)
  989. idcheck++;
  990. f = f->next;
  991. }
  992. }
  993. if (count != newykeys) {
  994. (void) fprintf(table->out,
  995. "Error in finding newykeys\toldykeys = %d\tnewykeys = %d\tactual = %d\n",
  996. oldykeys,newykeys,count);
  997. }
  998. if (idcheck != 0)
  999. (void) fprintf(table->out,
  1000. "Error in id's of ylist\twrong id's = %d\n",
  1001. idcheck);
  1002. count = 0;
  1003. idcheck = 0;
  1004. for (i = 0; i < xslots; i++) {
  1005. f = xlist[i];
  1006. while (f != sentinel) {
  1007. count++;
  1008. if (f->index != (DdHalfWord) xindex)
  1009. idcheck++;
  1010. f = f->next;
  1011. }
  1012. }
  1013. if (count != newxkeys) {
  1014. (void) fprintf(table->out,
  1015. "Error in finding newxkeys\toldxkeys = %d \tnewxkeys = %d \tactual = %d\n",
  1016. oldxkeys,newxkeys,count);
  1017. }
  1018. if (idcheck != 0)
  1019. (void) fprintf(table->out,
  1020. "Error in id's of xlist\twrong id's = %d\n",
  1021. idcheck);
  1022. #endif
  1023. isolated += (table->vars[xindex]->ref == 1) +
  1024. (table->vars[yindex]->ref == 1);
  1025. table->isolated += isolated;
  1026. }
  1027. /* Set the appropriate fields in table. */
  1028. table->subtables[x].nodelist = ylist;
  1029. table->subtables[x].slots = yslots;
  1030. table->subtables[x].shift = yshift;
  1031. table->subtables[x].keys = newykeys;
  1032. table->subtables[x].maxKeys = yslots * DD_MAX_SUBTABLE_DENSITY;
  1033. i = table->subtables[x].bindVar;
  1034. table->subtables[x].bindVar = table->subtables[y].bindVar;
  1035. table->subtables[y].bindVar = i;
  1036. /* Adjust filds for lazy sifting. */
  1037. varType = table->subtables[x].varType;
  1038. table->subtables[x].varType = table->subtables[y].varType;
  1039. table->subtables[y].varType = varType;
  1040. i = table->subtables[x].pairIndex;
  1041. table->subtables[x].pairIndex = table->subtables[y].pairIndex;
  1042. table->subtables[y].pairIndex = i;
  1043. i = table->subtables[x].varHandled;
  1044. table->subtables[x].varHandled = table->subtables[y].varHandled;
  1045. table->subtables[y].varHandled = i;
  1046. groupType = table->subtables[x].varToBeGrouped;
  1047. table->subtables[x].varToBeGrouped = table->subtables[y].varToBeGrouped;
  1048. table->subtables[y].varToBeGrouped = groupType;
  1049. table->subtables[y].nodelist = xlist;
  1050. table->subtables[y].slots = xslots;
  1051. table->subtables[y].shift = xshift;
  1052. table->subtables[y].keys = newxkeys;
  1053. table->subtables[y].maxKeys = xslots * DD_MAX_SUBTABLE_DENSITY;
  1054. table->perm[xindex] = y; table->perm[yindex] = x;
  1055. table->invperm[x] = yindex; table->invperm[y] = xindex;
  1056. table->keys += newxkeys + newykeys - oldxkeys - oldykeys;
  1057. return(table->keys - table->isolated);
  1058. cuddSwapOutOfMem:
  1059. (void) fprintf(table->err,"Error: cuddSwapInPlace out of memory\n");
  1060. return (0);
  1061. } /* end of cuddSwapInPlace */
  1062. /**Function********************************************************************
  1063. Synopsis [Reorders BDD variables according to the order of the ZDD
  1064. variables.]
  1065. Description [Reorders BDD variables according to the order of the
  1066. ZDD variables. This function can be called at the end of ZDD
  1067. reordering to insure that the order of the BDD variables is
  1068. consistent with the order of the ZDD variables. The number of ZDD
  1069. variables must be a multiple of the number of BDD variables. Let
  1070. <code>M</code> be the ratio of the two numbers. cuddBddAlignToZdd
  1071. then considers the ZDD variables from <code>M*i</code> to
  1072. <code>(M+1)*i-1</code> as corresponding to BDD variable
  1073. <code>i</code>. This function should be normally called from
  1074. Cudd_zddReduceHeap, which clears the cache. Returns 1 in case of
  1075. success; 0 otherwise.]
  1076. SideEffects [Changes the BDD variable order for all diagrams and performs
  1077. garbage collection of the BDD unique table.]
  1078. SeeAlso [Cudd_ShuffleHeap Cudd_zddReduceHeap]
  1079. ******************************************************************************/
  1080. int
  1081. cuddBddAlignToZdd(
  1082. DdManager * table /* DD manager */)
  1083. {
  1084. int *invperm; /* permutation array */
  1085. int M; /* ratio of ZDD variables to BDD variables */
  1086. int i; /* loop index */
  1087. int result; /* return value */
  1088. /* We assume that a ratio of 0 is OK. */
  1089. if (table->size == 0)
  1090. return(1);
  1091. M = table->sizeZ / table->size;
  1092. /* Check whether the number of ZDD variables is a multiple of the
  1093. ** number of BDD variables.
  1094. */
  1095. if (M * table->size != table->sizeZ)
  1096. return(0);
  1097. /* Create and initialize the inverse permutation array. */
  1098. invperm = ALLOC(int,table->size);
  1099. if (invperm == NULL) {
  1100. table->errorCode = CUDD_MEMORY_OUT;
  1101. return(0);
  1102. }
  1103. for (i = 0; i < table->sizeZ; i += M) {
  1104. int indexZ = table->invpermZ[i];
  1105. int index = indexZ / M;
  1106. invperm[i / M] = index;
  1107. }
  1108. /* Eliminate dead nodes. Do not scan the cache again, because we
  1109. ** assume that Cudd_zddReduceHeap has already cleared it.
  1110. */
  1111. cuddGarbageCollect(table,0);
  1112. /* Initialize number of isolated projection functions. */
  1113. table->isolated = 0;
  1114. for (i = 0; i < table->size; i++) {
  1115. if (table->vars[i]->ref == 1) table->isolated++;
  1116. }
  1117. /* Initialize the interaction matrix. */
  1118. result = cuddInitInteract(table);
  1119. if (result == 0) return(0);
  1120. result = ddShuffle(table, invperm);
  1121. FREE(invperm);
  1122. /* Free interaction matrix. */
  1123. FREE(table->interact);
  1124. /* Fix the BDD variable group tree. */
  1125. bddFixTree(table,table->tree);
  1126. return(result);
  1127. } /* end of cuddBddAlignToZdd */
  1128. /*---------------------------------------------------------------------------*/
  1129. /* Definition of static functions */
  1130. /*---------------------------------------------------------------------------*/
  1131. /**Function********************************************************************
  1132. Synopsis [Comparison function used by qsort.]
  1133. Description [Comparison function used by qsort to order the
  1134. variables according to the number of keys in the subtables.
  1135. Returns the difference in number of keys between the two
  1136. variables being compared.]
  1137. SideEffects [None]
  1138. ******************************************************************************/
  1139. static int
  1140. ddUniqueCompare(
  1141. int * ptrX,
  1142. int * ptrY)
  1143. {
  1144. #if 0
  1145. if (entry[*ptrY] == entry[*ptrX]) {
  1146. return((*ptrX) - (*ptrY));
  1147. }
  1148. #endif
  1149. return(entry[*ptrY] - entry[*ptrX]);
  1150. } /* end of ddUniqueCompare */
  1151. /**Function********************************************************************
  1152. Synopsis [Swaps any two variables.]
  1153. Description [Swaps any two variables. Returns the set of moves.]
  1154. SideEffects [None]
  1155. ******************************************************************************/
  1156. static Move *
  1157. ddSwapAny(
  1158. DdManager * table,
  1159. int x,
  1160. int y)
  1161. {
  1162. Move *move, *moves;
  1163. int xRef,yRef;
  1164. int xNext,yNext;
  1165. int size;
  1166. int limitSize;
  1167. int tmp;
  1168. if (x >y) {
  1169. tmp = x; x = y; y = tmp;
  1170. }
  1171. xRef = x; yRef = y;
  1172. xNext = cuddNextHigh(table,x);
  1173. yNext = cuddNextLow(table,y);
  1174. moves = NULL;
  1175. limitSize = table->keys - table->isolated;
  1176. for (;;) {
  1177. if ( xNext == yNext) {
  1178. size = cuddSwapInPlace(table,x,xNext);
  1179. if (size == 0) goto ddSwapAnyOutOfMem;
  1180. move = (Move *) cuddDynamicAllocNode(table);
  1181. if (move == NULL) goto ddSwapAnyOutOfMem;
  1182. move->x = x;
  1183. move->y = xNext;
  1184. move->size = size;
  1185. move->next = moves;
  1186. moves = move;
  1187. size = cuddSwapInPlace(table,yNext,y);
  1188. if (size == 0) goto ddSwapAnyOutOfMem;
  1189. move = (Move *) cuddDynamicAllocNode(table);
  1190. if (move == NULL) goto ddSwapAnyOutOfMem;
  1191. move->x = yNext;
  1192. move->y = y;
  1193. move->size = size;
  1194. move->next = moves;
  1195. moves = move;
  1196. size = cuddSwapInPlace(table,x,xNext);
  1197. if (size == 0) goto ddSwapAnyOutOfMem;
  1198. move = (Move *) cuddDynamicAllocNode(table);
  1199. if (move == NULL) goto ddSwapAnyOutOfMem;
  1200. move->x = x;
  1201. move->y = xNext;
  1202. move->size = size;
  1203. move->next = moves;
  1204. moves = move;
  1205. tmp = x; x = y; y = tmp;
  1206. } else if (x == yNext) {
  1207. size = cuddSwapInPlace(table,x,xNext);
  1208. if (size == 0) goto ddSwapAnyOutOfMem;
  1209. move = (Move *) cuddDynamicAllocNode(table);
  1210. if (move == NULL) goto ddSwapAnyOutOfMem;
  1211. move->x = x;
  1212. move->y = xNext;
  1213. move->size = size;
  1214. move->next = moves;
  1215. moves = move;
  1216. tmp = x; x = y; y = tmp;
  1217. } else {
  1218. size = cuddSwapInPlace(table,x,xNext);
  1219. if (size == 0) goto ddSwapAnyOutOfMem;
  1220. move = (Move *) cuddDynamicAllocNode(table);
  1221. if (move == NULL) goto ddSwapAnyOutOfMem;
  1222. move->x = x;
  1223. move->y = xNext;
  1224. move->size = size;
  1225. move->next = moves;
  1226. moves = move;
  1227. size = cuddSwapInPlace(table,yNext,y);
  1228. if (size == 0) goto ddSwapAnyOutOfMem;
  1229. move = (Move *) cuddDynamicAllocNode(table);
  1230. if (move == NULL) goto ddSwapAnyOutOfMem;
  1231. move->x = yNext;
  1232. move->y = y;
  1233. move->size = size;
  1234. move->next = moves;
  1235. moves = move;
  1236. x = xNext;
  1237. y = yNext;
  1238. }
  1239. xNext = cuddNextHigh(table,x);
  1240. yNext = cuddNextLow(table,y);
  1241. if (xNext > yRef) break;
  1242. if ((double) size > table->maxGrowth * (double) limitSize) break;
  1243. if (size < limitSize) limitSize = size;
  1244. }
  1245. if (yNext>=xRef) {
  1246. size = cuddSwapInPlace(table,yNext,y);
  1247. if (size == 0) goto ddSwapAnyOutOfMem;
  1248. move = (Move *) cuddDynamicAllocNode(table);
  1249. if (move == NULL) goto ddSwapAnyOutOfMem;
  1250. move->x = yNext;
  1251. move->y = y;
  1252. move->size = size;
  1253. move->next = moves;
  1254. moves = move;
  1255. }
  1256. return(moves);
  1257. ddSwapAnyOutOfMem:
  1258. while (moves != NULL) {
  1259. move = moves->next;
  1260. cuddDeallocMove(table, moves);
  1261. moves = move;
  1262. }
  1263. return(NULL);
  1264. } /* end of ddSwapAny */
  1265. /**Function********************************************************************
  1266. Synopsis [Given xLow <= x <= xHigh moves x up and down between the
  1267. boundaries.]
  1268. Description [Given xLow <= x <= xHigh moves x up and down between the
  1269. boundaries. Finds the best position and does the required changes.
  1270. Returns 1 if successful; 0 otherwise.]
  1271. SideEffects [None]
  1272. ******************************************************************************/
  1273. static int
  1274. ddSiftingAux(
  1275. DdManager * table,
  1276. int x,
  1277. int xLow,
  1278. int xHigh)
  1279. {
  1280. Move *move;
  1281. Move *moveUp; /* list of up moves */
  1282. Move *moveDown; /* list of down moves */
  1283. int initialSize;
  1284. int result;
  1285. initialSize = table->keys - table->isolated;
  1286. moveDown = NULL;
  1287. moveUp = NULL;
  1288. if (x == xLow) {
  1289. moveDown = ddSiftingDown(table,x,xHigh);
  1290. /* At this point x --> xHigh unless bounding occurred. */
  1291. if (moveDown == (Move *) CUDD_OUT_OF_MEM) goto ddSiftingAuxOutOfMem;
  1292. /* Move backward and stop at best position. */
  1293. result = ddSiftingBackward(table,initialSize,moveDown);
  1294. if (!result) goto ddSiftingAuxOutOfMem;
  1295. } else if (x == xHigh) {
  1296. moveUp = ddSiftingUp(table,x,xLow);
  1297. /* At this point x --> xLow unless bounding occurred. */
  1298. if (moveUp == (Move *) CUDD_OUT_OF_MEM) goto ddSiftingAuxOutOfMem;
  1299. /* Move backward and stop at best position. */
  1300. result = ddSiftingBackward(table,initialSize,moveUp);
  1301. if (!result) goto ddSiftingAuxOutOfMem;
  1302. } else if ((x - xLow) > (xHigh - x)) { /* must go down first: shorter */
  1303. moveDown = ddSiftingDown(table,x,xHigh);
  1304. /* At this point x --> xHigh unless bounding occurred. */
  1305. if (moveDown == (Move *) CUDD_OUT_OF_MEM) goto ddSiftingAuxOutOfMem;
  1306. if (moveDown != NULL) {
  1307. x = moveDown->y;
  1308. }
  1309. moveUp = ddSiftingUp(table,x,xLow);
  1310. if (moveUp == (Move *) CUDD_OUT_OF_MEM) goto ddSiftingAuxOutOfMem;
  1311. /* Move backward and stop at best position */
  1312. result = ddSiftingBackward(table,initialSize,moveUp);
  1313. if (!result) goto ddSiftingAuxOutOfMem;
  1314. } else { /* must go up first: shorter */
  1315. moveUp = ddSiftingUp(table,x,xLow);
  1316. /* At this point x --> xLow unless bounding occurred. */
  1317. if (moveUp == (Move *) CUDD_OUT_OF_MEM) goto ddSiftingAuxOutOfMem;
  1318. if (moveUp != NULL) {
  1319. x = moveUp->x;
  1320. }
  1321. moveDown = ddSiftingDown(table,x,xHigh);
  1322. if (moveDown == (Move *) CUDD_OUT_OF_MEM) goto ddSiftingAuxOutOfMem;
  1323. /* Move backward and stop at best position. */
  1324. result = ddSiftingBackward(table,initialSize,moveDown);
  1325. if (!result) goto ddSiftingAuxOutOfMem;
  1326. }
  1327. while (moveDown != NULL) {
  1328. move = moveDown->next;
  1329. cuddDeallocMove(table, moveDown);
  1330. moveDown = move;
  1331. }
  1332. while (moveUp != NULL) {
  1333. move = moveUp->next;
  1334. cuddDeallocMove(table, moveUp);
  1335. moveUp = move;
  1336. }
  1337. return(1);
  1338. ddSiftingAuxOutOfMem:
  1339. if (moveDown != (Move *) CUDD_OUT_OF_MEM) {
  1340. while (moveDown != NULL) {
  1341. move = moveDown->next;
  1342. cuddDeallocMove(table, moveDown);
  1343. moveDown = move;
  1344. }
  1345. }
  1346. if (moveUp != (Move *) CUDD_OUT_OF_MEM) {
  1347. while (moveUp != NULL) {
  1348. move = moveUp->next;
  1349. cuddDeallocMove(table, moveUp);
  1350. moveUp = move;
  1351. }
  1352. }
  1353. return(0);
  1354. } /* end of ddSiftingAux */
  1355. /**Function********************************************************************
  1356. Synopsis [Sifts a variable up.]
  1357. Description [Sifts a variable up. Moves y up until either it reaches
  1358. the bound (xLow) or the size of the DD heap increases too much.
  1359. Returns the set of moves in case of success; NULL if memory is full.]
  1360. SideEffects [None]
  1361. ******************************************************************************/
  1362. static Move *
  1363. ddSiftingUp(
  1364. DdManager * table,
  1365. int y,
  1366. int xLow)
  1367. {
  1368. Move *moves;
  1369. Move *move;
  1370. int x;
  1371. int size;
  1372. int limitSize;
  1373. int xindex, yindex;
  1374. int isolated;
  1375. int L; /* lower bound on DD size */
  1376. #ifdef DD_DEBUG
  1377. int checkL;
  1378. int z;
  1379. int zindex;
  1380. #endif
  1381. moves = NULL;
  1382. yindex = table->invperm[y];
  1383. /* Initialize the lower bound.
  1384. ** The part of the DD below y will not change.
  1385. ** The part of the DD above y that does not interact with y will not
  1386. ** change. The rest may vanish in the best case, except for
  1387. ** the nodes at level xLow, which will not vanish, regardless.
  1388. */
  1389. limitSize = L = table->keys - table->isolated;
  1390. for (x = xLow + 1; x < y; x++) {
  1391. xindex = table->invperm[x];
  1392. if (cuddTestInteract(table,xindex,yindex)) {
  1393. isolated = table->vars[xindex]->ref == 1;
  1394. L -= table->subtables[x].keys - isolated;
  1395. }
  1396. }
  1397. isolated = table->vars[yindex]->ref == 1;
  1398. L -= table->subtables[y].keys - isolated;
  1399. x = cuddNextLow(table,y);
  1400. while (x >= xLow && L <= limitSize) {
  1401. xindex = table->invperm[x];
  1402. #ifdef DD_DEBUG
  1403. checkL = table->keys - table->isolated;
  1404. for (z = xLow + 1; z < y; z++) {
  1405. zindex = table->invperm[z];
  1406. if (cuddTestInteract(table,zindex,yindex)) {
  1407. isolated = table->vars[zindex]->ref == 1;
  1408. checkL -= table->subtables[z].keys - isolated;
  1409. }
  1410. }
  1411. isolated = table->vars[yindex]->ref == 1;
  1412. checkL -= table->subtables[y].keys - isolated;
  1413. assert(L == checkL);
  1414. #endif
  1415. size = cuddSwapInPlace(table,x,y);
  1416. if (size == 0) goto ddSiftingUpOutOfMem;
  1417. /* Update the lower bound. */
  1418. if (cuddTestInteract(table,xindex,yindex)) {
  1419. isolated = table->vars[xindex]->ref == 1;
  1420. L += table->subtables[y].keys - isolated;
  1421. }
  1422. move = (Move *) cuddDynamicAllocNode(table);
  1423. if (move == NULL) goto ddSiftingUpOutOfMem;
  1424. move->x = x;
  1425. move->y = y;
  1426. move->size = size;
  1427. move->next = moves;
  1428. moves = move;
  1429. if ((double) size > (double) limitSize * table->maxGrowth) break;
  1430. if (size < limitSize) limitSize = size;
  1431. y = x;
  1432. x = cuddNextLow(table,y);
  1433. }
  1434. return(moves);
  1435. ddSiftingUpOutOfMem:
  1436. while (moves != NULL) {
  1437. move = moves->next;
  1438. cuddDeallocMove(table, moves);
  1439. moves = move;
  1440. }
  1441. return((Move *) CUDD_OUT_OF_MEM);
  1442. } /* end of ddSiftingUp */
  1443. /**Function********************************************************************
  1444. Synopsis [Sifts a variable down.]
  1445. Description [Sifts a variable down. Moves x down until either it
  1446. reaches the bound (xHigh) or the size of the DD heap increases too
  1447. much. Returns the set of moves in case of success; NULL if memory is
  1448. full.]
  1449. SideEffects [None]
  1450. ******************************************************************************/
  1451. static Move *
  1452. ddSiftingDown(
  1453. DdManager * table,
  1454. int x,
  1455. int xHigh)
  1456. {
  1457. Move *moves;
  1458. Move *move;
  1459. int y;
  1460. int size;
  1461. int R; /* upper bound on node decrease */
  1462. int limitSize;
  1463. int xindex, yindex;
  1464. int isolated;
  1465. #ifdef DD_DEBUG
  1466. int checkR;
  1467. int z;
  1468. int zindex;
  1469. #endif
  1470. moves = NULL;
  1471. /* Initialize R */
  1472. xindex = table->invperm[x];
  1473. limitSize = size = table->keys - table->isolated;
  1474. R = 0;
  1475. for (y = xHigh; y > x; y--) {
  1476. yindex = table->invperm[y];
  1477. if (cuddTestInteract(table,xindex,yindex)) {
  1478. isolated = table->vars[yindex]->ref == 1;
  1479. R += table->subtables[y].keys - isolated;
  1480. }
  1481. }
  1482. y = cuddNextHigh(table,x);
  1483. while (y <= xHigh && size - R < limitSize) {
  1484. #ifdef DD_DEBUG
  1485. checkR = 0;
  1486. for (z = xHigh; z > x; z--) {
  1487. zindex = table->invperm[z];
  1488. if (cuddTestInteract(table,xindex,zindex)) {
  1489. isolated = table->vars[zindex]->ref == 1;
  1490. checkR += table->subtables[z].keys - isolated;
  1491. }
  1492. }
  1493. assert(R == checkR);
  1494. #endif
  1495. /* Update upper bound on node decrease. */
  1496. yindex = table->invperm[y];
  1497. if (cuddTestInteract(table,xindex,yindex)) {
  1498. isolated = table->vars[yindex]->ref == 1;
  1499. R -= table->subtables[y].keys - isolated;
  1500. }
  1501. size = cuddSwapInPlace(table,x,y);
  1502. if (size == 0) goto ddSiftingDownOutOfMem;
  1503. move = (Move *) cuddDynamicAllocNode(table);
  1504. if (move == NULL) goto ddSiftingDownOutOfMem;
  1505. move->x = x;
  1506. move->y = y;
  1507. move->size = size;
  1508. move->next = moves;
  1509. moves = move;
  1510. if ((double) size > (double) limitSize * table->maxGrowth) break;
  1511. if (size < limitSize) limitSize = size;
  1512. x = y;
  1513. y = cuddNextHigh(table,x);
  1514. }
  1515. return(moves);
  1516. ddSiftingDownOutOfMem:
  1517. while (moves != NULL) {
  1518. move = moves->next;
  1519. cuddDeallocMove(table, moves);
  1520. moves = move;
  1521. }
  1522. return((Move *) CUDD_OUT_OF_MEM);
  1523. } /* end of ddSiftingDown */
  1524. /**Function********************************************************************
  1525. Synopsis [Given a set of moves, returns the DD heap to the position
  1526. giving the minimum size.]
  1527. Description [Given a set of moves, returns the DD heap to the
  1528. position giving the minimum size. In case of ties, returns to the
  1529. closest position giving the minimum size. Returns 1 in case of
  1530. success; 0 otherwise.]
  1531. SideEffects [None]
  1532. ******************************************************************************/
  1533. static int
  1534. ddSiftingBackward(
  1535. DdManager * table,
  1536. int size,
  1537. Move * moves)
  1538. {
  1539. Move *move;
  1540. int res;
  1541. for (move = moves; move != NULL; move = move->next) {
  1542. if (move->size < size) {
  1543. size = move->size;
  1544. }
  1545. }
  1546. for (move = moves; move != NULL; move = move->next) {
  1547. if (move->size == size) return(1);
  1548. res = cuddSwapInPlace(table,(int)move->x,(int)move->y);
  1549. if (!res) return(0);
  1550. }
  1551. return(1);
  1552. } /* end of ddSiftingBackward */
  1553. /**Function********************************************************************
  1554. Synopsis [Prepares the DD heap for dynamic reordering.]
  1555. Description [Prepares the DD heap for dynamic reordering. Does
  1556. garbage collection, to guarantee that there are no dead nodes;
  1557. clears the cache, which is invalidated by dynamic reordering; initializes
  1558. the number of isolated projection functions; and initializes the
  1559. interaction matrix. Returns 1 in case of success; 0 otherwise.]
  1560. SideEffects [None]
  1561. ******************************************************************************/
  1562. static int
  1563. ddReorderPreprocess(
  1564. DdManager * table)
  1565. {
  1566. int i;
  1567. int res;
  1568. /* Clear the cache. */
  1569. cuddCacheFlush(table);
  1570. cuddLocalCacheClearAll(table);
  1571. /* Eliminate dead nodes. Do not scan the cache again. */
  1572. cuddGarbageCollect(table,0);
  1573. /* Initialize number of isolated projection functions. */
  1574. table->isolated = 0;
  1575. for (i = 0; i < table->size; i++) {
  1576. if (table->vars[i]->ref == 1) table->isolated++;
  1577. }
  1578. /* Initialize the interaction matrix. */
  1579. res = cuddInitInteract(table);
  1580. if (res == 0) return(0);
  1581. return(1);
  1582. } /* end of ddReorderPreprocess */
  1583. /**Function********************************************************************
  1584. Synopsis [Cleans up at the end of reordering.]
  1585. Description []
  1586. SideEffects [None]
  1587. ******************************************************************************/
  1588. static int
  1589. ddReorderPostprocess(
  1590. DdManager * table)
  1591. {
  1592. #ifdef DD_VERBOSE
  1593. (void) fflush(table->out);
  1594. #endif
  1595. /* Free interaction matrix. */
  1596. FREE(table->interact);
  1597. return(1);
  1598. } /* end of ddReorderPostprocess */
  1599. /**Function********************************************************************
  1600. Synopsis [Reorders variables according to a given permutation.]
  1601. Description [Reorders variables according to a given permutation.
  1602. The i-th permutation array contains the index of the variable that
  1603. should be brought to the i-th level. ddShuffle assumes that no
  1604. dead nodes are present and that the interaction matrix is properly
  1605. initialized. The reordering is achieved by a series of upward sifts.
  1606. Returns 1 if successful; 0 otherwise.]
  1607. SideEffects [None]
  1608. SeeAlso []
  1609. ******************************************************************************/
  1610. static int
  1611. ddShuffle(
  1612. DdManager * table,
  1613. int * permutation)
  1614. {
  1615. int index;
  1616. int level;
  1617. int position;
  1618. int numvars;
  1619. int result;
  1620. #ifdef DD_STATS
  1621. unsigned long localTime;
  1622. int initialSize;
  1623. int finalSize;
  1624. int previousSize;
  1625. #endif
  1626. ddTotalNumberSwapping = 0;
  1627. #ifdef DD_STATS
  1628. localTime = util_cpu_time();
  1629. initialSize = table->keys - table->isolated;
  1630. (void) fprintf(table->out,"#:I_SHUFFLE %8d: initial size\n",
  1631. initialSize);
  1632. ddTotalNISwaps = 0;
  1633. #endif
  1634. numvars = table->size;
  1635. for (level = 0; level < numvars; level++) {
  1636. index = permutation[level];
  1637. position = table->perm[index];
  1638. #ifdef DD_STATS
  1639. previousSize = table->keys - table->isolated;
  1640. #endif
  1641. result = ddSiftUp(table,position,level);
  1642. if (!result) return(0);
  1643. #ifdef DD_STATS
  1644. if (table->keys < (unsigned) previousSize + table->isolated) {
  1645. (void) fprintf(table->out,"-");
  1646. } else if (table->keys > (unsigned) previousSize + table->isolated) {
  1647. (void) fprintf(table->out,"+"); /* should never happen */
  1648. } else {
  1649. (void) fprintf(table->out,"=");
  1650. }
  1651. fflush(table->out);
  1652. #endif
  1653. }
  1654. #ifdef DD_STATS
  1655. (void) fprintf(table->out,"\n");
  1656. finalSize = table->keys - table->isolated;
  1657. (void) fprintf(table->out,"#:F_SHUFFLE %8d: final size\n",finalSize);
  1658. (void) fprintf(table->out,"#:T_SHUFFLE %8g: total time (sec)\n",
  1659. ((double)(util_cpu_time() - localTime)/1000.0));
  1660. (void) fprintf(table->out,"#:N_SHUFFLE %8d: total swaps\n",
  1661. ddTotalNumberSwapping);
  1662. (void) fprintf(table->out,"#:M_SHUFFLE %8d: NI swaps\n",ddTotalNISwaps);
  1663. #endif
  1664. return(1);
  1665. } /* end of ddShuffle */
  1666. /**Function********************************************************************
  1667. Synopsis [Moves one variable up.]
  1668. Description [Takes a variable from position x and sifts it up to
  1669. position xLow; xLow should be less than or equal to x.
  1670. Returns 1 if successful; 0 otherwise]
  1671. SideEffects [None]
  1672. SeeAlso []
  1673. ******************************************************************************/
  1674. static int
  1675. ddSiftUp(
  1676. DdManager * table,
  1677. int x,
  1678. int xLow)
  1679. {
  1680. int y;
  1681. int size;
  1682. y = cuddNextLow(table,x);
  1683. while (y >= xLow) {
  1684. size = cuddSwapInPlace(table,y,x);
  1685. if (size == 0) {
  1686. return(0);
  1687. }
  1688. x = y;
  1689. y = cuddNextLow(table,x);
  1690. }
  1691. return(1);
  1692. } /* end of ddSiftUp */
  1693. /**Function********************************************************************
  1694. Synopsis [Fixes the BDD variable group tree after a shuffle.]
  1695. Description [Fixes the BDD variable group tree after a
  1696. shuffle. Assumes that the order of the variables in a terminal node
  1697. has not been changed.]
  1698. SideEffects [Changes the BDD variable group tree.]
  1699. SeeAlso []
  1700. ******************************************************************************/
  1701. static void
  1702. bddFixTree(
  1703. DdManager * table,
  1704. MtrNode * treenode)
  1705. {
  1706. if (treenode == NULL) return;
  1707. treenode->low = ((int) treenode->index < table->size) ?
  1708. table->perm[treenode->index] : treenode->index;
  1709. if (treenode->child != NULL) {
  1710. bddFixTree(table, treenode->child);
  1711. }
  1712. if (treenode->younger != NULL)
  1713. bddFixTree(table, treenode->younger);
  1714. if (treenode->parent != NULL && treenode->low < treenode->parent->low) {
  1715. treenode->parent->low = treenode->low;
  1716. treenode->parent->index = treenode->index;
  1717. }
  1718. return;
  1719. } /* end of bddFixTree */
  1720. /**Function********************************************************************
  1721. Synopsis [Updates the BDD variable group tree before a shuffle.]
  1722. Description [Updates the BDD variable group tree before a shuffle.
  1723. Returns 1 if successful; 0 otherwise.]
  1724. SideEffects [Changes the BDD variable group tree.]
  1725. SeeAlso []
  1726. ******************************************************************************/
  1727. static int
  1728. ddUpdateMtrTree(
  1729. DdManager * table,
  1730. MtrNode * treenode,
  1731. int * perm,
  1732. int * invperm)
  1733. {
  1734. unsigned int i, size;
  1735. int index, level, minLevel, maxLevel, minIndex;
  1736. if (treenode == NULL) return(1);
  1737. minLevel = CUDD_MAXINDEX;
  1738. maxLevel = 0;
  1739. minIndex = -1;
  1740. /* i : level */
  1741. for (i = treenode->low; i < treenode->low + treenode->size; i++) {
  1742. index = table->invperm[i];
  1743. level = perm[index];
  1744. if (level < minLevel) {
  1745. minLevel = level;
  1746. minIndex = index;
  1747. }
  1748. if (level > maxLevel)
  1749. maxLevel = level;
  1750. }
  1751. size = maxLevel - minLevel + 1;
  1752. if (minIndex == -1) return(0);
  1753. if (size == treenode->size) {
  1754. treenode->low = minLevel;
  1755. treenode->index = minIndex;
  1756. } else {
  1757. return(0);
  1758. }
  1759. if (treenode->child != NULL) {
  1760. if (!ddUpdateMtrTree(table, treenode->child, perm, invperm))
  1761. return(0);
  1762. }
  1763. if (treenode->younger != NULL) {
  1764. if (!ddUpdateMtrTree(table, treenode->younger, perm, invperm))
  1765. return(0);
  1766. }
  1767. return(1);
  1768. }
  1769. /**Function********************************************************************
  1770. Synopsis [Checks the BDD variable group tree before a shuffle.]
  1771. Description [Checks the BDD variable group tree before a shuffle.
  1772. Returns 1 if successful; 0 otherwise.]
  1773. SideEffects [Changes the BDD variable group tree.]
  1774. SeeAlso []
  1775. ******************************************************************************/
  1776. static int
  1777. ddCheckPermuation(
  1778. DdManager * table,
  1779. MtrNode * treenode,
  1780. int * perm,
  1781. int * invperm)
  1782. {
  1783. unsigned int i, size;
  1784. int index, level, minLevel, maxLevel;
  1785. if (treenode == NULL) return(1);
  1786. minLevel = table->size;
  1787. maxLevel = 0;
  1788. /* i : level */
  1789. for (i = treenode->low; i < treenode->low + treenode->size; i++) {
  1790. index = table->invperm[i];
  1791. level = perm[index];
  1792. if (level < minLevel)
  1793. minLevel = level;
  1794. if (level > maxLevel)
  1795. maxLevel = level;
  1796. }
  1797. size = maxLevel - minLevel + 1;
  1798. if (size != treenode->size)
  1799. return(0);
  1800. if (treenode->child != NULL) {
  1801. if (!ddCheckPermuation(table, treenode->child, perm, invperm))
  1802. return(0);
  1803. }
  1804. if (treenode->younger != NULL) {
  1805. if (!ddCheckPermuation(table, treenode->younger, perm, invperm))
  1806. return(0);
  1807. }
  1808. return(1);
  1809. }