6 #if !defined(ON_COMPILING_OPENNURBS_QSORT_FUNCTIONS) 11 #error Do not compile openurbs_qsort_template.c directly. 14 #define ON_QSORT_CUTOFF 8 22 #define ON_QSORT_STKSIZ (8*sizeof(void*) - 2) 26 #if !defined(ON_SORT_TEMPLATE_TYPE) 27 #error Define ON_SORT_TEMPLATE_TYPE macro before including opennurbs_qsort_template.c 30 #if !defined(ON_QSORT_FNAME) 31 #error Define ON_QSORT_FNAME macro before including opennurbs_qsort_template.c 34 #if !defined(ON_QSORT_GT) && !defined(ON_QSORT_LE) && !defined(ON_QSORT_EQ) 36 #if defined(ON_SORT_TEMPLATE_COMPARE) 38 #define ON_QSORT_GT(A,B) ON_SORT_TEMPLATE_COMPARE(A,B) > 0 39 #define ON_QSORT_LE(A,B) ON_SORT_TEMPLATE_COMPARE(A,B) <= 0 40 #define ON_QSORT_EQ(A,B) ON_SORT_TEMPLATE_COMPARE(A,B) == 0 43 #define ON_QSORT_GT(A,B) *A > *B 44 #define ON_QSORT_LE(A,B) *A <= *B 45 #define ON_QSORT_EQ(A,B) *A == *B 50 #if defined(ON_SORT_TEMPLATE_SWAP) 51 #define ON_QSORT_SWAP ON_SORT_TEMPLATE_SWAP 52 #elif defined(ON_SORT_TEMPLATE_USE_MEMCPY) 53 #define ON_QSORT_SWAP(A,B) memcpy(&tmp,A,sizeof(tmp));memcpy(A,B,sizeof(tmp));memcpy(B,&tmp,sizeof(tmp)) 55 #define ON_QSORT_SWAP(A,B) tmp = *A; *A = *B; *B = tmp 67 #if !defined(ON_SORT_TEMPLATE_HAVE_SHORT_SORT) 69 #if !defined(ON_QSORT_SHORT_SORT_FNAME) 71 #define ON_QSORT_SHORT_SORT_FNAME ON__shortsort 74 static void ON_QSORT_SHORT_SORT_FNAME(ON_SORT_TEMPLATE_TYPE *, ON_SORT_TEMPLATE_TYPE *);
75 static void ON_QSORT_SHORT_SORT_FNAME(ON_SORT_TEMPLATE_TYPE *lo, ON_SORT_TEMPLATE_TYPE *hi)
77 ON_SORT_TEMPLATE_TYPE *p;
78 ON_SORT_TEMPLATE_TYPE *max;
79 ON_SORT_TEMPLATE_TYPE tmp;
88 for (p = lo+1; p <= hi; p++)
91 if ( ON_QSORT_GT(p,max) )
100 ON_QSORT_SWAP(max,hi);
117 #if defined(ON_SORT_TEMPLATE_STATIC_FUNCTION) 122 ON_SORT_TEMPLATE_TYPE *base,
126 ON_SORT_TEMPLATE_TYPE *lo;
127 ON_SORT_TEMPLATE_TYPE *hi;
128 ON_SORT_TEMPLATE_TYPE *mid;
129 ON_SORT_TEMPLATE_TYPE *loguy;
130 ON_SORT_TEMPLATE_TYPE *higuy;
131 ON_SORT_TEMPLATE_TYPE *lostk[ON_QSORT_STKSIZ];
132 ON_SORT_TEMPLATE_TYPE *histk[ON_QSORT_STKSIZ];
135 ON_SORT_TEMPLATE_TYPE tmp;
137 if ( 0 == base || num < 2 )
150 size = (hi - lo) + 1;
153 if (size <= ON_QSORT_CUTOFF)
155 ON_QSORT_SHORT_SORT_FNAME(lo, hi);
167 mid = lo + (size / 2);
170 if ( ON_QSORT_GT(lo,mid) ) {ON_QSORT_SWAP(lo,mid);}
171 if ( ON_QSORT_GT(lo,hi) ) {ON_QSORT_SWAP(lo,hi);}
172 if ( ON_QSORT_GT(mid,hi) ) {ON_QSORT_SWAP(mid,hi);}
199 }
while (loguy < mid && ON_QSORT_LE(loguy,mid));
205 }
while (loguy <= hi && ON_QSORT_LE(loguy,mid));
213 }
while (higuy > mid && ON_QSORT_GT(higuy,mid));
225 ON_QSORT_SWAP(loguy,higuy);
255 }
while (higuy > mid && ON_QSORT_EQ(higuy,mid));
260 }
while (higuy > lo && ON_QSORT_EQ(higuy,mid));
276 if ( higuy - lo >= hi - loguy ) {
279 histk[stkptr] = higuy;
290 lostk[stkptr] = loguy;
319 #undef ON_QSORT_CUTOFF 320 #undef ON_QSORT_STKSIZ