abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
satStore.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "satStore.h"

Go to the source code of this file.

Functions

ABC_NAMESPACE_IMPL_START char * Sto_ManMemoryFetch (Sto_Man_t *p, int nBytes)
 DECLARATIONS ///. More...
 
void Sto_ManMemoryStop (Sto_Man_t *p)
 
int Sto_ManMemoryReport (Sto_Man_t *p)
 
Sto_Man_tSto_ManAlloc ()
 MACRO DEFINITIONS ///. More...
 
void Sto_ManFree (Sto_Man_t *p)
 
int Sto_ManAddClause (Sto_Man_t *p, lit *pBeg, lit *pEnd)
 
void Sto_ManMarkRoots (Sto_Man_t *p)
 
void Sto_ManMarkClausesA (Sto_Man_t *p)
 
int Sto_ManChangeLastClause (Sto_Man_t *p)
 
void Sto_ManDumpClauses (Sto_Man_t *p, char *pFileName)
 
int Sto_ManLoadNumber (FILE *pFile, int *pNumber)
 
Sto_Man_tSto_ManLoadClauses (char *pFileName)
 

Function Documentation

int Sto_ManAddClause ( Sto_Man_t p,
lit pBeg,
lit pEnd 
)

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

Synopsis [Adds one clause to the manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 160 of file satStore.c.

161 {
162  Sto_Cls_t * pClause;
163  lit Lit, * i, * j;
164  int nSize;
165 
166  // process the literals
167  if ( pBeg < pEnd )
168  {
169  // insertion sort
170  for ( i = pBeg + 1; i < pEnd; i++ )
171  {
172  Lit = *i;
173  for ( j = i; j > pBeg && *(j-1) > Lit; j-- )
174  *j = *(j-1);
175  *j = Lit;
176  }
177  // make sure there is no duplicated variables
178  for ( i = pBeg + 1; i < pEnd; i++ )
179  if ( lit_var(*(i-1)) == lit_var(*i) )
180  {
181  printf( "The clause contains two literals of the same variable: %d and %d.\n", *(i-1), *i );
182  return 0;
183  }
184  // check the largest var size
185  p->nVars = STO_MAX( p->nVars, lit_var(*(pEnd-1)) + 1 );
186  }
187 
188  // get memory for the clause
189  nSize = sizeof(Sto_Cls_t) + sizeof(lit) * (pEnd - pBeg);
190  nSize = (nSize / sizeof(char*) + ((nSize % sizeof(char*)) > 0)) * sizeof(char*); // added by Saurabh on Sep 3, 2009
191  pClause = (Sto_Cls_t *)Sto_ManMemoryFetch( p, nSize );
192  memset( pClause, 0, sizeof(Sto_Cls_t) );
193 
194  // assign the clause
195  pClause->Id = p->nClauses++;
196  pClause->nLits = pEnd - pBeg;
197  memcpy( pClause->pLits, pBeg, sizeof(lit) * (pEnd - pBeg) );
198 // assert( pClause->pLits[0] >= 0 );
199 
200  // add the clause to the list
201  if ( p->pHead == NULL )
202  p->pHead = pClause;
203  if ( p->pTail == NULL )
204  p->pTail = pClause;
205  else
206  {
207  p->pTail->pNext = pClause;
208  p->pTail = pClause;
209  }
210 
211  // add the empty clause
212  if ( pClause->nLits == 0 )
213  {
214  if ( p->pEmpty )
215  {
216  printf( "More than one empty clause!\n" );
217  return 0;
218  }
219  p->pEmpty = pClause;
220  }
221  return 1;
222 }
char * memset()
Sto_Cls_t * pEmpty
Definition: satStore.h:91
int nVars
Definition: satStore.h:85
static int lit_var(lit l)
Definition: satVec.h:145
lit pLits[0]
Definition: satStore.h:78
char * memcpy()
ABC_NAMESPACE_IMPL_START char * Sto_ManMemoryFetch(Sto_Man_t *p, int nBytes)
DECLARATIONS ///.
Definition: satStore.c:50
unsigned nLits
Definition: satStore.h:77
int lit
Definition: satVec.h:130
struct Sto_Cls_t_ Sto_Cls_t
BASIC TYPES ///.
Definition: satStore.h:67
Sto_Cls_t * pHead
Definition: satStore.h:89
int Id
Definition: satStore.h:73
Sto_Cls_t * pTail
Definition: satStore.h:90
Sto_Cls_t * pNext
Definition: satStore.h:70
#define STO_MAX(a, b)
INCLUDES ///.
Definition: satStore.h:48
int nClauses
Definition: satStore.h:87
Sto_Man_t* Sto_ManAlloc ( )

MACRO DEFINITIONS ///.

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

Synopsis [Allocate proof manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 121 of file satStore.c.

122 {
123  Sto_Man_t * p;
124  // allocate the manager
125  p = (Sto_Man_t *)ABC_ALLOC( char, sizeof(Sto_Man_t) );
126  memset( p, 0, sizeof(Sto_Man_t) );
127  // memory management
128  p->nChunkSize = (1<<16); // use 64K chunks
129  return p;
130 }
char * memset()
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nChunkSize
Definition: satStore.h:93
int Sto_ManChangeLastClause ( Sto_Man_t p)

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

Synopsis [Returns the literal of the last clause.]

Description []

SideEffects []

SeeAlso []

Definition at line 279 of file satStore.c.

280 {
281  Sto_Cls_t * pClause, * pPrev;
282  pPrev = NULL;
283  Sto_ManForEachClause( p, pClause )
284  pPrev = pClause;
285  assert( pPrev != NULL );
286  assert( pPrev->fA == 1 );
287  assert( pPrev->nLits == 1 );
288  p->nClausesA--;
289  pPrev->fA = 0;
290  return pPrev->pLits[0] >> 1;
291 }
#define assert(ex)
Definition: util_old.h:213
#define Sto_ManForEachClause(p, pCls)
Definition: satStore.h:99
void Sto_ManDumpClauses ( Sto_Man_t p,
char *  pFileName 
)

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

Synopsis [Writes the stored clauses into a file.]

Description []

SideEffects []

SeeAlso []

Definition at line 305 of file satStore.c.

306 {
307  FILE * pFile;
308  Sto_Cls_t * pClause;
309  int i;
310  // start the file
311  pFile = fopen( pFileName, "w" );
312  if ( pFile == NULL )
313  {
314  printf( "Error: Cannot open output file (%s).\n", pFileName );
315  return;
316  }
317  // write the data
318  fprintf( pFile, "p %d %d %d %d\n", p->nVars, p->nClauses, p->nRoots, p->nClausesA );
319  Sto_ManForEachClause( p, pClause )
320  {
321  for ( i = 0; i < (int)pClause->nLits; i++ )
322  fprintf( pFile, " %d", lit_print(pClause->pLits[i]) );
323  fprintf( pFile, " 0\n" );
324  }
325 // fprintf( pFile, " 0\n" );
326  fclose( pFile );
327 }
int nVars
Definition: satStore.h:85
lit pLits[0]
Definition: satStore.h:78
int nClausesA
Definition: satStore.h:88
unsigned nLits
Definition: satStore.h:77
int nClauses
Definition: satStore.h:87
#define Sto_ManForEachClause(p, pCls)
Definition: satStore.h:99
int nRoots
Definition: satStore.h:86
static int lit_print(lit l)
Definition: satVec.h:147
void Sto_ManFree ( Sto_Man_t p)

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

Synopsis [Deallocate proof manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 143 of file satStore.c.

144 {
145  Sto_ManMemoryStop( p );
146  ABC_FREE( p );
147 }
void Sto_ManMemoryStop(Sto_Man_t *p)
Definition: satStore.c:76
#define ABC_FREE(obj)
Definition: abc_global.h:232
Sto_Man_t* Sto_ManLoadClauses ( char *  pFileName)

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

Synopsis [Reads CNF from file.]

Description []

SideEffects []

SeeAlso []

Definition at line 384 of file satStore.c.

385 {
386  FILE * pFile;
387  Sto_Man_t * p;
388  Sto_Cls_t * pClause;
389  char pBuffer[1024];
390  int nLits, nLitsAlloc, Counter, Number;
391  lit * pLits;
392 
393  // start the file
394  pFile = fopen( pFileName, "r" );
395  if ( pFile == NULL )
396  {
397  printf( "Error: Cannot open input file (%s).\n", pFileName );
398  return NULL;
399  }
400 
401  // create the manager
402  p = Sto_ManAlloc();
403 
404  // alloc the array of literals
405  nLitsAlloc = 1024;
406  pLits = (lit *)ABC_ALLOC( char, sizeof(lit) * nLitsAlloc );
407 
408  // read file header
409  p->nVars = p->nClauses = p->nRoots = p->nClausesA = 0;
410  while ( fgets( pBuffer, 1024, pFile ) )
411  {
412  if ( pBuffer[0] == 'c' )
413  continue;
414  if ( pBuffer[0] == 'p' )
415  {
416  sscanf( pBuffer + 1, "%d %d %d %d", &p->nVars, &p->nClauses, &p->nRoots, &p->nClausesA );
417  break;
418  }
419  printf( "Warning: Skipping line: \"%s\"\n", pBuffer );
420  }
421 
422  // read the clauses
423  nLits = 0;
424  while ( Sto_ManLoadNumber(pFile, &Number) )
425  {
426  if ( Number == 0 )
427  {
428  int RetValue;
429  RetValue = Sto_ManAddClause( p, pLits, pLits + nLits );
430  assert( RetValue );
431  nLits = 0;
432  continue;
433  }
434  if ( nLits == nLitsAlloc )
435  {
436  nLitsAlloc *= 2;
437  pLits = ABC_REALLOC( lit, pLits, nLitsAlloc );
438  }
439  pLits[ nLits++ ] = lit_read(Number);
440  }
441  if ( nLits > 0 )
442  printf( "Error: The last clause was not saved.\n" );
443 
444  // count clauses
445  Counter = 0;
446  Sto_ManForEachClause( p, pClause )
447  Counter++;
448 
449  // check the number of clauses
450  if ( p->nClauses != Counter )
451  {
452  printf( "Error: The actual number of clauses (%d) is different than declared (%d).\n", Counter, p->nClauses );
453  Sto_ManFree( p );
454  return NULL;
455  }
456 
457  ABC_FREE( pLits );
458  fclose( pFile );
459  return p;
460 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define ABC_REALLOC(type, obj, num)
Definition: abc_global.h:233
int nVars
Definition: satStore.h:85
int nClausesA
Definition: satStore.h:88
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int lit
Definition: satVec.h:130
static lit lit_read(int s)
Definition: satVec.h:148
Sto_Man_t * Sto_ManAlloc()
MACRO DEFINITIONS ///.
Definition: satStore.c:121
if(last==0)
Definition: sparse_int.h:34
static int Counter
void Sto_ManFree(Sto_Man_t *p)
Definition: satStore.c:143
#define ABC_FREE(obj)
Definition: abc_global.h:232
int Sto_ManAddClause(Sto_Man_t *p, lit *pBeg, lit *pEnd)
Definition: satStore.c:160
int nClauses
Definition: satStore.h:87
int Sto_ManLoadNumber(FILE *pFile, int *pNumber)
Definition: satStore.c:340
#define assert(ex)
Definition: util_old.h:213
#define Sto_ManForEachClause(p, pCls)
Definition: satStore.h:99
int nRoots
Definition: satStore.h:86
int Sto_ManLoadNumber ( FILE *  pFile,
int *  pNumber 
)

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

Synopsis [Reads one literal from file.]

Description []

SideEffects []

SeeAlso []

Definition at line 340 of file satStore.c.

341 {
342  int Char, Number = 0, Sign = 0;
343  // skip space-like chars
344  do {
345  Char = fgetc( pFile );
346  if ( Char == EOF )
347  return 0;
348  } while ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' );
349  // read the literal
350  while ( 1 )
351  {
352  // get the next character
353  Char = fgetc( pFile );
354  if ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' )
355  break;
356  // check that the char is a digit
357  if ( (Char < '0' || Char > '9') && Char != '-' )
358  {
359  printf( "Error: Wrong char (%c) in the input file.\n", Char );
360  return 0;
361  }
362  // check if this is a minus
363  if ( Char == '-' )
364  Sign = 1;
365  else
366  Number = 10 * Number + Char;
367  }
368  // return the number
369  *pNumber = Sign? -Number : Number;
370  return 1;
371 }
char Char
Definition: bzlib_private.h:43
void Sto_ManMarkClausesA ( Sto_Man_t p)

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

Synopsis [Mark all clauses added so far as clause of A.]

Description []

SideEffects []

SeeAlso []

Definition at line 257 of file satStore.c.

258 {
259  Sto_Cls_t * pClause;
260  p->nClausesA = 0;
261  Sto_ManForEachClause( p, pClause )
262  {
263  pClause->fA = 1;
264  p->nClausesA++;
265  }
266 }
int nClausesA
Definition: satStore.h:88
#define Sto_ManForEachClause(p, pCls)
Definition: satStore.h:99
unsigned fA
Definition: satStore.h:74
void Sto_ManMarkRoots ( Sto_Man_t p)

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

Synopsis [Mark all clauses added so far as root clauses.]

Description []

SideEffects []

SeeAlso []

Definition at line 235 of file satStore.c.

236 {
237  Sto_Cls_t * pClause;
238  p->nRoots = 0;
239  Sto_ManForEachClause( p, pClause )
240  {
241  pClause->fRoot = 1;
242  p->nRoots++;
243  }
244 }
unsigned fRoot
Definition: satStore.h:75
#define Sto_ManForEachClause(p, pCls)
Definition: satStore.h:99
int nRoots
Definition: satStore.h:86
ABC_NAMESPACE_IMPL_START char* Sto_ManMemoryFetch ( Sto_Man_t p,
int  nBytes 
)

DECLARATIONS ///.

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

FileName [satStore.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [SAT solver.]

Synopsis [Records the trace of SAT solving in the CNF form.]

Author [Alan Mishchenko]

Affiliation [UC Berkeley]

Date [Ver. 1.0. Started - June 20, 2005.]

Revision [

Id:
satStore.c,v 1.4 2005/09/16 22:55:03 casem Exp

]FUNCTION DEFINITIONS /// Function*************************************************************

Synopsis [Fetches memory.]

Description []

SideEffects []

SeeAlso []

Definition at line 50 of file satStore.c.

51 {
52  char * pMem;
53  if ( p->pChunkLast == NULL || nBytes > p->nChunkSize - p->nChunkUsed )
54  {
55  pMem = (char *)ABC_ALLOC( char, p->nChunkSize );
56  *(char **)pMem = p->pChunkLast;
57  p->pChunkLast = pMem;
58  p->nChunkUsed = sizeof(char *);
59  }
60  pMem = p->pChunkLast + p->nChunkUsed;
61  p->nChunkUsed += nBytes;
62  return pMem;
63 }
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
int nChunkSize
Definition: satStore.h:93
char * pChunkLast
Definition: satStore.h:95
int nChunkUsed
Definition: satStore.h:94
int Sto_ManMemoryReport ( Sto_Man_t p)

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

Synopsis [Reports memory usage in bytes.]

Description []

SideEffects []

SeeAlso []

Definition at line 97 of file satStore.c.

98 {
99  int Total;
100  char * pMem, * pNext;
101  if ( p->pChunkLast == NULL )
102  return 0;
103  Total = p->nChunkUsed;
104  for ( pMem = p->pChunkLast; (pNext = *(char **)pMem); pMem = pNext )
105  Total += p->nChunkSize;
106  return Total;
107 }
int nChunkSize
Definition: satStore.h:93
char * pChunkLast
Definition: satStore.h:95
int nChunkUsed
Definition: satStore.h:94
void Sto_ManMemoryStop ( Sto_Man_t p)

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

Synopsis [Frees memory manager.]

Description []

SideEffects []

SeeAlso []

Definition at line 76 of file satStore.c.

77 {
78  char * pMem, * pNext;
79  if ( p->pChunkLast == NULL )
80  return;
81  for ( pMem = p->pChunkLast; (pNext = *(char **)pMem); pMem = pNext )
82  ABC_FREE( pMem );
83  ABC_FREE( pMem );
84 }
char * pChunkLast
Definition: satStore.h:95
#define ABC_FREE(obj)
Definition: abc_global.h:232