2005-10-27 07:29:40 +00:00
|
|
|
/**********************************************************************
|
|
|
|
Sort utility
|
|
|
|
|
|
|
|
(c) 1995 Innobase Oy
|
|
|
|
|
|
|
|
Created 11/9/1995 Heikki Tuuri
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#ifndef ut0sort_h
|
|
|
|
#define ut0sort_h
|
|
|
|
|
|
|
|
#include "univ.i"
|
|
|
|
|
|
|
|
/* This module gives a macro definition of the body of
|
|
|
|
a standard sort function for an array of elements of any
|
|
|
|
type. The comparison function is given as a parameter to
|
|
|
|
the macro. The sort algorithm is mergesort which has logarithmic
|
|
|
|
worst case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
This macro expands to the body of a standard sort function.
|
|
|
|
The sort function uses mergesort and must be defined separately
|
|
|
|
for each type of array.
|
|
|
|
Also the comparison function has to be defined individually
|
|
|
|
for each array cell type. SORT_FUN is the sort function name.
|
|
|
|
The function takes the array to be sorted (ARR),
|
|
|
|
the array of auxiliary space (AUX_ARR) of same size,
|
|
|
|
and the low (LOW), inclusive, and high (HIGH), noninclusive,
|
|
|
|
limits for the sort interval as arguments.
|
2007-05-28 07:42:28 +00:00
|
|
|
CMP_FUN is the comparison function name. It takes as arguments
|
2005-10-27 07:29:40 +00:00
|
|
|
two elements from the array and returns 1, if the first is bigger,
|
2007-06-08 07:37:07 +00:00
|
|
|
0 if equal, and -1 if the second bigger. */
|
2005-10-27 07:29:40 +00:00
|
|
|
|
2007-05-28 07:42:28 +00:00
|
|
|
#define UT_SORT_FUNCTION_BODY(SORT_FUN, ARR, AUX_ARR, LOW, HIGH, CMP_FUN)\
|
2005-10-27 07:29:40 +00:00
|
|
|
{\
|
|
|
|
ulint ut_sort_mid77;\
|
|
|
|
ulint ut_sort_i77;\
|
|
|
|
ulint ut_sort_low77;\
|
|
|
|
ulint ut_sort_high77;\
|
|
|
|
\
|
2006-02-23 19:25:29 +00:00
|
|
|
ut_ad((LOW) < (HIGH));\
|
|
|
|
ut_ad(ARR);\
|
|
|
|
ut_ad(AUX_ARR);\
|
2005-10-27 07:29:40 +00:00
|
|
|
\
|
2006-02-23 19:25:29 +00:00
|
|
|
if ((LOW) == (HIGH) - 1) {\
|
|
|
|
return;\
|
|
|
|
} else if ((LOW) == (HIGH) - 2) {\
|
2007-05-28 07:42:28 +00:00
|
|
|
if (CMP_FUN((ARR)[LOW], (ARR)[(HIGH) - 1]) > 0) {\
|
2006-02-23 19:25:29 +00:00
|
|
|
(AUX_ARR)[LOW] = (ARR)[LOW];\
|
|
|
|
(ARR)[LOW] = (ARR)[(HIGH) - 1];\
|
|
|
|
(ARR)[(HIGH) - 1] = (AUX_ARR)[LOW];\
|
|
|
|
}\
|
|
|
|
return;\
|
|
|
|
}\
|
2005-10-27 07:29:40 +00:00
|
|
|
\
|
|
|
|
ut_sort_mid77 = ((LOW) + (HIGH)) / 2;\
|
|
|
|
\
|
2007-05-28 07:42:28 +00:00
|
|
|
SORT_FUN((ARR), (AUX_ARR), (LOW), ut_sort_mid77);\
|
|
|
|
SORT_FUN((ARR), (AUX_ARR), ut_sort_mid77, (HIGH));\
|
2005-10-27 07:29:40 +00:00
|
|
|
\
|
|
|
|
ut_sort_low77 = (LOW);\
|
|
|
|
ut_sort_high77 = ut_sort_mid77;\
|
|
|
|
\
|
2006-02-23 19:25:29 +00:00
|
|
|
for (ut_sort_i77 = (LOW); ut_sort_i77 < (HIGH); ut_sort_i77++) {\
|
2005-10-27 07:29:40 +00:00
|
|
|
\
|
2006-02-23 19:25:29 +00:00
|
|
|
if (ut_sort_low77 >= ut_sort_mid77) {\
|
|
|
|
(AUX_ARR)[ut_sort_i77] = (ARR)[ut_sort_high77];\
|
|
|
|
ut_sort_high77++;\
|
|
|
|
} else if (ut_sort_high77 >= (HIGH)) {\
|
|
|
|
(AUX_ARR)[ut_sort_i77] = (ARR)[ut_sort_low77];\
|
|
|
|
ut_sort_low77++;\
|
2007-05-28 07:42:28 +00:00
|
|
|
} else if (CMP_FUN((ARR)[ut_sort_low77],\
|
2005-10-27 07:29:40 +00:00
|
|
|
(ARR)[ut_sort_high77]) > 0) {\
|
2006-02-23 19:25:29 +00:00
|
|
|
(AUX_ARR)[ut_sort_i77] = (ARR)[ut_sort_high77];\
|
|
|
|
ut_sort_high77++;\
|
2005-10-27 07:29:40 +00:00
|
|
|
} else {\
|
2006-02-23 19:25:29 +00:00
|
|
|
(AUX_ARR)[ut_sort_i77] = (ARR)[ut_sort_low77];\
|
|
|
|
ut_sort_low77++;\
|
2005-10-27 07:29:40 +00:00
|
|
|
}\
|
|
|
|
}\
|
|
|
|
\
|
2006-02-23 19:25:29 +00:00
|
|
|
for (ut_sort_i77 = (LOW); ut_sort_i77 < (HIGH); ut_sort_i77++) {\
|
|
|
|
(ARR)[ut_sort_i77] = (AUX_ARR)[ut_sort_i77];\
|
|
|
|
}\
|
2005-10-27 07:29:40 +00:00
|
|
|
}\
|
|
|
|
|
2006-02-23 19:25:29 +00:00
|
|
|
|
2005-10-27 07:29:40 +00:00
|
|
|
#endif
|
|
|
|
|