2000-04-24 02:51:08 +02:00
|
|
|
/* Sorting algorithms.
|
2021-01-04 10:26:59 +01:00
|
|
|
Copyright (C) 2000-2021 Free Software Foundation, Inc.
|
2000-04-24 02:51:08 +02:00
|
|
|
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
|
2005-05-10 17:33:18 +02:00
|
|
|
the Free Software Foundation, 51 Franklin Street - Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA. */
|
2000-04-24 02:51:08 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
#include "libiberty.h"
|
|
|
|
#include "sort.h"
|
2000-07-23 21:13:46 +02:00
|
|
|
#ifdef HAVE_LIMITS_H
|
2000-04-24 02:51:08 +02:00
|
|
|
#include <limits.h>
|
2000-07-23 21:13:46 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2000-04-24 02:51:08 +02:00
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
2000-05-29 21:33:52 +02:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
2000-04-24 02:51:08 +02:00
|
|
|
|
2000-05-04 17:40:25 +02:00
|
|
|
#ifndef UCHAR_MAX
|
|
|
|
#define UCHAR_MAX ((unsigned char)(-1))
|
|
|
|
#endif
|
|
|
|
|
2000-04-24 07:52:07 +02:00
|
|
|
/* POINTERS and WORK are both arrays of N pointers. When this
|
|
|
|
function returns POINTERS will be sorted in ascending order. */
|
2000-04-24 02:51:08 +02:00
|
|
|
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
void sort_pointers (size_t n, void **pointers, void **work)
|
2000-04-24 02:51:08 +02:00
|
|
|
{
|
|
|
|
/* 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. */
|
2000-04-24 07:52:07 +02:00
|
|
|
for (i = 0, j = 0; i < sizeof (size_t); ++i)
|
|
|
|
{
|
|
|
|
j *= (UCHAR_MAX + 1);
|
|
|
|
j += i;
|
|
|
|
}
|
2000-04-24 02:51:08 +02:00
|
|
|
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. */
|
2000-04-24 07:52:07 +02:00
|
|
|
bias = ((digit_t *) pointers) + j;
|
|
|
|
top = ((digit_t *) (pointers + n)) + j;
|
2000-04-24 02:51:08 +02:00
|
|
|
|
|
|
|
/* 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)
|
2000-04-24 07:52:07 +02:00
|
|
|
work[--count[((digit_t *) pointerp)[j]]] = *pointerp;
|
2000-04-24 02:51:08 +02:00
|
|
|
|
|
|
|
/* 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>
|
|
|
|
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
void *xmalloc (size_t n)
|
2000-04-24 02:51:08 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2005-05-24 22:48:25 +02:00
|
|
|
pointers = XNEWVEC (void*, k);
|
|
|
|
work = XNEWVEC (void*, k);
|
2000-04-24 02:51:08 +02:00
|
|
|
|
|
|
|
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
|