PandA-2024.02
qsort-specialized.c
Go to the documentation of this file.
1 /* Copyright (C) 2002 Manuel Novoa III
2  * From my (incomplete) stdlib library for linux and (soon) elks.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 
19 /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
20  *
21  * This code is currently under development. Also, I plan to port
22  * it to elks which is a 16-bit environment with a fairly limited
23  * compiler. Therefore, please refrain from modifying this code
24  * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
25  *
26  * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
27 
28 /* Oct 29, 2002
29  * Fix a couple of 'restrict' bugs in mbstowcs and wcstombs.
30  *
31  * Nov 21, 2002
32  * Add wscto{inttype} functions.
33  */
34 
35 #include <assert.h>
36 #include <unistd.h>
37 
38 
39 typedef int (*__compar_d_fn_t)(void *, void *, void *);
40 int less (void * a, void * b, void * notUsed);
41 
42 /* This code is derived from a public domain shell sort routine by
43  * Ray Gardner and found in Bob Stout's snippets collection. The
44  * original code is included below in an #if 0/#endif block.
45  *
46  * I modified it to avoid the possibility of overflow in the wgap
47  * calculation, as well as to reduce the generated code size with
48  * bcc and gcc. */
49 void qsort_r(void *base,
50  size_t nel,
51  size_t width,
52  int (*comp)(void *, void *, void*),
53  void *arg)
54 {
55  size_t wgap, i, j, k;
56  char tmp;
57 
58  if ((nel > 1) && (width > 0)) {
59  assert(nel <= ((size_t)(-1)) / width); /* check for overflow */
60  wgap = 0;
61  do {
62  wgap = 3 * wgap + 1;
63  } while (wgap < (nel-1)/3);
64  /* From the above, we know that either wgap == 1 < nel or */
65  /* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap < nel. */
66  wgap *= width; /* So this can not overflow if wnel doesn't. */
67  nel *= width; /* Convert nel to 'wnel' */
68  do {
69  i = wgap;
70  do {
71  j = i;
72  do {
73  register char *a;
74  register char *b;
75 
76  j -= wgap;
77  a = j + ((char *)base);
78  b = a + wgap;
79  if (less(a, b, arg) <= 0) {
80  break;
81  }
82  k = width;
83  do {
84  tmp = *a;
85  *a++ = *b;
86  *b++ = tmp;
87  } while (--k);
88  } while (j >= wgap);
89  i += width;
90  } while (i < nel);
91  wgap = (wgap - width)/3;
92  } while (wgap);
93  }
94 }
char base
This version is stamped on May 10, 2016.
Definition: nussinov.c:24
int less(const void *, const void *, void *)
Definition: less.c:4
static const uint32_t k[]
Definition: sha-256.c:22
int(* __compar_d_fn_t)(void *, void *, void *)
void qsort_r(void *base, size_t nel, size_t width, int(*comp)(void *, void *, void *), void *arg)

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13