00001
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <assert.h>
00025 #include <time.h>
00026 #include "satStore.h"
00027
00031
00035
00047 char * Sto_ManMemoryFetch( Sto_Man_t * p, int nBytes )
00048 {
00049 char * pMem;
00050 if ( p->pChunkLast == NULL || nBytes > p->nChunkSize - p->nChunkUsed )
00051 {
00052 pMem = (char *)malloc( p->nChunkSize );
00053 *(char **)pMem = p->pChunkLast;
00054 p->pChunkLast = pMem;
00055 p->nChunkUsed = sizeof(char *);
00056 }
00057 pMem = p->pChunkLast + p->nChunkUsed;
00058 p->nChunkUsed += nBytes;
00059 return pMem;
00060 }
00061
00073 void Sto_ManMemoryStop( Sto_Man_t * p )
00074 {
00075 char * pMem, * pNext;
00076 if ( p->pChunkLast == NULL )
00077 return;
00078 for ( pMem = p->pChunkLast; pNext = *(char **)pMem; pMem = pNext )
00079 free( pMem );
00080 free( pMem );
00081 }
00082
00094 int Sto_ManMemoryReport( Sto_Man_t * p )
00095 {
00096 int Total;
00097 char * pMem, * pNext;
00098 if ( p->pChunkLast == NULL )
00099 return 0;
00100 Total = p->nChunkUsed;
00101 for ( pMem = p->pChunkLast; pNext = *(char **)pMem; pMem = pNext )
00102 Total += p->nChunkSize;
00103 return Total;
00104 }
00105
00106
00118 Sto_Man_t * Sto_ManAlloc()
00119 {
00120 Sto_Man_t * p;
00121
00122 p = (Sto_Man_t *)malloc( sizeof(Sto_Man_t) );
00123 memset( p, 0, sizeof(Sto_Man_t) );
00124
00125 p->nChunkSize = (1<<16);
00126 return p;
00127 }
00128
00140 void Sto_ManFree( Sto_Man_t * p )
00141 {
00142 Sto_ManMemoryStop( p );
00143 free( p );
00144 }
00145
00157 int Sto_ManAddClause( Sto_Man_t * p, lit * pBeg, lit * pEnd )
00158 {
00159 Sto_Cls_t * pClause;
00160 lit Lit, * i, * j;
00161 int nSize;
00162
00163
00164 if ( pBeg < pEnd )
00165 {
00166
00167 for ( i = pBeg + 1; i < pEnd; i++ )
00168 {
00169 Lit = *i;
00170 for ( j = i; j > pBeg && *(j-1) > Lit; j-- )
00171 *j = *(j-1);
00172 *j = Lit;
00173 }
00174
00175 for ( i = pBeg + 1; i < pEnd; i++ )
00176 if ( lit_var(*(i-1)) == lit_var(*i) )
00177 {
00178 printf( "The clause contains two literals of the same variable: %d and %d.\n", *(i-1), *i );
00179 return 0;
00180 }
00181
00182 p->nVars = STO_MAX( p->nVars, lit_var(*(pEnd-1)) + 1 );
00183 }
00184
00185
00186 nSize = sizeof(Sto_Cls_t) + sizeof(lit) * (pEnd - pBeg);
00187 pClause = (Sto_Cls_t *)Sto_ManMemoryFetch( p, nSize );
00188 memset( pClause, 0, sizeof(Sto_Cls_t) );
00189
00190
00191 pClause->Id = p->nClauses++;
00192 pClause->nLits = pEnd - pBeg;
00193 memcpy( pClause->pLits, pBeg, sizeof(lit) * (pEnd - pBeg) );
00194
00195
00196 if ( p->pHead == NULL )
00197 p->pHead = pClause;
00198 if ( p->pTail == NULL )
00199 p->pTail = pClause;
00200 else
00201 {
00202 p->pTail->pNext = pClause;
00203 p->pTail = pClause;
00204 }
00205
00206
00207 if ( pClause->nLits == 0 )
00208 {
00209 if ( p->pEmpty )
00210 {
00211 printf( "More than one empty clause!\n" );
00212 return 0;
00213 }
00214 p->pEmpty = pClause;
00215 }
00216 return 1;
00217 }
00218
00230 void Sto_ManMarkRoots( Sto_Man_t * p )
00231 {
00232 Sto_Cls_t * pClause;
00233 p->nRoots = 0;
00234 Sto_ManForEachClause( p, pClause )
00235 {
00236 pClause->fRoot = 1;
00237 p->nRoots++;
00238 }
00239 }
00240
00252 void Sto_ManMarkClausesA( Sto_Man_t * p )
00253 {
00254 Sto_Cls_t * pClause;
00255 p->nClausesA = 0;
00256 Sto_ManForEachClause( p, pClause )
00257 {
00258 pClause->fA = 1;
00259 p->nClausesA++;
00260 }
00261 }
00262
00263
00275 void Sto_ManDumpClauses( Sto_Man_t * p, char * pFileName )
00276 {
00277 FILE * pFile;
00278 Sto_Cls_t * pClause;
00279 int i;
00280
00281 pFile = fopen( pFileName, "w" );
00282 if ( pFile == NULL )
00283 {
00284 printf( "Error: Cannot open output file (%s).\n", pFileName );
00285 return;
00286 }
00287
00288 fprintf( pFile, "p %d %d %d %d\n", p->nVars, p->nClauses, p->nRoots, p->nClausesA );
00289 Sto_ManForEachClause( p, pClause )
00290 {
00291 for ( i = 0; i < (int)pClause->nLits; i++ )
00292 fprintf( pFile, " %d", lit_print(pClause->pLits[i]) );
00293 fprintf( pFile, "\n" );
00294 }
00295 fprintf( pFile, " 0\n" );
00296 fclose( pFile );
00297 }
00298
00310 int Sto_ManLoadNumber( FILE * pFile, int * pNumber )
00311 {
00312 int Char, Number = 0, Sign = 0;
00313
00314 do {
00315 Char = fgetc( pFile );
00316 if ( Char == EOF )
00317 return 0;
00318 } while ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' );
00319
00320 while ( 1 )
00321 {
00322
00323 Char = fgetc( pFile );
00324 if ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' )
00325 break;
00326
00327 if ( (Char < '0' || Char > '9') && Char != '-' )
00328 {
00329 printf( "Error: Wrong char (%c) in the input file.\n", Char );
00330 return 0;
00331 }
00332
00333 if ( Char == '-' )
00334 Sign = 1;
00335 else
00336 Number = 10 * Number + Char;
00337 }
00338
00339 *pNumber = Sign? -Number : Number;
00340 return 1;
00341 }
00342
00354 Sto_Man_t * Sto_ManLoadClauses( char * pFileName )
00355 {
00356 FILE * pFile;
00357 Sto_Man_t * p;
00358 Sto_Cls_t * pClause;
00359 char pBuffer[1024];
00360 int nLits, nLitsAlloc, Counter, Number;
00361 lit * pLits;
00362
00363
00364 pFile = fopen( pFileName, "r" );
00365 if ( pFile == NULL )
00366 {
00367 printf( "Error: Cannot open input file (%s).\n", pFileName );
00368 return NULL;
00369 }
00370
00371
00372 p = Sto_ManAlloc();
00373
00374
00375 nLitsAlloc = 1024;
00376 pLits = (lit *)malloc( sizeof(lit) * nLitsAlloc );
00377
00378
00379 p->nVars = p->nClauses = p->nRoots = p->nClausesA = 0;
00380 while ( fgets( pBuffer, 1024, pFile ) )
00381 {
00382 if ( pBuffer[0] == 'c' )
00383 continue;
00384 if ( pBuffer[0] == 'p' )
00385 {
00386 sscanf( pBuffer + 1, "%d %d %d %d", p->nVars, p->nClauses, p->nRoots, p->nClausesA );
00387 break;
00388 }
00389 printf( "Warning: Skipping line: \"%s\"\n", pBuffer );
00390 }
00391
00392
00393 nLits = 0;
00394 while ( Sto_ManLoadNumber(pFile, &Number) )
00395 {
00396 if ( Number == 0 )
00397 {
00398 int RetValue;
00399 RetValue = Sto_ManAddClause( p, pLits, pLits + nLits );
00400 assert( RetValue );
00401 nLits = 0;
00402 continue;
00403 }
00404 if ( nLits == nLitsAlloc )
00405 {
00406 nLitsAlloc *= 2;
00407 pLits = (lit *)realloc( pLits, sizeof(lit) * nLitsAlloc );
00408 }
00409 pLits[ nLits++ ] = lit_read(Number);
00410 }
00411 if ( nLits > 0 )
00412 printf( "Error: The last clause was not saved.\n" );
00413
00414
00415 Counter = 0;
00416 Sto_ManForEachClause( p, pClause )
00417 Counter++;
00418
00419
00420 if ( p->nClauses != Counter )
00421 {
00422 printf( "Error: The actual number of clauses (%d) is different than declared (%d).\n", Counter, p->nClauses );
00423 Sto_ManFree( p );
00424 return NULL;
00425 }
00426
00427 free( pLits );
00428 fclose( pFile );
00429 return p;
00430 }
00431
00432
00436
00437