You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

583 lines
17 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddLevelQ.c]
  3. PackageName [cudd]
  4. Synopsis [Procedure to manage level queues.]
  5. Description [The functions in this file allow an application to
  6. easily manipulate a queue where nodes are prioritized by level. The
  7. emphasis is on efficiency. Therefore, the queue items can have
  8. variable size. If the application does not need to attach
  9. information to the nodes, it can declare the queue items to be of
  10. type DdQueueItem. Otherwise, it can declare them to be of a
  11. structure type such that the first three fields are data
  12. pointers. The third pointer points to the node. The first two
  13. pointers are used by the level queue functions. The remaining fields
  14. are initialized to 0 when a new item is created, and are then left
  15. to the exclusive use of the application. On the DEC Alphas the three
  16. pointers must be 32-bit pointers when CUDD is compiled with 32-bit
  17. pointers. The level queue functions make sure that each node
  18. appears at most once in the queue. They do so by keeping a hash
  19. table where the node is used as key. Queue items are recycled via a
  20. free list for efficiency.
  21. Internal procedures provided by this module:
  22. <ul>
  23. <li> cuddLevelQueueInit()
  24. <li> cuddLevelQueueQuit()
  25. <li> cuddLevelQueueEnqueue()
  26. <li> cuddLevelQueueDequeue()
  27. </ul>
  28. Static procedures included in this module:
  29. <ul>
  30. <li> hashLookup()
  31. <li> hashInsert()
  32. <li> hashDelete()
  33. <li> hashResize()
  34. </ul>
  35. ]
  36. SeeAlso []
  37. Author [Fabio Somenzi]
  38. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  39. All rights reserved.
  40. Redistribution and use in source and binary forms, with or without
  41. modification, are permitted provided that the following conditions
  42. are met:
  43. Redistributions of source code must retain the above copyright
  44. notice, this list of conditions and the following disclaimer.
  45. Redistributions in binary form must reproduce the above copyright
  46. notice, this list of conditions and the following disclaimer in the
  47. documentation and/or other materials provided with the distribution.
  48. Neither the name of the University of Colorado nor the names of its
  49. contributors may be used to endorse or promote products derived from
  50. this software without specific prior written permission.
  51. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  54. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  55. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  56. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  57. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  58. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  59. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  60. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  61. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  62. POSSIBILITY OF SUCH DAMAGE.]
  63. ******************************************************************************/
  64. #include "util.h"
  65. #include "cuddInt.h"
  66. /*---------------------------------------------------------------------------*/
  67. /* Constant declarations */
  68. /*---------------------------------------------------------------------------*/
  69. /*---------------------------------------------------------------------------*/
  70. /* Stucture declarations */
  71. /*---------------------------------------------------------------------------*/
  72. /*---------------------------------------------------------------------------*/
  73. /* Type declarations */
  74. /*---------------------------------------------------------------------------*/
  75. /*---------------------------------------------------------------------------*/
  76. /* Variable declarations */
  77. /*---------------------------------------------------------------------------*/
  78. #ifndef lint
  79. static char rcsid[] DD_UNUSED = "$Id: cuddLevelQ.c,v 1.16 2012/02/05 01:07:19 fabio Exp $";
  80. #endif
  81. /*---------------------------------------------------------------------------*/
  82. /* Macro declarations */
  83. /*---------------------------------------------------------------------------*/
  84. /**Macro***********************************************************************
  85. Synopsis [Hash function for the table of a level queue.]
  86. Description [Hash function for the table of a level queue.]
  87. SideEffects [None]
  88. SeeAlso [hashInsert hashLookup hashDelete]
  89. ******************************************************************************/
  90. #if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
  91. #define lqHash(key,shift) \
  92. (((unsigned)(ptruint)(key) * DD_P1) >> (shift))
  93. #else
  94. #define lqHash(key,shift) \
  95. (((unsigned)(key) * DD_P1) >> (shift))
  96. #endif
  97. /**AutomaticStart*************************************************************/
  98. /*---------------------------------------------------------------------------*/
  99. /* Static function prototypes */
  100. /*---------------------------------------------------------------------------*/
  101. static DdQueueItem * hashLookup(DdLevelQueue *queue, void *key);
  102. static int hashInsert(DdLevelQueue *queue, DdQueueItem *item);
  103. static void hashDelete(DdLevelQueue *queue, DdQueueItem *item);
  104. static int hashResize(DdLevelQueue *queue);
  105. /**AutomaticEnd***************************************************************/
  106. /*---------------------------------------------------------------------------*/
  107. /* Definition of internal functions */
  108. /*---------------------------------------------------------------------------*/
  109. /**Function********************************************************************
  110. Synopsis [Initializes a level queue.]
  111. Description [Initializes a level queue. A level queue is a queue
  112. where inserts are based on the levels of the nodes. Within each
  113. level the policy is FIFO. Level queues are useful in traversing a
  114. BDD top-down. Queue items are kept in a free list when dequeued for
  115. efficiency. Returns a pointer to the new queue if successful; NULL
  116. otherwise.]
  117. SideEffects [None]
  118. SeeAlso [cuddLevelQueueQuit cuddLevelQueueEnqueue cuddLevelQueueDequeue]
  119. ******************************************************************************/
  120. DdLevelQueue *
  121. cuddLevelQueueInit(
  122. int levels /* number of levels */,
  123. int itemSize /* size of the item */,
  124. int numBuckets /* initial number of hash buckets */)
  125. {
  126. DdLevelQueue *queue;
  127. int logSize;
  128. queue = ALLOC(DdLevelQueue,1);
  129. if (queue == NULL)
  130. return(NULL);
  131. /* Keep pointers to the insertion points for all levels. */
  132. queue->last = ALLOC(DdQueueItem *, levels);
  133. if (queue->last == NULL) {
  134. FREE(queue);
  135. return(NULL);
  136. }
  137. /* Use a hash table to test for uniqueness. */
  138. if (numBuckets < 2) numBuckets = 2;
  139. logSize = cuddComputeFloorLog2(numBuckets);
  140. queue->numBuckets = 1 << logSize;
  141. queue->shift = sizeof(int) * 8 - logSize;
  142. queue->buckets = ALLOC(DdQueueItem *, queue->numBuckets);
  143. if (queue->buckets == NULL) {
  144. FREE(queue->last);
  145. FREE(queue);
  146. return(NULL);
  147. }
  148. memset(queue->last, 0, levels * sizeof(DdQueueItem *));
  149. memset(queue->buckets, 0, queue->numBuckets * sizeof(DdQueueItem *));
  150. queue->first = NULL;
  151. queue->freelist = NULL;
  152. queue->levels = levels;
  153. queue->itemsize = itemSize;
  154. queue->size = 0;
  155. queue->maxsize = queue->numBuckets * DD_MAX_SUBTABLE_DENSITY;
  156. return(queue);
  157. } /* end of cuddLevelQueueInit */
  158. /**Function********************************************************************
  159. Synopsis [Shuts down a level queue.]
  160. Description [Shuts down a level queue and releases all the
  161. associated memory.]
  162. SideEffects [None]
  163. SeeAlso [cuddLevelQueueInit]
  164. ******************************************************************************/
  165. void
  166. cuddLevelQueueQuit(
  167. DdLevelQueue * queue)
  168. {
  169. DdQueueItem *item;
  170. while (queue->freelist != NULL) {
  171. item = queue->freelist;
  172. queue->freelist = item->next;
  173. FREE(item);
  174. }
  175. while (queue->first != NULL) {
  176. item = (DdQueueItem *) queue->first;
  177. queue->first = item->next;
  178. FREE(item);
  179. }
  180. FREE(queue->buckets);
  181. FREE(queue->last);
  182. FREE(queue);
  183. return;
  184. } /* end of cuddLevelQueueQuit */
  185. /**Function********************************************************************
  186. Synopsis [Inserts a new key in a level queue.]
  187. Description [Inserts a new key in a level queue. A new entry is
  188. created in the queue only if the node is not already
  189. enqueued. Returns a pointer to the queue item if successful; NULL
  190. otherwise.]
  191. SideEffects [None]
  192. SeeAlso [cuddLevelQueueInit cuddLevelQueueDequeue]
  193. ******************************************************************************/
  194. void *
  195. cuddLevelQueueEnqueue(
  196. DdLevelQueue * queue /* level queue */,
  197. void * key /* key to be enqueued */,
  198. int level /* level at which to insert */)
  199. {
  200. DdQueueItem *item;
  201. #ifdef DD_DEBUG
  202. assert(level < queue->levels);
  203. #endif
  204. /* Check whether entry for this node exists. */
  205. item = hashLookup(queue,key);
  206. if (item != NULL) return(item);
  207. /* Get a free item from either the free list or the memory manager. */
  208. if (queue->freelist == NULL) {
  209. item = (DdQueueItem *) ALLOC(char, queue->itemsize);
  210. if (item == NULL)
  211. return(NULL);
  212. } else {
  213. item = queue->freelist;
  214. queue->freelist = item->next;
  215. }
  216. /* Initialize. */
  217. memset(item, 0, queue->itemsize);
  218. item->key = key;
  219. /* Update stats. */
  220. queue->size++;
  221. if (queue->last[level]) {
  222. /* There are already items for this level in the queue. */
  223. item->next = queue->last[level]->next;
  224. queue->last[level]->next = item;
  225. } else {
  226. /* There are no items at the current level. Look for the first
  227. ** non-empty level preceeding this one. */
  228. int plevel = level;
  229. while (plevel != 0 && queue->last[plevel] == NULL)
  230. plevel--;
  231. if (queue->last[plevel] == NULL) {
  232. /* No element precedes this one in the queue. */
  233. item->next = (DdQueueItem *) queue->first;
  234. queue->first = item;
  235. } else {
  236. item->next = queue->last[plevel]->next;
  237. queue->last[plevel]->next = item;
  238. }
  239. }
  240. queue->last[level] = item;
  241. /* Insert entry for the key in the hash table. */
  242. if (hashInsert(queue,item) == 0) {
  243. return(NULL);
  244. }
  245. return(item);
  246. } /* end of cuddLevelQueueEnqueue */
  247. /**Function********************************************************************
  248. Synopsis [Inserts the first key in a level queue.]
  249. Description [Inserts the first key in a level queue. Returns a
  250. pointer to the queue item if successful; NULL otherwise.]
  251. SideEffects [None]
  252. SeeAlso [cuddLevelQueueEnqueue]
  253. ******************************************************************************/
  254. void *
  255. cuddLevelQueueFirst(
  256. DdLevelQueue * queue /* level queue */,
  257. void * key /* key to be enqueued */,
  258. int level /* level at which to insert */)
  259. {
  260. DdQueueItem *item;
  261. #ifdef DD_DEBUG
  262. assert(level < queue->levels);
  263. /* Check whether entry for this node exists. */
  264. item = hashLookup(queue,key);
  265. assert(item == NULL);
  266. #endif
  267. /* Get a free item from either the free list or the memory manager. */
  268. if (queue->freelist == NULL) {
  269. item = (DdQueueItem *) ALLOC(char, queue->itemsize);
  270. if (item == NULL)
  271. return(NULL);
  272. } else {
  273. item = queue->freelist;
  274. queue->freelist = item->next;
  275. }
  276. /* Initialize. */
  277. memset(item, 0, queue->itemsize);
  278. item->key = key;
  279. /* Update stats. */
  280. queue->size = 1;
  281. /* No element precedes this one in the queue. */
  282. queue->first = item;
  283. queue->last[level] = item;
  284. /* Insert entry for the key in the hash table. */
  285. if (hashInsert(queue,item) == 0) {
  286. return(NULL);
  287. }
  288. return(item);
  289. } /* end of cuddLevelQueueFirst */
  290. /**Function********************************************************************
  291. Synopsis [Remove an item from the front of a level queue.]
  292. Description [Remove an item from the front of a level queue.]
  293. SideEffects [None]
  294. SeeAlso [cuddLevelQueueEnqueue]
  295. ******************************************************************************/
  296. void
  297. cuddLevelQueueDequeue(
  298. DdLevelQueue * queue,
  299. int level)
  300. {
  301. DdQueueItem *item = (DdQueueItem *) queue->first;
  302. /* Delete from the hash table. */
  303. hashDelete(queue,item);
  304. /* Since we delete from the front, if this is the last item for
  305. ** its level, there are no other items for the same level. */
  306. if (queue->last[level] == item) {
  307. queue->last[level] = NULL;
  308. }
  309. queue->first = item->next;
  310. /* Put item on the free list. */
  311. item->next = queue->freelist;
  312. queue->freelist = item;
  313. /* Update stats. */
  314. queue->size--;
  315. return;
  316. } /* end of cuddLevelQueueDequeue */
  317. /*---------------------------------------------------------------------------*/
  318. /* Definition of static functions */
  319. /*---------------------------------------------------------------------------*/
  320. /**Function********************************************************************
  321. Synopsis [Looks up a key in the hash table of a level queue.]
  322. Description [Looks up a key in the hash table of a level queue. Returns
  323. a pointer to the item with the given key if the key is found; NULL
  324. otherwise.]
  325. SideEffects [None]
  326. SeeAlso [cuddLevelQueueEnqueue hashInsert]
  327. ******************************************************************************/
  328. static DdQueueItem *
  329. hashLookup(
  330. DdLevelQueue * queue,
  331. void * key)
  332. {
  333. int posn;
  334. DdQueueItem *item;
  335. posn = lqHash(key,queue->shift);
  336. item = queue->buckets[posn];
  337. while (item != NULL) {
  338. if (item->key == key) {
  339. return(item);
  340. }
  341. item = item->cnext;
  342. }
  343. return(NULL);
  344. } /* end of hashLookup */
  345. /**Function********************************************************************
  346. Synopsis [Inserts an item in the hash table of a level queue.]
  347. Description [Inserts an item in the hash table of a level queue. Returns
  348. 1 if successful; 0 otherwise. No check is performed to see if an item with
  349. the same key is already in the hash table.]
  350. SideEffects [None]
  351. SeeAlso [cuddLevelQueueEnqueue]
  352. ******************************************************************************/
  353. static int
  354. hashInsert(
  355. DdLevelQueue * queue,
  356. DdQueueItem * item)
  357. {
  358. int result;
  359. int posn;
  360. if (queue->size > queue->maxsize) {
  361. result = hashResize(queue);
  362. if (result == 0) return(0);
  363. }
  364. posn = lqHash(item->key,queue->shift);
  365. item->cnext = queue->buckets[posn];
  366. queue->buckets[posn] = item;
  367. return(1);
  368. } /* end of hashInsert */
  369. /**Function********************************************************************
  370. Synopsis [Removes an item from the hash table of a level queue.]
  371. Description [Removes an item from the hash table of a level queue.
  372. Nothing is done if the item is not in the table.]
  373. SideEffects [None]
  374. SeeAlso [cuddLevelQueueDequeue hashInsert]
  375. ******************************************************************************/
  376. static void
  377. hashDelete(
  378. DdLevelQueue * queue,
  379. DdQueueItem * item)
  380. {
  381. int posn;
  382. DdQueueItem *prevItem;
  383. posn = lqHash(item->key,queue->shift);
  384. prevItem = queue->buckets[posn];
  385. if (prevItem == NULL) return;
  386. if (prevItem == item) {
  387. queue->buckets[posn] = prevItem->cnext;
  388. return;
  389. }
  390. while (prevItem->cnext != NULL) {
  391. if (prevItem->cnext == item) {
  392. prevItem->cnext = item->cnext;
  393. return;
  394. }
  395. prevItem = prevItem->cnext;
  396. }
  397. return;
  398. } /* end of hashDelete */
  399. /**Function********************************************************************
  400. Synopsis [Resizes the hash table of a level queue.]
  401. Description [Resizes the hash table of a level queue. Returns 1 if
  402. successful; 0 otherwise.]
  403. SideEffects [None]
  404. SeeAlso [hashInsert]
  405. ******************************************************************************/
  406. static int
  407. hashResize(
  408. DdLevelQueue * queue)
  409. {
  410. int j;
  411. int posn;
  412. DdQueueItem *item;
  413. DdQueueItem *next;
  414. int numBuckets;
  415. DdQueueItem **buckets;
  416. DdQueueItem **oldBuckets = queue->buckets;
  417. int shift;
  418. int oldNumBuckets = queue->numBuckets;
  419. extern DD_OOMFP MMoutOfMemory;
  420. DD_OOMFP saveHandler;
  421. /* Compute the new size of the subtable. */
  422. numBuckets = oldNumBuckets << 1;
  423. saveHandler = MMoutOfMemory;
  424. MMoutOfMemory = Cudd_OutOfMem;
  425. buckets = queue->buckets = ALLOC(DdQueueItem *, numBuckets);
  426. MMoutOfMemory = saveHandler;
  427. if (buckets == NULL) {
  428. queue->maxsize <<= 1;
  429. return(1);
  430. }
  431. queue->numBuckets = numBuckets;
  432. shift = --(queue->shift);
  433. queue->maxsize <<= 1;
  434. memset(buckets, 0, numBuckets * sizeof(DdQueueItem *));
  435. for (j = 0; j < oldNumBuckets; j++) {
  436. item = oldBuckets[j];
  437. while (item != NULL) {
  438. next = item->cnext;
  439. posn = lqHash(item->key, shift);
  440. item->cnext = buckets[posn];
  441. buckets[posn] = item;
  442. item = next;
  443. }
  444. }
  445. FREE(oldBuckets);
  446. return(1);
  447. } /* end of hashResize */