00001
00026 #include "util_hack.h"
00027 #include "cuddInt.h"
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef lint
00050 static char rcsid[] DD_UNUSED = "$Id: cuddRead.c,v 1.1.1.1 2003/02/24 22:23:52 wjiang Exp $";
00051 #endif
00052
00053
00054
00055
00056
00057
00060
00061
00062
00063
00064
00068
00069
00070
00071
00072
00114 int
00115 Cudd_addRead(
00116 FILE * fp ,
00117 DdManager * dd ,
00118 DdNode ** E ,
00119 DdNode *** x ,
00120 DdNode *** y ,
00121 DdNode *** xn ,
00122 DdNode *** yn_ ,
00123 int * nx ,
00124 int * ny ,
00125 int * m ,
00126 int * n ,
00127 int bx ,
00128 int sx ,
00129 int by ,
00130 int sy )
00131 {
00132 DdNode *one, *zero;
00133 DdNode *w, *neW;
00134 DdNode *minterm1;
00135 int u, v, err, i, nv;
00136 int lnx, lny;
00137 CUDD_VALUE_TYPE val;
00138 DdNode **lx, **ly, **lxn, **lyn;
00139
00140 one = DD_ONE(dd);
00141 zero = DD_ZERO(dd);
00142
00143 err = fscanf(fp, "%d %d", &u, &v);
00144 if (err == EOF) {
00145 return(0);
00146 } else if (err != 2) {
00147 return(0);
00148 }
00149
00150 *m = u;
00151
00152 lx = *x; lxn = *xn;
00153 u--;
00154 for (lnx=0; u > 0; lnx++) {
00155 u >>= 1;
00156 }
00157
00158
00159
00160 if (lnx > *nx) {
00161 *x = lx = REALLOC(DdNode *, *x, lnx);
00162 if (lx == NULL) {
00163 dd->errorCode = CUDD_MEMORY_OUT;
00164 return(0);
00165 }
00166 *xn = lxn = REALLOC(DdNode *, *xn, lnx);
00167 if (lxn == NULL) {
00168 dd->errorCode = CUDD_MEMORY_OUT;
00169 return(0);
00170 }
00171 }
00172
00173 *n = v;
00174
00175 ly = *y; lyn = *yn_;
00176 v--;
00177 for (lny=0; v > 0; lny++) {
00178 v >>= 1;
00179 }
00180
00181
00182
00183 if (lny > *ny) {
00184 *y = ly = REALLOC(DdNode *, *y, lny);
00185 if (ly == NULL) {
00186 dd->errorCode = CUDD_MEMORY_OUT;
00187 return(0);
00188 }
00189 *yn_ = lyn = REALLOC(DdNode *, *yn_, lny);
00190 if (lyn == NULL) {
00191 dd->errorCode = CUDD_MEMORY_OUT;
00192 return(0);
00193 }
00194 }
00195
00196
00197 for (i = *nx, nv = bx + (*nx) * sx; i < lnx; i++, nv += sx) {
00198 do {
00199 dd->reordered = 0;
00200 lx[i] = cuddUniqueInter(dd, nv, one, zero);
00201 } while (dd->reordered == 1);
00202 if (lx[i] == NULL) return(0);
00203 cuddRef(lx[i]);
00204 do {
00205 dd->reordered = 0;
00206 lxn[i] = cuddUniqueInter(dd, nv, zero, one);
00207 } while (dd->reordered == 1);
00208 if (lxn[i] == NULL) return(0);
00209 cuddRef(lxn[i]);
00210 }
00211 for (i = *ny, nv = by + (*ny) * sy; i < lny; i++, nv += sy) {
00212 do {
00213 dd->reordered = 0;
00214 ly[i] = cuddUniqueInter(dd, nv, one, zero);
00215 } while (dd->reordered == 1);
00216 if (ly[i] == NULL) return(0);
00217 cuddRef(ly[i]);
00218 do {
00219 dd->reordered = 0;
00220 lyn[i] = cuddUniqueInter(dd, nv, zero, one);
00221 } while (dd->reordered == 1);
00222 if (lyn[i] == NULL) return(0);
00223 cuddRef(lyn[i]);
00224 }
00225 *nx = lnx;
00226 *ny = lny;
00227
00228 *E = dd->background;
00229 cuddRef(*E);
00230
00231 while (! feof(fp)) {
00232 err = fscanf(fp, "%d %d %lf", &u, &v, &val);
00233 if (err == EOF) {
00234 break;
00235 } else if (err != 3) {
00236 return(0);
00237 } else if (u >= *m || v >= *n || u < 0 || v < 0) {
00238 return(0);
00239 }
00240
00241 minterm1 = one; cuddRef(minterm1);
00242
00243
00244 for (i = lnx - 1; i>=0; i--) {
00245 if (u & 1) {
00246 w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lx[i]);
00247 } else {
00248 w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lxn[i]);
00249 }
00250 if (w == NULL) {
00251 Cudd_RecursiveDeref(dd, minterm1);
00252 return(0);
00253 }
00254 cuddRef(w);
00255 Cudd_RecursiveDeref(dd, minterm1);
00256 minterm1 = w;
00257 u >>= 1;
00258 }
00259 for (i = lny - 1; i>=0; i--) {
00260 if (v & 1) {
00261 w = Cudd_addApply(dd, Cudd_addTimes, minterm1, ly[i]);
00262 } else {
00263 w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lyn[i]);
00264 }
00265 if (w == NULL) {
00266 Cudd_RecursiveDeref(dd, minterm1);
00267 return(0);
00268 }
00269 cuddRef(w);
00270 Cudd_RecursiveDeref(dd, minterm1);
00271 minterm1 = w;
00272 v >>= 1;
00273 }
00274
00275
00276
00277 neW = cuddUniqueConst(dd, val);
00278 if (neW == NULL) {
00279 Cudd_RecursiveDeref(dd, minterm1);
00280 return(0);
00281 }
00282 cuddRef(neW);
00283
00284 w = Cudd_addIte(dd, minterm1, neW, *E);
00285 if (w == NULL) {
00286 Cudd_RecursiveDeref(dd, minterm1);
00287 Cudd_RecursiveDeref(dd, neW);
00288 return(0);
00289 }
00290 cuddRef(w);
00291 Cudd_RecursiveDeref(dd, minterm1);
00292 Cudd_RecursiveDeref(dd, neW);
00293 Cudd_RecursiveDeref(dd, *E);
00294 *E = w;
00295 }
00296 return(1);
00297
00298 }
00299
00300
00337 int
00338 Cudd_bddRead(
00339 FILE * fp ,
00340 DdManager * dd ,
00341 DdNode ** E ,
00342 DdNode *** x ,
00343 DdNode *** y ,
00344 int * nx ,
00345 int * ny ,
00346 int * m ,
00347 int * n ,
00348 int bx ,
00349 int sx ,
00350 int by ,
00351 int sy )
00352 {
00353 DdNode *one, *zero;
00354 DdNode *w;
00355 DdNode *minterm1;
00356 int u, v, err, i, nv;
00357 int lnx, lny;
00358 DdNode **lx, **ly;
00359
00360 one = DD_ONE(dd);
00361 zero = Cudd_Not(one);
00362
00363 err = fscanf(fp, "%d %d", &u, &v);
00364 if (err == EOF) {
00365 return(0);
00366 } else if (err != 2) {
00367 return(0);
00368 }
00369
00370 *m = u;
00371
00372 lx = *x;
00373 u--;
00374 for (lnx=0; u > 0; lnx++) {
00375 u >>= 1;
00376 }
00377 if (lnx > *nx) {
00378 *x = lx = REALLOC(DdNode *, *x, lnx);
00379 if (lx == NULL) {
00380 dd->errorCode = CUDD_MEMORY_OUT;
00381 return(0);
00382 }
00383 }
00384
00385 *n = v;
00386
00387 ly = *y;
00388 v--;
00389 for (lny=0; v > 0; lny++) {
00390 v >>= 1;
00391 }
00392 if (lny > *ny) {
00393 *y = ly = REALLOC(DdNode *, *y, lny);
00394 if (ly == NULL) {
00395 dd->errorCode = CUDD_MEMORY_OUT;
00396 return(0);
00397 }
00398 }
00399
00400
00401 for (i = *nx, nv = bx + (*nx) * sx; i < lnx; i++, nv += sx) {
00402 do {
00403 dd->reordered = 0;
00404 lx[i] = cuddUniqueInter(dd, nv, one, zero);
00405 } while (dd->reordered == 1);
00406 if (lx[i] == NULL) return(0);
00407 cuddRef(lx[i]);
00408 }
00409 for (i = *ny, nv = by + (*ny) * sy; i < lny; i++, nv += sy) {
00410 do {
00411 dd->reordered = 0;
00412 ly[i] = cuddUniqueInter(dd, nv, one, zero);
00413 } while (dd->reordered == 1);
00414 if (ly[i] == NULL) return(0);
00415 cuddRef(ly[i]);
00416 }
00417 *nx = lnx;
00418 *ny = lny;
00419
00420 *E = zero;
00421 cuddRef(*E);
00422
00423 while (! feof(fp)) {
00424 err = fscanf(fp, "%d %d", &u, &v);
00425 if (err == EOF) {
00426 break;
00427 } else if (err != 2) {
00428 return(0);
00429 } else if (u >= *m || v >= *n || u < 0 || v < 0) {
00430 return(0);
00431 }
00432
00433 minterm1 = one; cuddRef(minterm1);
00434
00435
00436 for (i = lnx - 1; i>=0; i--) {
00437 if (u & 1) {
00438 w = Cudd_bddAnd(dd, minterm1, lx[i]);
00439 } else {
00440 w = Cudd_bddAnd(dd, minterm1, Cudd_Not(lx[i]));
00441 }
00442 if (w == NULL) {
00443 Cudd_RecursiveDeref(dd, minterm1);
00444 return(0);
00445 }
00446 cuddRef(w);
00447 Cudd_RecursiveDeref(dd,minterm1);
00448 minterm1 = w;
00449 u >>= 1;
00450 }
00451 for (i = lny - 1; i>=0; i--) {
00452 if (v & 1) {
00453 w = Cudd_bddAnd(dd, minterm1, ly[i]);
00454 } else {
00455 w = Cudd_bddAnd(dd, minterm1, Cudd_Not(ly[i]));
00456 }
00457 if (w == NULL) {
00458 Cudd_RecursiveDeref(dd, minterm1);
00459 return(0);
00460 }
00461 cuddRef(w);
00462 Cudd_RecursiveDeref(dd, minterm1);
00463 minterm1 = w;
00464 v >>= 1;
00465 }
00466
00467 w = Cudd_bddAnd(dd, Cudd_Not(minterm1), Cudd_Not(*E));
00468 if (w == NULL) {
00469 Cudd_RecursiveDeref(dd, minterm1);
00470 return(0);
00471 }
00472 w = Cudd_Not(w);
00473 cuddRef(w);
00474 Cudd_RecursiveDeref(dd, minterm1);
00475 Cudd_RecursiveDeref(dd, *E);
00476 *E = w;
00477 }
00478 return(1);
00479
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490