OSDN Git Service

* sort.h (sys/types.h): File included unconditionnaly.
[pf3gnuchains/gcc-fork.git] / libiberty / sort.c
1 /* Sorting algorithms.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    Contributed by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GNU CC.
6    
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)
10 any later version.
11
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.
16
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.  */
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 #ifndef UCHAR_MAX
33 #define UCHAR_MAX ((unsigned char)(-1))
34 #endif
35
36 /* POINTERS and WORK are both arrays of N pointers.  When this
37    function returns POINTERS will be sorted in ascending order.  */
38
39 void sort_pointers (n, pointers, work)
40      size_t n;
41      void **pointers;
42      void **work;
43 {
44   /* The type of a single digit.  This can be any unsigned integral
45      type.  When changing this, DIGIT_MAX should be changed as 
46      well.  */
47   typedef unsigned char digit_t;
48
49   /* The maximum value a single digit can have.  */
50 #define DIGIT_MAX (UCHAR_MAX + 1)
51
52   /* The Ith entry is the number of elements in *POINTERSP that have I
53      in the digit on which we are currently sorting.  */
54   unsigned int count[DIGIT_MAX];
55   /* Nonzero if we are running on a big-endian machine.  */
56   int big_endian_p;
57   size_t i;
58   size_t j;
59
60   /* The algorithm used here is radix sort which takes time linear in
61      the number of elements in the array.  */
62
63   /* The algorithm here depends on being able to swap the two arrays
64      an even number of times.  */
65   if ((sizeof (void *) / sizeof (digit_t)) % 2 != 0)
66     abort ();
67
68   /* Figure out the endianness of the machine.  */
69   for (i = 0, j = 0; i < sizeof (size_t); ++i)
70     {
71       j *= (UCHAR_MAX + 1);
72       j += i;
73     }
74   big_endian_p = (((char *)&j)[0] == 0);
75
76   /* Move through the pointer values from least significant to most
77      significant digits.  */
78   for (i = 0; i < sizeof (void *) / sizeof (digit_t); ++i)
79     {
80       digit_t *digit;
81       digit_t *bias;
82       digit_t *top;
83       unsigned int *countp;
84       void **pointerp;
85
86       /* The offset from the start of the pointer will depend on the
87          endianness of the machine.  */
88       if (big_endian_p)
89         j = sizeof (void *) / sizeof (digit_t) - i;
90       else
91         j = i;
92         
93       /* Now, perform a stable sort on this digit.  We use counting
94          sort.  */
95       memset (count, 0, DIGIT_MAX * sizeof (unsigned int));
96
97       /* Compute the address of the appropriate digit in the first and
98          one-past-the-end elements of the array.  On a little-endian
99          machine, the least-significant digit is closest to the front.  */
100       bias = ((digit_t *) pointers) + j;
101       top = ((digit_t *) (pointers + n)) + j;
102
103       /* Count how many there are of each value.  At the end of this
104          loop, COUNT[K] will contain the number of pointers whose Ith
105          digit is K.  */
106       for (digit = bias; 
107            digit < top; 
108            digit += sizeof (void *) / sizeof (digit_t))
109         ++count[*digit];
110
111       /* Now, make COUNT[K] contain the number of pointers whose Ith
112          digit is less than or equal to K.  */
113       for (countp = count + 1; countp < count + DIGIT_MAX; ++countp)
114         *countp += countp[-1];
115
116       /* Now, drop the pointers into their correct locations.  */
117       for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp)
118         work[--count[((digit_t *) pointerp)[j]]] = *pointerp;
119
120       /* Swap WORK and POINTERS so that POINTERS contains the sorted
121          array.  */
122       pointerp = pointers;
123       pointers = work;
124       work = pointerp;
125     }
126 }
127
128 /* Everything below here is a unit test for the routines in this
129    file.  */
130
131 #ifdef UNIT_TEST
132
133 #include <stdio.h>
134
135 void *xmalloc (n)
136      size_t n;
137 {
138   return malloc (n);
139 }
140
141 int main (int argc, char **argv)
142 {
143   int k;
144   int result;
145   size_t i;
146   void **pointers;
147   void **work;
148
149   if (argc > 1)
150     k = atoi (argv[1]);
151   else
152     k = 10;
153
154   pointers = xmalloc (k * sizeof (void *));
155   work = xmalloc (k * sizeof (void *));
156
157   for (i = 0; i < k; ++i)
158     {
159       pointers[i] = (void *) random ();
160       printf ("%x\n", pointers[i]);
161     }
162
163   sort_pointers (k, pointers, work);
164
165   printf ("\nSorted\n\n");
166
167   result = 0;
168
169   for (i = 0; i < k; ++i)
170     {
171       printf ("%x\n", pointers[i]);
172       if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1])
173         result = 1;
174     }
175
176   free (pointers);
177   free (work);
178
179   return result;
180 }
181
182 #endif