00001
00019 #include "mvc.h"
00020
00024
00028
00040 int Mvc_CoverAnyLiteral( Mvc_Cover_t * pCover, Mvc_Cube_t * pMask )
00041 {
00042 Mvc_Cube_t * pCube;
00043 int nWord, nBit, i;
00044 int nLitsCur;
00045 int fUseFirst = 0;
00046
00047
00048 if ( fUseFirst )
00049 {
00050 for ( i = 0; i < pCover->nBits; i++ )
00051 if ( !pMask || Mvc_CubeBitValue(pMask,i) )
00052 {
00053
00054 nWord = Mvc_CubeWhichWord(i);
00055 nBit = Mvc_CubeWhichBit(i);
00056
00057 nLitsCur = 0;
00058 Mvc_CoverForEachCube( pCover, pCube )
00059 if ( pCube->pData[nWord] & (1<<nBit) )
00060 {
00061 nLitsCur++;
00062 if ( nLitsCur > 1 )
00063 return i;
00064 }
00065 }
00066 }
00067 else
00068 {
00069 for ( i = pCover->nBits - 1; i >=0; i-- )
00070 if ( !pMask || Mvc_CubeBitValue(pMask,i) )
00071 {
00072
00073 nWord = Mvc_CubeWhichWord(i);
00074 nBit = Mvc_CubeWhichBit(i);
00075
00076 nLitsCur = 0;
00077 Mvc_CoverForEachCube( pCover, pCube )
00078 if ( pCube->pData[nWord] & (1<<nBit) )
00079 {
00080 nLitsCur++;
00081 if ( nLitsCur > 1 )
00082 return i;
00083 }
00084 }
00085 }
00086 return -1;
00087 }
00088
00101 int Mvc_CoverBestLiteral( Mvc_Cover_t * pCover, Mvc_Cube_t * pMask )
00102 {
00103 Mvc_Cube_t * pCube;
00104 int nWord, nBit;
00105 int i, iMax, nLitsMax, nLitsCur;
00106 int fUseFirst = 1;
00107
00108
00109 iMax = -1;
00110 nLitsMax = -1;
00111 for ( i = 0; i < pCover->nBits; i++ )
00112 if ( !pMask || Mvc_CubeBitValue(pMask,i) )
00113 {
00114
00115 nWord = Mvc_CubeWhichWord(i);
00116 nBit = Mvc_CubeWhichBit(i);
00117
00118 nLitsCur = 0;
00119 Mvc_CoverForEachCube( pCover, pCube )
00120 if ( pCube->pData[nWord] & (1<<nBit) )
00121 nLitsCur++;
00122
00123
00124 if ( fUseFirst )
00125 {
00126 if ( nLitsMax < nLitsCur )
00127 {
00128 nLitsMax = nLitsCur;
00129 iMax = i;
00130 }
00131 }
00132 else
00133 {
00134 if ( nLitsMax <= nLitsCur )
00135 {
00136 nLitsMax = nLitsCur;
00137 iMax = i;
00138 }
00139 }
00140 }
00141
00142 if ( nLitsMax > 1 )
00143 return iMax;
00144 return -1;
00145 }
00146
00159 int Mvc_CoverWorstLiteral( Mvc_Cover_t * pCover, Mvc_Cube_t * pMask )
00160 {
00161 Mvc_Cube_t * pCube;
00162 int nWord, nBit;
00163 int i, iMin, nLitsMin, nLitsCur;
00164 int fUseFirst = 1;
00165
00166
00167 iMin = -1;
00168 nLitsMin = 1000000;
00169 for ( i = 0; i < pCover->nBits; i++ )
00170 if ( !pMask || Mvc_CubeBitValue(pMask,i) )
00171 {
00172
00173 nWord = Mvc_CubeWhichWord(i);
00174 nBit = Mvc_CubeWhichBit(i);
00175
00176 nLitsCur = 0;
00177 Mvc_CoverForEachCube( pCover, pCube )
00178 if ( pCube->pData[nWord] & (1<<nBit) )
00179 nLitsCur++;
00180
00181
00182 if ( nLitsCur < 2 )
00183 continue;
00184
00185
00186 if ( fUseFirst )
00187 {
00188 if ( nLitsMin > nLitsCur )
00189 {
00190 nLitsMin = nLitsCur;
00191 iMin = i;
00192 }
00193 }
00194 else
00195 {
00196 if ( nLitsMin >= nLitsCur )
00197 {
00198 nLitsMin = nLitsCur;
00199 iMin = i;
00200 }
00201 }
00202 }
00203
00204 if ( nLitsMin < 1000000 )
00205 return iMin;
00206 return -1;
00207 }
00208
00220 Mvc_Cover_t * Mvc_CoverBestLiteralCover( Mvc_Cover_t * pCover, Mvc_Cover_t * pSimple )
00221 {
00222 Mvc_Cover_t * pCoverNew;
00223 Mvc_Cube_t * pCubeNew;
00224 Mvc_Cube_t * pCubeS;
00225 int iLitBest;
00226
00227
00228 pCoverNew = Mvc_CoverClone( pCover );
00229
00230 pCubeNew = Mvc_CubeAlloc( pCoverNew );
00231
00232 Mvc_CubeBitClean( pCubeNew );
00233
00234
00235 assert( Mvc_CoverReadCubeNum(pSimple) == 1 );
00236 pCubeS = Mvc_CoverReadCubeHead( pSimple );
00237
00238 iLitBest = Mvc_CoverBestLiteral( pCover, pCubeS );
00239
00240
00241 Mvc_CubeBitInsert( pCubeNew, iLitBest );
00242
00243 Mvc_CoverAddCubeTail( pCoverNew, pCubeNew );
00244 return pCoverNew;
00245 }
00246
00258 int Mvc_CoverFirstCubeFirstLit( Mvc_Cover_t * pCover )
00259 {
00260 Mvc_Cube_t * pCube;
00261 int iBit, Value;
00262
00263
00264 pCube = Mvc_CoverReadCubeHead( pCover );
00265
00266 Mvc_CubeForEachBit( pCover, pCube, iBit, Value )
00267 if ( Value )
00268 return iBit;
00269 return -1;
00270 }
00271
00284 int Mvc_CoverCountLiterals( Mvc_Cover_t * pCover )
00285 {
00286 Mvc_Cube_t * pCube;
00287 int nWord, nBit;
00288 int i, CounterTot, CounterCur;
00289
00290
00291
00292
00293
00294 CounterTot = 0;
00295 for ( i = 0; i < pCover->nBits; i++ )
00296 {
00297
00298 nWord = Mvc_CubeWhichWord(i);
00299 nBit = Mvc_CubeWhichBit(i);
00300
00301 CounterCur = 0;
00302 Mvc_CoverForEachCube( pCover, pCube )
00303 if ( pCube->pData[nWord] & (1<<nBit) )
00304 CounterCur++;
00305 CounterTot += CounterCur;
00306 }
00307 return CounterTot;
00308 }
00309
00321 int Mvc_CoverIsOneLiteral( Mvc_Cover_t * pCover )
00322 {
00323 Mvc_Cube_t * pCube;
00324 int iBit, Counter, Value;
00325 if ( Mvc_CoverReadCubeNum(pCover) != 1 )
00326 return 0;
00327 pCube = Mvc_CoverReadCubeHead(pCover);
00328
00329 Counter = 0;
00330 Mvc_CubeForEachBit( pCover, pCube, iBit, Value )
00331 {
00332 if ( Value )
00333 {
00334 if ( Counter++ )
00335 return 0;
00336 }
00337 }
00338 return 1;
00339 }
00340
00344
00345