xref: /dflybsd-src/contrib/gcc-4.7/libiberty/sort.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Sorting algorithms.
2*e4b17023SJohn Marino    Copyright (C) 2000 Free Software Foundation, Inc.
3*e4b17023SJohn Marino    Contributed by Mark Mitchell <mark@codesourcery.com>.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GNU CC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GNU CC is free software; you can redistribute it and/or modify it
8*e4b17023SJohn Marino under the terms of the GNU General Public License as published by
9*e4b17023SJohn Marino the Free Software Foundation; either version 2, or (at your option)
10*e4b17023SJohn Marino any later version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GNU CC is distributed in the hope that it will be useful, but
13*e4b17023SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*e4b17023SJohn Marino General Public License for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GNU CC; see the file COPYING.  If not, write to
19*e4b17023SJohn Marino the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20*e4b17023SJohn Marino Boston, MA 02110-1301, USA.  */
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino #ifdef HAVE_CONFIG_H
23*e4b17023SJohn Marino #include "config.h"
24*e4b17023SJohn Marino #endif
25*e4b17023SJohn Marino #include "libiberty.h"
26*e4b17023SJohn Marino #include "sort.h"
27*e4b17023SJohn Marino #ifdef HAVE_LIMITS_H
28*e4b17023SJohn Marino #include <limits.h>
29*e4b17023SJohn Marino #endif
30*e4b17023SJohn Marino #ifdef HAVE_SYS_PARAM_H
31*e4b17023SJohn Marino #include <sys/param.h>
32*e4b17023SJohn Marino #endif
33*e4b17023SJohn Marino #ifdef HAVE_STDLIB_H
34*e4b17023SJohn Marino #include <stdlib.h>
35*e4b17023SJohn Marino #endif
36*e4b17023SJohn Marino #ifdef HAVE_STRING_H
37*e4b17023SJohn Marino #include <string.h>
38*e4b17023SJohn Marino #endif
39*e4b17023SJohn Marino 
40*e4b17023SJohn Marino #ifndef UCHAR_MAX
41*e4b17023SJohn Marino #define UCHAR_MAX ((unsigned char)(-1))
42*e4b17023SJohn Marino #endif
43*e4b17023SJohn Marino 
44*e4b17023SJohn Marino /* POINTERS and WORK are both arrays of N pointers.  When this
45*e4b17023SJohn Marino    function returns POINTERS will be sorted in ascending order.  */
46*e4b17023SJohn Marino 
sort_pointers(size_t n,void ** pointers,void ** work)47*e4b17023SJohn Marino void sort_pointers (size_t n, void **pointers, void **work)
48*e4b17023SJohn Marino {
49*e4b17023SJohn Marino   /* The type of a single digit.  This can be any unsigned integral
50*e4b17023SJohn Marino      type.  When changing this, DIGIT_MAX should be changed as
51*e4b17023SJohn Marino      well.  */
52*e4b17023SJohn Marino   typedef unsigned char digit_t;
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino   /* The maximum value a single digit can have.  */
55*e4b17023SJohn Marino #define DIGIT_MAX (UCHAR_MAX + 1)
56*e4b17023SJohn Marino 
57*e4b17023SJohn Marino   /* The Ith entry is the number of elements in *POINTERSP that have I
58*e4b17023SJohn Marino      in the digit on which we are currently sorting.  */
59*e4b17023SJohn Marino   unsigned int count[DIGIT_MAX];
60*e4b17023SJohn Marino   /* Nonzero if we are running on a big-endian machine.  */
61*e4b17023SJohn Marino   int big_endian_p;
62*e4b17023SJohn Marino   size_t i;
63*e4b17023SJohn Marino   size_t j;
64*e4b17023SJohn Marino 
65*e4b17023SJohn Marino   /* The algorithm used here is radix sort which takes time linear in
66*e4b17023SJohn Marino      the number of elements in the array.  */
67*e4b17023SJohn Marino 
68*e4b17023SJohn Marino   /* The algorithm here depends on being able to swap the two arrays
69*e4b17023SJohn Marino      an even number of times.  */
70*e4b17023SJohn Marino   if ((sizeof (void *) / sizeof (digit_t)) % 2 != 0)
71*e4b17023SJohn Marino     abort ();
72*e4b17023SJohn Marino 
73*e4b17023SJohn Marino   /* Figure out the endianness of the machine.  */
74*e4b17023SJohn Marino   for (i = 0, j = 0; i < sizeof (size_t); ++i)
75*e4b17023SJohn Marino     {
76*e4b17023SJohn Marino       j *= (UCHAR_MAX + 1);
77*e4b17023SJohn Marino       j += i;
78*e4b17023SJohn Marino     }
79*e4b17023SJohn Marino   big_endian_p = (((char *)&j)[0] == 0);
80*e4b17023SJohn Marino 
81*e4b17023SJohn Marino   /* Move through the pointer values from least significant to most
82*e4b17023SJohn Marino      significant digits.  */
83*e4b17023SJohn Marino   for (i = 0; i < sizeof (void *) / sizeof (digit_t); ++i)
84*e4b17023SJohn Marino     {
85*e4b17023SJohn Marino       digit_t *digit;
86*e4b17023SJohn Marino       digit_t *bias;
87*e4b17023SJohn Marino       digit_t *top;
88*e4b17023SJohn Marino       unsigned int *countp;
89*e4b17023SJohn Marino       void **pointerp;
90*e4b17023SJohn Marino 
91*e4b17023SJohn Marino       /* The offset from the start of the pointer will depend on the
92*e4b17023SJohn Marino 	 endianness of the machine.  */
93*e4b17023SJohn Marino       if (big_endian_p)
94*e4b17023SJohn Marino 	j = sizeof (void *) / sizeof (digit_t) - i;
95*e4b17023SJohn Marino       else
96*e4b17023SJohn Marino 	j = i;
97*e4b17023SJohn Marino 
98*e4b17023SJohn Marino       /* Now, perform a stable sort on this digit.  We use counting
99*e4b17023SJohn Marino 	 sort.  */
100*e4b17023SJohn Marino       memset (count, 0, DIGIT_MAX * sizeof (unsigned int));
101*e4b17023SJohn Marino 
102*e4b17023SJohn Marino       /* Compute the address of the appropriate digit in the first and
103*e4b17023SJohn Marino 	 one-past-the-end elements of the array.  On a little-endian
104*e4b17023SJohn Marino 	 machine, the least-significant digit is closest to the front.  */
105*e4b17023SJohn Marino       bias = ((digit_t *) pointers) + j;
106*e4b17023SJohn Marino       top = ((digit_t *) (pointers + n)) + j;
107*e4b17023SJohn Marino 
108*e4b17023SJohn Marino       /* Count how many there are of each value.  At the end of this
109*e4b17023SJohn Marino 	 loop, COUNT[K] will contain the number of pointers whose Ith
110*e4b17023SJohn Marino 	 digit is K.  */
111*e4b17023SJohn Marino       for (digit = bias;
112*e4b17023SJohn Marino 	   digit < top;
113*e4b17023SJohn Marino 	   digit += sizeof (void *) / sizeof (digit_t))
114*e4b17023SJohn Marino 	++count[*digit];
115*e4b17023SJohn Marino 
116*e4b17023SJohn Marino       /* Now, make COUNT[K] contain the number of pointers whose Ith
117*e4b17023SJohn Marino 	 digit is less than or equal to K.  */
118*e4b17023SJohn Marino       for (countp = count + 1; countp < count + DIGIT_MAX; ++countp)
119*e4b17023SJohn Marino 	*countp += countp[-1];
120*e4b17023SJohn Marino 
121*e4b17023SJohn Marino       /* Now, drop the pointers into their correct locations.  */
122*e4b17023SJohn Marino       for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp)
123*e4b17023SJohn Marino 	work[--count[((digit_t *) pointerp)[j]]] = *pointerp;
124*e4b17023SJohn Marino 
125*e4b17023SJohn Marino       /* Swap WORK and POINTERS so that POINTERS contains the sorted
126*e4b17023SJohn Marino 	 array.  */
127*e4b17023SJohn Marino       pointerp = pointers;
128*e4b17023SJohn Marino       pointers = work;
129*e4b17023SJohn Marino       work = pointerp;
130*e4b17023SJohn Marino     }
131*e4b17023SJohn Marino }
132*e4b17023SJohn Marino 
133*e4b17023SJohn Marino /* Everything below here is a unit test for the routines in this
134*e4b17023SJohn Marino    file.  */
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino #ifdef UNIT_TEST
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino #include <stdio.h>
139*e4b17023SJohn Marino 
xmalloc(size_t n)140*e4b17023SJohn Marino void *xmalloc (size_t n)
141*e4b17023SJohn Marino {
142*e4b17023SJohn Marino   return malloc (n);
143*e4b17023SJohn Marino }
144*e4b17023SJohn Marino 
main(int argc,char ** argv)145*e4b17023SJohn Marino int main (int argc, char **argv)
146*e4b17023SJohn Marino {
147*e4b17023SJohn Marino   int k;
148*e4b17023SJohn Marino   int result;
149*e4b17023SJohn Marino   size_t i;
150*e4b17023SJohn Marino   void **pointers;
151*e4b17023SJohn Marino   void **work;
152*e4b17023SJohn Marino 
153*e4b17023SJohn Marino   if (argc > 1)
154*e4b17023SJohn Marino     k = atoi (argv[1]);
155*e4b17023SJohn Marino   else
156*e4b17023SJohn Marino     k = 10;
157*e4b17023SJohn Marino 
158*e4b17023SJohn Marino   pointers = XNEWVEC (void*, k);
159*e4b17023SJohn Marino   work = XNEWVEC (void*, k);
160*e4b17023SJohn Marino 
161*e4b17023SJohn Marino   for (i = 0; i < k; ++i)
162*e4b17023SJohn Marino     {
163*e4b17023SJohn Marino       pointers[i] = (void *) random ();
164*e4b17023SJohn Marino       printf ("%x\n", pointers[i]);
165*e4b17023SJohn Marino     }
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino   sort_pointers (k, pointers, work);
168*e4b17023SJohn Marino 
169*e4b17023SJohn Marino   printf ("\nSorted\n\n");
170*e4b17023SJohn Marino 
171*e4b17023SJohn Marino   result = 0;
172*e4b17023SJohn Marino 
173*e4b17023SJohn Marino   for (i = 0; i < k; ++i)
174*e4b17023SJohn Marino     {
175*e4b17023SJohn Marino       printf ("%x\n", pointers[i]);
176*e4b17023SJohn Marino       if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1])
177*e4b17023SJohn Marino 	result = 1;
178*e4b17023SJohn Marino     }
179*e4b17023SJohn Marino 
180*e4b17023SJohn Marino   free (pointers);
181*e4b17023SJohn Marino   free (work);
182*e4b17023SJohn Marino 
183*e4b17023SJohn Marino   return result;
184*e4b17023SJohn Marino }
185*e4b17023SJohn Marino 
186*e4b17023SJohn Marino #endif
187