ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
utilSort.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <assert.h>
25
26#include "abc_global.h"
27
29
33
37
49void Abc_SortMerge( int * p1Beg, int * p1End, int * p2Beg, int * p2End, int * pOut )
50{
51 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
52 int * pOutBeg = pOut;
53 while ( p1Beg < p1End && p2Beg < p2End )
54 {
55 if ( *p1Beg == *p2Beg )
56 *pOut++ = *p1Beg++, *pOut++ = *p2Beg++;
57 else if ( *p1Beg < *p2Beg )
58 *pOut++ = *p1Beg++;
59 else // if ( *p1Beg > *p2Beg )
60 *pOut++ = *p2Beg++;
61 }
62 while ( p1Beg < p1End )
63 *pOut++ = *p1Beg++;
64 while ( p2Beg < p2End )
65 *pOut++ = *p2Beg++;
66 assert( pOut - pOutBeg == nEntries );
67}
68
80void Abc_Sort_rec( int * pInBeg, int * pInEnd, int * pOutBeg )
81{
82 int nSize = pInEnd - pInBeg;
83 assert( nSize > 0 );
84 if ( nSize == 1 )
85 return;
86 if ( nSize == 2 )
87 {
88 if ( pInBeg[0] > pInBeg[1] )
89 {
90 pInBeg[0] ^= pInBeg[1];
91 pInBeg[1] ^= pInBeg[0];
92 pInBeg[0] ^= pInBeg[1];
93 }
94 }
95 else if ( nSize < 8 )
96 {
97 int temp, i, j, best_i;
98 for ( i = 0; i < nSize-1; i++ )
99 {
100 best_i = i;
101 for ( j = i+1; j < nSize; j++ )
102 if ( pInBeg[j] < pInBeg[best_i] )
103 best_i = j;
104 temp = pInBeg[i];
105 pInBeg[i] = pInBeg[best_i];
106 pInBeg[best_i] = temp;
107 }
108 }
109 else
110 {
111 Abc_Sort_rec( pInBeg, pInBeg + nSize/2, pOutBeg );
112 Abc_Sort_rec( pInBeg + nSize/2, pInEnd, pOutBeg + nSize/2 );
113 Abc_SortMerge( pInBeg, pInBeg + nSize/2, pInBeg + nSize/2, pInEnd, pOutBeg );
114 memcpy( pInBeg, pOutBeg, sizeof(int) * nSize );
115 }
116}
117
129void Abc_MergeSort( int * pInput, int nSize )
130{
131 int * pOutput;
132 if ( nSize < 2 )
133 return;
134 pOutput = (int *) malloc( sizeof(int) * nSize );
135 Abc_Sort_rec( pInput, pInput + nSize, pOutput );
136 free( pOutput );
137}
138
139
151void Abc_SortMergeCost2( int * p1Beg, int * p1End, int * p2Beg, int * p2End, int * pOut, int * pCost )
152{
153 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
154 int * pOutBeg = pOut;
155 while ( p1Beg < p1End && p2Beg < p2End )
156 {
157 if ( pCost[*p1Beg] == pCost[*p2Beg] )
158 *pOut++ = *p1Beg++, *pOut++ = *p2Beg++;
159 else if ( pCost[*p1Beg] < pCost[*p2Beg] )
160 *pOut++ = *p1Beg++;
161 else // if ( pCost[*p1Beg] > pCost[*p2Beg] )
162 *pOut++ = *p2Beg++;
163 }
164 while ( p1Beg < p1End )
165 *pOut++ = *p1Beg++;
166 while ( p2Beg < p2End )
167 *pOut++ = *p2Beg++;
168 assert( pOut - pOutBeg == nEntries );
169}
170
182void Abc_SortCost2_rec( int * pInBeg, int * pInEnd, int * pOutBeg, int * pCost )
183{
184 int nSize = pInEnd - pInBeg;
185 assert( nSize > 0 );
186 if ( nSize == 1 )
187 return;
188 if ( nSize == 2 )
189 {
190 if ( pCost[pInBeg[0]] > pCost[pInBeg[1]] )
191 {
192 pInBeg[0] ^= pInBeg[1];
193 pInBeg[1] ^= pInBeg[0];
194 pInBeg[0] ^= pInBeg[1];
195 }
196 }
197 else if ( nSize < 8 )
198 {
199 int temp, i, j, best_i;
200 for ( i = 0; i < nSize-1; i++ )
201 {
202 best_i = i;
203 for ( j = i+1; j < nSize; j++ )
204 if ( pCost[pInBeg[j]] < pCost[pInBeg[best_i]] )
205 best_i = j;
206 temp = pInBeg[i];
207 pInBeg[i] = pInBeg[best_i];
208 pInBeg[best_i] = temp;
209 }
210 }
211 else
212 {
213 Abc_SortCost2_rec( pInBeg, pInBeg + nSize/2, pOutBeg, pCost );
214 Abc_SortCost2_rec( pInBeg + nSize/2, pInEnd, pOutBeg + nSize/2, pCost );
215 Abc_SortMergeCost2( pInBeg, pInBeg + nSize/2, pInBeg + nSize/2, pInEnd, pOutBeg, pCost );
216 memcpy( pInBeg, pOutBeg, sizeof(int) * nSize );
217 }
218}
219
231void Abc_MergeSortCost2( int * pInput, int nSize, int * pCost )
232{
233 int * pOutput;
234 if ( nSize < 2 )
235 return;
236 pOutput = (int *) malloc( sizeof(int) * nSize );
237 Abc_SortCost2_rec( pInput, pInput + nSize, pOutput, pCost );
238 free( pOutput );
239}
240
241
253void Abc_SortMergeCost2Reverse( int * p1Beg, int * p1End, int * p2Beg, int * p2End, int * pOut, int * pCost )
254{
255 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
256 int * pOutBeg = pOut;
257 while ( p1Beg < p1End && p2Beg < p2End )
258 {
259 if ( pCost[*p1Beg] == pCost[*p2Beg] )
260 *pOut++ = *p1Beg++, *pOut++ = *p2Beg++;
261 else if ( pCost[*p1Beg] > pCost[*p2Beg] )
262 *pOut++ = *p1Beg++;
263 else // if ( pCost[*p1Beg] < pCost[*p2Beg] )
264 *pOut++ = *p2Beg++;
265 }
266 while ( p1Beg < p1End )
267 *pOut++ = *p1Beg++;
268 while ( p2Beg < p2End )
269 *pOut++ = *p2Beg++;
270 assert( pOut - pOutBeg == nEntries );
271}
272
284void Abc_SortCost2Reverse_rec( int * pInBeg, int * pInEnd, int * pOutBeg, int * pCost )
285{
286 int nSize = pInEnd - pInBeg;
287 assert( nSize > 0 );
288 if ( nSize == 1 )
289 return;
290 if ( nSize == 2 )
291 {
292 if ( pCost[pInBeg[0]] < pCost[pInBeg[1]] )
293 {
294 pInBeg[0] ^= pInBeg[1];
295 pInBeg[1] ^= pInBeg[0];
296 pInBeg[0] ^= pInBeg[1];
297 }
298 }
299 else if ( nSize < 8 )
300 {
301 int temp, i, j, best_i;
302 for ( i = 0; i < nSize-1; i++ )
303 {
304 best_i = i;
305 for ( j = i+1; j < nSize; j++ )
306 if ( pCost[pInBeg[j]] > pCost[pInBeg[best_i]] )
307 best_i = j;
308 temp = pInBeg[i];
309 pInBeg[i] = pInBeg[best_i];
310 pInBeg[best_i] = temp;
311 }
312 }
313 else
314 {
315 Abc_SortCost2Reverse_rec( pInBeg, pInBeg + nSize/2, pOutBeg, pCost );
316 Abc_SortCost2Reverse_rec( pInBeg + nSize/2, pInEnd, pOutBeg + nSize/2, pCost );
317 Abc_SortMergeCost2Reverse( pInBeg, pInBeg + nSize/2, pInBeg + nSize/2, pInEnd, pOutBeg, pCost );
318 memcpy( pInBeg, pOutBeg, sizeof(int) * nSize );
319 }
320}
321
333void Abc_MergeSortCost2Reverse( int * pInput, int nSize, int * pCost )
334{
335 int * pOutput;
336 if ( nSize < 2 )
337 return;
338 pOutput = (int *) malloc( sizeof(int) * nSize );
339 Abc_SortCost2Reverse_rec( pInput, pInput + nSize, pOutput, pCost );
340 free( pOutput );
341}
342
343
344
356void Abc_MergeSortCostMerge( int * p1Beg, int * p1End, int * p2Beg, int * p2End, int * pOut )
357{
358 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
359 int * pOutBeg = pOut;
360 while ( p1Beg < p1End && p2Beg < p2End )
361 {
362 if ( p1Beg[1] == p2Beg[1] )
363 *pOut++ = *p1Beg++, *pOut++ = *p1Beg++, *pOut++ = *p2Beg++, *pOut++ = *p2Beg++;
364 else if ( p1Beg[1] < p2Beg[1] )
365 *pOut++ = *p1Beg++, *pOut++ = *p1Beg++;
366 else // if ( p1Beg[1] > p2Beg[1] )
367 *pOut++ = *p2Beg++, *pOut++ = *p2Beg++;
368 }
369 while ( p1Beg < p1End )
370 *pOut++ = *p1Beg++, *pOut++ = *p1Beg++;
371 while ( p2Beg < p2End )
372 *pOut++ = *p2Beg++, *pOut++ = *p2Beg++;
373 assert( pOut - pOutBeg == nEntries );
374}
375
387void Abc_MergeSortCost_rec( int * pInBeg, int * pInEnd, int * pOutBeg )
388{
389 int nSize = (pInEnd - pInBeg)/2;
390 assert( nSize > 0 );
391 if ( nSize == 1 )
392 return;
393 if ( nSize == 2 )
394 {
395 if ( pInBeg[1] > pInBeg[3] )
396 {
397 pInBeg[1] ^= pInBeg[3];
398 pInBeg[3] ^= pInBeg[1];
399 pInBeg[1] ^= pInBeg[3];
400 pInBeg[0] ^= pInBeg[2];
401 pInBeg[2] ^= pInBeg[0];
402 pInBeg[0] ^= pInBeg[2];
403 }
404 }
405 else if ( nSize < 8 )
406 {
407 int temp, i, j, best_i;
408 for ( i = 0; i < nSize-1; i++ )
409 {
410 best_i = i;
411 for ( j = i+1; j < nSize; j++ )
412 if ( pInBeg[2*j+1] < pInBeg[2*best_i+1] )
413 best_i = j;
414 temp = pInBeg[2*i];
415 pInBeg[2*i] = pInBeg[2*best_i];
416 pInBeg[2*best_i] = temp;
417 temp = pInBeg[2*i+1];
418 pInBeg[2*i+1] = pInBeg[2*best_i+1];
419 pInBeg[2*best_i+1] = temp;
420 }
421 }
422 else
423 {
424 Abc_MergeSortCost_rec( pInBeg, pInBeg + 2*(nSize/2), pOutBeg );
425 Abc_MergeSortCost_rec( pInBeg + 2*(nSize/2), pInEnd, pOutBeg + 2*(nSize/2) );
426 Abc_MergeSortCostMerge( pInBeg, pInBeg + 2*(nSize/2), pInBeg + 2*(nSize/2), pInEnd, pOutBeg );
427 memcpy( pInBeg, pOutBeg, sizeof(int) * 2 * nSize );
428 }
429}
430
442int * Abc_MergeSortCost( int * pCosts, int nSize )
443{
444 int i, * pResult, * pInput, * pOutput;
445 pResult = (int *) calloc( sizeof(int), nSize );
446 if ( nSize < 2 )
447 return pResult;
448 pInput = (int *) malloc( sizeof(int) * 2 * nSize );
449 pOutput = (int *) malloc( sizeof(int) * 2 * nSize );
450 for ( i = 0; i < nSize; i++ )
451 pInput[2*i] = i, pInput[2*i+1] = pCosts[i];
452 Abc_MergeSortCost_rec( pInput, pInput + 2*nSize, pOutput );
453 for ( i = 0; i < nSize; i++ )
454 pResult[i] = pInput[2*i];
455 free( pOutput );
456 free( pInput );
457 return pResult;
458}
459
460
461// this implementation uses 3x less memory but is 30% slower due to cache misses
462
463#if 0
464
476void Abc_MergeSortCostMerge( int * p1Beg, int * p1End, int * p2Beg, int * p2End, int * pOut, int * pCost )
477{
478 int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
479 int * pOutBeg = pOut;
480 while ( p1Beg < p1End && p2Beg < p2End )
481 {
482 if ( pCost[*p1Beg] == pCost[*p2Beg] )
483 *pOut++ = *p1Beg++, *pOut++ = *p2Beg++;
484 else if ( pCost[*p1Beg] < pCost[*p2Beg] )
485 *pOut++ = *p1Beg++;
486 else // if ( pCost[*p1Beg] > pCost[*p2Beg] )
487 *pOut++ = *p2Beg++;
488 }
489 while ( p1Beg < p1End )
490 *pOut++ = *p1Beg++;
491 while ( p2Beg < p2End )
492 *pOut++ = *p2Beg++;
493 assert( pOut - pOutBeg == nEntries );
494}
495
507void Abc_MergeSortCost_rec( int * pInBeg, int * pInEnd, int * pOutBeg, int * pCost )
508{
509 int nSize = pInEnd - pInBeg;
510 assert( nSize > 0 );
511 if ( nSize == 1 )
512 return;
513 if ( nSize == 2 )
514 {
515 if ( pCost[pInBeg[0]] > pCost[pInBeg[1]] )
516 {
517 pInBeg[0] ^= pInBeg[1];
518 pInBeg[1] ^= pInBeg[0];
519 pInBeg[0] ^= pInBeg[1];
520 }
521 }
522 else if ( nSize < 8 )
523 {
524 int temp, i, j, best_i;
525 for ( i = 0; i < nSize-1; i++ )
526 {
527 best_i = i;
528 for ( j = i+1; j < nSize; j++ )
529 if ( pCost[pInBeg[j]] < pCost[pInBeg[best_i]] )
530 best_i = j;
531 temp = pInBeg[i];
532 pInBeg[i] = pInBeg[best_i];
533 pInBeg[best_i] = temp;
534 }
535 }
536 else
537 {
538 Abc_MergeSortCost_rec( pInBeg, pInBeg + nSize/2, pOutBeg, pCost );
539 Abc_MergeSortCost_rec( pInBeg + nSize/2, pInEnd, pOutBeg + nSize/2, pCost );
540 Abc_MergeSortCostMerge( pInBeg, pInBeg + nSize/2, pInBeg + nSize/2, pInEnd, pOutBeg, pCost );
541 memcpy( pInBeg, pOutBeg, sizeof(int) * nSize );
542 }
543}
544
556int * Abc_MergeSortCost( int * pCosts, int nSize )
557{
558 int i, * pInput, * pOutput;
559 pInput = (int *) malloc( sizeof(int) * nSize );
560 for ( i = 0; i < nSize; i++ )
561 pInput[i] = i;
562 if ( nSize < 2 )
563 return pInput;
564 pOutput = (int *) malloc( sizeof(int) * nSize );
565 Abc_MergeSortCost_rec( pInput, pInput + nSize, pOutput, pCosts );
566 free( pOutput );
567 return pInput;
568}
569
570#endif
571
572
573
574
586int Abc_SortNumCompare( int * pNum1, int * pNum2 )
587{
588 return *pNum1 - *pNum2;
589}
590
603{
604 int fUseNew = 0;
605 int i, nSize = 50000000;
606 int * pArray = (int *)malloc( sizeof(int) * nSize );
607 int * pPerm;
608 abctime clk;
609 // generate numbers
610 srand( 1000 );
611 for ( i = 0; i < nSize; i++ )
612 pArray[i] = rand();
613
614 // try sorting
615 if ( fUseNew )
616 {
617 int fUseCost = 1;
618 if ( fUseCost )
619 {
620 clk = Abc_Clock();
621 pPerm = Abc_MergeSortCost( pArray, nSize );
622 Abc_PrintTime( 1, "New sort", Abc_Clock() - clk );
623 // check
624 for ( i = 1; i < nSize; i++ )
625 assert( pArray[pPerm[i-1]] <= pArray[pPerm[i]] );
626 free( pPerm );
627 }
628 else
629 {
630 clk = Abc_Clock();
631 Abc_MergeSort( pArray, nSize );
632 Abc_PrintTime( 1, "New sort", Abc_Clock() - clk );
633 // check
634 for ( i = 1; i < nSize; i++ )
635 assert( pArray[i-1] <= pArray[i] );
636 }
637 }
638 else
639 {
640 clk = Abc_Clock();
641 qsort( (void *)pArray, (size_t)nSize, sizeof(int), (int (*)(const void *, const void *)) Abc_SortNumCompare );
642 Abc_PrintTime( 1, "Old sort", Abc_Clock() - clk );
643 // check
644 for ( i = 1; i < nSize; i++ )
645 assert( pArray[i-1] <= pArray[i] );
646 }
647
648 free( pArray );
649}
650
651
652
653
666{
667 if ( (unsigned)(*p1) < (unsigned)(*p2) )
668 return -1;
669 if ( (unsigned)(*p1) > (unsigned)(*p2) )
670 return 1;
671 return 0;
672}
674{
675 if ( (unsigned)(*p1) > (unsigned)(*p2) )
676 return -1;
677 if ( (unsigned)(*p1) < (unsigned)(*p2) )
678 return 1;
679 return 0;
680}
681void Abc_QuickSort1( word * pData, int nSize, int fDecrease )
682{
683 int i, fVerify = 0;
684 if ( fDecrease )
685 {
686 qsort( (void *)pData, (size_t)nSize, sizeof(word), (int (*)(const void *, const void *))Abc_QuickSort1CompareDec );
687 if ( fVerify )
688 for ( i = 1; i < nSize; i++ )
689 assert( (unsigned)pData[i-1] >= (unsigned)pData[i] );
690 }
691 else
692 {
693 qsort( (void *)pData, (size_t)nSize, sizeof(word), (int (*)(const void *, const void *))Abc_QuickSort1CompareInc );
694 if ( fVerify )
695 for ( i = 1; i < nSize; i++ )
696 assert( (unsigned)pData[i-1] <= (unsigned)pData[i] );
697 }
698}
699
717static inline void Abc_SelectSortInc( word * pData, int nSize )
718{
719 int i, j, best_i;
720 for ( i = 0; i < nSize-1; i++ )
721 {
722 best_i = i;
723 for ( j = i+1; j < nSize; j++ )
724 if ( (unsigned)pData[j] < (unsigned)pData[best_i] )
725 best_i = j;
726 ABC_SWAP( word, pData[i], pData[best_i] );
727 }
728}
729static inline void Abc_SelectSortDec( word * pData, int nSize )
730{
731 int i, j, best_i;
732 for ( i = 0; i < nSize-1; i++ )
733 {
734 best_i = i;
735 for ( j = i+1; j < nSize; j++ )
736 if ( (unsigned)pData[j] > (unsigned)pData[best_i] )
737 best_i = j;
738 ABC_SWAP( word, pData[i], pData[best_i] );
739 }
740}
741
742void Abc_QuickSort2Inc_rec( word * pData, int l, int r )
743{
744 word v = pData[r];
745 int i = l-1, j = r;
746 if ( l >= r )
747 return;
748 assert( l < r );
749 if ( r - l < 10 )
750 {
751 Abc_SelectSortInc( pData + l, r - l + 1 );
752 return;
753 }
754 while ( 1 )
755 {
756 while ( (unsigned)pData[++i] < (unsigned)v );
757 while ( (unsigned)v < (unsigned)pData[--j] )
758 if ( j == l )
759 break;
760 if ( i >= j )
761 break;
762 ABC_SWAP( word, pData[i], pData[j] );
763 }
764 ABC_SWAP( word, pData[i], pData[r] );
765 Abc_QuickSort2Inc_rec( pData, l, i-1 );
766 Abc_QuickSort2Inc_rec( pData, i+1, r );
767}
768void Abc_QuickSort2Dec_rec( word * pData, int l, int r )
769{
770 word v = pData[r];
771 int i = l-1, j = r;
772 if ( l >= r )
773 return;
774 assert( l < r );
775 if ( r - l < 10 )
776 {
777 Abc_SelectSortDec( pData + l, r - l + 1 );
778 return;
779 }
780 while ( 1 )
781 {
782 while ( (unsigned)pData[++i] > (unsigned)v );
783 while ( (unsigned)v > (unsigned)pData[--j] )
784 if ( j == l )
785 break;
786 if ( i >= j )
787 break;
788 ABC_SWAP( word, pData[i], pData[j] );
789 }
790 ABC_SWAP( word, pData[i], pData[r] );
791 Abc_QuickSort2Dec_rec( pData, l, i-1 );
792 Abc_QuickSort2Dec_rec( pData, i+1, r );
793}
794
795void Abc_QuickSort3Inc_rec( word * pData, int l, int r )
796{
797 word v = pData[r];
798 int k, i = l-1, j = r, p = l-1, q = r;
799 if ( l >= r )
800 return;
801 assert( l < r );
802 if ( r - l < 10 )
803 {
804 Abc_SelectSortInc( pData + l, r - l + 1 );
805 return;
806 }
807 while ( 1 )
808 {
809 while ( (unsigned)pData[++i] < (unsigned)v );
810 while ( (unsigned)v < (unsigned)pData[--j] )
811 if ( j == l )
812 break;
813 if ( i >= j )
814 break;
815 ABC_SWAP( word, pData[i], pData[j] );
816 if ( (unsigned)pData[i] == (unsigned)v )
817 { p++; ABC_SWAP( word, pData[p], pData[i] ); }
818 if ( (unsigned)v == (unsigned)pData[j] )
819 { q--; ABC_SWAP( word, pData[j], pData[q] ); }
820 }
821 ABC_SWAP( word, pData[i], pData[r] );
822 j = i-1; i = i+1;
823 for ( k = l; k < p; k++, j-- )
824 ABC_SWAP( word, pData[k], pData[j] );
825 for ( k = r-1; k > q; k--, i++ )
826 ABC_SWAP( word, pData[i], pData[k] );
827 Abc_QuickSort3Inc_rec( pData, l, j );
828 Abc_QuickSort3Inc_rec( pData, i, r );
829}
830void Abc_QuickSort3Dec_rec( word * pData, int l, int r )
831{
832 word v = pData[r];
833 int k, i = l-1, j = r, p = l-1, q = r;
834 if ( l >= r )
835 return;
836 assert( l < r );
837 if ( r - l < 10 )
838 {
839 Abc_SelectSortDec( pData + l, r - l + 1 );
840 return;
841 }
842 while ( 1 )
843 {
844 while ( (unsigned)pData[++i] > (unsigned)v );
845 while ( (unsigned)v > (unsigned)pData[--j] )
846 if ( j == l )
847 break;
848 if ( i >= j )
849 break;
850 ABC_SWAP( word, pData[i], pData[j] );
851 if ( (unsigned)pData[i] == (unsigned)v )
852 { p++; ABC_SWAP( word, pData[p], pData[i] ); }
853 if ( (unsigned)v == (unsigned)pData[j] )
854 { q--; ABC_SWAP( word, pData[j], pData[q] ); }
855 }
856 ABC_SWAP( word, pData[i], pData[r] );
857 j = i-1; i = i+1;
858 for ( k = l; k < p; k++, j-- )
859 ABC_SWAP( word, pData[k], pData[j] );
860 for ( k = r-1; k > q; k--, i++ )
861 ABC_SWAP( word, pData[i], pData[k] );
862 Abc_QuickSort3Dec_rec( pData, l, j );
863 Abc_QuickSort3Dec_rec( pData, i, r );
864}
865
866void Abc_QuickSort2( word * pData, int nSize, int fDecrease )
867{
868 int i, fVerify = 0;
869 if ( fDecrease )
870 {
871 Abc_QuickSort2Dec_rec( pData, 0, nSize - 1 );
872 if ( fVerify )
873 for ( i = 1; i < nSize; i++ )
874 assert( (unsigned)pData[i-1] >= (unsigned)pData[i] );
875 }
876 else
877 {
878 Abc_QuickSort2Inc_rec( pData, 0, nSize - 1 );
879 if ( fVerify )
880 for ( i = 1; i < nSize; i++ )
881 assert( (unsigned)pData[i-1] <= (unsigned)pData[i] );
882 }
883}
884void Abc_QuickSort3( word * pData, int nSize, int fDecrease )
885{
886 int i, fVerify = 1;
887 if ( fDecrease )
888 {
889 Abc_QuickSort2Dec_rec( pData, 0, nSize - 1 );
890 if ( fVerify )
891 for ( i = 1; i < nSize; i++ )
892 assert( (unsigned)pData[i-1] >= (unsigned)pData[i] );
893 }
894 else
895 {
896 Abc_QuickSort2Inc_rec( pData, 0, nSize - 1 );
897 if ( fVerify )
898 for ( i = 1; i < nSize; i++ )
899 assert( (unsigned)pData[i-1] <= (unsigned)pData[i] );
900 }
901}
902
914void Abc_QuickSortCostData( int * pCosts, int nSize, int fDecrease, word * pData, int * pResult )
915{
916 int i;
917 for ( i = 0; i < nSize; i++ )
918 pData[i] = ((word)i << 32) | pCosts[i];
919 Abc_QuickSort3( pData, nSize, fDecrease );
920 for ( i = 0; i < nSize; i++ )
921 pResult[i] = (int)(pData[i] >> 32);
922}
923int * Abc_QuickSortCost( int * pCosts, int nSize, int fDecrease )
924{
925 word * pData = ABC_ALLOC( word, nSize );
926 int * pResult = ABC_ALLOC( int, nSize );
927 Abc_QuickSortCostData( pCosts, nSize, fDecrease, pData, pResult );
928 ABC_FREE( pData );
929 return pResult;
930}
931
932// extern void Abc_QuickSortTest();
933// Abc_QuickSortTest();
934
947{
948 int nSize = 1000000;
949 int fVerbose = 0;
950 word * pData1, * pData2;
951 int i;
952 abctime clk = Abc_Clock();
953 // generate numbers
954 pData1 = ABC_ALLOC( word, nSize );
955 pData2 = ABC_ALLOC( word, nSize );
956 srand( 1111 );
957 for ( i = 0; i < nSize; i++ )
958 pData2[i] = pData1[i] = ((word)i << 32) | rand();
959 Abc_PrintTime( 1, "Prepare ", Abc_Clock() - clk );
960 // perform sorting
961 clk = Abc_Clock();
962 Abc_QuickSort3( pData1, nSize, 1 );
963 Abc_PrintTime( 1, "Sort new", Abc_Clock() - clk );
964 // print the result
965 if ( fVerbose )
966 {
967 for ( i = 0; i < nSize; i++ )
968 printf( "(%d,%d) ", (int)(pData1[i] >> 32), (int)pData1[i] );
969 printf( "\n" );
970 }
971 // create new numbers
972 clk = Abc_Clock();
973 Abc_QuickSort1( pData2, nSize, 1 );
974 Abc_PrintTime( 1, "Sort old", Abc_Clock() - clk );
975 // print the result
976 if ( fVerbose )
977 {
978 for ( i = 0; i < nSize; i++ )
979 printf( "(%d,%d) ", (int)(pData2[i] >> 32), (int)pData2[i] );
980 printf( "\n" );
981 }
982 ABC_FREE( pData1 );
983 ABC_FREE( pData2 );
984}
985
997
998// Creates a sequence of random numbers.
999// http://www.codeproject.com/KB/recipes/SimpleRNG.aspx
1000
1001#define NUMBER1 3716960521u
1002#define NUMBER2 2174103536u
1003
1004unsigned Abc_Random( int fReset )
1005{
1006#ifdef _MSC_VER
1007 static unsigned int m_z = NUMBER1;
1008 static unsigned int m_w = NUMBER2;
1009#else
1010 static __thread unsigned int m_z = NUMBER1;
1011 static __thread unsigned int m_w = NUMBER2;
1012#endif
1013 if ( fReset )
1014 {
1015 m_z = NUMBER1;
1016 m_w = NUMBER2;
1017 }
1018 m_z = 36969 * (m_z & 65535) + (m_z >> 16);
1019 m_w = 18000 * (m_w & 65535) + (m_w >> 16);
1020 return (m_z << 16) + m_w;
1021}
1022word Abc_RandomW( int fReset )
1023{
1024 return ((word)Abc_Random(fReset) << 32) | ((word)Abc_Random(fReset) << 0);
1025}
1026
1030
1031
1033
#define ABC_SWAP(Type, a, b)
Definition abc_global.h:253
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define NUMBER2
Definition aigUtil.c:1157
#define NUMBER1
Definition aigUtil.c:1156
Cube * p
Definition exorList.c:222
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
word Abc_RandomW(int fReset)
Definition utilSort.c:1022
void Abc_QuickSortCostData(int *pCosts, int nSize, int fDecrease, word *pData, int *pResult)
Definition utilSort.c:914
void Abc_MergeSortCost_rec(int *pInBeg, int *pInEnd, int *pOutBeg)
Definition utilSort.c:387
void Abc_SortCost2_rec(int *pInBeg, int *pInEnd, int *pOutBeg, int *pCost)
Definition utilSort.c:182
void Abc_Sort_rec(int *pInBeg, int *pInEnd, int *pOutBeg)
Definition utilSort.c:80
void Abc_SortMergeCost2(int *p1Beg, int *p1End, int *p2Beg, int *p2End, int *pOut, int *pCost)
Definition utilSort.c:151
void Abc_MergeSort(int *pInput, int nSize)
Definition utilSort.c:129
void Abc_MergeSortCostMerge(int *p1Beg, int *p1End, int *p2Beg, int *p2End, int *pOut)
Definition utilSort.c:356
ABC_NAMESPACE_IMPL_START void Abc_SortMerge(int *p1Beg, int *p1End, int *p2Beg, int *p2End, int *pOut)
DECLARATIONS ///.
Definition utilSort.c:49
void Abc_MergeSortCost2(int *pInput, int nSize, int *pCost)
Definition utilSort.c:231
void Abc_QuickSort1(word *pData, int nSize, int fDecrease)
Definition utilSort.c:681
int * Abc_MergeSortCost(int *pCosts, int nSize)
Definition utilSort.c:442
void Abc_SortMergeCost2Reverse(int *p1Beg, int *p1End, int *p2Beg, int *p2End, int *pOut, int *pCost)
Definition utilSort.c:253
void Abc_SortCost2Reverse_rec(int *pInBeg, int *pInEnd, int *pOutBeg, int *pCost)
Definition utilSort.c:284
void Abc_QuickSort3Dec_rec(word *pData, int l, int r)
Definition utilSort.c:830
void Abc_QuickSort2(word *pData, int nSize, int fDecrease)
Definition utilSort.c:866
int Abc_QuickSort1CompareDec(word *p1, word *p2)
Definition utilSort.c:673
int * Abc_QuickSortCost(int *pCosts, int nSize, int fDecrease)
Definition utilSort.c:923
void Abc_QuickSort3Inc_rec(word *pData, int l, int r)
Definition utilSort.c:795
void Abc_QuickSort2Dec_rec(word *pData, int l, int r)
Definition utilSort.c:768
void Abc_QuickSort2Inc_rec(word *pData, int l, int r)
Definition utilSort.c:742
void Abc_SortTest()
Definition utilSort.c:602
unsigned Abc_Random(int fReset)
Definition utilSort.c:1004
int Abc_SortNumCompare(int *pNum1, int *pNum2)
Definition utilSort.c:586
void Abc_QuickSort3(word *pData, int nSize, int fDecrease)
Definition utilSort.c:884
int Abc_QuickSort1CompareInc(word *p1, word *p2)
Definition utilSort.c:665
void Abc_MergeSortCost2Reverse(int *pInput, int nSize, int *pCost)
Definition utilSort.c:333
void Abc_QuickSortTest()
Definition utilSort.c:946
#define assert(ex)
Definition util_old.h:213
char * memcpy()
char * calloc()
VOID_HACK free()
char * malloc()