1 /**CFile****************************************************************
2 
3   FileName    [vecPtr.h]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [Resizable arrays.]
8 
9   Synopsis    [Resizable arrays of generic pointers.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - June 20, 2005.]
16 
17   Revision    [$Id: vecPtr.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef ABC__misc__vec__vecPtr_h
22 #define ABC__misc__vec__vecPtr_h
23 
24 
25 ////////////////////////////////////////////////////////////////////////
26 ///                          INCLUDES                                ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 #include <stdio.h>
30 
31 ABC_NAMESPACE_HEADER_START
32 
33 
34 ////////////////////////////////////////////////////////////////////////
35 ///                         PARAMETERS                               ///
36 ////////////////////////////////////////////////////////////////////////
37 
38 ////////////////////////////////////////////////////////////////////////
39 ///                         BASIC TYPES                              ///
40 ////////////////////////////////////////////////////////////////////////
41 
42 typedef struct Vec_Ptr_t_       Vec_Ptr_t;
43 struct Vec_Ptr_t_
44 {
45     int              nCap;
46     int              nSize;
47     void **          pArray;
48 };
49 
50 ////////////////////////////////////////////////////////////////////////
51 ///                      MACRO DEFINITIONS                           ///
52 ////////////////////////////////////////////////////////////////////////
53 
54 // iterators through entries
55 #define Vec_PtrForEachEntry( Type, vVec, pEntry, i )                                               \
56     for ( i = 0; (i < Vec_PtrSize(vVec)) && (((pEntry) = (Type)Vec_PtrEntry(vVec, i)), 1); i++ )
57 #define Vec_PtrForEachEntryStart( Type, vVec, pEntry, i, Start )                                   \
58     for ( i = Start; (i < Vec_PtrSize(vVec)) && (((pEntry) = (Type)Vec_PtrEntry(vVec, i)), 1); i++ )
59 #define Vec_PtrForEachEntryStop( Type, vVec, pEntry, i, Stop )                                     \
60     for ( i = 0; (i < Stop) && (((pEntry) = (Type)Vec_PtrEntry(vVec, i)), 1); i++ )
61 #define Vec_PtrForEachEntryStartStop( Type, vVec, pEntry, i, Start, Stop )                         \
62     for ( i = Start; (i < Stop) && (((pEntry) = (Type)Vec_PtrEntry(vVec, i)), 1); i++ )
63 #define Vec_PtrForEachEntryReverse( Type, vVec, pEntry, i )                                        \
64     for ( i = Vec_PtrSize(vVec) - 1; (i >= 0) && (((pEntry) = (Type)Vec_PtrEntry(vVec, i)), 1); i-- )
65 #define Vec_PtrForEachEntryTwo( Type1, vVec1, Type2, vVec2, pEntry1, pEntry2, i )                  \
66     for ( i = 0; (i < Vec_PtrSize(vVec1)) && (((pEntry1) = (Type1)Vec_PtrEntry(vVec1, i)), 1) && (((pEntry2) = (Type2)Vec_PtrEntry(vVec2, i)), 1); i++ )
67 #define Vec_PtrForEachEntryDouble( Type1, Type2, vVec, Entry1, Entry2, i )                                \
68     for ( i = 0; (i+1 < Vec_PtrSize(vVec)) && (((Entry1) = (Type1)Vec_PtrEntry(vVec, i)), 1) && (((Entry2) = (Type2)Vec_PtrEntry(vVec, i+1)), 1); i += 2 )
69 
70 ////////////////////////////////////////////////////////////////////////
71 ///                     FUNCTION DEFINITIONS                         ///
72 ////////////////////////////////////////////////////////////////////////
73 
74 /**Function*************************************************************
75 
76   Synopsis    [Allocates a vector with the given capacity.]
77 
78   Description []
79 
80   SideEffects []
81 
82   SeeAlso     []
83 
84 ***********************************************************************/
Vec_PtrAlloc(int nCap)85 static inline Vec_Ptr_t * Vec_PtrAlloc( int nCap )
86 {
87     Vec_Ptr_t * p;
88     p = ABC_ALLOC( Vec_Ptr_t, 1 );
89     if ( nCap > 0 && nCap < 8 )
90         nCap = 8;
91     p->nSize  = 0;
92     p->nCap   = nCap;
93     p->pArray = p->nCap? ABC_ALLOC( void *, p->nCap ) : NULL;
94     return p;
95 }
Vec_PtrAllocExact(int nCap)96 static inline Vec_Ptr_t * Vec_PtrAllocExact( int nCap )
97 {
98     Vec_Ptr_t * p;
99     assert( nCap >= 0 );
100     p = ABC_ALLOC( Vec_Ptr_t, 1 );
101     p->nSize  = 0;
102     p->nCap   = nCap;
103     p->pArray = p->nCap? ABC_ALLOC( void *, p->nCap ) : NULL;
104     return p;
105 }
106 
107 /**Function*************************************************************
108 
109   Synopsis    [Allocates a vector with the given size and cleans it.]
110 
111   Description []
112 
113   SideEffects []
114 
115   SeeAlso     []
116 
117 ***********************************************************************/
Vec_PtrStart(int nSize)118 static inline Vec_Ptr_t * Vec_PtrStart( int nSize )
119 {
120     Vec_Ptr_t * p;
121     p = Vec_PtrAlloc( nSize );
122     p->nSize = nSize;
123     memset( p->pArray, 0, sizeof(void *) * (size_t)nSize );
124     return p;
125 }
126 
127 /**Function*************************************************************
128 
129   Synopsis    [Creates the vector from an integer array of the given size.]
130 
131   Description []
132 
133   SideEffects []
134 
135   SeeAlso     []
136 
137 ***********************************************************************/
Vec_PtrAllocArray(void ** pArray,int nSize)138 static inline Vec_Ptr_t * Vec_PtrAllocArray( void ** pArray, int nSize )
139 {
140     Vec_Ptr_t * p;
141     p = ABC_ALLOC( Vec_Ptr_t, 1 );
142     p->nSize  = nSize;
143     p->nCap   = nSize;
144     p->pArray = pArray;
145     return p;
146 }
147 
148 /**Function*************************************************************
149 
150   Synopsis    [Creates the vector from an integer array of the given size.]
151 
152   Description []
153 
154   SideEffects []
155 
156   SeeAlso     []
157 
158 ***********************************************************************/
Vec_PtrAllocArrayCopy(void ** pArray,int nSize)159 static inline Vec_Ptr_t * Vec_PtrAllocArrayCopy( void ** pArray, int nSize )
160 {
161     Vec_Ptr_t * p;
162     p = ABC_ALLOC( Vec_Ptr_t, 1 );
163     p->nSize  = nSize;
164     p->nCap   = nSize;
165     p->pArray = ABC_ALLOC( void *, nSize );
166     memcpy( p->pArray, pArray, sizeof(void *) * (size_t)nSize );
167     return p;
168 }
169 
170 /**Function*************************************************************
171 
172   Synopsis    [Duplicates the integer array.]
173 
174   Description []
175 
176   SideEffects []
177 
178   SeeAlso     []
179 
180 ***********************************************************************/
Vec_PtrDup(Vec_Ptr_t * pVec)181 static inline Vec_Ptr_t * Vec_PtrDup( Vec_Ptr_t * pVec )
182 {
183     Vec_Ptr_t * p;
184     p = ABC_ALLOC( Vec_Ptr_t, 1 );
185     p->nSize  = pVec->nSize;
186     p->nCap   = pVec->nCap;
187     p->pArray = p->nCap? ABC_ALLOC( void *, p->nCap ) : NULL;
188     memcpy( p->pArray, pVec->pArray, sizeof(void *) * (size_t)pVec->nSize );
189     return p;
190 }
Vec_PtrDupStr(Vec_Ptr_t * pVec)191 static inline Vec_Ptr_t * Vec_PtrDupStr( Vec_Ptr_t * pVec )
192 {
193     int i;
194     Vec_Ptr_t * p = Vec_PtrDup( pVec );
195     for ( i = 0; i < p->nSize; i++ )
196         p->pArray[i] = Abc_UtilStrsav( (char *)p->pArray[i] );
197     return p;
198 }
199 
200 /**Function*************************************************************
201 
202   Synopsis    [Transfers the array into another vector.]
203 
204   Description []
205 
206   SideEffects []
207 
208   SeeAlso     []
209 
210 ***********************************************************************/
Vec_PtrDupArray(Vec_Ptr_t * pVec)211 static inline Vec_Ptr_t * Vec_PtrDupArray( Vec_Ptr_t * pVec )
212 {
213     Vec_Ptr_t * p;
214     p = ABC_ALLOC( Vec_Ptr_t, 1 );
215     p->nSize  = pVec->nSize;
216     p->nCap   = pVec->nCap;
217     p->pArray = pVec->pArray;
218     pVec->nSize  = 0;
219     pVec->nCap   = 0;
220     pVec->pArray = NULL;
221     return p;
222 }
223 
224 /**Function*************************************************************
225 
226   Synopsis    [Frees the vector.]
227 
228   Description []
229 
230   SideEffects []
231 
232   SeeAlso     []
233 
234 ***********************************************************************/
Vec_PtrZero(Vec_Ptr_t * p)235 static inline void Vec_PtrZero( Vec_Ptr_t * p )
236 {
237     p->pArray = NULL;
238     p->nSize = 0;
239     p->nCap = 0;
240 }
Vec_PtrErase(Vec_Ptr_t * p)241 static inline void Vec_PtrErase( Vec_Ptr_t * p )
242 {
243     ABC_FREE( p->pArray );
244     p->nSize = 0;
245     p->nCap = 0;
246 }
Vec_PtrFree(Vec_Ptr_t * p)247 static inline void Vec_PtrFree( Vec_Ptr_t * p )
248 {
249     ABC_FREE( p->pArray );
250     ABC_FREE( p );
251 }
252 
253 /**Function*************************************************************
254 
255   Synopsis    []
256 
257   Description []
258 
259   SideEffects []
260 
261   SeeAlso     []
262 
263 ***********************************************************************/
Vec_PtrFreeP(Vec_Ptr_t ** p)264 static inline void Vec_PtrFreeP( Vec_Ptr_t ** p )
265 {
266     if ( *p == NULL )
267         return;
268     ABC_FREE( (*p)->pArray );
269     ABC_FREE( (*p) );
270 }
271 
272 /**Function*************************************************************
273 
274   Synopsis    []
275 
276   Description []
277 
278   SideEffects []
279 
280   SeeAlso     []
281 
282 ***********************************************************************/
Vec_PtrReleaseArray(Vec_Ptr_t * p)283 static inline void ** Vec_PtrReleaseArray( Vec_Ptr_t * p )
284 {
285     void ** pArray = p->pArray;
286     p->nCap = 0;
287     p->nSize = 0;
288     p->pArray = NULL;
289     return pArray;
290 }
291 
292 /**Function*************************************************************
293 
294   Synopsis    []
295 
296   Description []
297 
298   SideEffects []
299 
300   SeeAlso     []
301 
302 ***********************************************************************/
Vec_PtrArray(Vec_Ptr_t * p)303 static inline void ** Vec_PtrArray( Vec_Ptr_t * p )
304 {
305     return p->pArray;
306 }
307 
308 /**Function*************************************************************
309 
310   Synopsis    []
311 
312   Description []
313 
314   SideEffects []
315 
316   SeeAlso     []
317 
318 ***********************************************************************/
Vec_PtrSize(Vec_Ptr_t * p)319 static inline int Vec_PtrSize( Vec_Ptr_t * p )
320 {
321     return p->nSize;
322 }
323 
324 /**Function*************************************************************
325 
326   Synopsis    []
327 
328   Description []
329 
330   SideEffects []
331 
332   SeeAlso     []
333 
334 ***********************************************************************/
Vec_PtrCap(Vec_Ptr_t * p)335 static inline int Vec_PtrCap( Vec_Ptr_t * p )
336 {
337     return p->nCap;
338 }
339 
340 /**Function*************************************************************
341 
342   Synopsis    []
343 
344   Description []
345 
346   SideEffects []
347 
348   SeeAlso     []
349 
350 ***********************************************************************/
Vec_PtrMemory(Vec_Ptr_t * p)351 static inline double Vec_PtrMemory( Vec_Ptr_t * p )
352 {
353     return !p ? 0.0 : 1.0 * sizeof(void *) * (size_t)p->nCap + sizeof(Vec_Ptr_t);
354 }
355 
356 /**Function*************************************************************
357 
358   Synopsis    []
359 
360   Description []
361 
362   SideEffects []
363 
364   SeeAlso     []
365 
366 ***********************************************************************/
Vec_PtrCountZero(Vec_Ptr_t * p)367 static inline int Vec_PtrCountZero( Vec_Ptr_t * p )
368 {
369     int i, Counter = 0;
370     for ( i = 0; i < p->nSize; i++ )
371         Counter += (p->pArray[i] == NULL);
372     return Counter;
373 }
374 
375 /**Function*************************************************************
376 
377   Synopsis    []
378 
379   Description []
380 
381   SideEffects []
382 
383   SeeAlso     []
384 
385 ***********************************************************************/
Vec_PtrEntry(Vec_Ptr_t * p,int i)386 static inline void * Vec_PtrEntry( Vec_Ptr_t * p, int i )
387 {
388     assert( i >= 0 && i < p->nSize );
389     return p->pArray[i];
390 }
391 
392 /**Function*************************************************************
393 
394   Synopsis    []
395 
396   Description []
397 
398   SideEffects []
399 
400   SeeAlso     []
401 
402 ***********************************************************************/
Vec_PtrEntryP(Vec_Ptr_t * p,int i)403 static inline void ** Vec_PtrEntryP( Vec_Ptr_t * p, int i )
404 {
405     assert( i >= 0 && i < p->nSize );
406     return p->pArray + i;
407 }
408 
409 /**Function*************************************************************
410 
411   Synopsis    []
412 
413   Description []
414 
415   SideEffects []
416 
417   SeeAlso     []
418 
419 ***********************************************************************/
Vec_PtrWriteEntry(Vec_Ptr_t * p,int i,void * Entry)420 static inline void Vec_PtrWriteEntry( Vec_Ptr_t * p, int i, void * Entry )
421 {
422     assert( i >= 0 && i < p->nSize );
423     p->pArray[i] = Entry;
424 }
425 
426 /**Function*************************************************************
427 
428   Synopsis    []
429 
430   Description []
431 
432   SideEffects []
433 
434   SeeAlso     []
435 
436 ***********************************************************************/
Vec_PtrEntryLast(Vec_Ptr_t * p)437 static inline void * Vec_PtrEntryLast( Vec_Ptr_t * p )
438 {
439     assert( p->nSize > 0 );
440     return p->pArray[p->nSize-1];
441 }
442 
443 /**Function*************************************************************
444 
445   Synopsis    [Resizes the vector to the given capacity.]
446 
447   Description []
448 
449   SideEffects []
450 
451   SeeAlso     []
452 
453 ***********************************************************************/
Vec_PtrGrow(Vec_Ptr_t * p,int nCapMin)454 static inline void Vec_PtrGrow( Vec_Ptr_t * p, int nCapMin )
455 {
456     if ( p->nCap >= nCapMin )
457         return;
458     p->pArray = ABC_REALLOC( void *, p->pArray, nCapMin );
459     p->nCap   = nCapMin;
460 }
461 
462 /**Function*************************************************************
463 
464   Synopsis    [Fills the vector with given number of entries.]
465 
466   Description []
467 
468   SideEffects []
469 
470   SeeAlso     []
471 
472 ***********************************************************************/
Vec_PtrFill(Vec_Ptr_t * p,int nSize,void * Entry)473 static inline void Vec_PtrFill( Vec_Ptr_t * p, int nSize, void * Entry )
474 {
475     int i;
476     Vec_PtrGrow( p, nSize );
477     for ( i = 0; i < nSize; i++ )
478         p->pArray[i] = Entry;
479     p->nSize = nSize;
480 }
Vec_PtrFillTwo(Vec_Ptr_t * p,int nSize,void * EntryEven,void * EntryOdd)481 static inline void Vec_PtrFillTwo( Vec_Ptr_t * p, int nSize, void * EntryEven, void * EntryOdd )
482 {
483     int i;
484     Vec_PtrGrow( p, nSize );
485     for ( i = 0; i < nSize; i++ )
486         p->pArray[i] = (i & 1) ? EntryOdd : EntryEven;
487     p->nSize = nSize;
488 }
489 
490 /**Function*************************************************************
491 
492   Synopsis    [Fills the vector with given number of entries.]
493 
494   Description []
495 
496   SideEffects []
497 
498   SeeAlso     []
499 
500 ***********************************************************************/
Vec_PtrFillExtra(Vec_Ptr_t * p,int nSize,void * Fill)501 static inline void Vec_PtrFillExtra( Vec_Ptr_t * p, int nSize, void * Fill )
502 {
503     int i;
504     if ( nSize <= p->nSize )
505         return;
506     if ( nSize > 2 * p->nCap )
507         Vec_PtrGrow( p, nSize );
508     else if ( nSize > p->nCap )
509         Vec_PtrGrow( p, 2 * p->nCap );
510     for ( i = p->nSize; i < nSize; i++ )
511         p->pArray[i] = Fill;
512     p->nSize = nSize;
513 }
514 
515 /**Function*************************************************************
516 
517   Synopsis    [Returns the entry even if the place not exist.]
518 
519   Description []
520 
521   SideEffects []
522 
523   SeeAlso     []
524 
525 ***********************************************************************/
Vec_PtrGetEntry(Vec_Ptr_t * p,int i)526 static inline void * Vec_PtrGetEntry( Vec_Ptr_t * p, int i )
527 {
528     Vec_PtrFillExtra( p, i + 1, NULL );
529     return Vec_PtrEntry( p, i );
530 }
531 
532 /**Function*************************************************************
533 
534   Synopsis    [Inserts the entry even if the place does not exist.]
535 
536   Description []
537 
538   SideEffects []
539 
540   SeeAlso     []
541 
542 ***********************************************************************/
Vec_PtrSetEntry(Vec_Ptr_t * p,int i,void * Entry)543 static inline void Vec_PtrSetEntry( Vec_Ptr_t * p, int i, void * Entry )
544 {
545     Vec_PtrFillExtra( p, i + 1, NULL );
546     Vec_PtrWriteEntry( p, i, Entry );
547 }
548 
549 /**Function*************************************************************
550 
551   Synopsis    []
552 
553   Description []
554 
555   SideEffects []
556 
557   SeeAlso     []
558 
559 ***********************************************************************/
Vec_PtrShrink(Vec_Ptr_t * p,int nSizeNew)560 static inline void Vec_PtrShrink( Vec_Ptr_t * p, int nSizeNew )
561 {
562     assert( p->nSize >= nSizeNew );
563     p->nSize = nSizeNew;
564 }
565 
566 /**Function*************************************************************
567 
568   Synopsis    []
569 
570   Description []
571 
572   SideEffects []
573 
574   SeeAlso     []
575 
576 ***********************************************************************/
Vec_PtrClear(Vec_Ptr_t * p)577 static inline void Vec_PtrClear( Vec_Ptr_t * p )
578 {
579     p->nSize = 0;
580 }
581 
582 /**Function*************************************************************
583 
584   Synopsis    [Deallocates array of memory pointers.]
585 
586   Description []
587 
588   SideEffects []
589 
590   SeeAlso     []
591 
592 ***********************************************************************/
Vec_PtrFreeData(Vec_Ptr_t * p)593 static inline void Vec_PtrFreeData( Vec_Ptr_t * p )
594 {
595     void * pTemp; int i;
596     if ( p == NULL ) return;
597     Vec_PtrForEachEntry( void *, p, pTemp, i )
598         if ( pTemp != (void *)(ABC_PTRINT_T)1 && pTemp != (void *)(ABC_PTRINT_T)2 )
599             ABC_FREE( pTemp );
600 }
Vec_PtrFreeFree(Vec_Ptr_t * p)601 static inline void Vec_PtrFreeFree( Vec_Ptr_t * p )
602 {
603     if ( p == NULL ) return;
604     Vec_PtrFreeData( p );
605     Vec_PtrFree( p );
606 }
607 
608 /**Function*************************************************************
609 
610   Synopsis    [Copies the interger array.]
611 
612   Description []
613 
614   SideEffects []
615 
616   SeeAlso     []
617 
618 ***********************************************************************/
Vec_PtrCopy(Vec_Ptr_t * pDest,Vec_Ptr_t * pSour)619 static inline void Vec_PtrCopy( Vec_Ptr_t * pDest, Vec_Ptr_t * pSour )
620 {
621     pDest->nSize = 0;
622     Vec_PtrGrow( pDest, pSour->nSize );
623     memcpy( pDest->pArray, pSour->pArray, sizeof(void *) * (size_t)pSour->nSize );
624     pDest->nSize = pSour->nSize;
625 }
626 
627 /**Function*************************************************************
628 
629   Synopsis    []
630 
631   Description []
632 
633   SideEffects []
634 
635   SeeAlso     []
636 
637 ***********************************************************************/
Vec_PtrPush(Vec_Ptr_t * p,void * Entry)638 static inline void Vec_PtrPush( Vec_Ptr_t * p, void * Entry )
639 {
640     if ( p->nSize == p->nCap )
641     {
642         if ( p->nCap < 16 )
643             Vec_PtrGrow( p, 16 );
644         else
645             Vec_PtrGrow( p, 2 * p->nCap );
646     }
647     p->pArray[p->nSize++] = Entry;
648 }
Vec_PtrPushTwo(Vec_Ptr_t * p,void * Entry1,void * Entry2)649 static inline void Vec_PtrPushTwo( Vec_Ptr_t * p, void * Entry1, void * Entry2 )
650 {
651     Vec_PtrPush( p, Entry1 );
652     Vec_PtrPush( p, Entry2 );
653 }
654 
655 /**Function*************************************************************
656 
657   Synopsis    []
658 
659   Description []
660 
661   SideEffects []
662 
663   SeeAlso     []
664 
665 ***********************************************************************/
Vec_PtrPushFirst(Vec_Ptr_t * p,void * Entry)666 static inline void Vec_PtrPushFirst( Vec_Ptr_t * p, void * Entry )
667 {
668     int i;
669     if ( p->nSize == p->nCap )
670     {
671         if ( p->nCap < 16 )
672             Vec_PtrGrow( p, 16 );
673         else
674             Vec_PtrGrow( p, 2 * p->nCap );
675     }
676     p->nSize++;
677     for ( i = p->nSize - 1; i >= 1; i-- )
678         p->pArray[i] = p->pArray[i-1];
679     p->pArray[0] = Entry;
680 }
681 
682 /**Function*************************************************************
683 
684   Synopsis    []
685 
686   Description []
687 
688   SideEffects []
689 
690   SeeAlso     []
691 
692 ***********************************************************************/
Vec_PtrPushUnique(Vec_Ptr_t * p,void * Entry)693 static inline int Vec_PtrPushUnique( Vec_Ptr_t * p, void * Entry )
694 {
695     int i;
696     for ( i = 0; i < p->nSize; i++ )
697         if ( p->pArray[i] == Entry )
698             return 1;
699     Vec_PtrPush( p, Entry );
700     return 0;
701 }
702 
703 /**Function*************************************************************
704 
705   Synopsis    [Returns the last entry and removes it from the list.]
706 
707   Description []
708 
709   SideEffects []
710 
711   SeeAlso     []
712 
713 ***********************************************************************/
Vec_PtrPop(Vec_Ptr_t * p)714 static inline void * Vec_PtrPop( Vec_Ptr_t * p )
715 {
716     assert( p->nSize > 0 );
717     return p->pArray[--p->nSize];
718 }
719 
720 /**Function*************************************************************
721 
722   Synopsis    [Find entry.]
723 
724   Description []
725 
726   SideEffects []
727 
728   SeeAlso     []
729 
730 ***********************************************************************/
Vec_PtrFind(Vec_Ptr_t * p,void * Entry)731 static inline int Vec_PtrFind( Vec_Ptr_t * p, void * Entry )
732 {
733     int i;
734     for ( i = 0; i < p->nSize; i++ )
735         if ( p->pArray[i] == Entry )
736             return i;
737     return -1;
738 }
Vec_PtrFindStr(Vec_Ptr_t * p,char * Entry)739 static inline int Vec_PtrFindStr( Vec_Ptr_t * p, char * Entry )
740 {
741     int i;
742     for ( i = 0; i < p->nSize; i++ )
743         if ( p->pArray[i] && !strcmp((char *)p->pArray[i], Entry) )
744             return i;
745     return -1;
746 }
747 
748 /**Function*************************************************************
749 
750   Synopsis    []
751 
752   Description []
753 
754   SideEffects []
755 
756   SeeAlso     []
757 
758 ***********************************************************************/
Vec_PtrRemove(Vec_Ptr_t * p,void * Entry)759 static inline void Vec_PtrRemove( Vec_Ptr_t * p, void * Entry )
760 {
761     int i;
762     // delete assuming that it is closer to the end
763     for ( i = p->nSize - 1; i >= 0; i-- )
764         if ( p->pArray[i] == Entry )
765             break;
766     assert( i >= 0 );
767 /*
768     // delete assuming that it is closer to the beginning
769     for ( i = 0; i < p->nSize; i++ )
770         if ( p->pArray[i] == Entry )
771             break;
772     assert( i < p->nSize );
773 */
774     for ( i++; i < p->nSize; i++ )
775         p->pArray[i-1] = p->pArray[i];
776     p->nSize--;
777 }
778 
779 /**Function*************************************************************
780 
781   Synopsis    []
782 
783   Description []
784 
785   SideEffects []
786 
787   SeeAlso     []
788 
789 ***********************************************************************/
Vec_PtrDrop(Vec_Ptr_t * p,int i)790 static inline void Vec_PtrDrop( Vec_Ptr_t * p, int i )
791 {
792     int k;
793     assert( i >= 0 && i < Vec_PtrSize(p) );
794     p->nSize--;
795     for ( k = i; k < p->nSize; k++ )
796         p->pArray[k] = p->pArray[k+1];
797 }
798 
799 /**Function*************************************************************
800 
801   Synopsis    [Interts entry at the index iHere. Shifts other entries.]
802 
803   Description []
804 
805   SideEffects []
806 
807   SeeAlso     []
808 
809 ***********************************************************************/
Vec_PtrInsert(Vec_Ptr_t * p,int iHere,void * Entry)810 static inline void Vec_PtrInsert( Vec_Ptr_t * p, int iHere, void * Entry )
811 {
812     int i;
813     assert( iHere >= 0 && iHere < p->nSize );
814     Vec_PtrPush( p, 0 );
815     for ( i = p->nSize - 1; i > iHere; i-- )
816         p->pArray[i] = p->pArray[i-1];
817     p->pArray[i] = Entry;
818 }
819 
820 /**Function*************************************************************
821 
822   Synopsis    [Moves the first nItems to the end.]
823 
824   Description []
825 
826   SideEffects []
827 
828   SeeAlso     []
829 
830 ***********************************************************************/
Vec_PtrReorder(Vec_Ptr_t * p,int nItems)831 static inline void Vec_PtrReorder( Vec_Ptr_t * p, int nItems )
832 {
833     assert( nItems < p->nSize );
834     Vec_PtrGrow( p, nItems + p->nSize );
835     memmove( (char **)p->pArray + p->nSize, p->pArray, (size_t)nItems * sizeof(void*) );
836     memmove( p->pArray, (char **)p->pArray + nItems, (size_t)p->nSize * sizeof(void*) );
837 }
838 
839 /**Function*************************************************************
840 
841   Synopsis    [Reverses the order of entries.]
842 
843   Description []
844 
845   SideEffects []
846 
847   SeeAlso     []
848 
849 ***********************************************************************/
Vec_PtrReverseOrder(Vec_Ptr_t * p)850 static inline void Vec_PtrReverseOrder( Vec_Ptr_t * p )
851 {
852     void * Temp;
853     int i;
854     for ( i = 0; i < p->nSize/2; i++ )
855     {
856         Temp = p->pArray[i];
857         p->pArray[i] = p->pArray[p->nSize-1-i];
858         p->pArray[p->nSize-1-i] = Temp;
859     }
860 }
861 
862 /**Function*************************************************************
863 
864   Synopsis    [Checks if two vectors are equal.]
865 
866   Description []
867 
868   SideEffects []
869 
870   SeeAlso     []
871 
872 ***********************************************************************/
Vec_PtrEqual(Vec_Ptr_t * p1,Vec_Ptr_t * p2)873 static inline int Vec_PtrEqual( Vec_Ptr_t * p1, Vec_Ptr_t * p2 )
874 {
875     int i;
876     if ( p1->nSize != p2->nSize )
877         return 0;
878     for ( i = 0; i < p1->nSize; i++ )
879         if ( p1->pArray[i] != p2->pArray[i] )
880             return 0;
881     return 1;
882 }
883 
884 /**Function*************************************************************
885 
886   Synopsis    [Comparison procedure for two integers.]
887 
888   Description []
889 
890   SideEffects []
891 
892   SeeAlso     []
893 
894 ***********************************************************************/
Vec_PtrSortComparePtr(void ** pp1,void ** pp2)895 static int Vec_PtrSortComparePtr( void ** pp1, void ** pp2 )
896 {
897     if ( *pp1 < *pp2 )
898         return -1;
899     if ( *pp1 > *pp2 )
900         return 1;
901     return 0;
902 }
903 
904 /**Function*************************************************************
905 
906   Synopsis    [Sorting the entries by their integer value.]
907 
908   Description []
909 
910   SideEffects []
911 
912   SeeAlso     []
913 
914 ***********************************************************************/
915 static void Vec_PtrSort( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)() ) ___unused;
Vec_PtrSort(Vec_Ptr_t * p,int (* Vec_PtrSortCompare)())916 static void Vec_PtrSort( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)() )
917 {
918     if ( p->nSize < 2 )
919         return;
920     if ( Vec_PtrSortCompare == NULL )
921         qsort( (void *)p->pArray, (size_t)p->nSize, sizeof(void *),
922                 (int (*)(const void *, const void *)) Vec_PtrSortComparePtr );
923     else
924         qsort( (void *)p->pArray, (size_t)p->nSize, sizeof(void *),
925                 (int (*)(const void *, const void *)) Vec_PtrSortCompare );
926 }
927 
928 /**Function*************************************************************
929 
930   Synopsis    [Sorting the entries by their integer value.]
931 
932   Description []
933 
934   SideEffects []
935 
936   SeeAlso     []
937 
938 ***********************************************************************/
939 static void Vec_PtrUniqify( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)() ) ___unused;
Vec_PtrUniqify(Vec_Ptr_t * p,int (* Vec_PtrSortCompare)())940 static void Vec_PtrUniqify( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)() )
941 {
942     int i, k;
943     if ( p->nSize < 2 )
944         return;
945     Vec_PtrSort( p, Vec_PtrSortCompare );
946     for ( i = k = 1; i < p->nSize; i++ )
947         if ( p->pArray[i] != p->pArray[i-1] )
948             p->pArray[k++] = p->pArray[i];
949     p->nSize = k;
950 }
951 static void Vec_PtrUniqify2( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)(void**, void**), void (*Vec_PtrObjFree)(void*), Vec_Int_t * vCounts ) ___unused;
Vec_PtrUniqify2(Vec_Ptr_t * p,int (* Vec_PtrSortCompare)(void **,void **),void (* Vec_PtrObjFree)(void *),Vec_Int_t * vCounts)952 static void Vec_PtrUniqify2( Vec_Ptr_t * p, int (*Vec_PtrSortCompare)(void**, void**), void (*Vec_PtrObjFree)(void*), Vec_Int_t * vCounts )
953 {
954     int i, k;
955     if ( vCounts )
956         Vec_IntFill( vCounts, 1, 1 );
957     if ( p->nSize < 2 )
958         return;
959     Vec_PtrSort( p, (int (*)())Vec_PtrSortCompare );
960     for ( i = k = 1; i < p->nSize; i++ )
961         if ( Vec_PtrSortCompare(p->pArray+i, p->pArray+k-1) != 0 )
962         {
963             p->pArray[k++] = p->pArray[i];
964             if ( vCounts )
965                 Vec_IntPush( vCounts, 1 );
966         }
967         else
968         {
969             if ( Vec_PtrObjFree )
970                 Vec_PtrObjFree( p->pArray[i] );
971             if ( vCounts )
972                 Vec_IntAddToEntry( vCounts, Vec_IntSize(vCounts)-1, 1 );
973         }
974     p->nSize = k;
975     assert( vCounts == NULL || Vec_IntSize(vCounts) == Vec_PtrSize(p) );
976 }
977 
978 
979 
980 /**Function*************************************************************
981 
982   Synopsis    [Allocates the array of simulation info.]
983 
984   Description [Allocates the array containing given number of entries,
985   each of which contains given number of unsigned words of simulation data.
986   The resulting array can be freed using regular procedure Vec_PtrFree().
987   It is the responsibility of the user to ensure this array is never grown.]
988 
989   SideEffects []
990 
991   SeeAlso     []
992 
993 ***********************************************************************/
Vec_PtrAllocSimInfo(int nEntries,int nWords)994 static inline Vec_Ptr_t * Vec_PtrAllocSimInfo( int nEntries, int nWords )
995 {
996     void ** pMemory;
997     unsigned * pInfo;
998     int i;
999     pMemory = (void **)ABC_ALLOC( char, (sizeof(void *) + sizeof(unsigned) * (size_t)nWords) * nEntries );
1000     pInfo = (unsigned *)(pMemory + nEntries);
1001     for ( i = 0; i < nEntries; i++ )
1002         pMemory[i] = pInfo + i * nWords;
1003     return Vec_PtrAllocArray( pMemory, nEntries );
1004 }
1005 
1006 /**Function*************************************************************
1007 
1008   Synopsis    [Cleans simulation info of each entry beginning with iWord.]
1009 
1010   Description []
1011 
1012   SideEffects []
1013 
1014   SeeAlso     []
1015 
1016 ***********************************************************************/
Vec_PtrReadWordsSimInfo(Vec_Ptr_t * p)1017 static inline int Vec_PtrReadWordsSimInfo( Vec_Ptr_t * p )
1018 {
1019     return (unsigned *)Vec_PtrEntry(p,1) - (unsigned *)Vec_PtrEntry(p,0);
1020 }
1021 
1022 /**Function*************************************************************
1023 
1024   Synopsis    [Cleans simulation info of each entry beginning with iWord.]
1025 
1026   Description []
1027 
1028   SideEffects []
1029 
1030   SeeAlso     []
1031 
1032 ***********************************************************************/
Vec_PtrCleanSimInfo(Vec_Ptr_t * vInfo,int iWord,int nWords)1033 static inline void Vec_PtrCleanSimInfo( Vec_Ptr_t * vInfo, int iWord, int nWords )
1034 {
1035     int i;
1036     for ( i = 0; i < vInfo->nSize; i++ )
1037         memset( (char*)Vec_PtrEntry(vInfo,i) + 4*iWord, 0, (size_t)(4*(nWords-iWord)) );
1038 }
1039 
1040 /**Function*************************************************************
1041 
1042   Synopsis    [Cleans simulation info of each entry beginning with iWord.]
1043 
1044   Description []
1045 
1046   SideEffects []
1047 
1048   SeeAlso     []
1049 
1050 ***********************************************************************/
Vec_PtrFillSimInfo(Vec_Ptr_t * vInfo,int iWord,int nWords)1051 static inline void Vec_PtrFillSimInfo( Vec_Ptr_t * vInfo, int iWord, int nWords )
1052 {
1053     int i;
1054     for ( i = 0; i < vInfo->nSize; i++ )
1055         memset( (char*)Vec_PtrEntry(vInfo,i) + 4*iWord, 0xFF, (size_t)(4*(nWords-iWord)) );
1056 }
1057 
1058 /**Function*************************************************************
1059 
1060   Synopsis    [Resizes the array of simulation info.]
1061 
1062   Description [Doubles the number of objects for which siminfo is allocated.]
1063 
1064   SideEffects []
1065 
1066   SeeAlso     []
1067 
1068 ***********************************************************************/
Vec_PtrDoubleSimInfo(Vec_Ptr_t * vInfo)1069 static inline void Vec_PtrDoubleSimInfo( Vec_Ptr_t * vInfo )
1070 {
1071     Vec_Ptr_t * vInfoNew;
1072     int nWords;
1073     assert( Vec_PtrSize(vInfo) > 1 );
1074     // get the new array
1075     nWords = (unsigned *)Vec_PtrEntry(vInfo,1) - (unsigned *)Vec_PtrEntry(vInfo,0);
1076     vInfoNew = Vec_PtrAllocSimInfo( 2*Vec_PtrSize(vInfo), nWords );
1077     // copy the simulation info
1078     memcpy( Vec_PtrEntry(vInfoNew,0), Vec_PtrEntry(vInfo,0), (size_t)(Vec_PtrSize(vInfo) * nWords * 4) );
1079     // replace the array
1080     ABC_FREE( vInfo->pArray );
1081     vInfo->pArray = vInfoNew->pArray;
1082     vInfo->nSize *= 2;
1083     vInfo->nCap *= 2;
1084     // free the old array
1085     vInfoNew->pArray = NULL;
1086     ABC_FREE( vInfoNew );
1087 }
1088 
1089 /**Function*************************************************************
1090 
1091   Synopsis    [Resizes the array of simulation info.]
1092 
1093   Description [Doubles the number of simulation patterns stored for each object.]
1094 
1095   SideEffects []
1096 
1097   SeeAlso     []
1098 
1099 ***********************************************************************/
Vec_PtrReallocSimInfo(Vec_Ptr_t * vInfo)1100 static inline void Vec_PtrReallocSimInfo( Vec_Ptr_t * vInfo )
1101 {
1102     Vec_Ptr_t * vInfoNew;
1103     int nWords, i;
1104     assert( Vec_PtrSize(vInfo) > 1 );
1105     // get the new array
1106     nWords = (unsigned *)Vec_PtrEntry(vInfo,1) - (unsigned *)Vec_PtrEntry(vInfo,0);
1107     vInfoNew = Vec_PtrAllocSimInfo( Vec_PtrSize(vInfo), 2*nWords );
1108     // copy the simulation info
1109     for ( i = 0; i < vInfo->nSize; i++ )
1110         memcpy( Vec_PtrEntry(vInfoNew,i), Vec_PtrEntry(vInfo,i), (size_t)(nWords * 4) );
1111     // replace the array
1112     ABC_FREE( vInfo->pArray );
1113     vInfo->pArray = vInfoNew->pArray;
1114     // free the old array
1115     vInfoNew->pArray = NULL;
1116     ABC_FREE( vInfoNew );
1117 }
1118 
1119 /**Function*************************************************************
1120 
1121   Synopsis    [Allocates the array of truth tables for the given number of vars.]
1122 
1123   Description []
1124 
1125   SideEffects []
1126 
1127   SeeAlso     []
1128 
1129 ***********************************************************************/
Vec_PtrAllocTruthTables(int nVars)1130 static inline Vec_Ptr_t * Vec_PtrAllocTruthTables( int nVars )
1131 {
1132     Vec_Ptr_t * p;
1133     unsigned Masks[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
1134     unsigned * pTruth;
1135     int i, k, nWords;
1136     nWords = (nVars <= 5 ? 1 : (1 << (nVars - 5)));
1137     p = Vec_PtrAllocSimInfo( nVars, nWords );
1138     for ( i = 0; i < nVars; i++ )
1139     {
1140         pTruth = (unsigned *)p->pArray[i];
1141         if ( i < 5 )
1142         {
1143             for ( k = 0; k < nWords; k++ )
1144                 pTruth[k] = Masks[i];
1145         }
1146         else
1147         {
1148             for ( k = 0; k < nWords; k++ )
1149                 if ( k & (1 << (i-5)) )
1150                     pTruth[k] = ~(unsigned)0;
1151                 else
1152                     pTruth[k] = 0;
1153         }
1154     }
1155     return p;
1156 }
1157 
1158 
1159 
1160 ABC_NAMESPACE_HEADER_END
1161 
1162 #endif
1163 
1164 
1165 ////////////////////////////////////////////////////////////////////////
1166 ///                       END OF FILE                                ///
1167 ////////////////////////////////////////////////////////////////////////
1168 
1169