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.

357 lines
10 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddZddCount.c]
  3. PackageName [cudd]
  4. Synopsis [Procedures to count the number of minterms of a ZDD.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_zddCount();
  8. <li> Cudd_zddCountDouble();
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. </ul>
  13. Static procedures included in this module:
  14. <ul>
  15. <li> cuddZddCountStep();
  16. <li> cuddZddCountDoubleStep();
  17. <li> st_zdd_count_dbl_free()
  18. <li> st_zdd_countfree()
  19. </ul>
  20. ]
  21. SeeAlso []
  22. Author [Hyong-Kyoon Shin, In-Ho Moon]
  23. Copyright [Copyright (c) 1995-2012, 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. ******************************************************************************/
  49. #include "util.h"
  50. #include "cuddInt.h"
  51. /*---------------------------------------------------------------------------*/
  52. /* Constant declarations */
  53. /*---------------------------------------------------------------------------*/
  54. /*---------------------------------------------------------------------------*/
  55. /* Stucture declarations */
  56. /*---------------------------------------------------------------------------*/
  57. /*---------------------------------------------------------------------------*/
  58. /* Type declarations */
  59. /*---------------------------------------------------------------------------*/
  60. /*---------------------------------------------------------------------------*/
  61. /* Variable declarations */
  62. /*---------------------------------------------------------------------------*/
  63. #ifndef lint
  64. static char rcsid[] DD_UNUSED = "$Id: cuddZddCount.c,v 1.15 2012/02/05 01:07:19 fabio Exp $";
  65. #endif
  66. /*---------------------------------------------------------------------------*/
  67. /* Macro declarations */
  68. /*---------------------------------------------------------------------------*/
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. /**AutomaticStart*************************************************************/
  73. /*---------------------------------------------------------------------------*/
  74. /* Static function prototypes */
  75. /*---------------------------------------------------------------------------*/
  76. static int cuddZddCountStep (DdNode *P, st_table *table, DdNode *base, DdNode *empty);
  77. static double cuddZddCountDoubleStep (DdNode *P, st_table *table, DdNode *base, DdNode *empty);
  78. static enum st_retval st_zdd_countfree (char *key, char *value, char *arg);
  79. static enum st_retval st_zdd_count_dbl_free (char *key, char *value, char *arg);
  80. /**AutomaticEnd***************************************************************/
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. /*---------------------------------------------------------------------------*/
  85. /* Definition of exported functions */
  86. /*---------------------------------------------------------------------------*/
  87. /**Function********************************************************************
  88. Synopsis [Counts the number of minterms in a ZDD.]
  89. Description [Returns an integer representing the number of minterms
  90. in a ZDD.]
  91. SideEffects [None]
  92. SeeAlso [Cudd_zddCountDouble]
  93. ******************************************************************************/
  94. int
  95. Cudd_zddCount(
  96. DdManager * zdd,
  97. DdNode * P)
  98. {
  99. st_table *table;
  100. int res;
  101. DdNode *base, *empty;
  102. base = DD_ONE(zdd);
  103. empty = DD_ZERO(zdd);
  104. table = st_init_table(st_ptrcmp, st_ptrhash);
  105. if (table == NULL) return(CUDD_OUT_OF_MEM);
  106. res = cuddZddCountStep(P, table, base, empty);
  107. if (res == CUDD_OUT_OF_MEM) {
  108. zdd->errorCode = CUDD_MEMORY_OUT;
  109. }
  110. st_foreach(table, st_zdd_countfree, NIL(char));
  111. st_free_table(table);
  112. return(res);
  113. } /* end of Cudd_zddCount */
  114. /**Function********************************************************************
  115. Synopsis [Counts the number of minterms of a ZDD.]
  116. Description [Counts the number of minterms of a ZDD. The result is
  117. returned as a double. If the procedure runs out of memory, it
  118. returns (double) CUDD_OUT_OF_MEM. This procedure is used in
  119. Cudd_zddCountMinterm.]
  120. SideEffects [None]
  121. SeeAlso [Cudd_zddCountMinterm Cudd_zddCount]
  122. ******************************************************************************/
  123. double
  124. Cudd_zddCountDouble(
  125. DdManager * zdd,
  126. DdNode * P)
  127. {
  128. st_table *table;
  129. double res;
  130. DdNode *base, *empty;
  131. base = DD_ONE(zdd);
  132. empty = DD_ZERO(zdd);
  133. table = st_init_table(st_ptrcmp, st_ptrhash);
  134. if (table == NULL) return((double)CUDD_OUT_OF_MEM);
  135. res = cuddZddCountDoubleStep(P, table, base, empty);
  136. if (res == (double)CUDD_OUT_OF_MEM) {
  137. zdd->errorCode = CUDD_MEMORY_OUT;
  138. }
  139. st_foreach(table, st_zdd_count_dbl_free, NIL(char));
  140. st_free_table(table);
  141. return(res);
  142. } /* end of Cudd_zddCountDouble */
  143. /*---------------------------------------------------------------------------*/
  144. /* Definition of internal functions */
  145. /*---------------------------------------------------------------------------*/
  146. /*---------------------------------------------------------------------------*/
  147. /* Definition of static functions */
  148. /*---------------------------------------------------------------------------*/
  149. /**Function********************************************************************
  150. Synopsis [Performs the recursive step of Cudd_zddCount.]
  151. Description []
  152. SideEffects [None]
  153. SeeAlso []
  154. ******************************************************************************/
  155. static int
  156. cuddZddCountStep(
  157. DdNode * P,
  158. st_table * table,
  159. DdNode * base,
  160. DdNode * empty)
  161. {
  162. int res;
  163. int *dummy;
  164. if (P == empty)
  165. return(0);
  166. if (P == base)
  167. return(1);
  168. /* Check cache. */
  169. if (st_lookup(table, P, &dummy)) {
  170. res = *dummy;
  171. return(res);
  172. }
  173. res = cuddZddCountStep(cuddE(P), table, base, empty) +
  174. cuddZddCountStep(cuddT(P), table, base, empty);
  175. dummy = ALLOC(int, 1);
  176. if (dummy == NULL) {
  177. return(CUDD_OUT_OF_MEM);
  178. }
  179. *dummy = res;
  180. if (st_insert(table, (char *)P, (char *)dummy) == ST_OUT_OF_MEM) {
  181. FREE(dummy);
  182. return(CUDD_OUT_OF_MEM);
  183. }
  184. return(res);
  185. } /* end of cuddZddCountStep */
  186. /**Function********************************************************************
  187. Synopsis [Performs the recursive step of Cudd_zddCountDouble.]
  188. Description []
  189. SideEffects [None]
  190. SeeAlso []
  191. ******************************************************************************/
  192. static double
  193. cuddZddCountDoubleStep(
  194. DdNode * P,
  195. st_table * table,
  196. DdNode * base,
  197. DdNode * empty)
  198. {
  199. double res;
  200. double *dummy;
  201. if (P == empty)
  202. return((double)0.0);
  203. if (P == base)
  204. return((double)1.0);
  205. /* Check cache */
  206. if (st_lookup(table, P, &dummy)) {
  207. res = *dummy;
  208. return(res);
  209. }
  210. res = cuddZddCountDoubleStep(cuddE(P), table, base, empty) +
  211. cuddZddCountDoubleStep(cuddT(P), table, base, empty);
  212. dummy = ALLOC(double, 1);
  213. if (dummy == NULL) {
  214. return((double)CUDD_OUT_OF_MEM);
  215. }
  216. *dummy = res;
  217. if (st_insert(table, (char *)P, (char *)dummy) == ST_OUT_OF_MEM) {
  218. FREE(dummy);
  219. return((double)CUDD_OUT_OF_MEM);
  220. }
  221. return(res);
  222. } /* end of cuddZddCountDoubleStep */
  223. /**Function********************************************************************
  224. Synopsis [Frees the memory associated with the computed table of
  225. Cudd_zddCount.]
  226. Description []
  227. SideEffects [None]
  228. SeeAlso []
  229. ******************************************************************************/
  230. static enum st_retval
  231. st_zdd_countfree(
  232. char * key,
  233. char * value,
  234. char * arg)
  235. {
  236. int *d;
  237. d = (int *)value;
  238. FREE(d);
  239. return(ST_CONTINUE);
  240. } /* end of st_zdd_countfree */
  241. /**Function********************************************************************
  242. Synopsis [Frees the memory associated with the computed table of
  243. Cudd_zddCountDouble.]
  244. Description []
  245. SideEffects [None]
  246. SeeAlso []
  247. ******************************************************************************/
  248. static enum st_retval
  249. st_zdd_count_dbl_free(
  250. char * key,
  251. char * value,
  252. char * arg)
  253. {
  254. double *d;
  255. d = (double *)value;
  256. FREE(d);
  257. return(ST_CONTINUE);
  258. } /* end of st_zdd_count_dbl_free */