Back to index

cell-binutils  2.17cvs20070401
sort.c
Go to the documentation of this file.
00001 /* Sorting algorithms.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    Contributed by Mark Mitchell <mark@codesourcery.com>.
00004 
00005 This file is part of GNU CC.
00006    
00007 GNU CC is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2, or (at your option)
00010 any later version.
00011 
00012 GNU CC is distributed in the hope that it will be useful, but
00013 WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GNU CC; see the file COPYING.  If not, write to
00019 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
00020 Boston, MA 02110-1301, USA.  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 #include "libiberty.h"
00026 #include "sort.h"
00027 #ifdef HAVE_LIMITS_H
00028 #include <limits.h>
00029 #endif
00030 #ifdef HAVE_SYS_PARAM_H
00031 #include <sys/param.h>
00032 #endif
00033 #ifdef HAVE_STDLIB_H
00034 #include <stdlib.h>
00035 #endif
00036 #ifdef HAVE_STRING_H
00037 #include <string.h>
00038 #endif
00039 
00040 #ifndef UCHAR_MAX
00041 #define UCHAR_MAX ((unsigned char)(-1))
00042 #endif
00043 
00044 /* POINTERS and WORK are both arrays of N pointers.  When this
00045    function returns POINTERS will be sorted in ascending order.  */
00046 
00047 void sort_pointers (size_t n, void **pointers, void **work)
00048 {
00049   /* The type of a single digit.  This can be any unsigned integral
00050      type.  When changing this, DIGIT_MAX should be changed as 
00051      well.  */
00052   typedef unsigned char digit_t;
00053 
00054   /* The maximum value a single digit can have.  */
00055 #define DIGIT_MAX (UCHAR_MAX + 1)
00056 
00057   /* The Ith entry is the number of elements in *POINTERSP that have I
00058      in the digit on which we are currently sorting.  */
00059   unsigned int count[DIGIT_MAX];
00060   /* Nonzero if we are running on a big-endian machine.  */
00061   int big_endian_p;
00062   size_t i;
00063   size_t j;
00064 
00065   /* The algorithm used here is radix sort which takes time linear in
00066      the number of elements in the array.  */
00067 
00068   /* The algorithm here depends on being able to swap the two arrays
00069      an even number of times.  */
00070   if ((sizeof (void *) / sizeof (digit_t)) % 2 != 0)
00071     abort ();
00072 
00073   /* Figure out the endianness of the machine.  */
00074   for (i = 0, j = 0; i < sizeof (size_t); ++i)
00075     {
00076       j *= (UCHAR_MAX + 1);
00077       j += i;
00078     }
00079   big_endian_p = (((char *)&j)[0] == 0);
00080 
00081   /* Move through the pointer values from least significant to most
00082      significant digits.  */
00083   for (i = 0; i < sizeof (void *) / sizeof (digit_t); ++i)
00084     {
00085       digit_t *digit;
00086       digit_t *bias;
00087       digit_t *top;
00088       unsigned int *countp;
00089       void **pointerp;
00090 
00091       /* The offset from the start of the pointer will depend on the
00092         endianness of the machine.  */
00093       if (big_endian_p)
00094        j = sizeof (void *) / sizeof (digit_t) - i;
00095       else
00096        j = i;
00097        
00098       /* Now, perform a stable sort on this digit.  We use counting
00099         sort.  */
00100       memset (count, 0, DIGIT_MAX * sizeof (unsigned int));
00101 
00102       /* Compute the address of the appropriate digit in the first and
00103         one-past-the-end elements of the array.  On a little-endian
00104         machine, the least-significant digit is closest to the front.  */
00105       bias = ((digit_t *) pointers) + j;
00106       top = ((digit_t *) (pointers + n)) + j;
00107 
00108       /* Count how many there are of each value.  At the end of this
00109         loop, COUNT[K] will contain the number of pointers whose Ith
00110         digit is K.  */
00111       for (digit = bias; 
00112           digit < top; 
00113           digit += sizeof (void *) / sizeof (digit_t))
00114        ++count[*digit];
00115 
00116       /* Now, make COUNT[K] contain the number of pointers whose Ith
00117         digit is less than or equal to K.  */
00118       for (countp = count + 1; countp < count + DIGIT_MAX; ++countp)
00119        *countp += countp[-1];
00120 
00121       /* Now, drop the pointers into their correct locations.  */
00122       for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp)
00123        work[--count[((digit_t *) pointerp)[j]]] = *pointerp;
00124 
00125       /* Swap WORK and POINTERS so that POINTERS contains the sorted
00126         array.  */
00127       pointerp = pointers;
00128       pointers = work;
00129       work = pointerp;
00130     }
00131 }
00132 
00133 /* Everything below here is a unit test for the routines in this
00134    file.  */
00135 
00136 #ifdef UNIT_TEST
00137 
00138 #include <stdio.h>
00139 
00140 void *xmalloc (size_t n)
00141 {
00142   return malloc (n);
00143 }
00144 
00145 int main (int argc, char **argv)
00146 {
00147   int k;
00148   int result;
00149   size_t i;
00150   void **pointers;
00151   void **work;
00152 
00153   if (argc > 1)
00154     k = atoi (argv[1]);
00155   else
00156     k = 10;
00157 
00158   pointers = XNEWVEC (void*, k);
00159   work = XNEWVEC (void*, k);
00160 
00161   for (i = 0; i < k; ++i)
00162     {
00163       pointers[i] = (void *) random ();
00164       printf ("%x\n", pointers[i]);
00165     }
00166 
00167   sort_pointers (k, pointers, work);
00168 
00169   printf ("\nSorted\n\n");
00170 
00171   result = 0;
00172 
00173   for (i = 0; i < k; ++i)
00174     {
00175       printf ("%x\n", pointers[i]);
00176       if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1])
00177        result = 1;
00178     }
00179 
00180   free (pointers);
00181   free (work);
00182 
00183   return result;
00184 }
00185 
00186 #endif