cuddWindow.c 27.2 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**CFile***********************************************************************

  FileName    [cuddWindow.c]

  PackageName [cudd]

  Synopsis    [Functions for window permutation]

  Description [Internal procedures included in this module:
        <ul>
        <li> cuddWindowReorder()
        </ul>
    Static procedures included in this module:
        <ul>
        <li> ddWindow2()
        <li> ddWindowConv2()
        <li> ddPermuteWindow3()
        <li> ddWindow3()
        <li> ddWindowConv3()
        <li> ddPermuteWindow4()
        <li> ddWindow4()
        <li> ddWindowConv4()
        </ul>]

  Author      [Fabio Somenzi]

  Copyright   [This file was created at the University of Colorado at
  Boulder.  The University of Colorado at Boulder makes no warranty
  about the suitability of this software for any purpose.  It is
  presented on an AS IS basis.]

******************************************************************************/

Alan Mishchenko committed
34
#include "util_hack.h"
Alan Mishchenko committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
#include "cuddInt.h"

/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
static char rcsid[] DD_UNUSED = "$Id: cuddWindow.c,v 1.1.1.1 2003/02/24 22:23:53 wjiang Exp $";
#endif

#ifdef DD_STATS
extern  int     ddTotalNumberSwapping;
extern  int     ddTotalNISwaps;
#endif

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/


/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

static int ddWindow2 ARGS((DdManager *table, int low, int high));
static int ddWindowConv2 ARGS((DdManager *table, int low, int high));
static int ddPermuteWindow3 ARGS((DdManager *table, int x));
static int ddWindow3 ARGS((DdManager *table, int low, int high));
static int ddWindowConv3 ARGS((DdManager *table, int low, int high));
static int ddPermuteWindow4 ARGS((DdManager *table, int w));
static int ddWindow4 ARGS((DdManager *table, int low, int high));
static int ddWindowConv4 ARGS((DdManager *table, int low, int high));

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Reorders by applying the method of the sliding window.]

  Description [Reorders by applying the method of the sliding window.
  Tries all possible permutations to the variables in a window that
  slides from low to high. The size of the window is determined by
  submethod.  Assumes that no dead nodes are present.  Returns 1 in
  case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
int
cuddWindowReorder(
  DdManager * table /* DD table */,
  int low /* lowest index to reorder */,
  int high /* highest index to reorder */,
  Cudd_ReorderingType submethod /* window reordering option */)
{

    int res;
#ifdef DD_DEBUG
    int supposedOpt;
#endif

    switch (submethod) {
    case CUDD_REORDER_WINDOW2:
    res = ddWindow2(table,low,high);
    break;
    case CUDD_REORDER_WINDOW3:
    res = ddWindow3(table,low,high);
    break;
    case CUDD_REORDER_WINDOW4:
    res = ddWindow4(table,low,high);
    break;
    case CUDD_REORDER_WINDOW2_CONV:
    res = ddWindowConv2(table,low,high);
    break;
    case CUDD_REORDER_WINDOW3_CONV:
    res = ddWindowConv3(table,low,high);
#ifdef DD_DEBUG
    supposedOpt = table->keys - table->isolated;
    res = ddWindow3(table,low,high);
    if (table->keys - table->isolated != (unsigned) supposedOpt) {
        (void) fprintf(table->err, "Convergence failed! (%d != %d)\n",
               table->keys - table->isolated, supposedOpt);
    }
#endif
    break;
    case CUDD_REORDER_WINDOW4_CONV:
    res = ddWindowConv4(table,low,high);
#ifdef DD_DEBUG
    supposedOpt = table->keys - table->isolated;
    res = ddWindow4(table,low,high);
    if (table->keys - table->isolated != (unsigned) supposedOpt) {
        (void) fprintf(table->err,"Convergence failed! (%d != %d)\n",
               table->keys - table->isolated, supposedOpt);
    }
#endif
    break;
    default: return(0);
    }

    return(res);

} /* end of cuddWindowReorder */


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/

/**Function********************************************************************

  Synopsis    [Reorders by applying a sliding window of width 2.]

  Description [Reorders by applying a sliding window of width 2.
  Tries both permutations of the variables in a window
  that slides from low to high.  Assumes that no dead nodes are
  present.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindow2(
  DdManager * table,
  int  low,
  int  high)
{

    int x;
    int res;
    int size;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 1) return(0);

    res = table->keys - table->isolated;
    for (x = low; x < high; x++) {
    size = res;
    res = cuddSwapInPlace(table,x,x+1);
    if (res == 0) return(0);
    if (res >= size) { /* no improvement: undo permutation */
        res = cuddSwapInPlace(table,x,x+1);
        if (res == 0) return(0);
    }
#ifdef DD_STATS
    if (res < size) {
        (void) fprintf(table->out,"-");
    } else {
        (void) fprintf(table->out,"=");
    }
    fflush(table->out);
#endif
    }

    return(1);

} /* end of ddWindow2 */


/**Function********************************************************************

  Synopsis    [Reorders by repeatedly applying a sliding window of width 2.]

  Description [Reorders by repeatedly applying a sliding window of width
  2. Tries both permutations of the variables in a window
  that slides from low to high.  Assumes that no dead nodes are
  present.  Uses an event-driven approach to determine convergence.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindowConv2(
  DdManager * table,
  int  low,
  int  high)
{
    int x;
    int res;
    int nwin;
    int newevent;
    int *events;
    int size;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 1) return(ddWindowConv2(table,low,high));

    nwin = high-low;
    events = ALLOC(int,nwin);
    if (events == NULL) {
    table->errorCode = CUDD_MEMORY_OUT;
    return(0);
    }
    for (x=0; x<nwin; x++) {
    events[x] = 1;
    }

    res = table->keys - table->isolated;
    do {
    newevent = 0;
    for (x=0; x<nwin; x++) {
        if (events[x]) {
        size = res;
        res = cuddSwapInPlace(table,x+low,x+low+1);
        if (res == 0) {
            FREE(events);
            return(0);
        }
        if (res >= size) { /* no improvement: undo permutation */
            res = cuddSwapInPlace(table,x+low,x+low+1);
            if (res == 0) {
            FREE(events);
            return(0);
            }
        }
        if (res < size) {
            if (x < nwin-1)    events[x+1] = 1;
            if (x > 0)        events[x-1] = 1;
            newevent = 1;
        }
        events[x] = 0;
#ifdef DD_STATS
        if (res < size) {
            (void) fprintf(table->out,"-");
        } else {
            (void) fprintf(table->out,"=");
        }
        fflush(table->out);
#endif
        }
    }
#ifdef DD_STATS
    if (newevent) {
        (void) fprintf(table->out,"|");
        fflush(table->out);
    }
#endif
    } while (newevent);

    FREE(events);

    return(1);

} /* end of ddWindowConv3 */


/**Function********************************************************************

  Synopsis [Tries all the permutations of the three variables between
  x and x+2 and retains the best.]

  Description [Tries all the permutations of the three variables between
  x and x+2 and retains the best. Assumes that no dead nodes are
  present.  Returns the index of the best permutation (1-6) in case of
  success; 0 otherwise.Assumes that no dead nodes are present.  Returns
  the index of the best permutation (1-6) in case of success; 0
  otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddPermuteWindow3(
  DdManager * table,
  int  x)
{
    int y,z;
    int    size,sizeNew;
    int    best;

#ifdef DD_DEBUG
    assert(table->dead == 0);
    assert(x+2 < table->size);
#endif

    size = table->keys - table->isolated;
    y = x+1; z = y+1;
    
    /* The permutation pattern is:
    ** (x,y)(y,z)
    ** repeated three times to get all 3! = 6 permutations.
    */
#define ABC 1
    best = ABC;

#define    BAC 2
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = BAC;
    size = sizeNew;
    }
#define BCA 3
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = BCA;
    size = sizeNew;
    }
#define CBA 4
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = CBA;
    size = sizeNew;
    }
#define CAB 5
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = CAB;
    size = sizeNew;
    }
#define ACB 6
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = ACB;
    size = sizeNew;
    }

    /* Now take the shortest route to the best permuytation.
    ** The initial permutation is ACB.
    */
    switch(best) {
    case BCA: if (!cuddSwapInPlace(table,y,z)) return(0);
    case CBA: if (!cuddSwapInPlace(table,x,y)) return(0);
    case ABC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ACB: break;
    case BAC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case CAB: if (!cuddSwapInPlace(table,x,y)) return(0);
           break;
    default: return(0);
    }

#ifdef DD_DEBUG
    assert(table->keys - table->isolated == (unsigned) size);
#endif

    return(best);

} /* end of ddPermuteWindow3 */


/**Function********************************************************************

  Synopsis    [Reorders by applying a sliding window of width 3.]

  Description [Reorders by applying a sliding window of width 3.
  Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindow3(
  DdManager * table,
  int  low,
  int  high)
{

    int x;
    int res;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 2) return(ddWindow2(table,low,high));

    for (x = low; x+1 < high; x++) {
    res = ddPermuteWindow3(table,x);
    if (res == 0) return(0);
#ifdef DD_STATS
    if (res == ABC) {
        (void) fprintf(table->out,"=");
    } else {
        (void) fprintf(table->out,"-");
    }
    fflush(table->out);
#endif
    }

    return(1);

} /* end of ddWindow3 */


/**Function********************************************************************

  Synopsis    [Reorders by repeatedly applying a sliding window of width 3.]

  Description [Reorders by repeatedly applying a sliding window of width
  3. Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Uses an event-driven approach to determine convergence.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindowConv3(
  DdManager * table,
  int  low,
  int  high)
{
    int x;
    int res;
    int nwin;
    int newevent;
    int *events;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 2) return(ddWindowConv2(table,low,high));

    nwin = high-low-1;
    events = ALLOC(int,nwin);
    if (events == NULL) {
    table->errorCode = CUDD_MEMORY_OUT;
    return(0);
    }
    for (x=0; x<nwin; x++) {
    events[x] = 1;
    }

    do {
    newevent = 0;
    for (x=0; x<nwin; x++) {
        if (events[x]) {
        res = ddPermuteWindow3(table,x+low);
        switch (res) {
        case ABC:
            break;
        case BAC:
            if (x < nwin-1)    events[x+1] = 1;
            if (x > 1)        events[x-2] = 1;
            newevent = 1;
            break;
        case BCA:
        case CBA:
        case CAB:
            if (x < nwin-2)    events[x+2] = 1;
            if (x < nwin-1)    events[x+1] = 1;
            if (x > 0)        events[x-1] = 1;
            if (x > 1)        events[x-2] = 1;
            newevent = 1;
            break;
        case ACB:
            if (x < nwin-2)    events[x+2] = 1;
            if (x > 0)        events[x-1] = 1;
            newevent = 1;
            break;
        default:
            FREE(events);
            return(0);
        }
        events[x] = 0;
#ifdef DD_STATS
        if (res == ABC) {
            (void) fprintf(table->out,"=");
        } else {
            (void) fprintf(table->out,"-");
        }
        fflush(table->out);
#endif
        }
    }
#ifdef DD_STATS
    if (newevent) {
        (void) fprintf(table->out,"|");
        fflush(table->out);
    }
#endif
    } while (newevent);

    FREE(events);

    return(1);

} /* end of ddWindowConv3 */


/**Function********************************************************************

  Synopsis [Tries all the permutations of the four variables between w
  and w+3 and retains the best.]

  Description [Tries all the permutations of the four variables between
  w and w+3 and retains the best. Assumes that no dead nodes are
  present.  Returns the index of the best permutation (1-24) in case of
  success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddPermuteWindow4(
  DdManager * table,
  int  w)
{
    int x,y,z;
    int    size,sizeNew;
    int    best;

#ifdef DD_DEBUG
    assert(table->dead == 0);
    assert(w+3 < table->size);
#endif

    size = table->keys - table->isolated;
    x = w+1; y = x+1; z = y+1;
    
    /* The permutation pattern is:
     * (w,x)(y,z)(w,x)(x,y)
     * (y,z)(w,x)(y,z)(x,y)
     * repeated three times to get all 4! = 24 permutations.
     * This gives a hamiltonian circuit of Cayley's graph.
     * The codes to the permutation are assigned in topological order.
     * The permutations at lower distance from the final permutation are
     * assigned lower codes. This way we can choose, between
     * permutations that give the same size, one that requires the minimum
     * number of swaps from the final permutation of the hamiltonian circuit.
     * There is an exception to this rule: ABCD is given Code 1, to
     * avoid oscillation when convergence is sought.
     */
#define ABCD 1
    best = ABCD;

#define    BACD 7
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = BACD;
    size = sizeNew;
    }
#define BADC 13
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = BADC;
    size = sizeNew;
    }
#define ABDC 8
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && ABDC < best)) {
    if (sizeNew == 0) return(0);
    best = ABDC;
    size = sizeNew;
    }
#define ADBC 14
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = ADBC;
    size = sizeNew;
    }
#define ADCB 9
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && ADCB < best)) {
    if (sizeNew == 0) return(0);
    best = ADCB;
    size = sizeNew;
    }
#define DACB 15
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = DACB;
    size = sizeNew;
    }
#define DABC 20
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = DABC;
    size = sizeNew;
    }
#define DBAC 23
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = DBAC;
    size = sizeNew;
    }
#define BDAC 19
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && BDAC < best)) {
    if (sizeNew == 0) return(0);
    best = BDAC;
    size = sizeNew;
    }
#define BDCA 21
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && BDCA < best)) {
    if (sizeNew == 0) return(0);
    best = BDCA;
    size = sizeNew;
    }
#define DBCA 24
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size) {
    if (sizeNew == 0) return(0);
    best = DBCA;
    size = sizeNew;
    }
#define DCBA 22
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size || (sizeNew == size && DCBA < best)) {
    if (sizeNew == 0) return(0);
    best = DCBA;
    size = sizeNew;
    }
#define DCAB 18
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && DCAB < best)) {
    if (sizeNew == 0) return(0);
    best = DCAB;
    size = sizeNew;
    }
#define CDAB 12
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && CDAB < best)) {
    if (sizeNew == 0) return(0);
    best = CDAB;
    size = sizeNew;
    }
#define CDBA 17
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && CDBA < best)) {
    if (sizeNew == 0) return(0);
    best = CDBA;
    size = sizeNew;
    }
#define CBDA 11
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size || (sizeNew == size && CBDA < best)) {
    if (sizeNew == 0) return(0);
    best = CBDA;
    size = sizeNew;
    }
#define BCDA 16
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && BCDA < best)) {
    if (sizeNew == 0) return(0);
    best = BCDA;
    size = sizeNew;
    }
#define BCAD 10
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && BCAD < best)) {
    if (sizeNew == 0) return(0);
    best = BCAD;
    size = sizeNew;
    }
#define CBAD 5
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && CBAD < best)) {
    if (sizeNew == 0) return(0);
    best = CBAD;
    size = sizeNew;
    }
#define CABD 3
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size || (sizeNew == size && CABD < best)) {
    if (sizeNew == 0) return(0);
    best = CABD;
    size = sizeNew;
    }
#define CADB 6
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && CADB < best)) {
    if (sizeNew == 0) return(0);
    best = CADB;
    size = sizeNew;
    }
#define ACDB 4
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && ACDB < best)) {
    if (sizeNew == 0) return(0);
    best = ACDB;
    size = sizeNew;
    }
#define ACBD 2
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && ACBD < best)) {
    if (sizeNew == 0) return(0);
    best = ACBD;
    size = sizeNew;
    }

    /* Now take the shortest route to the best permutation.
    ** The initial permutation is ACBD.
    */
    switch(best) {
    case DBCA: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BDCA: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CDBA: if (!cuddSwapInPlace(table,w,x)) return(0);
    case ADBC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ABDC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case ACDB: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ACBD: break;
    case DCBA: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BCDA: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CBDA: if (!cuddSwapInPlace(table,w,x)) return(0);
           if (!cuddSwapInPlace(table,x,y)) return(0);
           if (!cuddSwapInPlace(table,y,z)) return(0);
           break;
    case DBAC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case DCAB: if (!cuddSwapInPlace(table,w,x)) return(0);
    case DACB: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BACD: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CABD: if (!cuddSwapInPlace(table,w,x)) return(0);
           break;
    case DABC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BADC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CADB: if (!cuddSwapInPlace(table,w,x)) return(0);
           if (!cuddSwapInPlace(table,y,z)) return(0);
           break;
    case BDAC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CDAB: if (!cuddSwapInPlace(table,w,x)) return(0);
    case ADCB: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ABCD: if (!cuddSwapInPlace(table,x,y)) return(0);
           break;
    case BCAD: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CBAD: if (!cuddSwapInPlace(table,w,x)) return(0);
           if (!cuddSwapInPlace(table,x,y)) return(0);
           break;
    default: return(0);
    }

#ifdef DD_DEBUG
    assert(table->keys - table->isolated == (unsigned) size);
#endif

    return(best);

} /* end of ddPermuteWindow4 */


/**Function********************************************************************

  Synopsis    [Reorders by applying a sliding window of width 4.]

  Description [Reorders by applying a sliding window of width 4.
  Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindow4(
  DdManager * table,
  int  low,
  int  high)
{

    int w;
    int res;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 3) return(ddWindow3(table,low,high));

    for (w = low; w+2 < high; w++) {
    res = ddPermuteWindow4(table,w);
    if (res == 0) return(0);
#ifdef DD_STATS
    if (res == ABCD) {
        (void) fprintf(table->out,"=");
    } else {
        (void) fprintf(table->out,"-");
    }
    fflush(table->out);
#endif
    }

    return(1);

} /* end of ddWindow4 */


/**Function********************************************************************

  Synopsis    [Reorders by repeatedly applying a sliding window of width 4.]

  Description [Reorders by repeatedly applying a sliding window of width
  4. Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Uses an event-driven approach to determine convergence.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindowConv4(
  DdManager * table,
  int  low,
  int  high)
{
    int x;
    int res;
    int nwin;
    int newevent;
    int *events;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 3) return(ddWindowConv3(table,low,high));

    nwin = high-low-2;
    events = ALLOC(int,nwin);
    if (events == NULL) {
    table->errorCode = CUDD_MEMORY_OUT;
    return(0);
    }
    for (x=0; x<nwin; x++) {
    events[x] = 1;
    }

    do {
    newevent = 0;
    for (x=0; x<nwin; x++) {
        if (events[x]) {
        res = ddPermuteWindow4(table,x+low);
        switch (res) {
        case ABCD:
            break;
        case BACD:
            if (x < nwin-1)    events[x+1] = 1;
            if (x > 2)        events[x-3] = 1;
            newevent = 1;
            break;
        case BADC:
            if (x < nwin-3)    events[x+3] = 1;
            if (x < nwin-1)    events[x+1] = 1;
            if (x > 0)        events[x-1] = 1;
            if (x > 2)        events[x-3] = 1;
            newevent = 1;
            break;
        case ABDC:
            if (x < nwin-3)    events[x+3] = 1;
            if (x > 0)        events[x-1] = 1;
            newevent = 1;
            break;
        case ADBC:
        case ADCB:
        case ACDB:
            if (x < nwin-3)    events[x+3] = 1;
            if (x < nwin-2)    events[x+2] = 1;
            if (x > 0)        events[x-1] = 1;
            if (x > 1)        events[x-2] = 1;
            newevent = 1;
            break;
        case DACB:
        case DABC:
        case DBAC:
        case BDAC:
        case BDCA:
        case DBCA:
        case DCBA:
        case DCAB:
        case CDAB:
        case CDBA:
        case CBDA:
        case BCDA:
        case CADB:
            if (x < nwin-3)    events[x+3] = 1;
            if (x < nwin-2)    events[x+2] = 1;
            if (x < nwin-1)    events[x+1] = 1;
            if (x > 0)        events[x-1] = 1;
            if (x > 1)        events[x-2] = 1;
            if (x > 2)        events[x-3] = 1;
            newevent = 1;
            break;
        case BCAD:
        case CBAD:
        case CABD:
            if (x < nwin-2)    events[x+2] = 1;
            if (x < nwin-1)    events[x+1] = 1;
            if (x > 1)        events[x-2] = 1;
            if (x > 2)        events[x-3] = 1;
            newevent = 1;
            break;
        case ACBD:
            if (x < nwin-2)    events[x+2] = 1;
            if (x > 1)        events[x-2] = 1;
            newevent = 1;
            break;
        default:
            FREE(events);
            return(0);
        }
        events[x] = 0;
#ifdef DD_STATS
        if (res == ABCD) {
            (void) fprintf(table->out,"=");
        } else {
            (void) fprintf(table->out,"-");
        }
        fflush(table->out);
#endif
        }
    }
#ifdef DD_STATS
    if (newevent) {
        (void) fprintf(table->out,"|");
        fflush(table->out);
    }
#endif
    } while (newevent);

    FREE(events);

    return(1);

} /* end of ddWindowConv4 */