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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 /*
29   Radixsort for pointers to fixed length strings.
30   A very quick sort for not to long (< 20 char) strings.
31   Neads a extra buffers of number_of_elements pointers but is
32   2-3 times faster than quicksort
33 */
34 
35 #include "mysys_priv.h"
36 #include <m_string.h>
37 
38 	/* Radixsort */
39 
radixsort_is_appliccable(uint n_items,size_t size_of_element)40 my_bool radixsort_is_appliccable(uint n_items, size_t size_of_element)
41 {
42   return size_of_element <= 20 && n_items >= 1000 && n_items < 100000;
43 }
44 
radixsort_for_str_ptr(uchar ** base,uint number_of_elements,size_t size_of_element,uchar ** buffer)45 void radixsort_for_str_ptr(uchar **base, uint number_of_elements, size_t size_of_element, uchar **buffer)
46 {
47   uchar **end,**ptr,**buffer_ptr;
48   uint32 *count_ptr,*count_end,count[256];
49   int pass;
50 
51   end=base+number_of_elements; count_end=count+256;
52   for (pass=(int) size_of_element-1 ; pass >= 0 ; pass--)
53   {
54     memset(count, 0, sizeof(uint32)*256);
55     for (ptr= base ; ptr < end ; ptr++)
56       count[ptr[0][pass]]++;
57     if (count[0] == number_of_elements)
58       goto next;
59     for (count_ptr=count+1 ; count_ptr < count_end ; count_ptr++)
60     {
61       if (*count_ptr == number_of_elements)
62 	goto next;
63       (*count_ptr)+= *(count_ptr-1);
64     }
65     for (ptr= end ; ptr-- != base ;)
66       buffer[--count[ptr[0][pass]]]= *ptr;
67     for (ptr=base, buffer_ptr=buffer ; ptr < end ;)
68       (*ptr++) = *buffer_ptr++;
69   next:;
70   }
71 }
72