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.

308 lines
8.0 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Procedures to count the number of minterms of a %ZDD.
  5. @author Hyong-Kyoon Shin, In-Ho Moon
  6. @copyright@parblock
  7. Copyright (c) 1995-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include "util.h"
  35. #include "cuddInt.h"
  36. /*---------------------------------------------------------------------------*/
  37. /* Constant declarations */
  38. /*---------------------------------------------------------------------------*/
  39. /*---------------------------------------------------------------------------*/
  40. /* Stucture declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Type declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. static int cuddZddCountStep (DdNode *P, st_table *table, DdNode *base, DdNode *empty);
  56. static double cuddZddCountDoubleStep (DdNode *P, st_table *table, DdNode *base, DdNode *empty);
  57. static enum st_retval st_zdd_countfree (void *key, void *value, void *arg);
  58. static enum st_retval st_zdd_count_dbl_free (void *key, void *value, void *arg);
  59. /** \endcond */
  60. /*---------------------------------------------------------------------------*/
  61. /* Definition of exported functions */
  62. /*---------------------------------------------------------------------------*/
  63. /**
  64. @brief Counts the number of minterms in a %ZDD.
  65. @details Returns an integer representing the number of minterms
  66. in a %ZDD.
  67. @sideeffect None
  68. @see Cudd_zddCountDouble
  69. */
  70. int
  71. Cudd_zddCount(
  72. DdManager * zdd,
  73. DdNode * P)
  74. {
  75. st_table *table;
  76. int res;
  77. DdNode *base, *empty;
  78. base = DD_ONE(zdd);
  79. empty = DD_ZERO(zdd);
  80. table = st_init_table(st_ptrcmp, st_ptrhash);
  81. if (table == NULL) return(CUDD_OUT_OF_MEM);
  82. res = cuddZddCountStep(P, table, base, empty);
  83. if (res == CUDD_OUT_OF_MEM) {
  84. zdd->errorCode = CUDD_MEMORY_OUT;
  85. }
  86. st_foreach(table, st_zdd_countfree, NIL(void));
  87. st_free_table(table);
  88. return(res);
  89. } /* end of Cudd_zddCount */
  90. /**
  91. @brief Counts the number of minterms of a %ZDD.
  92. @details This procedure is used in Cudd_zddCountMinterm.
  93. @result the count. If the procedure runs out of memory, it returns
  94. (double) CUDD_OUT_OF_MEM.
  95. @sideeffect None
  96. @see Cudd_zddCountMinterm Cudd_zddCount
  97. */
  98. double
  99. Cudd_zddCountDouble(
  100. DdManager * zdd,
  101. DdNode * P)
  102. {
  103. st_table *table;
  104. double res;
  105. DdNode *base, *empty;
  106. base = DD_ONE(zdd);
  107. empty = DD_ZERO(zdd);
  108. table = st_init_table(st_ptrcmp, st_ptrhash);
  109. if (table == NULL) return((double)CUDD_OUT_OF_MEM);
  110. res = cuddZddCountDoubleStep(P, table, base, empty);
  111. if (res == (double)CUDD_OUT_OF_MEM) {
  112. zdd->errorCode = CUDD_MEMORY_OUT;
  113. }
  114. st_foreach(table, st_zdd_count_dbl_free, NIL(void));
  115. st_free_table(table);
  116. return(res);
  117. } /* end of Cudd_zddCountDouble */
  118. /*---------------------------------------------------------------------------*/
  119. /* Definition of internal functions */
  120. /*---------------------------------------------------------------------------*/
  121. /*---------------------------------------------------------------------------*/
  122. /* Definition of static functions */
  123. /*---------------------------------------------------------------------------*/
  124. /**
  125. @brief Performs the recursive step of Cudd_zddCount.
  126. @sideeffect None
  127. */
  128. static int
  129. cuddZddCountStep(
  130. DdNode * P,
  131. st_table * table,
  132. DdNode * base,
  133. DdNode * empty)
  134. {
  135. int res;
  136. int *dummy;
  137. if (P == empty)
  138. return(0);
  139. if (P == base)
  140. return(1);
  141. /* Check cache. */
  142. if (st_lookup(table, P, (void **) &dummy)) {
  143. res = *dummy;
  144. return(res);
  145. }
  146. res = cuddZddCountStep(cuddE(P), table, base, empty) +
  147. cuddZddCountStep(cuddT(P), table, base, empty);
  148. dummy = ALLOC(int, 1);
  149. if (dummy == NULL) {
  150. return(CUDD_OUT_OF_MEM);
  151. }
  152. *dummy = res;
  153. if (st_insert(table, P, dummy) == ST_OUT_OF_MEM) {
  154. FREE(dummy);
  155. return(CUDD_OUT_OF_MEM);
  156. }
  157. return(res);
  158. } /* end of cuddZddCountStep */
  159. /**
  160. @brief Performs the recursive step of Cudd_zddCountDouble.
  161. @sideeffect None
  162. */
  163. static double
  164. cuddZddCountDoubleStep(
  165. DdNode * P,
  166. st_table * table,
  167. DdNode * base,
  168. DdNode * empty)
  169. {
  170. double res;
  171. double *dummy;
  172. if (P == empty)
  173. return((double)0.0);
  174. if (P == base)
  175. return((double)1.0);
  176. /* Check cache */
  177. if (st_lookup(table, P, (void **) &dummy)) {
  178. res = *dummy;
  179. return(res);
  180. }
  181. res = cuddZddCountDoubleStep(cuddE(P), table, base, empty) +
  182. cuddZddCountDoubleStep(cuddT(P), table, base, empty);
  183. dummy = ALLOC(double, 1);
  184. if (dummy == NULL) {
  185. return((double)CUDD_OUT_OF_MEM);
  186. }
  187. *dummy = res;
  188. if (st_insert(table, P, dummy) == ST_OUT_OF_MEM) {
  189. FREE(dummy);
  190. return((double)CUDD_OUT_OF_MEM);
  191. }
  192. return(res);
  193. } /* end of cuddZddCountDoubleStep */
  194. /**
  195. @brief Frees the memory associated with the computed table of
  196. Cudd_zddCount.
  197. @sideeffect None
  198. */
  199. static enum st_retval
  200. st_zdd_countfree(
  201. void * key,
  202. void * value,
  203. void * arg)
  204. {
  205. int *d = (int *) value;
  206. (void) key; /* avoid warning */
  207. (void) arg; /* avoid warning */
  208. FREE(d);
  209. return(ST_CONTINUE);
  210. } /* end of st_zdd_countfree */
  211. /**
  212. @brief Frees the memory associated with the computed table of
  213. Cudd_zddCountDouble.
  214. @sideeffect None
  215. */
  216. static enum st_retval
  217. st_zdd_count_dbl_free(
  218. void * key,
  219. void * value,
  220. void * arg)
  221. {
  222. double *d = (double *) value;
  223. (void) key; /* avoid warning */
  224. (void) arg; /* avoid warning */
  225. FREE(d);
  226. return(ST_CONTINUE);
  227. } /* end of st_zdd_count_dbl_free */