00001
00021 #ifndef __VEC_INT_H__
00022 #define __VEC_INT_H__
00023
00027
00028 #include <stdio.h>
00029
00033
00037
00038 typedef struct Vec_Int_t_ Vec_Int_t;
00039 struct Vec_Int_t_
00040 {
00041 int nCap;
00042 int nSize;
00043 int * pArray;
00044 };
00045
00049
00050 #define Vec_IntForEachEntry( vVec, Entry, i ) \
00051 for ( i = 0; (i < Vec_IntSize(vVec)) && (((Entry) = Vec_IntEntry(vVec, i)), 1); i++ )
00052 #define Vec_IntForEachEntryStart( vVec, Entry, i, Start ) \
00053 for ( i = Start; (i < Vec_IntSize(vVec)) && (((Entry) = Vec_IntEntry(vVec, i)), 1); i++ )
00054 #define Vec_IntForEachEntryStartStop( vVec, Entry, i, Start, Stop ) \
00055 for ( i = Start; (i < Stop) && (((Entry) = Vec_IntEntry(vVec, i)), 1); i++ )
00056 #define Vec_IntForEachEntryReverse( vVec, pEntry, i ) \
00057 for ( i = Vec_IntSize(vVec) - 1; (i >= 0) && (((pEntry) = Vec_IntEntry(vVec, i)), 1); i-- )
00058
00062
00074 static inline Vec_Int_t * Vec_IntAlloc( int nCap )
00075 {
00076 Vec_Int_t * p;
00077 p = ALLOC( Vec_Int_t, 1 );
00078 if ( nCap > 0 && nCap < 16 )
00079 nCap = 16;
00080 p->nSize = 0;
00081 p->nCap = nCap;
00082 p->pArray = p->nCap? ALLOC( int, p->nCap ) : NULL;
00083 return p;
00084 }
00085
00097 static inline Vec_Int_t * Vec_IntStart( int nSize )
00098 {
00099 Vec_Int_t * p;
00100 p = Vec_IntAlloc( nSize );
00101 p->nSize = nSize;
00102 memset( p->pArray, 0, sizeof(int) * nSize );
00103 return p;
00104 }
00105
00117 static inline Vec_Int_t * Vec_IntStartNatural( int nSize )
00118 {
00119 Vec_Int_t * p;
00120 int i;
00121 p = Vec_IntAlloc( nSize );
00122 p->nSize = nSize;
00123 for ( i = 0; i < nSize; i++ )
00124 p->pArray[i] = i;
00125 return p;
00126 }
00127
00139 static inline Vec_Int_t * Vec_IntAllocArray( int * pArray, int nSize )
00140 {
00141 Vec_Int_t * p;
00142 p = ALLOC( Vec_Int_t, 1 );
00143 p->nSize = nSize;
00144 p->nCap = nSize;
00145 p->pArray = pArray;
00146 return p;
00147 }
00148
00160 static inline Vec_Int_t * Vec_IntAllocArrayCopy( int * pArray, int nSize )
00161 {
00162 Vec_Int_t * p;
00163 p = ALLOC( Vec_Int_t, 1 );
00164 p->nSize = nSize;
00165 p->nCap = nSize;
00166 p->pArray = ALLOC( int, nSize );
00167 memcpy( p->pArray, pArray, sizeof(int) * nSize );
00168 return p;
00169 }
00170
00182 static inline Vec_Int_t * Vec_IntDup( Vec_Int_t * pVec )
00183 {
00184 Vec_Int_t * p;
00185 p = ALLOC( Vec_Int_t, 1 );
00186 p->nSize = pVec->nSize;
00187 p->nCap = pVec->nSize;
00188 p->pArray = p->nCap? ALLOC( int, p->nCap ) : NULL;
00189 memcpy( p->pArray, pVec->pArray, sizeof(int) * pVec->nSize );
00190 return p;
00191 }
00192
00204 static inline Vec_Int_t * Vec_IntDupArray( Vec_Int_t * pVec )
00205 {
00206 Vec_Int_t * p;
00207 p = ALLOC( Vec_Int_t, 1 );
00208 p->nSize = pVec->nSize;
00209 p->nCap = pVec->nCap;
00210 p->pArray = pVec->pArray;
00211 pVec->nSize = 0;
00212 pVec->nCap = 0;
00213 pVec->pArray = NULL;
00214 return p;
00215 }
00216
00228 static inline void Vec_IntFree( Vec_Int_t * p )
00229 {
00230 FREE( p->pArray );
00231 FREE( p );
00232 }
00233
00245 static inline int * Vec_IntReleaseArray( Vec_Int_t * p )
00246 {
00247 int * pArray = p->pArray;
00248 p->nCap = 0;
00249 p->nSize = 0;
00250 p->pArray = NULL;
00251 return pArray;
00252 }
00253
00265 static inline int * Vec_IntArray( Vec_Int_t * p )
00266 {
00267 return p->pArray;
00268 }
00269
00281 static inline int Vec_IntSize( Vec_Int_t * p )
00282 {
00283 return p->nSize;
00284 }
00285
00297 static inline int Vec_IntEntry( Vec_Int_t * p, int i )
00298 {
00299 assert( i >= 0 && i < p->nSize );
00300 return p->pArray[i];
00301 }
00302
00314 static inline void Vec_IntWriteEntry( Vec_Int_t * p, int i, int Entry )
00315 {
00316 assert( i >= 0 && i < p->nSize );
00317 p->pArray[i] = Entry;
00318 }
00319
00331 static inline void Vec_IntAddToEntry( Vec_Int_t * p, int i, int Addition )
00332 {
00333 assert( i >= 0 && i < p->nSize );
00334 p->pArray[i] += Addition;
00335 }
00336
00348 static inline int Vec_IntEntryLast( Vec_Int_t * p )
00349 {
00350 assert( p->nSize > 0 );
00351 return p->pArray[p->nSize-1];
00352 }
00353
00365 static inline void Vec_IntGrow( Vec_Int_t * p, int nCapMin )
00366 {
00367 if ( p->nCap >= nCapMin )
00368 return;
00369 p->pArray = REALLOC( int, p->pArray, nCapMin );
00370 assert( p->pArray );
00371 p->nCap = nCapMin;
00372 }
00373
00385 static inline void Vec_IntFill( Vec_Int_t * p, int nSize, int Entry )
00386 {
00387 int i;
00388 Vec_IntGrow( p, nSize );
00389 for ( i = 0; i < nSize; i++ )
00390 p->pArray[i] = Entry;
00391 p->nSize = nSize;
00392 }
00393
00405 static inline void Vec_IntFillExtra( Vec_Int_t * p, int nSize, int Entry )
00406 {
00407 int i;
00408 if ( p->nSize >= nSize )
00409 return;
00410 Vec_IntGrow( p, nSize );
00411 for ( i = p->nSize; i < nSize; i++ )
00412 p->pArray[i] = Entry;
00413 p->nSize = nSize;
00414 }
00415
00427 static inline void Vec_IntShrink( Vec_Int_t * p, int nSizeNew )
00428 {
00429 assert( p->nSize >= nSizeNew );
00430 p->nSize = nSizeNew;
00431 }
00432
00444 static inline void Vec_IntClear( Vec_Int_t * p )
00445 {
00446 p->nSize = 0;
00447 }
00448
00460 static inline void Vec_IntPush( Vec_Int_t * p, int Entry )
00461 {
00462 if ( p->nSize == p->nCap )
00463 {
00464 if ( p->nCap < 16 )
00465 Vec_IntGrow( p, 16 );
00466 else
00467 Vec_IntGrow( p, 2 * p->nCap );
00468 }
00469 p->pArray[p->nSize++] = Entry;
00470 }
00471
00483 static inline void Vec_IntPushFirst( Vec_Int_t * p, int Entry )
00484 {
00485 int i;
00486 if ( p->nSize == p->nCap )
00487 {
00488 if ( p->nCap < 16 )
00489 Vec_IntGrow( p, 16 );
00490 else
00491 Vec_IntGrow( p, 2 * p->nCap );
00492 }
00493 p->nSize++;
00494 for ( i = p->nSize - 1; i >= 1; i-- )
00495 p->pArray[i] = p->pArray[i-1];
00496 p->pArray[0] = Entry;
00497 }
00498
00510 static inline void Vec_IntPushOrder( Vec_Int_t * p, int Entry )
00511 {
00512 int i;
00513 if ( p->nSize == p->nCap )
00514 {
00515 if ( p->nCap < 16 )
00516 Vec_IntGrow( p, 16 );
00517 else
00518 Vec_IntGrow( p, 2 * p->nCap );
00519 }
00520 p->nSize++;
00521 for ( i = p->nSize-2; i >= 0; i-- )
00522 if ( p->pArray[i] > Entry )
00523 p->pArray[i+1] = p->pArray[i];
00524 else
00525 break;
00526 p->pArray[i+1] = Entry;
00527 }
00528
00540 static inline int Vec_IntPushUniqueOrder( Vec_Int_t * p, int Entry )
00541 {
00542 int i;
00543 for ( i = 0; i < p->nSize; i++ )
00544 if ( p->pArray[i] == Entry )
00545 return 1;
00546 Vec_IntPushOrder( p, Entry );
00547 return 0;
00548 }
00549
00561 static inline int Vec_IntPushUnique( Vec_Int_t * p, int Entry )
00562 {
00563 int i;
00564 for ( i = 0; i < p->nSize; i++ )
00565 if ( p->pArray[i] == Entry )
00566 return 1;
00567 Vec_IntPush( p, Entry );
00568 return 0;
00569 }
00570
00582 static inline unsigned * Vec_IntFetch( Vec_Int_t * p, int nWords )
00583 {
00584 if ( nWords == 0 )
00585 return NULL;
00586 assert( nWords > 0 );
00587 p->nSize += nWords;
00588 if ( p->nSize > p->nCap )
00589 {
00590
00591 return NULL;
00592 }
00593 return ((unsigned *)p->pArray) + p->nSize - nWords;
00594 }
00595
00607 static inline int Vec_IntPop( Vec_Int_t * p )
00608 {
00609 assert( p->nSize > 0 );
00610 return p->pArray[--p->nSize];
00611 }
00612
00624 static inline int Vec_IntFind( Vec_Int_t * p, int Entry )
00625 {
00626 int i;
00627 for ( i = 0; i < p->nSize; i++ )
00628 if ( p->pArray[i] == Entry )
00629 return i;
00630 return -1;
00631 }
00632
00644 static inline int Vec_IntRemove( Vec_Int_t * p, int Entry )
00645 {
00646 int i;
00647 for ( i = 0; i < p->nSize; i++ )
00648 if ( p->pArray[i] == Entry )
00649 break;
00650 if ( i == p->nSize )
00651 return 0;
00652 assert( i < p->nSize );
00653 for ( i++; i < p->nSize; i++ )
00654 p->pArray[i-1] = p->pArray[i];
00655 p->nSize--;
00656 return 1;
00657 }
00658
00670 static inline int Vec_IntSortCompare1( int * pp1, int * pp2 )
00671 {
00672
00673 if ( *pp1 < *pp2 )
00674 return -1;
00675 if ( *pp1 > *pp2 )
00676 return 1;
00677 return 0;
00678 }
00679
00691 static inline int Vec_IntSortCompare2( int * pp1, int * pp2 )
00692 {
00693
00694 if ( *pp1 > *pp2 )
00695 return -1;
00696 if ( *pp1 < *pp2 )
00697 return 1;
00698 return 0;
00699 }
00700
00712 static inline void Vec_IntSort( Vec_Int_t * p, int fReverse )
00713 {
00714 if ( fReverse )
00715 qsort( (void *)p->pArray, p->nSize, sizeof(int),
00716 (int (*)(const void *, const void *)) Vec_IntSortCompare2 );
00717 else
00718 qsort( (void *)p->pArray, p->nSize, sizeof(int),
00719 (int (*)(const void *, const void *)) Vec_IntSortCompare1 );
00720 }
00721
00722
00734 static inline int Vec_IntSortCompareUnsigned( unsigned * pp1, unsigned * pp2 )
00735 {
00736 if ( *pp1 < *pp2 )
00737 return -1;
00738 if ( *pp1 > *pp2 )
00739 return 1;
00740 return 0;
00741 }
00742
00754 static inline void Vec_IntSortUnsigned( Vec_Int_t * p )
00755 {
00756 qsort( (void *)p->pArray, p->nSize, sizeof(int),
00757 (int (*)(const void *, const void *)) Vec_IntSortCompareUnsigned );
00758 }
00759
00771 static inline int Vec_IntTwoCountCommon( Vec_Int_t * vArr1, Vec_Int_t * vArr2 )
00772 {
00773 int * pBeg1 = vArr1->pArray;
00774 int * pBeg2 = vArr2->pArray;
00775 int * pEnd1 = vArr1->pArray + vArr1->nSize;
00776 int * pEnd2 = vArr2->pArray + vArr2->nSize;
00777 int Counter = 0;
00778 while ( pBeg1 < pEnd1 && pBeg2 < pEnd2 )
00779 {
00780 if ( *pBeg1 == *pBeg2 )
00781 pBeg1++, pBeg2++, Counter++;
00782 else if ( *pBeg1 < *pBeg2 )
00783 pBeg1++;
00784 else
00785 pBeg2++;
00786 }
00787 return Counter;
00788 }
00789
00801 static inline Vec_Int_t * Vec_IntTwoMerge( Vec_Int_t * vArr1, Vec_Int_t * vArr2 )
00802 {
00803 Vec_Int_t * vArr = Vec_IntAlloc( vArr1->nSize + vArr2->nSize );
00804 int * pBeg = vArr->pArray;
00805 int * pBeg1 = vArr1->pArray;
00806 int * pBeg2 = vArr2->pArray;
00807 int * pEnd1 = vArr1->pArray + vArr1->nSize;
00808 int * pEnd2 = vArr2->pArray + vArr2->nSize;
00809 while ( pBeg1 < pEnd1 && pBeg2 < pEnd2 )
00810 {
00811 if ( *pBeg1 == *pBeg2 )
00812 *pBeg++ = *pBeg1++, pBeg2++;
00813 else if ( *pBeg1 < *pBeg2 )
00814 *pBeg++ = *pBeg1++;
00815 else
00816 *pBeg++ = *pBeg2++;
00817 }
00818 while ( pBeg1 < pEnd1 )
00819 *pBeg++ = *pBeg1++;
00820 while ( pBeg2 < pEnd2 )
00821 *pBeg++ = *pBeg2++;
00822 vArr->nSize = pBeg - vArr->pArray;
00823 assert( vArr->nSize <= vArr->nCap );
00824 assert( vArr->nSize >= vArr1->nSize );
00825 assert( vArr->nSize >= vArr2->nSize );
00826 return vArr;
00827 }
00828
00829 #endif
00830
00834