~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
994.2.4 by Monty Taylor
Blast. Fixed some make distcheck issues.
23
#include "mysys/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
75
/****************************************************************************
76
** 'standard' quicksort with the following extensions:
77
**
78
** Can be compiled with the qsort2_cmp compare function
79
** Store ranges on stack to avoid recursion
80
** Use insert sort on small ranges
81
** Optimize for sorting of pointers (used often by MySQL)
82
** Use median comparison to find partition element
83
*****************************************************************************/
84
85
#ifdef QSORT_EXTRA_CMP_ARGUMENT
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
86
void my_qsort2(void *base_ptr, size_t count, size_t size,
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
87
                       qsort2_cmp cmp, void *cmp_argument)
1 by brian
clean slate
88
#else
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
89
void my_qsort(void *base_ptr, size_t count, size_t size,
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
90
                      qsort_cmp cmp)
1 by brian
clean slate
91
#endif
92
{
93
  char *low, *high, *pivot;
94
  stack_node stack[STACK_SIZE], *stack_ptr;
146 by Brian Aker
my_bool cleanup.
95
  bool ptr_cmp;
1 by brian
clean slate
96
  /* Handle the simple case first */
97
  /* This will also make the rest of the code simpler */
98
  if (count <= 1)
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
99
    return;
1 by brian
clean slate
100
101
  low  = (char*) base_ptr;
102
  high = low+ size * (count - 1);
103
  stack_ptr = stack + 1;
104
#ifdef HAVE_purify
105
  /* The first element in the stack will be accessed for the last POP */
106
  stack[0].low=stack[0].high=0;
107
#endif
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
108
  pivot = (char *) malloc(size);
1 by brian
clean slate
109
  ptr_cmp= size == sizeof(char*) && !((low - (char*) 0)& (sizeof(char*)-1));
110
111
  /* The following loop sorts elements between high and low */
112
  do
113
  {
114
    char *low_ptr, *high_ptr, *mid;
115
116
    count=((size_t) (high - low) / size)+1;
117
    /* If count is small, then an insert sort is faster than qsort */
118
    if (count < THRESHOLD_FOR_INSERT_SORT)
119
    {
120
      for (low_ptr = low + size; low_ptr <= high; low_ptr += size)
121
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
122
        char *ptr;
123
        for (ptr = low_ptr; ptr > low && CMP(ptr - size, ptr) > 0;
124
             ptr -= size)
125
          SWAP(ptr, ptr - size, size, ptr_cmp);
1 by brian
clean slate
126
      }
127
      POP(low, high);
128
      continue;
129
    }
130
131
    /* Try to find a good middle element */
132
    mid= low + size * (count >> 1);
133
    if (count > 40)				/* Must be bigger than 24 */
134
    {
135
      size_t step = size* (count / 8);
136
      MEDIAN(low, low + step, low+step*2);
137
      MEDIAN(mid - step, mid, mid+step);
138
      MEDIAN(high - 2 * step, high-step, high);
139
      /* Put best median in 'mid' */
140
      MEDIAN(low+step, mid, high-step);
141
      low_ptr  = low;
142
      high_ptr = high;
143
    }
144
    else
145
    {
146
      MEDIAN(low, mid, high);
147
      /* The low and high argument are already in sorted against 'pivot' */
148
      low_ptr  = low + size;
149
      high_ptr = high - size;
150
    }
151
    memcpy(pivot, mid, size);
152
153
    do
154
    {
155
      while (CMP(low_ptr, pivot) < 0)
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
156
        low_ptr += size;
1 by brian
clean slate
157
      while (CMP(pivot, high_ptr) < 0)
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
158
        high_ptr -= size;
1 by brian
clean slate
159
160
      if (low_ptr < high_ptr)
161
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
162
        SWAP(low_ptr, high_ptr, size, ptr_cmp);
163
        low_ptr += size;
164
        high_ptr -= size;
1 by brian
clean slate
165
      }
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
166
      else
1 by brian
clean slate
167
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
168
        if (low_ptr == high_ptr)
169
        {
170
          low_ptr += size;
171
          high_ptr -= size;
172
        }
173
        break;
1 by brian
clean slate
174
      }
175
    }
176
    while (low_ptr <= high_ptr);
177
178
    /*
179
      Prepare for next iteration.
180
       Skip partitions of size 1 as these doesn't have to be sorted
181
       Push the larger partition and sort the smaller one first.
182
       This ensures that the stack is keept small.
183
    */
184
185
    if ((int) (high_ptr - low) <= 0)
186
    {
187
      if ((int) (high - low_ptr) <= 0)
188
      {
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
189
        POP(low, high);			/* Nothing more to sort */
1 by brian
clean slate
190
      }
191
      else
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
192
        low = low_ptr;			/* Ignore small left part. */
1 by brian
clean slate
193
    }
194
    else if ((int) (high - low_ptr) <= 0)
195
      high = high_ptr;			/* Ignore small right part. */
196
    else if ((high_ptr - low) > (high - low_ptr))
197
    {
198
      PUSH(low, high_ptr);		/* Push larger left part */
199
      low = low_ptr;
200
    }
201
    else
202
    {
203
      PUSH(low_ptr, high);		/* Push larger right part */
204
      high = high_ptr;
205
    }
206
  } while (stack_ptr > stack);
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
207
  free(pivot);
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
208
  return;
1 by brian
clean slate
209
}