1 /* *****************************************************************************
2 * Copyright (c) 2007-2014 Alexis Naveros.
4 * Ecere Corporation has unlimited/unrestricted rights.
5 * *****************************************************************************/
10 #include this whole file with the following definitions set:
12 #define MSORT_MAIN MyInlinedSortFunction
13 #define MSORT_CMP MyComparisonFunction
14 #define MSORT_TYPE int
19 #ifndef CC_MSORT_INLINE
30 #define CC_MSORT_INLINE
31 #define CC_MSORT_STACK_DEPTH (512)
37 #define MSORT_COPY(d,s) (*(d)=*(s))
43 #define MSORT_CONTEXT_PARAM , MSORT_CONTEXT context
44 #define MSORT_CONTEXT_PASS context,
45 #define MSORT_CONTEXT_PASSLAST , context
47 #define MSORT_CONTEXT_PARAM
48 #define MSORT_CONTEXT_PASS
49 #define MSORT_CONTEXT_PASSLAST
53 static MSORT_TYPE *MSORT_MAIN( MSORT_TYPE *src, MSORT_TYPE *tmp, int count MSORT_CONTEXT_PARAM )
55 int swapflag, depthbit, maxdepthbit;
56 ssize_t leftcount, rightcount;
64 ccMergeSortStack stack[CC_MSORT_STACK_DEPTH];
76 for( maxdepthbit = 1 ; ; maxdepthbit ^= 1 )
78 leftcount = leftcount - ( leftcount >> 1 );
87 if( !( depthbit ^ maxdepthbit ) )
89 if( ( count == 4 ) && MSORT_CMP( MSORT_CONTEXT_PASS &src[2], &src[3] ) )
91 MSORT_COPY( &temp, &src[2] );
92 MSORT_COPY( &src[2], &src[3] );
93 MSORT_COPY( &src[3], &temp );
95 if( MSORT_CMP( MSORT_CONTEXT_PASS &src[0], &src[1] ) )
97 MSORT_COPY( &temp, &src[0] );
98 MSORT_COPY( &src[0], &src[1] );
99 MSORT_COPY( &src[1], &temp );
107 if( MSORT_CMP( MSORT_CONTEXT_PASS &src[2], &src[3] ) )
109 MSORT_COPY( &dst[2], &src[3] );
110 MSORT_COPY( &dst[3], &src[2] );
114 MSORT_COPY( &dst[2], &src[2] );
115 MSORT_COPY( &dst[3], &src[3] );
118 else if( count == 3 )
119 MSORT_COPY( &dst[2], &src[2] );
120 if( MSORT_CMP( MSORT_CONTEXT_PASS &src[0], &src[1] ) )
122 MSORT_COPY( &dst[0], &src[1] );
123 MSORT_COPY( &dst[1], &src[0] );
127 MSORT_COPY( &dst[0], &src[0] );
128 MSORT_COPY( &dst[1], &src[1] );
138 rightcount = count >> 1;
139 leftcount = count - rightcount;
144 sp->depthbit = depthbit;
147 sp->src = src + leftcount;
148 sp->dst = dst + leftcount;
149 sp->count = (int)rightcount;
151 sp->depthbit = depthbit;
153 count = (int)leftcount;
159 rightcount = count >> 1;
160 leftcount = count - rightcount;
162 sr = src + leftcount;
166 if( MSORT_CMP( MSORT_CONTEXT_PASS sl, sr ) )
168 MSORT_COPY( dst++, sr++ );
171 for( dstend = &dst[leftcount] ; dst < dstend ; )
172 MSORT_COPY( dst++, sl++ );
177 MSORT_COPY( dst++, sl++ );
180 for( dstend = &dst[rightcount] ; dst < dstend ; )
181 MSORT_COPY( dst++, sr++ );
191 depthbit = sp->depthbit;
192 if( !( sp->mergeflag ) )
212 #undef MSORT_CONTEXT_PARAM
213 #undef MSORT_CONTEXT_PASS
214 #undef MSORT_CONTEXT_PASSLAST