00001
00021 #ifndef __VEC_PTR_H__
00022 #define __VEC_PTR_H__
00023
00027
00028 #include <stdio.h>
00029
00033
00037
00038 typedef struct Vec_Ptr_t_ Vec_Ptr_t;
00039 struct Vec_Ptr_t_
00040 {
00041 int nCap;
00042 int nSize;
00043 void ** pArray;
00044 };
00045
00049
00050
00051 #define Vec_PtrForEachEntry( vVec, pEntry, i ) \
00052 for ( i = 0; (i < Vec_PtrSize(vVec)) && (((pEntry) = Vec_PtrEntry(vVec, i)), 1); i++ )
00053 #define Vec_PtrForEachEntryStart( vVec, pEntry, i, Start ) \
00054 for ( i = Start; (i < Vec_PtrSize(vVec)) && (((pEntry) = Vec_PtrEntry(vVec, i)), 1); i++ )
00055 #define Vec_PtrForEachEntryStop( vVec, pEntry, i, Stop ) \
00056 for ( i = 0; (i < Stop) && (((pEntry) = Vec_PtrEntry(vVec, i)), 1); i++ )
00057 #define Vec_PtrForEachEntryStartStop( vVec, pEntry, i, Start, Stop ) \
00058 for ( i = Start; (i < Stop) && (((pEntry) = Vec_PtrEntry(vVec, i)), 1); i++ )
00059 #define Vec_PtrForEachEntryReverse( vVec, pEntry, i ) \
00060 for ( i = Vec_PtrSize(vVec) - 1; (i >= 0) && (((pEntry) = Vec_PtrEntry(vVec, i)), 1); i-- )
00061
00065
00077 static inline Vec_Ptr_t * Vec_PtrAlloc( int nCap )
00078 {
00079 Vec_Ptr_t * p;
00080 p = ALLOC( Vec_Ptr_t, 1 );
00081 if ( nCap > 0 && nCap < 8 )
00082 nCap = 8;
00083 p->nSize = 0;
00084 p->nCap = nCap;
00085 p->pArray = p->nCap? ALLOC( void *, p->nCap ) : NULL;
00086 return p;
00087 }
00088
00100 static inline Vec_Ptr_t * Vec_PtrStart( int nSize )
00101 {
00102 Vec_Ptr_t * p;
00103 p = Vec_PtrAlloc( nSize );
00104 p->nSize = nSize;
00105 memset( p->pArray, 0, sizeof(void *) * nSize );
00106 return p;
00107 }
00108
00120 static inline Vec_Ptr_t * Vec_PtrAllocArray( void ** pArray, int nSize )
00121 {
00122 Vec_Ptr_t * p;
00123 p = ALLOC( Vec_Ptr_t, 1 );
00124 p->nSize = nSize;
00125 p->nCap = nSize;
00126 p->pArray = pArray;
00127 return p;
00128 }
00129
00141 static inline Vec_Ptr_t * Vec_PtrAllocArrayCopy( void ** pArray, int nSize )
00142 {
00143 Vec_Ptr_t * p;
00144 p = ALLOC( Vec_Ptr_t, 1 );
00145 p->nSize = nSize;
00146 p->nCap = nSize;
00147 p->pArray = ALLOC( void *, nSize );
00148 memcpy( p->pArray, pArray, sizeof(void *) * nSize );
00149 return p;
00150 }
00151
00166 static inline Vec_Ptr_t * Vec_PtrAllocSimInfo( int nEntries, int nWords )
00167 {
00168 void ** pMemory;
00169 unsigned * pInfo;
00170 int i;
00171 pMemory = (void **)ALLOC( char, (sizeof(void *) + sizeof(unsigned) * nWords) * nEntries );
00172 pInfo = (unsigned *)(pMemory + nEntries);
00173 for ( i = 0; i < nEntries; i++ )
00174 pMemory[i] = pInfo + i * nWords;
00175 return Vec_PtrAllocArray( pMemory, nEntries );
00176 }
00177
00189 static inline Vec_Ptr_t * Vec_PtrAllocTruthTables( int nVars )
00190 {
00191 Vec_Ptr_t * p;
00192 unsigned Masks[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
00193 unsigned * pTruth;
00194 int i, k, nWords;
00195 nWords = (nVars <= 5 ? 1 : (1 << (nVars - 5)));
00196 p = Vec_PtrAllocSimInfo( nVars, nWords );
00197 for ( i = 0; i < nVars; i++ )
00198 {
00199 pTruth = (unsigned *)p->pArray[i];
00200 if ( i < 5 )
00201 {
00202 for ( k = 0; k < nWords; k++ )
00203 pTruth[k] = Masks[i];
00204 }
00205 else
00206 {
00207 for ( k = 0; k < nWords; k++ )
00208 if ( k & (1 << (i-5)) )
00209 pTruth[k] = ~(unsigned)0;
00210 else
00211 pTruth[k] = 0;
00212 }
00213 }
00214 return p;
00215 }
00216
00228 static inline Vec_Ptr_t * Vec_PtrDup( Vec_Ptr_t * pVec )
00229 {
00230 Vec_Ptr_t * p;
00231 p = ALLOC( Vec_Ptr_t, 1 );
00232 p->nSize = pVec->nSize;
00233 p->nCap = pVec->nCap;
00234 p->pArray = p->nCap? ALLOC( void *, p->nCap ) : NULL;
00235 memcpy( p->pArray, pVec->pArray, sizeof(void *) * pVec->nSize );
00236 return p;
00237 }
00238
00250 static inline Vec_Ptr_t * Vec_PtrDupArray( Vec_Ptr_t * pVec )
00251 {
00252 Vec_Ptr_t * p;
00253 p = ALLOC( Vec_Ptr_t, 1 );
00254 p->nSize = pVec->nSize;
00255 p->nCap = pVec->nCap;
00256 p->pArray = pVec->pArray;
00257 pVec->nSize = 0;
00258 pVec->nCap = 0;
00259 pVec->pArray = NULL;
00260 return p;
00261 }
00262
00274 static inline void Vec_PtrFree( Vec_Ptr_t * p )
00275 {
00276 FREE( p->pArray );
00277 FREE( p );
00278 }
00279
00291 static inline void ** Vec_PtrReleaseArray( Vec_Ptr_t * p )
00292 {
00293 void ** pArray = p->pArray;
00294 p->nCap = 0;
00295 p->nSize = 0;
00296 p->pArray = NULL;
00297 return pArray;
00298 }
00299
00311 static inline void ** Vec_PtrArray( Vec_Ptr_t * p )
00312 {
00313 return p->pArray;
00314 }
00315
00327 static inline int Vec_PtrSize( Vec_Ptr_t * p )
00328 {
00329 return p->nSize;
00330 }
00331
00343 static inline void * Vec_PtrEntry( Vec_Ptr_t * p, int i )
00344 {
00345 assert( i >= 0 && i < p->nSize );
00346 return p->pArray[i];
00347 }
00348
00360 static inline void Vec_PtrDoubleSimInfo( Vec_Ptr_t * vInfo )
00361 {
00362 Vec_Ptr_t * vInfoNew;
00363 int nWords;
00364 assert( Vec_PtrSize(vInfo) > 2 );
00365
00366 nWords = (unsigned *)Vec_PtrEntry(vInfo,1) - (unsigned *)Vec_PtrEntry(vInfo,0);
00367 vInfoNew = Vec_PtrAllocSimInfo( 2*Vec_PtrSize(vInfo), nWords );
00368
00369 memcpy( Vec_PtrEntry(vInfoNew,0), Vec_PtrEntry(vInfo,0), Vec_PtrSize(vInfo) * nWords * 4 );
00370
00371 free( vInfo->pArray );
00372 vInfo->pArray = vInfoNew->pArray;
00373 vInfo->nSize *= 2;
00374 vInfo->nCap *= 2;
00375
00376 vInfoNew->pArray = NULL;
00377 free( vInfoNew );
00378 }
00379
00391 static inline void ** Vec_PtrEntryP( Vec_Ptr_t * p, int i )
00392 {
00393 assert( i >= 0 && i < p->nSize );
00394 return p->pArray + i;
00395 }
00396
00408 static inline void Vec_PtrWriteEntry( Vec_Ptr_t * p, int i, void * Entry )
00409 {
00410 assert( i >= 0 && i < p->nSize );
00411 p->pArray[i] = Entry;
00412 }
00413
00425 static inline void * Vec_PtrEntryLast( Vec_Ptr_t * p )
00426 {
00427 assert( p->nSize > 0 );
00428 return p->pArray[p->nSize-1];
00429 }
00430
00442 static inline void Vec_PtrGrow( Vec_Ptr_t * p, int nCapMin )
00443 {
00444 if ( p->nCap >= nCapMin )
00445 return;
00446 p->pArray = REALLOC( void *, p->pArray, nCapMin );
00447 p->nCap = nCapMin;
00448 }
00449
00461 static inline void Vec_PtrFill( Vec_Ptr_t * p, int nSize, void * Entry )
00462 {
00463 int i;
00464 Vec_PtrGrow( p, nSize );
00465 for ( i = 0; i < nSize; i++ )
00466 p->pArray[i] = Entry;
00467 p->nSize = nSize;
00468 }
00469
00481 static inline void Vec_PtrFillExtra( Vec_Ptr_t * p, int nSize, void * Entry )
00482 {
00483 int i;
00484 if ( p->nSize >= nSize )
00485 return;
00486 assert( p->nSize < nSize );
00487 if ( 2 * p->nSize > nSize )
00488 Vec_PtrGrow( p, 2 * nSize );
00489 else
00490 Vec_PtrGrow( p, nSize );
00491 for ( i = p->nSize; i < nSize; i++ )
00492 p->pArray[i] = Entry;
00493 p->nSize = nSize;
00494 }
00495
00507 static inline void * Vec_PtrGetEntry( Vec_Ptr_t * p, int i )
00508 {
00509 Vec_PtrFillExtra( p, i + 1, NULL );
00510 return Vec_PtrEntry( p, i );
00511 }
00512
00524 static inline void Vec_PtrSetEntry( Vec_Ptr_t * p, int i, void * Entry )
00525 {
00526 Vec_PtrFillExtra( p, i + 1, NULL );
00527 Vec_PtrWriteEntry( p, i, Entry );
00528 }
00529
00541 static inline void Vec_PtrShrink( Vec_Ptr_t * p, int nSizeNew )
00542 {
00543 assert( p->nSize >= nSizeNew );
00544 p->nSize = nSizeNew;
00545 }
00546
00558 static inline void Vec_PtrClear( Vec_Ptr_t * p )
00559 {
00560 p->nSize = 0;
00561 }
00562
00574 static inline void Vec_PtrCopy( Vec_Ptr_t * pDest, Vec_Ptr_t * pSour )
00575 {
00576 pDest->nSize = 0;
00577 Vec_PtrGrow( pDest, pSour->nSize );
00578 memcpy( pDest->pArray, pSour->pArray, sizeof(void *) * pSour->nSize );
00579 pDest->nSize = pSour->nSize;
00580 }
00581
00593 static inline void Vec_PtrPush( Vec_Ptr_t * p, void * Entry )
00594 {
00595 if ( p->nSize == p->nCap )
00596 {
00597 if ( p->nCap < 16 )
00598 Vec_PtrGrow( p, 16 );
00599 else
00600 Vec_PtrGrow( p, 2 * p->nCap );
00601 }
00602 p->pArray[p->nSize++] = Entry;
00603 }
00604
00616 static inline int Vec_PtrPushUnique( Vec_Ptr_t * p, void * Entry )
00617 {
00618 int i;
00619 for ( i = 0; i < p->nSize; i++ )
00620 if ( p->pArray[i] == Entry )
00621 return 1;
00622 Vec_PtrPush( p, Entry );
00623 return 0;
00624 }
00625
00637 static inline void * Vec_PtrPop( Vec_Ptr_t * p )
00638 {
00639 assert( p->nSize > 0 );
00640 return p->pArray[--p->nSize];
00641 }
00642
00654 static inline int Vec_PtrFind( Vec_Ptr_t * p, void * Entry )
00655 {
00656 int i;
00657 for ( i = 0; i < p->nSize; i++ )
00658 if ( p->pArray[i] == Entry )
00659 return i;
00660 return -1;
00661 }
00662
00674 static inline void Vec_PtrRemove( Vec_Ptr_t * p, void * Entry )
00675 {
00676 int i;
00677
00678 for ( i = p->nSize - 1; i >= 0; i-- )
00679 if ( p->pArray[i] == Entry )
00680 break;
00681 assert( i >= 0 );
00682
00683
00684
00685
00686
00687
00688
00689 for ( i++; i < p->nSize; i++ )
00690 p->pArray[i-1] = p->pArray[i];
00691 p->nSize--;
00692 }
00693
00705 static inline void Vec_PtrReorder( Vec_Ptr_t * p, int nItems )
00706 {
00707 assert( nItems < p->nSize );
00708 Vec_PtrGrow( p, nItems + p->nSize );
00709 memmove( (char **)p->pArray + p->nSize, p->pArray, nItems * sizeof(void*) );
00710 memmove( p->pArray, (char **)p->pArray + nItems, p->nSize * sizeof(void*) );
00711 }
00712
00724 static inline void Vec_PtrSort( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)() )
00725 {
00726 if ( p->nSize < 2 )
00727 return;
00728 qsort( (void *)p->pArray, p->nSize, sizeof(void *),
00729 (int (*)(const void *, const void *)) Vec_PtrSortCompare );
00730 }
00731
00743 static inline void Vec_PtrUniqify( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)() )
00744 {
00745 int i, k;
00746 if ( p->nSize < 2 )
00747 return;
00748 qsort( (void *)p->pArray, p->nSize, sizeof(void *),
00749 (int (*)(const void *, const void *)) Vec_PtrSortCompare );
00750 for ( i = k = 1; i < p->nSize; i++ )
00751 if ( p->pArray[i] != p->pArray[i-1] )
00752 p->pArray[k++] = p->pArray[i];
00753 p->nSize = k;
00754 }
00755
00756 #endif
00757
00758
00762