00001
00019 #include "extra.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 struct Extra_BitMat_t_
00030 {
00031 unsigned ** ppData;
00032 int nSize;
00033 int nWords;
00034 int nBitShift;
00035 unsigned uMask;
00036 int nLookups;
00037 int nInserts;
00038 int nDeletes;
00039 };
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00056
00057
00058
00059
00063
00064
00065
00066
00078 Extra_BitMat_t * Extra_BitMatrixStart( int nSize )
00079 {
00080 Extra_BitMat_t * p;
00081 int i;
00082 p = ALLOC( Extra_BitMat_t, 1 );
00083 memset( p, 0, sizeof(Extra_BitMat_t) );
00084 p->nSize = nSize;
00085 p->nBitShift = (sizeof(unsigned) == 4) ? 5: 6;
00086 p->uMask = (sizeof(unsigned) == 4) ? 31: 63;
00087 p->nWords = nSize / (8 * sizeof(unsigned)) + ((nSize % (8 * sizeof(unsigned))) > 0);
00088 p->ppData = ALLOC( unsigned *, nSize );
00089 p->ppData[0] = ALLOC( unsigned, nSize * p->nWords );
00090 memset( p->ppData[0], 0, sizeof(unsigned) * nSize * p->nWords );
00091 for ( i = 1; i < nSize; i++ )
00092 p->ppData[i] = p->ppData[i-1] + p->nWords;
00093 return p;
00094 }
00095
00107 void Extra_BitMatrixClean( Extra_BitMat_t * p )
00108 {
00109 memset( p->ppData[0], 0, sizeof(unsigned) * p->nSize * p->nWords );
00110 }
00111
00123 void Extra_BitMatrixStop( Extra_BitMat_t * p )
00124 {
00125 FREE( p->ppData[0] );
00126 FREE( p->ppData );
00127 FREE( p );
00128 }
00129
00141 void Extra_BitMatrixPrint( Extra_BitMat_t * pMat )
00142 {
00143 int i, k, nVars;
00144 printf( "\n" );
00145 nVars = Extra_BitMatrixReadSize( pMat );
00146 for ( i = 0; i < nVars; i++ )
00147 {
00148 for ( k = 0; k <= i; k++ )
00149 printf( " " );
00150 for ( k = i+1; k < nVars; k++ )
00151 if ( Extra_BitMatrixLookup1( pMat, i, k ) )
00152 printf( "1" );
00153 else
00154 printf( "." );
00155 printf( "\n" );
00156 }
00157 }
00158
00159
00171 int Extra_BitMatrixReadSize( Extra_BitMat_t * p )
00172 {
00173 return p->nSize;
00174 }
00175
00187 void Extra_BitMatrixInsert1( Extra_BitMat_t * p, int i, int k )
00188 {
00189 p->nInserts++;
00190 if ( i < k )
00191 p->ppData[i][k>>p->nBitShift] |= (1<<(k & p->uMask));
00192 else
00193 p->ppData[k][i>>p->nBitShift] |= (1<<(i & p->uMask));
00194 }
00195
00207 int Extra_BitMatrixLookup1( Extra_BitMat_t * p, int i, int k )
00208 {
00209 p->nLookups++;
00210 if ( i < k )
00211 return ((p->ppData[i][k>>p->nBitShift] & (1<<(k & p->uMask))) > 0);
00212 else
00213 return ((p->ppData[k][i>>p->nBitShift] & (1<<(i & p->uMask))) > 0);
00214 }
00215
00227 void Extra_BitMatrixDelete1( Extra_BitMat_t * p, int i, int k )
00228 {
00229 p->nDeletes++;
00230 if ( i < k )
00231 p->ppData[i][k>>p->nBitShift] &= ~(1<<(k & p->uMask));
00232 else
00233 p->ppData[k][i>>p->nBitShift] &= ~(1<<(i & p->uMask));
00234 }
00235
00236
00237
00249 void Extra_BitMatrixInsert2( Extra_BitMat_t * p, int i, int k )
00250 {
00251 p->nInserts++;
00252 if ( i > k )
00253 p->ppData[i][k>>p->nBitShift] |= (1<<(k & p->uMask));
00254 else
00255 p->ppData[k][i>>p->nBitShift] |= (1<<(i & p->uMask));
00256 }
00257
00269 int Extra_BitMatrixLookup2( Extra_BitMat_t * p, int i, int k )
00270 {
00271 p->nLookups++;
00272 if ( i > k )
00273 return ((p->ppData[i][k>>p->nBitShift] & (1<<(k & p->uMask))) > 0);
00274 else
00275 return ((p->ppData[k][i>>p->nBitShift] & (1<<(i & p->uMask))) > 0);
00276 }
00277
00289 void Extra_BitMatrixDelete2( Extra_BitMat_t * p, int i, int k )
00290 {
00291 p->nDeletes++;
00292 if ( i > k )
00293 p->ppData[i][k>>p->nBitShift] &= ~(1<<(k & p->uMask));
00294 else
00295 p->ppData[k][i>>p->nBitShift] &= ~(1<<(i & p->uMask));
00296 }
00297
00298
00310 void Extra_BitMatrixOr( Extra_BitMat_t * p, int i, unsigned * pInfo )
00311 {
00312 int w;
00313 for ( w = 0; w < p->nWords; w++ )
00314 p->ppData[i][w] |= pInfo[w];
00315 }
00316
00328 void Extra_BitMatrixOrTwo( Extra_BitMat_t * p, int i, int j )
00329 {
00330 int w;
00331 for ( w = 0; w < p->nWords; w++ )
00332 p->ppData[i][w] = p->ppData[j][w] = (p->ppData[i][w] | p->ppData[j][w]);
00333 }
00334
00346 int Extra_BitMatrixCountOnesUpper( Extra_BitMat_t * p )
00347 {
00348 int i, k, nTotal = 0;
00349 for ( i = 0; i < p->nSize; i++ )
00350 for ( k = i + 1; k < p->nSize; k++ )
00351 nTotal += ( (p->ppData[i][k>>5] & (1 << (k&31))) > 0 );
00352 return nTotal;
00353 }
00354
00366 int Extra_BitMatrixIsDisjoint( Extra_BitMat_t * p1, Extra_BitMat_t * p2 )
00367 {
00368 int i, w;
00369 assert( p1->nSize == p2->nSize );
00370 for ( i = 0; i < p1->nSize; i++ )
00371 for ( w = 0; w < p1->nWords; w++ )
00372 if ( p1->ppData[i][w] & p2->ppData[i][w] )
00373 return 0;
00374 return 1;
00375 }
00376
00388 int Extra_BitMatrixIsClique( Extra_BitMat_t * pMat )
00389 {
00390 int v, u, i;
00391 for ( v = 0; v < pMat->nSize; v++ )
00392 for ( u = v+1; u < pMat->nSize; u++ )
00393 {
00394 if ( !Extra_BitMatrixLookup1( pMat, v, u ) )
00395 continue;
00396
00397 for ( i = 0; i < pMat->nSize; i++ )
00398 {
00399 if ( i == v || i == u )
00400 continue;
00401
00402
00403 if ( Extra_BitMatrixLookup1( pMat, i, v ) != Extra_BitMatrixLookup1( pMat, i, u ) )
00404 return 0;
00405 }
00406 }
00407 return 1;
00408 }
00409
00410
00414
00415