📄 cuddlevelq.c
字号:
/**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 free list for efficiency. Internal procedures provided by this module: <ul> <li> cuddLevelQueueInit() <li> cuddLevelQueueQuit() <li> cuddLevelQueueEnqueue() <li> cuddLevelQueueDequeue() </ul> Static procedures included in this module: <ul> <li> hashLookup() <li> hashInsert() <li> hashDelete() <li> hashResize() </ul> ] SeeAlso [] 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.]******************************************************************************/#include "util.h"#include "cuddInt.h"/*---------------------------------------------------------------------------*//* Constant declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Stucture declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Type declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Variable declarations *//*---------------------------------------------------------------------------*/#ifndef lintstatic char rcsid[] DD_UNUSED = "$Id: cuddLevelQ.c,v 1.1.1.1 2003/02/24 22:23:52 wjiang Exp $";#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) \(((unsigned)(unsigned long)(key) * DD_P1) >> (shift))#else#define lqHash(key,shift) \(((unsigned)(key) * DD_P1) >> (shift))#endif/**AutomaticStart*************************************************************//*---------------------------------------------------------------------------*//* Static function prototypes *//*---------------------------------------------------------------------------*/static DdQueueItem * hashLookup ARGS((DdLevelQueue *queue, void *key));static int hashInsert ARGS((DdLevelQueue *queue, DdQueueItem *item));static void hashDelete ARGS((DdLevelQueue *queue, DdQueueItem *item));static int hashResize ARGS((DdLevelQueue *queue));/**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 BDD top-down. Queue items are kept in a free list when dequeued for 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; queue = ALLOC(DdLevelQueue,1); if (queue == NULL) return(NULL);#ifdef __osf__#pragma pointer_size save#pragma pointer_size short#endif /* Keep pointers to the insertion points for all levels. */ queue->last = ALLOC(DdQueueItem *, levels);#ifdef __osf__#pragma pointer_size restore#endif if (queue->last == NULL) { FREE(queue); return(NULL); } /* 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 queue->buckets = ALLOC(DdQueueItem *, queue->numBuckets);#ifdef __osf__#pragma pointer_size restore#endif if (queue->buckets == NULL) { FREE(queue->last); FREE(queue); return(NULL); }#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]******************************************************************************/voidcuddLevelQueueQuit( DdLevelQueue * queue){ DdQueueItem *item; while (queue->freelist != NULL) { item = queue->freelist; queue->freelist = item->next; FREE(item); } while (queue->first != NULL) { item = (DdQueueItem *) queue->first; queue->first = item->next; FREE(item); } FREE(queue->buckets); FREE(queue->last); FREE(queue); 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); /* Get a free item from either the free list or the memory manager. */ if (queue->freelist == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -