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

  FileName    [mtrBasic.c]

  PackageName [mtr]

  Synopsis    [Basic manipulation of multiway branching trees.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23
            <ul>
            <li> Mtr_AllocNode()
            <li> Mtr_DeallocNode()
            <li> Mtr_InitTree()
            <li> Mtr_FreeTree()
            <li> Mtr_CopyTree()
            <li> Mtr_MakeFirstChild()
            <li> Mtr_MakeLastChild()
            <li> Mtr_CreateFirstChild()
            <li> Mtr_CreateLastChild()
            <li> Mtr_MakeNextSibling()
            <li> Mtr_PrintTree()
            </ul>
            ]
Alan Mishchenko committed
24 25 26 27 28

  SeeAlso     [cudd package]

  Author      [Fabio Somenzi]

29 30 31 32 33 34 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
  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
60 61 62

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

63
#include "misc/util/util_hack.h"
Alan Mishchenko committed
64 65
#include "mtrInt.h"

66 67
ABC_NAMESPACE_IMPL_START

Alan Mishchenko committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

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

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

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

#ifndef lint
85
static char rcsid[] MTR_UNUSED = "$Id: mtrBasic.c,v 1.13 2009/02/20 02:03:47 fabio Exp $";
Alan Mishchenko committed
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
#endif

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

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

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


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


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

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

  Synopsis    [Allocates new tree node.]

  Description [Allocates new tree node. Returns pointer to node.]

  SideEffects [None]

  SeeAlso     [Mtr_DeallocNode]

******************************************************************************/
MtrNode *
118
Mtr_AllocNode(void)
Alan Mishchenko committed
119 120 121
{
    MtrNode *node;

122
    node = ABC_ALLOC(MtrNode,1);
Alan Mishchenko committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    return node;

} /* Mtr_AllocNode */


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

  Synopsis    [Deallocates tree node.]

  Description []

  SideEffects [None]

  SeeAlso     [Mtr_AllocNode]

******************************************************************************/
void
Mtr_DeallocNode(
  MtrNode * node /* node to be deallocated */)
{
143
    ABC_FREE(node);
Alan Mishchenko committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    return;

} /* end of Mtr_DeallocNode */


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

  Synopsis    [Initializes tree with one node.]

  Description [Initializes tree with one node. Returns pointer to node.]

  SideEffects [None]

  SeeAlso     [Mtr_FreeTree Mtr_InitGroupTree]

******************************************************************************/
MtrNode *
161
Mtr_InitTree(void)
Alan Mishchenko committed
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
{
    MtrNode *node;

    node = Mtr_AllocNode();
    if (node == NULL) return(NULL);

    node->parent = node->child = node->elder = node->younger = NULL;
    node->flags = 0;

    return(node);

} /* end of Mtr_InitTree */


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

  Synopsis    [Disposes of tree rooted at node.]

  Description []

  SideEffects [None]

  SeeAlso     [Mtr_InitTree]

******************************************************************************/
void
Mtr_FreeTree(
  MtrNode * node)
{
    if (node == NULL) return;
    if (! MTR_TEST(node,MTR_TERMINAL)) Mtr_FreeTree(node->child);
    Mtr_FreeTree(node->younger);
    Mtr_DeallocNode(node);
    return;

} /* end of Mtr_FreeTree */


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

  Synopsis    [Makes a copy of tree.]

  Description [Makes a copy of tree. If parameter expansion is greater
  than 1, it will expand the tree by that factor. It is an error for
  expansion to be less than 1. Returns a pointer to the copy if
  successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Mtr_InitTree]

******************************************************************************/
MtrNode *
Mtr_CopyTree(
  MtrNode * node,
  int  expansion)
{
    MtrNode *copy;

    if (node == NULL) return(NULL);
    if (expansion < 1) return(NULL);
    copy = Mtr_AllocNode();
    if (copy == NULL) return(NULL);
    copy->parent = copy->elder = copy->child = copy->younger = NULL;
    if (node->child != NULL) {
227 228 229 230 231
        copy->child = Mtr_CopyTree(node->child, expansion);
        if (copy->child == NULL) {
            Mtr_DeallocNode(copy);
            return(NULL);
        }
Alan Mishchenko committed
232 233
    }
    if (node->younger != NULL) {
234 235 236 237 238
        copy->younger = Mtr_CopyTree(node->younger, expansion);
        if (copy->younger == NULL) {
            Mtr_FreeTree(copy);
            return(NULL);
        }
Alan Mishchenko committed
239 240 241 242 243 244 245
    }
    copy->flags = node->flags;
    copy->low = node->low * expansion;
    copy->size = node->size * expansion;
    copy->index = node->index * expansion;
    if (copy->younger) copy->younger->elder = copy;
    if (copy->child) {
246 247 248 249 250
        MtrNode *auxnode = copy->child;
        while (auxnode != NULL) {
            auxnode->parent = copy;
            auxnode = auxnode->younger;
        }
Alan Mishchenko committed
251 252
    }
    return(copy);
253

Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
} /* end of Mtr_CopyTree */


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

  Synopsis    [Makes child the first child of parent.]

  Description []

  SideEffects [None]

  SeeAlso     [Mtr_MakeLastChild Mtr_CreateFirstChild]

******************************************************************************/
void
Mtr_MakeFirstChild(
  MtrNode * parent,
  MtrNode * child)
{
    child->parent = parent;
    child->younger = parent->child;
    child->elder = NULL;
    if (parent->child != NULL) {
#ifdef MTR_DEBUG
278
        assert(parent->child->elder == NULL);
Alan Mishchenko committed
279
#endif
280
        parent->child->elder = child;
Alan Mishchenko committed
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
    }
    parent->child = child;
    return;

} /* end of Mtr_MakeFirstChild */


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

  Synopsis    [Makes child the last child of parent.]

  Description []

  SideEffects [None]

  SeeAlso     [Mtr_MakeFirstChild Mtr_CreateLastChild]

******************************************************************************/
void
Mtr_MakeLastChild(
  MtrNode * parent,
  MtrNode * child)
{
    MtrNode *node;

    child->younger = NULL;

    if (parent->child == NULL) {
309 310
        parent->child = child;
        child->elder = NULL;
Alan Mishchenko committed
311
    } else {
312 313 314 315 316
        for (node = parent->child;
             node->younger != NULL;
             node = node->younger);
        node->younger = child;
        child->elder = node;
Alan Mishchenko committed
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
    }
    child->parent = parent;
    return;

} /* end of Mtr_MakeLastChild */


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

  Synopsis    [Creates a new node and makes it the first child of parent.]

  Description [Creates a new node and makes it the first child of
  parent. Returns pointer to new child.]

  SideEffects [None]

  SeeAlso     [Mtr_MakeFirstChild Mtr_CreateLastChild]

******************************************************************************/
MtrNode *
Mtr_CreateFirstChild(
  MtrNode * parent)
{
    MtrNode *child;

    child = Mtr_AllocNode();
    if (child == NULL) return(NULL);

    child->child = child->younger = child-> elder = NULL;
    child->flags = 0;
    Mtr_MakeFirstChild(parent,child);
    return(child);

} /* end of Mtr_CreateFirstChild */


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

  Synopsis    [Creates a new node and makes it the last child of parent.]

  Description [Creates a new node and makes it the last child of parent.
  Returns pointer to new child.]

  SideEffects [None]

  SeeAlso     [Mtr_MakeLastChild Mtr_CreateFirstChild]

******************************************************************************/
MtrNode *
Mtr_CreateLastChild(
  MtrNode * parent)
{
    MtrNode *child;

    child = Mtr_AllocNode();
    if (child == NULL) return(NULL);

    child->child = child->younger = child->elder = NULL;
    child->flags = 0;
    Mtr_MakeLastChild(parent,child);
    return(child);

} /* end of Mtr_CreateLastChild */


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

  Synopsis    [Makes second the next sibling of first.]

  Description [Makes second the next sibling of first. Second becomes a
  child of the parent of first.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
void
Mtr_MakeNextSibling(
  MtrNode * first,
  MtrNode * second)
{
    second->younger = first->younger;
    if (first->younger != NULL) {
401
        first->younger->elder = second;
Alan Mishchenko committed
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
    }
    second->parent = first->parent;
    first->younger = second;
    second->elder = first;
    return;

} /* end of Mtr_MakeNextSibling */


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

  Synopsis    [Prints a tree, one node per line.]

  Description []

  SideEffects [None]

  SeeAlso     [Mtr_PrintGroups]

******************************************************************************/
void
Mtr_PrintTree(
  MtrNode * node)
{
    if (node == NULL) return;
    (void) fprintf(stdout,
#if SIZEOF_VOID_P == 8
429
    "N=0x%-8lx C=0x%-8lx Y=0x%-8lx E=0x%-8lx P=0x%-8lx F=%x L=%u S=%u\n",
Alan Mishchenko committed
430 431 432 433
    (unsigned long) node, (unsigned long) node->child,
    (unsigned long) node->younger, (unsigned long) node->elder,
    (unsigned long) node->parent, node->flags, node->low, node->size);
#else
434
    "N=0x%-8x C=0x%-8x Y=0x%-8x E=0x%-8x P=0x%-8x F=%x L=%hu S=%hu\n",
Alan Mishchenko committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    (unsigned) node, (unsigned) node->child,
    (unsigned) node->younger, (unsigned) node->elder,
    (unsigned) node->parent, node->flags, node->low, node->size);
#endif
    if (!MTR_TEST(node,MTR_TERMINAL)) Mtr_PrintTree(node->child);
    Mtr_PrintTree(node->younger);
    return;

} /* end of Mtr_PrintTree */

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

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

453
ABC_NAMESPACE_IMPL_END