cuddZddGroup.c 41.8 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddZddGroup.c]

  PackageName [cudd]

  Synopsis    [Functions for ZDD group sifting.]

  Description [External procedures included in this file:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
                <ul>
                <li> Cudd_MakeZddTreeNode()
                </ul>
        Internal procedures included in this file:
                <ul>
                <li> cuddZddTreeSifting()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> zddTreeSiftingAux()
                <li> zddCountInternalMtrNodes()
                <li> zddReorderChildren()
                <li> zddFindNodeHiLo()
                <li> zddUniqueCompareGroup()
                <li> zddGroupSifting()
                <li> zddGroupSiftingAux()
                <li> zddGroupSiftingUp()
                <li> zddGroupSiftingDown()
                <li> zddGroupMove()
                <li> zddGroupMoveBackward()
                <li> zddGroupSiftingBackward()
                <li> zddMergeGroups()
                </ul>]
Alan Mishchenko committed
33 34 35

  Author      [Fabio Somenzi]

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
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
67 68 69

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

70
#include "misc/util/util_hack.h"
Alan Mishchenko committed
71 72
#include "cuddInt.h"

73 74 75
ABC_NAMESPACE_IMPL_START


76

Alan Mishchenko committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

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

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

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

#ifndef lint
94
static char rcsid[] DD_UNUSED = "$Id: cuddZddGroup.c,v 1.20 2009/02/19 16:25:36 fabio Exp $";
Alan Mishchenko committed
95 96
#endif

97 98
static  int     *entry;
extern  int     zddTotalNumberSwapping;
Alan Mishchenko committed
99 100 101 102 103 104 105 106
#ifdef DD_STATS
static  int     extsymmcalls;
static  int     extsymm;
static  int     secdiffcalls;
static  int     secdiff;
static  int     secdiffmisfire;
#endif
#ifdef DD_DEBUG
107 108
static  int     pr = 0; /* flag to enable printing while debugging */
                        /* by depositing a 1 into it */
Alan Mishchenko committed
109 110 111 112 113 114 115 116 117 118 119 120
#endif

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

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

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

121
static int zddTreeSiftingAux (DdManager *table, MtrNode *treenode, Cudd_ReorderingType method);
Alan Mishchenko committed
122
#ifdef DD_STATS
123
static int zddCountInternalMtrNodes (DdManager *table, MtrNode *treenode);
Alan Mishchenko committed
124
#endif
125 126 127 128 129 130 131 132 133 134 135
static int zddReorderChildren (DdManager *table, MtrNode *treenode, Cudd_ReorderingType method);
static void zddFindNodeHiLo (DdManager *table, MtrNode *treenode, int *lower, int *upper);
static int zddUniqueCompareGroup (int *ptrX, int *ptrY);
static int zddGroupSifting (DdManager *table, int lower, int upper);
static int zddGroupSiftingAux (DdManager *table, int x, int xLow, int xHigh);
static int zddGroupSiftingUp (DdManager *table, int y, int xLow, Move **moves);
static int zddGroupSiftingDown (DdManager *table, int x, int xHigh, Move **moves);
static int zddGroupMove (DdManager *table, int x, int y, Move **moves);
static int zddGroupMoveBackward (DdManager *table, int x, int y);
static int zddGroupSiftingBackward (DdManager *table, Move *moves, int size);
static void zddMergeGroups (DdManager *table, MtrNode *treenode, int low, int high);
Alan Mishchenko committed
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

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


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


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

  Synopsis    [Creates a new ZDD variable group.]

  Description [Creates a new ZDD variable group. The group starts at
  variable and contains size variables. The parameter low is the index
  of the first variable. If the variable already exists, its current
  position in the order is known to the manager. If the variable does
  not exist yet, the position is assumed to be the same as the index.
  The group tree is created if it does not exist yet.
  Returns a pointer to the group if successful; NULL otherwise.]

  SideEffects [The ZDD variable tree is changed.]

  SeeAlso     [Cudd_MakeTreeNode]

******************************************************************************/
MtrNode *
Cudd_MakeZddTreeNode(
  DdManager * dd /* manager */,
  unsigned int  low /* index of the first group variable */,
  unsigned int  size /* number of variables in the group */,
  unsigned int  type /* MTR_DEFAULT or MTR_FIXED */)
{
    MtrNode *group;
    MtrNode *tree;
    unsigned int level;

    /* If the variable does not exist yet, the position is assumed to be
    ** the same as the index. Therefore, applications that rely on
    ** Cudd_bddNewVarAtLevel or Cudd_addNewVarAtLevel to create new
    ** variables have to create the variables before they group them.
    */
    level = (low < (unsigned int) dd->sizeZ) ? dd->permZ[low] : low;

    if (level + size - 1> (int) MTR_MAXHIGH)
181
        return(NULL);
Alan Mishchenko committed
182 183 184 185

    /* If the tree does not exist yet, create it. */
    tree = dd->treeZ;
    if (tree == NULL) {
186 187 188 189
        dd->treeZ = tree = Mtr_InitGroupTree(0, dd->sizeZ);
        if (tree == NULL)
            return(NULL);
        tree->index = dd->invpermZ[0];
Alan Mishchenko committed
190 191 192 193 194 195 196 197 198 199
    }

    /* Extend the upper bound of the tree if necessary. This allows the
    ** application to create groups even before the variables are created.
    */
    tree->size = ddMax(tree->size, level + size);

    /* Create the group. */
    group = Mtr_MakeGroup(tree, level, size, type);
    if (group == NULL)
200
        return(NULL);
Alan Mishchenko committed
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

    /* Initialize the index field to the index of the variable currently
    ** in position low. This field will be updated by the reordering
    ** procedure to provide a handle to the group once it has been moved.
    */
    group->index = (MtrHalfWord) low;

    return(group);

} /* end of Cudd_MakeZddTreeNode */


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


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

  Synopsis    [Tree sifting algorithm for ZDDs.]

  Description [Tree sifting algorithm for ZDDs. Assumes that a tree
  representing a group hierarchy is passed as a parameter. It then
  reorders each group in postorder fashion by calling
  zddTreeSiftingAux.  Assumes that no dead nodes are present.  Returns
  1 if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
int
cuddZddTreeSifting(
  DdManager * table /* DD table */,
  Cudd_ReorderingType method /* reordering method for the groups of leaves */)
{
    int i;
    int nvars;
    int result;
    int tempTree;

    /* If no tree is provided we create a temporary one in which all
    ** variables are in a single group. After reordering this tree is
    ** destroyed.
    */
    tempTree = table->treeZ == NULL;
    if (tempTree) {
247 248
        table->treeZ = Mtr_InitGroupTree(0,table->sizeZ);
        table->treeZ->index = table->invpermZ[0];
Alan Mishchenko committed
249 250 251 252 253
    }
    nvars = table->sizeZ;

#ifdef DD_DEBUG
    if (pr > 0 && !tempTree)
254
        (void) fprintf(table->out,"cuddZddTreeSifting:");
Alan Mishchenko committed
255 256 257 258 259
    Mtr_PrintGroups(table->treeZ,pr <= 0);
#endif
#if 0
    /* Debugging code. */
    if (table->tree && table->treeZ) {
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
        (void) fprintf(table->out,"\n");
        Mtr_PrintGroups(table->tree, 0);
        cuddPrintVarGroups(table,table->tree,0,0);
        for (i = 0; i < table->size; i++) {
            (void) fprintf(table->out,"%s%d",
                           (i == 0) ? "" : ",", table->invperm[i]);
        }
        (void) fprintf(table->out,"\n");
        for (i = 0; i < table->size; i++) {
            (void) fprintf(table->out,"%s%d",
                           (i == 0) ? "" : ",", table->perm[i]);
        }
        (void) fprintf(table->out,"\n\n");
        Mtr_PrintGroups(table->treeZ,0);
        cuddPrintVarGroups(table,table->treeZ,1,0);
        for (i = 0; i < table->sizeZ; i++) {
            (void) fprintf(table->out,"%s%d",
                           (i == 0) ? "" : ",", table->invpermZ[i]);
        }
        (void) fprintf(table->out,"\n");
        for (i = 0; i < table->sizeZ; i++) {
            (void) fprintf(table->out,"%s%d",
                           (i == 0) ? "" : ",", table->permZ[i]);
        }
        (void) fprintf(table->out,"\n");
Alan Mishchenko committed
285 286 287 288 289 290 291 292 293 294 295 296
    }
    /* End of debugging code. */
#endif
#ifdef DD_STATS
    extsymmcalls = 0;
    extsymm = 0;
    secdiffcalls = 0;
    secdiff = 0;
    secdiffmisfire = 0;

    (void) fprintf(table->out,"\n");
    if (!tempTree)
297 298
        (void) fprintf(table->out,"#:IM_NODES  %8d: group tree nodes\n",
                       zddCountInternalMtrNodes(table,table->treeZ));
Alan Mishchenko committed
299 300 301 302 303 304 305 306 307 308 309 310
#endif

    /* Initialize the group of each subtable to itself. Initially
    ** there are no groups. Groups are created according to the tree
    ** structure in postorder fashion.
    */
    for (i = 0; i < nvars; i++)
        table->subtableZ[i].next = i;

    /* Reorder. */
    result = zddTreeSiftingAux(table, table->treeZ, method);

311
#ifdef DD_STATS         /* print stats */
Alan Mishchenko committed
312
    if (!tempTree && method == CUDD_REORDER_GROUP_SIFT &&
313 314 315 316
        (table->groupcheck == CUDD_GROUP_CHECK7 ||
         table->groupcheck == CUDD_GROUP_CHECK5)) {
        (void) fprintf(table->out,"\nextsymmcalls = %d\n",extsymmcalls);
        (void) fprintf(table->out,"extsymm = %d",extsymm);
Alan Mishchenko committed
317 318
    }
    if (!tempTree && method == CUDD_REORDER_GROUP_SIFT &&
319 320 321 322
        table->groupcheck == CUDD_GROUP_CHECK7) {
        (void) fprintf(table->out,"\nsecdiffcalls = %d\n",secdiffcalls);
        (void) fprintf(table->out,"secdiff = %d\n",secdiff);
        (void) fprintf(table->out,"secdiffmisfire = %d",secdiffmisfire);
Alan Mishchenko committed
323 324 325 326
    }
#endif

    if (tempTree)
327
        Cudd_FreeZddTree(table);
Alan Mishchenko committed
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
    return(result);

} /* end of cuddZddTreeSifting */


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


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

  Synopsis    [Visits the group tree and reorders each group.]

  Description [Recursively visits the group tree and reorders each
  group in postorder fashion.  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddTreeSiftingAux(
  DdManager * table,
  MtrNode * treenode,
  Cudd_ReorderingType method)
{
    MtrNode  *auxnode;
    int res;

#ifdef DD_DEBUG
    Mtr_PrintGroups(treenode,1);
#endif

    auxnode = treenode;
    while (auxnode != NULL) {
363 364 365 366 367 368 369 370 371 372 373
        if (auxnode->child != NULL) {
            if (!zddTreeSiftingAux(table, auxnode->child, method))
                return(0);
            res = zddReorderChildren(table, auxnode, CUDD_REORDER_GROUP_SIFT);
            if (res == 0)
                return(0);
        } else if (auxnode->size > 1) {
            if (!zddReorderChildren(table, auxnode, method))
                return(0);
        }
        auxnode = auxnode->younger;
Alan Mishchenko committed
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
    }

    return(1);

} /* end of zddTreeSiftingAux */


#ifdef DD_STATS
/**Function********************************************************************

  Synopsis    [Counts the number of internal nodes of the group tree.]

  Description [Counts the number of internal nodes of the group tree.
  Returns the count.]

  SideEffects [None]

******************************************************************************/
static int
zddCountInternalMtrNodes(
  DdManager * table,
  MtrNode * treenode)
{
    MtrNode *auxnode;
    int     count,nodeCount;


    nodeCount = 0;
    auxnode = treenode;
    while (auxnode != NULL) {
404 405 406 407 408 409
        if (!(MTR_TEST(auxnode,MTR_TERMINAL))) {
            nodeCount++;
            count = zddCountInternalMtrNodes(table,auxnode->child);
            nodeCount += count;
        }
        auxnode = auxnode->younger;
Alan Mishchenko committed
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
    }

    return(nodeCount);

} /* end of zddCountInternalMtrNodes */
#endif


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

  Synopsis    [Reorders the children of a group tree node according to
  the options.]

  Description [Reorders the children of a group tree node according to
  the options. After reordering puts all the variables in the group
  and/or its descendents in a single group. This allows hierarchical
  reordering.  If the variables in the group do not exist yet, simply
  does nothing. Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddReorderChildren(
  DdManager * table,
  MtrNode * treenode,
  Cudd_ReorderingType method)
{
    int lower;
439
    int upper = -1;
Alan Mishchenko committed
440 441 442 443 444 445
    int result;
    unsigned int initialSize;

    zddFindNodeHiLo(table,treenode,&lower,&upper);
    /* If upper == -1 these variables do not exist yet. */
    if (upper == -1)
446
        return(1);
Alan Mishchenko committed
447 448

    if (treenode->flags == MTR_FIXED) {
449
        result = 1;
Alan Mishchenko committed
450 451
    } else {
#ifdef DD_STATS
452
        (void) fprintf(table->out," ");
Alan Mishchenko committed
453
#endif
454 455 456 457
        switch (method) {
        case CUDD_REORDER_RANDOM:
        case CUDD_REORDER_RANDOM_PIVOT:
            result = cuddZddSwapping(table,lower,upper,method);
Alan Mishchenko committed
458
            break;
459 460 461 462 463 464 465 466 467
        case CUDD_REORDER_SIFT:
            result = cuddZddSifting(table,lower,upper);
            break;
        case CUDD_REORDER_SIFT_CONVERGE:
            do {
                initialSize = table->keysZ;
                result = cuddZddSifting(table,lower,upper);
                if (initialSize <= table->keysZ)
                    break;
Alan Mishchenko committed
468
#ifdef DD_STATS
469 470
                else
                    (void) fprintf(table->out,"\n");
Alan Mishchenko committed
471
#endif
472 473 474 475 476 477 478 479 480 481
            } while (result != 0);
            break;
        case CUDD_REORDER_SYMM_SIFT:
            result = cuddZddSymmSifting(table,lower,upper);
            break;
        case CUDD_REORDER_SYMM_SIFT_CONV:
            result = cuddZddSymmSiftingConv(table,lower,upper);
            break;
        case CUDD_REORDER_GROUP_SIFT:
            result = zddGroupSifting(table,lower,upper);
Alan Mishchenko committed
482
            break;
483 484 485 486 487 488 489 490 491
        case CUDD_REORDER_LINEAR:
            result = cuddZddLinearSifting(table,lower,upper);
            break;
        case CUDD_REORDER_LINEAR_CONVERGE:
            do {
                initialSize = table->keysZ;
                result = cuddZddLinearSifting(table,lower,upper);
                if (initialSize <= table->keysZ)
                    break;
Alan Mishchenko committed
492
#ifdef DD_STATS
493 494
                else
                    (void) fprintf(table->out,"\n");
Alan Mishchenko committed
495
#endif
496 497 498 499 500
            } while (result != 0);
            break;
        default:
            return(0);
        }
Alan Mishchenko committed
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
    }

    /* Create a single group for all the variables that were sifted,
    ** so that they will be treated as a single block by successive
    ** invocations of zddGroupSifting.
    */
    zddMergeGroups(table,treenode,lower,upper);

#ifdef DD_DEBUG
    if (pr > 0) (void) fprintf(table->out,"zddReorderChildren:");
#endif

    return(result);

} /* end of zddReorderChildren */


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

  Synopsis    [Finds the lower and upper bounds of the group represented
  by treenode.]

  Description [Finds the lower and upper bounds of the group represented
  by treenode.  The high and low fields of treenode are indices.  From
  those we need to derive the current positions, and find maximum and
  minimum.]

  SideEffects [The bounds are returned as side effects.]

  SeeAlso     []

******************************************************************************/
static void
zddFindNodeHiLo(
  DdManager * table,
  MtrNode * treenode,
  int * lower,
  int * upper)
{
    int low;
    int high;

    /* Check whether no variables in this group already exist.
    ** If so, return immediately. The calling procedure will know from
    ** the values of upper that no reordering is needed.
    */
    if ((int) treenode->low >= table->sizeZ) {
548 549 550
        *lower = table->sizeZ;
        *upper = -1;
        return;
Alan Mishchenko committed
551 552 553 554 555 556
    }

    *lower = low = (unsigned int) table->permZ[treenode->index];
    high = (int) (low + treenode->size - 1);

    if (high >= table->sizeZ) {
557 558 559 560 561 562
        /* This is the case of a partially existing group. The aim is to
        ** reorder as many variables as safely possible.  If the tree
        ** node is terminal, we just reorder the subset of the group
        ** that is currently in existence.  If the group has
        ** subgroups, then we only reorder those subgroups that are
        ** fully instantiated.  This way we avoid breaking up a group.
Alan Mishchenko committed
563
        */
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
        MtrNode *auxnode = treenode->child;
        if (auxnode == NULL) {
            *upper = (unsigned int) table->sizeZ - 1;
        } else {
            /* Search the subgroup that strands the table->sizeZ line.
            ** If the first group starts at 0 and goes past table->sizeZ
            ** upper will get -1, thus correctly signaling that no reordering
            ** should take place.
            */
            while (auxnode != NULL) {
                int thisLower = table->permZ[auxnode->low];
                int thisUpper = thisLower + auxnode->size - 1;
                if (thisUpper >= table->sizeZ && thisLower < table->sizeZ)
                    *upper = (unsigned int) thisLower - 1;
                auxnode = auxnode->younger;
            }
Alan Mishchenko committed
580 581
        }
    } else {
582 583
        /* Normal case: All the variables of the group exist. */
        *upper = (unsigned int) high;
Alan Mishchenko committed
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
    }

#ifdef DD_DEBUG
    /* Make sure that all variables in group are contiguous. */
    assert(treenode->size >= *upper - *lower + 1);
#endif

    return;

} /* end of zddFindNodeHiLo */


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

  Synopsis    [Comparison function used by qsort.]

  Description [Comparison function used by qsort to order the variables
  according to the number of keys in the subtables.  Returns the
  difference in number of keys between the two variables being
  compared.]

  SideEffects [None]

******************************************************************************/
static int
zddUniqueCompareGroup(
  int * ptrX,
  int * ptrY)
{
#if 0
    if (entry[*ptrY] == entry[*ptrX]) {
615
        return((*ptrX) - (*ptrY));
Alan Mishchenko committed
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
    }
#endif
    return(entry[*ptrY] - entry[*ptrX]);

} /* end of zddUniqueCompareGroup */


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

  Synopsis    [Sifts from treenode->low to treenode->high.]

  Description [Sifts from treenode->low to treenode->high. If
  croupcheck == CUDD_GROUP_CHECK7, it checks for group creation at the
  end of the initial sifting. If a group is created, it is then sifted
  again. After sifting one variable, the group that contains it is
  dissolved.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddGroupSifting(
  DdManager * table,
  int  lower,
  int  upper)
{
642 643 644 645 646 647
    int         *var;
    int         i,j,x,xInit;
    int         nvars;
    int         classes;
    int         result;
    int         *sifted;
Alan Mishchenko committed
648 649 650
#ifdef DD_STATS
    unsigned    previousSize;
#endif
651
    int         xindex;
Alan Mishchenko committed
652 653 654 655 656 657

    nvars = table->sizeZ;

    /* Order variables to sift. */
    entry = NULL;
    sifted = NULL;
Alan Mishchenko committed
658
    var = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
659
    if (var == NULL) {
660 661
        table->errorCode = CUDD_MEMORY_OUT;
        goto zddGroupSiftingOutOfMem;
Alan Mishchenko committed
662
    }
Alan Mishchenko committed
663
    entry = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
664
    if (entry == NULL) {
665 666
        table->errorCode = CUDD_MEMORY_OUT;
        goto zddGroupSiftingOutOfMem;
Alan Mishchenko committed
667
    }
Alan Mishchenko committed
668
    sifted = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
669
    if (sifted == NULL) {
670 671
        table->errorCode = CUDD_MEMORY_OUT;
        goto zddGroupSiftingOutOfMem;
Alan Mishchenko committed
672 673 674 675
    }

    /* Here we consider only one representative for each group. */
    for (i = 0, classes = 0; i < nvars; i++) {
676 677 678 679 680 681 682
        sifted[i] = 0;
        x = table->permZ[i];
        if ((unsigned) x >= table->subtableZ[x].next) {
            entry[i] = table->subtableZ[x].keys;
            var[classes] = i;
            classes++;
        }
Alan Mishchenko committed
683 684
    }

685
    qsort((void *)var,classes,sizeof(int),(DD_QSFP)zddUniqueCompareGroup);
Alan Mishchenko committed
686 687 688

    /* Now sift. */
    for (i = 0; i < ddMin(table->siftMaxVar,classes); i++) {
689 690 691 692 693
        if (zddTotalNumberSwapping >= table->siftMaxSwap)
            break;
        xindex = var[i];
        if (sifted[xindex] == 1) /* variable already sifted as part of group */
            continue;
Alan Mishchenko committed
694
        x = table->permZ[xindex]; /* find current level of this variable */
695 696
        if (x < lower || x > upper)
            continue;
Alan Mishchenko committed
697
#ifdef DD_STATS
698
        previousSize = table->keysZ;
Alan Mishchenko committed
699 700
#endif
#ifdef DD_DEBUG
701
        /* x is bottom of group */
Alan Mishchenko committed
702 703
        assert((unsigned) x >= table->subtableZ[x].next);
#endif
704 705
        result = zddGroupSiftingAux(table,x,lower,upper);
        if (!result) goto zddGroupSiftingOutOfMem;
Alan Mishchenko committed
706 707

#ifdef DD_STATS
708 709 710 711 712 713 714 715
        if (table->keysZ < previousSize) {
            (void) fprintf(table->out,"-");
        } else if (table->keysZ > previousSize) {
            (void) fprintf(table->out,"+");
        } else {
            (void) fprintf(table->out,"=");
        }
        fflush(table->out);
Alan Mishchenko committed
716 717
#endif

718 719 720 721 722 723 724 725 726 727
        /* Mark variables in the group just sifted. */
        x = table->permZ[xindex];
        if ((unsigned) x != table->subtableZ[x].next) {
            xInit = x;
            do {
                j = table->invpermZ[x];
                sifted[j] = 1;
                x = table->subtableZ[x].next;
            } while (x != xInit);
        }
Alan Mishchenko committed
728 729

#ifdef DD_DEBUG
730
        if (pr > 0) (void) fprintf(table->out,"zddGroupSifting:");
Alan Mishchenko committed
731 732 733
#endif
    } /* for */

Alan Mishchenko committed
734 735 736
    ABC_FREE(sifted);
    ABC_FREE(var);
    ABC_FREE(entry);
Alan Mishchenko committed
737 738 739 740

    return(1);

zddGroupSiftingOutOfMem:
741
    if (entry != NULL)  ABC_FREE(entry);
Alan Mishchenko committed
742
    if (var != NULL)    ABC_FREE(var);
743
    if (sifted != NULL) ABC_FREE(sifted);
Alan Mishchenko committed
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

    return(0);

} /* end of zddGroupSifting */


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

  Synopsis    [Sifts one variable up and down until it has taken all
  positions. Checks for aggregation.]

  Description [Sifts one variable up and down until it has taken all
  positions. Checks for aggregation. There may be at most two sweeps,
  even if the group grows.  Assumes that x is either an isolated
  variable, or it is the bottom of a group. All groups may not have
  been found. The variable being moved is returned to the best position
  seen during sifting.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddGroupSiftingAux(
  DdManager * table,
  int  x,
  int  xLow,
  int  xHigh)
{
    Move *move;
773
    Move *moves;        /* list of moves */
Alan Mishchenko committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787
    int  initialSize;
    int  result;


#ifdef DD_DEBUG
    if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingAux from %d to %d\n",xLow,xHigh);
    assert((unsigned) x >= table->subtableZ[x].next); /* x is bottom of group */
#endif

    initialSize = table->keysZ;
    moves = NULL;

    if (x == xLow) { /* Sift down */
#ifdef DD_DEBUG
788 789
        /* x must be a singleton */
        assert((unsigned) x == table->subtableZ[x].next);
Alan Mishchenko committed
790
#endif
791
        if (x == xHigh) return(1);      /* just one variable */
Alan Mishchenko committed
792 793 794

        if (!zddGroupSiftingDown(table,x,xHigh,&moves))
            goto zddGroupSiftingAuxOutOfMem;
795
        /* at this point x == xHigh, unless early term */
Alan Mishchenko committed
796

797 798
        /* move backward and stop at best position */
        result = zddGroupSiftingBackward(table,moves,initialSize);
Alan Mishchenko committed
799
#ifdef DD_DEBUG
800
        assert(table->keysZ <= (unsigned) initialSize);
Alan Mishchenko committed
801 802 803 804 805
#endif
        if (!result) goto zddGroupSiftingAuxOutOfMem;

    } else if (cuddZddNextHigh(table,x) > xHigh) { /* Sift up */
#ifdef DD_DEBUG
806
        /* x is bottom of group */
Alan Mishchenko committed
807 808 809 810 811 812 813
        assert((unsigned) x >= table->subtableZ[x].next);
#endif
        /* Find top of x's group */
        x = table->subtableZ[x].next;

        if (!zddGroupSiftingUp(table,x,xLow,&moves))
            goto zddGroupSiftingAuxOutOfMem;
814
        /* at this point x == xLow, unless early term */
Alan Mishchenko committed
815

816 817
        /* move backward and stop at best position */
        result = zddGroupSiftingBackward(table,moves,initialSize);
Alan Mishchenko committed
818
#ifdef DD_DEBUG
819
        assert(table->keysZ <= (unsigned) initialSize);
Alan Mishchenko committed
820 821 822 823 824 825
#endif
        if (!result) goto zddGroupSiftingAuxOutOfMem;

    } else if (x - xLow > xHigh - x) { /* must go down first: shorter */
        if (!zddGroupSiftingDown(table,x,xHigh,&moves))
            goto zddGroupSiftingAuxOutOfMem;
826
        /* at this point x == xHigh, unless early term */
Alan Mishchenko committed
827 828

        /* Find top of group */
829 830 831 832 833
        if (moves) {
            x = moves->y;
        }
        while ((unsigned) x < table->subtableZ[x].next)
            x = table->subtableZ[x].next;
Alan Mishchenko committed
834 835 836 837 838 839 840 841 842
        x = table->subtableZ[x].next;
#ifdef DD_DEBUG
        /* x should be the top of a group */
        assert((unsigned) x <= table->subtableZ[x].next);
#endif

        if (!zddGroupSiftingUp(table,x,xLow,&moves))
            goto zddGroupSiftingAuxOutOfMem;

843 844
        /* move backward and stop at best position */
        result = zddGroupSiftingBackward(table,moves,initialSize);
Alan Mishchenko committed
845
#ifdef DD_DEBUG
846
        assert(table->keysZ <= (unsigned) initialSize);
Alan Mishchenko committed
847 848 849 850 851 852 853 854 855
#endif
        if (!result) goto zddGroupSiftingAuxOutOfMem;

    } else { /* moving up first: shorter */
        /* Find top of x's group */
        x = table->subtableZ[x].next;

        if (!zddGroupSiftingUp(table,x,xLow,&moves))
            goto zddGroupSiftingAuxOutOfMem;
856
        /* at this point x == xHigh, unless early term */
Alan Mishchenko committed
857 858

        if (moves) {
859 860 861 862
            x = moves->x;
        }
        while ((unsigned) x < table->subtableZ[x].next)
            x = table->subtableZ[x].next;
Alan Mishchenko committed
863 864 865 866 867 868 869 870
#ifdef DD_DEBUG
        /* x is bottom of a group */
        assert((unsigned) x >= table->subtableZ[x].next);
#endif

        if (!zddGroupSiftingDown(table,x,xHigh,&moves))
            goto zddGroupSiftingAuxOutOfMem;

871 872
        /* move backward and stop at best position */
        result = zddGroupSiftingBackward(table,moves,initialSize);
Alan Mishchenko committed
873
#ifdef DD_DEBUG
874
        assert(table->keysZ <= (unsigned) initialSize);
Alan Mishchenko committed
875 876 877 878 879 880
#endif
        if (!result) goto zddGroupSiftingAuxOutOfMem;
    }

    while (moves != NULL) {
        move = moves->next;
881
        cuddDeallocMove(table, moves);
Alan Mishchenko committed
882 883 884 885 886 887 888 889
        moves = move;
    }

    return(1);

zddGroupSiftingAuxOutOfMem:
    while (moves != NULL) {
        move = moves->next;
890
        cuddDeallocMove(table, moves);
Alan Mishchenko committed
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
        moves = move;
    }

    return(0);

} /* end of zddGroupSiftingAux */


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

  Synopsis    [Sifts up a variable until either it reaches position xLow
  or the size of the DD heap increases too much.]

  Description [Sifts up a variable until either it reaches position
  xLow or the size of the DD heap increases too much. Assumes that y is
  the top of a group (or a singleton).  Checks y for aggregation to the
  adjacent variables. Records all the moves that are appended to the
  list of moves received as input and returned as a side effect.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddGroupSiftingUp(
  DdManager * table,
  int  y,
  int  xLow,
  Move ** moves)
{
    Move *move;
    int  x;
    int  size;
    int  gxtop;
    int  limitSize;

    limitSize = table->keysZ;

    x = cuddZddNextLow(table,y);
    while (x >= xLow) {
        gxtop = table->subtableZ[x].next;
        if (table->subtableZ[x].next == (unsigned) x &&
933
            table->subtableZ[y].next == (unsigned) y) {
Alan Mishchenko committed
934 935 936 937 938 939 940 941 942 943 944
            /* x and y are self groups */
            size = cuddZddSwapInPlace(table,x,y);
#ifdef DD_DEBUG
            assert(table->subtableZ[x].next == (unsigned) x);
            assert(table->subtableZ[y].next == (unsigned) y);
#endif
            if (size == 0) goto zddGroupSiftingUpOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL) goto zddGroupSiftingUpOutOfMem;
            move->x = x;
            move->y = y;
945
            move->flags = MTR_DEFAULT;
Alan Mishchenko committed
946 947 948 949 950
            move->size = size;
            move->next = *moves;
            *moves = move;

#ifdef DD_DEBUG
951
            if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingUp (2 single groups):\n");
Alan Mishchenko committed
952 953
#endif
            if ((double) size > (double) limitSize * table->maxGrowth)
954
                return(1);
Alan Mishchenko committed
955 956 957
            if (size < limitSize) limitSize = size;
        } else { /* group move */
            size = zddGroupMove(table,x,y,moves);
958
            if (size == 0) goto zddGroupSiftingUpOutOfMem;
Alan Mishchenko committed
959
            if ((double) size > (double) limitSize * table->maxGrowth)
960
                return(1);
Alan Mishchenko committed
961 962 963 964 965 966 967 968 969 970 971
            if (size < limitSize) limitSize = size;
        }
        y = gxtop;
        x = cuddZddNextLow(table,y);
    }

    return(1);

zddGroupSiftingUpOutOfMem:
    while (*moves != NULL) {
        move = (*moves)->next;
972
        cuddDeallocMove(table, *moves);
Alan Mishchenko committed
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 998 999 1000 1001
        *moves = move;
    }
    return(0);

} /* end of zddGroupSiftingUp */


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

  Synopsis    [Sifts down a variable until it reaches position xHigh.]

  Description [Sifts down a variable until it reaches position xHigh.
  Assumes that x is the bottom of a group (or a singleton).  Records
  all the moves.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddGroupSiftingDown(
  DdManager * table,
  int  x,
  int  xHigh,
  Move ** moves)
{
    Move *move;
    int  y;
    int  size;
    int  limitSize;
1002
    int  gybot;
Alan Mishchenko committed
1003 1004 1005 1006 1007 1008


    /* Initialize R */
    limitSize = size = table->keysZ;
    y = cuddZddNextHigh(table,x);
    while (y <= xHigh) {
1009
        /* Find bottom of y group. */
Alan Mishchenko committed
1010 1011 1012 1013 1014
        gybot = table->subtableZ[y].next;
        while (table->subtableZ[gybot].next != (unsigned) y)
            gybot = table->subtableZ[gybot].next;

        if (table->subtableZ[x].next == (unsigned) x &&
1015
            table->subtableZ[y].next == (unsigned) y) {
Alan Mishchenko committed
1016 1017 1018 1019 1020 1021 1022 1023
            /* x and y are self groups */
            size = cuddZddSwapInPlace(table,x,y);
#ifdef DD_DEBUG
            assert(table->subtableZ[x].next == (unsigned) x);
            assert(table->subtableZ[y].next == (unsigned) y);
#endif
            if (size == 0) goto zddGroupSiftingDownOutOfMem;

1024
            /* Record move. */
Alan Mishchenko committed
1025 1026 1027 1028
            move = (Move *) cuddDynamicAllocNode(table);
            if (move == NULL) goto zddGroupSiftingDownOutOfMem;
            move->x = x;
            move->y = y;
1029
            move->flags = MTR_DEFAULT;
Alan Mishchenko committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
            move->size = size;
            move->next = *moves;
            *moves = move;

#ifdef DD_DEBUG
            if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingDown (2 single groups):\n");
#endif
            if ((double) size > (double) limitSize * table->maxGrowth)
                return(1);
            if (size < limitSize) limitSize = size;
            x = y;
            y = cuddZddNextHigh(table,x);
        } else { /* Group move */
            size = zddGroupMove(table,x,y,moves);
            if (size == 0) goto zddGroupSiftingDownOutOfMem;
            if ((double) size > (double) limitSize * table->maxGrowth)
1046
                return(1);
Alan Mishchenko committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
            if (size < limitSize) limitSize = size;
        }
        x = gybot;
        y = cuddZddNextHigh(table,x);
    }

    return(1);

zddGroupSiftingDownOutOfMem:
    while (*moves != NULL) {
        move = (*moves)->next;
1058
        cuddDeallocMove(table, *moves);
Alan Mishchenko committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
        *moves = move;
    }

    return(0);

} /* end of zddGroupSiftingDown */


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

  Synopsis    [Swaps two groups and records the move.]

  Description [Swaps two groups and records the move. Returns the
  number of keys in the DD table in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddGroupMove(
  DdManager * table,
  int  x,
  int  y,
  Move ** moves)
{
    Move *move;
    int  size;
    int  i,j,xtop,xbot,xsize,ytop,ybot,ysize,newxtop;
1087
    int  swapx=-1,swapy=-1;
Alan Mishchenko committed
1088 1089 1090 1091
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
    int  initialSize,bestSize;
#endif

1092
#ifdef DD_DEBUG
Alan Mishchenko committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    /* We assume that x < y */
    assert(x < y);
#endif
    /* Find top, bottom, and size for the two groups. */
    xbot = x;
    xtop = table->subtableZ[x].next;
    xsize = xbot - xtop + 1;
    ybot = y;
    while ((unsigned) ybot < table->subtableZ[ybot].next)
        ybot = table->subtableZ[ybot].next;
    ytop = y;
    ysize = ybot - ytop + 1;

#if defined(DD_DEBUG) && defined(DD_VERBOSE)
    initialSize = bestSize = table->keysZ;
#endif
    /* Sift the variables of the second group up through the first group */
    for (i = 1; i <= ysize; i++) {
        for (j = 1; j <= xsize; j++) {
            size = cuddZddSwapInPlace(table,x,y);
            if (size == 0) goto zddGroupMoveOutOfMem;
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
1115 1116
            if (size < bestSize)
                bestSize = size;
Alan Mishchenko committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
#endif
            swapx = x; swapy = y;
            y = x;
            x = cuddZddNextLow(table,y);
        }
        y = ytop + i;
        x = cuddZddNextLow(table,y);
    }
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
    if ((bestSize < initialSize) && (bestSize < size))
1127
        (void) fprintf(table->out,"Missed local minimum: initialSize:%d  bestSize:%d  finalSize:%d\n",initialSize,bestSize,size);
Alan Mishchenko committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
#endif

    /* fix groups */
    y = xtop; /* ytop is now where xtop used to be */
    for (i = 0; i < ysize - 1; i++) {
        table->subtableZ[y].next = cuddZddNextHigh(table,y);
        y = cuddZddNextHigh(table,y);
    }
    table->subtableZ[y].next = xtop; /* y is bottom of its group, join */
                                    /* it to top of its group */
    x = cuddZddNextHigh(table,y);
    newxtop = x;
    for (i = 0; i < xsize - 1; i++) {
        table->subtableZ[x].next = cuddZddNextHigh(table,x);
        x = cuddZddNextHigh(table,x);
    }
    table->subtableZ[x].next = newxtop; /* x is bottom of its group, join */
                                    /* it to top of its group */
#ifdef DD_DEBUG
    if (pr > 0) (void) fprintf(table->out,"zddGroupMove:\n");
#endif

    /* Store group move */
    move = (Move *) cuddDynamicAllocNode(table);
    if (move == NULL) goto zddGroupMoveOutOfMem;
    move->x = swapx;
    move->y = swapy;
    move->flags = MTR_DEFAULT;
    move->size = table->keysZ;
    move->next = *moves;
    *moves = move;

    return(table->keysZ);

zddGroupMoveOutOfMem:
    while (*moves != NULL) {
        move = (*moves)->next;
1165
        cuddDeallocMove(table, *moves);
Alan Mishchenko committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
        *moves = move;
    }
    return(0);

} /* end of zddGroupMove */


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

  Synopsis    [Undoes the swap two groups.]

  Description [Undoes the swap two groups.  Returns 1 in case of
  success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddGroupMoveBackward(
  DdManager * table,
  int  x,
  int  y)
{
    int size;
    int i,j,xtop,xbot,xsize,ytop,ybot,ysize,newxtop;


1193
#ifdef DD_DEBUG
Alan Mishchenko committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    /* We assume that x < y */
    assert(x < y);
#endif

    /* Find top, bottom, and size for the two groups. */
    xbot = x;
    xtop = table->subtableZ[x].next;
    xsize = xbot - xtop + 1;
    ybot = y;
    while ((unsigned) ybot < table->subtableZ[ybot].next)
        ybot = table->subtableZ[ybot].next;
    ytop = y;
    ysize = ybot - ytop + 1;

    /* Sift the variables of the second group up through the first group */
    for (i = 1; i <= ysize; i++) {
        for (j = 1; j <= xsize; j++) {
            size = cuddZddSwapInPlace(table,x,y);
            if (size == 0)
                return(0);
            y = x;
            x = cuddZddNextLow(table,y);
        }
        y = ytop + i;
        x = cuddZddNextLow(table,y);
    }

    /* fix groups */
    y = xtop;
    for (i = 0; i < ysize - 1; i++) {
        table->subtableZ[y].next = cuddZddNextHigh(table,y);
        y = cuddZddNextHigh(table,y);
    }
    table->subtableZ[y].next = xtop; /* y is bottom of its group, join */
                                    /* to its top */
    x = cuddZddNextHigh(table,y);
    newxtop = x;
    for (i = 0; i < xsize - 1; i++) {
        table->subtableZ[x].next = cuddZddNextHigh(table,x);
        x = cuddZddNextHigh(table,x);
    }
    table->subtableZ[x].next = newxtop; /* x is bottom of its group, join */
                                    /* to its top */
#ifdef DD_DEBUG
    if (pr > 0) (void) fprintf(table->out,"zddGroupMoveBackward:\n");
#endif

    return(1);

} /* end of zddGroupMoveBackward */


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

  Synopsis    [Determines the best position for a variables and returns
  it there.]

  Description [Determines the best position for a variables and returns
  it there.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
zddGroupSiftingBackward(
  DdManager * table,
  Move * moves,
  int  size)
{
    Move *move;
    int  res;


    for (move = moves; move != NULL; move = move->next) {
        if (move->size < size) {
            size = move->size;
        }
    }

    for (move = moves; move != NULL; move = move->next) {
        if (move->size == size) return(1);
        if ((table->subtableZ[move->x].next == move->x) &&
1276
        (table->subtableZ[move->y].next == move->y)) {
Alan Mishchenko committed
1277 1278 1279 1280 1281 1282 1283 1284
            res = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
            if (!res) return(0);
#ifdef DD_DEBUG
            if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingBackward:\n");
            assert(table->subtableZ[move->x].next == move->x);
            assert(table->subtableZ[move->y].next == move->y);
#endif
        } else { /* Group move necessary */
1285 1286
            res = zddGroupMoveBackward(table,(int)move->x,(int)move->y);
            if (!res) return(0);
Alan Mishchenko committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
        }
    }

    return(1);

} /* end of zddGroupSiftingBackward */


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

  Synopsis    [Merges groups in the DD table.]

  Description [Creates a single group from low to high and adjusts the
  idex field of the tree node.]

  SideEffects [None]

******************************************************************************/
static void
zddMergeGroups(
  DdManager * table,
  MtrNode * treenode,
  int  low,
  int  high)
{
    int i;
    MtrNode *auxnode;
    int saveindex;
    int newindex;

    /* Merge all variables from low to high in one group, unless
    ** this is the topmost group. In such a case we do not merge lest
    ** we lose the symmetry information. */
    if (treenode != table->treeZ) {
1321 1322 1323
        for (i = low; i < high; i++)
            table->subtableZ[i].next = i+1;
        table->subtableZ[high].next = low;
Alan Mishchenko committed
1324 1325 1326 1327 1328 1329 1330 1331
    }

    /* Adjust the index fields of the tree nodes. If a node is the
    ** first child of its parent, then the parent may also need adjustment. */
    saveindex = treenode->index;
    newindex = table->invpermZ[low];
    auxnode = treenode;
    do {
1332 1333 1334 1335 1336
        auxnode->index = newindex;
        if (auxnode->parent == NULL ||
                (int) auxnode->parent->index != saveindex)
            break;
        auxnode = auxnode->parent;
Alan Mishchenko committed
1337 1338 1339 1340 1341
    } while (1);
    return;

} /* end of zddMergeGroups */

1342

1343 1344
ABC_NAMESPACE_IMPL_END