abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cuddZddCount.c
Go to the documentation of this file.
1 /**CFile***********************************************************************
2 
3  FileName [cuddZddCount.c]
4 
5  PackageName [cudd]
6 
7  Synopsis [Procedures to count the number of minterms of a ZDD.]
8 
9  Description [External procedures included in this module:
10  <ul>
11  <li> Cudd_zddCount();
12  <li> Cudd_zddCountDouble();
13  </ul>
14  Internal procedures included in this module:
15  <ul>
16  </ul>
17  Static procedures included in this module:
18  <ul>
19  <li> cuddZddCountStep();
20  <li> cuddZddCountDoubleStep();
21  <li> st__zdd_count_dbl_free()
22  <li> st__zdd_countfree()
23  </ul>
24  ]
25 
26  SeeAlso []
27 
28  Author [Hyong-Kyoon Shin, In-Ho Moon]
29 
30  Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
31 
32  All rights reserved.
33 
34  Redistribution and use in source and binary forms, with or without
35  modification, are permitted provided that the following conditions
36  are met:
37 
38  Redistributions of source code must retain the above copyright
39  notice, this list of conditions and the following disclaimer.
40 
41  Redistributions in binary form must reproduce the above copyright
42  notice, this list of conditions and the following disclaimer in the
43  documentation and/or other materials provided with the distribution.
44 
45  Neither the name of the University of Colorado nor the names of its
46  contributors may be used to endorse or promote products derived from
47  this software without specific prior written permission.
48 
49  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
52  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
53  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
59  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  POSSIBILITY OF SUCH DAMAGE.]
61 
62 ******************************************************************************/
63 
64 #include "misc/util/util_hack.h"
65 #include "cuddInt.h"
66 
68 
69 
70 
71 /*---------------------------------------------------------------------------*/
72 /* Constant declarations */
73 /*---------------------------------------------------------------------------*/
74 
75 
76 /*---------------------------------------------------------------------------*/
77 /* Stucture declarations */
78 /*---------------------------------------------------------------------------*/
79 
80 
81 /*---------------------------------------------------------------------------*/
82 /* Type declarations */
83 /*---------------------------------------------------------------------------*/
84 
85 
86 /*---------------------------------------------------------------------------*/
87 /* Variable declarations */
88 /*---------------------------------------------------------------------------*/
89 
90 #ifndef lint
91 static char rcsid[] DD_UNUSED = "$Id: cuddZddCount.c,v 1.14 2004/08/13 18:04:53 fabio Exp $";
92 #endif
93 
94 /*---------------------------------------------------------------------------*/
95 /* Macro declarations */
96 /*---------------------------------------------------------------------------*/
97 
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101 
102 /**AutomaticStart*************************************************************/
103 
104 /*---------------------------------------------------------------------------*/
105 /* Static function prototypes */
106 /*---------------------------------------------------------------------------*/
107 
108 static int cuddZddCountStep (DdNode *P, st__table *table, DdNode *base, DdNode *empty);
109 static double cuddZddCountDoubleStep (DdNode *P, st__table *table, DdNode *base, DdNode *empty);
110 static enum st__retval st__zdd_countfree (char *key, char *value, char *arg);
111 static enum st__retval st__zdd_count_dbl_free (char *key, char *value, char *arg);
112 
113 /**AutomaticEnd***************************************************************/
114 
115 #ifdef __cplusplus
116 }
117 #endif
118 
119 /*---------------------------------------------------------------------------*/
120 /* Definition of exported functions */
121 /*---------------------------------------------------------------------------*/
122 
123 
124 /**Function********************************************************************
125 
126  Synopsis [Counts the number of minterms in a ZDD.]
127 
128  Description [Returns an integer representing the number of minterms
129  in a ZDD.]
130 
131  SideEffects [None]
132 
133  SeeAlso [Cudd_zddCountDouble]
134 
135 ******************************************************************************/
136 int
138  DdManager * zdd,
139  DdNode * P)
140 {
141  st__table *table;
142  int res;
143  DdNode *base, *empty;
144 
145  base = DD_ONE(zdd);
146  empty = DD_ZERO(zdd);
148  if (table == NULL) return(CUDD_OUT_OF_MEM);
149  res = cuddZddCountStep(P, table, base, empty);
150  if (res == CUDD_OUT_OF_MEM) {
151  zdd->errorCode = CUDD_MEMORY_OUT;
152  }
153  st__foreach(table, st__zdd_countfree, NIL(char));
154  st__free_table(table);
155 
156  return(res);
157 
158 } /* end of Cudd_zddCount */
159 
160 
161 /**Function********************************************************************
162 
163  Synopsis [Counts the number of minterms of a ZDD.]
164 
165  Description [Counts the number of minterms of a ZDD. The result is
166  returned as a double. If the procedure runs out of memory, it
167  returns (double) CUDD_OUT_OF_MEM. This procedure is used in
168  Cudd_zddCountMinterm.]
169 
170  SideEffects [None]
171 
172  SeeAlso [Cudd_zddCountMinterm Cudd_zddCount]
173 
174 ******************************************************************************/
175 double
177  DdManager * zdd,
178  DdNode * P)
179 {
180  st__table *table;
181  double res;
182  DdNode *base, *empty;
183 
184  base = DD_ONE(zdd);
185  empty = DD_ZERO(zdd);
187  if (table == NULL) return((double)CUDD_OUT_OF_MEM);
188  res = cuddZddCountDoubleStep(P, table, base, empty);
189  if (res == (double)CUDD_OUT_OF_MEM) {
190  zdd->errorCode = CUDD_MEMORY_OUT;
191  }
192  st__foreach(table, st__zdd_count_dbl_free, NIL(char));
193  st__free_table(table);
194 
195  return(res);
196 
197 } /* end of Cudd_zddCountDouble */
198 
199 
200 /*---------------------------------------------------------------------------*/
201 /* Definition of internal functions */
202 /*---------------------------------------------------------------------------*/
203 
204 
205 /*---------------------------------------------------------------------------*/
206 /* Definition of static functions */
207 /*---------------------------------------------------------------------------*/
208 
209 
210 /**Function********************************************************************
211 
212  Synopsis [Performs the recursive step of Cudd_zddCount.]
213 
214  Description []
215 
216  SideEffects [None]
217 
218  SeeAlso []
219 
220 ******************************************************************************/
221 static int
223  DdNode * P,
224  st__table * table,
225  DdNode * base,
226  DdNode * empty)
227 {
228  int res;
229  int *dummy;
230 
231  if (P == empty)
232  return(0);
233  if (P == base)
234  return(1);
235 
236  /* Check cache. */
237  if ( st__lookup(table, (const char *)P, (char **)&dummy)) {
238  res = *dummy;
239  return(res);
240  }
241 
242  res = cuddZddCountStep(cuddE(P), table, base, empty) +
243  cuddZddCountStep(cuddT(P), table, base, empty);
244 
245  dummy = ABC_ALLOC(int, 1);
246  if (dummy == NULL) {
247  return(CUDD_OUT_OF_MEM);
248  }
249  *dummy = res;
250  if ( st__insert(table, (char *)P, (char *)dummy) == st__OUT_OF_MEM) {
251  ABC_FREE(dummy);
252  return(CUDD_OUT_OF_MEM);
253  }
254 
255  return(res);
256 
257 } /* end of cuddZddCountStep */
258 
259 
260 /**Function********************************************************************
261 
262  Synopsis [Performs the recursive step of Cudd_zddCountDouble.]
263 
264  Description []
265 
266  SideEffects [None]
267 
268  SeeAlso []
269 
270 ******************************************************************************/
271 static double
273  DdNode * P,
274  st__table * table,
275  DdNode * base,
276  DdNode * empty)
277 {
278  double res;
279  double *dummy;
280 
281  if (P == empty)
282  return((double)0.0);
283  if (P == base)
284  return((double)1.0);
285 
286  /* Check cache */
287  if ( st__lookup(table, (const char *)P, (char **)&dummy)) {
288  res = *dummy;
289  return(res);
290  }
291 
292  res = cuddZddCountDoubleStep(cuddE(P), table, base, empty) +
293  cuddZddCountDoubleStep(cuddT(P), table, base, empty);
294 
295  dummy = ABC_ALLOC(double, 1);
296  if (dummy == NULL) {
297  return((double)CUDD_OUT_OF_MEM);
298  }
299  *dummy = res;
300  if ( st__insert(table, (char *)P, (char *)dummy) == st__OUT_OF_MEM) {
301  ABC_FREE(dummy);
302  return((double)CUDD_OUT_OF_MEM);
303  }
304 
305  return(res);
306 
307 } /* end of cuddZddCountDoubleStep */
308 
309 
310 /**Function********************************************************************
311 
312  Synopsis [Frees the memory associated with the computed table of
313  Cudd_zddCount.]
314 
315  Description []
316 
317  SideEffects [None]
318 
319  SeeAlso []
320 
321 ******************************************************************************/
322 static enum st__retval
324  char * key,
325  char * value,
326  char * arg)
327 {
328  int *d;
329 
330  d = (int *)value;
331  ABC_FREE(d);
332  return( st__CONTINUE);
333 
334 } /* end of st__zdd_countfree */
335 
336 
337 /**Function********************************************************************
338 
339  Synopsis [Frees the memory associated with the computed table of
340  Cudd_zddCountDouble.]
341 
342  Description []
343 
344  SideEffects [None]
345 
346  SeeAlso []
347 
348 ******************************************************************************/
349 static enum st__retval
351  char * key,
352  char * value,
353  char * arg)
354 {
355  double *d;
356 
357  d = (double *)value;
358  ABC_FREE(d);
359  return( st__CONTINUE);
360 
361 } /* end of st__zdd_count_dbl_free */
362 
363 
365 
void st__free_table(st__table *table)
Definition: st.c:81
#define CUDD_OUT_OF_MEM
Definition: cudd.h:95
Definition: cudd.h:278
double Cudd_zddCountDouble(DdManager *zdd, DdNode *P)
Definition: cuddZddCount.c:176
int st__insert(st__table *table, const char *key, char *value)
Definition: st.c:171
static byte P[256]
Definition: kitPerm.c:76
int st__ptrcmp(const char *, const char *)
Definition: st.c:480
static ABC_NAMESPACE_IMPL_START char rcsid[] DD_UNUSED
Definition: cuddZddCount.c:91
static enum st__retval st__zdd_countfree(char *key, char *value, char *arg)
Definition: cuddZddCount.c:323
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static int cuddZddCountStep(DdNode *P, st__table *table, DdNode *base, DdNode *empty)
Definition: cuddZddCount.c:222
#define NIL(type)
Definition: avl.h:25
st__retval
Definition: st.h:73
st__table * st__init_table(st__compare_func_type compare, st__hash_func_type hash)
Definition: st.c:72
Definition: st.h:52
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
int st__foreach(st__table *table, enum st__retval(*func)(char *, char *, char *), char *arg)
Definition: st.c:421
#define cuddT(node)
Definition: cuddInt.h:636
#define st__OUT_OF_MEM
Definition: st.h:113
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
int st__lookup(st__table *table, const char *key, char **value)
Definition: st.c:114
#define ABC_FREE(obj)
Definition: abc_global.h:232
enum keys key
#define cuddE(node)
Definition: cuddInt.h:652
int value
int Cudd_zddCount(DdManager *zdd, DdNode *P)
Definition: cuddZddCount.c:137
static DdNode * empty
Definition: cuddZddLin.c:98
#define DD_ONE(dd)
Definition: cuddInt.h:911
static double cuddZddCountDoubleStep(DdNode *P, st__table *table, DdNode *base, DdNode *empty)
Definition: cuddZddCount.c:272
Cudd_ErrorType errorCode
Definition: cuddInt.h:447
int st__ptrhash(const char *, int)
Definition: st.c:468
static enum st__retval st__zdd_count_dbl_free(char *key, char *value, char *arg)
Definition: cuddZddCount.c:350
#define DD_ZERO(dd)
Definition: cuddInt.h:927