1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/*
|
|
17 |
qsort implementation optimized for comparison of pointers
|
|
18 |
Inspired by the qsort implementations by Douglas C. Schmidt,
|
|
19 |
and Bentley & McIlroy's "Engineering a Sort Function".
|
|
20 |
*/
|
|
21 |
||
22 |
||
23 |
#include "mysys_priv.h" |
|
212.5.18
by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype. |
24 |
#include <mystrings/m_string.h> |
1
by brian
clean slate |
25 |
/* We need to use qsort with 2 different compare functions */
|
26 |
#ifdef QSORT_EXTRA_CMP_ARGUMENT
|
|
27 |
#define CMP(A,B) ((*cmp)(cmp_argument,(A),(B)))
|
|
28 |
#else
|
|
29 |
#define CMP(A,B) ((*cmp)((A),(B)))
|
|
30 |
#endif
|
|
31 |
||
32 |
#define SWAP(A, B, size,swap_ptrs) \
|
|
33 |
do { \
|
|
34 |
if (swap_ptrs) \
|
|
35 |
{ \
|
|
36 |
register char **a = (char**) (A), **b = (char**) (B); \
|
|
37 |
char *tmp = *a; *a++ = *b; *b++ = tmp; \
|
|
38 |
} \
|
|
39 |
else \
|
|
40 |
{ \
|
|
41 |
register char *a = (A), *b = (B); \
|
|
42 |
register char *end= a+size; \
|
|
43 |
do \
|
|
44 |
{ \
|
|
45 |
char tmp = *a; *a++ = *b; *b++ = tmp; \
|
|
46 |
} while (a < end); \
|
|
47 |
} \
|
|
48 |
} while (0)
|
|
49 |
||
50 |
/* Put the median in the middle argument */
|
|
51 |
#define MEDIAN(low, mid, high) \
|
|
52 |
{ \
|
|
53 |
if (CMP(high,low) < 0) \
|
|
54 |
SWAP(high, low, size, ptr_cmp); \
|
|
55 |
if (CMP(mid, low) < 0) \
|
|
56 |
SWAP(mid, low, size, ptr_cmp); \
|
|
57 |
else if (CMP(high, mid) < 0) \
|
|
58 |
SWAP(mid, high, size, ptr_cmp); \
|
|
59 |
}
|
|
60 |
||
61 |
/* The following node is used to store ranges to avoid recursive calls */
|
|
62 |
||
63 |
typedef struct st_stack |
|
64 |
{
|
|
65 |
char *low,*high; |
|
66 |
} stack_node; |
|
67 |
||
68 |
#define PUSH(LOW,HIGH) {stack_ptr->low = LOW; stack_ptr++->high = HIGH;}
|
|
69 |
#define POP(LOW,HIGH) {LOW = (--stack_ptr)->low; HIGH = stack_ptr->high;}
|
|
70 |
||
71 |
/* The following stack size is enough for ulong ~0 elements */
|
|
72 |
#define STACK_SIZE (8 * sizeof(unsigned long int))
|
|
73 |
#define THRESHOLD_FOR_INSERT_SORT 10
|
|
74 |
#if defined(QSORT_TYPE_IS_VOID)
|
|
75 |
#define SORT_RETURN return
|
|
76 |
#else
|
|
77 |
#define SORT_RETURN return 0
|
|
78 |
#endif
|
|
79 |
||
80 |
/****************************************************************************
|
|
81 |
** 'standard' quicksort with the following extensions:
|
|
82 |
**
|
|
83 |
** Can be compiled with the qsort2_cmp compare function
|
|
84 |
** Store ranges on stack to avoid recursion
|
|
85 |
** Use insert sort on small ranges
|
|
86 |
** Optimize for sorting of pointers (used often by MySQL)
|
|
87 |
** Use median comparison to find partition element
|
|
88 |
*****************************************************************************/
|
|
89 |
||
90 |
#ifdef QSORT_EXTRA_CMP_ARGUMENT
|
|
91 |
qsort_t my_qsort2(void *base_ptr, size_t count, size_t size, qsort2_cmp cmp, |
|
92 |
void *cmp_argument) |
|
93 |
#else
|
|
94 |
qsort_t my_qsort(void *base_ptr, size_t count, size_t size, qsort_cmp cmp) |
|
95 |
#endif
|
|
96 |
{
|
|
97 |
char *low, *high, *pivot; |
|
98 |
stack_node stack[STACK_SIZE], *stack_ptr; |
|
146
by Brian Aker
my_bool cleanup. |
99 |
bool ptr_cmp; |
1
by brian
clean slate |
100 |
/* Handle the simple case first */
|
101 |
/* This will also make the rest of the code simpler */
|
|
102 |
if (count <= 1) |
|
103 |
SORT_RETURN; |
|
104 |
||
105 |
low = (char*) base_ptr; |
|
106 |
high = low+ size * (count - 1); |
|
107 |
stack_ptr = stack + 1; |
|
108 |
#ifdef HAVE_purify
|
|
109 |
/* The first element in the stack will be accessed for the last POP */
|
|
110 |
stack[0].low=stack[0].high=0; |
|
111 |
#endif
|
|
112 |
pivot = (char *) my_alloca((int) size); |
|
113 |
ptr_cmp= size == sizeof(char*) && !((low - (char*) 0)& (sizeof(char*)-1)); |
|
114 |
||
115 |
/* The following loop sorts elements between high and low */
|
|
116 |
do
|
|
117 |
{
|
|
118 |
char *low_ptr, *high_ptr, *mid; |
|
119 |
||
120 |
count=((size_t) (high - low) / size)+1; |
|
121 |
/* If count is small, then an insert sort is faster than qsort */
|
|
122 |
if (count < THRESHOLD_FOR_INSERT_SORT) |
|
123 |
{
|
|
124 |
for (low_ptr = low + size; low_ptr <= high; low_ptr += size) |
|
125 |
{
|
|
126 |
char *ptr; |
|
127 |
for (ptr = low_ptr; ptr > low && CMP(ptr - size, ptr) > 0; |
|
128 |
ptr -= size) |
|
129 |
SWAP(ptr, ptr - size, size, ptr_cmp); |
|
130 |
}
|
|
131 |
POP(low, high); |
|
132 |
continue; |
|
133 |
}
|
|
134 |
||
135 |
/* Try to find a good middle element */
|
|
136 |
mid= low + size * (count >> 1); |
|
137 |
if (count > 40) /* Must be bigger than 24 */ |
|
138 |
{
|
|
139 |
size_t step = size* (count / 8); |
|
140 |
MEDIAN(low, low + step, low+step*2); |
|
141 |
MEDIAN(mid - step, mid, mid+step); |
|
142 |
MEDIAN(high - 2 * step, high-step, high); |
|
143 |
/* Put best median in 'mid' */
|
|
144 |
MEDIAN(low+step, mid, high-step); |
|
145 |
low_ptr = low; |
|
146 |
high_ptr = high; |
|
147 |
}
|
|
148 |
else
|
|
149 |
{
|
|
150 |
MEDIAN(low, mid, high); |
|
151 |
/* The low and high argument are already in sorted against 'pivot' */
|
|
152 |
low_ptr = low + size; |
|
153 |
high_ptr = high - size; |
|
154 |
}
|
|
155 |
memcpy(pivot, mid, size); |
|
156 |
||
157 |
do
|
|
158 |
{
|
|
159 |
while (CMP(low_ptr, pivot) < 0) |
|
160 |
low_ptr += size; |
|
161 |
while (CMP(pivot, high_ptr) < 0) |
|
162 |
high_ptr -= size; |
|
163 |
||
164 |
if (low_ptr < high_ptr) |
|
165 |
{
|
|
166 |
SWAP(low_ptr, high_ptr, size, ptr_cmp); |
|
167 |
low_ptr += size; |
|
168 |
high_ptr -= size; |
|
169 |
}
|
|
170 |
else
|
|
171 |
{
|
|
172 |
if (low_ptr == high_ptr) |
|
173 |
{
|
|
174 |
low_ptr += size; |
|
175 |
high_ptr -= size; |
|
176 |
}
|
|
177 |
break; |
|
178 |
}
|
|
179 |
}
|
|
180 |
while (low_ptr <= high_ptr); |
|
181 |
||
182 |
/*
|
|
183 |
Prepare for next iteration.
|
|
184 |
Skip partitions of size 1 as these doesn't have to be sorted
|
|
185 |
Push the larger partition and sort the smaller one first.
|
|
186 |
This ensures that the stack is keept small.
|
|
187 |
*/
|
|
188 |
||
189 |
if ((int) (high_ptr - low) <= 0) |
|
190 |
{
|
|
191 |
if ((int) (high - low_ptr) <= 0) |
|
192 |
{
|
|
193 |
POP(low, high); /* Nothing more to sort */ |
|
194 |
}
|
|
195 |
else
|
|
196 |
low = low_ptr; /* Ignore small left part. */ |
|
197 |
}
|
|
198 |
else if ((int) (high - low_ptr) <= 0) |
|
199 |
high = high_ptr; /* Ignore small right part. */ |
|
200 |
else if ((high_ptr - low) > (high - low_ptr)) |
|
201 |
{
|
|
202 |
PUSH(low, high_ptr); /* Push larger left part */ |
|
203 |
low = low_ptr; |
|
204 |
}
|
|
205 |
else
|
|
206 |
{
|
|
207 |
PUSH(low_ptr, high); /* Push larger right part */ |
|
208 |
high = high_ptr; |
|
209 |
}
|
|
210 |
} while (stack_ptr > stack); |
|
211 |
my_afree(pivot); |
|
212 |
SORT_RETURN; |
|
213 |
}
|