abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cuddRead.c File Reference
#include "misc/util/util_hack.h"
#include "cuddInt.h"

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
ABC_NAMESPACE_IMPL_START char
rcsid[] 
DD_UNUSED = "$Id: cuddRead.c,v 1.6 2004/08/13 18:04:50 fabio 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 146 of file cuddRead.c.

162 {
163  DdNode *one, *zero;
164  DdNode *w, *neW;
165  DdNode *minterm1;
166  int u, v, err, i, nv;
167  int lnx, lny;
168  CUDD_VALUE_TYPE val;
169  DdNode **lx, **ly, **lxn, **lyn;
170 
171  one = DD_ONE(dd);
172  zero = DD_ZERO(dd);
173 
174  err = fscanf(fp, "%d %d", &u, &v);
175  if (err == EOF) {
176  return(0);
177  } else if (err != 2) {
178  return(0);
179  }
180 
181  *m = u;
182  /* Compute the number of x variables. */
183  lx = *x; lxn = *xn;
184  u--; /* row and column numbers start from 0 */
185  for (lnx=0; u > 0; lnx++) {
186  u >>= 1;
187  }
188  /* Here we rely on the fact that REALLOC of a null pointer is
189  ** translates to an ALLOC.
190  */
191  if (lnx > *nx) {
192  *x = lx = ABC_REALLOC(DdNode *, *x, lnx);
193  if (lx == NULL) {
195  return(0);
196  }
197  *xn = lxn = ABC_REALLOC(DdNode *, *xn, lnx);
198  if (lxn == NULL) {
200  return(0);
201  }
202  }
203 
204  *n = v;
205  /* Compute the number of y variables. */
206  ly = *y; lyn = *yn_;
207  v--; /* row and column numbers start from 0 */
208  for (lny=0; v > 0; lny++) {
209  v >>= 1;
210  }
211  /* Here we rely on the fact that REALLOC of a null pointer is
212  ** translates to an ALLOC.
213  */
214  if (lny > *ny) {
215  *y = ly = ABC_REALLOC(DdNode *, *y, lny);
216  if (ly == NULL) {
218  return(0);
219  }
220  *yn_ = lyn = ABC_REALLOC(DdNode *, *yn_, lny);
221  if (lyn == NULL) {
223  return(0);
224  }
225  }
226 
227  /* Create all new variables. */
228  for (i = *nx, nv = bx + (*nx) * sx; i < lnx; i++, nv += sx) {
229  do {
230  dd->reordered = 0;
231  lx[i] = cuddUniqueInter(dd, nv, one, zero);
232  } while (dd->reordered == 1);
233  if (lx[i] == NULL) return(0);
234  cuddRef(lx[i]);
235  do {
236  dd->reordered = 0;
237  lxn[i] = cuddUniqueInter(dd, nv, zero, one);
238  } while (dd->reordered == 1);
239  if (lxn[i] == NULL) return(0);
240  cuddRef(lxn[i]);
241  }
242  for (i = *ny, nv = by + (*ny) * sy; i < lny; i++, nv += sy) {
243  do {
244  dd->reordered = 0;
245  ly[i] = cuddUniqueInter(dd, nv, one, zero);
246  } while (dd->reordered == 1);
247  if (ly[i] == NULL) return(0);
248  cuddRef(ly[i]);
249  do {
250  dd->reordered = 0;
251  lyn[i] = cuddUniqueInter(dd, nv, zero, one);
252  } while (dd->reordered == 1);
253  if (lyn[i] == NULL) return(0);
254  cuddRef(lyn[i]);
255  }
256  *nx = lnx;
257  *ny = lny;
258 
259  *E = dd->background; /* this call will never cause reordering */
260  cuddRef(*E);
261 
262  while (! feof(fp)) {
263  err = fscanf(fp, "%d %d %lf", &u, &v, &val);
264  if (err == EOF) {
265  break;
266  } else if (err != 3) {
267  return(0);
268  } else if (u >= *m || v >= *n || u < 0 || v < 0) {
269  return(0);
270  }
271 
272  minterm1 = one; cuddRef(minterm1);
273 
274  /* Build minterm1 corresponding to this arc */
275  for (i = lnx - 1; i>=0; i--) {
276  if (u & 1) {
277  w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lx[i]);
278  } else {
279  w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lxn[i]);
280  }
281  if (w == NULL) {
282  Cudd_RecursiveDeref(dd, minterm1);
283  return(0);
284  }
285  cuddRef(w);
286  Cudd_RecursiveDeref(dd, minterm1);
287  minterm1 = w;
288  u >>= 1;
289  }
290  for (i = lny - 1; i>=0; i--) {
291  if (v & 1) {
292  w = Cudd_addApply(dd, Cudd_addTimes, minterm1, ly[i]);
293  } else {
294  w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lyn[i]);
295  }
296  if (w == NULL) {
297  Cudd_RecursiveDeref(dd, minterm1);
298  return(0);
299  }
300  cuddRef(w);
301  Cudd_RecursiveDeref(dd, minterm1);
302  minterm1 = w;
303  v >>= 1;
304  }
305  /* Create new constant node if necessary.
306  ** This call will never cause reordering.
307  */
308  neW = cuddUniqueConst(dd, val);
309  if (neW == NULL) {
310  Cudd_RecursiveDeref(dd, minterm1);
311  return(0);
312  }
313  cuddRef(neW);
314 
315  w = Cudd_addIte(dd, minterm1, neW, *E);
316  if (w == NULL) {
317  Cudd_RecursiveDeref(dd, minterm1);
318  Cudd_RecursiveDeref(dd, neW);
319  return(0);
320  }
321  cuddRef(w);
322  Cudd_RecursiveDeref(dd, minterm1);
323  Cudd_RecursiveDeref(dd, neW);
324  Cudd_RecursiveDeref(dd, *E);
325  *E = w;
326  }
327  return(1);
328 
329 } /* end of Cudd_addRead */
#define cuddRef(n)
Definition: cuddInt.h:584
void Cudd_RecursiveDeref(DdManager *table, DdNode *n)
Definition: cuddRef.c:154
Definition: cudd.h:278
DdNode * cuddUniqueConst(DdManager *unique, CUDD_VALUE_TYPE value)
Definition: cuddTable.c:1450
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
DdNode * Cudd_addApply(DdManager *dd, DdNode *(*)(DdManager *, DdNode **, DdNode **), DdNode *f, DdNode *g)
int reordered
Definition: cuddInt.h:409
static DdNode * one
Definition: cuddDecomp.c:112
DdNode * Cudd_addIte(DdManager *dd, DdNode *f, DdNode *g, DdNode *h)
Definition: cuddAddIte.c:129
#define CUDD_VALUE_TYPE
Definition: cudd.h:94
#define DD_ONE(dd)
Definition: cuddInt.h:911
DdNode * Cudd_addTimes(DdManager *dd, DdNode **f, DdNode **g)
Definition: cuddAddApply.c:208
Cudd_ErrorType errorCode
Definition: cuddInt.h:447
DdNode * cuddUniqueInter(DdManager *unique, int index, DdNode *T, DdNode *E)
Definition: cuddTable.c:1128
static DdNode * zero
Definition: cuddApa.c:100
DdNode * background
Definition: cuddInt.h:349
#define DD_ZERO(dd)
Definition: cuddInt.h:927
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 369 of file cuddRead.c.

383 {
384  DdNode *one, *zero;
385  DdNode *w;
386  DdNode *minterm1;
387  int u, v, err, i, nv;
388  int lnx, lny;
389  DdNode **lx, **ly;
390 
391  one = DD_ONE(dd);
392  zero = Cudd_Not(one);
393 
394  err = fscanf(fp, "%d %d", &u, &v);
395  if (err == EOF) {
396  return(0);
397  } else if (err != 2) {
398  return(0);
399  }
400 
401  *m = u;
402  /* Compute the number of x variables. */
403  lx = *x;
404  u--; /* row and column numbers start from 0 */
405  for (lnx=0; u > 0; lnx++) {
406  u >>= 1;
407  }
408  if (lnx > *nx) {
409  *x = lx = ABC_REALLOC(DdNode *, *x, lnx);
410  if (lx == NULL) {
412  return(0);
413  }
414  }
415 
416  *n = v;
417  /* Compute the number of y variables. */
418  ly = *y;
419  v--; /* row and column numbers start from 0 */
420  for (lny=0; v > 0; lny++) {
421  v >>= 1;
422  }
423  if (lny > *ny) {
424  *y = ly = ABC_REALLOC(DdNode *, *y, lny);
425  if (ly == NULL) {
427  return(0);
428  }
429  }
430 
431  /* Create all new variables. */
432  for (i = *nx, nv = bx + (*nx) * sx; i < lnx; i++, nv += sx) {
433  do {
434  dd->reordered = 0;
435  lx[i] = cuddUniqueInter(dd, nv, one, zero);
436  } while (dd->reordered == 1);
437  if (lx[i] == NULL) return(0);
438  cuddRef(lx[i]);
439  }
440  for (i = *ny, nv = by + (*ny) * sy; i < lny; i++, nv += sy) {
441  do {
442  dd->reordered = 0;
443  ly[i] = cuddUniqueInter(dd, nv, one, zero);
444  } while (dd->reordered == 1);
445  if (ly[i] == NULL) return(0);
446  cuddRef(ly[i]);
447  }
448  *nx = lnx;
449  *ny = lny;
450 
451  *E = zero; /* this call will never cause reordering */
452  cuddRef(*E);
453 
454  while (! feof(fp)) {
455  err = fscanf(fp, "%d %d", &u, &v);
456  if (err == EOF) {
457  break;
458  } else if (err != 2) {
459  return(0);
460  } else if (u >= *m || v >= *n || u < 0 || v < 0) {
461  return(0);
462  }
463 
464  minterm1 = one; cuddRef(minterm1);
465 
466  /* Build minterm1 corresponding to this arc. */
467  for (i = lnx - 1; i>=0; i--) {
468  if (u & 1) {
469  w = Cudd_bddAnd(dd, minterm1, lx[i]);
470  } else {
471  w = Cudd_bddAnd(dd, minterm1, Cudd_Not(lx[i]));
472  }
473  if (w == NULL) {
474  Cudd_RecursiveDeref(dd, minterm1);
475  return(0);
476  }
477  cuddRef(w);
478  Cudd_RecursiveDeref(dd,minterm1);
479  minterm1 = w;
480  u >>= 1;
481  }
482  for (i = lny - 1; i>=0; i--) {
483  if (v & 1) {
484  w = Cudd_bddAnd(dd, minterm1, ly[i]);
485  } else {
486  w = Cudd_bddAnd(dd, minterm1, Cudd_Not(ly[i]));
487  }
488  if (w == NULL) {
489  Cudd_RecursiveDeref(dd, minterm1);
490  return(0);
491  }
492  cuddRef(w);
493  Cudd_RecursiveDeref(dd, minterm1);
494  minterm1 = w;
495  v >>= 1;
496  }
497 
498  w = Cudd_bddAnd(dd, Cudd_Not(minterm1), Cudd_Not(*E));
499  if (w == NULL) {
500  Cudd_RecursiveDeref(dd, minterm1);
501  return(0);
502  }
503  w = Cudd_Not(w);
504  cuddRef(w);
505  Cudd_RecursiveDeref(dd, minterm1);
506  Cudd_RecursiveDeref(dd, *E);
507  *E = w;
508  }
509  return(1);
510 
511 } /* end of Cudd_bddRead */
#define cuddRef(n)
Definition: cuddInt.h:584
void Cudd_RecursiveDeref(DdManager *table, DdNode *n)
Definition: cuddRef.c:154
Definition: cudd.h:278
#define Cudd_Not(node)
Definition: cudd.h:367
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
int reordered
Definition: cuddInt.h:409
static DdNode * one
Definition: cuddDecomp.c:112
DdNode * Cudd_bddAnd(DdManager *dd, DdNode *f, DdNode *g)
Definition: cuddBddIte.c:314
#define DD_ONE(dd)
Definition: cuddInt.h:911
Cudd_ErrorType errorCode
Definition: cuddInt.h:447
DdNode * cuddUniqueInter(DdManager *unique, int index, DdNode *T, DdNode *E)
Definition: cuddTable.c:1128
static DdNode * zero
Definition: cuddApa.c:100

Variable Documentation

ABC_NAMESPACE_IMPL_START char rcsid [] DD_UNUSED = "$Id: cuddRead.c,v 1.6 2004/08/13 18:04:50 fabio 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 [Copyright (c) 1995-2004, Regents of the University of Colorado

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name of the University of Colorado nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.]

Definition at line 81 of file cuddRead.c.