MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mf_radix.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /*
17  Radixsort for pointers to fixed length strings.
18  A very quick sort for not to long (< 20 char) strings.
19  Neads a extra buffers of number_of_elements pointers but is
20  2-3 times faster than quicksort
21 */
22 
23 #include "mysys_priv.h"
24 #include <m_string.h>
25 
26  /* Radixsort */
27 
28 my_bool radixsort_is_appliccable(uint n_items, size_t size_of_element)
29 {
30  return size_of_element <= 20 && n_items >= 1000 && n_items < 100000;
31 }
32 
33 void radixsort_for_str_ptr(uchar **base, uint number_of_elements, size_t size_of_element, uchar **buffer)
34 {
35  uchar **end,**ptr,**buffer_ptr;
36  uint32 *count_ptr,*count_end,count[256];
37  int pass;
38 
39  end=base+number_of_elements; count_end=count+256;
40  for (pass=(int) size_of_element-1 ; pass >= 0 ; pass--)
41  {
42  memset(count, 0, sizeof(uint32)*256);
43  for (ptr= base ; ptr < end ; ptr++)
44  count[ptr[0][pass]]++;
45  if (count[0] == number_of_elements)
46  goto next;
47  for (count_ptr=count+1 ; count_ptr < count_end ; count_ptr++)
48  {
49  if (*count_ptr == number_of_elements)
50  goto next;
51  (*count_ptr)+= *(count_ptr-1);
52  }
53  for (ptr= end ; ptr-- != base ;)
54  buffer[--count[ptr[0][pass]]]= *ptr;
55  for (ptr=base, buffer_ptr=buffer ; ptr < end ;)
56  (*ptr++) = *buffer_ptr++;
57  next:;
58  }
59 }