~drizzle-trunk/drizzle/development

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
365.2.9 by Monty Taylor
Got rid of all instances of ~0
71
/* The following stack size is enough for UINT32_MAX elements */
1 by brian
clean slate
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
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
91
RETQSORTTYPE my_qsort2(void *base_ptr, size_t count, size_t size,
92
                       qsort2_cmp cmp, void *cmp_argument)
1 by brian
clean slate
93
#else
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
94
RETQSORTTYPE my_qsort(void *base_ptr, size_t count, size_t size,
95
                      qsort_cmp cmp)
1 by brian
clean slate
96
#endif
97
{
98
  char *low, *high, *pivot;
99
  stack_node stack[STACK_SIZE], *stack_ptr;
146 by Brian Aker
my_bool cleanup.
100
  bool ptr_cmp;
1 by brian
clean slate
101
  /* Handle the simple case first */
102
  /* This will also make the rest of the code simpler */
103
  if (count <= 1)
104
    SORT_RETURN;
105
106
  low  = (char*) base_ptr;
107
  high = low+ size * (count - 1);
108
  stack_ptr = stack + 1;
109
#ifdef HAVE_purify
110
  /* The first element in the stack will be accessed for the last POP */
111
  stack[0].low=stack[0].high=0;
112
#endif
113
  pivot = (char *) my_alloca((int) size);
114
  ptr_cmp= size == sizeof(char*) && !((low - (char*) 0)& (sizeof(char*)-1));
115
116
  /* The following loop sorts elements between high and low */
117
  do
118
  {
119
    char *low_ptr, *high_ptr, *mid;
120
121
    count=((size_t) (high - low) / size)+1;
122
    /* If count is small, then an insert sort is faster than qsort */
123
    if (count < THRESHOLD_FOR_INSERT_SORT)
124
    {
125
      for (low_ptr = low + size; low_ptr <= high; low_ptr += size)
126
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
127
        char *ptr;
128
        for (ptr = low_ptr; ptr > low && CMP(ptr - size, ptr) > 0;
129
             ptr -= size)
130
          SWAP(ptr, ptr - size, size, ptr_cmp);
1 by brian
clean slate
131
      }
132
      POP(low, high);
133
      continue;
134
    }
135
136
    /* Try to find a good middle element */
137
    mid= low + size * (count >> 1);
138
    if (count > 40)				/* Must be bigger than 24 */
139
    {
140
      size_t step = size* (count / 8);
141
      MEDIAN(low, low + step, low+step*2);
142
      MEDIAN(mid - step, mid, mid+step);
143
      MEDIAN(high - 2 * step, high-step, high);
144
      /* Put best median in 'mid' */
145
      MEDIAN(low+step, mid, high-step);
146
      low_ptr  = low;
147
      high_ptr = high;
148
    }
149
    else
150
    {
151
      MEDIAN(low, mid, high);
152
      /* The low and high argument are already in sorted against 'pivot' */
153
      low_ptr  = low + size;
154
      high_ptr = high - size;
155
    }
156
    memcpy(pivot, mid, size);
157
158
    do
159
    {
160
      while (CMP(low_ptr, pivot) < 0)
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
161
        low_ptr += size;
1 by brian
clean slate
162
      while (CMP(pivot, high_ptr) < 0)
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
163
        high_ptr -= size;
1 by brian
clean slate
164
165
      if (low_ptr < high_ptr)
166
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
167
        SWAP(low_ptr, high_ptr, size, ptr_cmp);
168
        low_ptr += size;
169
        high_ptr -= size;
1 by brian
clean slate
170
      }
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
171
      else
1 by brian
clean slate
172
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
173
        if (low_ptr == high_ptr)
174
        {
175
          low_ptr += size;
176
          high_ptr -= size;
177
        }
178
        break;
1 by brian
clean slate
179
      }
180
    }
181
    while (low_ptr <= high_ptr);
182
183
    /*
184
      Prepare for next iteration.
185
       Skip partitions of size 1 as these doesn't have to be sorted
186
       Push the larger partition and sort the smaller one first.
187
       This ensures that the stack is keept small.
188
    */
189
190
    if ((int) (high_ptr - low) <= 0)
191
    {
192
      if ((int) (high - low_ptr) <= 0)
193
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
194
        POP(low, high);			/* Nothing more to sort */
1 by brian
clean slate
195
      }
196
      else
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
197
        low = low_ptr;			/* Ignore small left part. */
1 by brian
clean slate
198
    }
199
    else if ((int) (high - low_ptr) <= 0)
200
      high = high_ptr;			/* Ignore small right part. */
201
    else if ((high_ptr - low) > (high - low_ptr))
202
    {
203
      PUSH(low, high_ptr);		/* Push larger left part */
204
      low = low_ptr;
205
    }
206
    else
207
    {
208
      PUSH(low_ptr, high);		/* Push larger right part */
209
      high = high_ptr;
210
    }
211
  } while (stack_ptr > stack);
212
  my_afree(pivot);
213
  SORT_RETURN;
214
}