1 /**CFile****************************************************************
2
3 FileName [vecWrd.h]
4
5 SystemName [ABC: Logic synthesis and verification system.]
6
7 PackageName [Resizable arrays.]
8
9 Synopsis [Resizable arrays of long unsigned integers.]
10
11 Author [Alan Mishchenko]
12
13 Affiliation [UC Berkeley]
14
15 Date [Ver. 1.0. Started - June 20, 2005.]
16
17 Revision [$Id: vecWrd.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18
19 ***********************************************************************/
20
21 #ifndef ABC__misc__vec__vecWrd_h
22 #define ABC__misc__vec__vecWrd_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_Wrd_t_ Vec_Wrd_t;
43 struct Vec_Wrd_t_
44 {
45 int nCap;
46 int nSize;
47 word * pArray;
48 };
49
50 ////////////////////////////////////////////////////////////////////////
51 /// MACRO DEFINITIONS ///
52 ////////////////////////////////////////////////////////////////////////
53
54 #define Vec_WrdForEachEntry( vVec, Entry, i ) \
55 for ( i = 0; (i < Vec_WrdSize(vVec)) && (((Entry) = Vec_WrdEntry(vVec, i)), 1); i++ )
56 #define Vec_WrdForEachEntryStart( vVec, Entry, i, Start ) \
57 for ( i = Start; (i < Vec_WrdSize(vVec)) && (((Entry) = Vec_WrdEntry(vVec, i)), 1); i++ )
58 #define Vec_WrdForEachEntryStop( vVec, Entry, i, Stop ) \
59 for ( i = 0; (i < Stop) && (((Entry) = Vec_WrdEntry(vVec, i)), 1); i++ )
60 #define Vec_WrdForEachEntryStartStop( vVec, Entry, i, Start, Stop ) \
61 for ( i = Start; (i < Stop) && (((Entry) = Vec_WrdEntry(vVec, i)), 1); i++ )
62 #define Vec_WrdForEachEntryReverse( vVec, pEntry, i ) \
63 for ( i = Vec_WrdSize(vVec) - 1; (i >= 0) && (((pEntry) = Vec_WrdEntry(vVec, i)), 1); i-- )
64
65 ////////////////////////////////////////////////////////////////////////
66 /// FUNCTION DEFINITIONS ///
67 ////////////////////////////////////////////////////////////////////////
68
69 /**Function*************************************************************
70
71 Synopsis [Allocates a vector with the given capacity.]
72
73 Description []
74
75 SideEffects []
76
77 SeeAlso []
78
79 ***********************************************************************/
Vec_WrdAlloc(int nCap)80 static inline Vec_Wrd_t * Vec_WrdAlloc( int nCap )
81 {
82 Vec_Wrd_t * p;
83 p = ABC_ALLOC( Vec_Wrd_t, 1 );
84 if ( nCap > 0 && nCap < 16 )
85 nCap = 16;
86 p->nSize = 0;
87 p->nCap = nCap;
88 p->pArray = p->nCap? ABC_ALLOC( word, p->nCap ) : NULL;
89 return p;
90 }
Vec_WrdAllocExact(int nCap)91 static inline Vec_Wrd_t * Vec_WrdAllocExact( int nCap )
92 {
93 Vec_Wrd_t * p;
94 assert( nCap >= 0 );
95 p = ABC_ALLOC( Vec_Wrd_t, 1 );
96 p->nSize = 0;
97 p->nCap = nCap;
98 p->pArray = p->nCap? ABC_ALLOC( word, p->nCap ) : NULL;
99 return p;
100 }
101
102 /**Function*************************************************************
103
104 Synopsis [Allocates a vector with the given size and cleans it.]
105
106 Description []
107
108 SideEffects []
109
110 SeeAlso []
111
112 ***********************************************************************/
Vec_WrdStart(int nSize)113 static inline Vec_Wrd_t * Vec_WrdStart( int nSize )
114 {
115 Vec_Wrd_t * p;
116 p = Vec_WrdAlloc( nSize );
117 p->nSize = nSize;
118 memset( p->pArray, 0, sizeof(word) * (size_t)nSize );
119 return p;
120 }
121
122 /**Function*************************************************************
123
124 Synopsis [Allocates a vector with the given size and cleans it.]
125
126 Description []
127
128 SideEffects []
129
130 SeeAlso []
131
132 ***********************************************************************/
Vec_WrdStartFull(int nSize)133 static inline Vec_Wrd_t * Vec_WrdStartFull( int nSize )
134 {
135 Vec_Wrd_t * p;
136 p = Vec_WrdAlloc( nSize );
137 p->nSize = nSize;
138 memset( p->pArray, 0xff, sizeof(word) * (size_t)nSize );
139 return p;
140 }
141
142 /**Function*************************************************************
143
144 Synopsis [Allocates a vector with the given size and cleans it.]
145
146 Description []
147
148 SideEffects []
149
150 SeeAlso []
151
152 ***********************************************************************/
Vec_WrdStartNatural(int nSize)153 static inline Vec_Wrd_t * Vec_WrdStartNatural( int nSize )
154 {
155 Vec_Wrd_t * p;
156 int i;
157 p = Vec_WrdAlloc( nSize );
158 p->nSize = nSize;
159 for ( i = 0; i < nSize; i++ )
160 p->pArray[i] = i;
161 return p;
162 }
Vec_WrdStartRandom(int nSize)163 static inline Vec_Wrd_t * Vec_WrdStartRandom( int nSize )
164 {
165 Vec_Wrd_t * vSims = Vec_WrdStart( nSize ); int i;
166 for ( i = 0; i < nSize; i++ )
167 vSims->pArray[i] = Abc_RandomW(0);
168 return vSims;
169 }
170
171 /**Function*************************************************************
172
173 Synopsis [Creates the vector from an integer array of the given size.]
174
175 Description []
176
177 SideEffects []
178
179 SeeAlso []
180
181 ***********************************************************************/
Vec_WrdAllocArray(word * pArray,int nSize)182 static inline Vec_Wrd_t * Vec_WrdAllocArray( word * pArray, int nSize )
183 {
184 Vec_Wrd_t * p;
185 p = ABC_ALLOC( Vec_Wrd_t, 1 );
186 p->nSize = nSize;
187 p->nCap = nSize;
188 p->pArray = pArray;
189 return p;
190 }
191
192 /**Function*************************************************************
193
194 Synopsis [Creates the vector from an integer array of the given size.]
195
196 Description []
197
198 SideEffects []
199
200 SeeAlso []
201
202 ***********************************************************************/
Vec_WrdAllocArrayCopy(word * pArray,int nSize)203 static inline Vec_Wrd_t * Vec_WrdAllocArrayCopy( word * pArray, int nSize )
204 {
205 Vec_Wrd_t * p;
206 p = ABC_ALLOC( Vec_Wrd_t, 1 );
207 p->nSize = nSize;
208 p->nCap = nSize;
209 p->pArray = ABC_ALLOC( word, nSize );
210 memcpy( p->pArray, pArray, sizeof(word) * (size_t)nSize );
211 return p;
212 }
213
214 /**Function*************************************************************
215
216 Synopsis [Duplicates the integer array.]
217
218 Description []
219
220 SideEffects []
221
222 SeeAlso []
223
224 ***********************************************************************/
Vec_WrdDup(Vec_Wrd_t * pVec)225 static inline Vec_Wrd_t * Vec_WrdDup( Vec_Wrd_t * pVec )
226 {
227 Vec_Wrd_t * p;
228 p = ABC_ALLOC( Vec_Wrd_t, 1 );
229 p->nSize = pVec->nSize;
230 p->nCap = pVec->nSize;
231 p->pArray = p->nCap? ABC_ALLOC( word, p->nCap ) : NULL;
232 memcpy( p->pArray, pVec->pArray, sizeof(word) * (size_t)pVec->nSize );
233 return p;
234 }
235
236 /**Function*************************************************************
237
238 Synopsis [Transfers the array into another vector.]
239
240 Description []
241
242 SideEffects []
243
244 SeeAlso []
245
246 ***********************************************************************/
Vec_WrdDupArray(Vec_Wrd_t * pVec)247 static inline Vec_Wrd_t * Vec_WrdDupArray( Vec_Wrd_t * pVec )
248 {
249 Vec_Wrd_t * p;
250 p = ABC_ALLOC( Vec_Wrd_t, 1 );
251 p->nSize = pVec->nSize;
252 p->nCap = pVec->nCap;
253 p->pArray = pVec->pArray;
254 pVec->nSize = 0;
255 pVec->nCap = 0;
256 pVec->pArray = NULL;
257 return p;
258 }
259
260 /**Function*************************************************************
261
262 Synopsis []
263
264 Description []
265
266 SideEffects []
267
268 SeeAlso []
269
270 ***********************************************************************/
Vec_WrdErase(Vec_Wrd_t * p)271 static inline void Vec_WrdErase( Vec_Wrd_t * p )
272 {
273 ABC_FREE( p->pArray );
274 p->nSize = 0;
275 p->nCap = 0;
276 }
Vec_WrdFree(Vec_Wrd_t * p)277 static inline void Vec_WrdFree( Vec_Wrd_t * p )
278 {
279 ABC_FREE( p->pArray );
280 ABC_FREE( p );
281 }
282
283 /**Function*************************************************************
284
285 Synopsis []
286
287 Description []
288
289 SideEffects []
290
291 SeeAlso []
292
293 ***********************************************************************/
Vec_WrdFreeP(Vec_Wrd_t ** p)294 static inline void Vec_WrdFreeP( Vec_Wrd_t ** p )
295 {
296 if ( *p == NULL )
297 return;
298 ABC_FREE( (*p)->pArray );
299 ABC_FREE( (*p) );
300 }
301
302 /**Function*************************************************************
303
304 Synopsis []
305
306 Description []
307
308 SideEffects []
309
310 SeeAlso []
311
312 ***********************************************************************/
Vec_WrdReleaseArray(Vec_Wrd_t * p)313 static inline word * Vec_WrdReleaseArray( Vec_Wrd_t * p )
314 {
315 word * pArray = p->pArray;
316 p->nCap = 0;
317 p->nSize = 0;
318 p->pArray = NULL;
319 return pArray;
320 }
321
322 /**Function*************************************************************
323
324 Synopsis []
325
326 Description []
327
328 SideEffects []
329
330 SeeAlso []
331
332 ***********************************************************************/
Vec_WrdArray(Vec_Wrd_t * p)333 static inline word * Vec_WrdArray( Vec_Wrd_t * p )
334 {
335 return p->pArray;
336 }
Vec_WrdLimit(Vec_Wrd_t * p)337 static inline word * Vec_WrdLimit( Vec_Wrd_t * p )
338 {
339 return p->pArray + p->nSize;
340 }
341
342 /**Function*************************************************************
343
344 Synopsis []
345
346 Description []
347
348 SideEffects []
349
350 SeeAlso []
351
352 ***********************************************************************/
Vec_WrdSize(Vec_Wrd_t * p)353 static inline int Vec_WrdSize( Vec_Wrd_t * p )
354 {
355 return p->nSize;
356 }
357
358 /**Function*************************************************************
359
360 Synopsis []
361
362 Description []
363
364 SideEffects []
365
366 SeeAlso []
367
368 ***********************************************************************/
Vec_WrdCap(Vec_Wrd_t * p)369 static inline int Vec_WrdCap( Vec_Wrd_t * p )
370 {
371 return p->nCap;
372 }
373
374 /**Function*************************************************************
375
376 Synopsis []
377
378 Description []
379
380 SideEffects []
381
382 SeeAlso []
383
384 ***********************************************************************/
Vec_WrdMemory(Vec_Wrd_t * p)385 static inline double Vec_WrdMemory( Vec_Wrd_t * p )
386 {
387 return !p ? 0.0 : 1.0 * sizeof(word) * (size_t)p->nCap + sizeof(Vec_Wrd_t);
388 }
389
390 /**Function*************************************************************
391
392 Synopsis []
393
394 Description []
395
396 SideEffects []
397
398 SeeAlso []
399
400 ***********************************************************************/
Vec_WrdEntry(Vec_Wrd_t * p,int i)401 static inline word Vec_WrdEntry( Vec_Wrd_t * p, int i )
402 {
403 assert( i >= 0 && i < p->nSize );
404 return p->pArray[i];
405 }
406
407 /**Function*************************************************************
408
409 Synopsis []
410
411 Description []
412
413 SideEffects []
414
415 SeeAlso []
416
417 ***********************************************************************/
Vec_WrdEntryP(Vec_Wrd_t * p,int i)418 static inline word * Vec_WrdEntryP( Vec_Wrd_t * p, int i )
419 {
420 assert( i >= 0 && i < p->nSize );
421 return p->pArray + i;
422 }
423
424 /**Function*************************************************************
425
426 Synopsis []
427
428 Description []
429
430 SideEffects []
431
432 SeeAlso []
433
434 ***********************************************************************/
Vec_WrdWriteEntry(Vec_Wrd_t * p,int i,word Entry)435 static inline void Vec_WrdWriteEntry( Vec_Wrd_t * p, int i, word Entry )
436 {
437 assert( i >= 0 && i < p->nSize );
438 p->pArray[i] = Entry;
439 }
440
441 /**Function*************************************************************
442
443 Synopsis []
444
445 Description []
446
447 SideEffects []
448
449 SeeAlso []
450
451 ***********************************************************************/
Vec_WrdAddToEntry(Vec_Wrd_t * p,int i,word Addition)452 static inline word Vec_WrdAddToEntry( Vec_Wrd_t * p, int i, word Addition )
453 {
454 assert( i >= 0 && i < p->nSize );
455 return p->pArray[i] += Addition;
456 }
457
458 /**Function*************************************************************
459
460 Synopsis []
461
462 Description []
463
464 SideEffects []
465
466 SeeAlso []
467
468 ***********************************************************************/
Vec_WrdEntryLast(Vec_Wrd_t * p)469 static inline word Vec_WrdEntryLast( Vec_Wrd_t * p )
470 {
471 assert( p->nSize > 0 );
472 return p->pArray[p->nSize-1];
473 }
474
475 /**Function*************************************************************
476
477 Synopsis [Resizes the vector to the given capacity.]
478
479 Description []
480
481 SideEffects []
482
483 SeeAlso []
484
485 ***********************************************************************/
Vec_WrdGrow(Vec_Wrd_t * p,int nCapMin)486 static inline void Vec_WrdGrow( Vec_Wrd_t * p, int nCapMin )
487 {
488 if ( p->nCap >= nCapMin )
489 return;
490 p->pArray = ABC_REALLOC( word, p->pArray, nCapMin );
491 assert( p->pArray );
492 p->nCap = nCapMin;
493 }
494
495 /**Function*************************************************************
496
497 Synopsis [Fills the vector with given number of entries.]
498
499 Description []
500
501 SideEffects []
502
503 SeeAlso []
504
505 ***********************************************************************/
Vec_WrdFill(Vec_Wrd_t * p,int nSize,word Fill)506 static inline void Vec_WrdFill( Vec_Wrd_t * p, int nSize, word Fill )
507 {
508 int i;
509 Vec_WrdGrow( p, nSize );
510 for ( i = 0; i < nSize; i++ )
511 p->pArray[i] = Fill;
512 p->nSize = nSize;
513 }
514
515 /**Function*************************************************************
516
517 Synopsis [Fills the vector with given number of entries.]
518
519 Description []
520
521 SideEffects []
522
523 SeeAlso []
524
525 ***********************************************************************/
Vec_WrdFillExtra(Vec_Wrd_t * p,int nSize,word Fill)526 static inline void Vec_WrdFillExtra( Vec_Wrd_t * p, int nSize, word Fill )
527 {
528 int i;
529 if ( nSize <= p->nSize )
530 return;
531 if ( nSize > 2 * p->nCap )
532 Vec_WrdGrow( p, nSize );
533 else if ( nSize > p->nCap )
534 Vec_WrdGrow( p, 2 * p->nCap );
535 for ( i = p->nSize; i < nSize; i++ )
536 p->pArray[i] = Fill;
537 p->nSize = nSize;
538 }
539
540 /**Function*************************************************************
541
542 Synopsis [Returns the entry even if the place not exist.]
543
544 Description []
545
546 SideEffects []
547
548 SeeAlso []
549
550 ***********************************************************************/
Vec_WrdGetEntry(Vec_Wrd_t * p,int i)551 static inline word Vec_WrdGetEntry( Vec_Wrd_t * p, int i )
552 {
553 Vec_WrdFillExtra( p, i + 1, 0 );
554 return Vec_WrdEntry( p, i );
555 }
556
557 /**Function*************************************************************
558
559 Synopsis [Returns the entry even if the place not exist.]
560
561 Description []
562
563 SideEffects []
564
565 SeeAlso []
566
567 ***********************************************************************/
Vec_WrdGetEntryP(Vec_Wrd_t * p,int i)568 static inline word * Vec_WrdGetEntryP( Vec_Wrd_t * p, int i )
569 {
570 Vec_WrdFillExtra( p, i + 1, 0 );
571 return Vec_WrdEntryP( p, i );
572 }
573
574 /**Function*************************************************************
575
576 Synopsis [Inserts the entry even if the place does not exist.]
577
578 Description []
579
580 SideEffects []
581
582 SeeAlso []
583
584 ***********************************************************************/
Vec_WrdSetEntry(Vec_Wrd_t * p,int i,word Entry)585 static inline void Vec_WrdSetEntry( Vec_Wrd_t * p, int i, word Entry )
586 {
587 Vec_WrdFillExtra( p, i + 1, 0 );
588 Vec_WrdWriteEntry( p, i, Entry );
589 }
590
591 /**Function*************************************************************
592
593 Synopsis []
594
595 Description []
596
597 SideEffects []
598
599 SeeAlso []
600
601 ***********************************************************************/
Vec_WrdShrink(Vec_Wrd_t * p,int nSizeNew)602 static inline void Vec_WrdShrink( Vec_Wrd_t * p, int nSizeNew )
603 {
604 assert( p->nSize >= nSizeNew );
605 p->nSize = nSizeNew;
606 }
607
608 /**Function*************************************************************
609
610 Synopsis []
611
612 Description []
613
614 SideEffects []
615
616 SeeAlso []
617
618 ***********************************************************************/
Vec_WrdClear(Vec_Wrd_t * p)619 static inline void Vec_WrdClear( Vec_Wrd_t * p )
620 {
621 p->nSize = 0;
622 }
623
624 /**Function*************************************************************
625
626 Synopsis []
627
628 Description []
629
630 SideEffects []
631
632 SeeAlso []
633
634 ***********************************************************************/
Vec_WrdPush(Vec_Wrd_t * p,word Entry)635 static inline void Vec_WrdPush( Vec_Wrd_t * p, word Entry )
636 {
637 if ( p->nSize == p->nCap )
638 {
639 if ( p->nCap < 16 )
640 Vec_WrdGrow( p, 16 );
641 else
642 Vec_WrdGrow( p, 2 * p->nCap );
643 }
644 p->pArray[p->nSize++] = Entry;
645 }
646
647 /**Function*************************************************************
648
649 Synopsis []
650
651 Description []
652
653 SideEffects []
654
655 SeeAlso []
656
657 ***********************************************************************/
Vec_WrdPushFirst(Vec_Wrd_t * p,word Entry)658 static inline void Vec_WrdPushFirst( Vec_Wrd_t * p, word Entry )
659 {
660 int i;
661 if ( p->nSize == p->nCap )
662 {
663 if ( p->nCap < 16 )
664 Vec_WrdGrow( p, 16 );
665 else
666 Vec_WrdGrow( p, 2 * p->nCap );
667 }
668 p->nSize++;
669 for ( i = p->nSize - 1; i >= 1; i-- )
670 p->pArray[i] = p->pArray[i-1];
671 p->pArray[0] = Entry;
672 }
673
674 /**Function*************************************************************
675
676 Synopsis [Inserts the entry while preserving the increasing order.]
677
678 Description []
679
680 SideEffects []
681
682 SeeAlso []
683
684 ***********************************************************************/
Vec_WrdPushOrder(Vec_Wrd_t * p,word Entry)685 static inline void Vec_WrdPushOrder( Vec_Wrd_t * p, word Entry )
686 {
687 int i;
688 if ( p->nSize == p->nCap )
689 {
690 if ( p->nCap < 16 )
691 Vec_WrdGrow( p, 16 );
692 else
693 Vec_WrdGrow( p, 2 * p->nCap );
694 }
695 p->nSize++;
696 for ( i = p->nSize-2; i >= 0; i-- )
697 if ( p->pArray[i] > Entry )
698 p->pArray[i+1] = p->pArray[i];
699 else
700 break;
701 p->pArray[i+1] = Entry;
702 }
703
704 /**Function*************************************************************
705
706 Synopsis [Inserts the entry while preserving the increasing order.]
707
708 Description []
709
710 SideEffects []
711
712 SeeAlso []
713
714 ***********************************************************************/
Vec_WrdPushUniqueOrder(Vec_Wrd_t * p,word Entry)715 static inline int Vec_WrdPushUniqueOrder( Vec_Wrd_t * p, word Entry )
716 {
717 int i;
718 for ( i = 0; i < p->nSize; i++ )
719 if ( p->pArray[i] == Entry )
720 return 1;
721 Vec_WrdPushOrder( p, Entry );
722 return 0;
723 }
724
725 /**Function*************************************************************
726
727 Synopsis []
728
729 Description []
730
731 SideEffects []
732
733 SeeAlso []
734
735 ***********************************************************************/
Vec_WrdPushUnique(Vec_Wrd_t * p,word Entry)736 static inline int Vec_WrdPushUnique( Vec_Wrd_t * p, word Entry )
737 {
738 int i;
739 for ( i = 0; i < p->nSize; i++ )
740 if ( p->pArray[i] == Entry )
741 return 1;
742 Vec_WrdPush( p, Entry );
743 return 0;
744 }
745
746 /**Function*************************************************************
747
748 Synopsis [Returns the pointer to the next nWords entries in the vector.]
749
750 Description []
751
752 SideEffects []
753
754 SeeAlso []
755
756 ***********************************************************************/
Vec_WrdFetch(Vec_Wrd_t * p,int nWords)757 static inline word * Vec_WrdFetch( Vec_Wrd_t * p, int nWords )
758 {
759 if ( nWords == 0 )
760 return NULL;
761 assert( nWords > 0 );
762 p->nSize += nWords;
763 if ( p->nSize > p->nCap )
764 {
765 // Vec_WrdGrow( p, 2 * p->nSize );
766 return NULL;
767 }
768 return p->pArray + p->nSize - nWords;
769 }
770
771 /**Function*************************************************************
772
773 Synopsis [Returns the last entry and removes it from the list.]
774
775 Description []
776
777 SideEffects []
778
779 SeeAlso []
780
781 ***********************************************************************/
Vec_WrdPop(Vec_Wrd_t * p)782 static inline word Vec_WrdPop( Vec_Wrd_t * p )
783 {
784 assert( p->nSize > 0 );
785 return p->pArray[--p->nSize];
786 }
787
788 /**Function*************************************************************
789
790 Synopsis [Find entry.]
791
792 Description []
793
794 SideEffects []
795
796 SeeAlso []
797
798 ***********************************************************************/
Vec_WrdFind(Vec_Wrd_t * p,word Entry)799 static inline int Vec_WrdFind( Vec_Wrd_t * p, word Entry )
800 {
801 int i;
802 for ( i = 0; i < p->nSize; i++ )
803 if ( p->pArray[i] == Entry )
804 return i;
805 return -1;
806 }
807
808 /**Function*************************************************************
809
810 Synopsis []
811
812 Description []
813
814 SideEffects []
815
816 SeeAlso []
817
818 ***********************************************************************/
Vec_WrdRemove(Vec_Wrd_t * p,word Entry)819 static inline int Vec_WrdRemove( Vec_Wrd_t * p, word Entry )
820 {
821 int i;
822 for ( i = 0; i < p->nSize; i++ )
823 if ( p->pArray[i] == Entry )
824 break;
825 if ( i == p->nSize )
826 return 0;
827 assert( i < p->nSize );
828 for ( i++; i < p->nSize; i++ )
829 p->pArray[i-1] = p->pArray[i];
830 p->nSize--;
831 return 1;
832 }
833
834 /**Function*************************************************************
835
836 Synopsis [Interts entry at the index iHere. Shifts other entries.]
837
838 Description []
839
840 SideEffects []
841
842 SeeAlso []
843
844 ***********************************************************************/
Vec_WrdInsert(Vec_Wrd_t * p,int iHere,word Entry)845 static inline void Vec_WrdInsert( Vec_Wrd_t * p, int iHere, word Entry )
846 {
847 int i;
848 assert( iHere >= 0 && iHere < p->nSize );
849 Vec_WrdPush( p, 0 );
850 for ( i = p->nSize - 1; i > iHere; i-- )
851 p->pArray[i] = p->pArray[i-1];
852 p->pArray[i] = Entry;
853 }
854
855 /**Function*************************************************************
856
857 Synopsis [Find entry.]
858
859 Description []
860
861 SideEffects []
862
863 SeeAlso []
864
865 ***********************************************************************/
Vec_WrdFindMax(Vec_Wrd_t * p)866 static inline word Vec_WrdFindMax( Vec_Wrd_t * p )
867 {
868 word Best;
869 int i;
870 if ( p->nSize == 0 )
871 return 0;
872 Best = p->pArray[0];
873 for ( i = 1; i < p->nSize; i++ )
874 if ( Best < p->pArray[i] )
875 Best = p->pArray[i];
876 return Best;
877 }
878
879 /**Function*************************************************************
880
881 Synopsis [Find entry.]
882
883 Description []
884
885 SideEffects []
886
887 SeeAlso []
888
889 ***********************************************************************/
Vec_WrdFindMin(Vec_Wrd_t * p)890 static inline word Vec_WrdFindMin( Vec_Wrd_t * p )
891 {
892 word Best;
893 int i;
894 if ( p->nSize == 0 )
895 return 0;
896 Best = p->pArray[0];
897 for ( i = 1; i < p->nSize; i++ )
898 if ( Best > p->pArray[i] )
899 Best = p->pArray[i];
900 return Best;
901 }
902
903 /**Function*************************************************************
904
905 Synopsis [Reverses the order of entries.]
906
907 Description []
908
909 SideEffects []
910
911 SeeAlso []
912
913 ***********************************************************************/
Vec_WrdReverseOrder(Vec_Wrd_t * p)914 static inline void Vec_WrdReverseOrder( Vec_Wrd_t * p )
915 {
916 word Temp;
917 int i;
918 for ( i = 0; i < p->nSize/2; i++ )
919 {
920 Temp = p->pArray[i];
921 p->pArray[i] = p->pArray[p->nSize-1-i];
922 p->pArray[p->nSize-1-i] = Temp;
923 }
924 }
925
926 /**Function*************************************************************
927
928 Synopsis []
929
930 Description []
931
932 SideEffects []
933
934 SeeAlso []
935
936 ***********************************************************************/
Vec_WrdInvert(Vec_Wrd_t * p,word Fill)937 static inline Vec_Wrd_t * Vec_WrdInvert( Vec_Wrd_t * p, word Fill )
938 {
939 int i;
940 word Entry;
941 Vec_Wrd_t * vRes = Vec_WrdAlloc( 0 );
942 Vec_WrdFill( vRes, Vec_WrdFindMax(p) + 1, Fill );
943 Vec_WrdForEachEntry( p, Entry, i )
944 if ( Entry != Fill )
945 Vec_WrdWriteEntry( vRes, Entry, i );
946 return vRes;
947 }
948
949 /**Function*************************************************************
950
951 Synopsis []
952
953 Description []
954
955 SideEffects []
956
957 SeeAlso []
958
959 ***********************************************************************/
Vec_WrdSum(Vec_Wrd_t * p)960 static inline word Vec_WrdSum( Vec_Wrd_t * p )
961 {
962 word Counter = 0;
963 int i;
964 for ( i = 0; i < p->nSize; i++ )
965 Counter += p->pArray[i];
966 return Counter;
967 }
968
969 /**Function*************************************************************
970
971 Synopsis []
972
973 Description []
974
975 SideEffects []
976
977 SeeAlso []
978
979 ***********************************************************************/
Vec_WrdCountZero(Vec_Wrd_t * p)980 static inline int Vec_WrdCountZero( Vec_Wrd_t * p )
981 {
982 int i, Counter = 0;
983 for ( i = 0; i < p->nSize; i++ )
984 Counter += (p->pArray[i] == 0);
985 return Counter;
986 }
987
988 /**Function*************************************************************
989
990 Synopsis [Checks if two vectors are equal.]
991
992 Description []
993
994 SideEffects []
995
996 SeeAlso []
997
998 ***********************************************************************/
Vec_WrdEqual(Vec_Wrd_t * p1,Vec_Wrd_t * p2)999 static inline int Vec_WrdEqual( Vec_Wrd_t * p1, Vec_Wrd_t * p2 )
1000 {
1001 int i;
1002 if ( p1->nSize != p2->nSize )
1003 return 0;
1004 for ( i = 0; i < p1->nSize; i++ )
1005 if ( p1->pArray[i] != p2->pArray[i] )
1006 return 0;
1007 return 1;
1008 }
1009
1010 /**Function*************************************************************
1011
1012 Synopsis [Counts the number of common entries.]
1013
1014 Description []
1015
1016 SideEffects []
1017
1018 SeeAlso []
1019
1020 ***********************************************************************/
Vec_WrdCountCommon(Vec_Wrd_t * p1,Vec_Wrd_t * p2)1021 static inline int Vec_WrdCountCommon( Vec_Wrd_t * p1, Vec_Wrd_t * p2 )
1022 {
1023 Vec_Wrd_t * vTemp;
1024 word Entry;
1025 int i, Counter = 0;
1026 if ( Vec_WrdSize(p1) < Vec_WrdSize(p2) )
1027 vTemp = p1, p1 = p2, p2 = vTemp;
1028 assert( Vec_WrdSize(p1) >= Vec_WrdSize(p2) );
1029 vTemp = Vec_WrdInvert( p2, -1 );
1030 Vec_WrdFillExtra( vTemp, Vec_WrdFindMax(p1) + 1, ~((word)0) );
1031 Vec_WrdForEachEntry( p1, Entry, i )
1032 if ( Vec_WrdEntry(vTemp, Entry) != ~((word)0) )
1033 Counter++;
1034 Vec_WrdFree( vTemp );
1035 return Counter;
1036 }
1037
1038 /**Function*************************************************************
1039
1040 Synopsis [Comparison procedure for two integers.]
1041
1042 Description []
1043
1044 SideEffects []
1045
1046 SeeAlso []
1047
1048 ***********************************************************************/
Vec_WrdSortCompare1(word * pp1,word * pp2)1049 static int Vec_WrdSortCompare1( word * pp1, word * pp2 )
1050 {
1051 // for some reason commenting out lines (as shown) led to crashing of the release version
1052 if ( *pp1 < *pp2 )
1053 return -1;
1054 if ( *pp1 > *pp2 ) //
1055 return 1;
1056 return 0; //
1057 }
1058
1059 /**Function*************************************************************
1060
1061 Synopsis [Comparison procedure for two integers.]
1062
1063 Description []
1064
1065 SideEffects []
1066
1067 SeeAlso []
1068
1069 ***********************************************************************/
Vec_WrdSortCompare2(word * pp1,word * pp2)1070 static int Vec_WrdSortCompare2( word * pp1, word * pp2 )
1071 {
1072 // for some reason commenting out lines (as shown) led to crashing of the release version
1073 if ( *pp1 > *pp2 )
1074 return -1;
1075 if ( *pp1 < *pp2 ) //
1076 return 1;
1077 return 0; //
1078 }
1079
1080 /**Function*************************************************************
1081
1082 Synopsis [Sorting the entries by their integer value.]
1083
1084 Description []
1085
1086 SideEffects []
1087
1088 SeeAlso []
1089
1090 ***********************************************************************/
Vec_WrdSort(Vec_Wrd_t * p,int fReverse)1091 static inline void Vec_WrdSort( Vec_Wrd_t * p, int fReverse )
1092 {
1093 if ( fReverse )
1094 qsort( (void *)p->pArray, (size_t)p->nSize, sizeof(word),
1095 (int (*)(const void *, const void *)) Vec_WrdSortCompare2 );
1096 else
1097 qsort( (void *)p->pArray, (size_t)p->nSize, sizeof(word),
1098 (int (*)(const void *, const void *)) Vec_WrdSortCompare1 );
1099 }
1100
1101 /**Function*************************************************************
1102
1103 Synopsis [Leaves only unique entries.]
1104
1105 Description []
1106
1107 SideEffects []
1108
1109 SeeAlso []
1110
1111 ***********************************************************************/
Vec_WrdUniqify(Vec_Wrd_t * p)1112 static inline void Vec_WrdUniqify( Vec_Wrd_t * p )
1113 {
1114 int i, k;
1115 if ( p->nSize < 2 )
1116 return;
1117 Vec_WrdSort( p, 0 );
1118 for ( i = k = 1; i < p->nSize; i++ )
1119 if ( p->pArray[i] != p->pArray[i-1] )
1120 p->pArray[k++] = p->pArray[i];
1121 p->nSize = k;
1122 }
Vec_WrdUniqueCount(Vec_Wrd_t * vData,int nWordSize,Vec_Int_t ** pvMap)1123 static inline int Vec_WrdUniqueCount( Vec_Wrd_t * vData, int nWordSize, Vec_Int_t ** pvMap )
1124 {
1125 int Result;
1126 Vec_Int_t * vDataInt = (Vec_Int_t *)vData;
1127 vDataInt->nSize *= 2;
1128 vDataInt->nCap *= 2;
1129 Result = Vec_IntUniqueCount( vDataInt, 2 * nWordSize, pvMap );
1130 vDataInt->nSize /= 2;
1131 vDataInt->nCap /= 2;
1132 return Result;
1133 }
Vec_WrdUniqifyHash(Vec_Wrd_t * vData,int nWordSize)1134 static inline Vec_Wrd_t * Vec_WrdUniqifyHash( Vec_Wrd_t * vData, int nWordSize )
1135 {
1136 Vec_Int_t * vResInt;
1137 Vec_Int_t * vDataInt = (Vec_Int_t *)vData;
1138 vDataInt->nSize *= 2;
1139 vDataInt->nCap *= 2;
1140 vResInt = Vec_IntUniqifyHash( vDataInt, 2 * nWordSize );
1141 vDataInt->nSize /= 2;
1142 vDataInt->nCap /= 2;
1143 vResInt->nSize /= 2;
1144 vResInt->nCap /= 2;
1145 return (Vec_Wrd_t *)vResInt;
1146 }
1147
1148 /**Function*************************************************************
1149
1150 Synopsis [Comparison procedure for two integers.]
1151
1152 Description []
1153
1154 SideEffects []
1155
1156 SeeAlso []
1157
1158 ***********************************************************************/
Vec_WrdSortCompareUnsigned(word * pp1,word * pp2)1159 static int Vec_WrdSortCompareUnsigned( word * pp1, word * pp2 )
1160 {
1161 if ( *pp1 < *pp2 )
1162 return -1;
1163 if ( *pp1 > *pp2 )
1164 return 1;
1165 return 0;
1166 }
1167
1168 /**Function*************************************************************
1169
1170 Synopsis [Sorting the entries by their integer value.]
1171
1172 Description []
1173
1174 SideEffects []
1175
1176 SeeAlso []
1177
1178 ***********************************************************************/
Vec_WrdSortUnsigned(Vec_Wrd_t * p)1179 static inline void Vec_WrdSortUnsigned( Vec_Wrd_t * p )
1180 {
1181 qsort( (void *)p->pArray, (size_t)p->nSize, sizeof(word),
1182 (int (*)(const void *, const void *)) Vec_WrdSortCompareUnsigned );
1183 }
1184
1185
1186 /**Function*************************************************************
1187
1188 Synopsis [Appends the contents of the second vector.]
1189
1190 Description []
1191
1192 SideEffects []
1193
1194 SeeAlso []
1195
1196 ***********************************************************************/
Vec_WrdAppend(Vec_Wrd_t * vVec1,Vec_Wrd_t * vVec2)1197 static inline void Vec_WrdAppend( Vec_Wrd_t * vVec1, Vec_Wrd_t * vVec2 )
1198 {
1199 word Entry; int i;
1200 Vec_WrdForEachEntry( vVec2, Entry, i )
1201 Vec_WrdPush( vVec1, Entry );
1202 }
1203
1204
1205 ABC_NAMESPACE_HEADER_END
1206
1207 #endif
1208
1209 ////////////////////////////////////////////////////////////////////////
1210 /// END OF FILE ///
1211 ////////////////////////////////////////////////////////////////////////
1212
1213