00001
00037 #include "util_hack.h"
00038 #include "cuddInt.h"
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef lint
00060 static char rcsid[] DD_UNUSED = "$Id: cuddZddCount.c,v 1.1.1.1 2003/02/24 22:23:53 wjiang Exp $";
00061 #endif
00062
00063
00064
00065
00066
00067
00070
00071
00072
00073
00074 static int cuddZddCountStep ARGS((DdNode *P, st_table *table, DdNode *base, DdNode *empty));
00075 static double cuddZddCountDoubleStep ARGS((DdNode *P, st_table *table, DdNode *base, DdNode *empty));
00076 static enum st_retval st_zdd_countfree ARGS((char *key, char *value, char *arg));
00077 static enum st_retval st_zdd_count_dbl_free ARGS((char *key, char *value, char *arg));
00078
00082
00083
00084
00085
00086
00099 int
00100 Cudd_zddCount(
00101 DdManager * zdd,
00102 DdNode * P)
00103 {
00104 st_table *table;
00105 int res;
00106 DdNode *base, *empty;
00107
00108 base = DD_ONE(zdd);
00109 empty = DD_ZERO(zdd);
00110 table = st_init_table(st_ptrcmp, st_ptrhash);
00111 if (table == NULL) return(CUDD_OUT_OF_MEM);
00112 res = cuddZddCountStep(P, table, base, empty);
00113 if (res == CUDD_OUT_OF_MEM) {
00114 zdd->errorCode = CUDD_MEMORY_OUT;
00115 }
00116 st_foreach(table, st_zdd_countfree, NIL(char));
00117 st_free_table(table);
00118
00119 return(res);
00120
00121 }
00122
00123
00138 double
00139 Cudd_zddCountDouble(
00140 DdManager * zdd,
00141 DdNode * P)
00142 {
00143 st_table *table;
00144 double res;
00145 DdNode *base, *empty;
00146
00147 base = DD_ONE(zdd);
00148 empty = DD_ZERO(zdd);
00149 table = st_init_table(st_ptrcmp, st_ptrhash);
00150 if (table == NULL) return((double)CUDD_OUT_OF_MEM);
00151 res = cuddZddCountDoubleStep(P, table, base, empty);
00152 if (res == (double)CUDD_OUT_OF_MEM) {
00153 zdd->errorCode = CUDD_MEMORY_OUT;
00154 }
00155 st_foreach(table, st_zdd_count_dbl_free, NIL(char));
00156 st_free_table(table);
00157
00158 return(res);
00159
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00184 static int
00185 cuddZddCountStep(
00186 DdNode * P,
00187 st_table * table,
00188 DdNode * base,
00189 DdNode * empty)
00190 {
00191 int res;
00192 int *dummy;
00193
00194 if (P == empty)
00195 return(0);
00196 if (P == base)
00197 return(1);
00198
00199
00200 if (st_lookup(table, (char *)P, (char **)(&dummy))) {
00201 res = *dummy;
00202 return(res);
00203 }
00204
00205 res = cuddZddCountStep(cuddE(P), table, base, empty) +
00206 cuddZddCountStep(cuddT(P), table, base, empty);
00207
00208 dummy = ALLOC(int, 1);
00209 if (dummy == NULL) {
00210 return(CUDD_OUT_OF_MEM);
00211 }
00212 *dummy = res;
00213 if (st_insert(table, (char *)P, (char *)dummy) == ST_OUT_OF_MEM) {
00214 FREE(dummy);
00215 return(CUDD_OUT_OF_MEM);
00216 }
00217
00218 return(res);
00219
00220 }
00221
00222
00234 static double
00235 cuddZddCountDoubleStep(
00236 DdNode * P,
00237 st_table * table,
00238 DdNode * base,
00239 DdNode * empty)
00240 {
00241 double res;
00242 double *dummy;
00243
00244 if (P == empty)
00245 return((double)0.0);
00246 if (P == base)
00247 return((double)1.0);
00248
00249
00250 if (st_lookup(table, (char *)P, (char **)(&dummy))) {
00251 res = *dummy;
00252 return(res);
00253 }
00254
00255 res = cuddZddCountDoubleStep(cuddE(P), table, base, empty) +
00256 cuddZddCountDoubleStep(cuddT(P), table, base, empty);
00257
00258 dummy = ALLOC(double, 1);
00259 if (dummy == NULL) {
00260 return((double)CUDD_OUT_OF_MEM);
00261 }
00262 *dummy = res;
00263 if (st_insert(table, (char *)P, (char *)dummy) == ST_OUT_OF_MEM) {
00264 FREE(dummy);
00265 return((double)CUDD_OUT_OF_MEM);
00266 }
00267
00268 return(res);
00269
00270 }
00271
00272
00285 static enum st_retval
00286 st_zdd_countfree(
00287 char * key,
00288 char * value,
00289 char * arg)
00290 {
00291 int *d;
00292
00293 d = (int *)value;
00294 FREE(d);
00295 return(ST_CONTINUE);
00296
00297 }
00298
00299
00312 static enum st_retval
00313 st_zdd_count_dbl_free(
00314 char * key,
00315 char * value,
00316 char * arg)
00317 {
00318 double *d;
00319
00320 d = (double *)value;
00321 FREE(d);
00322 return(ST_CONTINUE);
00323
00324 }