00001
00019 #include "fraigInt.h"
00020
00024
00028
00040 Fraig_NodeVec_t * Fraig_NodeVecAlloc( int nCap )
00041 {
00042 Fraig_NodeVec_t * p;
00043 p = ALLOC( Fraig_NodeVec_t, 1 );
00044 if ( nCap > 0 && nCap < 8 )
00045 nCap = 8;
00046 p->nSize = 0;
00047 p->nCap = nCap;
00048 p->pArray = p->nCap? ALLOC( Fraig_Node_t *, p->nCap ) : NULL;
00049 return p;
00050 }
00051
00063 void Fraig_NodeVecFree( Fraig_NodeVec_t * p )
00064 {
00065 FREE( p->pArray );
00066 FREE( p );
00067 }
00068
00080 Fraig_NodeVec_t * Fraig_NodeVecDup( Fraig_NodeVec_t * pVec )
00081 {
00082 Fraig_NodeVec_t * p;
00083 p = ALLOC( Fraig_NodeVec_t, 1 );
00084 p->nSize = pVec->nSize;
00085 p->nCap = pVec->nCap;
00086 p->pArray = p->nCap? ALLOC( Fraig_Node_t *, p->nCap ) : NULL;
00087 memcpy( p->pArray, pVec->pArray, sizeof(Fraig_Node_t *) * pVec->nSize );
00088 return p;
00089 }
00090
00102 Fraig_Node_t ** Fraig_NodeVecReadArray( Fraig_NodeVec_t * p )
00103 {
00104 return p->pArray;
00105 }
00106
00118 int Fraig_NodeVecReadSize( Fraig_NodeVec_t * p )
00119 {
00120 return p->nSize;
00121 }
00122
00134 void Fraig_NodeVecGrow( Fraig_NodeVec_t * p, int nCapMin )
00135 {
00136 if ( p->nCap >= nCapMin )
00137 return;
00138 p->pArray = REALLOC( Fraig_Node_t *, p->pArray, nCapMin );
00139 p->nCap = nCapMin;
00140 }
00141
00153 void Fraig_NodeVecShrink( Fraig_NodeVec_t * p, int nSizeNew )
00154 {
00155 assert( p->nSize >= nSizeNew );
00156 p->nSize = nSizeNew;
00157 }
00158
00170 void Fraig_NodeVecClear( Fraig_NodeVec_t * p )
00171 {
00172 p->nSize = 0;
00173 }
00174
00186 void Fraig_NodeVecPush( Fraig_NodeVec_t * p, Fraig_Node_t * Entry )
00187 {
00188 if ( p->nSize == p->nCap )
00189 {
00190 if ( p->nCap < 16 )
00191 Fraig_NodeVecGrow( p, 16 );
00192 else
00193 Fraig_NodeVecGrow( p, 2 * p->nCap );
00194 }
00195 p->pArray[p->nSize++] = Entry;
00196 }
00197
00209 int Fraig_NodeVecPushUnique( Fraig_NodeVec_t * p, Fraig_Node_t * Entry )
00210 {
00211 int i;
00212 for ( i = 0; i < p->nSize; i++ )
00213 if ( p->pArray[i] == Entry )
00214 return 1;
00215 Fraig_NodeVecPush( p, Entry );
00216 return 0;
00217 }
00218
00230 void Fraig_NodeVecPushOrder( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
00231 {
00232 Fraig_Node_t * pNode1, * pNode2;
00233 int i;
00234 Fraig_NodeVecPush( p, pNode );
00235
00236 for ( i = p->nSize-1; i > 0; i-- )
00237 {
00238 pNode1 = p->pArray[i ];
00239 pNode2 = p->pArray[i-1];
00240 if ( pNode1 >= pNode2 )
00241 break;
00242 p->pArray[i ] = pNode2;
00243 p->pArray[i-1] = pNode1;
00244 }
00245 }
00246
00258 int Fraig_NodeVecPushUniqueOrder( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
00259 {
00260 int i;
00261 for ( i = 0; i < p->nSize; i++ )
00262 if ( p->pArray[i] == pNode )
00263 return 1;
00264 Fraig_NodeVecPushOrder( p, pNode );
00265 return 0;
00266 }
00267
00279 void Fraig_NodeVecPushOrderByLevel( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
00280 {
00281 Fraig_Node_t * pNode1, * pNode2;
00282 int i;
00283 Fraig_NodeVecPush( p, pNode );
00284
00285 for ( i = p->nSize-1; i > 0; i-- )
00286 {
00287 pNode1 = p->pArray[i ];
00288 pNode2 = p->pArray[i-1];
00289 if ( Fraig_Regular(pNode1)->Level <= Fraig_Regular(pNode2)->Level )
00290 break;
00291 p->pArray[i ] = pNode2;
00292 p->pArray[i-1] = pNode1;
00293 }
00294 }
00295
00307 int Fraig_NodeVecPushUniqueOrderByLevel( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
00308 {
00309 int i;
00310 for ( i = 0; i < p->nSize; i++ )
00311 if ( p->pArray[i] == pNode )
00312 return 1;
00313 Fraig_NodeVecPushOrderByLevel( p, pNode );
00314 return 0;
00315 }
00316
00328 Fraig_Node_t * Fraig_NodeVecPop( Fraig_NodeVec_t * p )
00329 {
00330 return p->pArray[--p->nSize];
00331 }
00332
00344 void Fraig_NodeVecRemove( Fraig_NodeVec_t * p, Fraig_Node_t * Entry )
00345 {
00346 int i;
00347 for ( i = 0; i < p->nSize; i++ )
00348 if ( p->pArray[i] == Entry )
00349 break;
00350 assert( i < p->nSize );
00351 for ( i++; i < p->nSize; i++ )
00352 p->pArray[i-1] = p->pArray[i];
00353 p->nSize--;
00354 }
00355
00367 void Fraig_NodeVecWriteEntry( Fraig_NodeVec_t * p, int i, Fraig_Node_t * Entry )
00368 {
00369 assert( i >= 0 && i < p->nSize );
00370 p->pArray[i] = Entry;
00371 }
00372
00384 Fraig_Node_t * Fraig_NodeVecReadEntry( Fraig_NodeVec_t * p, int i )
00385 {
00386 assert( i >= 0 && i < p->nSize );
00387 return p->pArray[i];
00388 }
00389
00401 int Fraig_NodeVecCompareLevelsIncreasing( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
00402 {
00403 int Level1 = Fraig_Regular(*pp1)->Level;
00404 int Level2 = Fraig_Regular(*pp2)->Level;
00405 if ( Level1 < Level2 )
00406 return -1;
00407 if ( Level1 > Level2 )
00408 return 1;
00409 return 0;
00410 }
00411
00423 int Fraig_NodeVecCompareLevelsDecreasing( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
00424 {
00425 int Level1 = Fraig_Regular(*pp1)->Level;
00426 int Level2 = Fraig_Regular(*pp2)->Level;
00427 if ( Level1 > Level2 )
00428 return -1;
00429 if ( Level1 < Level2 )
00430 return 1;
00431 return 0;
00432 }
00433
00445 int Fraig_NodeVecCompareNumbers( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
00446 {
00447 int Num1 = Fraig_Regular(*pp1)->Num;
00448 int Num2 = Fraig_Regular(*pp2)->Num;
00449 if ( Num1 < Num2 )
00450 return -1;
00451 if ( Num1 > Num2 )
00452 return 1;
00453 return 0;
00454 }
00455
00467 int Fraig_NodeVecCompareRefCounts( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
00468 {
00469 int nRefs1 = Fraig_Regular(*pp1)->nRefs;
00470 int nRefs2 = Fraig_Regular(*pp2)->nRefs;
00471
00472 if ( nRefs1 < nRefs2 )
00473 return -1;
00474 if ( nRefs1 > nRefs2 )
00475 return 1;
00476
00477 nRefs1 = Fraig_Regular(*pp1)->Level;
00478 nRefs2 = Fraig_Regular(*pp2)->Level;
00479
00480 if ( nRefs1 < nRefs2 )
00481 return -1;
00482 if ( nRefs1 > nRefs2 )
00483 return 1;
00484 return 0;
00485 }
00486
00498 void Fraig_NodeVecSortByLevel( Fraig_NodeVec_t * p, int fIncreasing )
00499 {
00500 if ( fIncreasing )
00501 qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
00502 (int (*)(const void *, const void *)) Fraig_NodeVecCompareLevelsIncreasing );
00503 else
00504 qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
00505 (int (*)(const void *, const void *)) Fraig_NodeVecCompareLevelsDecreasing );
00506 }
00507
00519 void Fraig_NodeVecSortByNumber( Fraig_NodeVec_t * p )
00520 {
00521 qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
00522 (int (*)(const void *, const void *)) Fraig_NodeVecCompareNumbers );
00523 }
00524
00536 void Fraig_NodeVecSortByRefCount( Fraig_NodeVec_t * p )
00537 {
00538 qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
00539 (int (*)(const void *, const void *)) Fraig_NodeVecCompareRefCounts );
00540 }
00541
00545