| 1 | /* Copyright (c) 2000, 2007 MySQL AB |
| 2 | Use is subject to license terms |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
| 16 | |
| 17 | /* |
| 18 | Radixsort for pointers to fixed length strings. |
| 19 | A very quick sort for not to long (< 20 char) strings. |
| 20 | Neads a extra buffers of number_of_elements pointers but is |
| 21 | 2-3 times faster than quicksort |
| 22 | */ |
| 23 | |
| 24 | #include "mysys_priv.h" |
| 25 | #include <m_string.h> |
| 26 | |
| 27 | /* Radixsort */ |
| 28 | |
| 29 | my_bool radixsort_is_appliccable(uint n_items, size_t size_of_element) |
| 30 | { |
| 31 | return size_of_element <= 20 && n_items >= 1000 && n_items < 100000; |
| 32 | } |
| 33 | |
| 34 | void radixsort_for_str_ptr(uchar **base, uint number_of_elements, size_t size_of_element, uchar **buffer) |
| 35 | { |
| 36 | uchar **end,**ptr,**buffer_ptr; |
| 37 | uint32 *count_ptr,*count_end,count[256]; |
| 38 | int pass; |
| 39 | |
| 40 | end=base+number_of_elements; count_end=count+256; |
| 41 | for (pass=(int) size_of_element-1 ; pass >= 0 ; pass--) |
| 42 | { |
| 43 | bzero((uchar*) count,sizeof(uint32)*256); |
| 44 | for (ptr= base ; ptr < end ; ptr++) |
| 45 | count[ptr[0][pass]]++; |
| 46 | if (count[0] == number_of_elements) |
| 47 | goto next; |
| 48 | for (count_ptr=count+1 ; count_ptr < count_end ; count_ptr++) |
| 49 | { |
| 50 | if (*count_ptr == number_of_elements) |
| 51 | goto next; |
| 52 | (*count_ptr)+= *(count_ptr-1); |
| 53 | } |
| 54 | for (ptr= end ; ptr-- != base ;) |
| 55 | buffer[--count[ptr[0][pass]]]= *ptr; |
| 56 | for (ptr=base, buffer_ptr=buffer ; ptr < end ;) |
| 57 | (*ptr++) = *buffer_ptr++; |
| 58 | next:; |
| 59 | } |
| 60 | } |
| 61 | |