00001
00019 #include "fxuInt.h"
00020
00024
00025 extern unsigned int Cudd_Prime( unsigned int p );
00026
00030
00042 Fxu_Matrix * Fxu_MatrixAllocate()
00043 {
00044 Fxu_Matrix * p;
00045 p = ALLOC( Fxu_Matrix, 1 );
00046 memset( p, 0, sizeof(Fxu_Matrix) );
00047 p->nTableSize = Cudd_Prime(10000);
00048 p->pTable = ALLOC( Fxu_ListDouble, p->nTableSize );
00049 memset( p->pTable, 0, sizeof(Fxu_ListDouble) * p->nTableSize );
00050 #ifndef USE_SYSTEM_MEMORY_MANAGEMENT
00051 {
00052
00053
00054
00055 int nSizeMax, nSizeCur;
00056 nSizeMax = -1;
00057 nSizeCur = sizeof(Fxu_Cube);
00058 if ( nSizeMax < nSizeCur )
00059 nSizeMax = nSizeCur;
00060 nSizeCur = sizeof(Fxu_Var);
00061 if ( nSizeMax < nSizeCur )
00062 nSizeMax = nSizeCur;
00063 nSizeCur = sizeof(Fxu_Lit);
00064 if ( nSizeMax < nSizeCur )
00065 nSizeMax = nSizeCur;
00066 nSizeCur = sizeof(Fxu_Pair);
00067 if ( nSizeMax < nSizeCur )
00068 nSizeMax = nSizeCur;
00069 nSizeCur = sizeof(Fxu_Double);
00070 if ( nSizeMax < nSizeCur )
00071 nSizeMax = nSizeCur;
00072 nSizeCur = sizeof(Fxu_Single);
00073 if ( nSizeMax < nSizeCur )
00074 nSizeMax = nSizeCur;
00075 p->pMemMan = Extra_MmFixedStart( nSizeMax );
00076 }
00077 #endif
00078 p->pHeapDouble = Fxu_HeapDoubleStart();
00079 p->pHeapSingle = Fxu_HeapSingleStart();
00080 p->vPairs = Vec_PtrAlloc( 100 );
00081 return p;
00082 }
00083
00095 void Fxu_MatrixDelete( Fxu_Matrix * p )
00096 {
00097 Fxu_HeapDoubleCheck( p->pHeapDouble );
00098 Fxu_HeapDoubleStop( p->pHeapDouble );
00099 Fxu_HeapSingleStop( p->pHeapSingle );
00100
00101
00102 #ifdef USE_SYSTEM_MEMORY_MANAGEMENT
00103
00104 {
00105 Fxu_Cube * pCube, * pCube2;
00106 Fxu_Var * pVar, * pVar2;
00107 Fxu_Lit * pLit, * pLit2;
00108 Fxu_Double * pDiv, * pDiv2;
00109 Fxu_Single * pSingle, * pSingle2;
00110 Fxu_Pair * pPair, * pPair2;
00111 int i;
00112
00113 Fxu_MatrixForEachDoubleSafe( p, pDiv, pDiv2, i )
00114 {
00115 Fxu_DoubleForEachPairSafe( pDiv, pPair, pPair2 )
00116 MEM_FREE_FXU( p, Fxu_Pair, 1, pPair );
00117 MEM_FREE_FXU( p, Fxu_Double, 1, pDiv );
00118 }
00119 Fxu_MatrixForEachSingleSafe( p, pSingle, pSingle2 )
00120 MEM_FREE_FXU( p, Fxu_Single, 1, pSingle );
00121
00122 Fxu_MatrixForEachCube( p, pCube )
00123 Fxu_CubeForEachLiteralSafe( pCube, pLit, pLit2 )
00124 MEM_FREE_FXU( p, Fxu_Lit, 1, pLit );
00125
00126 Fxu_MatrixForEachCubeSafe( p, pCube, pCube2 )
00127 MEM_FREE_FXU( p, Fxu_Cube, 1, pCube );
00128
00129 Fxu_MatrixForEachVariableSafe( p, pVar, pVar2 )
00130 MEM_FREE_FXU( p, Fxu_Var, 1, pVar );
00131 }
00132 #else
00133 Extra_MmFixedStop( p->pMemMan );
00134 #endif
00135
00136 Vec_PtrFree( p->vPairs );
00137 FREE( p->pppPairs );
00138 FREE( p->ppPairs );
00139
00140 FREE( p->pTable );
00141 FREE( p->ppVars );
00142 FREE( p );
00143 }
00144
00145
00146
00160 Fxu_Var * Fxu_MatrixAddVar( Fxu_Matrix * p )
00161 {
00162 Fxu_Var * pVar;
00163 pVar = MEM_ALLOC_FXU( p, Fxu_Var, 1 );
00164 memset( pVar, 0, sizeof(Fxu_Var) );
00165 pVar->iVar = p->lVars.nItems;
00166 p->ppVars[pVar->iVar] = pVar;
00167 Fxu_ListMatrixAddVariable( p, pVar );
00168 return pVar;
00169 }
00170
00182 Fxu_Cube * Fxu_MatrixAddCube( Fxu_Matrix * p, Fxu_Var * pVar, int iCube )
00183 {
00184 Fxu_Cube * pCube;
00185 pCube = MEM_ALLOC_FXU( p, Fxu_Cube, 1 );
00186 memset( pCube, 0, sizeof(Fxu_Cube) );
00187 pCube->pVar = pVar;
00188 pCube->iCube = iCube;
00189 Fxu_ListMatrixAddCube( p, pCube );
00190 return pCube;
00191 }
00192
00204 void Fxu_MatrixAddLiteral( Fxu_Matrix * p, Fxu_Cube * pCube, Fxu_Var * pVar )
00205 {
00206 Fxu_Lit * pLit;
00207 pLit = MEM_ALLOC_FXU( p, Fxu_Lit, 1 );
00208 memset( pLit, 0, sizeof(Fxu_Lit) );
00209
00210 Fxu_ListCubeAddLiteral( pCube, pLit );
00211 Fxu_ListVarAddLiteral( pVar, pLit );
00212
00213 pLit->pCube = pCube;
00214 pLit->pVar = pVar;
00215 pLit->iCube = pCube->iCube;
00216 pLit->iVar = pVar->iVar;
00217
00218 p->nEntries++;
00219 }
00220
00232 void Fxu_MatrixDelDivisor( Fxu_Matrix * p, Fxu_Double * pDiv )
00233 {
00234
00235 Fxu_ListTableDelDivisor( p, pDiv );
00236
00237 MEM_FREE_FXU( p, Fxu_Double, 1, pDiv );
00238 }
00239
00251 void Fxu_MatrixDelLiteral( Fxu_Matrix * p, Fxu_Lit * pLit )
00252 {
00253
00254 Fxu_ListCubeDelLiteral( pLit->pCube, pLit );
00255 Fxu_ListVarDelLiteral( pLit->pVar, pLit );
00256 MEM_FREE_FXU( p, Fxu_Lit, 1, pLit );
00257
00258 p->nEntries--;
00259 }
00260
00261
00273 void Fxu_MatrixAddSingle( Fxu_Matrix * p, Fxu_Var * pVar1, Fxu_Var * pVar2, int Weight )
00274 {
00275 Fxu_Single * pSingle;
00276 assert( pVar1->iVar < pVar2->iVar );
00277 pSingle = MEM_ALLOC_FXU( p, Fxu_Single, 1 );
00278 memset( pSingle, 0, sizeof(Fxu_Single) );
00279 pSingle->Num = p->lSingles.nItems;
00280 pSingle->Weight = Weight;
00281 pSingle->HNum = 0;
00282 pSingle->pVar1 = pVar1;
00283 pSingle->pVar2 = pVar2;
00284 Fxu_ListMatrixAddSingle( p, pSingle );
00285
00286 Fxu_HeapSingleInsert( p->pHeapSingle, pSingle );
00287 }
00288
00300 void Fxu_MatrixAddDivisor( Fxu_Matrix * p, Fxu_Cube * pCube1, Fxu_Cube * pCube2 )
00301 {
00302 Fxu_Pair * pPair;
00303 Fxu_Double * pDiv;
00304 int nBase, nLits1, nLits2;
00305 int fFound;
00306 unsigned Key;
00307
00308
00309 Fxu_PairCanonicize( &pCube1, &pCube2 );
00310
00311 Key = Fxu_PairHashKey( p, pCube1, pCube2, &nBase, &nLits1, &nLits2 );
00312
00313
00314 pPair = Fxu_PairAlloc( p, pCube1, pCube2 );
00315 pPair->nBase = nBase;
00316 pPair->nLits1 = nLits1;
00317 pPair->nLits2 = nLits2;
00318
00319
00320 fFound = 0;
00321 Key %= p->nTableSize;
00322 Fxu_TableForEachDouble( p, Key, pDiv )
00323 {
00324 if ( Fxu_PairCompare( pPair, pDiv->lPairs.pTail ) )
00325 {
00326 fFound = 1;
00327 break;
00328 }
00329 }
00330
00331 if ( !fFound )
00332 {
00333 pDiv = MEM_ALLOC_FXU( p, Fxu_Double, 1 );
00334 memset( pDiv, 0, sizeof(Fxu_Double) );
00335 pDiv->Key = Key;
00336
00337 pDiv->Num = p->nDivsTotal++;
00338
00339 Fxu_ListTableAddDivisor( p, pDiv );
00340
00341 pDiv->Weight -= pPair->nLits1 + pPair->nLits2;
00342 }
00343
00344
00345 Fxu_PairAdd( pPair );
00346
00347 pPair->pDiv = pDiv;
00348 Fxu_ListDoubleAddPairLast( pDiv, pPair );
00349
00350
00351
00352
00353 pDiv->Weight += pPair->nLits1 + pPair->nLits2 - 1 + pPair->nBase;
00354 if ( fFound )
00355 Fxu_HeapDoubleUpdate( p->pHeapDouble, pDiv );
00356 else
00357 Fxu_HeapDoubleInsert( p->pHeapDouble, pDiv );
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00373
00374