summaryrefslogtreecommitdiffstats
path: root/src/misc/util
diff options
context:
space:
mode:
authorAlan Mishchenko <alanmi@berkeley.edu>2021-09-26 11:12:17 -0700
committerAlan Mishchenko <alanmi@berkeley.edu>2021-09-26 11:12:17 -0700
commit2ce1ce8bed69623de58d9394e22a3a8812096561 (patch)
tree2847ac05aa528f00aeb6c34af8a6eec4f0e7ee98 /src/misc/util
parent787dbb9433e2757d087db55217194bad4773c0de (diff)
downloadabc-2ce1ce8bed69623de58d9394e22a3a8812096561.tar.gz
abc-2ce1ce8bed69623de58d9394e22a3a8812096561.tar.bz2
abc-2ce1ce8bed69623de58d9394e22a3a8812096561.zip
Various changes.
Diffstat (limited to 'src/misc/util')
-rw-r--r--src/misc/util/abc_global.h1
-rw-r--r--src/misc/util/utilSort.c103
2 files changed, 104 insertions, 0 deletions
diff --git a/src/misc/util/abc_global.h b/src/misc/util/abc_global.h
index f6076399..5ba614c7 100644
--- a/src/misc/util/abc_global.h
+++ b/src/misc/util/abc_global.h
@@ -516,6 +516,7 @@ static inline void Abc_ReverseOrder( int * pA, int nA )
extern void Abc_MergeSort( int * pInput, int nSize );
extern int * Abc_MergeSortCost( int * pCosts, int nSize );
extern void Abc_MergeSortCost2( int * pInput, int nSize, int * pCost );
+extern void Abc_MergeSortCost2Reverse( int * pInput, int nSize, int * pCost );
extern void Abc_QuickSort1( word * pData, int nSize, int fDecrease );
extern void Abc_QuickSort2( word * pData, int nSize, int fDecrease );
extern void Abc_QuickSort3( word * pData, int nSize, int fDecrease );
diff --git a/src/misc/util/utilSort.c b/src/misc/util/utilSort.c
index 679017ed..a748caf9 100644
--- a/src/misc/util/utilSort.c
+++ b/src/misc/util/utilSort.c
@@ -250,6 +250,109 @@ void Abc_MergeSortCost2( int * pInput, int nSize, int * pCost )
SeeAlso []
***********************************************************************/
+void Abc_SortMergeCost2Reverse( int * p1Beg, int * p1End, int * p2Beg, int * p2End, int * pOut, int * pCost )
+{
+ int nEntries = (p1End - p1Beg) + (p2End - p2Beg);
+ int * pOutBeg = pOut;
+ while ( p1Beg < p1End && p2Beg < p2End )
+ {
+ if ( pCost[*p1Beg] == pCost[*p2Beg] )
+ *pOut++ = *p1Beg++, *pOut++ = *p2Beg++;
+ else if ( pCost[*p1Beg] > pCost[*p2Beg] )
+ *pOut++ = *p1Beg++;
+ else // if ( pCost[*p1Beg] < pCost[*p2Beg] )
+ *pOut++ = *p2Beg++;
+ }
+ while ( p1Beg < p1End )
+ *pOut++ = *p1Beg++;
+ while ( p2Beg < p2End )
+ *pOut++ = *p2Beg++;
+ assert( pOut - pOutBeg == nEntries );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Recursive sorting.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Abc_SortCost2Reverse_rec( int * pInBeg, int * pInEnd, int * pOutBeg, int * pCost )
+{
+ int nSize = pInEnd - pInBeg;
+ assert( nSize > 0 );
+ if ( nSize == 1 )
+ return;
+ if ( nSize == 2 )
+ {
+ if ( pCost[pInBeg[0]] < pCost[pInBeg[1]] )
+ {
+ pInBeg[0] ^= pInBeg[1];
+ pInBeg[1] ^= pInBeg[0];
+ pInBeg[0] ^= pInBeg[1];
+ }
+ }
+ else if ( nSize < 8 )
+ {
+ int temp, i, j, best_i;
+ for ( i = 0; i < nSize-1; i++ )
+ {
+ best_i = i;
+ for ( j = i+1; j < nSize; j++ )
+ if ( pCost[pInBeg[j]] > pCost[pInBeg[best_i]] )
+ best_i = j;
+ temp = pInBeg[i];
+ pInBeg[i] = pInBeg[best_i];
+ pInBeg[best_i] = temp;
+ }
+ }
+ else
+ {
+ Abc_SortCost2Reverse_rec( pInBeg, pInBeg + nSize/2, pOutBeg, pCost );
+ Abc_SortCost2Reverse_rec( pInBeg + nSize/2, pInEnd, pOutBeg + nSize/2, pCost );
+ Abc_SortMergeCost2Reverse( pInBeg, pInBeg + nSize/2, pInBeg + nSize/2, pInEnd, pOutBeg, pCost );
+ memcpy( pInBeg, pOutBeg, sizeof(int) * nSize );
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns the sorted array of integers.]
+
+ Description [This procedure is about 10% faster than qsort().]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Abc_MergeSortCost2Reverse( int * pInput, int nSize, int * pCost )
+{
+ int * pOutput;
+ if ( nSize < 2 )
+ return;
+ pOutput = (int *) malloc( sizeof(int) * nSize );
+ Abc_SortCost2Reverse_rec( pInput, pInput + nSize, pOutput, pCost );
+ free( pOutput );
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Merging two lists of entries.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
void Abc_MergeSortCostMerge( int * p1Beg, int * p1End, int * p2Beg, int * p2End, int * pOut )
{
int nEntries = (p1End - p1Beg) + (p2End - p2Beg);