src/bdd/cudd/cuddRead.c File Reference

#include "util_hack.h"
#include "cuddInt.h"
Include dependency graph for cuddRead.c:

Go to the source code of this file.

Functions

int Cudd_addRead (FILE *fp, DdManager *dd, DdNode **E, DdNode ***x, DdNode ***y, DdNode ***xn, DdNode ***yn_, int *nx, int *ny, int *m, int *n, int bx, int sx, int by, int sy)
int Cudd_bddRead (FILE *fp, DdManager *dd, DdNode **E, DdNode ***x, DdNode ***y, int *nx, int *ny, int *m, int *n, int bx, int sx, int by, int sy)

Variables

static char rcsid[] DD_UNUSED = "$Id: cuddRead.c,v 1.1.1.1 2003/02/24 22:23:52 wjiang Exp $"

Function Documentation

int Cudd_addRead ( FILE *  fp,
DdManager dd,
DdNode **  E,
DdNode ***  x,
DdNode ***  y,
DdNode ***  xn,
DdNode ***  yn_,
int *  nx,
int *  ny,
int *  m,
int *  n,
int  bx,
int  sx,
int  by,
int  sy 
)

AutomaticStart AutomaticEnd Function********************************************************************

Synopsis [Reads in a sparse matrix.]

Description [Reads in a sparse matrix specified in a simple format. The first line of the input contains the numbers of rows and columns. The remaining lines contain the elements of the matrix, one per line. Given a background value (specified by the background field of the manager), only the values different from it are explicitly listed. Each foreground element is described by two integers, i.e., the row and column number, and a real number, i.e., the value.

Cudd_addRead produces an ADD that depends on two sets of variables: x and y. The x variables (x\[0\] ... x\[nx-1\]) encode the row index and the y variables (y\[0\] ... y\[ny-1\]) encode the column index. x\[0\] and y\[0\] are the most significant bits in the indices. The variables may already exist or may be created by the function. The index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy.

On input, nx and ny hold the numbers of row and column variables already in existence. On output, they hold the numbers of row and column variables actually used by the matrix. When Cudd_addRead creates the variable arrays, the index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy. When some variables already exist Cudd_addRead expects the indices of the existing x variables to be bx+i*sx, and the indices of the existing y variables to be by+i*sy.

m and n are set to the numbers of rows and columns of the matrix. Their values on input are immaterial. The ADD for the sparse matrix is returned in E, and its reference count is > 0. Cudd_addRead returns 1 in case of success; 0 otherwise.]

SideEffects [nx and ny are set to the numbers of row and column variables. m and n are set to the numbers of rows and columns. x and y are possibly extended to represent the array of row and column variables. Similarly for xn and yn_, which hold on return from Cudd_addRead the complements of the row and column variables.]

SeeAlso [Cudd_addHarwell Cudd_bddRead]

Definition at line 115 of file cuddRead.c.

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     /* Compute the number of x variables. */
00152     lx = *x; lxn = *xn;
00153     u--;        /* row and column numbers start from 0 */
00154     for (lnx=0; u > 0; lnx++) {
00155         u >>= 1;
00156     }
00157     /* Here we rely on the fact that REALLOC of a null pointer is
00158     ** translates to an ALLOC.
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     /* Compute the number of y variables. */
00175     ly = *y; lyn = *yn_;
00176     v--;        /* row and column numbers start from 0 */
00177     for (lny=0; v > 0; lny++) {
00178         v >>= 1;
00179     }
00180     /* Here we rely on the fact that REALLOC of a null pointer is
00181     ** translates to an ALLOC.
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     /* Create all new variables. */
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; /* this call will never cause reordering */
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         /* Build minterm1 corresponding to this arc */
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         /* Create new constant node if necessary.
00275         ** This call will never cause reordering.
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 } /* end of Cudd_addRead */

int Cudd_bddRead ( FILE *  fp,
DdManager dd,
DdNode **  E,
DdNode ***  x,
DdNode ***  y,
int *  nx,
int *  ny,
int *  m,
int *  n,
int  bx,
int  sx,
int  by,
int  sy 
)

Function********************************************************************

Synopsis [Reads in a graph (without labels) given as a list of arcs.]

Description [Reads in a graph (without labels) given as an adjacency matrix. The first line of the input contains the numbers of rows and columns of the adjacency matrix. The remaining lines contain the arcs of the graph, one per line. Each arc is described by two integers, i.e., the row and column number, or the indices of the two endpoints. Cudd_bddRead produces a BDD that depends on two sets of variables: x and y. The x variables (x\[0\] ... x\[nx-1\]) encode the row index and the y variables (y\[0\] ... y\[ny-1\]) encode the column index. x\[0\] and y\[0\] are the most significant bits in the indices. The variables may already exist or may be created by the function. The index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy.

On input, nx and ny hold the numbers of row and column variables already in existence. On output, they hold the numbers of row and column variables actually used by the matrix. When Cudd_bddRead creates the variable arrays, the index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy. When some variables already exist, Cudd_bddRead expects the indices of the existing x variables to be bx+i*sx, and the indices of the existing y variables to be by+i*sy.

m and n are set to the numbers of rows and columns of the matrix. Their values on input are immaterial. The BDD for the graph is returned in E, and its reference count is > 0. Cudd_bddRead returns 1 in case of success; 0 otherwise.]

SideEffects [nx and ny are set to the numbers of row and column variables. m and n are set to the numbers of rows and columns. x and y are possibly extended to represent the array of row and column variables.]

SeeAlso [Cudd_addHarwell Cudd_addRead]

Definition at line 338 of file cuddRead.c.

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     /* Compute the number of x variables. */
00372     lx = *x;
00373     u--;        /* row and column numbers start from 0 */
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     /* Compute the number of y variables. */
00387     ly = *y;
00388     v--;        /* row and column numbers start from 0 */
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     /* Create all new variables. */
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; /* this call will never cause reordering */
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         /* Build minterm1 corresponding to this arc. */
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 } /* end of Cudd_bddRead */


Variable Documentation

char rcsid [] DD_UNUSED = "$Id: cuddRead.c,v 1.1.1.1 2003/02/24 22:23:52 wjiang Exp $" [static]

CFile***********************************************************************

FileName [cuddRead.c]

PackageName [cudd]

Synopsis [Functions to read in a matrix]

Description [External procedures included in this module:

]

SeeAlso [cudd_addHarwell.c]

Author [Fabio Somenzi]

Copyright [ This file was created at the University of Colorado at Boulder. The University of Colorado at Boulder makes no warranty about the suitability of this software for any purpose. It is presented on an AS IS basis.]

Definition at line 50 of file cuddRead.c.


Generated on Tue Jan 5 12:18:55 2010 for abc70930 by  doxygen 1.6.1