~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*****************************************************************************

Copyright (C) 1995, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
St, Fifth Floor, Boston, MA 02110-1301 USA

*****************************************************************************/

/******************************************************************//**
@file include/ut0sort.h
Sort utility

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.
CMP_FUN is the comparison function name. It takes as arguments
two elements from the array and returns 1, if the first is bigger,
0 if equal, and -1 if the second bigger. */

#define UT_SORT_FUNCTION_BODY(SORT_FUN, ARR, AUX_ARR, LOW, HIGH, CMP_FUN)\
{\
	ulint		ut_sort_mid77;\
	ulint		ut_sort_i77;\
	ulint		ut_sort_low77;\
	ulint		ut_sort_high77;\
\
	ut_ad((LOW) < (HIGH));\
	ut_ad(ARR);\
	ut_ad(AUX_ARR);\
\
	if ((LOW) == (HIGH) - 1) {\
		return;\
	} else if ((LOW) == (HIGH) - 2) {\
		if (CMP_FUN((ARR)[LOW], (ARR)[(HIGH) - 1]) > 0) {\
			(AUX_ARR)[LOW] = (ARR)[LOW];\
			(ARR)[LOW] = (ARR)[(HIGH) - 1];\
			(ARR)[(HIGH) - 1] = (AUX_ARR)[LOW];\
		}\
		return;\
	}\
\
	ut_sort_mid77 = ((LOW) + (HIGH)) / 2;\
\
	SORT_FUN((ARR), (AUX_ARR), (LOW), ut_sort_mid77);\
	SORT_FUN((ARR), (AUX_ARR), ut_sort_mid77, (HIGH));\
\
	ut_sort_low77 = (LOW);\
	ut_sort_high77 = ut_sort_mid77;\
\
	for (ut_sort_i77 = (LOW); ut_sort_i77 < (HIGH); ut_sort_i77++) {\
\
		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++;\
		} else if (CMP_FUN((ARR)[ut_sort_low77],\
				   (ARR)[ut_sort_high77]) > 0) {\
			(AUX_ARR)[ut_sort_i77] = (ARR)[ut_sort_high77];\
			ut_sort_high77++;\
		} else {\
			(AUX_ARR)[ut_sort_i77] = (ARR)[ut_sort_low77];\
			ut_sort_low77++;\
		}\
	}\
\
	memcpy((void*) ((ARR) + (LOW)), (AUX_ARR) + (LOW),\
	       ((HIGH) - (LOW)) * sizeof *(ARR));\
}\


#endif