/stp-0.1/src/extlib-abc/vecInt.h

# · C Header · 834 lines · 349 code · 49 blank · 436 comment · 71 complexity · e544dbb269b85a9ce656b2969ea670d3 MD5 · raw file

  1. /**CFile****************************************************************
  2. FileName [vecInt.h]
  3. SystemName [ABC: Logic synthesis and verification system.]
  4. PackageName [Resizable arrays.]
  5. Synopsis [Resizable arrays of integers.]
  6. Author [Alan Mishchenko]
  7. Affiliation [UC Berkeley]
  8. Date [Ver. 1.0. Started - June 20, 2005.]
  9. Revision [$Id: vecInt.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
  10. ***********************************************************************/
  11. #ifndef __VEC_INT_H__
  12. #define __VEC_INT_H__
  13. ////////////////////////////////////////////////////////////////////////
  14. /// INCLUDES ///
  15. ////////////////////////////////////////////////////////////////////////
  16. #include <stdio.h>
  17. ////////////////////////////////////////////////////////////////////////
  18. /// PARAMETERS ///
  19. ////////////////////////////////////////////////////////////////////////
  20. ////////////////////////////////////////////////////////////////////////
  21. /// BASIC TYPES ///
  22. ////////////////////////////////////////////////////////////////////////
  23. typedef struct Vec_Int_t_ Vec_Int_t;
  24. struct Vec_Int_t_
  25. {
  26. int nCap;
  27. int nSize;
  28. int * pArray;
  29. };
  30. ////////////////////////////////////////////////////////////////////////
  31. /// MACRO DEFINITIONS ///
  32. ////////////////////////////////////////////////////////////////////////
  33. #define Vec_IntForEachEntry( vVec, Entry, i ) \
  34. for ( i = 0; (i < Vec_IntSize(vVec)) && (((Entry) = Vec_IntEntry(vVec, i)), 1); i++ )
  35. #define Vec_IntForEachEntryStart( vVec, Entry, i, Start ) \
  36. for ( i = Start; (i < Vec_IntSize(vVec)) && (((Entry) = Vec_IntEntry(vVec, i)), 1); i++ )
  37. #define Vec_IntForEachEntryStartStop( vVec, Entry, i, Start, Stop ) \
  38. for ( i = Start; (i < Stop) && (((Entry) = Vec_IntEntry(vVec, i)), 1); i++ )
  39. #define Vec_IntForEachEntryReverse( vVec, pEntry, i ) \
  40. for ( i = Vec_IntSize(vVec) - 1; (i >= 0) && (((pEntry) = Vec_IntEntry(vVec, i)), 1); i-- )
  41. ////////////////////////////////////////////////////////////////////////
  42. /// FUNCTION DEFINITIONS ///
  43. ////////////////////////////////////////////////////////////////////////
  44. /**Function*************************************************************
  45. Synopsis [Allocates a vector with the given capacity.]
  46. Description []
  47. SideEffects []
  48. SeeAlso []
  49. ***********************************************************************/
  50. static inline Vec_Int_t * Vec_IntAlloc( int nCap )
  51. {
  52. Vec_Int_t * p;
  53. p = ALLOC( Vec_Int_t, 1 );
  54. if ( nCap > 0 && nCap < 16 )
  55. nCap = 16;
  56. p->nSize = 0;
  57. p->nCap = nCap;
  58. p->pArray = p->nCap? ALLOC( int, p->nCap ) : NULL;
  59. return p;
  60. }
  61. /**Function*************************************************************
  62. Synopsis [Allocates a vector with the given size and cleans it.]
  63. Description []
  64. SideEffects []
  65. SeeAlso []
  66. ***********************************************************************/
  67. static inline Vec_Int_t * Vec_IntStart( int nSize )
  68. {
  69. Vec_Int_t * p;
  70. p = Vec_IntAlloc( nSize );
  71. p->nSize = nSize;
  72. memset( p->pArray, 0, sizeof(int) * nSize );
  73. return p;
  74. }
  75. /**Function*************************************************************
  76. Synopsis [Allocates a vector with the given size and cleans it.]
  77. Description []
  78. SideEffects []
  79. SeeAlso []
  80. ***********************************************************************/
  81. static inline Vec_Int_t * Vec_IntStartNatural( int nSize )
  82. {
  83. Vec_Int_t * p;
  84. int i;
  85. p = Vec_IntAlloc( nSize );
  86. p->nSize = nSize;
  87. for ( i = 0; i < nSize; i++ )
  88. p->pArray[i] = i;
  89. return p;
  90. }
  91. /**Function*************************************************************
  92. Synopsis [Creates the vector from an integer array of the given size.]
  93. Description []
  94. SideEffects []
  95. SeeAlso []
  96. ***********************************************************************/
  97. static inline Vec_Int_t * Vec_IntAllocArray( int * pArray, int nSize )
  98. {
  99. Vec_Int_t * p;
  100. p = ALLOC( Vec_Int_t, 1 );
  101. p->nSize = nSize;
  102. p->nCap = nSize;
  103. p->pArray = pArray;
  104. return p;
  105. }
  106. /**Function*************************************************************
  107. Synopsis [Creates the vector from an integer array of the given size.]
  108. Description []
  109. SideEffects []
  110. SeeAlso []
  111. ***********************************************************************/
  112. static inline Vec_Int_t * Vec_IntAllocArrayCopy( int * pArray, int nSize )
  113. {
  114. Vec_Int_t * p;
  115. p = ALLOC( Vec_Int_t, 1 );
  116. p->nSize = nSize;
  117. p->nCap = nSize;
  118. p->pArray = ALLOC( int, nSize );
  119. memcpy( p->pArray, pArray, sizeof(int) * nSize );
  120. return p;
  121. }
  122. /**Function*************************************************************
  123. Synopsis [Duplicates the integer array.]
  124. Description []
  125. SideEffects []
  126. SeeAlso []
  127. ***********************************************************************/
  128. static inline Vec_Int_t * Vec_IntDup( Vec_Int_t * pVec )
  129. {
  130. Vec_Int_t * p;
  131. p = ALLOC( Vec_Int_t, 1 );
  132. p->nSize = pVec->nSize;
  133. p->nCap = pVec->nSize;
  134. p->pArray = p->nCap? ALLOC( int, p->nCap ) : NULL;
  135. memcpy( p->pArray, pVec->pArray, sizeof(int) * pVec->nSize );
  136. return p;
  137. }
  138. /**Function*************************************************************
  139. Synopsis [Transfers the array into another vector.]
  140. Description []
  141. SideEffects []
  142. SeeAlso []
  143. ***********************************************************************/
  144. static inline Vec_Int_t * Vec_IntDupArray( Vec_Int_t * pVec )
  145. {
  146. Vec_Int_t * p;
  147. p = ALLOC( Vec_Int_t, 1 );
  148. p->nSize = pVec->nSize;
  149. p->nCap = pVec->nCap;
  150. p->pArray = pVec->pArray;
  151. pVec->nSize = 0;
  152. pVec->nCap = 0;
  153. pVec->pArray = NULL;
  154. return p;
  155. }
  156. /**Function*************************************************************
  157. Synopsis []
  158. Description []
  159. SideEffects []
  160. SeeAlso []
  161. ***********************************************************************/
  162. static inline void Vec_IntFree( Vec_Int_t * p )
  163. {
  164. FREE( p->pArray );
  165. FREE( p );
  166. }
  167. /**Function*************************************************************
  168. Synopsis []
  169. Description []
  170. SideEffects []
  171. SeeAlso []
  172. ***********************************************************************/
  173. static inline int * Vec_IntReleaseArray( Vec_Int_t * p )
  174. {
  175. int * pArray = p->pArray;
  176. p->nCap = 0;
  177. p->nSize = 0;
  178. p->pArray = NULL;
  179. return pArray;
  180. }
  181. /**Function*************************************************************
  182. Synopsis []
  183. Description []
  184. SideEffects []
  185. SeeAlso []
  186. ***********************************************************************/
  187. static inline int * Vec_IntArray( Vec_Int_t * p )
  188. {
  189. return p->pArray;
  190. }
  191. /**Function*************************************************************
  192. Synopsis []
  193. Description []
  194. SideEffects []
  195. SeeAlso []
  196. ***********************************************************************/
  197. static inline int Vec_IntSize( Vec_Int_t * p )
  198. {
  199. return p->nSize;
  200. }
  201. /**Function*************************************************************
  202. Synopsis []
  203. Description []
  204. SideEffects []
  205. SeeAlso []
  206. ***********************************************************************/
  207. static inline int Vec_IntEntry( Vec_Int_t * p, int i )
  208. {
  209. assert( i >= 0 && i < p->nSize );
  210. return p->pArray[i];
  211. }
  212. /**Function*************************************************************
  213. Synopsis []
  214. Description []
  215. SideEffects []
  216. SeeAlso []
  217. ***********************************************************************/
  218. static inline void Vec_IntWriteEntry( Vec_Int_t * p, int i, int Entry )
  219. {
  220. assert( i >= 0 && i < p->nSize );
  221. p->pArray[i] = Entry;
  222. }
  223. /**Function*************************************************************
  224. Synopsis []
  225. Description []
  226. SideEffects []
  227. SeeAlso []
  228. ***********************************************************************/
  229. static inline void Vec_IntAddToEntry( Vec_Int_t * p, int i, int Addition )
  230. {
  231. assert( i >= 0 && i < p->nSize );
  232. p->pArray[i] += Addition;
  233. }
  234. /**Function*************************************************************
  235. Synopsis []
  236. Description []
  237. SideEffects []
  238. SeeAlso []
  239. ***********************************************************************/
  240. static inline int Vec_IntEntryLast( Vec_Int_t * p )
  241. {
  242. assert( p->nSize > 0 );
  243. return p->pArray[p->nSize-1];
  244. }
  245. /**Function*************************************************************
  246. Synopsis [Resizes the vector to the given capacity.]
  247. Description []
  248. SideEffects []
  249. SeeAlso []
  250. ***********************************************************************/
  251. static inline void Vec_IntGrow( Vec_Int_t * p, int nCapMin )
  252. {
  253. if ( p->nCap >= nCapMin )
  254. return;
  255. p->pArray = REALLOC( int, p->pArray, nCapMin );
  256. assert( p->pArray );
  257. p->nCap = nCapMin;
  258. }
  259. /**Function*************************************************************
  260. Synopsis [Fills the vector with given number of entries.]
  261. Description []
  262. SideEffects []
  263. SeeAlso []
  264. ***********************************************************************/
  265. static inline void Vec_IntFill( Vec_Int_t * p, int nSize, int Entry )
  266. {
  267. int i;
  268. Vec_IntGrow( p, nSize );
  269. for ( i = 0; i < nSize; i++ )
  270. p->pArray[i] = Entry;
  271. p->nSize = nSize;
  272. }
  273. /**Function*************************************************************
  274. Synopsis [Fills the vector with given number of entries.]
  275. Description []
  276. SideEffects []
  277. SeeAlso []
  278. ***********************************************************************/
  279. static inline void Vec_IntFillExtra( Vec_Int_t * p, int nSize, int Entry )
  280. {
  281. int i;
  282. if ( p->nSize >= nSize )
  283. return;
  284. Vec_IntGrow( p, nSize );
  285. for ( i = p->nSize; i < nSize; i++ )
  286. p->pArray[i] = Entry;
  287. p->nSize = nSize;
  288. }
  289. /**Function*************************************************************
  290. Synopsis []
  291. Description []
  292. SideEffects []
  293. SeeAlso []
  294. ***********************************************************************/
  295. static inline void Vec_IntShrink( Vec_Int_t * p, int nSizeNew )
  296. {
  297. assert( p->nSize >= nSizeNew );
  298. p->nSize = nSizeNew;
  299. }
  300. /**Function*************************************************************
  301. Synopsis []
  302. Description []
  303. SideEffects []
  304. SeeAlso []
  305. ***********************************************************************/
  306. static inline void Vec_IntClear( Vec_Int_t * p )
  307. {
  308. p->nSize = 0;
  309. }
  310. /**Function*************************************************************
  311. Synopsis []
  312. Description []
  313. SideEffects []
  314. SeeAlso []
  315. ***********************************************************************/
  316. static inline void Vec_IntPush( Vec_Int_t * p, int Entry )
  317. {
  318. if ( p->nSize == p->nCap )
  319. {
  320. if ( p->nCap < 16 )
  321. Vec_IntGrow( p, 16 );
  322. else
  323. Vec_IntGrow( p, 2 * p->nCap );
  324. }
  325. p->pArray[p->nSize++] = Entry;
  326. }
  327. /**Function*************************************************************
  328. Synopsis []
  329. Description []
  330. SideEffects []
  331. SeeAlso []
  332. ***********************************************************************/
  333. static inline void Vec_IntPushFirst( Vec_Int_t * p, int Entry )
  334. {
  335. int i;
  336. if ( p->nSize == p->nCap )
  337. {
  338. if ( p->nCap < 16 )
  339. Vec_IntGrow( p, 16 );
  340. else
  341. Vec_IntGrow( p, 2 * p->nCap );
  342. }
  343. p->nSize++;
  344. for ( i = p->nSize - 1; i >= 1; i-- )
  345. p->pArray[i] = p->pArray[i-1];
  346. p->pArray[0] = Entry;
  347. }
  348. /**Function*************************************************************
  349. Synopsis [Inserts the entry while preserving the increasing order.]
  350. Description []
  351. SideEffects []
  352. SeeAlso []
  353. ***********************************************************************/
  354. static inline void Vec_IntPushOrder( Vec_Int_t * p, int Entry )
  355. {
  356. int i;
  357. if ( p->nSize == p->nCap )
  358. {
  359. if ( p->nCap < 16 )
  360. Vec_IntGrow( p, 16 );
  361. else
  362. Vec_IntGrow( p, 2 * p->nCap );
  363. }
  364. p->nSize++;
  365. for ( i = p->nSize-2; i >= 0; i-- )
  366. if ( p->pArray[i] > Entry )
  367. p->pArray[i+1] = p->pArray[i];
  368. else
  369. break;
  370. p->pArray[i+1] = Entry;
  371. }
  372. /**Function*************************************************************
  373. Synopsis [Inserts the entry while preserving the increasing order.]
  374. Description []
  375. SideEffects []
  376. SeeAlso []
  377. ***********************************************************************/
  378. static inline int Vec_IntPushUniqueOrder( Vec_Int_t * p, int Entry )
  379. {
  380. int i;
  381. for ( i = 0; i < p->nSize; i++ )
  382. if ( p->pArray[i] == Entry )
  383. return 1;
  384. Vec_IntPushOrder( p, Entry );
  385. return 0;
  386. }
  387. /**Function*************************************************************
  388. Synopsis []
  389. Description []
  390. SideEffects []
  391. SeeAlso []
  392. ***********************************************************************/
  393. static inline int Vec_IntPushUnique( Vec_Int_t * p, int Entry )
  394. {
  395. int i;
  396. for ( i = 0; i < p->nSize; i++ )
  397. if ( p->pArray[i] == Entry )
  398. return 1;
  399. Vec_IntPush( p, Entry );
  400. return 0;
  401. }
  402. /**Function*************************************************************
  403. Synopsis [Returns the pointer to the next nWords entries in the vector.]
  404. Description []
  405. SideEffects []
  406. SeeAlso []
  407. ***********************************************************************/
  408. static inline unsigned * Vec_IntFetch( Vec_Int_t * p, int nWords )
  409. {
  410. if ( nWords == 0 )
  411. return NULL;
  412. assert( nWords > 0 );
  413. p->nSize += nWords;
  414. if ( p->nSize > p->nCap )
  415. {
  416. // Vec_IntGrow( p, 2 * p->nSize );
  417. return NULL;
  418. }
  419. return ((unsigned *)p->pArray) + p->nSize - nWords;
  420. }
  421. /**Function*************************************************************
  422. Synopsis [Returns the last entry and removes it from the list.]
  423. Description []
  424. SideEffects []
  425. SeeAlso []
  426. ***********************************************************************/
  427. static inline int Vec_IntPop( Vec_Int_t * p )
  428. {
  429. assert( p->nSize > 0 );
  430. return p->pArray[--p->nSize];
  431. }
  432. /**Function*************************************************************
  433. Synopsis [Find entry.]
  434. Description []
  435. SideEffects []
  436. SeeAlso []
  437. ***********************************************************************/
  438. static inline int Vec_IntFind( Vec_Int_t * p, int Entry )
  439. {
  440. int i;
  441. for ( i = 0; i < p->nSize; i++ )
  442. if ( p->pArray[i] == Entry )
  443. return i;
  444. return -1;
  445. }
  446. /**Function*************************************************************
  447. Synopsis []
  448. Description []
  449. SideEffects []
  450. SeeAlso []
  451. ***********************************************************************/
  452. static inline int Vec_IntRemove( Vec_Int_t * p, int Entry )
  453. {
  454. int i;
  455. for ( i = 0; i < p->nSize; i++ )
  456. if ( p->pArray[i] == Entry )
  457. break;
  458. if ( i == p->nSize )
  459. return 0;
  460. assert( i < p->nSize );
  461. for ( i++; i < p->nSize; i++ )
  462. p->pArray[i-1] = p->pArray[i];
  463. p->nSize--;
  464. return 1;
  465. }
  466. /**Function*************************************************************
  467. Synopsis [Comparison procedure for two integers.]
  468. Description []
  469. SideEffects []
  470. SeeAlso []
  471. ***********************************************************************/
  472. static inline int Vec_IntSortCompare1( int * pp1, int * pp2 )
  473. {
  474. // for some reason commenting out lines (as shown) led to crashing of the release version
  475. if ( *pp1 < *pp2 )
  476. return -1;
  477. if ( *pp1 > *pp2 ) //
  478. return 1;
  479. return 0; //
  480. }
  481. /**Function*************************************************************
  482. Synopsis [Comparison procedure for two integers.]
  483. Description []
  484. SideEffects []
  485. SeeAlso []
  486. ***********************************************************************/
  487. static inline int Vec_IntSortCompare2( int * pp1, int * pp2 )
  488. {
  489. // for some reason commenting out lines (as shown) led to crashing of the release version
  490. if ( *pp1 > *pp2 )
  491. return -1;
  492. if ( *pp1 < *pp2 ) //
  493. return 1;
  494. return 0; //
  495. }
  496. /**Function*************************************************************
  497. Synopsis [Sorting the entries by their integer value.]
  498. Description []
  499. SideEffects []
  500. SeeAlso []
  501. ***********************************************************************/
  502. static inline void Vec_IntSort( Vec_Int_t * p, int fReverse )
  503. {
  504. if ( fReverse )
  505. qsort( (void *)p->pArray, p->nSize, sizeof(int),
  506. (int (*)(const void *, const void *)) Vec_IntSortCompare2 );
  507. else
  508. qsort( (void *)p->pArray, p->nSize, sizeof(int),
  509. (int (*)(const void *, const void *)) Vec_IntSortCompare1 );
  510. }
  511. /**Function*************************************************************
  512. Synopsis [Comparison procedure for two integers.]
  513. Description []
  514. SideEffects []
  515. SeeAlso []
  516. ***********************************************************************/
  517. static inline int Vec_IntSortCompareUnsigned( unsigned * pp1, unsigned * pp2 )
  518. {
  519. if ( *pp1 < *pp2 )
  520. return -1;
  521. if ( *pp1 > *pp2 )
  522. return 1;
  523. return 0;
  524. }
  525. /**Function*************************************************************
  526. Synopsis [Sorting the entries by their integer value.]
  527. Description []
  528. SideEffects []
  529. SeeAlso []
  530. ***********************************************************************/
  531. static inline void Vec_IntSortUnsigned( Vec_Int_t * p )
  532. {
  533. qsort( (void *)p->pArray, p->nSize, sizeof(int),
  534. (int (*)(const void *, const void *)) Vec_IntSortCompareUnsigned );
  535. }
  536. /**Function*************************************************************
  537. Synopsis [Returns the number of common entries.]
  538. Description [Assumes that the vectors are sorted in the increasing order.]
  539. SideEffects []
  540. SeeAlso []
  541. ***********************************************************************/
  542. static inline int Vec_IntTwoCountCommon( Vec_Int_t * vArr1, Vec_Int_t * vArr2 )
  543. {
  544. int * pBeg1 = vArr1->pArray;
  545. int * pBeg2 = vArr2->pArray;
  546. int * pEnd1 = vArr1->pArray + vArr1->nSize;
  547. int * pEnd2 = vArr2->pArray + vArr2->nSize;
  548. int Counter = 0;
  549. while ( pBeg1 < pEnd1 && pBeg2 < pEnd2 )
  550. {
  551. if ( *pBeg1 == *pBeg2 )
  552. pBeg1++, pBeg2++, Counter++;
  553. else if ( *pBeg1 < *pBeg2 )
  554. pBeg1++;
  555. else
  556. pBeg2++;
  557. }
  558. return Counter;
  559. }
  560. /**Function*************************************************************
  561. Synopsis [Returns the result of merging the two vectors.]
  562. Description [Assumes that the vectors are sorted in the increasing order.]
  563. SideEffects []
  564. SeeAlso []
  565. ***********************************************************************/
  566. static inline Vec_Int_t * Vec_IntTwoMerge( Vec_Int_t * vArr1, Vec_Int_t * vArr2 )
  567. {
  568. Vec_Int_t * vArr = Vec_IntAlloc( vArr1->nSize + vArr2->nSize );
  569. int * pBeg = vArr->pArray;
  570. int * pBeg1 = vArr1->pArray;
  571. int * pBeg2 = vArr2->pArray;
  572. int * pEnd1 = vArr1->pArray + vArr1->nSize;
  573. int * pEnd2 = vArr2->pArray + vArr2->nSize;
  574. while ( pBeg1 < pEnd1 && pBeg2 < pEnd2 )
  575. {
  576. if ( *pBeg1 == *pBeg2 )
  577. *pBeg++ = *pBeg1++, pBeg2++;
  578. else if ( *pBeg1 < *pBeg2 )
  579. *pBeg++ = *pBeg1++;
  580. else
  581. *pBeg++ = *pBeg2++;
  582. }
  583. while ( pBeg1 < pEnd1 )
  584. *pBeg++ = *pBeg1++;
  585. while ( pBeg2 < pEnd2 )
  586. *pBeg++ = *pBeg2++;
  587. vArr->nSize = pBeg - vArr->pArray;
  588. assert( vArr->nSize <= vArr->nCap );
  589. assert( vArr->nSize >= vArr1->nSize );
  590. assert( vArr->nSize >= vArr2->nSize );
  591. return vArr;
  592. }
  593. #endif
  594. ////////////////////////////////////////////////////////////////////////
  595. /// END OF FILE ///
  596. ////////////////////////////////////////////////////////////////////////