This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PATCH: New libiberty sorting routine
- To: gcc-patches at gcc dot gnu dot org
- Subject: PATCH: New libiberty sorting routine
- From: Mark Mitchell <mark at codesourcery dot com>
- Date: Sun, 23 Apr 2000 18:01:30 -0700
- Reply-to: mark at codesourcery dot com
This patch adds a routine to libiberty to do linear-time sorting of
pointers, using radix sort with counting sort as the subordinate
stable sorting algorithm.
Unfortunately, it turns out not to be useful for the thing I thought
it would be useful for in GCC (at least not yet!), but it's still
likely to be useful to somebody else at some point.
--
Mark Mitchell mark@codesourcery.com
CodeSourcery, LLC http://www.codesourcery.com
2000-04-23 Mark Mitchell <mark@codesourcery.com>
* sort.h: New file.
2000-04-23 Mark Mitchell <mark@codesourcery.com>
* sort.c: New file.
* Makefile.in (CFILES): Add sort.c
(REQUIRED_OFILES): Add sort.o.
(sort.o): New target.
Index: sort.h
===================================================================
RCS file: sort.h
diff -N sort.h
*** /dev/null Tue May 5 13:32:27 1998
--- sort.h Sun Apr 23 17:47:16 2000
***************
*** 0 ****
--- 1,45 ----
+ /* Sorting algorithms.
+ Copyright (C) 2000 Free Software Foundation, Inc.
+ Contributed by Mark Mitchell <mark@codesourcery.com>.
+
+ This file is part of GNU CC.
+
+ GNU CC is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU CC is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU CC; see the file COPYING. If not, write to
+ the Free Software Foundation, 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+ #ifndef SORT_H
+ #define SORT_H
+
+ #include <stddef.h>
+
+ #ifdef __cplusplus
+ extern "C" {
+ #endif /* __cplusplus */
+
+ #include <ansidecl.h>
+
+ /* Sort an array of pointers. */
+
+ extern void sort_pointers PARAMS ((size_t, void **, void **));
+
+ #ifdef __cplusplus
+ }
+ #endif /* __cplusplus */
+
+ #endif /* SORT_H */
+
+
+
+
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/egcs/libiberty/Makefile.in,v
retrieving revision 1.39
diff -c -p -r1.39 Makefile.in
*** Makefile.in 2000/03/24 21:32:08 1.39
--- Makefile.in 2000/04/24 00:47:23
*************** CFILES = asprintf.c alloca.c argv.c atex
*** 129,135 ****
getruntime.c floatformat.c hashtab.c hex.c index.c insque.c memchr.c \
memcmp.c memcpy.c memmove.c memset.c mkstemps.c objalloc.c obstack.c \
partition.c pexecute.c putenv.c random.c rename.c rindex.c \
! setenv.c sigsetmask.c spaces.c splay-tree.c strcasecmp.c \
strncasecmp.c strchr.c strdup.c strerror.c strrchr.c \
strsignal.c strstr.c strtod.c strtol.c strtoul.c tmpnam.c \
vasprintf.c vfork.c vfprintf.c vprintf.c vsprintf.c waitpid.c \
--- 129,135 ----
getruntime.c floatformat.c hashtab.c hex.c index.c insque.c memchr.c \
memcmp.c memcpy.c memmove.c memset.c mkstemps.c objalloc.c obstack.c \
partition.c pexecute.c putenv.c random.c rename.c rindex.c \
! setenv.c sigsetmask.c sort.c spaces.c splay-tree.c strcasecmp.c \
strncasecmp.c strchr.c strdup.c strerror.c strrchr.c \
strsignal.c strstr.c strtod.c strtol.c strtoul.c tmpnam.c \
vasprintf.c vfork.c vfprintf.c vprintf.c vsprintf.c waitpid.c \
*************** CFILES = asprintf.c alloca.c argv.c atex
*** 138,145 ****
# These are always included in the library.
REQUIRED_OFILES = argv.o choose-temp.o concat.o cplus-dem.o \
fdmatch.o fnmatch.o getopt.o getopt1.o getpwd.o getruntime.o hashtab.o \
! hex.o floatformat.o objalloc.o obstack.o partition.o pexecute.o spaces.o \
! splay-tree.o strerror.o strsignal.o xatexit.o xexit.o xmalloc.o \
xmemdup.o xstrdup.o xstrerror.o
$(TARGETLIB): $(REQUIRED_OFILES) $(EXTRA_OFILES) $(LIBOBJS) $(ALLOCA)
--- 138,145 ----
# These are always included in the library.
REQUIRED_OFILES = argv.o choose-temp.o concat.o cplus-dem.o \
fdmatch.o fnmatch.o getopt.o getopt1.o getpwd.o getruntime.o hashtab.o \
! hex.o floatformat.o objalloc.o obstack.o partition.o pexecute.o sort.o \
! spaces.o splay-tree.o strerror.o strsignal.o xatexit.o xexit.o xmalloc.o \
xmemdup.o xstrdup.o xstrerror.o
$(TARGETLIB): $(REQUIRED_OFILES) $(EXTRA_OFILES) $(LIBOBJS) $(ALLOCA)
*************** obstack.o: config.h $(INCDIR)/obstack.h
*** 275,280 ****
--- 275,281 ----
partition.o: config.h $(INCDIR)/partition.h
pexecute.o: config.h $(INCDIR)/libiberty.h
setenv.o: config.h
+ sort.o: config.h $(INCDIR)/sort.h $(INCDIR)/ansidecl.h
spaces.o: $(INCDIR)/libiberty.h
splay-tree.o: config.h $(INCDIR)/libiberty.h $(INCDIR)/splay-tree.h $(INCDIR)/ansidecl.h
strerror.o: config.h $(INCDIR)/libiberty.h
Index: sort.c
===================================================================
RCS file: sort.c
diff -N sort.c
*** /dev/null Tue May 5 13:32:27 1998
--- sort.c Sun Apr 23 17:47:25 2000
***************
*** 0 ****
--- 1,176 ----
+ /* Sorting algorithms.
+ Copyright (C) 2000 Free Software Foundation, Inc.
+ Contributed by Mark Mitchell <mark@codesourcery.com>.
+
+ This file is part of GNU CC.
+
+ GNU CC is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU CC is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU CC; see the file COPYING. If not, write to
+ the Free Software Foundation, 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+ #ifdef HAVE_CONFIG_H
+ #include "config.h"
+ #endif
+ #include "libiberty.h"
+ #include "sort.h"
+ #include <limits.h>
+ #ifdef HAVE_STDLIB_H
+ #include <stdlib.h>
+ #endif
+
+ /* POINTERSP and WORKP both point to arrays of N pointers. When
+ this function returns POINTERSP will point to a sorted version of
+ the original array pointed to by POINTERSP. */
+
+ void sort_pointers (n, pointers, work)
+ size_t n;
+ void **pointers;
+ void **work;
+ {
+ /* The type of a single digit. This can be any unsigned integral
+ type. When changing this, DIGIT_MAX should be changed as
+ well. */
+ typedef unsigned char digit_t;
+
+ /* The maximum value a single digit can have. */
+ #define DIGIT_MAX (UCHAR_MAX + 1)
+
+ /* The Ith entry is the number of elements in *POINTERSP that have I
+ in the digit on which we are currently sorting. */
+ unsigned int count[DIGIT_MAX];
+ /* Nonzero if we are running on a big-endian machine. */
+ int big_endian_p;
+ size_t i;
+ size_t j;
+
+ /* The algorithm used here is radix sort which takes time linear in
+ the number of elements in the array. */
+
+ /* The algorithm here depends on being able to swap the two arrays
+ an even number of times. */
+ if ((sizeof (void *) / sizeof (digit_t)) % 2 != 0)
+ abort ();
+
+ /* Figure out the endianness of the machine. */
+ for (i = 0; i < sizeof (size_t); ++i)
+ ((char *)&j)[i] = i;
+ big_endian_p = (((char *)&j)[0] == 0);
+
+ /* Move through the pointer values from least significant to most
+ significant digits. */
+ for (i = 0; i < sizeof (void *) / sizeof (digit_t); ++i)
+ {
+ digit_t *digit;
+ digit_t *bias;
+ digit_t *top;
+ unsigned int *countp;
+ void **pointerp;
+
+ /* The offset from the start of the pointer will depend on the
+ endianness of the machine. */
+ if (big_endian_p)
+ j = sizeof (void *) / sizeof (digit_t) - i;
+ else
+ j = i;
+
+ /* Now, perform a stable sort on this digit. We use counting
+ sort. */
+ memset (count, 0, DIGIT_MAX * sizeof (unsigned int));
+
+ /* Compute the address of the appropriate digit in the first and
+ one-past-the-end elements of the array. On a little-endian
+ machine, the least-significant digit is closest to the front. */
+ bias = ((digit_t *) pointers) + i;
+ top = ((digit_t *) (pointers + n)) + i;
+
+ /* Count how many there are of each value. At the end of this
+ loop, COUNT[K] will contain the number of pointers whose Ith
+ digit is K. */
+ for (digit = bias;
+ digit < top;
+ digit += sizeof (void *) / sizeof (digit_t))
+ ++count[*digit];
+
+ /* Now, make COUNT[K] contain the number of pointers whose Ith
+ digit is less than or equal to K. */
+ for (countp = count + 1; countp < count + DIGIT_MAX; ++countp)
+ *countp += countp[-1];
+
+ /* Now, drop the pointers into their correct locations. */
+ for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp)
+ work[--count[((digit_t *) pointerp)[i]]] = *pointerp;
+
+ /* Swap WORK and POINTERS so that POINTERS contains the sorted
+ array. */
+ pointerp = pointers;
+ pointers = work;
+ work = pointerp;
+ }
+ }
+
+ /* Everything below here is a unit test for the routines in this
+ file. */
+
+ #ifdef UNIT_TEST
+
+ #include <stdio.h>
+
+ void *xmalloc (n)
+ size_t n;
+ {
+ return malloc (n);
+ }
+
+ int main (int argc, char **argv)
+ {
+ int k;
+ int result;
+ size_t i;
+ void **pointers;
+ void **work;
+
+ if (argc > 1)
+ k = atoi (argv[1]);
+ else
+ k = 10;
+
+ pointers = xmalloc (k * sizeof (void *));
+ work = xmalloc (k * sizeof (void *));
+
+ for (i = 0; i < k; ++i)
+ {
+ pointers[i] = (void *) random ();
+ printf ("%x\n", pointers[i]);
+ }
+
+ sort_pointers (k, pointers, work);
+
+ printf ("\nSorted\n\n");
+
+ result = 0;
+
+ for (i = 0; i < k; ++i)
+ {
+ printf ("%x\n", pointers[i]);
+ if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1])
+ result = 1;
+ }
+
+ free (pointers);
+ free (work);
+
+ return result;
+ }
+
+ #endif