]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/sort.c
sort.c: New file.
[thirdparty/gcc.git] / libiberty / sort.c
CommitLineData
eeb0656f
MM
1/* Sorting algorithms.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell <mark@codesourcery.com>.
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25#include "libiberty.h"
26#include "sort.h"
27#include <limits.h>
28#ifdef HAVE_STDLIB_H
29#include <stdlib.h>
30#endif
31
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. */
35
36void sort_pointers (n, pointers, work)
37 size_t n;
38 void **pointers;
39 void **work;
40{
41 /* The type of a single digit. This can be any unsigned integral
42 type. When changing this, DIGIT_MAX should be changed as
43 well. */
44 typedef unsigned char digit_t;
45
46 /* The maximum value a single digit can have. */
47#define DIGIT_MAX (UCHAR_MAX + 1)
48
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. */
53 int big_endian_p;
54 size_t i;
55 size_t j;
56
57 /* The algorithm used here is radix sort which takes time linear in
58 the number of elements in the array. */
59
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)
63 abort ();
64
65 /* Figure out the endianness of the machine. */
66 for (i = 0; i < sizeof (size_t); ++i)
67 ((char *)&j)[i] = i;
68 big_endian_p = (((char *)&j)[0] == 0);
69
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)
73 {
74 digit_t *digit;
75 digit_t *bias;
76 digit_t *top;
77 unsigned int *countp;
78 void **pointerp;
79
80 /* The offset from the start of the pointer will depend on the
81 endianness of the machine. */
82 if (big_endian_p)
83 j = sizeof (void *) / sizeof (digit_t) - i;
84 else
85 j = i;
86
87 /* Now, perform a stable sort on this digit. We use counting
88 sort. */
89 memset (count, 0, DIGIT_MAX * sizeof (unsigned int));
90
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;
96
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
99 digit is K. */
100 for (digit = bias;
101 digit < top;
102 digit += sizeof (void *) / sizeof (digit_t))
103 ++count[*digit];
104
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];
109
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;
113
114 /* Swap WORK and POINTERS so that POINTERS contains the sorted
115 array. */
116 pointerp = pointers;
117 pointers = work;
118 work = pointerp;
119 }
120}
121
122/* Everything below here is a unit test for the routines in this
123 file. */
124
125#ifdef UNIT_TEST
126
127#include <stdio.h>
128
129void *xmalloc (n)
130 size_t n;
131{
132 return malloc (n);
133}
134
135int main (int argc, char **argv)
136{
137 int k;
138 int result;
139 size_t i;
140 void **pointers;
141 void **work;
142
143 if (argc > 1)
144 k = atoi (argv[1]);
145 else
146 k = 10;
147
148 pointers = xmalloc (k * sizeof (void *));
149 work = xmalloc (k * sizeof (void *));
150
151 for (i = 0; i < k; ++i)
152 {
153 pointers[i] = (void *) random ();
154 printf ("%x\n", pointers[i]);
155 }
156
157 sort_pointers (k, pointers, work);
158
159 printf ("\nSorted\n\n");
160
161 result = 0;
162
163 for (i = 0; i < k; ++i)
164 {
165 printf ("%x\n", pointers[i]);
166 if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1])
167 result = 1;
168 }
169
170 free (pointers);
171 free (work);
172
173 return result;
174}
175
176#endif