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.

1639 lines
42 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions for symmetry-based %ZDD variable reordering.
  5. @see cuddSymmetry.c
  6. @author Hyong-Kyoon Shin, In-Ho Moon
  7. @copyright@parblock
  8. Copyright (c) 1995-2015, Regents of the University of Colorado
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. Neither the name of the University of Colorado nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. POSSIBILITY OF SUCH DAMAGE.
  33. @endparblock
  34. */
  35. #include "util.h"
  36. #include "cuddInt.h"
  37. /*---------------------------------------------------------------------------*/
  38. /* Constant declarations */
  39. /*---------------------------------------------------------------------------*/
  40. #define ZDD_MV_OOM (Move *)1
  41. /*---------------------------------------------------------------------------*/
  42. /* Stucture declarations */
  43. /*---------------------------------------------------------------------------*/
  44. /*---------------------------------------------------------------------------*/
  45. /* Type declarations */
  46. /*---------------------------------------------------------------------------*/
  47. /*---------------------------------------------------------------------------*/
  48. /* Variable declarations */
  49. /*---------------------------------------------------------------------------*/
  50. /*---------------------------------------------------------------------------*/
  51. /* Macro declarations */
  52. /*---------------------------------------------------------------------------*/
  53. /** \cond */
  54. /*---------------------------------------------------------------------------*/
  55. /* Static function prototypes */
  56. /*---------------------------------------------------------------------------*/
  57. static int cuddZddSymmSiftingAux (DdManager *table, int x, int x_low, int x_high);
  58. static int cuddZddSymmSiftingConvAux (DdManager *table, int x, int x_low, int x_high);
  59. static Move * cuddZddSymmSifting_up (DdManager *table, int x, int x_low, int initial_size);
  60. static Move * cuddZddSymmSifting_down (DdManager *table, int x, int x_high, int initial_size);
  61. static int cuddZddSymmSiftingBackward (DdManager *table, Move *moves, int size);
  62. static int zdd_group_move (DdManager *table, int x, int y, Move **moves);
  63. static int zdd_group_move_backward (DdManager *table, int x, int y);
  64. static void cuddZddSymmSummary (DdManager *table, int lower, int upper, int *symvars, int *symgroups);
  65. /** \endcond */
  66. /*---------------------------------------------------------------------------*/
  67. /* Definition of exported functions */
  68. /*---------------------------------------------------------------------------*/
  69. /**
  70. @brief Prints statistics on symmetric %ZDD variables.
  71. @sideeffect None
  72. */
  73. void
  74. Cudd_zddSymmProfile(
  75. DdManager * table,
  76. int lower,
  77. int upper)
  78. {
  79. int i, x, gbot;
  80. int TotalSymm = 0;
  81. int TotalSymmGroups = 0;
  82. for (i = lower; i < upper; i++) {
  83. if (table->subtableZ[i].next != (unsigned) i) {
  84. x = i;
  85. (void) fprintf(table->out,"Group:");
  86. do {
  87. (void) fprintf(table->out," %d", table->invpermZ[x]);
  88. TotalSymm++;
  89. gbot = x;
  90. x = table->subtableZ[x].next;
  91. } while (x != i);
  92. TotalSymmGroups++;
  93. #ifdef DD_DEBUG
  94. assert(table->subtableZ[gbot].next == (unsigned) i);
  95. #endif
  96. i = gbot;
  97. (void) fprintf(table->out,"\n");
  98. }
  99. }
  100. (void) fprintf(table->out,"Total Symmetric = %d\n", TotalSymm);
  101. (void) fprintf(table->out,"Total Groups = %d\n", TotalSymmGroups);
  102. } /* end of Cudd_zddSymmProfile */
  103. /*---------------------------------------------------------------------------*/
  104. /* Definition of internal functions */
  105. /*---------------------------------------------------------------------------*/
  106. /**
  107. @brief Checks for symmetry of x and y.
  108. @details Ignores projection functions, unless they are isolated.
  109. @return 1 in case of symmetry; 0 otherwise.
  110. @sideeffect None
  111. */
  112. int
  113. cuddZddSymmCheck(
  114. DdManager * table,
  115. int x,
  116. int y)
  117. {
  118. int i;
  119. DdNode *f, *f0, *f1, *f01, *f00, *f11, *f10;
  120. int yindex;
  121. int xsymmy = 1;
  122. int xsymmyp = 1;
  123. int arccount = 0;
  124. int TotalRefCount = 0;
  125. int symm_found;
  126. DdNode *empty = table->zero;
  127. yindex = table->invpermZ[y];
  128. for (i = table->subtableZ[x].slots - 1; i >= 0; i--) {
  129. f = table->subtableZ[x].nodelist[i];
  130. while (f != NULL) {
  131. /* Find f1, f0, f11, f10, f01, f00 */
  132. f1 = cuddT(f);
  133. f0 = cuddE(f);
  134. if ((int) f1->index == yindex) {
  135. f11 = cuddT(f1);
  136. f10 = cuddE(f1);
  137. if (f10 != empty)
  138. arccount++;
  139. } else {
  140. if ((int) f0->index != yindex) {
  141. return(0); /* f bypasses layer y */
  142. }
  143. f11 = empty;
  144. f10 = f1;
  145. }
  146. if ((int) f0->index == yindex) {
  147. f01 = cuddT(f0);
  148. f00 = cuddE(f0);
  149. if (f00 != empty)
  150. arccount++;
  151. } else {
  152. f01 = empty;
  153. f00 = f0;
  154. }
  155. if (f01 != f10)
  156. xsymmy = 0;
  157. if (f11 != f00)
  158. xsymmyp = 0;
  159. if ((xsymmy == 0) && (xsymmyp == 0))
  160. return(0);
  161. f = f->next;
  162. } /* for each element of the collision list */
  163. } /* for each slot of the subtable */
  164. /* Calculate the total reference counts of y
  165. ** whose else arc is not empty.
  166. */
  167. for (i = table->subtableZ[y].slots - 1; i >= 0; i--) {
  168. f = table->subtableZ[y].nodelist[i];
  169. while (f != NIL(DdNode)) {
  170. if (cuddE(f) != empty)
  171. TotalRefCount += f->ref;
  172. f = f->next;
  173. }
  174. }
  175. symm_found = (arccount == TotalRefCount);
  176. #if defined(DD_DEBUG) && defined(DD_VERBOSE)
  177. if (symm_found) {
  178. int xindex = table->invpermZ[x];
  179. (void) fprintf(table->out,
  180. "Found symmetry! x =%d\ty = %d\tPos(%d,%d)\n",
  181. xindex,yindex,x,y);
  182. }
  183. #endif
  184. return(symm_found);
  185. } /* end cuddZddSymmCheck */
  186. /**
  187. @brief Symmetric sifting algorithm for ZDDs.
  188. @details Assumes that no dead nodes are present.
  189. <ol>
  190. <li> Order all the variables according to the number of entries in
  191. each unique subtable.
  192. <li> Sift the variable up and down, remembering each time the total
  193. size of the %ZDD heap and grouping variables that are symmetric.
  194. <li> Select the best permutation.
  195. <li> Repeat 3 and 4 for all variables.
  196. </ol>
  197. @return 1 plus the number of symmetric variables if successful; 0
  198. otherwise.
  199. @sideeffect None
  200. @see cuddZddSymmSiftingConv
  201. */
  202. int
  203. cuddZddSymmSifting(
  204. DdManager * table,
  205. int lower,
  206. int upper)
  207. {
  208. int i;
  209. IndexKey *var;
  210. int nvars;
  211. int x;
  212. int result;
  213. int symvars;
  214. int symgroups;
  215. int iteration;
  216. #ifdef DD_STATS
  217. int previousSize;
  218. #endif
  219. nvars = table->sizeZ;
  220. /* Find order in which to sift variables. */
  221. var = ALLOC(IndexKey, nvars);
  222. if (var == NULL) {
  223. table->errorCode = CUDD_MEMORY_OUT;
  224. goto cuddZddSymmSiftingOutOfMem;
  225. }
  226. for (i = 0; i < nvars; i++) {
  227. x = table->permZ[i];
  228. var[i].index = i;
  229. var[i].keys = table->subtableZ[x].keys;
  230. }
  231. util_qsort(var, nvars, sizeof(IndexKey), cuddZddUniqueCompare);
  232. /* Initialize the symmetry of each subtable to itself. */
  233. for (i = lower; i <= upper; i++)
  234. table->subtableZ[i].next = i;
  235. iteration = ddMin(table->siftMaxVar, nvars);
  236. for (i = 0; i < iteration; i++) {
  237. if (table->zddTotalNumberSwapping >= table->siftMaxSwap)
  238. break;
  239. if (util_cpu_time() - table->startTime > table->timeLimit) {
  240. table->autoDynZ = 0; /* prevent further reordering */
  241. break;
  242. }
  243. if (table->terminationCallback != NULL &&
  244. table->terminationCallback(table->tcbArg)) {
  245. table->autoDynZ = 0; /* prevent further reordering */
  246. break;
  247. }
  248. x = table->permZ[var[i].index];
  249. #ifdef DD_STATS
  250. previousSize = table->keysZ;
  251. #endif
  252. if (x < lower || x > upper) continue;
  253. if (table->subtableZ[x].next == (unsigned) x) {
  254. result = cuddZddSymmSiftingAux(table, x, lower, upper);
  255. if (!result)
  256. goto cuddZddSymmSiftingOutOfMem;
  257. #ifdef DD_STATS
  258. if (table->keysZ < (unsigned) previousSize) {
  259. (void) fprintf(table->out,"-");
  260. } else if (table->keysZ > (unsigned) previousSize) {
  261. (void) fprintf(table->out,"+");
  262. #ifdef DD_VERBOSE
  263. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i].index);
  264. #endif
  265. } else {
  266. (void) fprintf(table->out,"=");
  267. }
  268. fflush(table->out);
  269. #endif
  270. }
  271. }
  272. FREE(var);
  273. cuddZddSymmSummary(table, lower, upper, &symvars, &symgroups);
  274. #ifdef DD_STATS
  275. (void) fprintf(table->out,"\n#:S_SIFTING %8d: symmetric variables\n",symvars);
  276. (void) fprintf(table->out,"#:G_SIFTING %8d: symmetric groups\n",symgroups);
  277. #endif
  278. return(1+symvars);
  279. cuddZddSymmSiftingOutOfMem:
  280. if (var != NULL)
  281. FREE(var);
  282. return(0);
  283. } /* end of cuddZddSymmSifting */
  284. /**
  285. @brief Symmetric sifting to convergence algorithm for ZDDs.
  286. @details Assumes that no dead nodes are present.
  287. <ol>
  288. <li> Order all the variables according to the number of entries in
  289. each unique subtable.
  290. <li> Sift the variable up and down, remembering each time the total
  291. size of the %ZDD heap and grouping variables that are symmetric.
  292. <li> Select the best permutation.
  293. <li> Repeat 3 and 4 for all variables.
  294. <li> Repeat 1-4 until no further improvement.
  295. </ol>
  296. @return 1 plus the number of symmetric variables if successful; 0
  297. otherwise.
  298. @sideeffect None
  299. @see cuddZddSymmSifting
  300. */
  301. int
  302. cuddZddSymmSiftingConv(
  303. DdManager * table,
  304. int lower,
  305. int upper)
  306. {
  307. int i;
  308. IndexKey *var;
  309. int nvars;
  310. int initialSize;
  311. int x;
  312. int result;
  313. int symvars;
  314. int symgroups;
  315. int classes;
  316. int iteration;
  317. #ifdef DD_STATS
  318. int previousSize;
  319. #endif
  320. initialSize = table->keysZ;
  321. nvars = table->sizeZ;
  322. /* Find order in which to sift variables. */
  323. var = ALLOC(IndexKey, nvars);
  324. if (var == NULL) {
  325. table->errorCode = CUDD_MEMORY_OUT;
  326. goto cuddZddSymmSiftingConvOutOfMem;
  327. }
  328. for (i = 0; i < nvars; i++) {
  329. x = table->permZ[i];
  330. var[i].index = i;
  331. var[i].keys = table->subtableZ[x].keys;
  332. }
  333. util_qsort(var, nvars, sizeof(IndexKey), cuddZddUniqueCompare);
  334. /* Initialize the symmetry of each subtable to itself
  335. ** for first pass of converging symmetric sifting.
  336. */
  337. for (i = lower; i <= upper; i++)
  338. table->subtableZ[i].next = i;
  339. iteration = ddMin(table->siftMaxVar, table->sizeZ);
  340. for (i = 0; i < iteration; i++) {
  341. if (table->zddTotalNumberSwapping >= table->siftMaxSwap)
  342. break;
  343. if (util_cpu_time() - table->startTime > table->timeLimit) {
  344. table->autoDynZ = 0; /* prevent further reordering */
  345. break;
  346. }
  347. if (table->terminationCallback != NULL &&
  348. table->terminationCallback(table->tcbArg)) {
  349. table->autoDynZ = 0; /* prevent further reordering */
  350. break;
  351. }
  352. x = table->permZ[var[i].index];
  353. if (x < lower || x > upper) continue;
  354. /* Only sift if not in symmetry group already. */
  355. if (table->subtableZ[x].next == (unsigned) x) {
  356. #ifdef DD_STATS
  357. previousSize = table->keysZ;
  358. #endif
  359. result = cuddZddSymmSiftingAux(table, x, lower, upper);
  360. if (!result)
  361. goto cuddZddSymmSiftingConvOutOfMem;
  362. #ifdef DD_STATS
  363. if (table->keysZ < (unsigned) previousSize) {
  364. (void) fprintf(table->out,"-");
  365. } else if (table->keysZ > (unsigned) previousSize) {
  366. (void) fprintf(table->out,"+");
  367. #ifdef DD_VERBOSE
  368. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i].index);
  369. #endif
  370. } else {
  371. (void) fprintf(table->out,"=");
  372. }
  373. fflush(table->out);
  374. #endif
  375. }
  376. }
  377. /* Sifting now until convergence. */
  378. while ((unsigned) initialSize > table->keysZ) {
  379. initialSize = table->keysZ;
  380. #ifdef DD_STATS
  381. (void) fprintf(table->out,"\n");
  382. #endif
  383. /* Here we consider only one representative for each symmetry class. */
  384. for (x = lower, classes = 0; x <= upper; x++, classes++) {
  385. while ((unsigned) x < table->subtableZ[x].next)
  386. x = table->subtableZ[x].next;
  387. /* Here x is the largest index in a group.
  388. ** Groups consists of adjacent variables.
  389. ** Hence, the next increment of x will move it to a new group.
  390. */
  391. i = table->invpermZ[x];
  392. var[classes].index = i;
  393. var[classes].keys = table->subtableZ[x].keys;
  394. }
  395. util_qsort(var,classes,sizeof(IndexKey),cuddZddUniqueCompare);
  396. /* Now sift. */
  397. iteration = ddMin(table->siftMaxVar, nvars);
  398. for (i = 0; i < iteration; i++) {
  399. if (table->zddTotalNumberSwapping >= table->siftMaxSwap)
  400. break;
  401. if (util_cpu_time() - table->startTime > table->timeLimit) {
  402. table->autoDynZ = 0; /* prevent further reordering */
  403. break;
  404. }
  405. if (table->terminationCallback != NULL &&
  406. table->terminationCallback(table->tcbArg)) {
  407. table->autoDynZ = 0; /* prevent further reordering */
  408. break;
  409. }
  410. x = table->permZ[var[i].index];
  411. if ((unsigned) x >= table->subtableZ[x].next) {
  412. #ifdef DD_STATS
  413. previousSize = table->keysZ;
  414. #endif
  415. result = cuddZddSymmSiftingConvAux(table, x, lower, upper);
  416. if (!result)
  417. goto cuddZddSymmSiftingConvOutOfMem;
  418. #ifdef DD_STATS
  419. if (table->keysZ < (unsigned) previousSize) {
  420. (void) fprintf(table->out,"-");
  421. } else if (table->keysZ > (unsigned) previousSize) {
  422. (void) fprintf(table->out,"+");
  423. #ifdef DD_VERBOSE
  424. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i].index);
  425. #endif
  426. } else {
  427. (void) fprintf(table->out,"=");
  428. }
  429. fflush(table->out);
  430. #endif
  431. }
  432. } /* for */
  433. }
  434. cuddZddSymmSummary(table, lower, upper, &symvars, &symgroups);
  435. #ifdef DD_STATS
  436. (void) fprintf(table->out,"\n#:S_SIFTING %8d: symmetric variables\n",
  437. symvars);
  438. (void) fprintf(table->out,"#:G_SIFTING %8d: symmetric groups\n",
  439. symgroups);
  440. #endif
  441. FREE(var);
  442. return(1+symvars);
  443. cuddZddSymmSiftingConvOutOfMem:
  444. if (var != NULL)
  445. FREE(var);
  446. return(0);
  447. } /* end of cuddZddSymmSiftingConv */
  448. /*---------------------------------------------------------------------------*/
  449. /* Definition of static functions */
  450. /*---------------------------------------------------------------------------*/
  451. /**
  452. @brief Given x_low <= x <= x_high moves x up and down between the
  453. boundaries.
  454. @details Finds the best position and does the required changes.
  455. Assumes that x is not part of a symmetry group.
  456. @return 1 if successful; 0 otherwise.
  457. @sideeffect None
  458. */
  459. static int
  460. cuddZddSymmSiftingAux(
  461. DdManager * table,
  462. int x,
  463. int x_low,
  464. int x_high)
  465. {
  466. Move *move;
  467. Move *move_up; /* list of up move */
  468. Move *move_down; /* list of down move */
  469. int initial_size;
  470. int result;
  471. int i;
  472. int topbot; /* index to either top or bottom of symmetry group */
  473. int init_group_size, final_group_size;
  474. initial_size = table->keysZ;
  475. move_down = NULL;
  476. move_up = NULL;
  477. /* Look for consecutive symmetries above x. */
  478. for (i = x; i > x_low; i--) {
  479. if (!cuddZddSymmCheck(table, i - 1, i))
  480. break;
  481. /* find top of i-1's symmetry */
  482. topbot = table->subtableZ[i - 1].next;
  483. table->subtableZ[i - 1].next = i;
  484. table->subtableZ[x].next = topbot;
  485. /* x is bottom of group so its symmetry is top of i-1's
  486. group */
  487. i = topbot + 1; /* add 1 for i--, new i is top of symm group */
  488. }
  489. /* Look for consecutive symmetries below x. */
  490. for (i = x; i < x_high; i++) {
  491. if (!cuddZddSymmCheck(table, i, i + 1))
  492. break;
  493. /* find bottom of i+1's symm group */
  494. topbot = i + 1;
  495. while ((unsigned) topbot < table->subtableZ[topbot].next)
  496. topbot = table->subtableZ[topbot].next;
  497. table->subtableZ[topbot].next = table->subtableZ[i].next;
  498. table->subtableZ[i].next = i + 1;
  499. i = topbot - 1; /* add 1 for i++,
  500. new i is bottom of symm group */
  501. }
  502. /* Now x maybe in the middle of a symmetry group. */
  503. if (x == x_low) { /* Sift down */
  504. /* Find bottom of x's symm group */
  505. while ((unsigned) x < table->subtableZ[x].next)
  506. x = table->subtableZ[x].next;
  507. i = table->subtableZ[x].next;
  508. init_group_size = x - i + 1;
  509. move_down = cuddZddSymmSifting_down(table, x, x_high,
  510. initial_size);
  511. /* after that point x --> x_high, unless early term */
  512. if (move_down == ZDD_MV_OOM)
  513. goto cuddZddSymmSiftingAuxOutOfMem;
  514. if (move_down == NULL ||
  515. table->subtableZ[move_down->y].next != move_down->y) {
  516. /* symmetry detected may have to make another complete
  517. pass */
  518. if (move_down != NULL)
  519. x = move_down->y;
  520. else
  521. x = table->subtableZ[x].next;
  522. i = x;
  523. while ((unsigned) i < table->subtableZ[i].next) {
  524. i = table->subtableZ[i].next;
  525. }
  526. final_group_size = i - x + 1;
  527. if (init_group_size == final_group_size) {
  528. /* No new symmetry groups detected,
  529. return to best position */
  530. result = cuddZddSymmSiftingBackward(table,
  531. move_down, initial_size);
  532. }
  533. else {
  534. initial_size = table->keysZ;
  535. move_up = cuddZddSymmSifting_up(table, x, x_low,
  536. initial_size);
  537. result = cuddZddSymmSiftingBackward(table, move_up,
  538. initial_size);
  539. }
  540. }
  541. else {
  542. result = cuddZddSymmSiftingBackward(table, move_down,
  543. initial_size);
  544. /* move backward and stop at best position */
  545. }
  546. if (!result)
  547. goto cuddZddSymmSiftingAuxOutOfMem;
  548. }
  549. else if (x == x_high) { /* Sift up */
  550. /* Find top of x's symm group */
  551. while ((unsigned) x < table->subtableZ[x].next)
  552. x = table->subtableZ[x].next;
  553. x = table->subtableZ[x].next;
  554. i = x;
  555. while ((unsigned) i < table->subtableZ[i].next) {
  556. i = table->subtableZ[i].next;
  557. }
  558. init_group_size = i - x + 1;
  559. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  560. /* after that point x --> x_low, unless early term */
  561. if (move_up == ZDD_MV_OOM)
  562. goto cuddZddSymmSiftingAuxOutOfMem;
  563. if (move_up == NULL ||
  564. table->subtableZ[move_up->x].next != move_up->x) {
  565. /* symmetry detected may have to make another complete
  566. pass */
  567. if (move_up != NULL)
  568. x = move_up->x;
  569. else {
  570. while ((unsigned) x < table->subtableZ[x].next)
  571. x = table->subtableZ[x].next;
  572. }
  573. i = table->subtableZ[x].next;
  574. final_group_size = x - i + 1;
  575. if (init_group_size == final_group_size) {
  576. /* No new symmetry groups detected,
  577. return to best position */
  578. result = cuddZddSymmSiftingBackward(table, move_up,
  579. initial_size);
  580. }
  581. else {
  582. initial_size = table->keysZ;
  583. move_down = cuddZddSymmSifting_down(table, x, x_high,
  584. initial_size);
  585. result = cuddZddSymmSiftingBackward(table, move_down,
  586. initial_size);
  587. }
  588. }
  589. else {
  590. result = cuddZddSymmSiftingBackward(table, move_up,
  591. initial_size);
  592. /* move backward and stop at best position */
  593. }
  594. if (!result)
  595. goto cuddZddSymmSiftingAuxOutOfMem;
  596. }
  597. else if ((x - x_low) > (x_high - x)) { /* must go down first:
  598. shorter */
  599. /* Find bottom of x's symm group */
  600. while ((unsigned) x < table->subtableZ[x].next)
  601. x = table->subtableZ[x].next;
  602. move_down = cuddZddSymmSifting_down(table, x, x_high,
  603. initial_size);
  604. /* after that point x --> x_high, unless early term */
  605. if (move_down == ZDD_MV_OOM)
  606. goto cuddZddSymmSiftingAuxOutOfMem;
  607. if (move_down != NULL) {
  608. x = move_down->y;
  609. }
  610. else {
  611. x = table->subtableZ[x].next;
  612. }
  613. i = x;
  614. while ((unsigned) i < table->subtableZ[i].next) {
  615. i = table->subtableZ[i].next;
  616. }
  617. init_group_size = i - x + 1;
  618. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  619. if (move_up == ZDD_MV_OOM)
  620. goto cuddZddSymmSiftingAuxOutOfMem;
  621. if (move_up == NULL ||
  622. table->subtableZ[move_up->x].next != move_up->x) {
  623. /* symmetry detected may have to make another complete
  624. pass */
  625. if (move_up != NULL) {
  626. x = move_up->x;
  627. }
  628. else {
  629. while ((unsigned) x < table->subtableZ[x].next)
  630. x = table->subtableZ[x].next;
  631. }
  632. i = table->subtableZ[x].next;
  633. final_group_size = x - i + 1;
  634. if (init_group_size == final_group_size) {
  635. /* No new symmetry groups detected,
  636. return to best position */
  637. result = cuddZddSymmSiftingBackward(table, move_up,
  638. initial_size);
  639. }
  640. else {
  641. while (move_down != NULL) {
  642. move = move_down->next;
  643. cuddDeallocMove(table, move_down);
  644. move_down = move;
  645. }
  646. initial_size = table->keysZ;
  647. move_down = cuddZddSymmSifting_down(table, x, x_high,
  648. initial_size);
  649. result = cuddZddSymmSiftingBackward(table, move_down,
  650. initial_size);
  651. }
  652. }
  653. else {
  654. result = cuddZddSymmSiftingBackward(table, move_up,
  655. initial_size);
  656. /* move backward and stop at best position */
  657. }
  658. if (!result)
  659. goto cuddZddSymmSiftingAuxOutOfMem;
  660. }
  661. else { /* moving up first:shorter */
  662. /* Find top of x's symmetry group */
  663. while ((unsigned) x < table->subtableZ[x].next)
  664. x = table->subtableZ[x].next;
  665. x = table->subtableZ[x].next;
  666. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  667. /* after that point x --> x_high, unless early term */
  668. if (move_up == ZDD_MV_OOM)
  669. goto cuddZddSymmSiftingAuxOutOfMem;
  670. if (move_up != NULL) {
  671. x = move_up->x;
  672. }
  673. else {
  674. while ((unsigned) x < table->subtableZ[x].next)
  675. x = table->subtableZ[x].next;
  676. }
  677. i = table->subtableZ[x].next;
  678. init_group_size = x - i + 1;
  679. move_down = cuddZddSymmSifting_down(table, x, x_high,
  680. initial_size);
  681. if (move_down == ZDD_MV_OOM)
  682. goto cuddZddSymmSiftingAuxOutOfMem;
  683. if (move_down == NULL ||
  684. table->subtableZ[move_down->y].next != move_down->y) {
  685. /* symmetry detected may have to make another complete
  686. pass */
  687. if (move_down != NULL) {
  688. x = move_down->y;
  689. }
  690. else {
  691. x = table->subtableZ[x].next;
  692. }
  693. i = x;
  694. while ((unsigned) i < table->subtableZ[i].next) {
  695. i = table->subtableZ[i].next;
  696. }
  697. final_group_size = i - x + 1;
  698. if (init_group_size == final_group_size) {
  699. /* No new symmetries detected,
  700. go back to best position */
  701. result = cuddZddSymmSiftingBackward(table, move_down,
  702. initial_size);
  703. }
  704. else {
  705. while (move_up != NULL) {
  706. move = move_up->next;
  707. cuddDeallocMove(table, move_up);
  708. move_up = move;
  709. }
  710. initial_size = table->keysZ;
  711. move_up = cuddZddSymmSifting_up(table, x, x_low,
  712. initial_size);
  713. result = cuddZddSymmSiftingBackward(table, move_up,
  714. initial_size);
  715. }
  716. }
  717. else {
  718. result = cuddZddSymmSiftingBackward(table, move_down,
  719. initial_size);
  720. /* move backward and stop at best position */
  721. }
  722. if (!result)
  723. goto cuddZddSymmSiftingAuxOutOfMem;
  724. }
  725. while (move_down != NULL) {
  726. move = move_down->next;
  727. cuddDeallocMove(table, move_down);
  728. move_down = move;
  729. }
  730. while (move_up != NULL) {
  731. move = move_up->next;
  732. cuddDeallocMove(table, move_up);
  733. move_up = move;
  734. }
  735. return(1);
  736. cuddZddSymmSiftingAuxOutOfMem:
  737. if (move_down != ZDD_MV_OOM) {
  738. while (move_down != NULL) {
  739. move = move_down->next;
  740. cuddDeallocMove(table, move_down);
  741. move_down = move;
  742. }
  743. }
  744. if (move_up != ZDD_MV_OOM) {
  745. while (move_up != NULL) {
  746. move = move_up->next;
  747. cuddDeallocMove(table, move_up);
  748. move_up = move;
  749. }
  750. }
  751. return(0);
  752. } /* end of cuddZddSymmSiftingAux */
  753. /**
  754. @brief Given x_low <= x <= x_high moves x up and down between the
  755. boundaries.
  756. @details Finds the best position and does the required changes.
  757. Assumes that x is either an isolated variable, or it is the bottom of
  758. a symmetry group. All symmetries may not have been found, because of
  759. exceeded growth limit.
  760. @return 1 if successful; 0 otherwise.
  761. @sideeffect None
  762. */
  763. static int
  764. cuddZddSymmSiftingConvAux(
  765. DdManager * table,
  766. int x,
  767. int x_low,
  768. int x_high)
  769. {
  770. Move *move;
  771. Move *move_up; /* list of up move */
  772. Move *move_down; /* list of down move */
  773. int initial_size;
  774. int result;
  775. int i;
  776. int init_group_size, final_group_size;
  777. initial_size = table->keysZ;
  778. move_down = NULL;
  779. move_up = NULL;
  780. if (x == x_low) { /* Sift down */
  781. i = table->subtableZ[x].next;
  782. init_group_size = x - i + 1;
  783. move_down = cuddZddSymmSifting_down(table, x, x_high,
  784. initial_size);
  785. /* after that point x --> x_high, unless early term */
  786. if (move_down == ZDD_MV_OOM)
  787. goto cuddZddSymmSiftingConvAuxOutOfMem;
  788. if (move_down == NULL ||
  789. table->subtableZ[move_down->y].next != move_down->y) {
  790. /* symmetry detected may have to make another complete
  791. pass */
  792. if (move_down != NULL)
  793. x = move_down->y;
  794. else {
  795. while ((unsigned) x < table->subtableZ[x].next)
  796. x = table->subtableZ[x].next;
  797. x = table->subtableZ[x].next;
  798. }
  799. i = x;
  800. while ((unsigned) i < table->subtableZ[i].next) {
  801. i = table->subtableZ[i].next;
  802. }
  803. final_group_size = i - x + 1;
  804. if (init_group_size == final_group_size) {
  805. /* No new symmetries detected,
  806. go back to best position */
  807. result = cuddZddSymmSiftingBackward(table, move_down,
  808. initial_size);
  809. }
  810. else {
  811. initial_size = table->keysZ;
  812. move_up = cuddZddSymmSifting_up(table, x, x_low,
  813. initial_size);
  814. result = cuddZddSymmSiftingBackward(table, move_up,
  815. initial_size);
  816. }
  817. }
  818. else {
  819. result = cuddZddSymmSiftingBackward(table, move_down,
  820. initial_size);
  821. /* move backward and stop at best position */
  822. }
  823. if (!result)
  824. goto cuddZddSymmSiftingConvAuxOutOfMem;
  825. }
  826. else if (x == x_high) { /* Sift up */
  827. /* Find top of x's symm group */
  828. while ((unsigned) x < table->subtableZ[x].next)
  829. x = table->subtableZ[x].next;
  830. x = table->subtableZ[x].next;
  831. i = x;
  832. while ((unsigned) i < table->subtableZ[i].next) {
  833. i = table->subtableZ[i].next;
  834. }
  835. init_group_size = i - x + 1;
  836. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  837. /* after that point x --> x_low, unless early term */
  838. if (move_up == ZDD_MV_OOM)
  839. goto cuddZddSymmSiftingConvAuxOutOfMem;
  840. if (move_up == NULL ||
  841. table->subtableZ[move_up->x].next != move_up->x) {
  842. /* symmetry detected may have to make another complete
  843. pass */
  844. if (move_up != NULL)
  845. x = move_up->x;
  846. else {
  847. while ((unsigned) x < table->subtableZ[x].next)
  848. x = table->subtableZ[x].next;
  849. }
  850. i = table->subtableZ[x].next;
  851. final_group_size = x - i + 1;
  852. if (init_group_size == final_group_size) {
  853. /* No new symmetry groups detected,
  854. return to best position */
  855. result = cuddZddSymmSiftingBackward(table, move_up,
  856. initial_size);
  857. }
  858. else {
  859. initial_size = table->keysZ;
  860. move_down = cuddZddSymmSifting_down(table, x, x_high,
  861. initial_size);
  862. result = cuddZddSymmSiftingBackward(table, move_down,
  863. initial_size);
  864. }
  865. }
  866. else {
  867. result = cuddZddSymmSiftingBackward(table, move_up,
  868. initial_size);
  869. /* move backward and stop at best position */
  870. }
  871. if (!result)
  872. goto cuddZddSymmSiftingConvAuxOutOfMem;
  873. }
  874. else if ((x - x_low) > (x_high - x)) { /* must go down first:
  875. shorter */
  876. move_down = cuddZddSymmSifting_down(table, x, x_high,
  877. initial_size);
  878. /* after that point x --> x_high */
  879. if (move_down == ZDD_MV_OOM)
  880. goto cuddZddSymmSiftingConvAuxOutOfMem;
  881. if (move_down != NULL) {
  882. x = move_down->y;
  883. }
  884. else {
  885. while ((unsigned) x < table->subtableZ[x].next)
  886. x = table->subtableZ[x].next;
  887. x = table->subtableZ[x].next;
  888. }
  889. i = x;
  890. while ((unsigned) i < table->subtableZ[i].next) {
  891. i = table->subtableZ[i].next;
  892. }
  893. init_group_size = i - x + 1;
  894. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  895. if (move_up == ZDD_MV_OOM)
  896. goto cuddZddSymmSiftingConvAuxOutOfMem;
  897. if (move_up == NULL ||
  898. table->subtableZ[move_up->x].next != move_up->x) {
  899. /* symmetry detected may have to make another complete
  900. pass */
  901. if (move_up != NULL) {
  902. x = move_up->x;
  903. }
  904. else {
  905. while ((unsigned) x < table->subtableZ[x].next)
  906. x = table->subtableZ[x].next;
  907. }
  908. i = table->subtableZ[x].next;
  909. final_group_size = x - i + 1;
  910. if (init_group_size == final_group_size) {
  911. /* No new symmetry groups detected,
  912. return to best position */
  913. result = cuddZddSymmSiftingBackward(table, move_up,
  914. initial_size);
  915. }
  916. else {
  917. while (move_down != NULL) {
  918. move = move_down->next;
  919. cuddDeallocMove(table, move_down);
  920. move_down = move;
  921. }
  922. initial_size = table->keysZ;
  923. move_down = cuddZddSymmSifting_down(table, x, x_high,
  924. initial_size);
  925. result = cuddZddSymmSiftingBackward(table, move_down,
  926. initial_size);
  927. }
  928. }
  929. else {
  930. result = cuddZddSymmSiftingBackward(table, move_up,
  931. initial_size);
  932. /* move backward and stop at best position */
  933. }
  934. if (!result)
  935. goto cuddZddSymmSiftingConvAuxOutOfMem;
  936. }
  937. else { /* moving up first:shorter */
  938. /* Find top of x's symmetry group */
  939. x = table->subtableZ[x].next;
  940. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  941. /* after that point x --> x_high, unless early term */
  942. if (move_up == ZDD_MV_OOM)
  943. goto cuddZddSymmSiftingConvAuxOutOfMem;
  944. if (move_up != NULL) {
  945. x = move_up->x;
  946. }
  947. else {
  948. while ((unsigned) x < table->subtableZ[x].next)
  949. x = table->subtableZ[x].next;
  950. }
  951. i = table->subtableZ[x].next;
  952. init_group_size = x - i + 1;
  953. move_down = cuddZddSymmSifting_down(table, x, x_high,
  954. initial_size);
  955. if (move_down == ZDD_MV_OOM)
  956. goto cuddZddSymmSiftingConvAuxOutOfMem;
  957. if (move_down == NULL ||
  958. table->subtableZ[move_down->y].next != move_down->y) {
  959. /* symmetry detected may have to make another complete
  960. pass */
  961. if (move_down != NULL) {
  962. x = move_down->y;
  963. }
  964. else {
  965. while ((unsigned) x < table->subtableZ[x].next)
  966. x = table->subtableZ[x].next;
  967. x = table->subtableZ[x].next;
  968. }
  969. i = x;
  970. while ((unsigned) i < table->subtableZ[i].next) {
  971. i = table->subtableZ[i].next;
  972. }
  973. final_group_size = i - x + 1;
  974. if (init_group_size == final_group_size) {
  975. /* No new symmetries detected,
  976. go back to best position */
  977. result = cuddZddSymmSiftingBackward(table, move_down,
  978. initial_size);
  979. }
  980. else {
  981. while (move_up != NULL) {
  982. move = move_up->next;
  983. cuddDeallocMove(table, move_up);
  984. move_up = move;
  985. }
  986. initial_size = table->keysZ;
  987. move_up = cuddZddSymmSifting_up(table, x, x_low,
  988. initial_size);
  989. result = cuddZddSymmSiftingBackward(table, move_up,
  990. initial_size);
  991. }
  992. }
  993. else {
  994. result = cuddZddSymmSiftingBackward(table, move_down,
  995. initial_size);
  996. /* move backward and stop at best position */
  997. }
  998. if (!result)
  999. goto cuddZddSymmSiftingConvAuxOutOfMem;
  1000. }
  1001. while (move_down != NULL) {
  1002. move = move_down->next;
  1003. cuddDeallocMove(table, move_down);
  1004. move_down = move;
  1005. }
  1006. while (move_up != NULL) {
  1007. move = move_up->next;
  1008. cuddDeallocMove(table, move_up);
  1009. move_up = move;
  1010. }
  1011. return(1);
  1012. cuddZddSymmSiftingConvAuxOutOfMem:
  1013. if (move_down != ZDD_MV_OOM) {
  1014. while (move_down != NULL) {
  1015. move = move_down->next;
  1016. cuddDeallocMove(table, move_down);
  1017. move_down = move;
  1018. }
  1019. }
  1020. if (move_up != ZDD_MV_OOM) {
  1021. while (move_up != NULL) {
  1022. move = move_up->next;
  1023. cuddDeallocMove(table, move_up);
  1024. move_up = move;
  1025. }
  1026. }
  1027. return(0);
  1028. } /* end of cuddZddSymmSiftingConvAux */
  1029. /**
  1030. @brief Moves x up until either it reaches the bound (x_low) or
  1031. the size of the %ZDD heap increases too much.
  1032. @details Assumes that x is the top of a symmetry group. Checks x
  1033. for symmetry to the adjacent variables. If symmetry is found, the
  1034. symmetry group of x is merged with the symmetry group of the other
  1035. variable.
  1036. @return the set of moves in case of success; ZDD_MV_OOM if memory is
  1037. full.
  1038. @sideeffect None
  1039. */
  1040. static Move *
  1041. cuddZddSymmSifting_up(
  1042. DdManager * table,
  1043. int x,
  1044. int x_low,
  1045. int initial_size)
  1046. {
  1047. Move *moves;
  1048. Move *move;
  1049. int y;
  1050. int size;
  1051. int limit_size = initial_size;
  1052. int i, gytop;
  1053. moves = NULL;
  1054. y = cuddZddNextLow(table, x);
  1055. while (y >= x_low) {
  1056. gytop = table->subtableZ[y].next;
  1057. if (cuddZddSymmCheck(table, y, x)) {
  1058. /* Symmetry found, attach symm groups */
  1059. table->subtableZ[y].next = x;
  1060. i = table->subtableZ[x].next;
  1061. while (table->subtableZ[i].next != (unsigned) x)
  1062. i = table->subtableZ[i].next;
  1063. table->subtableZ[i].next = gytop;
  1064. }
  1065. else if ((table->subtableZ[x].next == (unsigned) x) &&
  1066. (table->subtableZ[y].next == (unsigned) y)) {
  1067. /* x and y have self symmetry */
  1068. size = cuddZddSwapInPlace(table, y, x);
  1069. if (size == 0)
  1070. goto cuddZddSymmSifting_upOutOfMem;
  1071. move = (Move *)cuddDynamicAllocNode(table);
  1072. if (move == NULL)
  1073. goto cuddZddSymmSifting_upOutOfMem;
  1074. move->x = y;
  1075. move->y = x;
  1076. move->size = size;
  1077. move->next = moves;
  1078. moves = move;
  1079. if ((double)size >
  1080. (double)limit_size * table->maxGrowth)
  1081. return(moves);
  1082. if (size < limit_size)
  1083. limit_size = size;
  1084. }
  1085. else { /* Group move */
  1086. size = zdd_group_move(table, y, x, &moves);
  1087. if ((double)size >
  1088. (double)limit_size * table->maxGrowth)
  1089. return(moves);
  1090. if (size < limit_size)
  1091. limit_size = size;
  1092. }
  1093. x = gytop;
  1094. y = cuddZddNextLow(table, x);
  1095. }
  1096. return(moves);
  1097. cuddZddSymmSifting_upOutOfMem:
  1098. while (moves != NULL) {
  1099. move = moves->next;
  1100. cuddDeallocMove(table, moves);
  1101. moves = move;
  1102. }
  1103. return(ZDD_MV_OOM);
  1104. } /* end of cuddZddSymmSifting_up */
  1105. /**
  1106. @brief Moves x down until either it reaches the bound (x_high) or
  1107. the size of the %ZDD heap increases too much.
  1108. @details Assumes that x is the bottom of a symmetry group. Checks x
  1109. for symmetry to the adjacent variables. If symmetry is found, the
  1110. symmetry group of x is merged with the symmetry group of the other
  1111. variable.
  1112. @return the set of moves in case of success; ZDD_MV_OOM if memory is
  1113. full.
  1114. @sideeffect None
  1115. */
  1116. static Move *
  1117. cuddZddSymmSifting_down(
  1118. DdManager * table,
  1119. int x,
  1120. int x_high,
  1121. int initial_size)
  1122. {
  1123. Move *moves;
  1124. Move *move;
  1125. int y;
  1126. int size;
  1127. int limit_size = initial_size;
  1128. int i, gxtop, gybot;
  1129. moves = NULL;
  1130. y = cuddZddNextHigh(table, x);
  1131. while (y <= x_high) {
  1132. gybot = table->subtableZ[y].next;
  1133. while (table->subtableZ[gybot].next != (unsigned) y)
  1134. gybot = table->subtableZ[gybot].next;
  1135. if (cuddZddSymmCheck(table, x, y)) {
  1136. /* Symmetry found, attach symm groups */
  1137. gxtop = table->subtableZ[x].next;
  1138. table->subtableZ[x].next = y;
  1139. i = table->subtableZ[y].next;
  1140. while (table->subtableZ[i].next != (unsigned) y)
  1141. i = table->subtableZ[i].next;
  1142. table->subtableZ[i].next = gxtop;
  1143. }
  1144. else if ((table->subtableZ[x].next == (unsigned) x) &&
  1145. (table->subtableZ[y].next == (unsigned) y)) {
  1146. /* x and y have self symmetry */
  1147. size = cuddZddSwapInPlace(table, x, y);
  1148. if (size == 0)
  1149. goto cuddZddSymmSifting_downOutOfMem;
  1150. move = (Move *)cuddDynamicAllocNode(table);
  1151. if (move == NULL)
  1152. goto cuddZddSymmSifting_downOutOfMem;
  1153. move->x = x;
  1154. move->y = y;
  1155. move->size = size;
  1156. move->next = moves;
  1157. moves = move;
  1158. if ((double)size >
  1159. (double)limit_size * table->maxGrowth)
  1160. return(moves);
  1161. if (size < limit_size)
  1162. limit_size = size;
  1163. }
  1164. else { /* Group move */
  1165. size = zdd_group_move(table, x, y, &moves);
  1166. if ((double)size >
  1167. (double)limit_size * table->maxGrowth)
  1168. return(moves);
  1169. if (size < limit_size)
  1170. limit_size = size;
  1171. }
  1172. x = gybot;
  1173. y = cuddZddNextHigh(table, x);
  1174. }
  1175. return(moves);
  1176. cuddZddSymmSifting_downOutOfMem:
  1177. while (moves != NULL) {
  1178. move = moves->next;
  1179. cuddDeallocMove(table, moves);
  1180. moves = move;
  1181. }
  1182. return(ZDD_MV_OOM);
  1183. } /* end of cuddZddSymmSifting_down */
  1184. /**
  1185. @brief Given a set of moves, returns the %ZDD heap to the position
  1186. giving the minimum size.
  1187. @details In case of ties, returns to the closest position giving the
  1188. minimum size.
  1189. @return 1 in case of success; 0 otherwise.
  1190. @sideeffect None
  1191. */
  1192. static int
  1193. cuddZddSymmSiftingBackward(
  1194. DdManager * table,
  1195. Move * moves,
  1196. int size)
  1197. {
  1198. int i;
  1199. int i_best;
  1200. Move *move;
  1201. int res;
  1202. i_best = -1;
  1203. for (move = moves, i = 0; move != NULL; move = move->next, i++) {
  1204. if (move->size < size) {
  1205. i_best = i;
  1206. size = move->size;
  1207. }
  1208. }
  1209. for (move = moves, i = 0; move != NULL; move = move->next, i++) {
  1210. if (i == i_best) break;
  1211. if ((table->subtableZ[move->x].next == move->x) &&
  1212. (table->subtableZ[move->y].next == move->y)) {
  1213. res = cuddZddSwapInPlace(table, move->x, move->y);
  1214. if (!res) return(0);
  1215. }
  1216. else { /* Group move necessary */
  1217. res = zdd_group_move_backward(table, move->x, move->y);
  1218. }
  1219. if (i_best == -1 && res == size)
  1220. break;
  1221. }
  1222. return(1);
  1223. } /* end of cuddZddSymmSiftingBackward */
  1224. /**
  1225. @brief Swaps two groups.
  1226. @details x is assumed to be the bottom variable of the first
  1227. group. y is assumed to be the top variable of the second group.
  1228. Updates the list of moves.
  1229. @return the number of keys in the table if successful; 0 otherwise.
  1230. @sideeffect None
  1231. */
  1232. static int
  1233. zdd_group_move(
  1234. DdManager * table,
  1235. int x,
  1236. int y,
  1237. Move ** moves)
  1238. {
  1239. Move *move;
  1240. int size;
  1241. int i, temp, gxtop, gxbot, gybot, yprev;
  1242. int swapx = 0, swapy = 0;
  1243. #ifdef DD_DEBUG
  1244. assert(x < y); /* we assume that x < y */
  1245. #endif
  1246. /* Find top and bottom for the two groups. */
  1247. gxtop = table->subtableZ[x].next;
  1248. gxbot = x;
  1249. gybot = table->subtableZ[y].next;
  1250. while (table->subtableZ[gybot].next != (unsigned) y)
  1251. gybot = table->subtableZ[gybot].next;
  1252. yprev = gybot;
  1253. while (x <= y) {
  1254. while (y > gxtop) {
  1255. /* Set correct symmetries. */
  1256. temp = table->subtableZ[x].next;
  1257. if (temp == x)
  1258. temp = y;
  1259. i = gxtop;
  1260. for (;;) {
  1261. if (table->subtableZ[i].next == (unsigned) x) {
  1262. table->subtableZ[i].next = y;
  1263. break;
  1264. } else {
  1265. i = table->subtableZ[i].next;
  1266. }
  1267. }
  1268. if (table->subtableZ[y].next != (unsigned) y) {
  1269. table->subtableZ[x].next = table->subtableZ[y].next;
  1270. } else {
  1271. table->subtableZ[x].next = x;
  1272. }
  1273. if (yprev != y) {
  1274. table->subtableZ[yprev].next = x;
  1275. } else {
  1276. yprev = x;
  1277. }
  1278. table->subtableZ[y].next = temp;
  1279. size = cuddZddSwapInPlace(table, x, y);
  1280. if (size == 0)
  1281. goto zdd_group_moveOutOfMem;
  1282. swapx = x;
  1283. swapy = y;
  1284. y = x;
  1285. x--;
  1286. } /* while y > gxtop */
  1287. /* Trying to find the next y. */
  1288. if (table->subtableZ[y].next > (unsigned) y) {
  1289. y = table->subtableZ[y].next;
  1290. }
  1291. yprev = gxtop;
  1292. gxtop++;
  1293. gxbot++;
  1294. x = gxbot;
  1295. } /* while x <= y, end of group movement */
  1296. move = (Move *)cuddDynamicAllocNode(table);
  1297. if (move == NULL)
  1298. goto zdd_group_moveOutOfMem;
  1299. move->x = swapx;
  1300. move->y = swapy;
  1301. move->size = table->keysZ;
  1302. move->next = *moves;
  1303. *moves = move;
  1304. return(table->keysZ);
  1305. zdd_group_moveOutOfMem:
  1306. while (*moves != NULL) {
  1307. move = (*moves)->next;
  1308. cuddDeallocMove(table, *moves);
  1309. *moves = move;
  1310. }
  1311. return(0);
  1312. } /* end of zdd_group_move */
  1313. /**
  1314. @brief Undoes the swap of two groups.
  1315. @details x is assumed to be the bottom variable of the first
  1316. group. y is assumed to be the top variable of the second group.
  1317. @return 1 if successful; 0 otherwise.
  1318. @sideeffect None
  1319. */
  1320. static int
  1321. zdd_group_move_backward(
  1322. DdManager * table,
  1323. int x,
  1324. int y)
  1325. {
  1326. int size = table->keysZ;
  1327. int i, temp, gxtop, gxbot, gybot, yprev;
  1328. #ifdef DD_DEBUG
  1329. assert(x < y); /* we assume that x < y */
  1330. #endif
  1331. /* Find top and bottom of the two groups. */
  1332. gxtop = table->subtableZ[x].next;
  1333. gxbot = x;
  1334. gybot = table->subtableZ[y].next;
  1335. while (table->subtableZ[gybot].next != (unsigned) y)
  1336. gybot = table->subtableZ[gybot].next;
  1337. yprev = gybot;
  1338. while (x <= y) {
  1339. while (y > gxtop) {
  1340. /* Set correct symmetries. */
  1341. temp = table->subtableZ[x].next;
  1342. if (temp == x)
  1343. temp = y;
  1344. i = gxtop;
  1345. for (;;) {
  1346. if (table->subtableZ[i].next == (unsigned) x) {
  1347. table->subtableZ[i].next = y;
  1348. break;
  1349. } else {
  1350. i = table->subtableZ[i].next;
  1351. }
  1352. }
  1353. if (table->subtableZ[y].next != (unsigned) y) {
  1354. table->subtableZ[x].next = table->subtableZ[y].next;
  1355. } else {
  1356. table->subtableZ[x].next = x;
  1357. }
  1358. if (yprev != y) {
  1359. table->subtableZ[yprev].next = x;
  1360. } else {
  1361. yprev = x;
  1362. }
  1363. table->subtableZ[y].next = temp;
  1364. size = cuddZddSwapInPlace(table, x, y);
  1365. if (size == 0)
  1366. return(0);
  1367. y = x;
  1368. x--;
  1369. } /* while y > gxtop */
  1370. /* Trying to find the next y. */
  1371. if (table->subtableZ[y].next > (unsigned) y) {
  1372. y = table->subtableZ[y].next;
  1373. }
  1374. yprev = gxtop;
  1375. gxtop++;
  1376. gxbot++;
  1377. x = gxbot;
  1378. } /* while x <= y, end of group movement backward */
  1379. return(size);
  1380. } /* end of zdd_group_move_backward */
  1381. /**
  1382. @brief Counts numbers of symmetric variables and symmetry groups.
  1383. @sideeffect None
  1384. */
  1385. static void
  1386. cuddZddSymmSummary(
  1387. DdManager * table,
  1388. int lower,
  1389. int upper,
  1390. int * symvars,
  1391. int * symgroups)
  1392. {
  1393. int i,x,gbot;
  1394. int TotalSymm = 0;
  1395. int TotalSymmGroups = 0;
  1396. for (i = lower; i <= upper; i++) {
  1397. if (table->subtableZ[i].next != (unsigned) i) {
  1398. TotalSymmGroups++;
  1399. x = i;
  1400. do {
  1401. TotalSymm++;
  1402. gbot = x;
  1403. x = table->subtableZ[x].next;
  1404. } while (x != i);
  1405. #ifdef DD_DEBUG
  1406. assert(table->subtableZ[gbot].next == (unsigned) i);
  1407. #endif
  1408. i = gbot;
  1409. }
  1410. }
  1411. *symvars = TotalSymm;
  1412. *symgroups = TotalSymmGroups;
  1413. return;
  1414. } /* end of cuddZddSymmSummary */