The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

547 lines
14 KiB

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