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