cuddLevelQ.c 17 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
/**CFile***********************************************************************

  FileName    [cuddLevelQ.c]

  PackageName [cudd]

  Synopsis    [Procedure to manage level queues.]

  Description [The functions in this file allow an application to
  easily manipulate a queue where nodes are prioritized by level. The
  emphasis is on efficiency. Therefore, the queue items can have
  variable size.  If the application does not need to attach
  information to the nodes, it can declare the queue items to be of
  type DdQueueItem. Otherwise, it can declare them to be of a
  structure type such that the first three fields are data
  pointers. The third pointer points to the node.  The first two
  pointers are used by the level queue functions. The remaining fields
  are initialized to 0 when a new item is created, and are then left
  to the exclusive use of the application. On the DEC Alphas the three
  pointers must be 32-bit pointers when CUDD is compiled with 32-bit
  pointers.  The level queue functions make sure that each node
  appears at most once in the queue. They do so by keeping a hash
  table where the node is used as key.  Queue items are recycled via a
24
  free list for efficiency.
Alan Mishchenko committed
25 26 27
  
  Internal procedures provided by this module:
                <ul>
28 29 30 31 32
                <li> cuddLevelQueueInit()
                <li> cuddLevelQueueQuit()
                <li> cuddLevelQueueEnqueue()
                <li> cuddLevelQueueDequeue()
                </ul>
Alan Mishchenko committed
33
  Static procedures included in this module:
34 35 36 37 38 39 40
                <ul>
                <li> hashLookup()
                <li> hashInsert()
                <li> hashDelete()
                <li> hashResize()
                </ul>
                ]
Alan Mishchenko committed
41 42 43 44 45

  SeeAlso     []

  Author      [Fabio Somenzi]

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
  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
77 78 79

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

80
#include "misc/util/util_hack.h"
Alan Mishchenko committed
81 82
#include "cuddInt.h"

83 84 85
ABC_NAMESPACE_IMPL_START


86

Alan Mishchenko committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


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

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

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

#ifndef lint
105
static char rcsid[] DD_UNUSED = "$Id: cuddLevelQ.c,v 1.13 2009/03/08 02:49:02 fabio Exp $";
Alan Mishchenko committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#endif

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


/**Macro***********************************************************************

  Synopsis    [Hash function for the table of a level queue.]

  Description [Hash function for the table of a level queue.]

  SideEffects [None]

  SeeAlso     [hashInsert hashLookup hashDelete]

******************************************************************************/
#if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
#define lqHash(key,shift) \
126
(((unsigned)(ptruint)(key) * DD_P1) >> (shift))
Alan Mishchenko committed
127 128 129 130 131 132 133 134 135 136 137 138
#else
#define lqHash(key,shift) \
(((unsigned)(key) * DD_P1) >> (shift))
#endif


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

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

139 140 141 142
static DdQueueItem * hashLookup (DdLevelQueue *queue, void *key);
static int hashInsert (DdLevelQueue *queue, DdQueueItem *item);
static void hashDelete (DdLevelQueue *queue, DdQueueItem *item);
static int hashResize (DdLevelQueue *queue);
Alan Mishchenko committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

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


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


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

  Synopsis    [Initializes a level queue.]

  Description [Initializes a level queue. A level queue is a queue
  where inserts are based on the levels of the nodes. Within each
  level the policy is FIFO. Level queues are useful in traversing a
159
  BDD top-down. Queue items are kept in a free list when dequeued for
Alan Mishchenko committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  efficiency. Returns a pointer to the new queue if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [cuddLevelQueueQuit cuddLevelQueueEnqueue cuddLevelQueueDequeue]

******************************************************************************/
DdLevelQueue *
cuddLevelQueueInit(
  int  levels /* number of levels */,
  int  itemSize /* size of the item */,
  int  numBuckets /* initial number of hash buckets */)
{
    DdLevelQueue *queue;
    int logSize;

Alan Mishchenko committed
177
    queue = ABC_ALLOC(DdLevelQueue,1);
Alan Mishchenko committed
178
    if (queue == NULL)
179
        return(NULL);
Alan Mishchenko committed
180 181 182 183 184
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
    /* Keep pointers to the insertion points for all levels. */
Alan Mishchenko committed
185
    queue->last = ABC_ALLOC(DdQueueItem *, levels);
Alan Mishchenko committed
186 187 188 189
#ifdef __osf__
#pragma pointer_size restore
#endif
    if (queue->last == NULL) {
190 191
        ABC_FREE(queue);
        return(NULL);
Alan Mishchenko committed
192 193 194 195 196 197 198 199 200 201
    }
    /* Use a hash table to test for uniqueness. */
    if (numBuckets < 2) numBuckets = 2;
    logSize = cuddComputeFloorLog2(numBuckets);
    queue->numBuckets = 1 << logSize;
    queue->shift = sizeof(int) * 8 - logSize;
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
Alan Mishchenko committed
202
    queue->buckets = ABC_ALLOC(DdQueueItem *, queue->numBuckets);
Alan Mishchenko committed
203 204 205 206
#ifdef __osf__
#pragma pointer_size restore
#endif
    if (queue->buckets == NULL) {
207 208 209
        ABC_FREE(queue->last);
        ABC_FREE(queue);
        return(NULL);
Alan Mishchenko committed
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
    }
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
    memset(queue->last, 0, levels * sizeof(DdQueueItem *));
    memset(queue->buckets, 0, queue->numBuckets * sizeof(DdQueueItem *));
#ifdef __osf__
#pragma pointer_size restore
#endif
    queue->first = NULL;
    queue->freelist = NULL;
    queue->levels = levels;
    queue->itemsize = itemSize;
    queue->size = 0;
    queue->maxsize = queue->numBuckets * DD_MAX_SUBTABLE_DENSITY;
    return(queue);

} /* end of cuddLevelQueueInit */


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

  Synopsis    [Shuts down a level queue.]

  Description [Shuts down a level queue and releases all the
  associated memory.]

  SideEffects [None]

  SeeAlso     [cuddLevelQueueInit]

******************************************************************************/
void
cuddLevelQueueQuit(
  DdLevelQueue * queue)
{
    DdQueueItem *item;

    while (queue->freelist != NULL) {
250 251 252
        item = queue->freelist;
        queue->freelist = item->next;
        ABC_FREE(item);
Alan Mishchenko committed
253 254
    }
    while (queue->first != NULL) {
255 256 257
        item = (DdQueueItem *) queue->first;
        queue->first = item->next;
        ABC_FREE(item);
Alan Mishchenko committed
258
    }
Alan Mishchenko committed
259 260 261
    ABC_FREE(queue->buckets);
    ABC_FREE(queue->last);
    ABC_FREE(queue);
Alan Mishchenko committed
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
    return;

} /* end of cuddLevelQueueQuit */


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

  Synopsis    [Inserts a new key in a level queue.]

  Description [Inserts a new key in a level queue. A new entry is
  created in the queue only if the node is not already
  enqueued. Returns a pointer to the queue item if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [cuddLevelQueueInit cuddLevelQueueDequeue]

******************************************************************************/
void *
cuddLevelQueueEnqueue(
  DdLevelQueue * queue /* level queue */,
  void * key /* key to be enqueued */,
  int  level /* level at which to insert */)
{
    int plevel;
    DdQueueItem *item;

#ifdef DD_DEBUG
    assert(level < queue->levels);
#endif
    /* Check whether entry for this node exists. */
    item = hashLookup(queue,key);
    if (item != NULL) return(item);

297
    /* Get a free item from either the free list or the memory manager. */
Alan Mishchenko committed
298
    if (queue->freelist == NULL) {
299 300 301
        item = (DdQueueItem *) ABC_ALLOC(char, queue->itemsize);
        if (item == NULL)
            return(NULL);
Alan Mishchenko committed
302
    } else {
303 304
        item = queue->freelist;
        queue->freelist = item->next;
Alan Mishchenko committed
305 306 307 308 309 310 311 312
    }
    /* Initialize. */
    memset(item, 0, queue->itemsize);
    item->key = key;
    /* Update stats. */
    queue->size++;

    if (queue->last[level]) {
313 314 315
        /* There are already items for this level in the queue. */
        item->next = queue->last[level]->next;
        queue->last[level]->next = item;
Alan Mishchenko committed
316
    } else {
317 318 319 320 321 322 323 324 325 326 327 328 329
        /* There are no items at the current level.  Look for the first
        ** non-empty level preceeding this one. */
        plevel = level;
        while (plevel != 0 && queue->last[plevel] == NULL)
            plevel--;
        if (queue->last[plevel] == NULL) {
            /* No element precedes this one in the queue. */
            item->next = (DdQueueItem *) queue->first;
            queue->first = item;
        } else {
            item->next = queue->last[plevel]->next;
            queue->last[plevel]->next = item;
        }
Alan Mishchenko committed
330 331 332 333 334
    }
    queue->last[level] = item;

    /* Insert entry for the key in the hash table. */
    if (hashInsert(queue,item) == 0) {
335
        return(NULL);
Alan Mishchenko committed
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
    }
    return(item);

} /* end of cuddLevelQueueEnqueue */


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

  Synopsis    [Remove an item from the front of a level queue.]

  Description [Remove an item from the front of a level queue.]

  SideEffects [None]

  SeeAlso     [cuddLevelQueueEnqueue]

******************************************************************************/
void
cuddLevelQueueDequeue(
  DdLevelQueue * queue,
  int  level)
{
    DdQueueItem *item = (DdQueueItem *) queue->first;

    /* Delete from the hash table. */
    hashDelete(queue,item);

    /* Since we delete from the front, if this is the last item for
    ** its level, there are no other items for the same level. */
    if (queue->last[level] == item)
366
        queue->last[level] = NULL;
Alan Mishchenko committed
367 368

    queue->first = item->next;
369
    /* Put item on the free list. */
Alan Mishchenko committed
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
    item->next = queue->freelist;
    queue->freelist = item;
    /* Update stats. */
    queue->size--;
    return;

} /* end of cuddLevelQueueDequeue */


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


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

  Synopsis    [Looks up a key in the hash table of a level queue.]

  Description [Looks up a key in the hash table of a level queue. Returns
  a pointer to the item with the given key if the key is found; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [cuddLevelQueueEnqueue hashInsert]

******************************************************************************/
static DdQueueItem *
hashLookup(
  DdLevelQueue * queue,
  void * key)
{
    int posn;
    DdQueueItem *item;

    posn = lqHash(key,queue->shift);
    item = queue->buckets[posn];

    while (item != NULL) {
409 410 411 412
        if (item->key == key) {
            return(item);
        }
        item = item->cnext;
Alan Mishchenko committed
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
    }
    return(NULL);

} /* end of hashLookup */


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

  Synopsis    [Inserts an item in the hash table of a level queue.]

  Description [Inserts an item in the hash table of a level queue. Returns
  1 if successful; 0 otherwise. No check is performed to see if an item with
  the same key is already in the hash table.]

  SideEffects [None]

  SeeAlso     [cuddLevelQueueEnqueue]

******************************************************************************/
static int
hashInsert(
  DdLevelQueue * queue,
  DdQueueItem * item)
{
    int result;
    int posn;

    if (queue->size > queue->maxsize) {
441 442
        result = hashResize(queue);
        if (result == 0) return(0);
Alan Mishchenko committed
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
    }

    posn = lqHash(item->key,queue->shift);
    item->cnext = queue->buckets[posn];
    queue->buckets[posn] = item;

    return(1);
    
} /* end of hashInsert */


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

  Synopsis    [Removes an item from the hash table of a level queue.]

  Description [Removes an item from the hash table of a level queue.
  Nothing is done if the item is not in the table.]

  SideEffects [None]

  SeeAlso     [cuddLevelQueueDequeue hashInsert]

******************************************************************************/
static void
hashDelete(
  DdLevelQueue * queue,
  DdQueueItem * item)
{
    int posn;
    DdQueueItem *prevItem;

    posn = lqHash(item->key,queue->shift);
    prevItem = queue->buckets[posn];

    if (prevItem == NULL) return;
    if (prevItem == item) {
479 480
        queue->buckets[posn] = prevItem->cnext;
        return;
Alan Mishchenko committed
481 482 483
    }

    while (prevItem->cnext != NULL) {
484 485 486 487 488
        if (prevItem->cnext == item) {
            prevItem->cnext = item->cnext;
            return;
        }
        prevItem = prevItem->cnext;
Alan Mishchenko committed
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
    }
    return;

} /* end of hashDelete */


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

  Synopsis    [Resizes the hash table of a level queue.]

  Description [Resizes the hash table of a level queue. Returns 1 if
  successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [hashInsert]

******************************************************************************/
static int
hashResize(
  DdLevelQueue * queue)
{
    int j;
    int posn;
    DdQueueItem *item;
    DdQueueItem *next;
    int numBuckets;
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
    DdQueueItem **buckets;
    DdQueueItem **oldBuckets = queue->buckets;
#ifdef __osf__
#pragma pointer_size restore
#endif
    int shift;
    int oldNumBuckets = queue->numBuckets;
527 528
    extern DD_OOMFP MMoutOfMemory;
    DD_OOMFP saveHandler;
Alan Mishchenko committed
529 530 531 532 533 534 535 536 537

    /* Compute the new size of the subtable. */
    numBuckets = oldNumBuckets << 1;
    saveHandler = MMoutOfMemory;
    MMoutOfMemory = Cudd_OutOfMem;
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
Alan Mishchenko committed
538
    buckets = queue->buckets = ABC_ALLOC(DdQueueItem *, numBuckets);
539
    MMoutOfMemory = saveHandler;
Alan Mishchenko committed
540
    if (buckets == NULL) {
541 542
        queue->maxsize <<= 1;
        return(1);
Alan Mishchenko committed
543 544 545 546 547 548 549 550 551 552
    }

    queue->numBuckets = numBuckets;
    shift = --(queue->shift);
    queue->maxsize <<= 1;
    memset(buckets, 0, numBuckets * sizeof(DdQueueItem *));
#ifdef __osf__
#pragma pointer_size restore
#endif
    for (j = 0; j < oldNumBuckets; j++) {
553 554 555 556 557 558 559 560
        item = oldBuckets[j];
        while (item != NULL) {
            next = item->cnext;
            posn = lqHash(item->key, shift);
            item->cnext = buckets[posn];
            buckets[posn] = item;
            item = next;
        }
Alan Mishchenko committed
561
    }
Alan Mishchenko committed
562
    ABC_FREE(oldBuckets);
Alan Mishchenko committed
563 564 565
    return(1);

} /* end of hashResize */
566 567


568 569
ABC_NAMESPACE_IMPL_END