ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
satStore.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
26#include "satStore.h"
27
29
30
34
38
50char * Sto_ManMemoryFetch( Sto_Man_t * p, int nBytes )
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}
64
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}
85
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}
108
109
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}
131
144{
146 ABC_FREE( p );
147}
148
160int Sto_ManAddClause( Sto_Man_t * p, lit * pBeg, lit * pEnd )
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}
223
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}
245
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}
267
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}
292
293
305void Sto_ManDumpClauses( Sto_Man_t * p, char * pFileName )
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}
328
340int Sto_ManLoadNumber( FILE * pFile, int * pNumber )
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}
372
384Sto_Man_t * Sto_ManLoadClauses( char * pFileName )
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}
461
462
466
467
469
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_REALLOC(type, obj, num)
Definition abc_global.h:268
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
char Char
Cube * p
Definition exorList.c:222
void Sto_ManMemoryStop(Sto_Man_t *p)
Definition satStore.c:76
void Sto_ManMarkClausesA(Sto_Man_t *p)
Definition satStore.c:257
int Sto_ManMemoryReport(Sto_Man_t *p)
Definition satStore.c:97
int Sto_ManLoadNumber(FILE *pFile, int *pNumber)
Definition satStore.c:340
Sto_Man_t * Sto_ManAlloc()
MACRO DEFINITIONS ///.
Definition satStore.c:121
void Sto_ManDumpClauses(Sto_Man_t *p, char *pFileName)
Definition satStore.c:305
Sto_Man_t * Sto_ManLoadClauses(char *pFileName)
Definition satStore.c:384
int Sto_ManAddClause(Sto_Man_t *p, lit *pBeg, lit *pEnd)
Definition satStore.c:160
int Sto_ManChangeLastClause(Sto_Man_t *p)
Definition satStore.c:279
void Sto_ManFree(Sto_Man_t *p)
Definition satStore.c:143
void Sto_ManMarkRoots(Sto_Man_t *p)
Definition satStore.c:235
ABC_NAMESPACE_IMPL_START char * Sto_ManMemoryFetch(Sto_Man_t *p, int nBytes)
DECLARATIONS ///.
Definition satStore.c:50
#define Sto_ManForEachClause(p, pCls)
Definition satStore.h:99
#define STO_MAX(a, b)
INCLUDES ///.
Definition satStore.h:48
struct Sto_Man_t_ Sto_Man_t
Definition satStore.h:81
struct Sto_Cls_t_ Sto_Cls_t
BASIC TYPES ///.
Definition satStore.h:67
int lit
Definition satVec.h:130
lit pLits[0]
Definition satStore.h:78
unsigned fA
Definition satStore.h:74
unsigned nLits
Definition satStore.h:77
unsigned fRoot
Definition satStore.h:75
#define assert(ex)
Definition util_old.h:213
char * memcpy()
char * memset()