00001
00021 #include "msatInt.h"
00022
00026
00027
00028 struct Msat_Order_t_
00029 {
00030 Msat_Solver_t * pSat;
00031 Msat_IntVec_t * vIndex;
00032 Msat_IntVec_t * vHeap;
00033 };
00034
00035
00036
00037
00038
00039
00040
00041 #define HLEFT(i) ((i)<<1)
00042 #define HRIGHT(i) (((i)<<1)+1)
00043 #define HPARENT(i) ((i)>>1)
00044 #define HCOMPARE(p, i, j) ((p)->pSat->pdActivity[i] > (p)->pSat->pdActivity[j])
00045 #define HHEAP(p, i) ((p)->vHeap->pArray[i])
00046 #define HSIZE(p) ((p)->vHeap->nSize)
00047 #define HOKAY(p, i) ((i) >= 0 && (i) < (p)->vIndex->nSize)
00048 #define HINHEAP(p, i) (HOKAY(p, i) && (p)->vIndex->pArray[i] != 0)
00049 #define HEMPTY(p) (HSIZE(p) == 1)
00050
00051 static int Msat_HeapCheck_rec( Msat_Order_t * p, int i );
00052 static int Msat_HeapGetTop( Msat_Order_t * p );
00053 static void Msat_HeapInsert( Msat_Order_t * p, int n );
00054 static void Msat_HeapIncrease( Msat_Order_t * p, int n );
00055 static void Msat_HeapPercolateUp( Msat_Order_t * p, int i );
00056 static void Msat_HeapPercolateDown( Msat_Order_t * p, int i );
00057
00058 extern int timeSelect;
00059
00063
00075 Msat_Order_t * Msat_OrderAlloc( Msat_Solver_t * pSat )
00076 {
00077 Msat_Order_t * p;
00078 p = ALLOC( Msat_Order_t, 1 );
00079 memset( p, 0, sizeof(Msat_Order_t) );
00080 p->pSat = pSat;
00081 p->vIndex = Msat_IntVecAlloc( 0 );
00082 p->vHeap = Msat_IntVecAlloc( 0 );
00083 Msat_OrderSetBounds( p, pSat->nVarsAlloc );
00084 return p;
00085 }
00086
00098 void Msat_OrderSetBounds( Msat_Order_t * p, int nVarsMax )
00099 {
00100 Msat_IntVecGrow( p->vIndex, nVarsMax );
00101 Msat_IntVecGrow( p->vHeap, nVarsMax + 1 );
00102 p->vIndex->nSize = nVarsMax;
00103 p->vHeap->nSize = 0;
00104 }
00105
00117 void Msat_OrderClean( Msat_Order_t * p, Msat_IntVec_t * vCone )
00118 {
00119 int i;
00120 for ( i = 0; i < p->vIndex->nSize; i++ )
00121 p->vIndex->pArray[i] = 0;
00122 for ( i = 0; i < vCone->nSize; i++ )
00123 {
00124 assert( i+1 < p->vHeap->nCap );
00125 p->vHeap->pArray[i+1] = vCone->pArray[i];
00126
00127 assert( vCone->pArray[i] < p->vIndex->nSize );
00128 p->vIndex->pArray[vCone->pArray[i]] = i+1;
00129 }
00130 p->vHeap->nSize = vCone->nSize + 1;
00131 }
00132
00144 int Msat_OrderCheck( Msat_Order_t * p )
00145 {
00146 return Msat_HeapCheck_rec( p, 1 );
00147 }
00148
00160 void Msat_OrderFree( Msat_Order_t * p )
00161 {
00162 Msat_IntVecFree( p->vHeap );
00163 Msat_IntVecFree( p->vIndex );
00164 free( p );
00165 }
00166
00167
00168
00180 int Msat_OrderVarSelect( Msat_Order_t * p )
00181 {
00182
00183
00184
00185
00186
00187
00188
00189
00190 int Var;
00191 int clk = clock();
00192
00193 while ( !HEMPTY(p) )
00194 {
00195 Var = Msat_HeapGetTop(p);
00196 if ( (p)->pSat->pAssigns[Var] == MSAT_VAR_UNASSIGNED )
00197 {
00198
00199 timeSelect += clock() - clk;
00200 return Var;
00201 }
00202 }
00203 return MSAT_ORDER_UNKNOWN;
00204 }
00205
00217 void Msat_OrderVarAssigned( Msat_Order_t * p, int Var )
00218 {
00219 }
00220
00232 void Msat_OrderVarUnassigned( Msat_Order_t * p, int Var )
00233 {
00234
00235
00236
00237 int clk = clock();
00238 if ( !HINHEAP(p,Var) )
00239 Msat_HeapInsert( p, Var );
00240 timeSelect += clock() - clk;
00241 }
00242
00254 void Msat_OrderUpdate( Msat_Order_t * p, int Var )
00255 {
00256
00257
00258
00259 int clk = clock();
00260 if ( HINHEAP(p,Var) )
00261 Msat_HeapIncrease( p, Var );
00262 timeSelect += clock() - clk;
00263 }
00264
00265
00266
00267
00279 int Msat_HeapCheck_rec( Msat_Order_t * p, int i )
00280 {
00281 return i >= HSIZE(p) ||
00282 ( HPARENT(i) == 0 || !HCOMPARE(p, HHEAP(p, i), HHEAP(p, HPARENT(i))) ) &&
00283 Msat_HeapCheck_rec( p, HLEFT(i) ) && Msat_HeapCheck_rec( p, HRIGHT(i) );
00284 }
00285
00297 int Msat_HeapGetTop( Msat_Order_t * p )
00298 {
00299 int Result, NewTop;
00300 Result = HHEAP(p, 1);
00301 NewTop = Msat_IntVecPop( p->vHeap );
00302 p->vHeap->pArray[1] = NewTop;
00303 p->vIndex->pArray[NewTop] = 1;
00304 p->vIndex->pArray[Result] = 0;
00305 if ( p->vHeap->nSize > 1 )
00306 Msat_HeapPercolateDown( p, 1 );
00307 return Result;
00308 }
00309
00321 void Msat_HeapInsert( Msat_Order_t * p, int n )
00322 {
00323 assert( HOKAY(p, n) );
00324 p->vIndex->pArray[n] = HSIZE(p);
00325 Msat_IntVecPush( p->vHeap, n );
00326 Msat_HeapPercolateUp( p, p->vIndex->pArray[n] );
00327 }
00328
00340 void Msat_HeapIncrease( Msat_Order_t * p, int n )
00341 {
00342 Msat_HeapPercolateUp( p, p->vIndex->pArray[n] );
00343 }
00344
00356 void Msat_HeapPercolateUp( Msat_Order_t * p, int i )
00357 {
00358 int x = HHEAP(p, i);
00359 while ( HPARENT(i) != 0 && HCOMPARE(p, x, HHEAP(p, HPARENT(i))) )
00360 {
00361 p->vHeap->pArray[i] = HHEAP(p, HPARENT(i));
00362 p->vIndex->pArray[HHEAP(p, i)] = i;
00363 i = HPARENT(i);
00364 }
00365 p->vHeap->pArray[i] = x;
00366 p->vIndex->pArray[x] = i;
00367 }
00368
00380 void Msat_HeapPercolateDown( Msat_Order_t * p, int i )
00381 {
00382 int x = HHEAP(p, i);
00383 int Child;
00384 while ( HLEFT(i) < HSIZE(p) )
00385 {
00386 if ( HRIGHT(i) < HSIZE(p) && HCOMPARE(p, HHEAP(p, HRIGHT(i)), HHEAP(p, HLEFT(i))) )
00387 Child = HRIGHT(i);
00388 else
00389 Child = HLEFT(i);
00390 if ( !HCOMPARE(p, HHEAP(p, Child), x) )
00391 break;
00392 p->vHeap->pArray[i] = HHEAP(p, Child);
00393 p->vIndex->pArray[HHEAP(p, i)] = i;
00394 i = Child;
00395 }
00396 p->vHeap->pArray[i] = x;
00397 p->vIndex->pArray[x] = i;
00398 }
00399
00400
00404
00405