2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
32 /* POINTERSP and WORKP both point to arrays of N pointers. When
33 this function returns POINTERSP will point to a sorted version of
34 the original array pointed to by POINTERSP. */
36 void sort_pointers (n, pointers, work)
41 /* The type of a single digit. This can be any unsigned integral
42 type. When changing this, DIGIT_MAX should be changed as
44 typedef unsigned char digit_t;
46 /* The maximum value a single digit can have. */
47 #define DIGIT_MAX (UCHAR_MAX + 1)
49 /* The Ith entry is the number of elements in *POINTERSP that have I
50 in the digit on which we are currently sorting. */
51 unsigned int count[DIGIT_MAX];
52 /* Nonzero if we are running on a big-endian machine. */
57 /* The algorithm used here is radix sort which takes time linear in
58 the number of elements in the array. */
60 /* The algorithm here depends on being able to swap the two arrays
61 an even number of times. */
62 if ((sizeof (void *) / sizeof (digit_t)) % 2 != 0)
65 /* Figure out the endianness of the machine. */
66 for (i = 0; i < sizeof (size_t); ++i)
68 big_endian_p = (((char *)&j)[0] == 0);
70 /* Move through the pointer values from least significant to most
71 significant digits. */
72 for (i = 0; i < sizeof (void *) / sizeof (digit_t); ++i)
80 /* The offset from the start of the pointer will depend on the
81 endianness of the machine. */
83 j = sizeof (void *) / sizeof (digit_t) - i;
87 /* Now, perform a stable sort on this digit. We use counting
89 memset (count, 0, DIGIT_MAX * sizeof (unsigned int));
91 /* Compute the address of the appropriate digit in the first and
92 one-past-the-end elements of the array. On a little-endian
93 machine, the least-significant digit is closest to the front. */
94 bias = ((digit_t *) pointers) + i;
95 top = ((digit_t *) (pointers + n)) + i;
97 /* Count how many there are of each value. At the end of this
98 loop, COUNT[K] will contain the number of pointers whose Ith
102 digit += sizeof (void *) / sizeof (digit_t))
105 /* Now, make COUNT[K] contain the number of pointers whose Ith
106 digit is less than or equal to K. */
107 for (countp = count + 1; countp < count + DIGIT_MAX; ++countp)
108 *countp += countp[-1];
110 /* Now, drop the pointers into their correct locations. */
111 for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp)
112 work[--count[((digit_t *) pointerp)[i]]] = *pointerp;
114 /* Swap WORK and POINTERS so that POINTERS contains the sorted
122 /* Everything below here is a unit test for the routines in this
135 int main (int argc, char **argv)
148 pointers = xmalloc (k * sizeof (void *));
149 work = xmalloc (k * sizeof (void *));
151 for (i = 0; i < k; ++i)
153 pointers[i] = (void *) random ();
154 printf ("%x\n", pointers[i]);
157 sort_pointers (k, pointers, work);
159 printf ("\nSorted\n\n");
163 for (i = 0; i < k; ++i)
165 printf ("%x\n", pointers[i]);
166 if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1])